1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OHOS_DRM_KEY_SESSION_IMPL_H_
16 #define OHOS_DRM_KEY_SESSION_IMPL_H_
17 
18 #include <cstring>
19 #include <map>
20 #include <unordered_map>
21 #include "nocopyable.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "i_keysession_service.h"
25 #include "i_keysession_service_callback.h"
26 #include "drm_listener_stub.h"
27 #include "key_session_service_callback_stub.h"
28 #include "drm_death_recipient.h"
29 
30 namespace OHOS {
31 namespace DrmStandard {
32 namespace MediaKeySessionEvent {
33 const std::string EVENT_STR_KEY_NEEDED = "keyRequired";
34 const std::string EVENT_STR_KEY_EXPIRED = "keyExpired";
35 const std::string EVENT_STR_EXPIRATION_UPDATED = "expirationUpdate";
36 const std::string EVENT_STR_KEY_CHANGED = "keysChange";
37 const std::string EVENT_STR_VENDOR_DEFINED = "vendorDefined";
38 }
39 
40 class MediaKeySessionImplCallback : public RefBase {
41 public:
42     MediaKeySessionImplCallback() = default;
43     virtual ~MediaKeySessionImplCallback() = default;
44     virtual void SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data) = 0;
45     virtual void SendEventKeyChanged(std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable,
46         bool hasNewGoodLicense) = 0;
47 };
48 
49 class MediaKeySessionImpl : public RefBase {
50 public:
51     explicit MediaKeySessionImpl(sptr<IMediaKeySessionService> &keySession);
52     ~MediaKeySessionImpl();
53     int32_t Release();
54     int32_t Init();
55 
56     int32_t GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo,
57         IMediaKeySessionService::MediaKeyRequest &licenseRequest);
58     int32_t ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &licenseResponse);
59     int32_t GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseRequest);
60     int32_t ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseReponse);
61     int32_t CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus);
62     int32_t RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId);
63 
64     int32_t ClearMediaKeys();
65 
66     int32_t GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel);
67 
68     sptr<IMediaKeySessionService> GetMediaKeySessionServiceProxy();
69     sptr<MediaKeySessionImplCallback> GetApplicationCallback();
70     int32_t SetCallback(const sptr<MediaKeySessionImplCallback> &callback);
71 
72     int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status);
73 
74 private:
75     void MediaKeySessionServerDied(pid_t pid);
76     sptr<MediaKeySessionImplCallback> keySessionApplicationCallback_;
77     sptr<IMediaKeySessionServiceCallback> keySessionServiceCallback_;
78     sptr<OHOS::DrmStandard::IMediaKeySessionService> keySessionServiceProxy_;
79     std::recursive_mutex mutex_;
80     sptr<DrmDeathRecipient> deathRecipient_ = nullptr;
81 };
82 
83 class MediaKeySessionServiceCallback : public MediaKeySessionServiceCallbackStub {
84 public:
MediaKeySessionServiceCallback()85     MediaKeySessionServiceCallback() : keySessionImpl_(nullptr)
86     {
87         InitEventMap();
88     };
MediaKeySessionServiceCallback(MediaKeySessionImpl * keySessionImpl)89     explicit MediaKeySessionServiceCallback(MediaKeySessionImpl *keySessionImpl)
90         : keySessionImpl_(keySessionImpl)
91     {
92         InitEventMap();
93     }
94 
~MediaKeySessionServiceCallback()95     ~MediaKeySessionServiceCallback()
96     {
97         std::lock_guard<std::recursive_mutex> lock(mutex_);
98         keySessionImpl_ = nullptr;
99     }
100 
101     void InitEventMap();
102     std::string GetEventName(DrmEventType event);
103     int32_t SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data) override;
104     int32_t SendEventKeyChanged(std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable,
105         bool hasNewGoodLicense) override;
106 
107 private:
108     std::recursive_mutex mutex_;
109     MediaKeySessionImpl *keySessionImpl_;
110     std::unordered_map<int32_t, std::string> eventMap_;
111 };
112 } // DrmStandard
113 } // OHOS
114 
115 
116 #endif