• 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 #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,WorkParam * param,std::function<void (WorkParam *)> workCb)50 bool JsPrintCallback::Call(napi_env env, WorkParam *param, std::function<void(WorkParam*)> workCb)
51 {
52     auto task = [param, workCb]() {
53         if (param == nullptr) {
54             PRINT_HILOGE("param is a nullptr");
55             return;
56         }
57         workCb(param);
58         delete param;
59     };
60 
61     napi_status ret = napi_send_event(env, task, napi_eprio_immediate);
62     if (ret != napi_ok) {
63         PRINT_HILOGE("napi_send_event fail");
64         return false;
65     }
66     return true;
67 }
68 
BuildJsWorker(napi_value jsObj,const std::string & name,napi_value const * argv,size_t argc,bool isSync)69 uv_work_t* JsPrintCallback::BuildJsWorker(napi_value jsObj, const std::string &name,
70     napi_value const *argv, size_t argc, bool isSync)
71 {
72     napi_value obj = jsObj;
73     if (obj == nullptr) {
74         PRINT_HILOGE("Failed to get PrintExtension object");
75         return nullptr;
76     }
77 
78     napi_env env = jsRuntime_.GetNapiEnv();
79     napi_value method = nullptr;
80     napi_get_named_property(env, obj, name.c_str(), &method);
81     if (method == nullptr) {
82         PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name.c_str());
83         return nullptr;
84     }
85 
86     uv_work_t *worker = new (std::nothrow) uv_work_t;
87     if (worker == nullptr) {
88         PRINT_HILOGE("Failed to create uv work");
89         return nullptr;
90     }
91 
92     std::unique_lock<std::mutex> conditionLock(conditionMutex_);
93     jsParam_.self = shared_from_this();
94     jsParam_.nativeEngine = jsRuntime_.GetNapiEnv();
95     jsParam_.jsObj = jsObj;
96     jsParam_.jsMethod = method;
97     jsParam_.argv = argv;
98     jsParam_.argc = argc;
99     jsParam_.jsResult = nullptr;
100     jsParam_.isSync = isSync;
101     jsParam_.isCompleted = false;
102     worker->data = &jsParam_;
103     return worker;
104 }
105 
Exec(napi_value jsObj,const std::string & name,napi_value const * argv,size_t argc,bool isSync)106 napi_value JsPrintCallback::Exec(
107     napi_value jsObj, const std::string &name, napi_value const *argv, size_t argc, bool isSync)
108 {
109     PRINT_HILOGD("%{public}s callback in", name.c_str());
110     HandleScope handleScope(jsRuntime_);
111     uv_loop_s *loop = GetJsLoop(jsRuntime_);
112     if (loop == nullptr) {
113         PRINT_HILOGE("Failed to acquire js event loop");
114         return nullptr;
115     }
116     uv_work_t *worker = BuildJsWorker(jsObj, name, argv, argc, isSync);
117     if (worker == nullptr) {
118         PRINT_HILOGE("Failed to build JS worker");
119         return nullptr;
120     }
121     PRINT_SAFE_DELETE(worker);
122     auto task = [jsWorkParam = &jsParam_]() {
123         napi_call_function(jsWorkParam->nativeEngine, jsWorkParam->jsObj,
124             jsWorkParam->jsMethod, jsWorkParam->argc, jsWorkParam->argv, &(jsWorkParam->jsResult));
125         jsWorkParam->isCompleted = true;
126         if (jsWorkParam->isSync) {
127             jsWorkParam->self = nullptr;
128         } else {
129             std::unique_lock<std::mutex> lock(jsWorkParam->self->conditionMutex_);
130             jsWorkParam->self->syncCon_.notify_one();
131         }
132     };
133     if (napi_send_event(jsParam_.nativeEngine, task, napi_eprio_low) != napi_ok) {
134         PRINT_HILOGE("Failed to send event");
135         return nullptr;
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