• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ClearKeyCryptoPlugin"
19 #include <utils/Log.h>
20 
21 #include <utils/String8.h>
22 
23 #include "SessionLibrary.h"
24 
25 namespace clearkeydrm {
26 
27 using android::Mutex;
28 using android::sp;
29 using android::String8;
30 using android::Vector;
31 
32 Mutex SessionLibrary::sSingletonLock;
33 SessionLibrary* SessionLibrary::sSingleton = NULL;
34 
get()35 SessionLibrary* SessionLibrary::get() {
36     Mutex::Autolock lock(sSingletonLock);
37 
38     if (sSingleton == NULL) {
39         ALOGD("Instantiating Session Library Singleton.");
40         sSingleton = new SessionLibrary();
41     }
42 
43     return sSingleton;
44 }
45 
createSession()46 sp<Session> SessionLibrary::createSession() {
47     Mutex::Autolock lock(mSessionsLock);
48 
49     String8 sessionIdString = String8::format("%u", mNextSessionId);
50     mNextSessionId += 1;
51     Vector<uint8_t> sessionId;
52     sessionId.appendArray(
53             reinterpret_cast<const uint8_t*>(sessionIdString.string()),
54             sessionIdString.size());
55 
56     mSessions.add(sessionId, new Session(sessionId));
57     return mSessions.valueFor(sessionId);
58 }
59 
findSession(const Vector<uint8_t> & sessionId)60 sp<Session> SessionLibrary::findSession(
61         const Vector<uint8_t>& sessionId) {
62     Mutex::Autolock lock(mSessionsLock);
63     if (mSessions.indexOfKey(sessionId) < 0) {
64         return sp<Session>(NULL);
65     }
66     return mSessions.valueFor(sessionId);
67 }
68 
destroySession(const sp<Session> & session)69 void SessionLibrary::destroySession(const sp<Session>& session) {
70     Mutex::Autolock lock(mSessionsLock);\
71     mSessions.removeItem(session->sessionId());
72 }
73 
74 } // namespace clearkeydrm
75