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_callback_proxy.h"
16 
17 #include "message_parcel.h"
18 #include "print_log.h"
19 
20 namespace OHOS::Print {
PrintCallbackProxy(const sptr<IRemoteObject> & impl)21 PrintCallbackProxy::PrintCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IPrintCallback>(impl) {}
22 
OnCallback()23 bool PrintCallbackProxy::OnCallback()
24 {
25     PRINT_HILOGD("PrintCallbackProxy::OnCallback Start");
26     MessageParcel data;
27     MessageParcel reply;
28     MessageOption option;
29 
30     data.WriteInterfaceToken(GetDescriptor());
31     sptr<IRemoteObject> remote = Remote();
32     if (remote == nullptr) {
33         PRINT_HILOGE("SendRequest failed, error: remote is null");
34         return false;
35     }
36     int error = remote->SendRequest(PRINT_CALLBACK_TASK, data, reply, option);
37     if (error != 0) {
38         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
39         return false;
40     }
41     PRINT_HILOGD("PrintCallbackProxy::OnCallback End");
42     return true;
43 }
44 
OnCallback(uint32_t state,const PrinterInfo & info)45 bool PrintCallbackProxy::OnCallback(uint32_t state, const PrinterInfo &info)
46 {
47     PRINT_HILOGD("PrintCallbackProxy::OnCallback Start");
48     MessageParcel data;
49     MessageParcel reply;
50     MessageOption option;
51 
52     PRINT_HILOGD("Printer Event argument:[%{public}d], printerId [%{private}s]", state, info.GetPrinterId().c_str());
53     data.WriteInterfaceToken(GetDescriptor());
54     data.WriteUint32(state);
55     info.Marshalling(data);
56 
57     sptr<IRemoteObject> remote = Remote();
58     if (remote == nullptr) {
59         PRINT_HILOGE("SendRequest failed, error: remote is null");
60         return false;
61     }
62     int error = remote->SendRequest(PRINT_CALLBACK_PRINTER, data, reply, option);
63     if (error != 0) {
64         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
65         return false;
66     }
67     PRINT_HILOGD("PrintCallbackProxy::OnCallback End");
68     return true;
69 }
70 
OnCallback(uint32_t state,const PrintJob & info)71 bool PrintCallbackProxy::OnCallback(uint32_t state, const PrintJob &info)
72 {
73     PRINT_HILOGD("PrintCallbackProxy::OnCallback Start");
74     MessageParcel data;
75     MessageParcel reply;
76     MessageOption option;
77 
78     PRINT_HILOGD("PrintJob Event state:[%{public}d], subState [%{public}d]", state, info.GetSubState());
79     data.WriteInterfaceToken(GetDescriptor());
80     data.WriteUint32(state);
81     info.Marshalling(data);
82 
83     sptr<IRemoteObject> remote = Remote();
84     if (remote == nullptr) {
85         PRINT_HILOGE("SendRequest failed, error: remote is null");
86         return false;
87     }
88     int error = remote->SendRequest(PRINT_CALLBACK_PRINT_JOB, data, reply, option);
89     if (error != 0) {
90         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
91         return false;
92     }
93     PRINT_HILOGD("PrintCallbackProxy::OnCallback End");
94     return true;
95 }
96 
OnCallback(const std::string & extensionId,const std::string & info)97 bool PrintCallbackProxy::OnCallback(const std::string &extensionId, const std::string &info)
98 {
99     PRINT_HILOGD("PrintCallbackProxy::OnCallback Start");
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103 
104     data.WriteInterfaceToken(GetDescriptor());
105     data.WriteString(extensionId);
106     data.WriteString(info);
107 
108     sptr<IRemoteObject> remote = Remote();
109     if (remote == nullptr) {
110         PRINT_HILOGE("SendRequest failed, error: remote is null");
111         return false;
112     }
113     int error = remote->SendRequest(PRINT_CALLBACK_EXTINFO, data, reply, option);
114     if (error != 0) {
115         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
116         return false;
117     }
118     PRINT_HILOGD("PrintCallbackProxy::OnCallback End");
119     return true;
120 }
121 
OnCallbackAdapterLayout(const std::string & jobId,const PrintAttributes & oldAttrs,const PrintAttributes & newAttrs,uint32_t fd)122 bool PrintCallbackProxy::OnCallbackAdapterLayout(const std::string &jobId, const PrintAttributes &oldAttrs,
123     const PrintAttributes &newAttrs, uint32_t fd)
124 {
125     PRINT_HILOGI("PrintCallbackProxy::OnCallbackAdapterLayout Start");
126     MessageParcel data;
127     MessageParcel reply;
128     MessageOption option;
129 
130     if (!data.WriteInterfaceToken(GetDescriptor())) {
131         PRINT_HILOGE("write descriptor failed");
132         return false;
133     }
134 
135     data.WriteString(jobId);
136     oldAttrs.Marshalling(data);
137     newAttrs.Marshalling(data);
138     data.WriteFileDescriptor(fd);
139 
140     sptr<IRemoteObject> remote = Remote();
141     if (remote == nullptr) {
142         PRINT_HILOGE("SendRequest failed, error: remote is null");
143         return false;
144     }
145     int error = remote->SendRequest(PRINT_CALLBACK_PRINT_JOB_ADAPTER, data, reply, option);
146     if (error != 0) {
147         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
148         return false;
149     }
150     PRINT_HILOGI("PrintCallbackProxy::OnCallbackAdapterLayout End");
151     return true;
152 }
153 
onCallbackAdapterJobStateChanged(const std::string jobId,const uint32_t state,const uint32_t subState)154 bool PrintCallbackProxy::onCallbackAdapterJobStateChanged(const std::string jobId, const uint32_t state,
155     const uint32_t subState)
156 {
157     PRINT_HILOGI("PrintCallbackProxy::onCallbackAdapterJobStateChanged Start");
158     MessageParcel data;
159     MessageParcel reply;
160     MessageOption option;
161 
162     if (!data.WriteInterfaceToken(GetDescriptor())) {
163         PRINT_HILOGE("write descriptor failed");
164         return false;
165     }
166 
167     data.WriteString(jobId);
168     data.WriteUint32(state);
169     data.WriteUint32(subState);
170 
171     sptr<IRemoteObject> remote = Remote();
172     if (remote == nullptr) {
173         PRINT_HILOGE("SendRequest failed, error: remote is null");
174         return false;
175     }
176     int error = remote->SendRequest(PRINT_CALLBACK_PRINT_JOB_CHANGED_ADAPTER, data, reply, option);
177     if (error != 0) {
178         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
179         return false;
180     }
181     PRINT_HILOGI("PrintCallbackProxy::onCallbackAdapterJobStateChanged End");
182     return true;
183 }
184 
OnCallbackAdapterGetFile(uint32_t state)185 bool PrintCallbackProxy::OnCallbackAdapterGetFile(uint32_t state)
186 {
187     PRINT_HILOGI("PrintCallbackProxy::OnCallbackAdapterGetFile Start");
188     MessageParcel data;
189     MessageParcel reply;
190     MessageOption option;
191 
192     if (!data.WriteInterfaceToken(GetDescriptor())) {
193         PRINT_HILOGE("write descriptor failed");
194         return false;
195     }
196 
197     data.WriteUint32(state);
198 
199     sptr<IRemoteObject> remote = Remote();
200     if (remote == nullptr) {
201         PRINT_HILOGE("SendRequest failed, error: remote is null");
202         return false;
203     }
204     int error = remote->SendRequest(PRINT_CALLBACK_PRINT_GET_FILE_ADAPTER, data, reply, option);
205     if (error != 0) {
206         PRINT_HILOGE("SendRequest failed, error %{public}d", error);
207         return false;
208     }
209     PRINT_HILOGI("PrintCallbackProxy::OnCallbackAdapterGetFile End");
210     return true;
211 }
212 } // namespace OHOS::Print
213