• 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 #include "iservice_registry.h"
19 #include "print_constant.h"
20 #include "print_extension_callback_stub.h"
21 #include "print_log.h"
22 #include "print_sync_load_callback.h"
23 #include "print_utils.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS::Print {
27 std::mutex PrintManagerClient::instanceLock_;
28 sptr<PrintManagerClient> PrintManagerClient::instance_ = nullptr;
29 
PrintManagerClient()30 PrintManagerClient::PrintManagerClient() : printServiceProxy_(nullptr), deathRecipient_(nullptr) {}
31 
~PrintManagerClient()32 PrintManagerClient::~PrintManagerClient() {}
33 
GetInstance()34 sptr<PrintManagerClient> PrintManagerClient::GetInstance()
35 {
36     if (instance_ == nullptr) {
37         std::lock_guard<std::mutex> autoLock(instanceLock_);
38         if (instance_ == nullptr) {
39             instance_ = new PrintManagerClient;
40         }
41     }
42     return instance_;
43 }
44 
GetPrintServiceProxy()45 bool PrintManagerClient::GetPrintServiceProxy()
46 {
47     if (printServiceProxy_ != nullptr) {
48         return true;
49     }
50     bool result = false;
51     std::lock_guard<std::mutex> lock(proxyLock_);
52     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (systemAbilityManager != nullptr) {
54         auto systemAbility = systemAbilityManager->GetSystemAbility(PRINT_SERVICE_ID, "");
55         if (systemAbility != nullptr) {
56             deathRecipient_ = new (std::nothrow) PrintSaDeathRecipient();
57             if (deathRecipient_ != nullptr) {
58                 systemAbility->AddDeathRecipient(deathRecipient_);
59                 printServiceProxy_ = iface_cast<IPrintService>(systemAbility);
60                 PRINT_HILOGD("Getting PrintManagerClientProxy succeeded.");
61                 result = true;
62             }
63         }
64     }
65     PRINT_HILOGD("Getting PrintManagerClientProxy ret[%{public}d].", result);
66     return result;
67 }
68 
OnRemoteSaDied(const wptr<IRemoteObject> & remote)69 void PrintManagerClient::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
70 {
71     PRINT_HILOGD("start");
72     if (remote == nullptr) {
73         PRINT_HILOGE("remote is nullptr");
74         return;
75     }
76     std::lock_guard<std::mutex> lock(proxyLock_);
77     if (printServiceProxy_ == nullptr) {
78         PRINT_HILOGE("printServiceProxy_ is null");
79         return;
80     }
81     auto serviceRemote = printServiceProxy_->AsObject();
82     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
83         PRINT_HILOGD("need reset");
84         serviceRemote->RemoveDeathRecipient(deathRecipient_);
85         printServiceProxy_ = nullptr;
86         deathRecipient_ = nullptr;
87     }
88 }
89 
StartPrint(const std::vector<std::string> & fileList,const std::vector<uint32_t> & fdList,std::string & taskId)90 int32_t PrintManagerClient::StartPrint(const std::vector<std::string> &fileList,
91     const std::vector<uint32_t> &fdList, std::string &taskId)
92 {
93     PRINT_HILOGD("PrintManagerClient StartPrint start.");
94     int32_t ret = E_PRINT_RPC_FAILURE;
95     if (LoadServer() && GetPrintServiceProxy()) {
96         ret = printServiceProxy_->StartPrint(fileList, fdList, taskId);
97         PRINT_HILOGD("PrintManagerClient StartPrint out ret = [%{public}d].", ret);
98     }
99     return ret;
100 }
101 
StartPrint(const std::vector<std::string> & fileList,const std::vector<uint32_t> & fdList,std::string & taskId,const sptr<IRemoteObject> & token)102 int32_t PrintManagerClient::StartPrint(const std::vector<std::string> &fileList, const std::vector<uint32_t> &fdList,
103     std::string &taskId, const sptr<IRemoteObject> &token)
104 {
105     PRINT_HILOGD("PrintManagerClient StartPrint start.");
106     int32_t ret = E_PRINT_RPC_FAILURE;
107     if (LoadServer() && GetPrintServiceProxy()) {
108         ret = printServiceProxy_->StartPrint(fileList, fdList, taskId, token);
109         PRINT_HILOGD("PrintManagerClient StartPrint out ret = [%{public}d].", ret);
110     }
111     return ret;
112 }
113 
StopPrint(const std::string & taskId)114 int32_t PrintManagerClient::StopPrint(const std::string &taskId)
115 {
116     PRINT_HILOGD("PrintManagerClient StopPrint start.");
117     int32_t ret = E_PRINT_RPC_FAILURE;
118     if (LoadServer() && GetPrintServiceProxy()) {
119         ret = printServiceProxy_->StopPrint(taskId);
120         PRINT_HILOGD("PrintManagerClient StopPrint out ret = [%{public}d].", ret);
121     }
122     return ret;
123 }
124 
ConnectPrinter(const std::string & printerId)125 int32_t PrintManagerClient::ConnectPrinter(const std::string &printerId)
126 {
127     PRINT_HILOGD("PrintManagerClient ConnectPrinter start.");
128     int32_t ret = E_PRINT_RPC_FAILURE;
129     if (LoadServer() && GetPrintServiceProxy()) {
130         ret = printServiceProxy_->ConnectPrinter(printerId);
131         PRINT_HILOGD("PrintManagerClient ConnectPrinter out ret = [%{public}d].", ret);
132     }
133     return ret;
134 }
135 
DisconnectPrinter(const std::string & printerId)136 int32_t PrintManagerClient::DisconnectPrinter(const std::string &printerId)
137 {
138     PRINT_HILOGD("PrintManagerClient DisconnectPrinter start.");
139     int32_t ret = E_PRINT_RPC_FAILURE;
140     if (LoadServer() && GetPrintServiceProxy()) {
141         ret = printServiceProxy_->DisconnectPrinter(printerId);
142         PRINT_HILOGD("PrintManagerClient DisconnectPrinter out ret = [%{public}d].", ret);
143     }
144     return ret;
145 }
146 
QueryAllExtension(std::vector<PrintExtensionInfo> & extensionInfos)147 int32_t PrintManagerClient::QueryAllExtension(std::vector<PrintExtensionInfo> &extensionInfos)
148 {
149     PRINT_HILOGD("PrintManagerClient QueryAllExtension start.");
150     int32_t ret = E_PRINT_RPC_FAILURE;
151     if (LoadServer() && GetPrintServiceProxy()) {
152         ret = printServiceProxy_->QueryAllExtension(extensionInfos);
153         PRINT_HILOGD("PrintManagerClient QueryAllExtension out ret = [%{public}d].", ret);
154     }
155     return ret;
156 }
157 
StartDiscoverPrinter(const std::vector<std::string> & extensionList)158 int32_t PrintManagerClient::StartDiscoverPrinter(const std::vector<std::string> &extensionList)
159 {
160     PRINT_HILOGD("PrintManagerClient StartDiscoverPrinter start.");
161     int32_t ret = E_PRINT_RPC_FAILURE;
162     if (LoadServer() && GetPrintServiceProxy()) {
163         ret = printServiceProxy_->StartDiscoverPrinter(extensionList);
164         PRINT_HILOGD("PrintManagerClient StartDiscoverPrinter out ret = [%{public}d].", ret);
165     }
166     return ret;
167 }
168 
StopDiscoverPrinter()169 int32_t PrintManagerClient::StopDiscoverPrinter()
170 {
171     PRINT_HILOGD("PrintManagerClient StopDiscoverPrinter start.");
172     int32_t ret = E_PRINT_RPC_FAILURE;
173     if (LoadServer() && GetPrintServiceProxy()) {
174         ret = printServiceProxy_->StopDiscoverPrinter();
175         PRINT_HILOGD("PrintManagerClient StopDiscoverPrinter out ret = [%{public}d].", ret);
176     }
177     return ret;
178 }
179 
StartPrintJob(const PrintJob & jobinfo)180 int32_t PrintManagerClient::StartPrintJob(const PrintJob &jobinfo)
181 {
182     PRINT_HILOGD("PrintManagerClient StartPrintJob start.");
183     int32_t ret = E_PRINT_RPC_FAILURE;
184     if (LoadServer() && GetPrintServiceProxy()) {
185         ret = printServiceProxy_->StartPrintJob(jobinfo);
186         PRINT_HILOGD("PrintManagerClient StartPrintJob out ret = [%{public}d].", ret);
187     }
188     return ret;
189 }
190 
CancelPrintJob(const std::string & jobId)191 int32_t PrintManagerClient::CancelPrintJob(const std::string &jobId)
192 {
193     PRINT_HILOGD("PrintManagerClient CancelPrintJob start.");
194     int32_t ret = E_PRINT_RPC_FAILURE;
195     if (LoadServer() && GetPrintServiceProxy()) {
196         ret = printServiceProxy_->CancelPrintJob(jobId);
197         PRINT_HILOGD("PrintManagerClient CancelPrintJob out ret = [%{public}d].", ret);
198     }
199     return ret;
200 }
201 
AddPrinters(const std::vector<PrinterInfo> & printerInfos)202 int32_t PrintManagerClient::AddPrinters(const std::vector<PrinterInfo> &printerInfos)
203 {
204     PRINT_HILOGD("PrintManagerClient AddPrinters start.");
205     int32_t ret = E_PRINT_RPC_FAILURE;
206     if (LoadServer() && GetPrintServiceProxy()) {
207         ret = printServiceProxy_->AddPrinters(printerInfos);
208         PRINT_HILOGD("PrintManagerClient AddPrinters out ret = [%{public}d].", ret);
209     }
210     return ret;
211 }
212 
RemovePrinters(const std::vector<std::string> & printerIds)213 int32_t PrintManagerClient::RemovePrinters(const std::vector<std::string> &printerIds)
214 {
215     PRINT_HILOGD("PrintManagerClient RemovePrinters start.");
216     int32_t ret = E_PRINT_RPC_FAILURE;
217     if (LoadServer() && GetPrintServiceProxy()) {
218         ret = printServiceProxy_->RemovePrinters(printerIds);
219         PRINT_HILOGD("PrintManagerClient RemovePrinters out ret = [%{public}d].", ret);
220     }
221     return ret;
222 }
223 
UpdatePrinters(const std::vector<PrinterInfo> & printerInfos)224 int32_t PrintManagerClient::UpdatePrinters(const std::vector<PrinterInfo> &printerInfos)
225 {
226     PRINT_HILOGD("PrintManagerClient UpdatePrinters start.");
227     int32_t ret = E_PRINT_RPC_FAILURE;
228     if (LoadServer() && GetPrintServiceProxy()) {
229         ret = printServiceProxy_->UpdatePrinters(printerInfos);
230         PRINT_HILOGD("PrintManagerClient UpdatePrinters out ret = [%{public}d].", ret);
231     }
232     return ret;
233 }
234 
UpdatePrinterState(const std::string & printerId,uint32_t state)235 int32_t PrintManagerClient::UpdatePrinterState(const std::string &printerId, uint32_t state)
236 {
237     PRINT_HILOGD("PrintManagerClient UpdatePrinterState start.");
238     int32_t ret = E_PRINT_RPC_FAILURE;
239     if (LoadServer() && GetPrintServiceProxy()) {
240         ret = printServiceProxy_->UpdatePrinterState(printerId, state);
241         PRINT_HILOGD("PrintManagerClient UpdatePrinterState out ret = [%{public}d].", ret);
242     }
243     return ret;
244 }
245 
UpdatePrintJobState(const std::string & jobId,uint32_t state,uint32_t subState)246 int32_t PrintManagerClient::UpdatePrintJobState(const std::string &jobId, uint32_t state, uint32_t subState)
247 {
248     PRINT_HILOGD("PrintManagerClient UpdatePrintJobState start.");
249     int32_t ret = E_PRINT_RPC_FAILURE;
250     if (LoadServer() && GetPrintServiceProxy()) {
251         ret = printServiceProxy_->UpdatePrintJobState(jobId, state, subState);
252         PRINT_HILOGD("PrintManagerClient UpdatePrintJobState out ret = [%{public}d].", ret);
253     }
254     return ret;
255 }
256 
UpdateExtensionInfo(const std::string & extInfo)257 int32_t PrintManagerClient::UpdateExtensionInfo(const std::string &extInfo)
258 {
259     PRINT_HILOGD("PrintManagerClient UpdateExtensionInfo start.");
260     int32_t ret = E_PRINT_RPC_FAILURE;
261     if (LoadServer() && GetPrintServiceProxy()) {
262         ret = printServiceProxy_->UpdateExtensionInfo(extInfo);
263         PRINT_HILOGD("PrintManagerClient UpdateExtensionInfo out ret = [%{public}d].", ret);
264     }
265     return ret;
266 }
267 
RequestPreview(const PrintJob & jobinfo,std::string & previewResult)268 int32_t PrintManagerClient::RequestPreview(const PrintJob &jobinfo, std::string &previewResult)
269 {
270     PRINT_HILOGD("PrintManagerClient RequestPreview start.");
271     int32_t ret = E_PRINT_RPC_FAILURE;
272     if (LoadServer() && GetPrintServiceProxy()) {
273         ret = printServiceProxy_->RequestPreview(jobinfo, previewResult);
274         PRINT_HILOGD("PrintManagerClient RequestPreview out ret = [%{public}d].", ret);
275     }
276     return ret;
277 }
278 
QueryPrinterCapability(const std::string & printerId)279 int32_t PrintManagerClient::QueryPrinterCapability(const std::string &printerId)
280 {
281     PRINT_HILOGD("PrintManagerClient QueryPrinterCapability start.");
282     int32_t ret = E_PRINT_RPC_FAILURE;
283     if (LoadServer() && GetPrintServiceProxy()) {
284         ret = printServiceProxy_->QueryPrinterCapability(printerId);
285         PRINT_HILOGD("PrintManagerClient QueryPrinterCapability out ret = [%{public}d].", ret);
286     }
287     return ret;
288 }
289 
QueryAllPrintJob(std::vector<PrintJob> & printJobs)290 int32_t PrintManagerClient::QueryAllPrintJob(std::vector<PrintJob> &printJobs)
291 {
292     PRINT_HILOGD("PrintManagerClient QueryAllPrintJob start.");
293     int32_t ret = E_PRINT_RPC_FAILURE;
294     if (LoadServer() && GetPrintServiceProxy()) {
295         ret = printServiceProxy_->QueryAllPrintJob(printJobs);
296         PRINT_HILOGD("PrintManagerClient QueryAllPrintJob out ret = [%{public}d].", ret);
297     }
298     return ret;
299 }
300 
QueryPrintJobById(std::string & printJobId,PrintJob & printjob)301 int32_t PrintManagerClient::QueryPrintJobById(std::string &printJobId, PrintJob &printjob)
302 {
303     PRINT_HILOGD("PrintManagerClient QueryPrintJobById start.");
304     int32_t ret = E_PRINT_RPC_FAILURE;
305     if (LoadServer() && GetPrintServiceProxy()) {
306         ret = printServiceProxy_->QueryPrintJobById(printJobId, printjob);
307         PRINT_HILOGD("PrintManagerClient QueryPrintJobById out ret = [%{public}d].", ret);
308     }
309     return ret;
310 }
311 
AddPrinterToCups(const std::string & printerUri,const std::string & printerName)312 int32_t PrintManagerClient::AddPrinterToCups(const std::string &printerUri, const std::string &printerName)
313 {
314     PRINT_HILOGD("PrintManagerClient AddPrinterToCups start.");
315     int32_t ret = E_PRINT_RPC_FAILURE;
316     if (LoadServer() && GetPrintServiceProxy()) {
317         ret = printServiceProxy_->AddPrinterToCups(printerUri, printerName);
318         PRINT_HILOGD("PrintManagerClient AddPrinterToCups out ret = [%{public}d].", ret);
319     }
320     return ret;
321 }
322 
QueryPrinterCapabilityByUri(const std::string & printerUri,PrinterCapability & printerCaps)323 int32_t PrintManagerClient::QueryPrinterCapabilityByUri(const std::string &printerUri, PrinterCapability &printerCaps)
324 {
325     PRINT_HILOGD("PrintManagerClient QueryPrinterCapabilityByUri start.");
326     int32_t ret = E_PRINT_RPC_FAILURE;
327     if (LoadServer() && GetPrintServiceProxy()) {
328         ret = printServiceProxy_->QueryPrinterCapabilityByUri(printerUri, printerCaps);
329         PRINT_HILOGD("PrintManagerClient QueryPrinterCapabilityByUri out ret = [%{public}d].", ret);
330     }
331     return ret;
332 }
333 
On(const std::string & taskId,const std::string & type,const sptr<IPrintCallback> & listener)334 int32_t PrintManagerClient::On(const std::string &taskId, const std::string &type, const sptr<IPrintCallback> &listener)
335 {
336     PRINT_HILOGD("PrintManagerClient On start.");
337     int32_t ret = E_PRINT_RPC_FAILURE;
338     if (LoadServer() && GetPrintServiceProxy()) {
339         ret = printServiceProxy_->On(taskId, type, listener);
340         PRINT_HILOGD("PrintManagerClient On out ret = [%{public}d].", ret);
341     }
342     return ret;
343 }
344 
Off(const std::string & taskId,const std::string & type)345 int32_t PrintManagerClient::Off(const std::string &taskId, const std::string &type)
346 {
347     PRINT_HILOGD("PrintManagerClient Off start.");
348     int32_t ret = E_PRINT_RPC_FAILURE;
349     if (LoadServer() && GetPrintServiceProxy()) {
350         ret = printServiceProxy_->Off(taskId, type);
351         PRINT_HILOGD("PrintManagerClient Off out ret = [%{public}d].", ret);
352     }
353     return ret;
354 }
355 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrintExtCallback cb)356 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
357     uint32_t callbackId, PrintExtCallback cb)
358 {
359     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
360     if (callbackId >= PRINT_EXTCB_MAX) {
361         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
362         return E_PRINT_INVALID_PARAMETER;
363     }
364     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
365     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
366     auto it = extCallbackMap_.find(extensionCID);
367     if (it == extCallbackMap_.end()) {
368         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
369         if (callbackStub != nullptr) {
370             callbackStub->SetExtCallback(cb);
371             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
372         }
373     } else {
374         callbackStub = it->second;
375         callbackStub->SetExtCallback(cb);
376     }
377     int32_t ret = E_PRINT_RPC_FAILURE;
378     if (LoadServer() && GetPrintServiceProxy()) {
379         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
380         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
381     }
382     return ret;
383 }
384 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrintJobCallback cb)385 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
386     uint32_t callbackId, PrintJobCallback cb)
387 {
388     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
389     if (callbackId >= PRINT_EXTCB_MAX) {
390         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
391         return E_PRINT_INVALID_PARAMETER;
392     }
393 
394     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
395     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
396     auto it = extCallbackMap_.find(extensionCID);
397     if (it == extCallbackMap_.end()) {
398         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
399         if (callbackStub != nullptr) {
400             callbackStub->SetPrintJobCallback(cb);
401             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
402         }
403     } else {
404         callbackStub = it->second;
405         callbackStub->SetPrintJobCallback(cb);
406     }
407 
408     int32_t ret = E_PRINT_RPC_FAILURE;
409     if (LoadServer() && GetPrintServiceProxy()) {
410         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
411         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
412     }
413     return ret;
414 }
415 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrinterCapabilityCallback cb)416 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
417     uint32_t callbackId, PrinterCapabilityCallback cb)
418 {
419     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
420     if (callbackId >= PRINT_EXTCB_MAX) {
421         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
422         return E_PRINT_INVALID_PARAMETER;
423     }
424 
425     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
426     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
427     auto it = extCallbackMap_.find(extensionCID);
428     if (it == extCallbackMap_.end()) {
429         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
430         if (callbackStub != nullptr) {
431             callbackStub->SetCapabilityCallback(cb);
432             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
433         }
434     } else {
435         callbackStub = it->second;
436         callbackStub->SetCapabilityCallback(cb);
437     }
438 
439     int32_t ret = E_PRINT_RPC_FAILURE;
440     if (LoadServer() && GetPrintServiceProxy()) {
441         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
442         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
443     }
444     return ret;
445 }
446 
RegisterExtCallback(const std::string & extensionId,uint32_t callbackId,PrinterCallback cb)447 int32_t PrintManagerClient::RegisterExtCallback(const std::string &extensionId,
448     uint32_t callbackId, PrinterCallback cb)
449 {
450     PRINT_HILOGD("PrintManagerClient RegisterExtCallback start.");
451     if (callbackId >= PRINT_EXTCB_MAX) {
452         PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId);
453         return E_PRINT_INVALID_PARAMETER;
454     }
455 
456     sptr<PrintExtensionCallbackStub> callbackStub = nullptr;
457     std::string extensionCID = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
458     auto it = extCallbackMap_.find(extensionCID);
459     if (it == extCallbackMap_.end()) {
460         callbackStub = new (std::nothrow) PrintExtensionCallbackStub;
461         if (callbackStub != nullptr) {
462             callbackStub->SetPrinterCallback(cb);
463             extCallbackMap_.insert(std::make_pair(extensionCID, callbackStub));
464         }
465     } else {
466         callbackStub = it->second;
467         callbackStub->SetPrinterCallback(cb);
468     }
469 
470     int32_t ret = E_PRINT_RPC_FAILURE;
471     if (LoadServer() && GetPrintServiceProxy()) {
472         ret = printServiceProxy_->RegisterExtCallback(extensionCID, callbackStub);
473         PRINT_HILOGD("PrintManagerClient RegisterExtCallback out ret = [%{public}d].", ret);
474     }
475     return ret;
476 }
477 
UnregisterAllExtCallback(const std::string & extensionId)478 int32_t PrintManagerClient::UnregisterAllExtCallback(const std::string &extensionId)
479 {
480     PRINT_HILOGD("PrintManagerClient UnregisterAllExtCallback start.");
481     int32_t ret = E_PRINT_RPC_FAILURE;
482     if (LoadServer() && GetPrintServiceProxy()) {
483         ret = printServiceProxy_->UnregisterAllExtCallback(extensionId);
484         PRINT_HILOGD("PrintManagerClient UnregisterAllExtCallback out ret = [%{public}d].", ret);
485     }
486     extCallbackMap_.clear();
487     return ret;
488 }
489 
LoadExtSuccess(const std::string & extensionId)490 int32_t PrintManagerClient::LoadExtSuccess(const std::string &extensionId)
491 {
492     PRINT_HILOGD("PrintManagerClient LoadExtSuccess start.");
493     int32_t ret = E_PRINT_RPC_FAILURE;
494     if (LoadServer() && GetPrintServiceProxy()) {
495         ret = printServiceProxy_->LoadExtSuccess(extensionId);
496         PRINT_HILOGD("PrintManagerClient LoadExtSuccess out ret = [%{public}d].", ret);
497     }
498     return ret;
499 }
500 
LoadServer()501 bool PrintManagerClient::LoadServer()
502 {
503     if (ready_) {
504         return true;
505     }
506     std::lock_guard<std::mutex> lock(loadMutex_);
507     if (ready_) {
508         return true;
509     }
510 
511     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
512     if (sm == nullptr) {
513         PRINT_HILOGE("GetSystemAbilityManager return null");
514         return false;
515     }
516 
517     sptr<PrintSyncLoadCallback> loadCallback_ = new (std::nothrow) PrintSyncLoadCallback();
518     if (loadCallback_ == nullptr) {
519         PRINT_HILOGE("new PrintSyncLoadCallback fail");
520         return false;
521     }
522 
523     int32_t result = sm->LoadSystemAbility(PRINT_SERVICE_ID, loadCallback_);
524     if (result != ERR_OK) {
525         PRINT_HILOGE("LoadSystemAbility %{public}d failed, result: %{public}d", PRINT_SERVICE_ID, result);
526         return false;
527     }
528 
529     {
530         std::unique_lock<std::mutex> conditionLock(conditionMutex_);
531         auto waitStatus = syncCon_.wait_for(
532             conditionLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), [this]() { return ready_; });
533         if (!waitStatus) {
534             PRINT_HILOGE("print server load sa timeout");
535             return false;
536         }
537     }
538     return true;
539 }
540 
LoadServerSuccess()541 void PrintManagerClient::LoadServerSuccess()
542 {
543     std::unique_lock<std::mutex> lock(conditionMutex_);
544     ready_ = true;
545     syncCon_.notify_one();
546     PRINT_HILOGD("load print server success");
547 }
548 
LoadServerFail()549 void PrintManagerClient::LoadServerFail()
550 {
551     ready_ = false;
552     PRINT_HILOGE("load print server fail");
553 }
SetProxy(const sptr<IRemoteObject> & obj)554 void PrintManagerClient::SetProxy(const sptr<IRemoteObject> &obj)
555 {
556     if (printServiceProxy_ != nullptr) {
557         auto serviceRemote = printServiceProxy_->AsObject();
558         if (serviceRemote != nullptr) {
559             serviceRemote->RemoveDeathRecipient(deathRecipient_);
560             printServiceProxy_ = nullptr;
561             deathRecipient_ = nullptr;
562         }
563     }
564     deathRecipient_ = new (std::nothrow) PrintSaDeathRecipient();
565     if (deathRecipient_ != nullptr) {
566         obj->AddDeathRecipient(deathRecipient_);
567         PRINT_HILOGD("Getting PrintManagerClientProxy succeeded.");
568     }
569     printServiceProxy_ = iface_cast<IPrintService>(obj);
570 }
ResetProxy()571 void PrintManagerClient::ResetProxy()
572 {
573     printServiceProxy_ = nullptr;
574 }
575 } // namespace OHOS::Print
576