11fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce/*
21fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * Copyright (C) 2014 The Android Open Source Project
31fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce *
41fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * Licensed under the Apache License, Version 2.0 (the "License");
51fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * you may not use this file except in compliance with the License.
61fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * You may obtain a copy of the License at
71fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce *
81fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce *      http://www.apache.org/licenses/LICENSE-2.0
91fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce *
101fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * Unless required by applicable law or agreed to in writing, software
111fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * distributed under the License is distributed on an "AS IS" BASIS,
121fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * See the License for the specific language governing permissions and
141fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce * limitations under the License.
151fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce */
161fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
171fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce//#define LOG_NDEBUG 0
181fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce#define LOG_TAG "ClearKeyCryptoPlugin"
191fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce#include <utils/Log.h>
201fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
211fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce#include <utils/String8.h>
221fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
231fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce#include "SessionLibrary.h"
241fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
251fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Brucenamespace clearkeydrm {
261fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
271fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruceusing android::Mutex;
281fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruceusing android::sp;
291fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruceusing android::String8;
301fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruceusing android::Vector;
311fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
321fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" BruceMutex SessionLibrary::sSingletonLock;
331fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" BruceSessionLibrary* SessionLibrary::sSingleton = NULL;
341fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
351fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" BruceSessionLibrary* SessionLibrary::get() {
361fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    Mutex::Autolock lock(sSingletonLock);
371fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
381fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    if (sSingleton == NULL) {
391fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce        ALOGD("Instantiating Session Library Singleton.");
401fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce        sSingleton = new SessionLibrary();
411fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    }
421fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
431fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    return sSingleton;
441fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce}
451fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
461fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruceconst sp<Session>& SessionLibrary::createSession() {
471fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    Mutex::Autolock lock(mSessionsLock);
481fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
491fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    String8 sessionIdString = String8::format("%u", mNextSessionId);
501fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    mNextSessionId += 1;
511fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    Vector<uint8_t> sessionId;
521fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    sessionId.appendArray(
531fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce            reinterpret_cast<const uint8_t*>(sessionIdString.string()),
541fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce            sessionIdString.size());
551fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
561fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    mSessions.add(sessionId, new Session(sessionId));
571fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    return mSessions.valueFor(sessionId);
581fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce}
591fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
601fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruceconst sp<Session>& SessionLibrary::findSession(
611fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce        const Vector<uint8_t>& sessionId) {
621fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    Mutex::Autolock lock(mSessionsLock);
631fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    return mSessions.valueFor(sessionId);
641fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce}
651fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
661fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Brucevoid SessionLibrary::destroySession(const sp<Session>& session) {
671fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    Mutex::Autolock lock(mSessionsLock);\
681fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce    mSessions.removeItem(session->sessionId());
691fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce}
701fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce
711fe11a5d1b7932a8a4a4e6e8cf1aedd21fcdb3aaJohn "Juce" Bruce} // namespace clearkeydrm
72