• 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_extension_callback_stub.h"
17 
18 #include "print_constant.h"
19 #include "print_log.h"
20 #include "print_resolution.h"
21 
22 namespace OHOS::Print {
PrintExtensionCallbackStub()23 PrintExtensionCallbackStub::PrintExtensionCallbackStub() : extCb_(nullptr), jobCb_(nullptr),
24     cb_(nullptr), capability_(nullptr)
25 {
26     cmdMap_[PRINT_EXTCB] = &PrintExtensionCallbackStub::HandleExtCallback;
27     cmdMap_[PRINT_EXTCB_PRINTER] = &PrintExtensionCallbackStub::HandlePrinterCallback;
28     cmdMap_[PRINT_EXTCB_PRINTJOB] = &PrintExtensionCallbackStub::HandlePrintJobCallback;
29     cmdMap_[PRINT_EXTCB_PRINTCAPABILITY] = &PrintExtensionCallbackStub::HandleCapabilityCallback;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t PrintExtensionCallbackStub::OnRemoteRequest(
33     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
34 {
35     PRINT_HILOGD("OnRemoteRequest started, code = %{public}d", code);
36     auto descriptorToken = data.ReadInterfaceToken();
37     if (descriptorToken != GetDescriptor()) {
38         PRINT_HILOGE("Remote descriptor not the same as local descriptor.");
39         return E_PRINT_RPC_FAILURE;
40     }
41 
42     auto itFunc = cmdMap_.find(code);
43     if (itFunc != cmdMap_.end()) {
44         auto requestFunc = itFunc->second;
45         if (requestFunc != nullptr) {
46             return (this->*requestFunc)(data, reply);
47         }
48     }
49     PRINT_HILOGW("default case, need check.");
50     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
51 }
52 
OnCallback()53 bool PrintExtensionCallbackStub::OnCallback()
54 {
55     if (extCb_ != nullptr) {
56         return extCb_();
57     }
58     return false;
59 }
60 
OnCallback(const std::string & printerId)61 bool PrintExtensionCallbackStub::OnCallback(const std::string &printerId)
62 {
63     if (cb_ != nullptr) {
64         return cb_(printerId);
65     }
66     return false;
67 }
68 
OnCallback(const PrintJob & job)69 bool PrintExtensionCallbackStub::OnCallback(const PrintJob &job)
70 {
71     if (jobCb_ != nullptr) {
72         return jobCb_(job);
73     }
74     return false;
75 }
76 
OnCallback(const std::string & printerId,PrinterCapability & cap)77 bool PrintExtensionCallbackStub::OnCallback(const std::string &printerId, PrinterCapability &cap)
78 {
79     if (capability_ != nullptr) {
80         if (capability_(printerId, cap)) {
81             return true;
82         }
83     }
84     return false;
85 }
86 
SetExtCallback(PrintExtCallback cb)87 void PrintExtensionCallbackStub::SetExtCallback(PrintExtCallback cb)
88 {
89     extCb_ = cb;
90 }
91 
SetPrintJobCallback(PrintJobCallback cb)92 void PrintExtensionCallbackStub::SetPrintJobCallback(PrintJobCallback cb)
93 {
94     jobCb_ = cb;
95 }
96 
SetPrinterCallback(PrinterCallback cb)97 void PrintExtensionCallbackStub::SetPrinterCallback(PrinterCallback cb)
98 {
99     cb_ = cb;
100 }
101 
SetCapabilityCallback(PrinterCapabilityCallback cb)102 void PrintExtensionCallbackStub::SetCapabilityCallback(PrinterCapabilityCallback cb)
103 {
104     capability_ = cb;
105 }
106 
HandleExtCallback(MessageParcel & data,MessageParcel & reply)107 bool PrintExtensionCallbackStub::HandleExtCallback(MessageParcel &data, MessageParcel &reply)
108 {
109     bool result = OnCallback();
110     PRINT_HILOGE("Handle Print Extension Callback ret[%{public}d]", result);
111     reply.WriteBool(result);
112     return result;
113 }
114 
HandlePrinterCallback(MessageParcel & data,MessageParcel & reply)115 bool PrintExtensionCallbackStub::HandlePrinterCallback(MessageParcel &data, MessageParcel &reply)
116 {
117     std::string printerId = data.ReadString();
118     bool result = OnCallback(printerId);
119     PRINT_HILOGE("Handle Printer Extension Callback ret[%{public}d]", result);
120     reply.WriteBool(result);
121     return result;
122 }
123 
HandlePrintJobCallback(MessageParcel & data,MessageParcel & reply)124 bool PrintExtensionCallbackStub::HandlePrintJobCallback(MessageParcel &data, MessageParcel &reply)
125 {
126     auto printJobPtr = PrintJob::Unmarshalling(data);
127     bool result = false;
128     if (printJobPtr != nullptr) {
129         result = OnCallback(*printJobPtr);
130         PRINT_HILOGE("Handle Print Job Extension Callback ret[%{public}d]", result);
131         reply.WriteBool(result);
132     }
133     return result;
134 }
135 
HandleCapabilityCallback(MessageParcel & data,MessageParcel & reply)136 bool PrintExtensionCallbackStub::HandleCapabilityCallback(MessageParcel &data, MessageParcel &reply)
137 {
138     std::string printerId = data.ReadString();
139     PrinterCapability cap;
140     bool result = OnCallback(printerId, cap);
141     PRINT_HILOGE("Handle Printer Capability Extension Callback ret[%{public}d]", result);
142     reply.WriteBool(result);
143     cap.Marshalling(reply);
144     return result;
145 }
146 } // namespace OHOS::Print
147