• 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 "napi_inner_print.h"
17 
18 #include "napi_print_utils.h"
19 #include "print_callback.h"
20 #include "print_extension_info_helper.h"
21 #include "print_job_helper.h"
22 #include "print_log.h"
23 #include "print_manager_client.h"
24 #include "print_task.h"
25 
26 namespace OHOS::Print {
27 const std::string PRINTER_EVENT_TYPE = "printerStateChange";
28 const std::string PRINTJOB_EVENT_TYPE = "jobStateChange";
29 const std::string EXTINFO_EVENT_TYPE = "extInfoChange";
30 
QueryExtensionInfo(napi_env env,napi_callback_info info)31 napi_value NapiInnerPrint::QueryExtensionInfo(napi_env env, napi_callback_info info)
32 {
33     PRINT_HILOGD("Enter ---->");
34     auto context = std::make_shared<InnerPrintContext>();
35     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
36         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
37         return napi_ok;
38     };
39     auto output = [context](napi_env env, napi_value *result) -> napi_status {
40         PRINT_HILOGD("ouput enter---->");
41         napi_status status = napi_create_array(env, result);
42         uint32_t index = 0;
43         for (auto extInfo : context->allExtensionInfos) {
44             PRINT_HILOGD("ExtensionId = %{public}s", extInfo.GetExtensionId().c_str());
45             PRINT_HILOGD("VendorId = %{public}s", extInfo.GetVendorId().c_str());
46             PRINT_HILOGD("VendorName = %{public}s", extInfo.GetVendorName().c_str());
47             PRINT_HILOGD("VendorIcon = %{public}d", extInfo.GetVendorIcon());
48             PRINT_HILOGD("Version = %{public}s", extInfo.GetVersion().c_str());
49             status = napi_set_element(env, *result, index++, PrintExtensionInfoHelper::MakeJsObject(env, extInfo));
50         }
51         return napi_ok;
52     };
53     auto exec = [context](PrintAsyncCall::Context *ctx) {
54         int32_t ret = PrintManagerClient::GetInstance()->QueryAllExtension(context->allExtensionInfos);
55         context->result = ret == E_PRINT_NONE;
56         if (ret != E_PRINT_NONE) {
57             PRINT_HILOGE("Failed to query all ext info");
58             context->SetErrorIndex(ret);
59         }
60     };
61     context->SetAction(std::move(input), std::move(output));
62     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
63     return asyncCall.Call(env, exec);
64 }
65 
StartDiscovery(napi_env env,napi_callback_info info)66 napi_value NapiInnerPrint::StartDiscovery(napi_env env, napi_callback_info info)
67 {
68     PRINT_HILOGD("Enter StartDiscovery---->");
69     auto context = std::make_shared<InnerPrintContext>();
70     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
71         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
72         bool isArray = false;
73         napi_is_array(env, argv[NapiPrintUtils::INDEX_ZERO], &isArray);
74         PRINT_ASSERT_BASE(env, isArray, " is not array!", napi_array_expected);
75 
76         uint32_t len = 0;
77         napi_get_array_length(env, argv[0], &len);
78 
79         for (uint32_t index = 0; index < len; index++) {
80             napi_value value;
81             napi_get_element(env, argv[NapiPrintUtils::INDEX_ZERO], index, &value);
82             std::string extensionId = NapiPrintUtils::GetStringFromValueUtf8(env, value);
83             PRINT_HILOGD("output for :---- extensionList value is :[%{public}s]", extensionId.c_str());
84             if (extensionId != "") {
85                 context->extensionList.emplace_back(extensionId);
86             }
87         }
88         return napi_ok;
89     };
90     auto output = [context](napi_env env, napi_value *result) -> napi_status {
91         napi_status status = napi_get_boolean(env, context->result, result);
92         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
93         return status;
94     };
95     auto exec = [context](PrintAsyncCall::Context *ctx) {
96         int32_t ret = PrintManagerClient::GetInstance()->StartDiscoverPrinter(context->extensionList);
97         context->result = ret == E_PRINT_NONE;
98         if (ret != E_PRINT_NONE) {
99             PRINT_HILOGE("Failed to start discover printer");
100             context->SetErrorIndex(ret);
101         }
102     };
103     context->SetAction(std::move(input), std::move(output));
104     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
105     return asyncCall.Call(env, exec);
106 }
107 
StopDiscovery(napi_env env,napi_callback_info info)108 napi_value NapiInnerPrint::StopDiscovery(napi_env env, napi_callback_info info)
109 {
110     PRINT_HILOGD("Enter StopDiscovery---->");
111     auto context = std::make_shared<InnerPrintContext>();
112     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
113         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
114         return napi_ok;
115     };
116     auto output = [context](napi_env env, napi_value *result) -> napi_status {
117         napi_status status = napi_get_boolean(env, context->result, result);
118         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
119         return status;
120     };
121     auto exec = [context](PrintAsyncCall::Context *ctx) {
122         int32_t ret = PrintManagerClient::GetInstance()->StopDiscoverPrinter();
123         context->result = ret == E_PRINT_NONE;
124         if (ret != E_PRINT_NONE) {
125             PRINT_HILOGE("Failed to stop discover printer");
126             context->SetErrorIndex(ret);
127         }
128     };
129     context->SetAction(std::move(input), std::move(output));
130     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
131     return asyncCall.Call(env, exec);
132 }
133 
ConnectPrinter(napi_env env,napi_callback_info info)134 napi_value NapiInnerPrint::ConnectPrinter(napi_env env, napi_callback_info info)
135 {
136     PRINT_HILOGD("Enter ConnectPrinter---->");
137     auto context = std::make_shared<InnerPrintContext>();
138     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
139         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
140         napi_valuetype valuetype;
141         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
142         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
143         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
144         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
145         context->printerId = printerId;
146         return napi_ok;
147     };
148     auto output = [context](napi_env env, napi_value *result) -> napi_status {
149         napi_status status = napi_get_boolean(env, context->result, result);
150         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
151         return status;
152     };
153     auto exec = [context](PrintAsyncCall::Context *ctx) {
154         int32_t ret = PrintManagerClient::GetInstance()->ConnectPrinter(context->printerId);
155         context->result = ret == E_PRINT_NONE;
156         if (ret != E_PRINT_NONE) {
157             PRINT_HILOGE("Failed to connect the printer");
158             context->SetErrorIndex(ret);
159         }
160     };
161     context->SetAction(std::move(input), std::move(output));
162     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
163     return asyncCall.Call(env, exec);
164 }
165 
DisconnectPrinter(napi_env env,napi_callback_info info)166 napi_value NapiInnerPrint::DisconnectPrinter(napi_env env, napi_callback_info info)
167 {
168     PRINT_HILOGD("Enter DisconnectPrinter---->");
169     auto context = std::make_shared<InnerPrintContext>();
170     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
171         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
172         napi_valuetype valuetype;
173         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
174         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
175         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
176         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
177         context->printerId = printerId;
178         return napi_ok;
179     };
180     auto output = [context](napi_env env, napi_value *result) -> napi_status {
181         napi_status status = napi_get_boolean(env, context->result, result);
182         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
183         return status;
184     };
185     auto exec = [context](PrintAsyncCall::Context *ctx) {
186         int32_t ret = PrintManagerClient::GetInstance()->DisconnectPrinter(context->printerId);
187         context->result = ret == E_PRINT_NONE;
188         if (ret != E_PRINT_NONE) {
189             PRINT_HILOGE("Failed to connect the printer");
190             context->SetErrorIndex(ret);
191         }
192     };
193     context->SetAction(std::move(input), std::move(output));
194     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
195     return asyncCall.Call(env, exec);
196 }
197 
StartPrintJob(napi_env env,napi_callback_info info)198 napi_value NapiInnerPrint::StartPrintJob(napi_env env, napi_callback_info info)
199 {
200     PRINT_HILOGD("Enter StartPrintJob---->");
201     auto context = std::make_shared<InnerPrintContext>();
202     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
203         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
204         auto printJobPtr = PrintJobHelper::BuildFromJs(env, argv[NapiPrintUtils::INDEX_ZERO]);
205         if (printJobPtr == nullptr) {
206             PRINT_HILOGE("ParseJob type error!");
207             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
208             return napi_invalid_arg;
209         }
210         context->printJob = *printJobPtr;
211         return napi_ok;
212     };
213     auto output = [context](napi_env env, napi_value *result) -> napi_status {
214         napi_status status = napi_get_boolean(env, context->result, result);
215         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
216         return status;
217     };
218     auto exec = [context](PrintAsyncCall::Context *ctx) {
219         context->printJob.Dump();
220         int32_t ret = PrintManagerClient::GetInstance()->StartPrintJob(context->printJob);
221         context->result = ret == E_PRINT_NONE;
222         if (ret != E_PRINT_NONE) {
223             PRINT_HILOGE("Failed to start print job");
224             context->SetErrorIndex(ret);
225         }
226     };
227     context->SetAction(std::move(input), std::move(output));
228     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
229     return asyncCall.Call(env, exec);
230 }
231 
CancelPrintJob(napi_env env,napi_callback_info info)232 napi_value NapiInnerPrint::CancelPrintJob(napi_env env, napi_callback_info info)
233 {
234     PRINT_HILOGD("Enter CancelPrintJob---->");
235     auto context = std::make_shared<InnerPrintContext>();
236     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
237         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
238         napi_valuetype valuetype;
239         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
240         PRINT_ASSERT_BASE(env, valuetype == napi_string, "jobId is not a string", napi_string_expected);
241         std::string jobId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
242         if (jobId == "") {
243             PRINT_HILOGE("Parse JobId error!");
244             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
245             return napi_invalid_arg;
246         }
247         context->jobId = jobId;
248         return napi_ok;
249     };
250     auto output = [context](napi_env env, napi_value *result) -> napi_status {
251         napi_status status = napi_get_boolean(env, context->result, result);
252         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
253         return status;
254     };
255     auto exec = [context](PrintAsyncCall::Context *ctx) {
256         context->printJob.Dump();
257         int32_t ret = PrintManagerClient::GetInstance()->CancelPrintJob(context->jobId);
258         context->result = ret == E_PRINT_NONE;
259         if (ret != E_PRINT_NONE) {
260             PRINT_HILOGE("Failed to start print job");
261             context->SetErrorIndex(ret);
262         }
263     };
264     context->SetAction(std::move(input), std::move(output));
265     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
266     return asyncCall.Call(env, exec);
267 }
268 
RequestPreview(napi_env env,napi_callback_info info)269 napi_value NapiInnerPrint::RequestPreview(napi_env env, napi_callback_info info)
270 {
271     PRINT_HILOGD("Enter ---->");
272     auto context = std::make_shared<InnerPrintContext>();
273     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
274         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
275         auto printJobPtr = PrintJobHelper::BuildFromJs(env, argv[NapiPrintUtils::INDEX_ZERO]);
276         if (printJobPtr == nullptr) {
277             PRINT_HILOGE("ParseJob type error!");
278             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
279             return napi_invalid_arg;
280         }
281         context->printJob = *printJobPtr;
282         return napi_ok;
283     };
284     auto output = [context](napi_env env, napi_value *result) -> napi_status {
285         napi_status status = napi_create_string_utf8(env, context->previewResult.c_str(), NAPI_AUTO_LENGTH, result);
286         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
287         return status;
288     };
289     auto exec = [context](PrintAsyncCall::Context *ctx) {
290         PRINT_HILOGD("exec----");
291         context->printJob.Dump();
292         int32_t ret = PrintManagerClient::GetInstance()->RequestPreview(context->printJob, context->previewResult);
293         context->result = ret == E_PRINT_NONE;
294         if (ret != E_PRINT_NONE) {
295             PRINT_HILOGE("Failed to request preview of print job");
296             context->SetErrorIndex(ret);
297         }
298     };
299     context->SetAction(std::move(input), std::move(output));
300     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
301     return asyncCall.Call(env, exec);
302 }
303 
QueryCapability(napi_env env,napi_callback_info info)304 napi_value NapiInnerPrint::QueryCapability(napi_env env, napi_callback_info info)
305 {
306     PRINT_HILOGD("Enter QueryCapability---->");
307     auto context = std::make_shared<InnerPrintContext>();
308     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
309         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
310         napi_valuetype valuetype;
311         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
312         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId number is not a string", napi_string_expected);
313         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
314         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
315         context->printerId = printerId;
316         return napi_ok;
317     };
318     auto output = [context](napi_env env, napi_value *result) -> napi_status {
319         napi_status status = napi_get_boolean(env, context->result, result);
320         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
321         return status;
322     };
323     auto exec = [context](PrintAsyncCall::Context *ctx) {
324         int32_t ret = PrintManagerClient::GetInstance()->QueryPrinterCapability(context->printerId);
325         context->result = ret == E_PRINT_NONE;
326         if (ret != E_PRINT_NONE) {
327             PRINT_HILOGE("Failed to query capability of printer");
328             context->SetErrorIndex(ret);
329         }
330     };
331     context->SetAction(std::move(input), std::move(output));
332     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
333     return asyncCall.Call(env, exec);
334 }
335 
QueryAllPrintJob(napi_env env,napi_callback_info info)336 napi_value NapiInnerPrint::QueryAllPrintJob(napi_env env, napi_callback_info info)
337 {
338     PRINT_HILOGD("Enter QueryAllPrintJob---->");
339     auto context = std::make_shared<InnerPrintContext>();
340     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
341         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
342         return napi_ok;
343     };
344     auto output = [context](napi_env env, napi_value *result) -> napi_status {
345         PRINT_HILOGD("ouput enter---->");
346         napi_status status = napi_create_array(env, result);
347         uint32_t index = 0;
348         for (auto printJob : context->allPrintJobs) {
349             PRINT_HILOGD("PrinterId = %{public}s", printJob.GetPrinterId().c_str());
350             PRINT_HILOGD("JobId = %{public}s", printJob.GetJobId().c_str());
351             status = napi_set_element(env, *result, index++, PrintJobHelper::MakeJsObject(env, printJob));
352         }
353         return napi_ok;
354     };
355     auto exec = [context](PrintAsyncCall::Context *ctx) {
356         int32_t ret = PrintManagerClient::GetInstance()->QueryAllPrintJob(context->allPrintJobs);
357         context->result = ret == E_PRINT_NONE;
358         if (ret != E_PRINT_NONE) {
359             PRINT_HILOGE("Failed to query printJobList");
360             context->SetErrorIndex(ret);
361         }
362     };
363     context->SetAction(std::move(input), std::move(output));
364     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
365     return asyncCall.Call(env, exec);
366 }
367 
QueryPrintJobById(napi_env env,napi_callback_info info)368 napi_value NapiInnerPrint::QueryPrintJobById(napi_env env, napi_callback_info info)
369 {
370     PRINT_HILOGD("Enter QueryPrintJobById---->");
371     auto context = std::make_shared<InnerPrintContext>();
372     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
373         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
374         napi_valuetype valuetype;
375         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
376         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId number is not a string", napi_string_expected);
377         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
378         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
379         context->printerId = printerId;
380         return napi_ok;
381     };
382     auto output = [context](napi_env env, napi_value *result) -> napi_status {
383         PRINT_HILOGD("ouput enter---->");
384         *result = PrintJobHelper::MakeJsObject(env, context->printJob);
385         return napi_ok;
386     };
387     auto exec = [context](PrintAsyncCall::Context *ctx) {
388         int32_t ret = PrintManagerClient::GetInstance()->QueryPrintJobById(context->printerId, context->printJob);
389         context->result = ret == E_PRINT_NONE;
390         if (ret != E_PRINT_NONE) {
391             PRINT_HILOGE("Failed to query printJob from printList");
392             context->SetErrorIndex(ret);
393         }
394     };
395     context->SetAction(std::move(input), std::move(output));
396     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
397     return asyncCall.Call(env, exec);
398 }
399 
On(napi_env env,napi_callback_info info)400 napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info)
401 {
402     PRINT_HILOGD("Enter ---->");
403     size_t argc = NapiPrintUtils::MAX_ARGC;
404     napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
405     napi_value thisVal = nullptr;
406     void *data = nullptr;
407     PRINT_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, &data));
408     PRINT_ASSERT(env, argc == NapiPrintUtils::ARGC_TWO, "need 2 parameter!");
409 
410     napi_valuetype valuetype;
411     PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype));
412     PRINT_ASSERT(env, valuetype == napi_string, "type is not a string");
413     std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
414     PRINT_HILOGD("type : %{public}s", type.c_str());
415 
416     if (!NapiInnerPrint::IsSupportType(type)) {
417         PRINT_HILOGE("Event On type : %{public}s not support", type.c_str());
418         return nullptr;
419     }
420 
421     valuetype = napi_undefined;
422     napi_typeof(env, argv[1], &valuetype);
423     PRINT_ASSERT(env, valuetype == napi_function, "callback is not a function");
424 
425     napi_ref callbackRef = NapiPrintUtils::CreateReference(env, argv[1]);
426     sptr<IPrintCallback> callback = new (std::nothrow) PrintCallback(env, callbackRef);
427     if (callback == nullptr) {
428         PRINT_HILOGE("create print callback object fail");
429         return nullptr;
430     }
431     int32_t ret = PrintManagerClient::GetInstance()->On("", type, callback);
432     if (ret != E_PRINT_NONE) {
433         PRINT_HILOGE("Failed to register event");
434         return nullptr;
435     }
436     return nullptr;
437 }
438 
Off(napi_env env,napi_callback_info info)439 napi_value NapiInnerPrint::Off(napi_env env, napi_callback_info info)
440 {
441     PRINT_HILOGD("Enter ---->");
442     auto context = std::make_shared<InnerPrintContext>();
443     auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
444         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
445         napi_valuetype valuetype;
446         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
447         PRINT_ASSERT_BASE(env, valuetype == napi_string, "type is not a string", napi_string_expected);
448         std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
449         if (!NapiInnerPrint::IsSupportType(type)) {
450             PRINT_HILOGE("Event Off type : %{public}s not support", context->type.c_str());
451             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
452             return napi_invalid_arg;
453         }
454         context->type = type;
455         PRINT_HILOGD("event type : %{public}s", context->type.c_str());
456         return napi_ok;
457     };
458     auto output = [context](napi_env env, napi_value *result) -> napi_status {
459         napi_status status = napi_get_boolean(env, context->result, result);
460         PRINT_HILOGD("context->result = %{public}d", context->result);
461         return status;
462     };
463     auto exec = [context](PrintAsyncCall::Context *ctx) {
464         int32_t ret = PrintManagerClient::GetInstance()->Off("", context->type);
465         context->result = ret == E_PRINT_NONE;
466         if (ret != E_PRINT_NONE) {
467             PRINT_HILOGE("Failed to unregister event");
468             context->SetErrorIndex(ret);
469         }
470     };
471     context->SetAction(std::move(input), std::move(output));
472     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
473     return asyncCall.Call(env, exec);
474 }
475 
IsSupportType(const std::string & type)476 bool NapiInnerPrint::IsSupportType(const std::string& type)
477 {
478     if (type == PRINTER_EVENT_TYPE || type == PRINTJOB_EVENT_TYPE || type == EXTINFO_EVENT_TYPE) {
479         return true;
480     }
481     return false;
482 }
483 } // namespace OHOS::Print
484