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 
16 #ifndef OHOS_DRM_MEDIAKEYSESSIONCALLBACKNAPI_H
17 #define OHOS_DRM_MEDIAKEYSESSIONCALLBACKNAPI_H
18 
19 #include <map>
20 #include "key_session_impl.h"
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "common_napi.h"
24 #include "napi_param_utils.h"
25 #include "napi_async_work.h"
26 
27 namespace OHOS {
28 namespace DrmStandard {
29 class MediaKeySessionCallbackNapi : public MediaKeySessionImplCallback {
30 public:
31     explicit MediaKeySessionCallbackNapi(napi_env env);
32     virtual ~MediaKeySessionCallbackNapi();
33     void SetCallbackReference(const std::string eventType, std::shared_ptr<AutoRef> callbackPair);
34     void ClearCallbackReference(const std::string eventType);
35     void SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data) override;
36     void SendEventKeyChanged(std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable,
37         bool hasNewGoodLicense) override;
38 
39 private:
40     struct MediaKeySessionJsCallback {
41         std::shared_ptr<AutoRef> callback = nullptr;
42         std::string callbackName = "unknown";
43         DrmEventParame eventParame;
44         DrmKeysChangeEventParame keysChangeParame;
45     };
46 
47     static void WorkCallbackStateChangeDone(uv_work_t *work, int status);
48     static void WorkCallbackInterruptDone(uv_work_t *work, int status);
49     void OnJsCallbackInterrupt(std::unique_ptr<MediaKeySessionJsCallback> &jsCb);
50     void OnJsCallbackStateChange(std::unique_ptr<MediaKeySessionJsCallback> &jsCb);
51 
52     napi_env env_ = nullptr;
53     std::mutex mutex_;
54     std::map<std::string, std::shared_ptr<AutoRef>> callbackMap_;
55 };
56 } // namespace DrmStandard
57 } // namespace OHOS
58 
59 
60 #endif