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