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
25namespace clearkeydrm {
26
27using android::Mutex;
28using android::sp;
29using android::String8;
30using android::Vector;
31
32Mutex SessionLibrary::sSingletonLock;
33SessionLibrary* SessionLibrary::sSingleton = NULL;
34
35SessionLibrary* 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
46const 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
60const sp<Session>& SessionLibrary::findSession(
61        const Vector<uint8_t>& sessionId) {
62    Mutex::Autolock lock(mSessionsLock);
63    return mSessions.valueFor(sessionId);
64}
65
66const sp<Session>& SessionLibrary::findSession(
67        const void* data, size_t size) {
68    Vector<uint8_t> sessionId;
69    sessionId.appendArray(reinterpret_cast<const uint8_t*>(data), size);
70    return findSession(sessionId);
71}
72
73void SessionLibrary::destroySession(const sp<Session>& session) {
74    Mutex::Autolock lock(mSessionsLock);\
75    mSessions.removeItem(session->sessionId());
76}
77
78} // namespace clearkeydrm
79