• 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 "Call"
16 #include "call.h"
17 
18 #include "hilog_wrapper.h"
19 #include "js_error.h"
20 #include "wallpaper_js_util.h"
21 
22 namespace OHOS::WallpaperNAPI {
Call(napi_env env,napi_callback_info info,std::shared_ptr<Context> context,size_t pos,bool needException)23 Call::Call(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos, bool needException)
24     : env_(env)
25 {
26     context_ = new CallContext();
27     size_t argc = WallpaperJSUtil::MAX_ARGC;
28     napi_value self = nullptr;
29     napi_value argv[WallpaperJSUtil::MAX_ARGC] = { nullptr };
30     NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
31     pos = ((pos == ASYNC_DEFAULT_POS) ? (argc - 1) : pos);
32     if (pos < argc) {
33         napi_valuetype valueType = napi_undefined;
34         napi_typeof(env, argv[pos], &valueType);
35         if (valueType == napi_function) {
36             napi_create_reference(env, argv[pos], 1, &context_->callback);
37             argc = pos;
38         } else {
39             context->errCode_ = ErrorThrowType::PARAMETER_ERROR;
40             context->errMsg_ = PARAMETERERRORMESSAGE;
41         }
42     }
43     auto status = (*context)(env, argc, argv, self);
44     if (status != napi_ok && context->errCode_ != 0 && needException) {
45         JsError::ThrowError(env, context->errCode_, context->errMsg_);
46         context->output_ = nullptr;
47         context->exec_ = nullptr;
48     } else {
49         context_->ctx = std::move(context);
50         napi_create_reference(env, self, 1, &context_->self);
51     }
52 }
53 
~Call()54 Call::~Call()
55 {
56     if (context_ == nullptr) {
57         return;
58     }
59 
60     DeleteContext(env_, context_);
61 }
62 
AsyncCall(napi_env env,const std::string & resourceName)63 napi_value Call::AsyncCall(napi_env env, const std::string &resourceName)
64 {
65     if (context_ == nullptr) {
66         HILOG_DEBUG("context_ is null");
67         return nullptr;
68     }
69     if (context_->ctx == nullptr) {
70         HILOG_DEBUG("context_->ctx is null");
71         return nullptr;
72     }
73     HILOG_DEBUG("async call exec");
74     napi_value promise = nullptr;
75     if (context_->callback == nullptr) {
76         napi_create_promise(env, &context_->defer, &promise);
77     } else {
78         napi_get_undefined(env, &promise);
79     }
80     napi_async_work work = context_->work;
81     napi_value resource = nullptr;
82     std::string name = "THEME_" + resourceName;
83     napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
84     napi_create_async_work(env, nullptr, resource, Call::OnExecute, Call::OnComplete, context_, &work);
85     context_->work = work;
86     context_ = nullptr;
87     napi_queue_async_work_with_qos(env, work, napi_qos_user_initiated);
88     HILOG_DEBUG("async call exec");
89     return promise;
90 }
91 
SyncCall(napi_env env)92 napi_value Call::SyncCall(napi_env env)
93 {
94     if ((context_ == nullptr) || (context_->ctx == nullptr)) {
95         HILOG_DEBUG("context_ or context_->ctx is null");
96         return nullptr;
97     }
98     Call::OnExecute(env, context_);
99     napi_value output = nullptr;
100     napi_status runStatus = (*context_->ctx)(env, &output);
101     if (runStatus != napi_ok && context_->ctx->errCode_ != 0) {
102         JsError::ThrowError(env, context_->ctx->errCode_, context_->ctx->errMsg_);
103         output = nullptr;
104     }
105     DeleteContext(env, context_);
106     context_ = nullptr;
107     return output;
108 }
109 
OnExecute(napi_env env,void * data)110 void Call::OnExecute(napi_env env, void *data)
111 {
112     HILOG_DEBUG("run the async runnable");
113     CallContext *context = reinterpret_cast<CallContext *>(data);
114     context->ctx->Exec();
115 }
116 
OnComplete(napi_env env,napi_status status,void * data)117 void Call::OnComplete(napi_env env, napi_status status, void *data)
118 {
119     HILOG_DEBUG("run the js callback function");
120     CallContext *context = reinterpret_cast<CallContext *>(data);
121     napi_value output = nullptr;
122     napi_status runStatus = (*context->ctx)(env, &output);
123     napi_value result[ARG_BUTT] = { 0 };
124     HILOG_DEBUG("run the js callback function:status[%{public}d]runStatus[%{public}d]", status, runStatus);
125     if (status == napi_ok && runStatus == napi_ok) {
126         napi_get_undefined(env, &result[ARG_ERROR]);
127         if (output != nullptr) {
128             HILOG_DEBUG("AsyncCall::OnComplete output != nullptr");
129             result[ARG_DATA] = output;
130         } else {
131             HILOG_DEBUG("AsyncCall::OnComplete output == nullptr");
132             napi_get_undefined(env, &result[ARG_DATA]);
133         }
134     } else {
135         napi_value errCode = nullptr;
136         napi_value message = nullptr;
137         std::string errMsg("async call failed");
138         if (context->ctx->errCode_ != 0) {
139             napi_create_int32(env, context->ctx->errCode_, &errCode);
140         }
141         if (!context->ctx->errMsg_.empty()) {
142             errMsg = context->ctx->errMsg_;
143         }
144         napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
145         napi_create_error(env, nullptr, message, &result[ARG_ERROR]);
146         napi_set_named_property(env, result[ARG_ERROR], "code", errCode);
147         napi_get_undefined(env, &result[ARG_DATA]);
148     }
149     HILOG_DEBUG("run the js callback function:(context->defer != nullptr)?[%{public}d]", context->defer != nullptr);
150     if (context->defer != nullptr) {
151         // promise
152         HILOG_DEBUG("Promise to do!");
153         if (status == napi_ok && runStatus == napi_ok) {
154             napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
155         } else {
156             napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
157         }
158     } else {
159         // callback
160         HILOG_DEBUG("Callback to do!");
161         napi_value callback = nullptr;
162         napi_get_reference_value(env, context->callback, &callback);
163         napi_value returnValue;
164         napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
165     }
166     DeleteContext(env, context);
167 }
DeleteContext(napi_env env,CallContext * context)168 void Call::DeleteContext(napi_env env, CallContext *context)
169 {
170     if (env != nullptr) {
171         napi_delete_reference(env, context->callback);
172         napi_delete_reference(env, context->self);
173         napi_delete_async_work(env, context->work);
174     }
175     delete context;
176 }
177 } // namespace OHOS::WallpaperNAPI