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 #include "drm_log.h"
16 #include "drm_error_code.h"
17 #include "media_key_system_callback_napi.h"
18 
19 namespace OHOS {
20 namespace DrmStandard {
MediaKeySystemCallbackNapi(napi_env env)21 MediaKeySystemCallbackNapi::MediaKeySystemCallbackNapi(napi_env env)
22 {
23     env_ = env;
24 }
25 
~MediaKeySystemCallbackNapi()26 MediaKeySystemCallbackNapi::~MediaKeySystemCallbackNapi() {}
27 
SetCallbackReference(const std::string eventType,std::shared_ptr<AutoRef> callbackPair)28 void MediaKeySystemCallbackNapi::SetCallbackReference(const std::string eventType,
29     std::shared_ptr<AutoRef> callbackPair)
30 {
31     DRM_INFO_LOG("MediaKeySystemCallbackNapi SetCallbackReference");
32     std::lock_guard<std::mutex> lock(mutex_);
33     callbackMap_[eventType] = callbackPair;
34 }
35 
ClearCallbackReference(const std::string eventType)36 void MediaKeySystemCallbackNapi::ClearCallbackReference(const std::string eventType)
37 {
38     DRM_INFO_LOG("MediaKeySystemCallbackNapi ClearCallbackReference");
39     std::lock_guard<std::mutex> lock(mutex_);
40     callbackMap_.erase(eventType);
41 }
42 
WorkCallbackInterruptDone(uv_work_t * work,int status)43 void MediaKeySystemCallbackNapi::WorkCallbackInterruptDone(uv_work_t *work, int status)
44 {
45     // Js Thread
46     std::shared_ptr<MediaKeySystemJsCallback> context(static_cast<MediaKeySystemJsCallback *>(work->data),
47         [work](MediaKeySystemJsCallback *ptr) {
48             delete ptr;
49             delete work;
50         });
51     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(work != nullptr, "work is nullptr");
52     MediaKeySystemJsCallback *event = reinterpret_cast<MediaKeySystemJsCallback *>(work->data);
53     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(event != nullptr, "event is nullptr");
54     std::string request = event->callbackName;
55 
56     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(event->callback != nullptr, "event is nullptr");
57     napi_env env = event->callback->env_;
58     napi_ref callback = event->callback->cb_;
59 
60     napi_handle_scope scope = nullptr;
61     napi_open_handle_scope(env, &scope);
62     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(scope != nullptr, "scope is nullptr");
63     DRM_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work_with_qos start", request.c_str());
64     do {
65         DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(status != UV_ECANCELED, "%{public}s cancelled", request.c_str());
66 
67         napi_value jsCallback = nullptr;
68         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
69         DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok && jsCallback != nullptr,
70             "%{public}s get reference value fail", request.c_str());
71 
72         // Call back function
73         napi_value args[ARGS_ONE] = { nullptr };
74         nstatus = NapiParamUtils::SetDrmEventInfo(env, event->eventParame, args[PARAM0]);
75         DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
76             "%{public}s fail to create keysystem callback", request.c_str());
77 
78         const size_t argCount = ARGS_ONE;
79         napi_value result = nullptr;
80         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
81         DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback",
82             request.c_str());
83     } while (0);
84     napi_close_handle_scope(env, scope);
85 }
86 
SendEvent(const std::string & event,int32_t extra,const std::vector<uint8_t> & data)87 void MediaKeySystemCallbackNapi::SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data)
88 {
89     std::lock_guard<std::mutex> lock(mutex_);
90     std::unique_ptr<MediaKeySystemJsCallback> cb = std::make_unique<MediaKeySystemJsCallback>();
91     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(cb != nullptr, "No memory");
92     cb->callback = callbackMap_[event];
93     cb->callbackName = event;
94     cb->eventParame.extra = extra;
95     cb->eventParame.data.assign(data.begin(), data.end());
96     return OnJsCallbackInterrupt(cb);
97 }
98 
OnJsCallbackInterrupt(std::unique_ptr<MediaKeySystemJsCallback> & jsCb)99 void MediaKeySystemCallbackNapi::OnJsCallbackInterrupt(std::unique_ptr<MediaKeySystemJsCallback> &jsCb)
100 {
101     uv_loop_s *loop = nullptr;
102     napi_get_uv_event_loop(env_, &loop);
103     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(loop != nullptr, "loop nullptr, No memory");
104 
105     uv_work_t *work = new (std::nothrow) uv_work_t;
106     DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(work != nullptr, "work nullptr, No memory");
107 
108     if (jsCb.get() == nullptr) {
109         DRM_DEBUG_LOG("OnJsCallBackInterrupt: jsCb.get() is null");
110         delete work;
111         return;
112     }
113     work->data = reinterpret_cast<void *>(jsCb.get());
114 
115     int ret = uv_queue_work_with_qos(
116         loop, work, [](uv_work_t *work) {}, WorkCallbackInterruptDone, uv_qos_default);
117     if (ret != 0) {
118         DRM_DEBUG_LOG("Failed to execute libuv work queue");
119         delete work;
120     } else {
121         jsCb.release();
122     }
123 }
124 } // namespace DrmStandard
125 } // namespace OHOS