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 "async_call.h"
17 #include "constant.h"
18
19 using namespace OHOS::Request::Upload;
20 namespace OHOS::Request::UploadNapi {
AsyncCall(napi_env env,napi_callback_info info,std::shared_ptr<Context> context)21 AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context)
22 : env_(env)
23 {
24 context_ = new AsyncContext();
25 size_t argc = JSUtil::MAX_ARGC;
26 napi_value self = nullptr;
27 napi_value argv[JSUtil::MAX_ARGC] = {nullptr};
28 NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
29 napi_valuetype valueType = napi_undefined;
30 napi_typeof(env, argv[argc - 1], &valueType);
31 if (valueType == napi_function) {
32 napi_create_reference(env, argv[argc - 1], 1, &context_->callback);
33 argc = argc - 1;
34 }
35 napi_status status = (*context)(env, argc, argv, self);
36 if (status == napi_ok) {
37 context_->ctx = std::move(context);
38 napi_create_reference(env, self, 1, &context_->self);
39 } else {
40 UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "context fail");
41 }
42 }
43
~AsyncCall()44 AsyncCall::~AsyncCall()
45 {
46 if (context_ == nullptr) {
47 return;
48 }
49
50 DeleteContext(env_, context_);
51 }
52
Call(napi_env env,Context::ExecAction exec)53 napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec)
54 {
55 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
56 UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "context_ or context_->ctx is null");
57 return nullptr;
58 }
59 UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "async call exec");
60 context_->ctx->exec_ = std::move(exec);
61 napi_value promise = nullptr;
62 if (context_->callback == nullptr) {
63 napi_create_promise(env, &context_->defer, &promise);
64 } else {
65 napi_get_undefined(env, &promise);
66 }
67 napi_async_work work = context_->work;
68 napi_value resource = nullptr;
69 napi_create_string_utf8(env, "AsyncCall", NAPI_AUTO_LENGTH, &resource);
70 napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
71 context_->work = work;
72 context_ = nullptr;
73 napi_queue_async_work(env, work);
74 UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "async call exec");
75 return promise;
76 }
77
SyncCall(napi_env env,AsyncCall::Context::ExecAction exec)78 napi_value AsyncCall::SyncCall(napi_env env, AsyncCall::Context::ExecAction exec)
79 {
80 if ((context_ == nullptr) || (context_->ctx == nullptr)) {
81 UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "context_ or context_->ctx is null");
82 return nullptr;
83 }
84 context_->ctx->exec_ = std::move(exec);
85 napi_value promise = nullptr;
86 if (context_->callback == nullptr) {
87 napi_create_promise(env, &context_->defer, &promise);
88 } else {
89 napi_get_undefined(env, &promise);
90 }
91 AsyncCall::OnExecute(env, context_);
92 AsyncCall::OnComplete(env, napi_ok, context_);
93 context_ = nullptr;
94 return promise;
95 }
96
OnExecute(napi_env env,void * data)97 void AsyncCall::OnExecute(napi_env env, void *data)
98 {
99 UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "run the async runnable");
100 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
101 context->ctx->Exec();
102 }
103
OnComplete(napi_env env,napi_status status,void * data)104 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
105 {
106 UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "run the js callback function");
107 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
108 napi_value output = nullptr;
109 napi_status runStatus = (*context->ctx)(env, &output);
110 napi_value result[ARG_BUTT] = { 0 };
111 if (status == napi_ok && runStatus == napi_ok) {
112 napi_get_undefined(env, &result[ARG_ERROR]);
113 if (output != nullptr) {
114 result[ARG_DATA] = output;
115 } else {
116 napi_get_undefined(env, &result[ARG_DATA]);
117 }
118 } else {
119 result[ARG_ERROR] = JSUtil::CreateBusinessError(env, Download::EXCEPTION_SERVICE_ERROR, "upload fail");
120 napi_get_undefined(env, &result[ARG_DATA]);
121 }
122 if (context->defer != nullptr) {
123 // promise
124 if (status == napi_ok && runStatus == napi_ok) {
125 napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
126 } else {
127 napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
128 }
129 } else {
130 // callback
131 napi_value callback = nullptr;
132 napi_get_reference_value(env, context->callback, &callback);
133 napi_value returnValue;
134 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
135 }
136 DeleteContext(env, context);
137 }
DeleteContext(napi_env env,AsyncContext * context)138 void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
139 {
140 if (env != nullptr) {
141 napi_delete_reference(env, context->callback);
142 napi_delete_reference(env, context->self);
143 napi_delete_async_work(env, context->work);
144 }
145 delete context;
146 }
147 }