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