1 /*
2 * Copyright (C) 2021 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 #define LOG_TAG "clearkey-SessionLibrary"
17
18 #include <utils/Log.h>
19
20 #include "SessionLibrary.h"
21
22 namespace clearkeydrm {
23
24 using ::android::Mutex;
25 using ::android::sp;
26
27 Mutex SessionLibrary::sSingletonLock;
28 SessionLibrary* SessionLibrary::sSingleton = NULL;
29
get()30 SessionLibrary* SessionLibrary::get() {
31 Mutex::Autolock lock(sSingletonLock);
32
33 if (sSingleton == NULL) {
34 ALOGD("Instantiating Session Library Singleton.");
35 sSingleton = new SessionLibrary();
36 }
37
38 return sSingleton;
39 }
40
createSession()41 sp<Session> SessionLibrary::createSession() {
42 Mutex::Autolock lock(mSessionsLock);
43
44 char sessionIdRaw[16];
45 snprintf(sessionIdRaw, sizeof(sessionIdRaw), "%u", mNextSessionId);
46
47 mNextSessionId += 1;
48
49 std::vector<uint8_t> sessionId;
50 sessionId.insert(sessionId.end(), sessionIdRaw,
51 sessionIdRaw + sizeof(sessionIdRaw) / sizeof(uint8_t));
52
53 mSessions.insert(
54 std::pair<std::vector<uint8_t>, sp<Session>>(sessionId, new Session(sessionId)));
55 std::map<std::vector<uint8_t>, sp<Session>>::iterator itr = mSessions.find(sessionId);
56 if (itr != mSessions.end()) {
57 return itr->second;
58 } else {
59 return nullptr;
60 }
61 }
62
findSession(const std::vector<uint8_t> & sessionId)63 sp<Session> SessionLibrary::findSession(const std::vector<uint8_t>& sessionId) {
64 Mutex::Autolock lock(mSessionsLock);
65 std::map<std::vector<uint8_t>, sp<Session>>::iterator itr = mSessions.find(sessionId);
66 if (itr != mSessions.end()) {
67 return itr->second;
68 } else {
69 return nullptr;
70 }
71 }
72
destroySession(const sp<Session> & session)73 void SessionLibrary::destroySession(const sp<Session>& session) {
74 Mutex::Autolock lock(mSessionsLock);
75 mSessions.erase(session->sessionId());
76 }
77
78 } // namespace clearkeydrm
79