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 "js_print_callback.h"
17 
18 #include "ability_info.h"
19 #include "iprint_extension_callback.h"
20 #include "js_print_extension_context.h"
21 #include "js_runtime.h"
22 #include "js_runtime_utils.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "napi_common_util.h"
26 #include "napi_common_want.h"
27 #include "napi_remote_object.h"
28 #include "print_log.h"
29 #include "print_manager_client.h"
30 #include "print_constant.h"
31 
32 namespace OHOS {
33 namespace AbilityRuntime {
34 using namespace OHOS::AppExecFwk;
35 using namespace OHOS::Print;
36 
JsPrintCallback(JsRuntime & jsRuntime)37 JsPrintCallback::JsPrintCallback(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {}
38 
GetJsLoop(JsRuntime & jsRuntime)39 uv_loop_s* JsPrintCallback::GetJsLoop(JsRuntime &jsRuntime)
40 {
41     napi_env env = jsRuntime_.GetNapiEnv();
42     uv_loop_s* loop = nullptr;
43     napi_get_uv_event_loop(env, &loop);
44     if (loop == nullptr) {
45         return nullptr;
46     }
47     return loop;
48 }
49 
Call(napi_env env,void * data,uv_after_work_cb afterCallback)50 bool JsPrintCallback::Call(napi_env env, void *data, uv_after_work_cb afterCallback)
51 {
52     uv_loop_s *loop = nullptr;
53     napi_get_uv_event_loop(env, &loop);
54     if (loop == nullptr) {
55         return false;
56     }
57     uv_work_t *work = new (std::nothrow) uv_work_t;
58     if (work == nullptr) {
59         return false;
60     }
61     work->data = data;
62     int retVal = uv_queue_work_with_qos(loop, work,
63                                         [](uv_work_t *work) {}, afterCallback, uv_qos_user_initiated);
64     if (retVal != 0) {
65         PRINT_HILOGE("Failed to create uv work");
66         delete work;
67         return false;
68     }
69     return true;
70 }
71 
BuildJsWorker(napi_value jsObj,const std::string & name,napi_value const * argv,size_t argc,bool isSync)72 uv_work_t* JsPrintCallback::BuildJsWorker(napi_value jsObj, const std::string &name,
73     napi_value const *argv, size_t argc, bool isSync)
74 {
75     napi_value obj = jsObj;
76     if (obj == nullptr) {
77         PRINT_HILOGE("Failed to get PrintExtension object");
78         return nullptr;
79     }
80 
81     napi_env env = jsRuntime_.GetNapiEnv();
82     napi_value method = nullptr;
83     napi_get_named_property(env, obj, name.c_str(), &method);
84     if (method == nullptr) {
85         PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name.c_str());
86         return nullptr;
87     }
88 
89     uv_work_t *worker = new (std::nothrow) uv_work_t;
90     if (worker == nullptr) {
91         PRINT_HILOGE("Failed to create uv work");
92         return nullptr;
93     }
94 
95     std::unique_lock<std::mutex> conditionLock(conditionMutex_);
96     jsParam_.self = shared_from_this();
97     jsParam_.nativeEngine = jsRuntime_.GetNapiEnv();
98     jsParam_.jsObj = jsObj;
99     jsParam_.jsMethod = method;
100     jsParam_.argv = argv;
101     jsParam_.argc = argc;
102     jsParam_.jsResult = nullptr;
103     jsParam_.isSync = isSync;
104     jsParam_.isCompleted = false;
105     worker->data = &jsParam_;
106     return worker;
107 }
108 
Exec(napi_value jsObj,const std::string & name,napi_value const * argv,size_t argc,bool isSync)109 napi_value JsPrintCallback::Exec(
110     napi_value jsObj, const std::string &name, napi_value const *argv, size_t argc, bool isSync)
111 {
112     PRINT_HILOGD("%{public}s callback in", name.c_str());
113     HandleScope handleScope(jsRuntime_);
114     uv_loop_s *loop = GetJsLoop(jsRuntime_);
115     if (loop == nullptr) {
116         PRINT_HILOGE("Failed to acquire js event loop");
117         return nullptr;
118     }
119     uv_work_t *worker = BuildJsWorker(jsObj, name, argv, argc, isSync);
120     if (worker == nullptr) {
121         PRINT_HILOGE("Failed to build JS worker");
122         return nullptr;
123     }
124 
125     if (UvQueueWork(loop, worker) != 0) {
126         PRINT_HILOGE("Failed to uv_queue_work");
127         PRINT_SAFE_DELETE(worker);
128         return nullptr;
129     }
130     if (isSync) {
131         std::unique_lock<std::mutex> conditionLock(conditionMutex_);
132         auto waitStatus = syncCon_.wait_for(
133             conditionLock, std::chrono::milliseconds(SYNC_TIME_OUT), [this]() { return jsParam_.isCompleted; });
134         if (!waitStatus) {
135             PRINT_HILOGE("print server load sa timeout");
136             return nullptr;
137         }
138         return jsParam_.jsResult;
139     }
140     return nullptr;
141 }
142 
UvQueueWork(uv_loop_s * loop,uv_work_t * worker)143 int JsPrintCallback::UvQueueWork(uv_loop_s* loop, uv_work_t* worker)
144 {
145     return uv_queue_work(
146         loop, worker, [](uv_work_t *work) {},
147         [](uv_work_t *work, int statusInt) {
148             if (work == nullptr) {
149                 PRINT_HILOGE("work is nullptr!");
150                 return;
151             }
152             auto jsWorkParam = reinterpret_cast<JsPrintCallback::JsWorkParam*>(work->data);
153             if (jsWorkParam == nullptr) {
154                 PRINT_HILOGE("jsWorkParam is nullptr!");
155                 PRINT_SAFE_DELETE(work);
156                 return;
157             }
158             napi_call_function(jsWorkParam->nativeEngine, jsWorkParam->jsObj,
159                 jsWorkParam->jsMethod, jsWorkParam->argc, jsWorkParam->argv, &(jsWorkParam->jsResult));
160             jsWorkParam->isCompleted = true;
161             if (jsWorkParam->isSync) {
162                 jsWorkParam->self = nullptr;
163             } else {
164                 std::unique_lock<std::mutex> lock(jsWorkParam->self->conditionMutex_);
165                 jsWorkParam->self->syncCon_.notify_one();
166             }
167             PRINT_SAFE_DELETE(jsWorkParam);
168             PRINT_SAFE_DELETE(work);
169         });
170 }
171 } // namespace AbilityRuntime
172 } // namespace OHOS
173