• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_error_code_napi.h"
16 #include "napi_async_work.h"
17 
18 namespace OHOS {
19 namespace DrmStandard {
~ContextBase()20 ContextBase::~ContextBase()
21 {
22     DRM_DEBUG_LOG("no memory leak after callback or promise[resolved/rejected]");
23     if (env != nullptr) {
24         if (work != nullptr) {
25             napi_delete_async_work(env, work);
26         }
27         if (callbackRef != nullptr) {
28             napi_delete_reference(env, callbackRef);
29         }
30         napi_delete_reference(env, selfRef);
31         env = nullptr;
32         callbackRef = nullptr;
33         selfRef = nullptr;
34     }
35 }
36 
GetCbInfo(napi_env envi,napi_callback_info info,NapiCbInfoParser parser,bool sync)37 void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoParser parser, bool sync)
38 {
39     env = envi;
40     size_t argc = ARGC_MAX;
41     napi_value argv[ARGC_MAX] = {nullptr};
42     status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr);
43     NAPI_CHECK_STATUS_RETURN_VOID(this, "napi_get_cb_info failed!", DRM_NAPI_ERR_INVALID_VAL);
44     NAPI_CHECK_ARGS_RETURN_VOID(this, argc <= ARGC_MAX, "too many arguments!", DRM_NAPI_ERR_INVALID_VAL);
45     NAPI_CHECK_ARGS_RETURN_VOID(this, self != nullptr, "no JavaScript this argument!", DRM_NAPI_ERR_INVALID_VAL);
46     napi_create_reference(env, self, 1, &selfRef);
47     status = napi_unwrap(env, self, &native);
48     NAPI_CHECK_STATUS_RETURN_VOID(this, "self unwrap failed!", DRM_NAPI_ERR_INVALID_VAL);
49 
50     if (!sync && (argc > 0)) {
51         // get the last arguments :: <callback>
52         size_t index = argc - 1;
53         napi_valuetype type = napi_undefined;
54         napi_status tyst = napi_typeof(env, argv[index], &type);
55         if ((tyst == napi_ok) && (type == napi_function)) {
56             status = napi_create_reference(env, argv[index], 1, &callbackRef);
57             NAPI_CHECK_STATUS_RETURN_VOID(this, "ref callback failed!", DRM_NAPI_ERR_INVALID_VAL);
58             argc = index;
59             DRM_DEBUG_LOG("async callback, no promise");
60         } else {
61             DRM_DEBUG_LOG("no callback, async promise");
62         }
63     }
64 
65     if (parser) {
66         parser(argc, argv);
67     } else {
68         NAPI_CHECK_ARGS_RETURN_VOID(this, argc == 0, "required no arguments!", DRM_NAPI_ERR_INVALID_VAL);
69     }
70 }
71 
SignError(int32_t code)72 void ContextBase::SignError(int32_t code)
73 {
74     status = napi_generic_failure;
75     errCode = code;
76     errMessage = NapiDrmError::GetMessageByCode(errCode);
77 }
78 
Enqueue(napi_env env,std::shared_ptr<ContextBase> ctxt,const std::string & name,NapiAsyncExecute execute,NapiAsyncComplete complete)79 napi_value NapiAsyncWork::Enqueue(napi_env env, std::shared_ptr<ContextBase> ctxt, const std::string &name,
80     NapiAsyncExecute execute, NapiAsyncComplete complete)
81 {
82     DRM_DEBUG_LOG("name=%{public}s", name.c_str());
83     ctxt->execute = std::move(execute);
84     ctxt->complete = std::move(complete);
85     ctxt->taskName = name;
86     napi_value promise = nullptr;
87     if (ctxt->callbackRef == nullptr) {
88         napi_create_promise(ctxt->env, &ctxt->deferred, &promise);
89         DRM_DEBUG_LOG("create deferred promise");
90     } else {
91         napi_get_undefined(ctxt->env, &promise);
92     }
93 
94     napi_value resource = nullptr;
95     napi_create_string_utf8(ctxt->env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
96     napi_create_async_work(
97         ctxt->env, nullptr, resource,
98         [](napi_env env, void* data) {
99             DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(data != nullptr, "napi_async_execute_callback nullptr");
100             auto ctxt = reinterpret_cast<ContextBase*>(data);
101             DRM_DEBUG_LOG("napi_async_execute_callback ctxt->status=%{public}d", ctxt->status);
102             if (ctxt->execute && ctxt->status == napi_ok) {
103                 ctxt->execute();
104             }
105         },
106         [](napi_env env, napi_status status, void* data) {
107             DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(data != nullptr, "napi_async_complete_callback nullptr");
108             auto ctxt = reinterpret_cast<ContextBase*>(data);
109             DRM_DEBUG_LOG("napi_async_complete_callback status=%{public}d, ctxt->status=%{public}d",
110                 status, ctxt->status);
111             if ((status != napi_ok) && (ctxt->status == napi_ok)) {
112                 ctxt->status = status;
113             }
114             if ((ctxt->complete) && (status == napi_ok) && (ctxt->status == napi_ok)) {
115                 ctxt->complete(ctxt->output);
116             }
117             CommonCallbackRoutine(ctxt);
118         },
119         reinterpret_cast<void*>(ctxt.get()), &ctxt->work);
120     napi_queue_async_work_with_qos(ctxt->env, ctxt->work, napi_qos_user_initiated);
121     ctxt->hold = ctxt; // save crossing-thread ctxt.
122     return promise;
123 }
124 
CommonCallbackRoutine(ContextBase * ctxt)125 void NapiAsyncWork::CommonCallbackRoutine(ContextBase *ctxt)
126 {
127     napi_value result[RESULT_ALL] = {nullptr};
128     if (ctxt->status == napi_ok) {
129         napi_get_undefined(ctxt->env, &result[RESULT_ERROR]);
130         if (ctxt->output == nullptr) {
131             napi_get_undefined(ctxt->env, &ctxt->output);
132         }
133         result[RESULT_DATA] = ctxt->output;
134     } else {
135         napi_value message = nullptr;
136         napi_value code = nullptr;
137         napi_create_string_utf8(ctxt->env, ctxt->errMessage.c_str(), NAPI_AUTO_LENGTH, &message);
138         napi_create_error(ctxt->env, nullptr, message, &result[RESULT_ERROR]);
139         napi_create_int32(ctxt->env, ctxt->errCode, &code);
140         napi_set_named_property(ctxt->env, result[RESULT_ERROR], "code", code);
141         napi_get_undefined(ctxt->env, &result[RESULT_DATA]);
142     }
143     if (ctxt->deferred != nullptr) {
144         if (ctxt->status == napi_ok) {
145             DRM_DEBUG_LOG("deferred promise resolved");
146             napi_resolve_deferred(ctxt->env, ctxt->deferred, result[RESULT_DATA]);
147         } else {
148             DRM_DEBUG_LOG("deferred promise rejected");
149             napi_reject_deferred(ctxt->env, ctxt->deferred, result[RESULT_ERROR]);
150         }
151     } else {
152         napi_value callback = nullptr;
153         napi_get_reference_value(ctxt->env, ctxt->callbackRef, &callback);
154         napi_value callbackResult = nullptr;
155         DRM_DEBUG_LOG("call callback function");
156         napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult);
157     }
158     ctxt->hold->execute = nullptr;
159     ctxt->hold->complete = nullptr;
160     ctxt->hold.reset(); // release ctxt.
161 }
162 } // namespace DrmStandard
163 } // namespace OHOS