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_print_ext.h"
17
18 #include "napi_print_utils.h"
19 #include "print_log.h"
20 #include "print_manager_client.h"
21 #include "printer_info_helper.h"
22 #include "printer_capability_helper.h"
23
24 namespace OHOS::Print {
AddPrinters(napi_env env,napi_callback_info info)25 napi_value NapiPrintExt::AddPrinters(napi_env env, napi_callback_info info)
26 {
27 PRINT_HILOGD("Enter ---->");
28 auto context = std::make_shared<NapiPrintExtContext>();
29 auto input =
30 [context](
31 napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
32 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
33 bool isArray = false;
34 napi_is_array(env, argv[NapiPrintUtils::INDEX_ZERO], &isArray);
35 PRINT_ASSERT_BASE(env, isArray, " is not array!", napi_array_expected);
36
37 uint32_t len = 0;
38 napi_get_array_length(env, argv[NapiPrintUtils::INDEX_ZERO], &len);
39
40 for (uint32_t index = 0; index < len; index++) {
41 napi_value value;
42 napi_get_element(env, argv[NapiPrintUtils::INDEX_ZERO], index, &value);
43 auto printerInfoPtr = PrinterInfoHelper::BuildFromJs(env, value);
44 if (printerInfoPtr == nullptr) {
45 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
46 PRINT_HILOGE("PrinterInfo format error!");
47 return napi_invalid_arg;
48 }
49 printerInfoPtr->SetPrinterId(printerInfoPtr->GetPrinterId());
50 PRINT_HILOGD("printerInfoPtr->GetPrinterId() = %{public}s", printerInfoPtr->GetPrinterId().c_str());
51 context->printerInfos.emplace_back(*printerInfoPtr);
52 }
53 if (context->printerInfos.empty()) {
54 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
55 PRINT_HILOGE("no valid printer info exists!");
56 return napi_invalid_arg;
57 }
58 return napi_ok;
59 };
60 auto output = [context](napi_env env, napi_value *result) -> napi_status {
61 napi_status status = napi_get_boolean(env, context->result, result);
62 PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
63 return status;
64 };
65 auto exec = [context](PrintAsyncCall::Context *ctx) {
66 int32_t ret = PrintManagerClient::GetInstance()->AddPrinters(context->printerInfos);
67 context->result = ret == E_PRINT_NONE;
68 if (ret != E_PRINT_NONE) {
69 PRINT_HILOGE("Failed to add printers");
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
RemovePrinters(napi_env env,napi_callback_info info)78 napi_value NapiPrintExt::RemovePrinters(napi_env env, napi_callback_info info)
79 {
80 PRINT_HILOGD("Enter ---->");
81 auto context = std::make_shared<NapiPrintExtContext>();
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
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[NapiPrintUtils::INDEX_ZERO], &len);
93
94 for (uint32_t index = 0; index < len; index++) {
95 napi_value value;
96 napi_get_element(env, argv[NapiPrintUtils::INDEX_ZERO], index, &value);
97 std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, value);
98 if (printerId != "") {
99 context->printerIds.emplace_back(printerId);
100 }
101 }
102 if (context->printerIds.empty()) {
103 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
104 PRINT_HILOGE("no valid printer info exists!");
105 return napi_invalid_arg;
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()->RemovePrinters(context->printerIds);
116 context->result = ret == E_PRINT_NONE;
117 if (ret != E_PRINT_NONE) {
118 PRINT_HILOGE("Failed to remove printers");
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
UpdatePrinters(napi_env env,napi_callback_info info)127 napi_value NapiPrintExt::UpdatePrinters(napi_env env, napi_callback_info info)
128 {
129 PRINT_HILOGD("Enter ---->");
130 auto context = std::make_shared<NapiPrintExtContext>();
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_ONE, " should 1 parameter!", napi_invalid_arg);
135 bool isArray = false;
136 napi_is_array(env, argv[NapiPrintUtils::INDEX_ZERO], &isArray);
137 PRINT_ASSERT_BASE(env, isArray, " is not array!", napi_array_expected);
138
139 uint32_t len = 0;
140 napi_get_array_length(env, argv[NapiPrintUtils::INDEX_ZERO], &len);
141
142 for (uint32_t index = 0; index < len; index++) {
143 napi_value value;
144 napi_get_element(env, argv[NapiPrintUtils::INDEX_ZERO], index, &value);
145 auto printerInfoPtr = PrinterInfoHelper::BuildFromJs(env, value);
146 if (printerInfoPtr == nullptr) {
147 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
148 PRINT_HILOGE("PrinterInfo format error!");
149 return napi_invalid_arg;
150 }
151 context->printerInfos.emplace_back(*printerInfoPtr);
152 }
153 if (context->printerInfos.empty()) {
154 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
155 PRINT_HILOGE("no valid printer info exists!");
156 return napi_invalid_arg;
157 }
158 return napi_ok;
159 };
160 auto output = [context](napi_env env, napi_value *result) -> napi_status {
161 napi_status status = napi_get_boolean(env, context->result, result);
162 PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
163 return status;
164 };
165 auto exec = [context](PrintAsyncCall::Context *ctx) {
166 int32_t ret = PrintManagerClient::GetInstance()->UpdatePrinters(context->printerInfos);
167 context->result = ret == E_PRINT_NONE;
168 if (ret != E_PRINT_NONE) {
169 PRINT_HILOGE("Failed to update printers");
170 context->SetErrorIndex(ret);
171 }
172 };
173 context->SetAction(std::move(input), std::move(output));
174 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
175 return asyncCall.Call(env, exec);
176 }
177
UpdatePrinterState(napi_env env,napi_callback_info info)178 napi_value NapiPrintExt::UpdatePrinterState(napi_env env, napi_callback_info info)
179 {
180 PRINT_HILOGD("Enter ---->");
181 auto context = std::make_shared<NapiPrintExtContext>();
182 auto input =
183 [context](
184 napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
185 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_TWO, " should 2 parameter!", napi_invalid_arg);
186 napi_valuetype valuetype;
187 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
188 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
189
190 PRINT_CALL_BASE(env, napi_typeof(env, argv[1], &valuetype), napi_invalid_arg);
191 PRINT_ASSERT_BASE(env, valuetype == napi_number, "printerStateis not a number", napi_number_expected);
192
193 std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
194 PRINT_HILOGD("printerId : %{public}s", printerId.c_str());
195
196 uint32_t printerState = NapiPrintUtils::GetUint32FromValue(env, argv[1]);
197 PRINT_HILOGD("printerState : %{public}d", printerState);
198
199 if (printerId == "" || !IsValidPrinterState(printerState)) {
200 PRINT_HILOGE("invalid printer id or printer state");
201 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
202 return napi_invalid_arg;
203 }
204
205 context->printerId = printerId;
206 PRINT_HILOGD("context->printerId : %{private}s", context->printerId.c_str());
207 context->printerState = printerState;
208 return napi_ok;
209 };
210 auto output = [context](napi_env env, napi_value *result) -> napi_status {
211 napi_status status = napi_get_boolean(env, context->result, result);
212 PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
213 return status;
214 };
215 auto exec = [context](PrintAsyncCall::Context *ctx) {
216 int32_t ret = PrintManagerClient::GetInstance()->UpdatePrinterState(context->printerId, context->printerState);
217 context->result = ret == E_PRINT_NONE;
218 if (ret != E_PRINT_NONE) {
219 PRINT_HILOGE("Failed to update state of printer");
220 context->SetErrorIndex(ret);
221 }
222 };
223 context->SetAction(std::move(input), std::move(output));
224 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
225 return asyncCall.Call(env, exec);
226 }
227
UpdatePrintJobStateOnlyForSystemApp(napi_env env,napi_callback_info info)228 napi_value NapiPrintExt::UpdatePrintJobStateOnlyForSystemApp(napi_env env, napi_callback_info info)
229 {
230 PRINT_HILOGD("Enter ---->");
231 auto context = std::make_shared<NapiPrintExtContext>();
232 auto input =
233 [context](
234 napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
235 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_THREE, " should 3 parameter!", napi_invalid_arg);
236 napi_valuetype valuetype;
237 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
238 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printJobId is not a string", napi_string_expected);
239
240 PRINT_CALL_BASE(env, napi_typeof(env, argv[1], &valuetype), napi_invalid_arg);
241 PRINT_ASSERT_BASE(env, valuetype == napi_number, "printJobState is not a number", napi_number_expected);
242
243 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_TWO], &valuetype), napi_invalid_arg);
244 PRINT_ASSERT_BASE(env, valuetype == napi_number, "reason is not a number", napi_number_expected);
245
246 std::string printJobId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
247 PRINT_HILOGD("printJobId : %{public}s", printJobId.c_str());
248
249 uint32_t printJobState = NapiPrintUtils::GetUint32FromValue(env, argv[1]);
250 PRINT_HILOGD("printerJobState : %{public}d", printJobState);
251
252 uint32_t jobSubState = NapiPrintUtils::GetUint32FromValue(env, argv[NapiPrintUtils::INDEX_TWO]);
253 PRINT_HILOGD("jobSubState : %{public}d", jobSubState);
254
255 if (printJobId == "" || !IsValidPrintJobState(printJobState) || !IsValidPrintJobSubState(jobSubState)) {
256 PRINT_HILOGE("invalid job id, job state or job substate");
257 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
258 return napi_invalid_arg;
259 }
260
261 context->printJobId = printJobId;
262 context->printJobState = printJobState;
263 context->jobSubState = jobSubState;
264 return napi_ok;
265 };
266 auto output = [context](napi_env env, napi_value *result) -> napi_status {
267 napi_status status = napi_get_boolean(env, context->result, result);
268 PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
269 return status;
270 };
271 auto exec = [context](PrintAsyncCall::Context *ctx) {
272 int32_t ret = PrintManagerClient::GetInstance()->UpdatePrintJobStateOnlyForSystemApp(context->printJobId,
273 context->printJobState, context->jobSubState);
274 context->result = ret == E_PRINT_NONE;
275 if (ret != E_PRINT_NONE) {
276 PRINT_HILOGE("Failed to update state of print job");
277 context->SetErrorIndex(ret);
278 }
279 };
280 context->SetAction(std::move(input), std::move(output));
281 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
282 return asyncCall.Call(env, exec);
283 }
284
UpdateExtensionInfo(napi_env env,napi_callback_info info)285 napi_value NapiPrintExt::UpdateExtensionInfo(napi_env env, napi_callback_info info)
286 {
287 PRINT_HILOGD("Enter ---->");
288 auto context = std::make_shared<NapiPrintExtContext>();
289 auto input =
290 [context](
291 napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
292 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg);
293 napi_valuetype valuetype;
294 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
295 PRINT_ASSERT_BASE(env, valuetype == napi_string, "extInfo is not a string", napi_string_expected);
296
297 std::string extInfo = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
298 PRINT_HILOGD("extInfo : %{public}s", extInfo.c_str());
299
300 if (extInfo == "") {
301 PRINT_HILOGE("invalid extension id or extension information");
302 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
303 return napi_invalid_arg;
304 }
305 context->extInfo = extInfo;
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 int32_t ret = PrintManagerClient::GetInstance()->UpdateExtensionInfo(context->extInfo);
315 context->result = ret == E_PRINT_NONE;
316 if (ret != E_PRINT_NONE) {
317 PRINT_HILOGE("Failed to update extension information");
318 context->SetErrorIndex(ret);
319 }
320 };
321 context->SetAction(std::move(input), std::move(output));
322 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
323 return asyncCall.Call(env, exec);
324 }
325
AddPrinterToCups(napi_env env,napi_callback_info info)326 napi_value NapiPrintExt::AddPrinterToCups(napi_env env, napi_callback_info info)
327 {
328 PRINT_HILOGD("Enter AddPrinterToCups---->");
329 auto context = std::make_shared<NapiPrintExtContext>();
330 auto input =
331 [context](
332 napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
333 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_THREE, " should 3 parameter!", napi_invalid_arg);
334 napi_valuetype valuetype;
335 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
336 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerUri is not a string", napi_string_expected);
337
338 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ONE], &valuetype), napi_invalid_arg);
339 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerName is not a string", napi_string_expected);
340
341 std::string printerUri = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
342 PRINT_HILOGD("printerUri : %{private}s", printerUri.c_str());
343 context->printerUri = printerUri;
344
345 std::string printerName = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ONE]);
346 PRINT_HILOGD("printerName : %{private}s", printerName.c_str());
347 context->printerName = printerName;
348
349 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_TWO], &valuetype), napi_invalid_arg);
350 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerMake is not a string", napi_string_expected);
351 std::string printerMake = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_TWO]);
352 PRINT_HILOGD("printerMake : %{private}s", printerMake.c_str());
353 context->printerMake = printerMake;
354 return napi_ok;
355 };
356 auto output = [context](napi_env env, napi_value *result) -> napi_status {
357 napi_status status = napi_get_boolean(env, context->result, result);
358 PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
359 return status;
360 };
361 auto exec = [context](PrintAsyncCall::Context *ctx) {
362 int32_t ret = PrintManagerClient::GetInstance()->AddPrinterToCups(context->printerUri, context->printerName,
363 context->printerMake);
364 PRINT_HILOGD("ret: %d", ret);
365 context->result = (ret == E_PRINT_NONE);
366 if (ret != E_PRINT_NONE) {
367 PRINT_HILOGE("Failed to set cups printer");
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
QueryPrinterCapabilityByUri(napi_env env,napi_callback_info info)376 napi_value NapiPrintExt::QueryPrinterCapabilityByUri(napi_env env, napi_callback_info info)
377 {
378 PRINT_HILOGD("Enter QueryPrinterCapabilityByUri---->");
379 auto context = std::make_shared<NapiPrintExtContext>();
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,
384 argc == NapiPrintUtils::ARGC_ONE || argc == NapiPrintUtils::ARGC_TWO,
385 " should 1 or 2 parameter!",
386 napi_invalid_arg);
387
388 napi_valuetype valuetype;
389 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
390 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerUri is not a string", napi_string_expected);
391
392 std::string printerUri = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
393 PRINT_HILOGD("printerUri : %{private}s", printerUri.c_str());
394 context->printerUri = printerUri;
395
396 if (argc == NapiPrintUtils::ARGC_TWO) {
397 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ONE], &valuetype), napi_invalid_arg);
398 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerId is not a string", napi_string_expected);
399
400 std::string printerId = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ONE]);
401 PRINT_HILOGD("printerId : %{private}s", printerUri.c_str());
402 context->printerId = printerId;
403 }
404 return napi_ok;
405 };
406 // promise return
407 auto output = [context](napi_env env, napi_value *result) -> napi_status {
408 PRINT_HILOGD("output enter ---->");
409 *result = PrinterCapabilityHelper::MakeJsObject(env, context->printerCaps);
410 return napi_ok;
411 };
412
413 auto exec = [context](PrintAsyncCall::Context *ctx) {
414 int32_t ret = PrintManagerClient::GetInstance()->QueryPrinterCapabilityByUri(context->printerUri,
415 context->printerId, context->printerCaps);
416 context->result = (ret == E_PRINT_NONE);
417 PRINT_HILOGD("ret: %d", ret);
418 if (ret != E_PRINT_NONE) {
419 PRINT_HILOGE("Failed to get printers caps");
420 context->SetErrorIndex(ret);
421 }
422 };
423 context->SetAction(std::move(input), std::move(output));
424 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
425 return asyncCall.Call(env, exec);
426 }
427
DeletePrinterFromCups(napi_env env,napi_callback_info info)428 napi_value NapiPrintExt::DeletePrinterFromCups(napi_env env, napi_callback_info info)
429 {
430 PRINT_HILOGD("Enter DeletePrinterFromCups---->");
431 auto context = std::make_shared<NapiPrintExtContext>();
432 auto input =
433 [context](
434 napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {
435 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_THREE, " should 3 parameter!", napi_invalid_arg);
436 napi_valuetype valuetype;
437 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
438 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerUri is not a string", napi_string_expected);
439
440 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ONE], &valuetype), napi_invalid_arg);
441 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerName is not a string", napi_string_expected);
442
443 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_TWO], &valuetype), napi_invalid_arg);
444 PRINT_ASSERT_BASE(env, valuetype == napi_string, "printerMake is not a string", napi_string_expected);
445
446 std::string printerUri = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
447 PRINT_HILOGD("printerUri : %{private}s", printerUri.c_str());
448 context->printerUri = printerUri;
449
450 std::string printerName = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ONE]);
451 PRINT_HILOGD("printerName : %{private}s", printerName.c_str());
452 context->printerName = printerName;
453
454 std::string printerMake = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_TWO]);
455 PRINT_HILOGD("printerMake : %{private}s", printerMake.c_str());
456 context->printerMake = printerMake;
457 return napi_ok;
458 };
459 auto output = [context](napi_env env, napi_value *result) -> napi_status {
460 napi_status status = napi_get_boolean(env, context->result, result);
461 PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
462 return status;
463 };
464 auto exec = [context](PrintAsyncCall::Context *ctx) {
465 int32_t ret =
466 PrintManagerClient::GetInstance()->DeletePrinterFromCups(
467 context->printerUri, context->printerName, context->printerMake);
468 PRINT_HILOGD("ret: %d", ret);
469 context->result = (ret == E_PRINT_NONE);
470 if (ret != E_PRINT_NONE) {
471 PRINT_HILOGE("Failed to delete cups printer");
472 context->SetErrorIndex(ret);
473 }
474 };
475 context->SetAction(std::move(input), std::move(output));
476 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
477 return asyncCall.Call(env, exec);
478 }
479
IsValidPrinterState(uint32_t state)480 bool NapiPrintExt::IsValidPrinterState(uint32_t state)
481 {
482 if (state >= PRINTER_ADDED && state < PRINTER_UNKNOWN) {
483 return true;
484 }
485 return false;
486 }
487
IsValidPrintJobState(uint32_t state)488 bool NapiPrintExt::IsValidPrintJobState(uint32_t state)
489 {
490 if (state >= PRINT_JOB_PREPARED && state <= PRINT_JOB_UNKNOWN) {
491 return true;
492 }
493 return false;
494 }
495
IsValidPrintJobSubState(uint32_t subState)496 bool NapiPrintExt::IsValidPrintJobSubState(uint32_t subState)
497 {
498 if (subState >= PRINT_JOB_COMPLETED_SUCCESS && subState <= PRINT_JOB_BLOCKED_UNKNOWN) {
499 return true;
500 }
501 return false;
502 }
503 } // namespace OHOS::Print
504