• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /* Copyright (c) 2022 Huawei Device Co., Ltd.
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  *     http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 #define LOG_TAG "IObjectSaveCallback"
16 
17 #include "iobject_callback.h"
18 
19 #include <ipc_skeleton.h>
20 
21 #include "itypes_util.h"
22 #include "log_print.h"
23 
24 namespace OHOS {
25 namespace DistributedObject {
26 constexpr static int32_t COMPLETED = 0;
27 
Completed(const std::map<std::string,int32_t> & results)28 void ObjectSaveCallbackProxy::Completed(const std::map<std::string, int32_t> &results)
29 {
30     MessageParcel data;
31     MessageParcel reply;
32     if (!data.WriteInterfaceToken(GetDescriptor())) {
33         ZLOGE("write descriptor failed");
34         return;
35     }
36     if (!ITypesUtil::Marshal(data, results)) {
37         ZLOGE("Marshalling failed");
38         return;
39     }
40     MessageOption mo { MessageOption::TF_SYNC };
41     int error = Remote()->SendRequest(COMPLETED, data, reply, mo);
42     if (error != 0) {
43         ZLOGW("SendRequest failed, error %d", error);
44     }
45 }
46 
ObjectSaveCallbackProxy(const sptr<IRemoteObject> & impl)47 ObjectSaveCallbackProxy::ObjectSaveCallbackProxy(const sptr<IRemoteObject> &impl)
48     : IRemoteProxy<IObjectSaveCallback>(impl)
49 {
50 }
51 
ObjectRevokeSaveCallbackProxy(const sptr<IRemoteObject> & impl)52 ObjectRevokeSaveCallbackProxy::ObjectRevokeSaveCallbackProxy(const sptr<IRemoteObject> &impl)
53     : IRemoteProxy<IObjectRevokeSaveCallback>(impl)
54 {
55 }
56 
ObjectRetrieveCallbackProxy(const sptr<IRemoteObject> & impl)57 ObjectRetrieveCallbackProxy::ObjectRetrieveCallbackProxy(const sptr<IRemoteObject> &impl)
58     : IRemoteProxy<IObjectRetrieveCallback>(impl)
59 {
60 }
61 
ObjectChangeCallbackProxy(const sptr<IRemoteObject> & impl)62 ObjectChangeCallbackProxy::ObjectChangeCallbackProxy(const sptr<IRemoteObject> &impl)
63     : IRemoteProxy<IObjectChangeCallback>(impl)
64 {
65 }
66 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int ObjectSaveCallbackStub::OnRemoteRequest(
68     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
69 {
70     ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
71     auto localDescriptor = GetDescriptor();
72     auto remoteDescriptor = data.ReadInterfaceToken();
73     if (remoteDescriptor != localDescriptor) {
74         ZLOGE("interface token is not equal");
75         return -1;
76     }
77     switch (code) {
78         case COMPLETED: {
79             std::map<std::string, int32_t> results;
80             if (!ITypesUtil::Unmarshal(data, results)) {
81                 ZLOGE("Unmarshalling failed");
82                 return -1;
83             }
84             ZLOGI("object start complete");
85             Completed(results);
86             ZLOGI("object end complete");
87             return 0;
88         }
89         default:
90             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
91     }
92 }
93 
Completed(int32_t status)94 void ObjectRevokeSaveCallbackProxy::Completed(int32_t status)
95 {
96     MessageParcel data;
97     MessageParcel reply;
98     if (!data.WriteInterfaceToken(GetDescriptor())) {
99         ZLOGE("write descriptor failed");
100         return;
101     }
102     if (!ITypesUtil::Marshal(data, status)) {
103         ZLOGE("write descriptor failed");
104         return;
105     }
106     MessageOption mo { MessageOption::TF_SYNC };
107     int error = Remote()->SendRequest(COMPLETED, data, reply, mo);
108     if (error != 0) {
109         ZLOGW("SendRequest failed, error %d", error);
110     }
111 }
112 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)113 int ObjectRevokeSaveCallbackStub::OnRemoteRequest(
114     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
115 {
116     ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
117     auto localDescriptor = GetDescriptor();
118     auto remoteDescriptor = data.ReadInterfaceToken();
119     if (remoteDescriptor != localDescriptor) {
120         ZLOGE("interface token is not equal");
121         return -1;
122     }
123     switch (code) {
124         case COMPLETED: {
125             int32_t status;
126             if (!ITypesUtil::Unmarshal(data, status)) {
127                 ZLOGE("write descriptor failed");
128                 return -1;
129             }
130             ZLOGI("object start complete");
131             Completed(status);
132             ZLOGI("object end complete");
133             return 0;
134         }
135         default:
136             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
137     }
138 }
139 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)140 int ObjectRetrieveCallbackStub::OnRemoteRequest(
141     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
142 {
143     ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
144     auto localDescriptor = GetDescriptor();
145     auto remoteDescriptor = data.ReadInterfaceToken();
146     if (remoteDescriptor != localDescriptor) {
147         ZLOGE("interface token is not equal");
148         return -1;
149     }
150     switch (code) {
151         case COMPLETED: {
152             std::map<std::string, std::vector<uint8_t>> results;
153             if (!ITypesUtil::Unmarshal(data, results)) {
154                 ZLOGE("write descriptor failed");
155                 return -1;
156             }
157             ZLOGI("object start complete");
158             Completed(results);
159             ZLOGI("object end complete");
160             return 0;
161         }
162         default:
163             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
164     }
165 }
166 
Completed(const std::map<std::string,std::vector<uint8_t>> & results)167 void ObjectRetrieveCallbackProxy::Completed(const std::map<std::string, std::vector<uint8_t>> &results)
168 {
169     MessageParcel data;
170     MessageParcel reply;
171     if (!data.WriteInterfaceToken(GetDescriptor())) {
172         ZLOGE("write descriptor failed");
173         return;
174     }
175     if (!ITypesUtil::Marshal(data, results)) {
176         ZLOGE("write descriptor failed");
177         return;
178     }
179     MessageOption mo { MessageOption::TF_SYNC };
180     int error = Remote()->SendRequest(COMPLETED, data, reply, mo);
181     if (error != 0) {
182         ZLOGW("SendRequest failed, error %d", error);
183     }
184 }
185 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)186 int ObjectChangeCallbackStub::OnRemoteRequest(
187     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
188 {
189     ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
190     auto localDescriptor = GetDescriptor();
191     auto remoteDescriptor = data.ReadInterfaceToken();
192     if (remoteDescriptor != localDescriptor) {
193         ZLOGE("interface token is not equal");
194         return -1;
195     }
196     switch (code) {
197         case COMPLETED: {
198             std::map<std::string, std::vector<uint8_t>> results;
199             if (!ITypesUtil::Unmarshal(data, results)) {
200                 ZLOGE("write descriptor failed");
201                 return -1;
202             }
203             Completed(results);
204             return 0;
205         }
206         default:
207             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
208     }
209 }
210 
Completed(const std::map<std::string,std::vector<uint8_t>> & results)211 void ObjectChangeCallbackProxy::Completed(const std::map<std::string, std::vector<uint8_t>> &results)
212 {
213     MessageParcel data;
214     MessageParcel reply;
215     if (!data.WriteInterfaceToken(GetDescriptor())) {
216         ZLOGE("write descriptor failed");
217         return;
218     }
219     if (!ITypesUtil::Marshal(data, results)) {
220         ZLOGE("write descriptor failed");
221         return;
222     }
223     MessageOption mo { MessageOption::TF_SYNC };
224     int error = Remote()->SendRequest(COMPLETED, data, reply, mo);
225     if (error != 0) {
226         ZLOGW("SendRequest failed, error %d", error);
227     }
228 }
229 } // namespace DistributedObject
230 } // namespace OHOS
231