• 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_attributes_helper.h"
23 #include "print_log.h"
24 #include "print_manager_client.h"
25 #include "print_task.h"
26 #include "iprint_adapter_inner.h"
27 #include "printer_info_helper.h"
28 #include "printer_preferences_helper.h"
29 
30 namespace OHOS::Print {
31 const std::string PRINTER_EVENT_TYPE = "printerStateChange";
32 const std::string PRINTJOB_EVENT_TYPE = "jobStateChange";
33 const std::string EXTINFO_EVENT_TYPE = "extInfoChange";
34 const uint32_t ARRAY_LENGTH_ONE_THOUSAND = 1000;
35 
QueryExtensionInfo(napi_env env,napi_callback_info info)36 napi_value NapiInnerPrint::QueryExtensionInfo(napi_env env, napi_callback_info info)
37 {
38     PRINT_HILOGD("Enter ---->");
39     auto context = std::make_shared<InnerPrintContext>();
40     auto input =
41         [context](
42             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
43         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
44         return napi_ok;
45     };
46     auto output = [context](napi_env env, napi_value *result) -> napi_status {
47         PRINT_HILOGD("ouput enter---->");
48         napi_status status = napi_create_array(env, result);
49         uint32_t index = 0;
50         for (auto extInfo : context->allExtensionInfos) {
51             PRINT_HILOGD("ExtensionId = %{public}s", extInfo.GetExtensionId().c_str());
52             PRINT_HILOGD("VendorId = %{public}s", extInfo.GetVendorId().c_str());
53             PRINT_HILOGD("VendorName = %{public}s", extInfo.GetVendorName().c_str());
54             PRINT_HILOGD("VendorIcon = %{public}d", extInfo.GetVendorIcon());
55             PRINT_HILOGD("Version = %{public}s", extInfo.GetVersion().c_str());
56             status = napi_set_element(env, *result, index++, PrintExtensionInfoHelper::MakeJsObject(env, extInfo));
57         }
58         return napi_ok;
59     };
60     auto exec = [context](PrintAsyncCall::Context *ctx) {
61         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
62             PRINT_HILOGE("Non-system applications use system APIS!");
63             context->result = false;
64             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
65             return;
66         }
67         int32_t ret = PrintManagerClient::GetInstance()->QueryAllExtension(context->allExtensionInfos);
68         context->result = ret == E_PRINT_NONE;
69         if (ret != E_PRINT_NONE) {
70             PRINT_HILOGE("Failed to query all ext info");
71             context->SetErrorIndex(ret);
72         }
73     };
74     context->SetAction(std::move(input), std::move(output));
75     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
76     return asyncCall.Call(env, exec);
77 }
78 
StartDiscovery(napi_env env,napi_callback_info info)79 napi_value NapiInnerPrint::StartDiscovery(napi_env env, napi_callback_info info)
80 {
81     PRINT_HILOGD("Enter StartDiscovery---->");
82     auto context = std::make_shared<InnerPrintContext>();
83     auto input =
84         [context](
85             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
86         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
87         bool isArray = false;
88         napi_is_array(env, argv[NapiPrintUtils::INDEX_ZERO], &isArray);
89         PRINT_ASSERT_BASE(env, isArray, " is not array!", napi_array_expected);
90 
91         uint32_t len = 0;
92         napi_get_array_length(env, argv[0], &len);
93         if (len > ARRAY_LENGTH_ONE_THOUSAND) {
94             PRINT_HILOGE("The length of array is too long");
95             return napi_invalid_arg;
96         }
97 
98         for (uint32_t index = 0; index < len; index++) {
99             napi_value value;
100             napi_get_element(env, argv[NapiPrintUtils::INDEX_ZERO], index, &value);
101             std::string extensionId = NapiPrintUtils::GetStringFromValueUtf8(env, value);
102             PRINT_HILOGD("output for :---- extensionList value is :[%{public}s]", extensionId.c_str());
103             if (extensionId != "") {
104                 context->extensionList.emplace_back(extensionId);
105             }
106         }
107         return napi_ok;
108     };
109     auto output = [context](napi_env env, napi_value *result) -> napi_status {
110         napi_status status = napi_get_boolean(env, context->result, result);
111         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
112         return status;
113     };
114     auto exec = [context](PrintAsyncCall::Context *ctx) {
115         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
116             PRINT_HILOGE("Non-system applications use system APIS!");
117             context->result = false;
118             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
119             return;
120         }
121         int32_t ret = PrintManagerClient::GetInstance()->StartDiscoverPrinter(context->extensionList);
122         context->result = ret == E_PRINT_NONE;
123         if (ret != E_PRINT_NONE) {
124             PRINT_HILOGE("Failed to start discover printer");
125             context->SetErrorIndex(ret);
126         }
127     };
128     context->SetAction(std::move(input), std::move(output));
129     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
130     return asyncCall.Call(env, exec);
131 }
132 
StopDiscovery(napi_env env,napi_callback_info info)133 napi_value NapiInnerPrint::StopDiscovery(napi_env env, napi_callback_info info)
134 {
135     PRINT_HILOGD("Enter StopDiscovery---->");
136     auto context = std::make_shared<InnerPrintContext>();
137     auto input =
138         [context](
139             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
140         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
141         return napi_ok;
142     };
143     auto output = [context](napi_env env, napi_value *result) -> napi_status {
144         napi_status status = napi_get_boolean(env, context->result, result);
145         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
146         return status;
147     };
148     auto exec = [context](PrintAsyncCall::Context *ctx) {
149         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
150             PRINT_HILOGE("Non-system applications use system APIS!");
151             context->result = false;
152             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
153             return;
154         }
155         int32_t ret = PrintManagerClient::GetInstance()->StopDiscoverPrinter();
156         context->result = ret == E_PRINT_NONE;
157         if (ret != E_PRINT_NONE) {
158             PRINT_HILOGE("Failed to stop discover printer");
159             context->SetErrorIndex(ret);
160         }
161     };
162     context->SetAction(std::move(input), std::move(output));
163     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
164     return asyncCall.Call(env, exec);
165 }
166 
ConnectPrinter(napi_env env,napi_callback_info info)167 napi_value NapiInnerPrint::ConnectPrinter(napi_env env, napi_callback_info info)
168 {
169     PRINT_HILOGD("Enter ConnectPrinter---->");
170     auto context = std::make_shared<InnerPrintContext>();
171     auto input =
172         [context](
173             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
174         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
175         napi_valuetype valuetype;
176         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
177         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
178         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
179         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
180         context->printerId = printerId;
181         return napi_ok;
182     };
183     auto output = [context](napi_env env, napi_value *result) -> napi_status {
184         napi_status status = napi_get_boolean(env, context->result, result);
185         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
186         return status;
187     };
188     auto exec = [context](PrintAsyncCall::Context *ctx) {
189         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
190             PRINT_HILOGE("Non-system applications use system APIS!");
191             context->result = false;
192             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
193             return;
194         }
195         int32_t ret = PrintManagerClient::GetInstance()->ConnectPrinter(context->printerId);
196         context->result = ret == E_PRINT_NONE;
197         if (ret != E_PRINT_NONE) {
198             PRINT_HILOGE("Failed to connect the printer");
199             context->SetErrorIndex(ret);
200         }
201     };
202     context->SetAction(std::move(input), std::move(output));
203     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
204     return asyncCall.Call(env, exec);
205 }
206 
DisconnectPrinter(napi_env env,napi_callback_info info)207 napi_value NapiInnerPrint::DisconnectPrinter(napi_env env, napi_callback_info info)
208 {
209     PRINT_HILOGD("Enter DisconnectPrinter---->");
210     auto context = std::make_shared<InnerPrintContext>();
211     auto input =
212         [context](
213             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
214         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
215         napi_valuetype valuetype;
216         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
217         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
218         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
219         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
220         context->printerId = printerId;
221         return napi_ok;
222     };
223     auto output = [context](napi_env env, napi_value *result) -> napi_status {
224         napi_status status = napi_get_boolean(env, context->result, result);
225         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
226         return status;
227     };
228     auto exec = [context](PrintAsyncCall::Context *ctx) {
229         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
230             PRINT_HILOGE("Non-system applications use system APIS!");
231             context->result = false;
232             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
233             return;
234         }
235         int32_t ret = PrintManagerClient::GetInstance()->DisconnectPrinter(context->printerId);
236         context->result = ret == E_PRINT_NONE;
237         if (ret != E_PRINT_NONE) {
238             PRINT_HILOGE("Failed to connect the printer");
239             context->SetErrorIndex(ret);
240         }
241     };
242     context->SetAction(std::move(input), std::move(output));
243     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
244     return asyncCall.Call(env, exec);
245 }
246 
StartPrintJob(napi_env env,napi_callback_info info)247 napi_value NapiInnerPrint::StartPrintJob(napi_env env, napi_callback_info info)
248 {
249     PRINT_HILOGD("Enter StartPrintJob---->");
250     auto context = std::make_shared<InnerPrintContext>();
251     auto input =
252         [context](
253             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
254         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
255         auto printJobPtr = PrintJobHelper::BuildFromJs(env, argv[NapiPrintUtils::INDEX_ZERO]);
256         if (printJobPtr == nullptr) {
257             PRINT_HILOGE("ParseJob type error!");
258             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
259             return napi_invalid_arg;
260         }
261         context->printJob = *printJobPtr;
262         return napi_ok;
263     };
264     auto output = [context](napi_env env, napi_value *result) -> napi_status {
265         napi_status status = napi_get_boolean(env, context->result, result);
266         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
267         return status;
268     };
269     auto exec = [context](PrintAsyncCall::Context *ctx) {
270         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
271             PRINT_HILOGE("Non-system applications use system APIS!");
272             context->result = false;
273             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
274             return;
275         }
276         context->printJob.Dump();
277         int32_t ret = PrintManagerClient::GetInstance()->StartPrintJob(context->printJob);
278         context->result = ret == E_PRINT_NONE;
279         if (ret != E_PRINT_NONE) {
280             PRINT_HILOGE("Failed to start print job");
281             context->SetErrorIndex(ret);
282         }
283     };
284     context->SetAction(std::move(input), std::move(output));
285     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
286     return asyncCall.Call(env, exec);
287 }
288 
CancelPrintJob(napi_env env,napi_callback_info info)289 napi_value NapiInnerPrint::CancelPrintJob(napi_env env, napi_callback_info info)
290 {
291     PRINT_HILOGD("Enter CancelPrintJob---->");
292     auto context = std::make_shared<InnerPrintContext>();
293     auto input =
294         [context](
295             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
296         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
297         napi_valuetype valuetype;
298         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
299         PRINT_ASSERT_BASE(env, valuetype == napi_string, "jobId is not a string", napi_string_expected);
300         std::string jobId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
301         if (jobId == "") {
302             PRINT_HILOGE("Parse JobId error!");
303             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
304             return napi_invalid_arg;
305         }
306         context->jobId = jobId;
307         return napi_ok;
308     };
309     auto output = [context](napi_env env, napi_value *result) -> napi_status {
310         napi_status status = napi_get_boolean(env, context->result, result);
311         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
312         return status;
313     };
314     auto exec = [context](PrintAsyncCall::Context *ctx) {
315         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
316             PRINT_HILOGE("Non-system applications use system APIS!");
317             context->result = false;
318             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
319             return;
320         }
321         context->printJob.Dump();
322         int32_t ret = PrintManagerClient::GetInstance()->CancelPrintJob(context->jobId);
323         context->result = ret == E_PRINT_NONE;
324         if (ret != E_PRINT_NONE) {
325             PRINT_HILOGE("Failed to start print job");
326             context->SetErrorIndex(ret);
327         }
328     };
329     context->SetAction(std::move(input), std::move(output));
330     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
331     return asyncCall.Call(env, exec);
332 }
333 
RequestPreview(napi_env env,napi_callback_info info)334 napi_value NapiInnerPrint::RequestPreview(napi_env env, napi_callback_info info)
335 {
336     PRINT_HILOGD("Enter ---->");
337     auto context = std::make_shared<InnerPrintContext>();
338     auto input =
339         [context](
340             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
341         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
342         auto printJobPtr = PrintJobHelper::BuildFromJs(env, argv[NapiPrintUtils::INDEX_ZERO]);
343         if (printJobPtr == nullptr) {
344             PRINT_HILOGE("ParseJob type error!");
345             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
346             return napi_invalid_arg;
347         }
348         context->printJob = *printJobPtr;
349         return napi_ok;
350     };
351     auto output = [context](napi_env env, napi_value *result) -> napi_status {
352         napi_status status = napi_create_string_utf8(env, context->previewResult.c_str(), NAPI_AUTO_LENGTH, result);
353         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
354         return status;
355     };
356     auto exec = [context](PrintAsyncCall::Context *ctx) {
357         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
358             PRINT_HILOGE("Non-system applications use system APIS!");
359             context->result = false;
360             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
361             return;
362         }
363         PRINT_HILOGD("exec----");
364         context->printJob.Dump();
365         int32_t ret = PrintManagerClient::GetInstance()->RequestPreview(context->printJob, context->previewResult);
366         context->result = ret == E_PRINT_NONE;
367         if (ret != E_PRINT_NONE) {
368             PRINT_HILOGE("Failed to request preview of print job");
369             context->SetErrorIndex(ret);
370         }
371     };
372     context->SetAction(std::move(input), std::move(output));
373     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
374     return asyncCall.Call(env, exec);
375 }
376 
QueryCapability(napi_env env,napi_callback_info info)377 napi_value NapiInnerPrint::QueryCapability(napi_env env, napi_callback_info info)
378 {
379     PRINT_HILOGD("Enter QueryCapability---->");
380     auto context = std::make_shared<InnerPrintContext>();
381     auto input =
382         [context](
383             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
384         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
385         napi_valuetype valuetype;
386         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
387         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId number is not a string", napi_string_expected);
388         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
389         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
390         context->printerId = printerId;
391         return napi_ok;
392     };
393     auto output = [context](napi_env env, napi_value *result) -> napi_status {
394         napi_status status = napi_get_boolean(env, context->result, result);
395         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
396         return status;
397     };
398     auto exec = [context](PrintAsyncCall::Context *ctx) {
399         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
400             PRINT_HILOGE("Non-system applications use system APIS!");
401             context->result = false;
402             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
403             return;
404         }
405         int32_t ret = PrintManagerClient::GetInstance()->QueryPrinterCapability(context->printerId);
406         context->result = ret == E_PRINT_NONE;
407         if (ret != E_PRINT_NONE) {
408             PRINT_HILOGE("Failed to query capability of printer");
409             context->SetErrorIndex(ret);
410         }
411     };
412     context->SetAction(std::move(input), std::move(output));
413     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
414     return asyncCall.Call(env, exec);
415 }
416 
QueryAllPrintJob(napi_env env,napi_callback_info info)417 napi_value NapiInnerPrint::QueryAllPrintJob(napi_env env, napi_callback_info info)
418 {
419     PRINT_HILOGD("Enter QueryAllPrintJob---->");
420     auto context = std::make_shared<InnerPrintContext>();
421     auto input =
422         [context](
423             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
424         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
425         return napi_ok;
426     };
427     auto output = [context](napi_env env, napi_value *result) -> napi_status {
428         PRINT_HILOGD("ouput enter---->");
429         napi_status status = napi_create_array(env, result);
430         uint32_t index = 0;
431         for (auto printJob : context->allPrintJobs) {
432             PRINT_HILOGD("PrinterId = %{public}s", printJob.GetPrinterId().c_str());
433             PRINT_HILOGD("JobId = %{public}s", printJob.GetJobId().c_str());
434             status = napi_set_element(env, *result, index++, PrintJobHelper::MakeJsObject(env, printJob));
435         }
436         return napi_ok;
437     };
438     auto exec = [context](PrintAsyncCall::Context *ctx) {
439         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
440             PRINT_HILOGE("Non-system applications use system APIS!");
441             context->result = false;
442             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
443             return;
444         }
445         int32_t ret = PrintManagerClient::GetInstance()->QueryAllPrintJob(context->allPrintJobs);
446         context->result = ret == E_PRINT_NONE;
447         if (ret != E_PRINT_NONE) {
448             PRINT_HILOGE("Failed to query printJobList");
449             context->SetErrorIndex(ret);
450         }
451     };
452     context->SetAction(std::move(input), std::move(output));
453     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
454     return asyncCall.Call(env, exec);
455 }
456 
QueryPrintJobById(napi_env env,napi_callback_info info)457 napi_value NapiInnerPrint::QueryPrintJobById(napi_env env, napi_callback_info info)
458 {
459     PRINT_HILOGD("Enter QueryPrintJobById---->");
460     auto context = std::make_shared<InnerPrintContext>();
461     auto input =
462         [context](
463             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
464         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
465         napi_valuetype valuetype;
466         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
467         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId number is not a string", napi_string_expected);
468         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
469         PRINT_HILOGD("printerId : %{private}s", printerId.c_str());
470         context->printerId = printerId;
471         return napi_ok;
472     };
473     auto output = [context](napi_env env, napi_value *result) -> napi_status {
474         PRINT_HILOGD("ouput enter---->");
475         *result = PrintJobHelper::MakeJsObject(env, context->printJob);
476         return napi_ok;
477     };
478     auto exec = [context](PrintAsyncCall::Context *ctx) {
479         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
480             PRINT_HILOGE("Non-system applications use system APIS!");
481             context->result = false;
482             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
483             return;
484         }
485         int32_t ret = PrintManagerClient::GetInstance()->QueryPrintJobById(context->printerId, context->printJob);
486         context->result = ret == E_PRINT_NONE;
487         if (ret != E_PRINT_NONE) {
488             PRINT_HILOGE("Failed to query printJob from printList");
489             context->SetErrorIndex(ret);
490         }
491     };
492     context->SetAction(std::move(input), std::move(output));
493     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
494     return asyncCall.Call(env, exec);
495 }
496 
SetPrinterPreference(napi_env env,napi_callback_info info)497 napi_value NapiInnerPrint::SetPrinterPreference(napi_env env, napi_callback_info info)
498 {
499     PRINT_HILOGI("Enter SetPrinterPreference---->");
500     auto context = std::make_shared<InnerPrintContext>();
501     if (context == nullptr) {
502         PRINT_HILOGE("InnerPrintContext context nullptr");
503         return nullptr;
504     }
505     auto input =
506         [context](
507             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
508         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_TWO, " should 2 parameter!", napi_invalid_arg);
509         napi_valuetype valuetype;
510         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
511         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
512         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ONE], &valuetype), napi_invalid_arg);
513         PRINT_ASSERT_BASE(env, valuetype == napi_object, "printerPreference is not an object", napi_string_expected);
514         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
515         auto preferencesPtr = PrinterPreferencesHelper::BuildFromJs(env, argv[NapiPrintUtils::INDEX_ONE]);
516         if (preferencesPtr == nullptr) {
517             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
518             PRINT_HILOGE("printerPreference format error!");
519             return napi_invalid_arg;
520         }
521         context->printerId = printerId;
522         context->printerPreference = *preferencesPtr;
523         return napi_ok;
524     };
525     auto output = [context](napi_env env, napi_value *result) -> napi_status {
526         napi_status status = napi_get_boolean(env, context->result, result);
527         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
528         return status;
529     };
530     auto exec = [context](PrintAsyncCall::Context *ctx) {
531         int32_t ret = PrintManagerClient::GetInstance()->SetPrinterPreference(context->printerId,
532             context->printerPreference);
533         context->result = ret == E_PRINT_NONE;
534         if (ret != E_PRINT_NONE) {
535             PRINT_HILOGE("Failed to SetPrinterPreference");
536             context->SetErrorIndex(ret);
537         }
538     };
539     context->SetAction(std::move(input), std::move(output));
540     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
541     return asyncCall.Call(env, exec);
542 }
543 
On(napi_env env,napi_callback_info info)544 napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info)
545 {
546     PRINT_HILOGD("Enter ---->");
547     size_t argc = NapiPrintUtils::MAX_ARGC;
548     napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
549     napi_value thisVal = nullptr;
550     void *data = nullptr;
551     PRINT_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, &data));
552     PRINT_ASSERT(env, argc == NapiPrintUtils::ARGC_TWO, "need 2 parameter!");
553 
554     napi_valuetype valuetype;
555     PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype));
556     PRINT_ASSERT(env, valuetype == napi_string, "type is not a string");
557     std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
558     PRINT_HILOGD("type : %{public}s", type.c_str());
559 
560     if (type == PRINTER_EVENT_TYPE || type == PRINTJOB_EVENT_TYPE ||
561         type == EXTINFO_EVENT_TYPE) {
562         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
563             PRINT_HILOGE("Non-system applications use system APIS!");
564             NapiThrowError(env, E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
565             return nullptr;
566         }
567     }
568 
569     if (!NapiInnerPrint::IsSupportType(type)) {
570         PRINT_HILOGE("Event On type : %{public}s not support", type.c_str());
571         NapiThrowError(env, E_PRINT_INVALID_PARAMETER);
572         return nullptr;
573     }
574 
575     valuetype = napi_undefined;
576     napi_typeof(env, argv[1], &valuetype);
577     PRINT_ASSERT(env, valuetype == napi_function, "callback is not a function");
578 
579     napi_ref callbackRef = NapiPrintUtils::CreateReference(env, argv[1]);
580     sptr<IPrintCallback> callback = new (std::nothrow) PrintCallback(env, callbackRef);
581     if (callback == nullptr) {
582         NapiPrintUtils::DeleteReference(env, callbackRef);
583         PRINT_HILOGE("create print callback object fail");
584         return nullptr;
585     }
586     int32_t ret = PrintManagerClient::GetInstance()->On("", type, callback);
587     if (ret != E_PRINT_NONE) {
588         PRINT_HILOGE("Failed to register event");
589         NapiThrowError(env, ret);
590         return nullptr;
591     }
592     return nullptr;
593 }
594 
Off(napi_env env,napi_callback_info info)595 napi_value NapiInnerPrint::Off(napi_env env, napi_callback_info info)
596 {
597     PRINT_HILOGD("Enter ---->");
598     size_t argc = NapiPrintUtils::MAX_ARGC;
599     napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
600     napi_value thisVal = nullptr;
601     void *data = nullptr;
602     PRINT_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, &data));
603     PRINT_ASSERT(env, argc == NapiPrintUtils::ARGC_ONE || argc == NapiPrintUtils::ARGC_TWO, "need 1-2 parameter!");
604 
605     napi_valuetype valuetype;
606     PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype));
607     PRINT_ASSERT(env, valuetype == napi_string, "type is not a string");
608     std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
609     PRINT_HILOGD("type : %{public}s", type.c_str());
610 
611     if (type == PRINTER_EVENT_TYPE || type == PRINTJOB_EVENT_TYPE ||
612         type == EXTINFO_EVENT_TYPE) {
613         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
614             PRINT_HILOGE("Non-system applications use system APIS!");
615             NapiThrowError(env, E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
616             return nullptr;
617         }
618     }
619 
620     if (!NapiInnerPrint::IsSupportType(type)) {
621         PRINT_HILOGE("Event Off type : %{public}s not support", type.c_str());
622         NapiThrowError(env, E_PRINT_INVALID_PARAMETER);
623         return nullptr;
624     }
625 
626     if (argc == NapiPrintUtils::ARGC_TWO) {
627         valuetype = napi_undefined;
628         napi_typeof(env, argv[1], &valuetype);
629         PRINT_ASSERT(env, valuetype == napi_function, "callback is not a function");
630     }
631 
632     int32_t ret = PrintManagerClient::GetInstance()->Off("", type);
633     if (ret != E_PRINT_NONE) {
634         PRINT_HILOGE("Failed to unregister event");
635         NapiThrowError(env, ret);
636         return nullptr;
637     }
638     return nullptr;
639 }
640 
StartGetPrintFile(napi_env env,napi_callback_info info)641 napi_value NapiInnerPrint::StartGetPrintFile(napi_env env, napi_callback_info info)
642 {
643     PRINT_HILOGI("StartGetPrintFile start ---->");
644     if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
645         PRINT_HILOGE("Non-system applications use system APIS!");
646         NapiThrowError(env, E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
647         return nullptr;
648     }
649     napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
650     size_t argc = NapiPrintUtils::GetJsVal(env, info, argv, NapiPrintUtils::MAX_ARGC);
651     PRINT_ASSERT(env, argc == NapiPrintUtils::ARGC_FOUR, "StartGetPrintFile need 4 parameter!");
652 
653     napi_valuetype valuetype;
654     PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype));
655     PRINT_ASSERT(env, valuetype == napi_string, "type is not a string");
656     std::string jobId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
657 
658     if (static_cast<uint32_t>(argc) > NapiPrintUtils::INDEX_THREE) {
659         napi_ref callbackRef = NapiPrintUtils::CreateReference(env, argv[NapiPrintUtils::INDEX_THREE]);
660         sptr<IPrintCallback> callback = new (std::nothrow) PrintCallback(env, callbackRef);
661         if (callback == nullptr) {
662             NapiPrintUtils::DeleteReference(env, callbackRef);
663             PRINT_HILOGE("create startGetPrintFile callback object fail");
664             return nullptr;
665         }
666         int32_t retCallback = PrintManagerClient::GetInstance()->On("", PRINT_GET_FILE_CALLBACK_ADAPTER, callback);
667         if (retCallback != E_PRINT_NONE) {
668             PRINT_HILOGE("Failed to register startGetPrintFile callback");
669             NapiThrowError(env, retCallback);
670             return nullptr;
671         }
672     }
673 
674     auto printAttributes = PrintAttributesHelper::BuildFromJs(env, argv[1]);
675     if (printAttributes == nullptr) {
676         PRINT_HILOGE("printAttributes is nullptr");
677         return nullptr;
678     }
679     if (static_cast<uint32_t>(argc) > NapiPrintUtils::INDEX_TWO) {
680         uint32_t fd = NapiPrintUtils::GetUint32FromValue(env, argv[NapiPrintUtils::INDEX_TWO]);
681         int32_t ret = PrintManagerClient::GetInstance()->StartGetPrintFile(jobId, *printAttributes, fd);
682         if (ret != E_PRINT_NONE) {
683             PRINT_HILOGE("Failed to StartGetPrintFile");
684             NapiThrowError(env, ret);
685             return nullptr;
686         }
687     }
688     return nullptr;
689 }
690 
NotifyPrintService(napi_env env,napi_callback_info info)691 napi_value NapiInnerPrint::NotifyPrintService(napi_env env, napi_callback_info info)
692 {
693     PRINT_HILOGI("Enter NotifyPrintService---->");
694     auto context = std::make_shared<InnerPrintContext>();
695     auto input =
696         [context](
697             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
698         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_TWO, " should 2 parameter!", napi_invalid_arg);
699         napi_valuetype valuetype;
700         PRINT_CALL_BASE(env, napi_typeof(env, argv[0], &valuetype), napi_invalid_arg);
701         PRINT_ASSERT_BASE(env, valuetype == napi_string, "jobId is not a string", napi_string_expected);
702         PRINT_CALL_BASE(env, napi_typeof(env, argv[1], &valuetype), napi_invalid_arg);
703         PRINT_ASSERT_BASE(env, valuetype == napi_string, "info type is not a string", napi_string_expected);
704         std::string jobId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
705         std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[1]);
706         if (type == "") {
707             PRINT_HILOGE("Parse type error!");
708             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
709             return napi_invalid_arg;
710         }
711         context->jobId = jobId;
712         context->type = type;
713         return napi_ok;
714     };
715     auto output = [context](napi_env env, napi_value *result) -> napi_status {
716         napi_status status = napi_get_boolean(env, context->result, result);
717         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
718         return status;
719     };
720     auto exec = [context](PrintAsyncCall::Context *ctx) {
721         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
722             PRINT_HILOGE("Non-system applications use system APIS!");
723             context->result = false;
724             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
725             return;
726         }
727         int32_t ret = PrintManagerClient::GetInstance()->NotifyPrintService(context->jobId, context->type);
728         context->result = ret == E_PRINT_NONE;
729         if (ret != E_PRINT_NONE) {
730             PRINT_HILOGE("Failed to NotifyPrintService");
731             context->SetErrorIndex(ret);
732         }
733     };
734     context->SetAction(std::move(input), std::move(output));
735     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
736     return asyncCall.Call(env, exec);
737 }
738 
QueryAddedPrinter(napi_env env,napi_callback_info info)739 napi_value NapiInnerPrint::QueryAddedPrinter(napi_env env, napi_callback_info info)
740 {
741     PRINT_HILOGD("Enter QueryAddedPrinter---->");
742     auto context = std::make_shared<InnerPrintContext>();
743     auto input =
744         [context](
745             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
746         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ZERO, " should 0 parameter!", napi_invalid_arg);
747         return napi_ok;
748     };
749     auto output = [context](napi_env env, napi_value *result) -> napi_status {
750         PRINT_HILOGD("ouput enter---->");
751         napi_status status = napi_create_array(env, result);
752         uint32_t index = 0;
753         for (auto printerId : context->allPrinters) {
754             PRINT_HILOGD("PrinterId = %{public}s", printerId.c_str());
755             status = napi_set_element(env, *result, index++, NapiPrintUtils::CreateStringUtf8(env, printerId));
756         }
757         return napi_ok;
758     };
759     auto exec = [context](PrintAsyncCall::Context *ctx) {
760         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
761             PRINT_HILOGE("Non-system applications use system APIS!");
762             context->result = false;
763             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
764             return;
765         }
766         int32_t ret = PrintManagerClient::GetInstance()->QueryAddedPrinter(context->allPrinters);
767         context->result = ret == E_PRINT_NONE;
768         if (ret != E_PRINT_NONE) {
769             PRINT_HILOGE("Failed to query printerList");
770             context->SetErrorIndex(ret);
771         }
772     };
773     context->SetAction(std::move(input), std::move(output));
774     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
775     return asyncCall.Call(env, exec);
776 }
777 
QueryPrinterInfoByPrinterId(napi_env env,napi_callback_info info)778 napi_value NapiInnerPrint::QueryPrinterInfoByPrinterId(napi_env env, napi_callback_info info)
779 {
780     PRINT_HILOGD("Enter QueryPrinterInfoByPrinterId---->");
781     auto context = std::make_shared<InnerPrintContext>();
782     auto input =
783         [context](
784             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
785         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
786         napi_valuetype valuetype;
787         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
788         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId number is not a string", napi_string_expected);
789         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
790         PRINT_HILOGD("printerId : %{public}s", printerId.c_str());
791         context->printerId = printerId;
792         return napi_ok;
793     };
794     auto output = [context](napi_env env, napi_value *result) -> napi_status {
795         PRINT_HILOGD("ouput enter---->");
796         *result = PrinterInfoHelper::MakeJsObject(env, context->printerInfo);
797         return napi_ok;
798     };
799     auto exec = [context](PrintAsyncCall::Context *ctx) {
800         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
801             PRINT_HILOGE("Non-system applications use system APIS!");
802             context->result = false;
803             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
804             return;
805         }
806         int32_t ret =
807             PrintManagerClient::GetInstance()->QueryPrinterInfoByPrinterId(context->printerId, context->printerInfo);
808         context->result = ret == E_PRINT_NONE;
809         if (ret != E_PRINT_NONE) {
810             PRINT_HILOGE("Failed to query printerInfo from printerList");
811             context->SetErrorIndex(ret);
812         }
813     };
814     context->SetAction(std::move(input), std::move(output));
815     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
816     return asyncCall.Call(env, exec);
817 }
818 
NotifyPrintServiceEvent(napi_env env,napi_callback_info info)819 napi_value NapiInnerPrint::NotifyPrintServiceEvent(napi_env env, napi_callback_info info)
820 {
821     PRINT_HILOGI("Enter NotifyPrintServiceEvent---->");
822     auto context = std::make_shared<InnerPrintContext>();
823     auto input =
824         [context](
825             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
826             PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE || argc == NapiPrintUtils::ARGC_TWO,
827                               "should 1 or 2 parameter!", napi_invalid_arg);
828         napi_valuetype valuetype;
829         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
830         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ONE], &valuetype), napi_invalid_arg);
831         uint32_t event = NapiPrintUtils::GetUint32FromValue(env, argv[NapiPrintUtils::INDEX_ZERO]);
832         std::string jobId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ONE]);
833         PRINT_HILOGI("jobId: %{public}s, event : %{public}d", jobId.c_str(), event);
834         if (!IsValidApplicationEvent(event)) {
835             PRINT_HILOGE("invalid event");
836             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
837             return napi_invalid_arg;
838         }
839         context->jobId = jobId;
840         context->applicationEvent = event;
841         return napi_ok;
842     };
843     auto output = [context](napi_env env, napi_value *result) -> napi_status {
844         napi_status status = napi_get_boolean(env, context->result, result);
845         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
846         return status;
847     };
848     auto exec = [context](PrintAsyncCall::Context *ctx) {
849         if (!NapiPrintUtils::CheckCallerIsSystemApp()) {
850             PRINT_HILOGE("Non-system applications use system APIS!");
851             context->result = false;
852             context->SetErrorIndex(E_PRINT_ILLEGAL_USE_OF_SYSTEM_API);
853             return;
854         }
855         int32_t ret =
856             PrintManagerClient::GetInstance()->NotifyPrintServiceEvent(context->jobId, context->applicationEvent);
857         context->result = ret == E_PRINT_NONE;
858         if (ret != E_PRINT_NONE) {
859             PRINT_HILOGE("Failed to NotifyPrintServiceEvent");
860             context->SetErrorIndex(ret);
861         }
862     };
863     context->SetAction(std::move(input), std::move(output));
864     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
865     return asyncCall.Call(env, exec);
866 }
867 
SetDefaultPrinter(napi_env env,napi_callback_info info)868 napi_value NapiInnerPrint::SetDefaultPrinter(napi_env env, napi_callback_info info)
869 {
870     PRINT_HILOGD("Enter SetDefaultPrinter---->");
871     auto context = std::make_shared<InnerPrintContext>();
872     auto input =
873         [context](
874             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
875         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_TWO, " should 2 parameter!", napi_invalid_arg);
876         napi_valuetype valuetype;
877         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
878         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
879         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
880 
881         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ONE], &valuetype), napi_invalid_arg);
882         PRINT_ASSERT_BASE(env, valuetype == napi_number, "defaultPrinterType is not a number", napi_number_expected);
883         uint32_t type = NapiPrintUtils::GetUint32FromValue(env, argv[NapiPrintUtils::INDEX_ONE]);
884         if (!IsValidDefaultPrinterType(type)) {
885             PRINT_HILOGE("invalid defaultPrinterType");
886             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
887             return napi_invalid_arg;
888         }
889         PRINT_HILOGD("printerId : %{public}s", printerId.c_str());
890         context->printerId = printerId;
891         context->defaultPrinterType = type;
892         return napi_ok;
893     };
894     auto output = [context](napi_env env, napi_value *result) -> napi_status {
895         napi_status status = napi_get_boolean(env, context->result, result);
896         PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
897         return status;
898     };
899     auto exec = [context](PrintAsyncCall::Context *ctx) {
900         int32_t ret =
901             PrintManagerClient::GetInstance()->SetDefaultPrinter(context->printerId, context->defaultPrinterType);
902         context->result = ret == E_PRINT_NONE;
903         if (ret != E_PRINT_NONE) {
904             PRINT_HILOGE("Failed to set default printer");
905             context->SetErrorIndex(ret);
906         }
907     };
908     context->SetAction(std::move(input), std::move(output));
909     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
910     return asyncCall.Call(env, exec);
911 }
912 
GetAddedPrinterInfoById(napi_env env,napi_callback_info info)913 napi_value NapiInnerPrint::GetAddedPrinterInfoById(napi_env env, napi_callback_info info)
914 {
915     PRINT_HILOGD("Enter GetAddedPrinterInfoById---->");
916     auto context = std::make_shared<InnerPrintContext>();
917     auto input =
918         [context](
919             napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
920         PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
921         napi_valuetype valuetype;
922         PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
923         PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId number is not a string", napi_string_expected);
924         std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
925         if (printerId.empty()) {
926             PRINT_HILOGE("printerId is empty!");
927             context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
928             return napi_invalid_arg;
929         }
930         PRINT_HILOGD("printerId : %{public}s", printerId.c_str());
931         context->printerId = printerId;
932         return napi_ok;
933     };
934     auto output = [context](napi_env env, napi_value *result) -> napi_status {
935         PRINT_HILOGD("ouput enter---->");
936         *result = PrinterInfoHelper::MakeJsObject(env, context->printerInfo);
937         return napi_ok;
938     };
939     auto exec = [context](PrintAsyncCall::Context *ctx) {
940         int32_t ret =
941             PrintManagerClient::GetInstance()->QueryPrinterInfoByPrinterId(context->printerId, context->printerInfo);
942         context->result = ret == E_PRINT_NONE;
943         if (ret != E_PRINT_NONE) {
944             PRINT_HILOGE("Failed to query printerInfo from printerList");
945             context->SetErrorIndex(ret);
946         }
947     };
948     context->SetAction(std::move(input), std::move(output));
949     PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
950     return asyncCall.Call(env, exec);
951 }
952 
IsSupportType(const std::string & type)953 bool NapiInnerPrint::IsSupportType(const std::string &type)
954 {
955     if (type == PRINTER_EVENT_TYPE || type == PRINTJOB_EVENT_TYPE || type == EXTINFO_EVENT_TYPE ||
956         type == PRINTER_CHANGE_EVENT_TYPE) {
957         return true;
958     }
959     return false;
960 }
961 
IsValidApplicationEvent(uint32_t event)962 bool NapiInnerPrint::IsValidApplicationEvent(uint32_t event)
963 {
964     if (event >= APPLICATION_CREATED && event <= APPLICATION_CLOSED_FOR_CANCELED) {
965         return true;
966     }
967     return false;
968 }
969 
IsValidDefaultPrinterType(uint32_t type)970 bool NapiInnerPrint::IsValidDefaultPrinterType(uint32_t type)
971 {
972     if (type >= DEFAULT_PRINTER_TYPE_SETTED_BY_USER &&
973         type <= DEFAULT_PRINTER_TYPE_LAST_USED_PRINTER) {
974         return true;
975     }
976     return false;
977 }
978 
NapiThrowError(napi_env env,const int32_t errCode)979 void NapiInnerPrint::NapiThrowError(napi_env env, const int32_t errCode)
980 {
981     napi_value result = nullptr;
982     napi_create_error(env, NapiPrintUtils::CreateInt32(env, errCode),
983         NapiPrintUtils::CreateStringUtf8(env, NapiPrintUtils::GetPrintErrorMsg(errCode)), &result);
984     napi_throw(env, result);
985 }
986 } // namespace OHOS::Print
987