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 "webview_javascript_execute_callback.h"
17
18 #include "business_error.h"
19 #include "napi_parse_utils.h"
20 #include "nweb_log.h"
21 #include "web_errors.h"
22
23 namespace OHOS::NWeb {
24 using namespace NWebError;
25
OnReceiveValue(std::string result)26 void WebviewJavaScriptExecuteCallback::OnReceiveValue(std::string result)
27 {
28 WVLOG_D("WebviewJavaScriptExecuteCallback::OnReceiveValue, result = %{public}s", result.c_str());
29 uv_loop_s *loop = nullptr;
30 uv_work_t *work = nullptr;
31
32 napi_get_uv_event_loop(env_, &loop);
33 if (loop == nullptr) {
34 return;
35 }
36 work = new (std::nothrow) uv_work_t;
37 if (work == nullptr) {
38 return;
39 }
40
41 JavaScriptExecuteParam *param = new (std::nothrow) JavaScriptExecuteParam();
42 if (param == nullptr) {
43 delete work;
44 return;
45 }
46 param->env_ = env_;
47 param->callbackRef_ = callbackRef_;
48 param->deferred_ = deferred_;
49 param->result_ = result;
50
51 work->data = reinterpret_cast<void*>(param);
52
53 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvAfterWorkCb);
54 if (ret != 0) {
55 if (param != nullptr) {
56 delete param;
57 param = nullptr;
58 }
59 if (work != nullptr) {
60 delete work;
61 work = nullptr;
62 }
63 }
64 }
65
UvAfterWorkCb(uv_work_t * work,int status)66 void WebviewJavaScriptExecuteCallback::UvAfterWorkCb(uv_work_t* work, int status)
67 {
68 WVLOG_D("WebviewJavaScriptExecuteCallback::UvAfterWorkCb");
69 (void)status;
70 if (!work) {
71 return;
72 }
73 JavaScriptExecuteParam *param = reinterpret_cast<JavaScriptExecuteParam*>(work->data);
74 if (!param) {
75 delete work;
76 work = nullptr;
77 return;
78 }
79 napi_handle_scope scope = nullptr;
80 napi_open_handle_scope(param->env_, &scope);
81 if (scope == nullptr) {
82 return;
83 }
84
85 if (param->callbackRef_) {
86 UvAfterWorkCbAsync(param->env_, param->callbackRef_, param->result_);
87 } else if (param->deferred_) {
88 UvAfterWorkCbPromise(param->env_, param->deferred_, param->result_);
89 }
90
91 napi_close_handle_scope(param->env_, scope);
92 delete param;
93 param = nullptr;
94 delete work;
95 work = nullptr;
96 }
97
UvAfterWorkCbAsync(napi_env env,napi_ref callbackRef,const std::string & result)98 void WebviewJavaScriptExecuteCallback::UvAfterWorkCbAsync(napi_env env, napi_ref callbackRef,
99 const std::string& result)
100 {
101 WVLOG_D("WebviewJavaScriptExecuteCallback::UvAfterWorkCbAsync");
102 napi_value setResult[INTEGER_TWO] = {0};
103 if (result.empty()) {
104 setResult[INTEGER_ZERO] = BusinessError::CreateError(env, NWebError::INVALID_RESOURCE);
105 napi_get_null(env, &setResult[INTEGER_ONE]);
106 } else {
107 napi_get_undefined(env, &setResult[INTEGER_ZERO]);
108 napi_create_string_utf8(env, result.c_str(), NAPI_AUTO_LENGTH, &setResult[INTEGER_ONE]);
109 }
110
111 napi_value args[INTEGER_TWO] = {setResult[INTEGER_ZERO], setResult[INTEGER_ONE]};
112 napi_value callback = nullptr;
113 napi_value callbackResult = nullptr;
114
115 napi_get_reference_value(env, callbackRef, &callback);
116 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult);
117 napi_delete_reference(env, callbackRef);
118 }
119
UvAfterWorkCbPromise(napi_env env,napi_deferred deferred,const std::string & result)120 void WebviewJavaScriptExecuteCallback::UvAfterWorkCbPromise(napi_env env, napi_deferred deferred,
121 const std::string& result)
122 {
123 WVLOG_D("WebviewJavaScriptExecuteCallback::UvAfterWorkCbPromise");
124 napi_value setResult[INTEGER_TWO] = {0};
125 setResult[INTEGER_ZERO] = NWebError::BusinessError::CreateError(env, NWebError::INVALID_RESOURCE);
126 napi_create_string_utf8(env, result.c_str(), NAPI_AUTO_LENGTH, &setResult[INTEGER_ONE]);
127
128 napi_value args[INTEGER_TWO] = {setResult[INTEGER_ZERO], setResult[INTEGER_ONE]};
129 if (!result.empty()) {
130 napi_resolve_deferred(env, deferred, args[INTEGER_ONE]);
131 } else {
132 napi_reject_deferred(env, deferred, args[INTEGER_ZERO]);
133 }
134 }
135
136 } // namespace NWeb