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 "ObjectServiceStub"
17
18 #include "object_service_stub.h"
19
20 #include <ipc_skeleton.h>
21
22 #include "common_types.h"
23 #include "object_types_utils.h"
24 #include "log_print.h"
25 #include "utils/anonymous.h"
26
27 namespace OHOS::DistributedObject {
28 using namespace DistributedKv;
29 using Anonymous = DistributedData::Anonymous;
ObjectStoreSaveOnRemote(MessageParcel & data,MessageParcel & reply)30 int32_t ObjectServiceStub::ObjectStoreSaveOnRemote(MessageParcel &data, MessageParcel &reply)
31 {
32 std::string sessionId;
33 std::string bundleName;
34 std::string deviceId;
35 std::map<std::string, std::vector<uint8_t>> objectData;
36 sptr<IRemoteObject> obj;
37 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId, objectData, obj) || obj == nullptr) {
38 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s objectData size:%{public}zu, "
39 "callback is nullptr:%{public}d",
40 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(),
41 DistributedData::Anonymous::Change(deviceId).c_str(), objectData.size(), obj == nullptr);
42 return IPC_STUB_INVALID_DATA_ERR;
43 }
44 int32_t status = ObjectStoreSave(bundleName, sessionId, deviceId, objectData, obj);
45 if (!ITypesUtil::Marshal(reply, status)) {
46 ZLOGE("Marshal status:0x%{public}x", status);
47 return IPC_STUB_WRITE_PARCEL_ERR;
48 }
49 return 0;
50 }
51
OnAssetChangedOnRemote(MessageParcel & data,MessageParcel & reply)52 int32_t ObjectServiceStub::OnAssetChangedOnRemote(MessageParcel &data, MessageParcel &reply)
53 {
54 std::string sessionId;
55 std::string bundleName;
56 std::string deviceId;
57 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, deviceId)) {
58 ZLOGE("Unmarshal fail sessionId:%{public}s bundleName:%{public}s deviceId:%{public}s",
59 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(),
60 DistributedData::Anonymous::Change(deviceId).c_str());
61 return IPC_STUB_INVALID_DATA_ERR;
62 }
63 ObjectStore::Asset assetValue;
64 if (!ITypesUtil::Unmarshalling(assetValue, data)) {
65 ZLOGE("Unmarshal assetValue fail, asset name: %{public}s", Anonymous::Change(assetValue.name).c_str());
66 return IPC_STUB_INVALID_DATA_ERR;
67 }
68 int32_t status = OnAssetChanged(bundleName, sessionId, deviceId, assetValue);
69 if (!ITypesUtil::Marshal(reply, status)) {
70 ZLOGE("Marshal status:0x%{public}x", status);
71 return IPC_STUB_WRITE_PARCEL_ERR;
72 }
73 return status;
74 }
75
ObjectStoreBindAssetOnRemote(MessageParcel & data,MessageParcel & reply)76 int32_t ObjectServiceStub::ObjectStoreBindAssetOnRemote(MessageParcel &data, MessageParcel &reply)
77 {
78 std::string sessionId;
79 std::string bundleName;
80 ObjectStore::Asset asset;
81 ObjectStore::AssetBindInfo bindInfo;
82 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, asset, bindInfo)) {
83 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s assetName:%{public}s bindStore:%{public}s",
84 Anonymous::Change(sessionId).c_str(), bundleName.c_str(), Anonymous::Change(asset.name).c_str(),
85 Anonymous::Change(bindInfo.storeName).c_str());
86 return IPC_STUB_INVALID_DATA_ERR;
87 }
88
89 int32_t status = BindAssetStore(bundleName, sessionId, asset, bindInfo);
90 if (!ITypesUtil::Marshal(reply, status)) {
91 ZLOGE("Marshal status:0x%{public}x", status);
92 return IPC_STUB_WRITE_PARCEL_ERR;
93 }
94 return 0;
95 }
96
ObjectStoreRevokeSaveOnRemote(MessageParcel & data,MessageParcel & reply)97 int32_t ObjectServiceStub::ObjectStoreRevokeSaveOnRemote(MessageParcel &data, MessageParcel &reply)
98 {
99 std::string sessionId;
100 std::string bundleName;
101 sptr<IRemoteObject> obj;
102 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
103 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
104 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
105 return IPC_STUB_INVALID_DATA_ERR;
106 }
107 int32_t status = ObjectStoreRevokeSave(bundleName, sessionId, obj);
108 if (!ITypesUtil::Marshal(reply, status)) {
109 ZLOGE("Marshal status:0x%{public}x", status);
110 return IPC_STUB_WRITE_PARCEL_ERR;
111 }
112 return 0;
113 }
114
ObjectStoreRetrieveOnRemote(MessageParcel & data,MessageParcel & reply)115 int32_t ObjectServiceStub::ObjectStoreRetrieveOnRemote(MessageParcel &data, MessageParcel &reply)
116 {
117 std::string sessionId;
118 std::string bundleName;
119 sptr<IRemoteObject> obj;
120 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
121 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
122 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
123 return IPC_STUB_INVALID_DATA_ERR;
124 }
125 int32_t status = ObjectStoreRetrieve(bundleName, sessionId, obj);
126 if (!ITypesUtil::Marshal(reply, status)) {
127 ZLOGE("Marshal status:0x%{public}x", status);
128 return IPC_STUB_WRITE_PARCEL_ERR;
129 }
130 return 0;
131 }
132
OnSubscribeRequest(MessageParcel & data,MessageParcel & reply)133 int32_t ObjectServiceStub::OnSubscribeRequest(MessageParcel &data, MessageParcel &reply)
134 {
135 std::string sessionId;
136 std::string bundleName;
137 sptr<IRemoteObject> obj;
138 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
139 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
140 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
141 return IPC_STUB_INVALID_DATA_ERR;
142 }
143 int32_t status = RegisterDataObserver(bundleName, sessionId, obj);
144 if (!ITypesUtil::Marshal(reply, status)) {
145 ZLOGE("Marshal status:0x%{public}x", status);
146 return IPC_STUB_WRITE_PARCEL_ERR;
147 }
148 return 0;
149 }
150
OnUnsubscribeRequest(MessageParcel & data,MessageParcel & reply)151 int32_t ObjectServiceStub::OnUnsubscribeRequest(MessageParcel &data, MessageParcel &reply)
152 {
153 std::string sessionId;
154 std::string bundleName;
155 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) {
156 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
157 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
158 return IPC_STUB_INVALID_DATA_ERR;
159 }
160 int32_t status = UnregisterDataChangeObserver(bundleName, sessionId);
161 if (!ITypesUtil::Marshal(reply, status)) {
162 ZLOGE("Marshal status:0x%{public}x", status);
163 return IPC_STUB_WRITE_PARCEL_ERR;
164 }
165 return 0;
166 }
167
OnSubscribeProgress(MessageParcel & data,MessageParcel & reply)168 int32_t ObjectServiceStub::OnSubscribeProgress(MessageParcel &data, MessageParcel &reply)
169 {
170 std::string sessionId;
171 std::string bundleName;
172 sptr<IRemoteObject> obj;
173 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId, obj) || obj == nullptr) {
174 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s, callback is nullptr: %{public}d",
175 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str(), obj == nullptr);
176 return IPC_STUB_INVALID_DATA_ERR;
177 }
178 int32_t status = RegisterProgressObserver(bundleName, sessionId, obj);
179 if (!ITypesUtil::Marshal(reply, status)) {
180 ZLOGE("Marshal status:0x%{public}x", status);
181 return IPC_STUB_WRITE_PARCEL_ERR;
182 }
183 return status;
184 }
185
OnUnsubscribeProgress(MessageParcel & data,MessageParcel & reply)186 int32_t ObjectServiceStub::OnUnsubscribeProgress(MessageParcel &data, MessageParcel &reply)
187 {
188 std::string sessionId;
189 std::string bundleName;
190 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) {
191 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
192 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
193 return IPC_STUB_INVALID_DATA_ERR;
194 }
195 int32_t status = UnregisterProgressObserver(bundleName, sessionId);
196 if (!ITypesUtil::Marshal(reply, status)) {
197 ZLOGE("Marshal status:0x%{public}x", status);
198 return IPC_STUB_WRITE_PARCEL_ERR;
199 }
200 return status;
201 }
202
OnDeleteSnapshot(MessageParcel & data,MessageParcel & reply)203 int32_t ObjectServiceStub::OnDeleteSnapshot(MessageParcel &data, MessageParcel &reply)
204 {
205 std::string sessionId;
206 std::string bundleName;
207 if (!ITypesUtil::Unmarshal(data, bundleName, sessionId)) {
208 ZLOGE("Unmarshal sessionId:%{public}s bundleName:%{public}s",
209 DistributedData::Anonymous::Change(sessionId).c_str(), bundleName.c_str());
210 return IPC_STUB_INVALID_DATA_ERR;
211 }
212 int32_t status = DeleteSnapshot(bundleName, sessionId);
213 if (!ITypesUtil::Marshal(reply, status)) {
214 ZLOGE("Marshal status:0x%{public}x", status);
215 return IPC_STUB_WRITE_PARCEL_ERR;
216 }
217 return 0;
218 }
219
OnIsContinue(MessageParcel & data,MessageParcel & reply)220 int32_t ObjectServiceStub::OnIsContinue(MessageParcel &data, MessageParcel &reply)
221 {
222 bool isContinue = false;
223 int32_t status = IsContinue(isContinue);
224 if (!ITypesUtil::Marshal(reply, isContinue)) {
225 ZLOGE("Marshal isContinue failed, isContinue: %{public}d, status: %{public}d", isContinue, status);
226 return -1;
227 }
228 return 0;
229 }
230
CheckInterfaceToken(MessageParcel & data)231 bool ObjectServiceStub::CheckInterfaceToken(MessageParcel& data)
232 {
233 auto localDescriptor = IObjectService::GetDescriptor();
234 auto remoteDescriptor = data.ReadInterfaceToken();
235 if (remoteDescriptor != localDescriptor) {
236 ZLOGE("interface token is not equal");
237 return false;
238 }
239 return true;
240 }
241
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)242 int ObjectServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
243 {
244 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
245 if (!CheckInterfaceToken(data)) {
246 return -1;
247 }
248 if (code >= 0 && code < static_cast<uint32_t>(ObjectCode::OBJECTSTORE_SERVICE_CMD_MAX)) {
249 return (this->*HANDLERS[code])(data, reply);
250 }
251 return -1;
252 }
253 } // namespace OHOS::DistributedRdb
254