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