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 #define LOG_TAG "ObjectServiceProxy"
17
18 #include "object_service_proxy.h"
19
20 #include "itypes_util.h"
21 #include "log_print.h"
22
23 namespace OHOS::DistributedObject {
ObjectServiceProxy(const sptr<IRemoteObject> & impl)24 ObjectServiceProxy::ObjectServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IObjectService>(impl)
25 {
26 ZLOGI("init service proxy.");
27 }
28
ObjectStoreSave(const std::string & bundleName,const std::string & sessionId,const std::string & deviceId,const std::map<std::string,std::vector<uint8_t>> & objectData,sptr<IObjectSaveCallback> callback)29 int32_t ObjectServiceProxy::ObjectStoreSave(const std::string &bundleName, const std::string &sessionId,
30 const std::string &deviceId, const std::map<std::string, std::vector<uint8_t>> &objectData,
31 sptr<IObjectSaveCallback> callback)
32 {
33 MessageParcel data;
34 if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
35 ZLOGE("write descriptor failed");
36 return Status::IPC_ERROR;
37 }
38 if (!ITypesUtil::Marshal(data, bundleName, sessionId, deviceId, objectData, callback->AsObject().GetRefPtr())) {
39 ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
40 return Status::IPC_ERROR;
41 }
42 MessageParcel reply;
43 MessageOption mo { MessageOption::TF_SYNC };
44 int32_t error = Remote()->SendRequest(OBJECTSTORE_SAVE, data, reply, mo);
45 if (error != 0) {
46 ZLOGE("SendRequest returned %d", error);
47 return Status::IPC_ERROR;
48 }
49 return static_cast<Status>(reply.ReadInt32());
50 }
51
ObjectStoreRevokeSave(const std::string & bundleName,const std::string & sessionId,sptr<IObjectRevokeSaveCallback> callback)52 int32_t ObjectServiceProxy::ObjectStoreRevokeSave(
53 const std::string &bundleName, const std::string &sessionId, sptr<IObjectRevokeSaveCallback> callback)
54 {
55 MessageParcel data;
56 if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
57 ZLOGE("write descriptor failed");
58 return Status::IPC_ERROR;
59 }
60
61 if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback->AsObject().GetRefPtr())) {
62 ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
63 return Status::IPC_ERROR;
64 }
65
66 MessageParcel reply;
67 MessageOption mo { MessageOption::TF_SYNC };
68 int32_t error = Remote()->SendRequest(OBJECTSTORE_REVOKE_SAVE, data, reply, mo);
69 if (error != 0) {
70 ZLOGE("SendRequest returned %d", error);
71 return Status::IPC_ERROR;
72 }
73 return static_cast<Status>(reply.ReadInt32());
74 }
75
ObjectStoreRetrieve(const std::string & bundleName,const std::string & sessionId,sptr<IObjectRetrieveCallback> callback)76 int32_t ObjectServiceProxy::ObjectStoreRetrieve(
77 const std::string &bundleName, const std::string &sessionId, sptr<IObjectRetrieveCallback> callback)
78 {
79 MessageParcel data;
80 if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
81 ZLOGE("write descriptor failed");
82 return Status::IPC_ERROR;
83 }
84
85 if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback->AsObject().GetRefPtr())) {
86 ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
87 return Status::IPC_ERROR;
88 }
89
90 MessageParcel reply;
91 MessageOption mo { MessageOption::TF_SYNC };
92 int32_t error = Remote()->SendRequest(OBJECTSTORE_RETRIEVE, data, reply, mo);
93 if (error != 0) {
94 ZLOGE("SendRequest returned %d", error);
95 return Status::IPC_ERROR;
96 }
97 return static_cast<Status>(reply.ReadInt32());
98 }
99
RegisterDataObserver(const std::string & bundleName,const std::string & sessionId,sptr<IObjectChangeCallback> callback)100 int32_t ObjectServiceProxy::RegisterDataObserver(const std::string &bundleName,
101 const std::string &sessionId, sptr<IObjectChangeCallback> callback)
102 {
103 MessageParcel data;
104 if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
105 ZLOGE("write descriptor failed");
106 return Status::IPC_ERROR;
107 }
108
109 if (!ITypesUtil::Marshal(data, bundleName, sessionId, callback->AsObject().GetRefPtr())) {
110 ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
111 return Status::IPC_ERROR;
112 }
113
114 MessageParcel reply;
115 MessageOption mo { MessageOption::TF_SYNC };
116 int32_t error = Remote()->SendRequest(OBJECTSTORE_REGISTER_OBSERVER, data, reply, mo);
117 if (error != 0) {
118 ZLOGE("SendRequest returned %d", error);
119 return Status::IPC_ERROR;
120 }
121 return static_cast<Status>(reply.ReadInt32());
122 }
123
UnregisterDataChangeObserver(const std::string & bundleName,const std::string & sessionId)124 int32_t ObjectServiceProxy::UnregisterDataChangeObserver(const std::string &bundleName, const std::string &sessionId)
125 {
126 MessageParcel data;
127 if (!data.WriteInterfaceToken(ObjectServiceProxy::GetDescriptor())) {
128 ZLOGE("write descriptor failed");
129 return Status::IPC_ERROR;
130 }
131
132 if (!ITypesUtil::Marshal(data, bundleName, sessionId)) {
133 ZLOGE("Marshalling failed, bundleName = %{public}s", bundleName.c_str());
134 return Status::IPC_ERROR;
135 }
136
137 MessageParcel reply;
138 MessageOption mo { MessageOption::TF_SYNC };
139 int32_t error = Remote()->SendRequest(OBJECTSTORE_UNREGISTER_OBSERVER, data, reply, mo);
140 if (error != 0) {
141 ZLOGE("SendRequest returned %d", error);
142 return Status::IPC_ERROR;
143 }
144 return static_cast<Status>(reply.ReadInt32());
145 }
146 } // namespace OHOS::DistributedObject
147