1/*
2 * Copyright (C) 2017 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 "ClearKeySessionLibrary"
19#include <utils/Log.h>
20
21#include <media/stagefright/foundation/ABuffer.h>
22#include <media/stagefright/foundation/ADebug.h>
23
24#include "ClearKeySessionLibrary.h"
25
26namespace android {
27namespace clearkeycas {
28
29Mutex ClearKeySessionLibrary::sSingletonLock;
30ClearKeySessionLibrary* ClearKeySessionLibrary::sSingleton = NULL;
31
32ClearKeyCasSession::ClearKeyCasSession(CasPlugin *plugin)
33    : mPlugin(plugin) {
34    mKeyInfo[0].valid = mKeyInfo[1].valid = false;
35}
36
37ClearKeyCasSession::~ClearKeyCasSession() {
38}
39
40ClearKeySessionLibrary* ClearKeySessionLibrary::get() {
41    Mutex::Autolock lock(sSingletonLock);
42
43    if (sSingleton == NULL) {
44        ALOGV("Instantiating Session Library Singleton.");
45        sSingleton = new ClearKeySessionLibrary();
46    }
47
48    return sSingleton;
49}
50
51ClearKeySessionLibrary::ClearKeySessionLibrary() : mNextSessionId(1) {}
52
53status_t ClearKeySessionLibrary::addSession(
54        CasPlugin *plugin, CasSessionId *sessionId) {
55    CHECK(sessionId);
56
57    Mutex::Autolock lock(mSessionsLock);
58
59    sp<ClearKeyCasSession> session = new ClearKeyCasSession(plugin);
60
61    uint8_t *byteArray = (uint8_t *) &mNextSessionId;
62    sessionId->push_back(byteArray[3]);
63    sessionId->push_back(byteArray[2]);
64    sessionId->push_back(byteArray[1]);
65    sessionId->push_back(byteArray[0]);
66    mNextSessionId++;
67
68    mIDToSessionMap.add(*sessionId, session);
69    return OK;
70}
71
72sp<ClearKeyCasSession> ClearKeySessionLibrary::findSession(
73        const CasSessionId& sessionId) {
74    Mutex::Autolock lock(mSessionsLock);
75
76    ssize_t index = mIDToSessionMap.indexOfKey(sessionId);
77    if (index < 0) {
78        return NULL;
79    }
80    return mIDToSessionMap.valueFor(sessionId);
81}
82
83void ClearKeySessionLibrary::destroySession(const CasSessionId& sessionId) {
84    Mutex::Autolock lock(mSessionsLock);
85
86    ssize_t index = mIDToSessionMap.indexOfKey(sessionId);
87    if (index < 0) {
88        return;
89    }
90
91    sp<ClearKeyCasSession> session = mIDToSessionMap.valueAt(index);
92    mIDToSessionMap.removeItemsAt(index);
93}
94
95void ClearKeySessionLibrary::destroyPlugin(CasPlugin *plugin) {
96    Mutex::Autolock lock(mSessionsLock);
97
98    for (ssize_t index = mIDToSessionMap.size() - 1; index >= 0; index--) {
99        sp<ClearKeyCasSession> session = mIDToSessionMap.valueAt(index);
100        if (session->getPlugin() == plugin) {
101            mIDToSessionMap.removeItemsAt(index);
102        }
103    }
104}
105
106} // namespace clearkeycas
107} // namespace android
108