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