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
31 namespace OHOS {
32 namespace AbilityRuntime {
33 using namespace OHOS::AppExecFwk;
34 using namespace OHOS::Print;
35
JsPrintCallback(JsRuntime & jsRuntime)36 JsPrintCallback::JsPrintCallback(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime), jsWorker_(nullptr) {}
37
GetJsLoop(JsRuntime & jsRuntime)38 uv_loop_s* JsPrintCallback::GetJsLoop(JsRuntime &jsRuntime)
39 {
40 NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine();
41 uv_loop_s* loop = nullptr;
42 napi_get_uv_event_loop(reinterpret_cast<napi_env>(nativeEngine), &loop);
43 if (loop == nullptr) {
44 return nullptr;
45 }
46 return loop;
47 }
48
BuildJsWorker(NativeValue * jsObj,const std::string & name,NativeValue * const * argv,size_t argc,bool isSync)49 bool JsPrintCallback::BuildJsWorker(NativeValue *jsObj, const std::string &name,
50 NativeValue *const *argv, size_t argc, bool isSync)
51 {
52 NativeObject *obj = ConvertNativeValueTo<NativeObject>(jsObj);
53 if (obj == nullptr) {
54 PRINT_HILOGE("Failed to get PrintExtension object");
55 return false;
56 }
57
58 NativeValue *method = obj->GetProperty(name.c_str());
59 if (method == nullptr) {
60 PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name.c_str());
61 return false;
62 }
63
64 jsWorker_ = new (std::nothrow) uv_work_t;
65 if (jsWorker_ == nullptr) {
66 PRINT_HILOGE("Failed to create uv work");
67 return false;
68 }
69
70 jsParam_.self = shared_from_this();
71 jsParam_.nativeEngine = &jsRuntime_.GetNativeEngine();
72 jsParam_.jsObj = jsObj;
73 jsParam_.jsMethod = method;
74 jsParam_.argv = argv;
75 jsParam_.argc = argc;
76 jsParam_.jsResult = nullptr;
77 jsParam_.isSync = isSync;
78 jsParam_.isCompleted = false;
79 jsWorker_->data = &jsParam_;
80 return true;
81 }
82
Exec(NativeValue * jsObj,const std::string & name,NativeValue * const * argv,size_t argc,bool isSync)83 NativeValue *JsPrintCallback::Exec(
84 NativeValue *jsObj, const std::string &name, NativeValue *const *argv, size_t argc, bool isSync)
85 {
86 PRINT_HILOGD("%{public}s callback in", name.c_str());
87 HandleScope handleScope(jsRuntime_);
88 uv_loop_s *loop = GetJsLoop(jsRuntime_);
89 if (loop == nullptr) {
90 PRINT_HILOGE("Failed to acquire js event loop");
91 return nullptr;
92 }
93 if (!BuildJsWorker(jsObj, name, argv, argc, isSync)) {
94 PRINT_HILOGE("Failed to build JS worker");
95 return nullptr;
96 }
97 uv_queue_work(
98 loop, jsWorker_, [](uv_work_t *work) {},
99 [](uv_work_t *work, int statusInt) {
100 auto jsWorkParam = reinterpret_cast<JsPrintCallback::JsWorkParam*>(work->data);
101 if (jsWorkParam != nullptr) {
102 jsWorkParam->jsResult = jsWorkParam->nativeEngine->CallFunction(
103 jsWorkParam->jsObj, jsWorkParam->jsMethod, jsWorkParam->argv, jsWorkParam->argc);
104 jsWorkParam->isCompleted = true;
105 if (jsWorkParam->isSync) {
106 jsWorkParam->self = nullptr;
107 } else {
108 std::unique_lock<std::mutex> lock(jsWorkParam->self->conditionMutex_);
109 jsWorkParam->self->syncCon_.notify_one();
110 }
111 }
112 });
113 if (isSync) {
114 std::unique_lock<std::mutex> conditionLock(conditionMutex_);
115 auto waitStatus = syncCon_.wait_for(
116 conditionLock, std::chrono::milliseconds(SYNC_TIME_OUT), [this]() { return jsParam_.isCompleted; });
117 if (!waitStatus) {
118 PRINT_HILOGE("print server load sa timeout");
119 return nullptr;
120 }
121 return jsParam_.jsResult;
122 }
123 return nullptr;
124 }
125 } // namespace AbilityRuntime
126 } // namespace OHOS
127