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_H
16 #define OHOS_DRM_KEY_SESSION_H
17 
18 #include <map>
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "drm_log.h"
22 #include "key_session_callback_napi.h"
23 #include "key_session_impl.h"
24 #include "napi_param_utils.h"
25 #include "napi_async_work.h"
26 
27 namespace OHOS {
28 namespace DrmStandard {
29 static const char KEY_SESSION_NAPI_CLASS_NAME[] = "MediaKeySession";
30 
31 struct MediaKeySessionAsyncContext : public ContextBase {
32     int32_t intValue;
33     std::vector<uint8_t> response;
34     std::vector<uint8_t> licenseId;
35     std::vector<uint8_t> releaseRequest;
36     std::vector<uint8_t> releaseLicenseId;
37     std::vector<uint8_t> restoreLicenseId;
38     std::vector<uint8_t> releaseResponse;
39     std::vector<uint8_t> releaseResponseLicenseId;
40     IMediaKeySessionService::MediaKeyRequestInfo mediaKeyRequestInfo;
41     IMediaKeySessionService::MediaKeyRequest mediaKeyRequest;
42 };
43 
44 class MediaKeySessionNapi {
45 public:
46     MediaKeySessionNapi();
47     ~MediaKeySessionNapi();
48     static napi_value Init(napi_env env, napi_value exports);
49     static napi_value CreateMediaKeySession(napi_env env, sptr<MediaKeySessionImpl> keySessionImpl);
50     static napi_value GenerateMediaKeyRequest(napi_env env, napi_callback_info info);
51     static napi_value ProcessMediaKeyResponse(napi_env env, napi_callback_info info);
52     static napi_value GenerateOfflineReleaseRequest(napi_env env, napi_callback_info info);
53     static napi_value ProcessOfflineReleaseResponse(napi_env env, napi_callback_info info);
54     static napi_value CheckMediaKeyStatus(napi_env env, napi_callback_info info);
55     static napi_value RestoreOfflineMediaKeys(napi_env env, napi_callback_info info);
56     static napi_value ClearMediaKeys(napi_env env, napi_callback_info info);
57     static napi_value GetContentProtectionLevel(napi_env env, napi_callback_info info);
58     static napi_value GetDecryptModule(napi_env env, napi_callback_info info);
59     static napi_value AddEventListener(napi_env env, napi_callback_info info);
60     static napi_value DeleteEventListener(napi_env env, napi_callback_info info);
61     static napi_value RequireSecureDecoderModule(napi_env env, napi_callback_info info);
62     static napi_value SetEventCallback(napi_env env, napi_callback_info info);
63     static napi_value UnsetEventCallback(napi_env env, napi_callback_info info);
64     static bool SetMediaKeySessionNativeProperty(napi_env env, napi_value obj, const std::string &name,
65         sptr<MediaKeySessionImpl> keySessionImpl);
66     void SetEventCallbackReference(const std::string eventType, std::shared_ptr<AutoRef> callbackPair);
67     void ClearEventCallbackReference(const std::string eventType);
68     static napi_value Destroy(napi_env env, napi_callback_info info);
69     static bool CheckMediaKeySessionStatus(MediaKeySessionNapi *napi,
70         std::shared_ptr<MediaKeySessionAsyncContext> context);
71     static bool CheckContextStatus(std::shared_ptr<MediaKeySessionAsyncContext> context);
72 
73 private:
74     static napi_value MediaKeySessionNapiConstructor(napi_env env, napi_callback_info info);
75     static void MediaKeySessionNapiDestructor(napi_env env, void *nativeObject, void *finalize);
76 
77     napi_env env_;
78     napi_ref wrapper_;
79     static thread_local napi_ref sConstructor_;
80     std::mutex mutex_;
81     static thread_local sptr<MediaKeySessionImpl> sMediaKeySessionImpl_;
82     sptr<MediaKeySessionImpl> keySessionImpl_;
83     sptr<MediaKeySessionCallbackNapi> keySessionCallbackNapi_;
84 };
85 } // DrmStandard
86 } // OHOS
87 
88 
89 #endif // OHOS_DRM_KEY_SESSION_H_