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 #include "print_service_proxy.h"
16 #include "iremote_broker.h"
17 #include "print_constant.h"
18 #include "print_job.h"
19 #include "print_log.h"
20 #include "printer_info.h"
21
22 namespace OHOS::Print {
23 using namespace OHOS::HiviewDFX;
24
PrintServiceProxy(const sptr<IRemoteObject> & object)25 PrintServiceProxy::PrintServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IPrintService>(object) {}
26
GetResult(int retCode,MessageParcel & reply)27 int32_t PrintServiceProxy::GetResult(int retCode, MessageParcel &reply)
28 {
29 if (retCode != ERR_NONE) {
30 PRINT_HILOGE("rpc error code = %{public}d", retCode);
31 return E_PRINT_RPC_FAILURE;
32 }
33
34 retCode = reply.ReadInt32();
35 PRINT_HILOGD("PrintServiceProxy out. ret = [%{public}d]", retCode);
36 return retCode;
37 }
38
StartService()39 int32_t PrintServiceProxy::StartService()
40 {
41 MessageParcel data, reply;
42 MessageOption option;
43 data.WriteInterfaceToken(GetDescriptor());
44 const std::string ndkInfo = "nativePrint";
45 data.WriteString(ndkInfo);
46 PRINT_HILOGI("nativePrint PrintServiceProxy StartService started.");
47 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_START_SERVICE, data, reply, option);
48 ret = GetResult(ret, reply);
49 PRINT_HILOGD("PrintServiceProxy CMD_START_SERVICE ret = [%{public}d]", ret);
50 return ret;
51 }
52
StartPrint(const std::vector<std::string> & fileList,const std::vector<uint32_t> & fdList,std::string & taskId)53 int32_t PrintServiceProxy::StartPrint(const std::vector<std::string> &fileList,
54 const std::vector<uint32_t> &fdList, std::string &taskId)
55 {
56 MessageParcel data, reply;
57 MessageOption option;
58 data.WriteInterfaceToken(GetDescriptor());
59 PRINT_HILOGD("Current file is %{public}zd", fileList.size());
60 for (auto file : fileList) {
61 PRINT_HILOGD("file is %{private}s", file.c_str());
62 }
63
64 data.WriteBool(fileList.size() > 0);
65 if (!fileList.empty()) {
66 data.WriteStringVector(fileList);
67 }
68
69 data.WriteBool(fdList.size() > 0);
70 if (!fdList.empty()) {
71 data.WriteInt32(fdList.size());
72 for (auto fd : fdList) {
73 data.WriteFileDescriptor(fd);
74 }
75 }
76
77 data.WriteString(taskId);
78
79 PRINT_HILOGD("PrintServiceProxy StartPrint started.");
80 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_START_PRINT, data, reply, option);
81 ret = GetResult(ret, reply);
82 taskId = reply.ReadString();
83 PRINT_HILOGD("PrintServiceProxy StartPrint ret = [%{public}d] TaskId = %{public}s", ret, taskId.c_str());
84 return ret;
85 }
86
StopPrint(const std::string & taskId)87 int32_t PrintServiceProxy::StopPrint(const std::string &taskId)
88 {
89 MessageParcel data, reply;
90 MessageOption option;
91 data.WriteInterfaceToken(GetDescriptor());
92 data.WriteString(taskId);
93 PRINT_HILOGD("PrintServiceProxy StopPrint started.");
94 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_STOP_PRINT, data, reply, option);
95 ret = GetResult(ret, reply);
96 PRINT_HILOGD("PrintServiceProxy StopPrint out. ret = [%{public}d]", ret);
97 return ret;
98 }
99
ConnectPrinter(const std::string & printerId)100 int32_t PrintServiceProxy::ConnectPrinter(const std::string &printerId)
101 {
102 MessageParcel data, reply;
103 MessageOption option;
104 data.WriteInterfaceToken(GetDescriptor());
105 data.WriteString(printerId);
106 PRINT_HILOGD("PrintServiceProxy ConnectPrinter started.");
107 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_CONNECTPRINTER, data, reply, option);
108 ret = GetResult(ret, reply);
109 PRINT_HILOGD("PrintServiceProxy ConnectPrinter out. ret = [%{public}d]", ret);
110 return ret;
111 }
112
DisconnectPrinter(const std::string & printerId)113 int32_t PrintServiceProxy::DisconnectPrinter(const std::string &printerId)
114 {
115 MessageParcel data, reply;
116 MessageOption option;
117 data.WriteInterfaceToken(GetDescriptor());
118 data.WriteString(printerId);
119 PRINT_HILOGD("PrintServiceProxy DisconnectPrinter started.");
120 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_DISCONNECTPRINTER, data, reply, option);
121 ret = GetResult(ret, reply);
122 PRINT_HILOGD("PrintServiceProxy DisconnectPrinter out. ret = [%{public}d]", ret);
123 return ret;
124 }
125
QueryAllExtension(std::vector<PrintExtensionInfo> & extensionInfos)126 int32_t PrintServiceProxy::QueryAllExtension(std::vector<PrintExtensionInfo> &extensionInfos)
127 {
128 MessageParcel data, reply;
129 MessageOption option;
130 data.WriteInterfaceToken(GetDescriptor());
131 PRINT_HILOGD("PrintServiceProxy QueryAllExtension started.");
132 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_QUERYALLEXTENSION, data, reply, option);
133 ret = GetResult(ret, reply);
134 if (ret != E_PRINT_NONE) {
135 PRINT_HILOGD("PrintServiceProxy QueryAllExtension Failed.");
136 return ret;
137 }
138
139 uint32_t len = reply.ReadUint32();
140 for (uint32_t i = 0; i < len; i++) {
141 auto infoPtr = PrintExtensionInfo::Unmarshalling(reply);
142 if (infoPtr == nullptr) {
143 PRINT_HILOGE("wrong information from data");
144 return E_PRINT_GENERIC_FAILURE;
145 }
146 extensionInfos.emplace_back(*infoPtr);
147 }
148 PRINT_HILOGD("PrintServiceProxy QueryAllExtension succeeded.");
149 return E_PRINT_NONE;
150 }
151
StartDiscoverPrinter(const std::vector<std::string> & extensionList)152 int32_t PrintServiceProxy::StartDiscoverPrinter(const std::vector<std::string> &extensionList)
153 {
154 MessageParcel data, reply;
155 MessageOption option;
156 data.WriteInterfaceToken(GetDescriptor());
157 data.WriteStringVector(extensionList);
158 PRINT_HILOGD("PrintServiceProxy StartDiscoverPrinter started.");
159 int32_t ret = Remote()->SendRequest(
160 OHOS::Print::IPrintInterfaceCode::CMD_STARTDISCOVERPRINTER, data, reply, option);
161 ret = GetResult(ret, reply);
162 PRINT_HILOGD("PrintServiceProxy StartDiscoverPrinter out. ret = [%{public}d]", ret);
163 return ret;
164 }
165
StopDiscoverPrinter()166 int32_t PrintServiceProxy::StopDiscoverPrinter()
167 {
168 MessageParcel data, reply;
169 MessageOption option;
170 data.WriteInterfaceToken(GetDescriptor());
171 PRINT_HILOGD("PrintServiceProxy StopDiscoverPrinter started.");
172 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_STOPDISCOVERPRINTER, data, reply, option);
173 ret = GetResult(ret, reply);
174 PRINT_HILOGD("PrintServiceProxy StopDiscoverPrinter out. ret = [%{public}d]", ret);
175 return ret;
176 }
177
StartPrintJob(PrintJob & jobinfo)178 int32_t PrintServiceProxy::StartPrintJob(PrintJob &jobinfo)
179 {
180 MessageParcel data, reply;
181 MessageOption option;
182
183 data.WriteInterfaceToken(GetDescriptor());
184 jobinfo.Marshalling(data);
185 PRINT_HILOGD("PrintServiceProxy StartPrintJob started.");
186 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_STARTPRINTJOB, data, reply, option);
187 ret = GetResult(ret, reply);
188 PRINT_HILOGD("PrintServiceProxy StartPrintJob out. ret = [%{public}d]", ret);
189 return ret;
190 }
191
CancelPrintJob(const std::string & jobId)192 int32_t PrintServiceProxy::CancelPrintJob(const std::string &jobId)
193 {
194 MessageParcel data, reply;
195 MessageOption option;
196
197 data.WriteInterfaceToken(GetDescriptor());
198 data.WriteString(jobId);
199 PRINT_HILOGD("PrintServiceProxy CancelPrintJob started.");
200 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_CANCELPRINTJOB, data, reply, option);
201 ret = GetResult(ret, reply);
202 PRINT_HILOGD("PrintServiceProxy CancelPrintJob out. ret = [%{public}d]", ret);
203 return ret;
204 }
205
AddPrinters(const std::vector<PrinterInfo> & printerInfos)206 int32_t PrintServiceProxy::AddPrinters(const std::vector<PrinterInfo> &printerInfos)
207 {
208 MessageParcel data, reply;
209 MessageOption option;
210 data.WriteInterfaceToken(GetDescriptor());
211 data.WriteUint32(printerInfos.size());
212 PRINT_HILOGD("AddPrinters printerInfos.size() = %{public}zu", printerInfos.size());
213 for (uint32_t i = 0; i < printerInfos.size(); i++) {
214 printerInfos[i].Marshalling(data);
215 }
216 PRINT_HILOGD("PrintServiceProxy AddPrinters started.");
217 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_ADDPRINTERS, data, reply, option);
218 ret = GetResult(ret, reply);
219 PRINT_HILOGD("PrintServiceProxy AddPrinters out. ret = [%{public}d]", ret);
220 return ret;
221 }
222
RemovePrinters(const std::vector<std::string> & printerIds)223 int32_t PrintServiceProxy::RemovePrinters(const std::vector<std::string> &printerIds)
224 {
225 MessageParcel data, reply;
226 MessageOption option;
227 data.WriteInterfaceToken(GetDescriptor());
228 data.WriteStringVector(printerIds);
229
230 PRINT_HILOGD("PrintServiceProxy RemovePrinters started.");
231 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_REMOVEPRINTERS, data, reply, option);
232 ret = GetResult(ret, reply);
233 PRINT_HILOGD("PrintServiceProxy RemovePrinters out. ret = [%{public}d]", ret);
234 return ret;
235 }
236
UpdatePrinters(const std::vector<PrinterInfo> & printerInfos)237 int32_t PrintServiceProxy::UpdatePrinters(const std::vector<PrinterInfo> &printerInfos)
238 {
239 MessageParcel data, reply;
240 MessageOption option;
241 data.WriteInterfaceToken(GetDescriptor());
242 data.WriteUint32(printerInfos.size());
243 PRINT_HILOGD("UpdatePrinters printerInfos.size() = %{public}zu", printerInfos.size());
244 for (uint32_t i = 0; i < printerInfos.size(); i++) {
245 printerInfos[i].Marshalling(data);
246 }
247 PRINT_HILOGD("PrintServiceProxy UpdatePrinters started.");
248 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_UPDATEPRINTERS, data, reply, option);
249 ret = GetResult(ret, reply);
250 PRINT_HILOGD("PrintServiceProxy UpdatePrinters out. ret = [%{public}d]", ret);
251 return ret;
252 }
253
UpdatePrinterState(const std::string & printerId,uint32_t state)254 int32_t PrintServiceProxy::UpdatePrinterState(const std::string &printerId, uint32_t state)
255 {
256 MessageParcel data, reply;
257 MessageOption option;
258 data.WriteInterfaceToken(GetDescriptor());
259 data.WriteString(printerId);
260 data.WriteUint32(state);
261 PRINT_HILOGD("PrintServiceProxy UpdatePrinterState started.");
262 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_UPDATEPRINTERSTATE, data, reply, option);
263 ret = GetResult(ret, reply);
264 PRINT_HILOGD("PrintServiceProxy UpdatePrinterState out. ret = [%{public}d]", ret);
265 return ret;
266 }
267
UpdatePrintJobStateOnlyForSystemApp(const std::string & jobId,uint32_t state,uint32_t subState)268 int32_t PrintServiceProxy::UpdatePrintJobStateOnlyForSystemApp(
269 const std::string &jobId, uint32_t state, uint32_t subState)
270 {
271 MessageParcel data, reply;
272 MessageOption option;
273 data.WriteInterfaceToken(GetDescriptor());
274 data.WriteString(jobId);
275 data.WriteUint32(state);
276 data.WriteUint32(subState);
277 PRINT_HILOGD("PrintServiceProxy UpdatePrintJobStateOnlyForSystemApp started.");
278 int32_t ret = Remote()->SendRequest(
279 OHOS::Print::IPrintInterfaceCode::CMD_UPDATEPRINTJOBSTATE, data, reply, option);
280 ret = GetResult(ret, reply);
281 PRINT_HILOGD("PrintServiceProxy UpdatePrintJobStateOnlyForSystemApp out. ret = [%{public}d]", ret);
282 return ret;
283 }
284
UpdateExtensionInfo(const std::string & extInfo)285 int32_t PrintServiceProxy::UpdateExtensionInfo(const std::string &extInfo)
286 {
287 MessageParcel data, reply;
288 MessageOption option;
289 data.WriteInterfaceToken(GetDescriptor());
290 data.WriteString(extInfo);
291 PRINT_HILOGD("PrintServiceProxy UpdateExtensionInfo started.");
292 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_UPDATEEXTENSIONINFO, data, reply, option);
293 ret = GetResult(ret, reply);
294 PRINT_HILOGD("PrintServiceProxy UpdateExtensionInfo out. ret = [%{public}d]", ret);
295 return ret;
296 }
297
RequestPreview(const PrintJob & jobinfo,std::string & previewResult)298 int32_t PrintServiceProxy::RequestPreview(const PrintJob &jobinfo, std::string &previewResult)
299 {
300 MessageParcel data, reply;
301 MessageOption option;
302 data.WriteInterfaceToken(GetDescriptor());
303 jobinfo.Marshalling(data);
304 PRINT_HILOGD("PrintServiceProxy RequestPreview started.");
305 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_REQUESTPREVIEW, data, reply, option);
306 ret = GetResult(ret, reply);
307 previewResult = reply.ReadString();
308 PRINT_HILOGD("PrintServiceProxy RequestPreview ret = [%{public}d] previewResult = %{public}s",
309 ret, previewResult.c_str());
310 return ret;
311 }
312
QueryPrinterCapability(const std::string & printerId)313 int32_t PrintServiceProxy::QueryPrinterCapability(const std::string &printerId)
314 {
315 MessageParcel data, reply;
316 MessageOption option;
317 data.WriteInterfaceToken(GetDescriptor());
318 data.WriteString(printerId);
319 PRINT_HILOGD("PrintServiceProxy QueryPrinterCapability started.");
320 int32_t ret = Remote()->SendRequest(
321 OHOS::Print::IPrintInterfaceCode::CMD_QUERYPRINTERCAPABILITY, data, reply, option);
322 ret = GetResult(ret, reply);
323 PRINT_HILOGD("PrintServiceProxy QueryPrinterCapability out. ret = [%{public}d]", ret);
324 return ret;
325 }
326
QueryPrinterInfoByPrinterId(const std::string & printerId,PrinterInfo & info)327 int32_t PrintServiceProxy::QueryPrinterInfoByPrinterId(const std::string &printerId, PrinterInfo &info)
328 {
329 MessageParcel data, reply;
330 MessageOption option;
331 data.WriteInterfaceToken(GetDescriptor());
332 data.WriteString(printerId);
333 info.Marshalling(data);
334 PRINT_HILOGD("PrintServiceProxy QueryPrinterInfoByPrinterId started.");
335 int32_t ret = Remote()->SendRequest(
336 OHOS::Print::IPrintInterfaceCode::CMD_QUERYPRINTERINFOBYPRINTERID, data, reply, option);
337 ret = GetResult(ret, reply);
338 auto printerInfoPtr = PrinterInfo::Unmarshalling(reply);
339 if (printerInfoPtr == nullptr) {
340 PRINT_HILOGE("wrong printJob from data");
341 return E_PRINT_GENERIC_FAILURE;
342 }
343 info = *printerInfoPtr;
344 PRINT_HILOGD("PrintServiceProxy QueryPrinterInfoByPrinterId out. ret = [%{public}d]", ret);
345 return ret;
346 }
347
QueryAddedPrinter(std::vector<std::string> & printerNameList)348 int32_t PrintServiceProxy::QueryAddedPrinter(std::vector<std::string> &printerNameList)
349 {
350 MessageParcel data, reply;
351 MessageOption option;
352 data.WriteInterfaceToken(GetDescriptor());
353 PRINT_HILOGD("PrintServiceProxy QueryAddedPrinter started.");
354 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_QUERYADDEDPRINTER, data, reply, option);
355 ret = GetResult(ret, reply);
356 PRINT_HILOGD("PrintServiceProxy QueryAddedPrinter out. ret = [%{public}d]", ret);
357 reply.ReadStringVector(&printerNameList);
358 PRINT_HILOGD("PrintServiceProxy QueryAddedPrinter printerNameList size %{public}zu.", printerNameList.size());
359 return ret;
360 }
361
QueryPrinterProperties(const std::string & printerId,const std::vector<std::string> & keyList,std::vector<std::string> & valueList)362 int32_t PrintServiceProxy::QueryPrinterProperties(const std::string &printerId,
363 const std::vector<std::string> &keyList, std::vector<std::string> &valueList)
364 {
365 MessageParcel data, reply;
366 MessageOption option;
367 data.WriteInterfaceToken(GetDescriptor());
368 data.WriteString(printerId);
369 data.WriteStringVector(keyList);
370 PRINT_HILOGD("PrintServiceProxy QueryPrinterProperties started.");
371 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_QUERYPRINTERPROPERTIES, data, reply,
372 option);
373 ret = GetResult(ret, reply);
374 reply.ReadStringVector(&valueList);
375 PRINT_HILOGD("PrintServiceProxy RemovePrinters out. ret = [%{public}d]", ret);
376 return ret;
377 }
378
StartNativePrintJob(PrintJob & printJob)379 int32_t PrintServiceProxy::StartNativePrintJob(PrintJob &printJob)
380 {
381 MessageParcel data, reply;
382 MessageOption option;
383 data.WriteInterfaceToken(GetDescriptor());
384 printJob.Marshalling(data);
385 PRINT_HILOGD("PrintServiceProxy RequestPreview started.");
386 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_STARTNATIVEPRINTJOB, data, reply,
387 option);
388 ret = GetResult(ret, reply);
389 PRINT_HILOGD("PrintServiceProxy RemovePrinters out. ret = [%{public}d]", ret);
390 return ret;
391 }
392
GetPrinterPreference(const std::string & printerId,std::string & printerPreference)393 int32_t PrintServiceProxy::GetPrinterPreference(const std::string &printerId, std::string &printerPreference)
394 {
395 MessageParcel data, reply;
396 MessageOption option;
397 data.WriteInterfaceToken(GetDescriptor());
398 data.WriteString(printerId);
399 PRINT_HILOGD("PrintServiceProxy GetPrinterPreference started.");
400 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_GET_PRINTER_PREFERENCE,
401 data, reply, option);
402 ret = GetResult(ret, reply);
403 printerPreference = reply.ReadString();
404 PRINT_HILOGI("PrintServiceProxy GetPrinterPreference ret = [%{public}d] GetPrinterPreference = %{public}s",
405 ret, printerPreference.c_str());
406 return ret;
407 }
408
SetPrinterPreference(const std::string & printerId,const std::string & printerPreference)409 int32_t PrintServiceProxy::SetPrinterPreference(const std::string &printerId, const std::string &printerPreference)
410 {
411 MessageParcel data, reply;
412 MessageOption option;
413 data.WriteInterfaceToken(GetDescriptor());
414 data.WriteString(printerId);
415 data.WriteString(printerPreference);
416 PRINT_HILOGI("PrintServiceProxy SetPrinterPreference started.");
417 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_SET_PRINTER_PREFERENCE,
418 data, reply, option);
419 ret = GetResult(ret, reply);
420 PRINT_HILOGI("PrintServiceProxy SetPrinterPreference ret = [%{public}d]", ret);
421 return ret;
422 }
423
QueryAllPrintJob(std::vector<PrintJob> & printJobs)424 int32_t PrintServiceProxy::QueryAllPrintJob(std::vector<PrintJob> &printJobs)
425 {
426 MessageParcel data, reply;
427 MessageOption option;
428 data.WriteInterfaceToken(GetDescriptor());
429 PRINT_HILOGD("PrintServiceProxy QueryAllPrintJob started.");
430 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_QUERYALLPRINTJOB, data, reply, option);
431 ret = GetResult(ret, reply);
432 if (ret != E_PRINT_NONE) {
433 PRINT_HILOGD("PrintServiceProxy QueryAllPrintJob Failed.");
434 return ret;
435 }
436
437 uint32_t len = reply.ReadUint32();
438 for (uint32_t i = 0; i < len; i++) {
439 auto jobPtr = PrintJob::Unmarshalling(reply);
440 if (jobPtr == nullptr) {
441 PRINT_HILOGE("wrong printJob from data");
442 return E_PRINT_GENERIC_FAILURE;
443 }
444 printJobs.emplace_back(*jobPtr);
445 }
446 PRINT_HILOGD("PrintServiceProxy QueryAllPrintJob succeeded.");
447 return E_PRINT_NONE;
448 }
449
QueryPrintJobById(std::string & printJobId,PrintJob & printJob)450 int32_t PrintServiceProxy::QueryPrintJobById(std::string &printJobId, PrintJob &printJob)
451 {
452 MessageParcel data, reply;
453 MessageOption option;
454 data.WriteInterfaceToken(GetDescriptor());
455 data.WriteString(printJobId);
456 PRINT_HILOGD("PrintServiceProxy QueryAllPrintJob started.");
457 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_QUERYPRINTJOBBYID, data, reply, option);
458 ret = GetResult(ret, reply);
459 auto printJobPtr = PrintJob::Unmarshalling(reply);
460 if (printJobPtr == nullptr) {
461 PRINT_HILOGE("wrong printJob from data");
462 return E_PRINT_GENERIC_FAILURE;
463 }
464 printJob = *printJobPtr;
465 PRINT_HILOGD("[QueryPrintJobById] printerId : %{public}s", printJob.GetJobId().c_str());
466 PRINT_HILOGD("PrintServiceProxy QueryPrintJobById succeeded.");
467 return ret;
468 }
469
AddPrinterToCups(const std::string & printerUri,const std::string & printerName,const std::string & printerMake)470 int32_t PrintServiceProxy::AddPrinterToCups(const std::string &printerUri, const std::string &printerName,
471 const std::string &printerMake)
472 {
473 MessageParcel data, reply;
474 MessageOption option;
475 data.WriteInterfaceToken(GetDescriptor());
476 data.WriteString(printerUri);
477 data.WriteString(printerName);
478 data.WriteString(printerMake);
479 PRINT_HILOGD("PrintServiceProxy AddPrinterToCups started.");
480 sptr<IRemoteObject> remote = Remote();
481 if (remote == nullptr) {
482 PRINT_HILOGE("PrintServiceProxy AddPrinterToCups remote is null");
483 return E_PRINT_RPC_FAILURE;
484 }
485 int32_t ret = remote->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_ADDPRINTERTOCUPS, data, reply, option);
486 ret = GetResult(ret, reply);
487 PRINT_HILOGD("PrintServiceProxy AddPrinterToCups succeeded.");
488 return ret;
489 }
490
QueryPrinterCapabilityByUri(const std::string & printerUri,const std::string & printerId,PrinterCapability & printerCaps)491 int32_t PrintServiceProxy::QueryPrinterCapabilityByUri(const std::string &printerUri, const std::string &printerId,
492 PrinterCapability &printerCaps)
493 {
494 MessageParcel data, reply;
495 MessageOption option;
496 data.WriteInterfaceToken(GetDescriptor());
497 data.WriteString(printerUri);
498 data.WriteString(printerId);
499 PRINT_HILOGD("PrintServiceProxy QueryPrinterCapabilityByUri started.");
500 sptr<IRemoteObject> remote = Remote();
501 if (remote == nullptr) {
502 PRINT_HILOGE("PrintServiceProxy AddPrinterToCups remote is null");
503 return E_PRINT_RPC_FAILURE;
504 }
505 int32_t ret = remote->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_QUERYPRINTERCAPABILITYBYURI,
506 data, reply, option);
507 ret = GetResult(ret, reply);
508 auto printerCapsPtr = PrinterCapability::Unmarshalling(reply);
509 printerCaps = *printerCapsPtr;
510 PRINT_HILOGD("PrintServiceProxy QueryPrinterCapabilityByUri succeeded.");
511 return ret;
512 }
513
NotifyPrintServiceEvent(std::string & jobId,uint32_t event)514 int32_t PrintServiceProxy::NotifyPrintServiceEvent(std::string &jobId, uint32_t event)
515 {
516 MessageParcel data, reply;
517 MessageOption option;
518 data.WriteInterfaceToken(GetDescriptor());
519 data.WriteString(jobId);
520 data.WriteUint32(event);
521 PRINT_HILOGD("PrintServiceProxy NotifyPrintServiceEvent started.");
522 int32_t ret = Remote()->SendRequest(
523 OHOS::Print::IPrintInterfaceCode::CMD_NOTIFY_PRINT_SERVICE_EVENT, data, reply, option);
524 ret = GetResult(ret, reply);
525 PRINT_HILOGD("PrintServiceProxy NotifyPrintServiceEvent out. ret = [%{public}d]", ret);
526 return ret;
527 }
528
SetDefaultPrinter(const std::string & printerId,uint32_t type)529 int32_t PrintServiceProxy::SetDefaultPrinter(const std::string &printerId, uint32_t type)
530 {
531 MessageParcel data, reply;
532 MessageOption option;
533 data.WriteInterfaceToken(GetDescriptor());
534 data.WriteString(printerId);
535 data.WriteUint32(type);
536 PRINT_HILOGD("PrintServiceProxy SetDefaultPrinter started.");
537 int32_t ret =
538 Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_SET_DEFAULT_PRINTERID, data, reply, option);
539 ret = GetResult(ret, reply);
540 PRINT_HILOGD("PrintServiceProxy SetDefaultPrinter out. ret = [%{public}d]", ret);
541 return ret;
542 }
543
DeletePrinterFromCups(const std::string & printerUri,const std::string & printerName,const std::string & printerMake)544 int32_t PrintServiceProxy::DeletePrinterFromCups(
545 const std::string &printerUri, const std::string &printerName, const std::string &printerMake)
546 {
547 MessageParcel data, reply;
548 MessageOption option;
549 data.WriteInterfaceToken(GetDescriptor());
550 data.WriteString(printerUri);
551 data.WriteString(printerName);
552 data.WriteString(printerMake);
553 PRINT_HILOGD("PrintServiceProxy DeletePrinterFromCups started.");
554 int32_t ret =
555 Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_DELETE_PRINTER_FROM_CUPS, data, reply, option);
556 ret = GetResult(ret, reply);
557 PRINT_HILOGD("PrintServiceProxy DeletePrinterFromCups out. ret = [%{public}d]", ret);
558 return ret;
559 }
560
On(const std::string taskId,const std::string & type,const sptr<IPrintCallback> & listener)561 int32_t PrintServiceProxy::On(const std::string taskId, const std::string &type, const sptr<IPrintCallback> &listener)
562 {
563 if (listener == nullptr) {
564 PRINT_HILOGE("listener is nullptr");
565 return E_PRINT_INVALID_PARAMETER;
566 }
567
568 if (type.empty()) {
569 PRINT_HILOGE("PrintServiceProxy::On type is null.");
570 return E_PRINT_INVALID_PARAMETER;
571 }
572
573 MessageParcel data, reply;
574 MessageOption option;
575
576 data.WriteInterfaceToken(GetDescriptor());
577 data.WriteString(taskId);
578 data.WriteString(type);
579 data.WriteRemoteObject(listener->AsObject().GetRefPtr());
580 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_ON, data, reply, option);
581 ret = GetResult(ret, reply);
582 PRINT_HILOGD("PrintServiceProxy On out. ret = [%{public}d]", ret);
583 return ret;
584 }
585
Off(const std::string taskId,const std::string & type)586 int32_t PrintServiceProxy::Off(const std::string taskId, const std::string &type)
587 {
588 PRINT_HILOGD("PrintServiceProxy::Off in");
589 if (type.empty()) {
590 PRINT_HILOGE("PrintServiceProxy::On type is null.");
591 return E_PRINT_INVALID_PARAMETER;
592 }
593
594 MessageParcel data, reply;
595 MessageOption option;
596
597 data.WriteInterfaceToken(GetDescriptor());
598 data.WriteString(taskId);
599 data.WriteString(type);
600 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_OFF, data, reply, option);
601 ret = GetResult(ret, reply);
602 PRINT_HILOGD("PrintServiceProxy Off out. ret = [%{public}d]", ret);
603 return ret;
604 }
605
RegisterPrinterCallback(const std::string & type,const sptr<IPrintCallback> & listener)606 int32_t PrintServiceProxy::RegisterPrinterCallback(const std::string &type, const sptr<IPrintCallback> &listener)
607 {
608 if (listener == nullptr) {
609 PRINT_HILOGE("listener is nullptr");
610 return E_PRINT_INVALID_PARAMETER;
611 }
612
613 if (type.empty()) {
614 PRINT_HILOGE("PrintServiceProxy:: type is empty.");
615 return E_PRINT_INVALID_PARAMETER;
616 }
617
618 MessageParcel data, reply;
619 MessageOption option;
620
621 data.WriteInterfaceToken(GetDescriptor());
622 data.WriteString(type);
623 data.WriteRemoteObject(listener->AsObject().GetRefPtr());
624 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_REG_PRINTER_CB, data, reply, option);
625 ret = GetResult(ret, reply);
626 PRINT_HILOGD("PrintServiceProxy RegisterPrinterCallback out. ret = [%{public}d]", ret);
627 return ret;
628 }
629
UnregisterPrinterCallback(const std::string & type)630 int32_t PrintServiceProxy::UnregisterPrinterCallback(const std::string &type)
631 {
632 PRINT_HILOGD("PrintServiceProxy::UnregisterPrinterCallback in");
633 if (type.empty()) {
634 PRINT_HILOGE("PrintServiceProxy::type is empty.");
635 return E_PRINT_INVALID_PARAMETER;
636 }
637
638 MessageParcel data, reply;
639 MessageOption option;
640
641 data.WriteInterfaceToken(GetDescriptor());
642 data.WriteString(type);
643 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_UNREG_PRINTER_CB, data, reply, option);
644 ret = GetResult(ret, reply);
645 PRINT_HILOGD("PrintServiceProxy UnregisterPrinterCallback out. ret = [%{public}d]", ret);
646 return ret;
647 }
648
RegisterExtCallback(const std::string & extensionCID,const sptr<IPrintExtensionCallback> & listener)649 int32_t PrintServiceProxy::RegisterExtCallback(const std::string &extensionCID,
650 const sptr<IPrintExtensionCallback> &listener)
651 {
652 if (listener == nullptr) {
653 PRINT_HILOGE("listener is nullptr");
654 return E_PRINT_INVALID_PARAMETER;
655 }
656
657 PRINT_HILOGD("PrintServiceProxy::RegisterExtCallback in: %{public}s", extensionCID.c_str());
658 MessageParcel data, reply;
659 MessageOption option;
660
661 data.WriteInterfaceToken(GetDescriptor());
662 data.WriteString(extensionCID);
663 data.WriteRemoteObject(listener->AsObject().GetRefPtr());
664
665 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_REG_EXT_CB, data, reply, option);
666 ret = GetResult(ret, reply);
667 PRINT_HILOGD("PrintServiceProxy RegisterExtCallback out. ret = [%{public}d]", ret);
668 return ret;
669 }
670
PrintByAdapter(const std::string printJobName,const PrintAttributes & printAttributes,std::string & taskId)671 int32_t PrintServiceProxy::PrintByAdapter(const std::string printJobName, const PrintAttributes &printAttributes,
672 std::string &taskId)
673 {
674 PRINT_HILOGI("PrintServiceProxy PrintByAdapter start.");
675 MessageParcel data, reply;
676 MessageOption option;
677
678 data.WriteInterfaceToken(GetDescriptor());
679 data.WriteString(printJobName);
680 printAttributes.Marshalling(data);
681 data.WriteString(taskId);
682 PRINT_HILOGD("PrintServiceProxy PrintByAdapter started.");
683 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_STARTPRINTJOB_BY_ADAPTER,
684 data, reply, option);
685 if (ret != ERR_NONE) {
686 PRINT_HILOGE("PrintByAdapter, rpc error code = %{public}d", ret);
687 return E_PRINT_RPC_FAILURE;
688 }
689 ret = GetResult(ret, reply);
690 PRINT_HILOGD("PrintServiceProxy PrintByAdapter out. ret = [%{public}d]", ret);
691 return ret;
692 }
693
StartGetPrintFile(const std::string & jobId,const PrintAttributes & printAttributes,const uint32_t fd)694 int32_t PrintServiceProxy::StartGetPrintFile(const std::string &jobId, const PrintAttributes &printAttributes,
695 const uint32_t fd)
696 {
697 MessageParcel data, reply;
698 MessageOption option;
699
700 data.WriteInterfaceToken(GetDescriptor());
701 data.WriteString(jobId);
702 printAttributes.Marshalling(data);
703 data.WriteFileDescriptor(fd);
704 PRINT_HILOGI("PrintServiceProxy StartGetPrintFile started.");
705 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_START_GET_FILE, data, reply, option);
706 if (ret != ERR_NONE) {
707 PRINT_HILOGE("StartGetPrintFile, rpc error code = %{public}d", ret);
708 return E_PRINT_RPC_FAILURE;
709 }
710
711 ret = GetResult(ret, reply);
712 PRINT_HILOGD("PrintServiceProxy StartGetPrintFile out. ret = [%{public}d]", ret);
713 return ret;
714 }
715
NotifyPrintService(const std::string & jobId,const std::string & type)716 int32_t PrintServiceProxy::NotifyPrintService(const std::string &jobId, const std::string &type)
717 {
718 PRINT_HILOGD("PrintServiceProxy::NotifyPrintService in");
719 MessageParcel data, reply;
720 MessageOption option;
721
722 data.WriteInterfaceToken(GetDescriptor());
723 data.WriteString(jobId);
724 data.WriteString(type);
725 int32_t ret = Remote()->SendRequest(
726 OHOS::Print::IPrintInterfaceCode::CMD_NOTIFY_PRINT_SERVICE, data, reply, option);
727 ret = GetResult(ret, reply);
728 PRINT_HILOGD("PrintServiceProxy NotifyPrintService out. ret = [%{public}d]", ret);
729 return ret;
730 }
731
UnregisterAllExtCallback(const std::string & extensionId)732 int32_t PrintServiceProxy::UnregisterAllExtCallback(const std::string &extensionId)
733 {
734 PRINT_HILOGD("PrintServiceProxy::UnregisterAllExtCallback in");
735 MessageParcel data, reply;
736 MessageOption option;
737 data.WriteInterfaceToken(GetDescriptor());
738 data.WriteString(extensionId);
739 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_UNREG_EXT_CB, data, reply, option);
740 ret = GetResult(ret, reply);
741 PRINT_HILOGD("PrintServiceProxy UnregisterAllExtCallback out. ret = [%{public}d]", ret);
742 return ret;
743 }
744
LoadExtSuccess(const std::string & extensionId)745 int32_t PrintServiceProxy::LoadExtSuccess(const std::string &extensionId)
746 {
747 PRINT_HILOGD("PrintServiceProxy::LoadExtSuccess in");
748 MessageParcel data, reply;
749 MessageOption option;
750 data.WriteInterfaceToken(GetDescriptor());
751 data.WriteString(extensionId);
752 int32_t ret = Remote()->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_LOAD_EXT, data, reply, option);
753 ret = GetResult(ret, reply);
754 PRINT_HILOGD("PrintServiceProxy LoadExtSuccess out. ret = [%{public}d]", ret);
755 return ret;
756 }
757 } // namespace OHOS::Print
758