• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "print_manager_client.h"
17 
18 #ifdef PDFIUM_ENABLE
19 #include "fpdf_edit.h"
20 #include "cpp/fpdf_scopers.h"
21 #include "image_diff_png.h"
22 #include "iostream"
23 #include "sys/stat.h"
24 #include "unistd.h"
25 #endif // PDFIUM_ENABLE
26 
27 #include "iservice_registry.h"
28 #include "print_constant.h"
29 #include "print_extension_callback_stub.h"
30 #include "print_log.h"
31 #include "print_sync_load_callback.h"
32 #include "print_utils.h"
33 #include "system_ability_definition.h"
34 
35 namespace OHOS::Print {
36 std::mutex PrintManagerClient::instanceLock_;
37 sptr<PrintManagerClient> PrintManagerClient::instance_ = nullptr;
38 
PrintManagerClient()39 PrintManagerClient::PrintManagerClient() : printServiceProxy_(nullptr), deathRecipient_(nullptr) {}
40 
~PrintManagerClient()41 PrintManagerClient::~PrintManagerClient() {}
42 
GetInstance()43 sptr<PrintManagerClient> PrintManagerClient::GetInstance()
44 {
45     if (instance_ == nullptr) {
46         std::lock_guard<std::mutex> autoLock(instanceLock_);
47         if (instance_ == nullptr) {
48             instance_ = new PrintManagerClient;
49         }
50     }
51     return instance_;
52 }
53 
GetPrintServiceProxy()54 bool PrintManagerClient::GetPrintServiceProxy()
55 {
56     if (printServiceProxy_ != nullptr) {
57         return true;
58     }
59     bool result = false;
60     std::lock_guard<std::mutex> lock(proxyLock_);
61     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62     if (systemAbilityManager != nullptr) {
63         auto systemAbility = systemAbilityManager->GetSystemAbility(PRINT_SERVICE_ID, "");
64         if (systemAbility != nullptr) {
65             deathRecipient_ = new (std::nothrow) PrintSaDeathRecipient();
66             if (deathRecipient_ != nullptr) {
67                 systemAbility->AddDeathRecipient(deathRecipient_);
68                 printServiceProxy_ = iface_cast<IPrintService>(systemAbility);
69                 PRINT_HILOGD("Getting PrintManagerClientProxy succeeded.");
70                 result = true;
71             }
72         }
73     }
74     PRINT_HILOGD("Getting PrintManagerClientProxy ret[%{public}d].", result);
75     return result;
76 }
77 
OnRemoteSaDied(const wptr<IRemoteObject> & remote)78 void PrintManagerClient::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
79 {
80     PRINT_HILOGD("start");
81     if (remote == nullptr) {
82         PRINT_HILOGE("remote is nullptr");
83         return;
84     }
85     std::lock_guard<std::mutex> lock(proxyLock_);
86     if (printServiceProxy_ == nullptr) {
87         PRINT_HILOGE("printServiceProxy_ is null");
88         return;
89     }
90     auto serviceRemote = printServiceProxy_->AsObject();
91     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
92         PRINT_HILOGD("need reset");
93         serviceRemote->RemoveDeathRecipient(deathRecipient_);
94         printServiceProxy_ = nullptr;
95         deathRecipient_ = nullptr;
96     }
97 }
98 
StartPrint(const std::vector<std::string> & fileList,const std::vector<uint32_t> & fdList,std::string & taskId)99 int32_t PrintManagerClient::StartPrint(const std::vector<std::string> &fileList,
100     const std::vector<uint32_t> &fdList, std::string &taskId)
101 {
102     PRINT_HILOGD("PrintManagerClient StartPrint start.");
103     int32_t ret = E_PRINT_RPC_FAILURE;
104     if (LoadServer() && GetPrintServiceProxy()) {
105         ret = printServiceProxy_->StartPrint(fileList, fdList, taskId);
106         PRINT_HILOGD("PrintManagerClient StartPrint out ret = [%{public}d].", ret);
107     }
108     return ret;
109 }
110 
StartPrint(const std::vector<std::string> & fileList,const std::vector<uint32_t> & fdList,std::string & taskId,const sptr<IRemoteObject> & token)111 int32_t PrintManagerClient::StartPrint(const std::vector<std::string> &fileList, const std::vector<uint32_t> &fdList,
112     std::string &taskId, const sptr<IRemoteObject> &token)
113 {
114     PRINT_HILOGD("PrintManagerClient StartPrint start.");
115     int32_t ret = E_PRINT_RPC_FAILURE;
116     if (LoadServer() && GetPrintServiceProxy()) {
117         ret = printServiceProxy_->StartPrint(fileList, fdList, taskId, token);
118         PRINT_HILOGD("PrintManagerClient StartPrint out ret = [%{public}d].", ret);
119     }
120     return ret;
121 }
122 
StopPrint(const std::string & taskId)123 int32_t PrintManagerClient::StopPrint(const std::string &taskId)
124 {
125     PRINT_HILOGD("PrintManagerClient StopPrint start.");
126     int32_t ret = E_PRINT_RPC_FAILURE;
127     if (LoadServer() && GetPrintServiceProxy()) {
128         ret = printServiceProxy_->StopPrint(taskId);
129         PRINT_HILOGD("PrintManagerClient StopPrint out ret = [%{public}d].", ret);
130     }
131     return ret;
132 }
133 
ConnectPrinter(const std::string & printerId)134 int32_t PrintManagerClient::ConnectPrinter(const std::string &printerId)
135 {
136     PRINT_HILOGD("PrintManagerClient ConnectPrinter start.");
137     int32_t ret = E_PRINT_RPC_FAILURE;
138     if (LoadServer() && GetPrintServiceProxy()) {
139         ret = printServiceProxy_->ConnectPrinter(printerId);
140         PRINT_HILOGD("PrintManagerClient ConnectPrinter out ret = [%{public}d].", ret);
141     }
142     return ret;
143 }
144 
DisconnectPrinter(const std::string & printerId)145 int32_t PrintManagerClient::DisconnectPrinter(const std::string &printerId)
146 {
147     PRINT_HILOGD("PrintManagerClient DisconnectPrinter start.");
148     int32_t ret = E_PRINT_RPC_FAILURE;
149     if (LoadServer() && GetPrintServiceProxy()) {
150         ret = printServiceProxy_->DisconnectPrinter(printerId);
151         PRINT_HILOGD("PrintManagerClient DisconnectPrinter out ret = [%{public}d].", ret);
152     }
153     return ret;
154 }
155 
QueryAllExtension(std::vector<PrintExtensionInfo> & extensionInfos)156 int32_t PrintManagerClient::QueryAllExtension(std::vector<PrintExtensionInfo> &extensionInfos)
157 {
158     PRINT_HILOGD("PrintManagerClient QueryAllExtension start.");
159     int32_t ret = E_PRINT_RPC_FAILURE;
160     if (LoadServer() && GetPrintServiceProxy()) {
161         ret = printServiceProxy_->QueryAllExtension(extensionInfos);
162         PRINT_HILOGD("PrintManagerClient QueryAllExtension out ret = [%{public}d].", ret);
163     }
164     return ret;
165 }
166 
StartDiscoverPrinter(const std::vector<std::string> & extensionList)167 int32_t PrintManagerClient::StartDiscoverPrinter(const std::vector<std::string> &extensionList)
168 {
169     PRINT_HILOGD("PrintManagerClient StartDiscoverPrinter start.");
170     int32_t ret = E_PRINT_RPC_FAILURE;
171     if (LoadServer() && GetPrintServiceProxy()) {
172         ret = printServiceProxy_->StartDiscoverPrinter(extensionList);
173         PRINT_HILOGD("PrintManagerClient StartDiscoverPrinter out ret = [%{public}d].", ret);
174     }
175     return ret;
176 }
177 
StopDiscoverPrinter()178 int32_t PrintManagerClient::StopDiscoverPrinter()
179 {
180     PRINT_HILOGD("PrintManagerClient StopDiscoverPrinter start.");
181     int32_t ret = E_PRINT_RPC_FAILURE;
182     if (LoadServer() && GetPrintServiceProxy()) {
183         ret = printServiceProxy_->StopDiscoverPrinter();
184         PRINT_HILOGD("PrintManagerClient StopDiscoverPrinter out ret = [%{public}d].", ret);
185     }
186     return ret;
187 }
188 
StartPrintJob(const PrintJob & jobinfo)189 int32_t PrintManagerClient::StartPrintJob(const PrintJob &jobinfo)
190 {
191     PRINT_HILOGD("PrintManagerClient StartPrintJob start.");
192     int32_t ret = E_PRINT_RPC_FAILURE;
193     if (LoadServer() && GetPrintServiceProxy()) {
194         ret = printServiceProxy_->StartPrintJob(jobinfo);
195         PRINT_HILOGD("PrintManagerClient StartPrintJob out ret = [%{public}d].", ret);
196     }
197     return ret;
198 }
199 
CancelPrintJob(const std::string & jobId)200 int32_t PrintManagerClient::CancelPrintJob(const std::string &jobId)
201 {
202     PRINT_HILOGD("PrintManagerClient CancelPrintJob start.");
203     int32_t ret = E_PRINT_RPC_FAILURE;
204     if (LoadServer() && GetPrintServiceProxy()) {
205         ret = printServiceProxy_->CancelPrintJob(jobId);
206         PRINT_HILOGD("PrintManagerClient CancelPrintJob out ret = [%{public}d].", ret);
207     }
208     return ret;
209 }
210 
AddPrinters(const std::vector<PrinterInfo> & printerInfos)211 int32_t PrintManagerClient::AddPrinters(const std::vector<PrinterInfo> &printerInfos)
212 {
213     PRINT_HILOGD("PrintManagerClient AddPrinters start.");
214     int32_t ret = E_PRINT_RPC_FAILURE;
215     if (LoadServer() && GetPrintServiceProxy()) {
216         ret = printServiceProxy_->AddPrinters(printerInfos);
217         PRINT_HILOGD("PrintManagerClient AddPrinters out ret = [%{public}d].", ret);
218     }
219     return ret;
220 }
221 
RemovePrinters(const std::vector<std::string> & printerIds)222 int32_t PrintManagerClient::RemovePrinters(const std::vector<std::string> &printerIds)
223 {
224     PRINT_HILOGD("PrintManagerClient RemovePrinters start.");
225     int32_t ret = E_PRINT_RPC_FAILURE;
226     if (LoadServer() && GetPrintServiceProxy()) {
227         ret = printServiceProxy_->RemovePrinters(printerIds);
228         PRINT_HILOGD("PrintManagerClient RemovePrinters out ret = [%{public}d].", ret);
229     }
230     return ret;
231 }
232 
UpdatePrinters(const std::vector<PrinterInfo> & printerInfos)233 int32_t PrintManagerClient::UpdatePrinters(const std::vector<PrinterInfo> &printerInfos)
234 {
235     PRINT_HILOGD("PrintManagerClient UpdatePrinters start.");
236     int32_t ret = E_PRINT_RPC_FAILURE;
237     if (LoadServer() && GetPrintServiceProxy()) {
238         ret = printServiceProxy_->UpdatePrinters(printerInfos);
239         PRINT_HILOGD("PrintManagerClient UpdatePrinters out ret = [%{public}d].", ret);
240     }
241     return ret;
242 }
243 
UpdatePrinterState(const std::string & printerId,uint32_t state)244 int32_t PrintManagerClient::UpdatePrinterState(const std::string &printerId, uint32_t state)
245 {
246     PRINT_HILOGD("PrintManagerClient UpdatePrinterState start.");
247     int32_t ret = E_PRINT_RPC_FAILURE;
248     if (LoadServer() && GetPrintServiceProxy()) {
249         ret = printServiceProxy_->UpdatePrinterState(printerId, state);
250         PRINT_HILOGD("PrintManagerClient UpdatePrinterState out ret = [%{public}d].", ret);
251     }
252     return ret;
253 }
254 
UpdatePrintJobState(const std::string & jobId,uint32_t state,uint32_t subState)255 int32_t PrintManagerClient::UpdatePrintJobState(const std::string &jobId, uint32_t state, uint32_t subState)
256 {
257     PRINT_HILOGD("PrintManagerClient UpdatePrintJobState start.");
258     int32_t ret = E_PRINT_RPC_FAILURE;
259     if (LoadServer() && GetPrintServiceProxy()) {
260         ret = printServiceProxy_->UpdatePrintJobState(jobId, state, subState);
261         PRINT_HILOGD("PrintManagerClient UpdatePrintJobState out ret = [%{public}d].", ret);
262     }
263     return ret;
264 }
265 
UpdateExtensionInfo(const std::string & extInfo)266 int32_t PrintManagerClient::UpdateExtensionInfo(const std::string &extInfo)
267 {
268     PRINT_HILOGD("PrintManagerClient UpdateExtensionInfo start.");
269     int32_t ret = E_PRINT_RPC_FAILURE;
270     if (LoadServer() && GetPrintServiceProxy()) {
271         ret = printServiceProxy_->UpdateExtensionInfo(extInfo);
272         PRINT_HILOGD("PrintManagerClient UpdateExtensionInfo out ret = [%{public}d].", ret);
273     }
274     return ret;
275 }
276 
RequestPreview(const PrintJob & jobinfo,std::string & previewResult)277 int32_t PrintManagerClient::RequestPreview(const PrintJob &jobinfo, std::string &previewResult)
278 {
279     PRINT_HILOGD("PrintManagerClient RequestPreview start.");
280     int32_t ret = E_PRINT_RPC_FAILURE;
281     if (LoadServer() && GetPrintServiceProxy()) {
282         ret = printServiceProxy_->RequestPreview(jobinfo, previewResult);
283         PRINT_HILOGD("PrintManagerClient RequestPreview out ret = [%{public}d].", ret);
284     }
285     return ret;
286 }
287 
QueryPrinterCapability(const std::string & printerId)288 int32_t PrintManagerClient::QueryPrinterCapability(const std::string &printerId)
289 {
290     PRINT_HILOGD("PrintManagerClient QueryPrinterCapability start.");
291     int32_t ret = E_PRINT_RPC_FAILURE;
292     if (LoadServer() && GetPrintServiceProxy()) {
293         ret = printServiceProxy_->QueryPrinterCapability(printerId);
294         PRINT_HILOGD("PrintManagerClient QueryPrinterCapability out ret = [%{public}d].", ret);
295     }
296     return ret;
297 }
298 
QueryAllPrintJob(std::vector<PrintJob> & printJobs)299 int32_t PrintManagerClient::QueryAllPrintJob(std::vector<PrintJob> &printJobs)
300 {
301     PRINT_HILOGD("PrintManagerClient QueryAllPrintJob start.");
302     int32_t ret = E_PRINT_RPC_FAILURE;
303     if (LoadServer() && GetPrintServiceProxy()) {
304         ret = printServiceProxy_->QueryAllPrintJob(printJobs);
305         PRINT_HILOGD("PrintManagerClient QueryAllPrintJob out ret = [%{public}d].", ret);
306     }
307     return ret;
308 }
309 
QueryPrintJobById(std::string & printJobId,PrintJob & printjob)310 int32_t PrintManagerClient::QueryPrintJobById(std::string &printJobId, PrintJob &printjob)
311 {
312     PRINT_HILOGD("PrintManagerClient QueryPrintJobById start.");
313     int32_t ret = E_PRINT_RPC_FAILURE;
314     if (LoadServer() && GetPrintServiceProxy()) {
315         ret = printServiceProxy_->QueryPrintJobById(printJobId, printjob);
316         PRINT_HILOGD("PrintManagerClient QueryPrintJobById out ret = [%{public}d].", ret);
317     }
318     return ret;
319 }
320 
AddPrinterToCups(const std::string & printerUri,const std::string & printerName,const std::string & printerMake)321 int32_t PrintManagerClient::AddPrinterToCups(const std::string &printerUri, const std::string &printerName,
322     const std::string &printerMake)
323 {
324     PRINT_HILOGD("PrintManagerClient AddPrinterToCups start.");
325     int32_t ret = E_PRINT_RPC_FAILURE;
326     if (LoadServer() && GetPrintServiceProxy()) {
327         ret = printServiceProxy_->AddPrinterToCups(printerUri, printerName, printerMake);
328         PRINT_HILOGD("PrintManagerClient AddPrinterToCups out ret = [%{public}d].", ret);
329     }
330     return ret;
331 }
332 
QueryPrinterCapabilityByUri(const std::string & printerUri,PrinterCapability & printerCaps)333 int32_t PrintManagerClient::QueryPrinterCapabilityByUri(const std::string &printerUri, PrinterCapability &printerCaps)
334 {
335     PRINT_HILOGD("PrintManagerClient QueryPrinterCapabilityByUri start.");
336     int32_t ret = E_PRINT_RPC_FAILURE;
337     if (LoadServer() && GetPrintServiceProxy()) {
338         ret = printServiceProxy_->QueryPrinterCapabilityByUri(printerUri, printerCaps);
339         PRINT_HILOGD("PrintManagerClient QueryPrinterCapabilityByUri out ret = [%{public}d].", ret);
340     }
341     return ret;
342 }
343 
On(const std::string & taskId,const std::string & type,const sptr<IPrintCallback> & listener)344 int32_t PrintManagerClient::On(const std::string &taskId, const std::string &type, const sptr<IPrintCallback> &listener)
345 {
346     PRINT_HILOGD("PrintManagerClient On start.");
347     int32_t ret = E_PRINT_RPC_FAILURE;
348     if (LoadServer() && GetPrintServiceProxy()) {
349         ret = printServiceProxy_->On(taskId, type, listener);
350         PRINT_HILOGD("PrintManagerClient On out ret = [%{public}d].", ret);
351     }
352     return ret;
353 }
354 
Off(const std::string & taskId,const std::string & type)355 int32_t PrintManagerClient::Off(const std::string &taskId, const std::string &type)
356 {
357     PRINT_HILOGD("PrintManagerClient Off start.");
358     int32_t ret = E_PRINT_RPC_FAILURE;
359     if (LoadServer() && GetPrintServiceProxy()) {
360         ret = printServiceProxy_->Off(taskId, type);
361         PRINT_HILOGD("PrintManagerClient Off out ret = [%{public}d].", ret);
362     }
363     return ret;
364 }
365 
Print(const std::string & printJobName,const sptr<IPrintCallback> & listener,const PrintAttributes & printAttributes)366 int32_t PrintManagerClient::Print(const std::string &printJobName, const sptr<IPrintCallback> &listener,
367     const PrintAttributes &printAttributes)
368 {
369     std::string taskId = "";
370     return Print(printJobName, listener, printAttributes, taskId, nullptr);
371 }
372 
Print(const std::string & printJobName,const sptr<IPrintCallback> & listener,const PrintAttributes & printAttributes,void * contextToken)373 int32_t PrintManagerClient::Print(const std::string &printJobName, const sptr<IPrintCallback> &listener,
374     const PrintAttributes &printAttributes, void* contextToken)
375 {
376     std::string taskId = "";
377     return Print(printJobName, listener, printAttributes, taskId, contextToken);
378 }
379 
Print(const std::string & printJobName,const sptr<IPrintCallback> & listener,const PrintAttributes & printAttributes,std::string & taskId)380 int32_t PrintManagerClient::Print(const std::string &printJobName, const sptr<IPrintCallback> &listener,
381     const PrintAttributes &printAttributes, std::string &taskId)
382 {
383     return Print(printJobName, listener, printAttributes, taskId, nullptr);
384 }
385 
Print(const std::string & printJobName,const sptr<IPrintCallback> & listener,const PrintAttributes & printAttributes,std::string & taskId,void * contextToken)386 int32_t PrintManagerClient::Print(const std::string &printJobName, const sptr<IPrintCallback> &listener,
387     const PrintAttributes &printAttributes, std::string &taskId, void* contextToken)
388 {
389     auto func = [printJobName, listener, printAttributes, &taskId, contextToken](sptr<IPrintService> serviceProxy) {
390         serviceProxy->On("", PRINT_CALLBACK_ADAPTER, listener);
391         sptr<IRemoteObject> token = static_cast<IRemoteObject*>(contextToken);
392         return serviceProxy->PrintByAdapter(printJobName, printAttributes, taskId, token);
393     };
394     return CALL_COMMON_CLIENT(func);
395 }
396 
StartGetPrintFile(const std::string & jobId,const PrintAttributes & printAttributes,const uint32_t fd)397 int32_t PrintManagerClient::StartGetPrintFile(const std::string &jobId, const PrintAttributes &printAttributes,
398     const uint32_t fd)
399 {
400     auto func = [jobId, printAttributes, fd](sptr<IPrintService> serviceProxy) {
401         return serviceProxy->StartGetPrintFile(jobId, printAttributes, fd);
402     };
403     return CALL_COMMON_CLIENT(func);
404 }
405 
NotifyPrintService(const std::string & jobId,const std::string & type)406 int32_t PrintManagerClient::NotifyPrintService(const std::string &jobId, const std::string &type)
407 {
408     PRINT_HILOGD("PrintManagerClient NotifyPrintService start.");
409     int32_t ret = E_PRINT_RPC_FAILURE;
410     if (LoadServer() && GetPrintServiceProxy()) {
411         ret = printServiceProxy_->NotifyPrintService(jobId, type);
412         PRINT_HILOGD("PrintManagerClient NotifyPrintService out ret = [%{public}d].", ret);
413     }
414     return ret;
415 }
416 
417 #ifdef PDFIUM_ENABLE
418 #ifndef INT_MAX
419 #define INT_MAX 2147483647
420 #endif
421 static constexpr const int FPDF_CONFIG_VERSION = 2;
422 static constexpr const double SCALING_RATE = 300.0 / 72.0;
423 static int pdfiumInitRequestCount = 0;
424 
initializeLibraryIfNeeded()425 static void initializeLibraryIfNeeded()
426 {
427     if (pdfiumInitRequestCount == 0) {
428         // Init
429         FPDF_LIBRARY_CONFIG config;
430         config.version = FPDF_CONFIG_VERSION;
431         config.m_pUserFontPaths = NULL;
432         config.m_pIsolate = NULL;
433         config.m_v8EmbedderSlot = 0;
434         FPDF_InitLibraryWithConfig(&config);
435     }
436     pdfiumInitRequestCount++;
437 }
438 
destroyLibraryIfNeeded()439 static void destroyLibraryIfNeeded()
440 {
441     if (pdfiumInitRequestCount == 1) {
442         FPDF_DestroyLibrary();
443     }
444     pdfiumInitRequestCount--;
445 }
446 
LoadPdfFile(const std::string filePath)447 static FPDF_DOCUMENT LoadPdfFile(const std::string filePath)
448 {
449     initializeLibraryIfNeeded();
450     // Load pdf file
451     FPDF_DOCUMENT doc = FPDF_LoadDocument(filePath.c_str(), nullptr);
452     return doc;
453 }
454 
CheckDimensions(int stride,int width,int height)455 static bool CheckDimensions(int stride, int width, int height)
456 {
457     if (stride <= 0 || width <= 0 || height <= 0) {
458         return false;
459     }
460     if (height > 0 && stride > INT_MAX / height) {
461         return false;
462     }
463     return true;
464 }
465 
EncodePng(pdfium::span<const uint8_t> input,int height,int width,int format,int stride)466 static std::vector<uint8_t> EncodePng(pdfium::span<const uint8_t> input, int height, int width, int format, int stride)
467 {
468     std::vector<uint8_t> png;
469     switch (format) {
470         case FPDFBitmap_Unknown:
471             break;
472         case FPDFBitmap_Gray:
473             PRINT_HILOGD("EncodePng FPDFBitmap_Gray\n");
474             png = image_diff_png::EncodeGrayPNG(input, width, height, stride);
475             break;
476         case FPDFBitmap_BGR:
477             PRINT_HILOGD("EncodePng FPDFBitmap_BGR\n");
478             png = image_diff_png::EncodeBGRPNG(input, width, height, stride);
479             break;
480         case FPDFBitmap_BGRx:
481             PRINT_HILOGD("EncodePng FPDFBitmap_BGRx\n");
482             png = image_diff_png::EncodeBGRAPNG(input, width, height, stride, true);
483             break;
484         case FPDFBitmap_BGRA:
485             PRINT_HILOGD("EncodePng FPDFBitmap_BGRA\n");
486             png = image_diff_png::EncodeBGRAPNG(input, width, height, stride, false);
487             break;
488     }
489     return png;
490 }
491 
WritePng(std::string imagePath,void * buffer,int width,int height,int stride)492 static bool WritePng(std::string imagePath, void *buffer, int width, int height, int stride)
493 {
494     if (!CheckDimensions(stride, width, height)) {
495         return false;
496     }
497     auto input = pdfium::make_span(static_cast<uint8_t *>(buffer), stride * height);
498     std::vector<uint8_t> png_encoding = EncodePng(input, height, width, FPDFBitmap_BGRA, stride);
499     if (png_encoding.empty()) {
500         PRINT_HILOGE("Failed to convert bitmap to PNG\n");
501         return false;
502     }
503     FILE *fp = fopen(imagePath.c_str(), "wb");
504     if (!fp) {
505         PRINT_HILOGE("Failed to open %s for output\n", imagePath.c_str());
506         return false;
507     }
508     size_t bytes_written = fwrite(&png_encoding.front(), 1, png_encoding.size(), fp);
509     if (bytes_written != png_encoding.size()) {
510         PRINT_HILOGE("Failed to write to %s\n", imagePath.c_str());
511     }
512     (void)fclose(fp);
513     return true;
514 }
515 
GetImagePathByIndex(std::string basePngName,int32_t pageIndex)516 static std::string GetImagePathByIndex(std::string basePngName, int32_t pageIndex)
517 {
518     std::string imagePath = basePngName + "-" + std::to_string(pageIndex) + ".png";
519     return imagePath;
520 }
521 
BitmapInit(FPDF_PAGE page,uint32_t width,uint32_t height)522 static ScopedFPDFBitmap BitmapInit(FPDF_PAGE page, uint32_t width, uint32_t height)
523 {
524     int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
525     ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
526     FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
527     FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
528     int rotation = 0;
529     int flags = FPDF_ANNOT;
530     FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, rotation, flags);
531     return bitmap;
532 }
533 
PdfRenderInit(const std::string filePath,const std::string sandBoxPath,std::string & basePngName,int32_t & pageCount,FPDF_DOCUMENT & doc)534 int32_t PrintManagerClient::PdfRenderInit(const std::string filePath, const std::string sandBoxPath,
535     std::string &basePngName, int32_t &pageCount, FPDF_DOCUMENT &doc)
536 {
537     if (access(sandBoxPath.c_str(), F_OK) != 0) {
538         PRINT_HILOGE("PdfRenderInit SandBoxPath can't be opened.");
539         return E_PRINT_INVALID_PARAMETER;
540     }
541     // Create floder when the floder isn't exist;
542     std::string floderPath = sandBoxPath + "/preview/";
543     if (access(floderPath.c_str(), F_OK) != 0) {
544         mkdir(floderPath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
545         if (access(floderPath.c_str(), F_OK) == 0) {
546             PRINT_HILOGD("PdfRenderInit Create floder %{public}s success.", floderPath.c_str());
547         }
548     }
549     PRINT_HILOGD("PdfRenderInit SandBoxPath:%{public}s.", sandBoxPath.c_str());
550     doc = LoadPdfFile(filePath);
551     if (doc == NULL) {
552         PRINT_HILOGE("PdfRenderInit Pdfium LoadPdfFile failed.");
553         return E_PRINT_FILE_IO;
554     }
555     size_t filename_start = filePath.find_last_of("/");
556     size_t filename_end = filePath.find_last_of(".");
557     if (filename_start == filePath.npos || filename_end == filePath.npos) {
558         PRINT_HILOGE("PdfRenderInit Find filename failed.");
559         return E_PRINT_INVALID_PARAMETER;
560     }
561     std::string filename = filePath.substr(filename_start + 1, filename_end - filename_start - 1);
562     basePngName = floderPath + filename;
563     PRINT_HILOGD("PdfRenderInit basePngName:%{public}s.", basePngName.c_str());
564     // Get pdf file pageCount
565     pageCount = FPDF_GetPageCount(doc);
566     if (pageCount == 0) {
567         PRINT_HILOGE("PdfRenderInit Pdfium GetPageCount failed.");
568         return E_PRINT_INVALID_PARAMETER;
569     }
570     PRINT_HILOGD("PdfRenderInit filePath:%{public}s count %{public}d.", filePath.c_str(), pageCount);
571     return E_PRINT_NONE;
572 }
573 
PdfRenderDestroy(const std::string basePngName,const int32_t pageCount,FPDF_DOCUMENT & doc)574 int32_t PrintManagerClient::PdfRenderDestroy(const std::string basePngName, const int32_t pageCount,
575     FPDF_DOCUMENT &doc)
576 {
577     for (int32_t pageIndex = 0; pageIndex < pageCount; pageIndex++) {
578         std::string imagePath = GetImagePathByIndex(basePngName, pageIndex);
579         if (imagePath.empty()) {
580             PRINT_HILOGE("PdfRenderDestroy This imagePath is empty.");
581             return E_PRINT_INVALID_PARAMETER;
582         }
583         if (access(imagePath.c_str(), F_OK) == 0) {
584             PRINT_HILOGD("PdfRenderDestroy Start delete floder %{public}s.", imagePath.c_str());
585             unlink(imagePath.c_str());
586         }
587     }
588     FPDF_CloseDocument(doc);
589     destroyLibraryIfNeeded();
590     return E_PRINT_NONE;
591 }
592 
GetPdfPageSize(const int32_t pageIndex,uint32_t & width,uint32_t & height,FPDF_DOCUMENT & doc)593 int32_t PrintManagerClient::GetPdfPageSize(const int32_t pageIndex, uint32_t &width, uint32_t &height,
594     FPDF_DOCUMENT &doc)
595 {
596     if (doc == NULL) {
597         PRINT_HILOGE("GetPdfPageSize loaded doc is null.");
598         return E_PRINT_FILE_IO;
599     }
600     // Get page of pageIndex from pdf
601     FPDF_PAGE page = FPDF_LoadPage(doc, pageIndex);
602     if (page == NULL) {
603         PRINT_HILOGE("GetPdfPageSize Pdfium FPDF_LoadPage failed.");
604         return E_PRINT_FILE_IO;
605     }
606     // Get pdf's width and length
607     width = static_cast<uint32_t>(FPDF_GetPageWidth(page) * SCALING_RATE);
608     height = static_cast<uint32_t>(FPDF_GetPageHeight(page) * SCALING_RATE);
609     PRINT_HILOGD("GetPdfPageSize page width: %{public}d, height: %{public}d.", width, height);
610     FPDF_ClosePage(page);
611     return E_PRINT_NONE;
612 }
613 
RenderPdfToPng(const std::string basePngName,const int32_t pageIndex,std::string & imagePath,FPDF_DOCUMENT & doc)614 int32_t PrintManagerClient::RenderPdfToPng(const std::string basePngName, const int32_t pageIndex,
615     std::string &imagePath, FPDF_DOCUMENT &doc)
616 {
617     if (doc == NULL) {
618         PRINT_HILOGE("RenderPdfToPng  loaded doc is null..");
619         return E_PRINT_FILE_IO;
620     }
621     imagePath = GetImagePathByIndex(basePngName, pageIndex);
622     if (imagePath.empty()) {
623         PRINT_HILOGE("RenderPdfToPng This imagePath is empty.");
624         return E_PRINT_INVALID_PARAMETER;
625     }
626     if (access(imagePath.c_str(), F_OK) != -1) {
627         PRINT_HILOGD("RenderPdfToPng This page image is exist %{public}s.", imagePath.c_str());
628         return E_PRINT_NONE;
629     }
630     // Get pdf file pageCount.
631     int pageCount = FPDF_GetPageCount(doc);
632     if (pageCount == 0) {
633         PRINT_HILOGE("RenderPdfToPng Pdfium GetPageCount failed.");
634         return E_PRINT_INVALID_PARAMETER;
635     }
636     PRINT_HILOGD("RenderPdfToPng %{public}s count %{public}d.", basePngName.c_str(), pageCount);
637     // Get page of pageIndex from pdf.
638     FPDF_PAGE page = FPDF_LoadPage(doc, pageIndex);
639     if (page == NULL) {
640         PRINT_HILOGE("RenderPdfToPng Pdfium FPDF_LoadPage failed.");
641         return E_PRINT_FILE_IO;
642     }
643     // Get pdf's width and length.
644     uint32_t width = static_cast<uint32_t>(FPDF_GetPageWidth(page) * SCALING_RATE);
645     uint32_t height = static_cast<uint32_t>(FPDF_GetPageHeight(page) * SCALING_RATE);
646     if (width <= 0 || height <= 0) {
647         PRINT_HILOGE("RenderPdfToPng pdfium get page's width or height error.");
648         return E_PRINT_GENERIC_FAILURE;
649     }
650     PRINT_HILOGD("RenderPdfToPng  page width: %{public}d height: %{public}d.", width, height);
651     ScopedFPDFBitmap bitmap = BitmapInit(page, width, height);
652     FPDF_ClosePage(page);
653     if (bitmap) {
654         void *buffer = FPDFBitmap_GetBuffer(bitmap.get());
655         int stride = FPDFBitmap_GetStride(bitmap.get());
656         PRINT_HILOGD("RenderPdfToPng bitmap stride %{public}d.", stride);
657         if (!WritePng(imagePath, buffer, width, height, stride)) {
658             unlink(imagePath.c_str());
659         }
660     } else {
661         PRINT_HILOGE("RenderPdfToPng FPDF_RenderPageBitmap error.");
662         return E_PRINT_GENERIC_FAILURE;
663     }
664     return E_PRINT_NONE;
665 }
666 #endif // PDFIUM_ENABLE
667 
runBase(const char * callerFunName,std::function<int32_t (sptr<IPrintService>)> func)668 int32_t PrintManagerClient::runBase(const char* callerFunName, std::function<int32_t(sptr<IPrintService>)> func)
669 {
670     PRINT_HILOGI("PrintManagerClient %{public}s start.", callerFunName);
671     int32_t ret = E_PRINT_RPC_FAILURE;
672     if (!LoadServer()) {
673         PRINT_HILOGE("LoadServer error");
674         return ret;
675     }
676 
677     if (!GetPrintServiceProxy()) {
678         PRINT_HILOGE("GetPrintServiceProxy error");
679         return ret;
680     }
681 
682     ret = func(printServiceProxy_);
683     PRINT_HILOGI("PrintManagerClient %{public}s end ret = [%{public}d].", callerFunName, ret);
684     return ret;
685 }
686 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrintExtCallback cb)687 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
688     uint32_t callbackId, PrintExtCallback cb)
689 {
690     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
691     if (callbackId >= PRINT_EXTCB_MAX) {
692         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
693         return E_PRINT_INVALID_PARAMETER;
694     }
695     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
696     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
697     auto it = extCallbackMap_.find(extensionCID);
698     if (it == extCallbackMap_.end()) {
699         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
700         if (callbackStub != nullptr) {
701             callbackStub->SetExtCallback(cb);
702             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
703         }
704     } else {
705         callbackStub = it->second;
706         callbackStub->SetExtCallback(cb);
707     }
708     int32_t ret = E_PRINT_RPC_FAILURE;
709     if (LoadServer() && GetPrintServiceProxy()) {
710         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
711         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
712     }
713     return ret;
714 }
715 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrintJobCallback cb)716 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
717     uint32_t callbackId, PrintJobCallback cb)
718 {
719     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
720     if (callbackId >= PRINT_EXTCB_MAX) {
721         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
722         return E_PRINT_INVALID_PARAMETER;
723     }
724 
725     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
726     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
727     auto it = extCallbackMap_.find(extensionCID);
728     if (it == extCallbackMap_.end()) {
729         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
730         if (callbackStub != nullptr) {
731             callbackStub->SetPrintJobCallback(cb);
732             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
733         }
734     } else {
735         callbackStub = it->second;
736         callbackStub->SetPrintJobCallback(cb);
737     }
738 
739     int32_t ret = E_PRINT_RPC_FAILURE;
740     if (LoadServer() && GetPrintServiceProxy()) {
741         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
742         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
743     }
744     return ret;
745 }
746 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrinterCapabilityCallback cb)747 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
748     uint32_t callbackId, PrinterCapabilityCallback cb)
749 {
750     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
751     if (callbackId >= PRINT_EXTCB_MAX) {
752         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
753         return E_PRINT_INVALID_PARAMETER;
754     }
755 
756     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
757     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
758     auto it = extCallbackMap_.find(extensionCID);
759     if (it == extCallbackMap_.end()) {
760         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
761         if (callbackStub != nullptr) {
762             callbackStub->SetCapabilityCallback(cb);
763             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
764         }
765     } else {
766         callbackStub = it->second;
767         callbackStub->SetCapabilityCallback(cb);
768     }
769 
770     int32_t ret = E_PRINT_RPC_FAILURE;
771     if (LoadServer() && GetPrintServiceProxy()) {
772         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
773         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
774     }
775     return ret;
776 }
777 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrinterCallback cb)778 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
779     uint32_t callbackId, PrinterCallback cb)
780 {
781     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
782     if (callbackId >= PRINT_EXTCB_MAX) {
783         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
784         return E_PRINT_INVALID_PARAMETER;
785     }
786 
787     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
788     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
789     auto it = extCallbackMap_.find(extensionCID);
790     if (it == extCallbackMap_.end()) {
791         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
792         if (callbackStub != nullptr) {
793             callbackStub->SetPrinterCallback(cb);
794             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
795         }
796     } else {
797         callbackStub = it->second;
798         callbackStub->SetPrinterCallback(cb);
799     }
800 
801     int32_t ret = E_PRINT_RPC_FAILURE;
802     if (LoadServer() && GetPrintServiceProxy()) {
803         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
804         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
805     }
806     return ret;
807 }
808 
UnregisterAllExtCallback(const std::string & extensionId)809 int32_t PrintManagerClient::UnregisterAllExtCallback(const std::string &extensionId)
810 {
811     PRINT_HILOGD("PrintManagerClient UnregisterAllExtCallback start.");
812     int32_t ret = E_PRINT_RPC_FAILURE;
813     if (LoadServer() && GetPrintServiceProxy()) {
814         ret = printServiceProxy_->UnregisterAllExtCallback(extensionId);
815         PRINT_HILOGD("PrintManagerClient UnregisterAllExtCallback out ret = [%{public}d].", ret);
816     }
817     extCallbackMap_.clear();
818     return ret;
819 }
820 
LoadExtSuccess(const std::string & extensionId)821 int32_t PrintManagerClient::LoadExtSuccess(const std::string &extensionId)
822 {
823     PRINT_HILOGD("PrintManagerClient LoadExtSuccess start.");
824     int32_t ret = E_PRINT_RPC_FAILURE;
825     if (LoadServer() && GetPrintServiceProxy()) {
826         ret = printServiceProxy_->LoadExtSuccess(extensionId);
827         PRINT_HILOGD("PrintManagerClient LoadExtSuccess out ret = [%{public}d].", ret);
828     }
829     return ret;
830 }
831 
LoadServer()832 bool PrintManagerClient::LoadServer()
833 {
834     if (ready_) {
835         return true;
836     }
837     std::lock_guard<std::mutex> lock(loadMutex_);
838     if (ready_) {
839         return true;
840     }
841 
842     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
843     if (sm == nullptr) {
844         PRINT_HILOGE("GetSystemAbilityManager return null");
845         return false;
846     }
847 
848     sptr<PrintSyncLoadCallback> loadCallback_ = new (std::nothrow) PrintSyncLoadCallback();
849     if (loadCallback_ == nullptr) {
850         PRINT_HILOGE("new PrintSyncLoadCallback fail");
851         return false;
852     }
853 
854     int32_t result = sm->LoadSystemAbility(PRINT_SERVICE_ID, loadCallback_);
855     if (result != ERR_OK) {
856         PRINT_HILOGE("LoadSystemAbility %{public}d failed, result: %{public}d", PRINT_SERVICE_ID, result);
857         return false;
858     }
859 
860     {
861         std::unique_lock<std::mutex> conditionLock(conditionMutex_);
862         auto waitStatus = syncCon_.wait_for(
863             conditionLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), [this]() { return ready_; });
864         if (!waitStatus) {
865             PRINT_HILOGE("print server load sa timeout");
866             return false;
867         }
868     }
869     return true;
870 }
871 
LoadServerSuccess()872 void PrintManagerClient::LoadServerSuccess()
873 {
874     std::unique_lock<std::mutex> lock(conditionMutex_);
875     ready_ = true;
876     syncCon_.notify_one();
877     PRINT_HILOGD("load print server success");
878 }
879 
LoadServerFail()880 void PrintManagerClient::LoadServerFail()
881 {
882     ready_ = false;
883     PRINT_HILOGE("load print server fail");
884 }
SetProxy(const sptr<IRemoteObject> & obj)885 void PrintManagerClient::SetProxy(const sptr<IRemoteObject> &obj)
886 {
887     if (printServiceProxy_ != nullptr) {
888         auto serviceRemote = printServiceProxy_->AsObject();
889         if (serviceRemote != nullptr) {
890             serviceRemote->RemoveDeathRecipient(deathRecipient_);
891             printServiceProxy_ = nullptr;
892             deathRecipient_ = nullptr;
893         }
894     }
895     deathRecipient_ = new (std::nothrow) PrintSaDeathRecipient();
896     if (deathRecipient_ != nullptr) {
897         obj->AddDeathRecipient(deathRecipient_);
898         PRINT_HILOGD("Getting PrintManagerClientProxy succeeded.");
899     }
900     printServiceProxy_ = iface_cast<IPrintService>(obj);
901 }
ResetProxy()902 void PrintManagerClient::ResetProxy()
903 {
904     printServiceProxy_ = nullptr;
905 }
906 } // namespace OHOS::Print
907