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