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