• 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 
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     napi_env env = jsRuntime_.GetNapiEnv();
41     uv_loop_s* loop = nullptr;
42     napi_get_uv_event_loop(env, &loop);
43     if (loop == nullptr) {
44         return nullptr;
45     }
46     return loop;
47 }
48 
Call(napi_env env,void * data,uv_after_work_cb afterCallback)49 bool JsPrintCallback::Call(napi_env env, void *data, uv_after_work_cb afterCallback)
50 {
51     uv_loop_s *loop = nullptr;
52     napi_get_uv_event_loop(env, &loop);
53     if (loop == nullptr) {
54         return false;
55     }
56     uv_work_t *work = new (std::nothrow) uv_work_t;
57     if (work == nullptr) {
58         return false;
59     }
60     work->data = data;
61     int retVal = uv_queue_work_with_qos(loop, work,
62                                         [](uv_work_t *work) {}, afterCallback, uv_qos_user_initiated);
63     if (retVal != 0) {
64         PRINT_HILOGE("Failed to create uv work");
65         delete work;
66         return false;
67     }
68     return true;
69 }
70 
BuildJsWorker(napi_value jsObj,const std::string & name,napi_value const * argv,size_t argc,bool isSync)71 bool JsPrintCallback::BuildJsWorker(napi_value jsObj, const std::string &name,
72     napi_value const *argv, size_t argc, bool isSync)
73 {
74     napi_value obj = jsObj;
75     if (obj == nullptr) {
76         PRINT_HILOGE("Failed to get PrintExtension object");
77         return false;
78     }
79 
80     napi_env env = jsRuntime_.GetNapiEnv();
81     napi_value method = nullptr;
82     napi_get_named_property(env, obj, name.c_str(), &method);
83     if (method == nullptr) {
84         PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name.c_str());
85         return false;
86     }
87 
88     jsWorker_ = new (std::nothrow) uv_work_t;
89     if (jsWorker_ == nullptr) {
90         PRINT_HILOGE("Failed to create uv work");
91         return false;
92     }
93 
94     jsParam_.self = shared_from_this();
95     jsParam_.nativeEngine = jsRuntime_.GetNapiEnv();
96     jsParam_.jsObj = jsObj;
97     jsParam_.jsMethod = method;
98     jsParam_.argv = argv;
99     jsParam_.argc = argc;
100     jsParam_.jsResult = nullptr;
101     jsParam_.isSync = isSync;
102     jsParam_.isCompleted = false;
103     jsWorker_->data = &jsParam_;
104     return true;
105 }
106 
Exec(napi_value jsObj,const std::string & name,napi_value const * argv,size_t argc,bool isSync)107 napi_value JsPrintCallback::Exec(
108     napi_value jsObj, const std::string &name, napi_value const *argv, size_t argc, bool isSync)
109 {
110     PRINT_HILOGD("%{public}s callback in", name.c_str());
111     HandleScope handleScope(jsRuntime_);
112     uv_loop_s *loop = GetJsLoop(jsRuntime_);
113     if (loop == nullptr) {
114         PRINT_HILOGE("Failed to acquire js event loop");
115         return nullptr;
116     }
117     if (!BuildJsWorker(jsObj, name, argv, argc, isSync)) {
118         PRINT_HILOGE("Failed to build JS worker");
119         return nullptr;
120     }
121     uv_queue_work(
122         loop, jsWorker_, [](uv_work_t *work) {},
123         [](uv_work_t *work, int statusInt) {
124             auto jsWorkParam = reinterpret_cast<JsPrintCallback::JsWorkParam*>(work->data);
125             if (jsWorkParam != nullptr) {
126                 napi_call_function(jsWorkParam->nativeEngine, jsWorkParam->jsObj,
127                     jsWorkParam->jsMethod, jsWorkParam->argc, jsWorkParam->argv, &(jsWorkParam->jsResult));
128                 jsWorkParam->isCompleted = true;
129                 if (jsWorkParam->isSync) {
130                     jsWorkParam->self = nullptr;
131                 } else {
132                     std::unique_lock<std::mutex> lock(jsWorkParam->self->conditionMutex_);
133                     jsWorkParam->self->syncCon_.notify_one();
134                 }
135             }
136         });
137     if (isSync) {
138         std::unique_lock<std::mutex> conditionLock(conditionMutex_);
139         auto waitStatus = syncCon_.wait_for(
140             conditionLock, std::chrono::milliseconds(SYNC_TIME_OUT), [this]() { return jsParam_.isCompleted; });
141         if (!waitStatus) {
142             PRINT_HILOGE("print server load sa timeout");
143             return nullptr;
144         }
145         return jsParam_.jsResult;
146     }
147     return nullptr;
148 }
149 } // namespace AbilityRuntime
150 } // namespace OHOS
151