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