1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "print_extension_callback_proxy.h"
16 
17 #include "message_parcel.h"
18 #include "print_log.h"
19 
20 namespace OHOS::Print {
PrintExtensionCallbackProxy(const sptr<IRemoteObject> & impl)21 PrintExtensionCallbackProxy::PrintExtensionCallbackProxy(const sptr<IRemoteObject> &impl)
22     : IRemoteProxy<IPrintExtensionCallback>(impl) {
23 }
24 
OnCallback()25 bool PrintExtensionCallbackProxy::OnCallback()
26 {
27     PRINT_HILOGD("PrintExtensionCallbackProxy::OnCallBack Start");
28     MessageParcel data;
29     MessageParcel reply;
30     MessageOption option;
31     data.WriteInterfaceToken(GetDescriptor());
32     sptr<IRemoteObject> remote = Remote();
33     if (remote == nullptr) {
34         PRINT_HILOGE("error: remote is null");
35         return false;
36     }
37     int error = remote->SendRequest(PRINT_EXTCB, data, reply, option);
38     if (error != 0) {
39         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
40         return false;
41     }
42     bool ret = reply.ReadBool();
43     PRINT_HILOGD("PrintExtcbProxy::OnCallBack End, ret = %{public}d", ret);
44     return ret;
45 }
46 
OnCallback(const std::string & printerId)47 bool PrintExtensionCallbackProxy::OnCallback(const std::string &printerId)
48 {
49     PRINT_HILOGD("PrintExtensionCallbackProxy::OnCallBack Start");
50     MessageParcel data;
51     MessageParcel reply;
52     MessageOption option;
53     data.WriteInterfaceToken(GetDescriptor());
54     data.WriteString(printerId);
55     sptr<IRemoteObject> remote = Remote();
56     if (remote == nullptr) {
57         PRINT_HILOGE("error: remote is null");
58         return false;
59     }
60     int error = remote->SendRequest(PRINT_EXTCB_PRINTER, data, reply, option);
61     if (error != 0) {
62         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
63         return false;
64     }
65     bool ret = reply.ReadBool();
66     PRINT_HILOGD("PrintExtcbProxy::OnCallBack End, ret = %{public}d", ret);
67     return ret;
68 }
69 
OnCallback(const PrintJob & job)70 bool PrintExtensionCallbackProxy::OnCallback(const PrintJob &job)
71 {
72     PRINT_HILOGD("PrintExtensionCallbackProxy::OnCallBack Start");
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option;
76     data.WriteInterfaceToken(GetDescriptor());
77     job.Marshalling(data);
78     sptr<IRemoteObject> remote = Remote();
79     if (remote == nullptr) {
80         PRINT_HILOGE("error: remote is null");
81         return false;
82     }
83     int error = remote->SendRequest(PRINT_EXTCB_PRINTJOB, data, reply, option);
84     if (error != 0) {
85         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
86         return false;
87     }
88     bool ret = reply.ReadBool();
89     PRINT_HILOGD("PrintExtcbProxy::OnCallBack End, ret = %{public}d", ret);
90     return ret;
91 }
92 
OnCallback(const std::string & printerId,PrinterCapability & cap)93 bool PrintExtensionCallbackProxy::OnCallback(const std::string &printerId, PrinterCapability &cap)
94 {
95     PRINT_HILOGD("PrintExtcbProxy::OnCallBack Start");
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option;
99     data.WriteInterfaceToken(GetDescriptor());
100     data.WriteString(printerId);
101     sptr<IRemoteObject> remote = Remote();
102     if (remote == nullptr) {
103         PRINT_HILOGE("error: remote is null");
104         return false;
105     }
106     int error = remote->SendRequest(PRINT_EXTCB_PRINTCAPABILITY, data, reply, option);
107     if (error != 0) {
108         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
109         return false;
110     }
111     bool ret = reply.ReadBool();
112     if (ret) {
113         auto capPtr = PrinterCapability::Unmarshalling(reply);
114         if (capPtr == nullptr) {
115             PRINT_HILOGE("Failed to create printer capability object");
116             return false;
117         }
118         cap = *capPtr;
119     }
120 
121     PRINT_HILOGD("PrintExtcbProxy::OnCallBack End, ret = %{public}d", ret);
122     return ret;
123 }
124 } // namespace OHOS::Print
125