1 /* 2 * Copyright (C) 2015 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 #ifndef DRM_SESSION_MANAGER_H_ 18 19 #define DRM_SESSION_MANAGER_H_ 20 21 #include <aidl/android/media/IResourceManagerClient.h> 22 #include <aidl/android/media/IResourceManagerService.h> 23 #include <android/binder_auto_utils.h> 24 #include <media/stagefright/foundation/ABase.h> 25 #include <utils/RefBase.h> 26 #include <utils/KeyedVector.h> 27 #include <utils/threads.h> 28 #include <utils/Vector.h> 29 30 #include <map> 31 #include <memory> 32 #include <utility> 33 #include <vector> 34 35 namespace android { 36 37 class DrmSessionManagerTest; 38 39 using aidl::android::media::IResourceManagerClient; 40 using aidl::android::media::IResourceManagerService; 41 42 bool isEqualSessionId(const Vector<uint8_t> &sessionId1, const Vector<uint8_t> &sessionId2); 43 44 struct SessionInfo { 45 pid_t pid; 46 uid_t uid; 47 int64_t clientId; 48 }; 49 50 typedef std::map<std::vector<uint8_t>, SessionInfo> SessionInfoMap; 51 52 struct DrmSessionManager : public RefBase { 53 static sp<DrmSessionManager> Instance(); 54 55 DrmSessionManager(); 56 explicit DrmSessionManager(const std::shared_ptr<IResourceManagerService> &service); 57 58 void addSession(int pid, 59 const std::shared_ptr<IResourceManagerClient>& drm, 60 const Vector<uint8_t>& sessionId); 61 void useSession(const Vector<uint8_t>& sessionId); 62 void removeSession(const Vector<uint8_t>& sessionId); 63 bool reclaimSession(int callingPid); 64 65 // inspection APIs 66 size_t getSessionCount() const; 67 bool containsSession(const Vector<uint8_t>& sessionId) const; 68 69 // implements DeathRecipient 70 void binderDied(); 71 72 protected: 73 virtual ~DrmSessionManager(); 74 75 private: 76 void init(); 77 78 std::shared_ptr<IResourceManagerService> mService; 79 mutable Mutex mLock; 80 SessionInfoMap mSessionMap; 81 bool mInitialized; 82 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 83 84 DISALLOW_EVIL_CONSTRUCTORS(DrmSessionManager); 85 }; 86 87 } // namespace android 88 89 #endif // DRM_SESSION_MANAGER_H_ 90