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