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