1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "ObjectServiceProxy"
17 #include "object_service_proxy.h"
18 
19 #include <logger.h>
20 #include "log_print.h"
21 #include "objectstore_errors.h"
22 #include "object_types_util.h"
23 
24 namespace OHOS::DistributedObject {
25 using namespace ObjectStore;
26 
27 using ObjectCode = ObjectStoreService::ObjectServiceInterfaceCode;
28 
ObjectServiceProxy(const sptr<IRemoteObject> & impl)29 ObjectServiceProxy::ObjectServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IObjectService>(impl)
30 {
31     ZLOGI("init service proxy.");
32 }
33 
ObjectStoreSave(const std::string & bundleName,const std::string & sessionId,const std::string & deviceId,const std::map<std::string,std::vector<uint8_t>> & objectData,sptr<IRemoteObject> callback)34 int32_t ObjectServiceProxy::ObjectStoreSave(const std::string &bundleName, const std::string &sessionId,
35     const std::string &deviceId, const std::map<std::string, std::vector<uint8_t>> &objectData,
36     sptr<IRemoteObject> callback)
37 {
38     MessageParcel data;
39     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
40         ZLOGE("write descriptor failed");
41         return ERR_IPC;
42     }
43     if (!ITypesUtil::Marshal(data, bundleName, sessionId, deviceId, objectData, callback)) {
44         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
45         return ERR_IPC;
46     }
47     MessageParcel reply;
48     MessageOption mo { MessageOption::TF_SYNC };
49     sptr<IRemoteObject> remoteObject = Remote();
50     if (remoteObject == nullptr) {
51         LOG_ERROR("ObjectStoreSave remoteObject is nullptr.");
52         return ERR_IPC;
53     }
54     int32_t error = remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_SAVE), data, reply, mo);
55     if (error != 0) {
56         ZLOGE("SendRequest returned %d", error);
57         return ERR_IPC;
58     }
59     return reply.ReadInt32();
60 }
61 
OnAssetChanged(const std::string & bundleName,const std::string & sessionId,const std::string & deviceId,const Asset & assetValue)62 int32_t ObjectServiceProxy::OnAssetChanged(const std::string &bundleName, const std::string &sessionId,
63     const std::string &deviceId, const Asset &assetValue)
64 {
65     MessageParcel data;
66     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
67         ZLOGE("write descriptor failed");
68         return ERR_IPC;
69     }
70     if (!ITypesUtil::Marshal(data, bundleName, sessionId, deviceId)) {
71         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
72         return ERR_IPC;
73     }
74     if (!ITypesUtil::Marshalling(assetValue, data)) {
75         ZLOGE("Marshalling failed assetValue");
76         return ERR_IPC;
77     }
78     MessageParcel reply;
79     MessageOption mo { MessageOption::TF_SYNC };
80     sptr<IRemoteObject> remoteObject = Remote();
81     if (remoteObject == nullptr) {
82         LOG_ERROR("OnAssetChanged remoteObject is nullptr.");
83         return ERR_IPC;
84     }
85     int32_t error = remoteObject->SendRequest(
86         static_cast<uint32_t>(ObjectCode::OBJECTSTORE_ON_ASSET_CHANGED), data, reply, mo);
87     if (error != 0) {
88         ZLOGE("SendRequest returned %d", error);
89         return ERR_IPC;
90     }
91     return reply.ReadInt32();
92 }
93 
ObjectStoreRevokeSave(const std::string & bundleName,const std::string & sessionId,sptr<IRemoteObject> callback)94 int32_t ObjectServiceProxy::ObjectStoreRevokeSave(
95     const std::string &bundleName, const std::string &sessionId, sptr<IRemoteObject> callback)
96 {
97     MessageParcel data;
98     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
99         ZLOGE("write descriptor failed");
100         return ERR_IPC;
101     }
102 
103     if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback)) {
104         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
105         return ERR_IPC;
106     }
107 
108     MessageParcel reply;
109     MessageOption mo { MessageOption::TF_SYNC };
110     sptr<IRemoteObject> remoteObject = Remote();
111     if (remoteObject == nullptr) {
112         LOG_ERROR("ObjectStoreRevokeSave remoteObject is nullptr.");
113         return ERR_IPC;
114     }
115     int32_t error =
116         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_REVOKE_SAVE), data, reply, mo);
117     if (error != 0) {
118         ZLOGE("SendRequest returned %d", error);
119         return ERR_IPC;
120     }
121     return reply.ReadInt32();
122 }
123 
ObjectStoreRetrieve(const std::string & bundleName,const std::string & sessionId,sptr<IRemoteObject> callback)124 int32_t ObjectServiceProxy::ObjectStoreRetrieve(
125     const std::string &bundleName, const std::string &sessionId, sptr<IRemoteObject> callback)
126 {
127     MessageParcel data;
128     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
129         ZLOGE("write descriptor failed");
130         return ERR_IPC;
131     }
132 
133     if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback)) {
134         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
135         return ERR_IPC;
136     }
137 
138     MessageParcel reply;
139     MessageOption mo { MessageOption::TF_SYNC };
140     sptr<IRemoteObject> remoteObject = Remote();
141     if (remoteObject == nullptr) {
142         LOG_ERROR("ObjectStoreRetrieve remoteObject is nullptr.");
143         return ERR_IPC;
144     }
145     int32_t error =
146         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_RETRIEVE), data, reply, mo);
147     if (error != 0) {
148         ZLOGE("SendRequest returned %d", error);
149         return ERR_IPC;
150     }
151     return reply.ReadInt32();
152 }
153 
RegisterDataObserver(const std::string & bundleName,const std::string & sessionId,sptr<IRemoteObject> callback)154 int32_t ObjectServiceProxy::RegisterDataObserver(const std::string &bundleName,
155                                                  const std::string &sessionId, sptr<IRemoteObject> callback)
156 {
157     MessageParcel data;
158     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
159         ZLOGE("write descriptor failed");
160         return ERR_IPC;
161     }
162 
163     if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback)) {
164         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
165         return ERR_IPC;
166     }
167 
168     MessageParcel reply;
169     MessageOption mo { MessageOption::TF_SYNC };
170     sptr<IRemoteObject> remoteObject = Remote();
171     if (remoteObject == nullptr) {
172         LOG_ERROR("RegisterDataObserver remoteObject is nullptr.");
173         return ERR_IPC;
174     }
175     int32_t error =
176         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_REGISTER_OBSERVER), data, reply, mo);
177     if (error != 0) {
178         ZLOGE("SendRequest returned %d", error);
179         return ERR_IPC;
180     }
181     return reply.ReadInt32();
182 }
183 
UnregisterDataChangeObserver(const std::string & bundleName,const std::string & sessionId)184 int32_t ObjectServiceProxy::UnregisterDataChangeObserver(const std::string &bundleName, const std::string &sessionId)
185 {
186     MessageParcel data;
187     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
188         ZLOGE("write descriptor failed");
189         return ERR_IPC;
190     }
191 
192     if (!ITypesUtil::Marshal(data, bundleName, sessionId)) {
193         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
194         return ERR_IPC;
195     }
196 
197     MessageParcel reply;
198     MessageOption mo { MessageOption::TF_SYNC };
199     sptr<IRemoteObject> remoteObject = Remote();
200     if (remoteObject == nullptr) {
201         LOG_ERROR("UnregisterDataChangeObserver remoteObject is nullptr.");
202         return ERR_IPC;
203     }
204     int32_t error =
205         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_UNREGISTER_OBSERVER), data, reply, mo);
206     if (error != 0) {
207         ZLOGE("SendRequest returned %d", error);
208         return ERR_IPC;
209     }
210     return reply.ReadInt32();
211 }
212 
BindAssetStore(const std::string & bundleName,const std::string & sessionId,Asset & asset,AssetBindInfo & bindInfo)213 int32_t ObjectServiceProxy::BindAssetStore(const std::string &bundleName, const std::string &sessionId,
214     Asset &asset, AssetBindInfo &bindInfo)
215 {
216     MessageParcel data;
217     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
218         ZLOGE("write descriptor failed");
219         return ERR_IPC;
220     }
221 
222     if (!ITypesUtil::Marshal(data, bundleName, sessionId, asset, bindInfo)) {
223         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
224         return ERR_IPC;
225     }
226 
227     MessageParcel reply;
228     MessageOption mo { MessageOption::TF_SYNC };
229     sptr<IRemoteObject> remoteObject = Remote();
230     if (remoteObject == nullptr) {
231         LOG_ERROR("BindAssetStore remoteObject is nullptr.");
232         return ERR_IPC;
233     }
234     int32_t error =
235         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_BIND_ASSET_STORE), data, reply, mo);
236     if (error != 0) {
237         ZLOGE("SendRequest returned %d", error);
238         return ERR_IPC;
239     }
240     return reply.ReadInt32();
241 }
242 
DeleteSnapshot(const std::string & bundleName,const std::string & sessionId)243 int32_t ObjectServiceProxy::DeleteSnapshot(const std::string &bundleName, const std::string &sessionId)
244 {
245     MessageParcel data;
246     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
247         ZLOGE("write descriptor failed");
248         return ERR_IPC;
249     }
250 
251     if (!ITypesUtil::Marshal(data, bundleName, sessionId)) {
252         ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
253         return ERR_IPC;
254     }
255 
256     MessageParcel reply;
257     MessageOption mo { MessageOption::TF_SYNC };
258     sptr<IRemoteObject> remoteObject = Remote();
259     if (remoteObject == nullptr) {
260         LOG_ERROR("DeleteSnapshot remoteObject is nullptr.");
261         return ERR_IPC;
262     }
263     int32_t error =
264         remoteObject->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_DELETE_SNAPSHOT), data, reply, mo);
265     if (error != 0) {
266         ZLOGE("SendRequest returned %d", error);
267         return ERR_IPC;
268     }
269     return reply.ReadInt32();
270 }
271 
IsContinue(bool & result)272 int32_t ObjectServiceProxy::IsContinue(bool &result)
273 {
274     sptr<IRemoteObject> remote = Remote();
275     if (remote == nullptr) {
276         LOG_ERROR("Remote object is nullptr.");
277         return ERR_IPC;
278     }
279     MessageParcel data;
280     MessageParcel reply;
281     MessageOption mo { MessageOption::TF_SYNC };
282     if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
283         ZLOGE("Write descriptor failed");
284         return ERR_IPC;
285     }
286     if (!ITypesUtil::Marshal(data, result)) {
287         ZLOGE("Marshall result failed, result: %{public}d", result);
288         return ERR_IPC;
289     }
290     int32_t status = remote->SendRequest(static_cast<uint32_t>(ObjectCode::OBJECTSTORE_IS_CONTINUE), data, reply, mo);
291     if (status != SUCCESS) {
292         ZLOGE("Send request failed, status: %{public}d", status);
293         return status;
294     }
295     if (!ITypesUtil::Unmarshal(reply, result)) {
296         ZLOGE("Unmarshal result failed, result: %{public}d", result);
297         return ERR_IPC;
298     }
299     return SUCCESS;
300 }
301 } // namespace OHOS::DistributedObject
302