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 "RdbServiceStub"
17
18 #include "rdb_service_stub.h"
19
20 #include <ipc_skeleton.h>
21
22 #include "itypes_util.h"
23 #include "log_print.h"
24 #include "rdb_result_set_stub.h"
25 #include "utils/anonymous.h"
26
27 namespace OHOS::DistributedRdb {
28 using Anonymous = DistributedData::Anonymous;
OnRemoteObtainDistributedTableName(MessageParcel &data, MessageParcel &reply)29 int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, MessageParcel &reply)
30 {
31 std::string device;
32 std::string table;
33 if (!ITypesUtil::Unmarshal(data, device, table)) {
34 ZLOGE("Unmarshal device:%{public}s table:%{public}s", Anonymous::Change(device).c_str(), table.c_str());
35 return IPC_STUB_INVALID_DATA_ERR;
36 }
37
38 std::string distributedTableName = ObtainDistributedTableName(device, table);
39 if (!ITypesUtil::Marshal(reply, distributedTableName)) {
40 ZLOGE("Marshal distributedTableName:%{public}s", distributedTableName.c_str());
41 return IPC_STUB_WRITE_PARCEL_ERR;
42 }
43 return RDB_OK;
44 }
45
OnBeforeOpen(MessageParcel &data, MessageParcel &reply)46 int32_t RdbServiceStub::OnBeforeOpen(MessageParcel &data, MessageParcel &reply)
47 {
48 RdbSyncerParam param;
49 if (!ITypesUtil::Unmarshal(data, param)) {
50 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
51 Anonymous::Change(param.storeName_).c_str());
52 return IPC_STUB_INVALID_DATA_ERR;
53 }
54 auto status = BeforeOpen(param);
55 if (!ITypesUtil::Marshal(reply, status, param)) {
56 ZLOGE("Marshal status:0x%{public}x", status);
57 return IPC_STUB_WRITE_PARCEL_ERR;
58 }
59 return RDB_OK;
60 }
61
OnAfterOpen(MessageParcel &data, MessageParcel &reply)62 int32_t RdbServiceStub::OnAfterOpen(MessageParcel &data, MessageParcel &reply)
63 {
64 RdbSyncerParam param;
65 if (!ITypesUtil::Unmarshal(data, param)) {
66 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
67 Anonymous::Change(param.storeName_).c_str());
68 return IPC_STUB_INVALID_DATA_ERR;
69 }
70 auto status = AfterOpen(param);
71 if (!ITypesUtil::Marshal(reply, status)) {
72 ZLOGE("Marshal status:0x%{public}x", status);
73 return IPC_STUB_WRITE_PARCEL_ERR;
74 }
75 return RDB_OK;
76 }
77
OnDelete(MessageParcel &data, MessageParcel &reply)78 int32_t RdbServiceStub::OnDelete(MessageParcel &data, MessageParcel &reply)
79 {
80 RdbSyncerParam param;
81 if (!ITypesUtil::Unmarshal(data, param)) {
82 ZLOGE("Unmarshal storeName_:%{public}s", Anonymous::Change(param.storeName_).c_str());
83 return IPC_STUB_INVALID_DATA_ERR;
84 }
85 auto status = Delete(param);
86 if (!ITypesUtil::Marshal(reply, status)) {
87 ZLOGE("Marshal status:0x%{public}x", status);
88 return IPC_STUB_WRITE_PARCEL_ERR;
89 }
90 return RDB_OK;
91 }
92
OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply)93 int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply)
94 {
95 RdbSyncerParam param;
96 sptr<IRemoteObject> notifier;
97 if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) {
98 ZLOGE("Unmarshal bundleName:%{public}s storeName_:%{public}s notifier is nullptr:%{public}d",
99 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), notifier == nullptr);
100 return IPC_STUB_INVALID_DATA_ERR;
101 }
102 auto status = InitNotifier(param, notifier);
103 if (!ITypesUtil::Marshal(reply, status)) {
104 ZLOGE("Marshal status:0x%{public}x", status);
105 return IPC_STUB_WRITE_PARCEL_ERR;
106 }
107 return RDB_OK;
108 }
109
OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply)110 int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply)
111 {
112 RdbSyncerParam param;
113 std::vector<std::string> tables;
114 std::vector<Reference> references;
115 int32_t type;
116 bool isRebuild;
117 if (!ITypesUtil::Unmarshal(data, param, tables, references, type, isRebuild)) {
118 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables size:%{public}zu type:%{public}d",
119 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), tables.size(), type);
120 return IPC_STUB_INVALID_DATA_ERR;
121 }
122
123 auto status = SetDistributedTables(param, tables, references, isRebuild, type);
124 if (!ITypesUtil::Marshal(reply, status)) {
125 ZLOGE("Marshal status:0x%{public}x", status);
126 return IPC_STUB_WRITE_PARCEL_ERR;
127 }
128 return RDB_OK;
129 }
130
OnRemoteDoSync(MessageParcel &data, MessageParcel &reply)131 int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply)
132 {
133 RdbSyncerParam param;
134 Option option {};
135 PredicatesMemo predicates;
136 if (!ITypesUtil::Unmarshal(data, param, option, predicates)) {
137 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}zu", param.bundleName_.c_str(),
138 Anonymous::Change(param.storeName_).c_str(), predicates.tables_.size());
139 return IPC_STUB_INVALID_DATA_ERR;
140 }
141
142 Details result = {};
143 auto status = Sync(param, option, predicates, [&result](Details &&details) { result = std::move(details); });
144 if (!ITypesUtil::Marshal(reply, status, result)) {
145 ZLOGE("Marshal status:0x%{public}x result size:%{public}zu", status, result.size());
146 return IPC_STUB_WRITE_PARCEL_ERR;
147 }
148 return RDB_OK;
149 }
150
OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply)151 int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply)
152 {
153 RdbSyncerParam param;
154 Option option {};
155 PredicatesMemo predicates;
156 if (!ITypesUtil::Unmarshal(data, param, option, predicates)) {
157 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s seqNum:%{public}u table:%{public}s",
158 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), option.seqNum,
159 predicates.tables_.empty() ? "null" : predicates.tables_.begin()->c_str());
160 return IPC_STUB_INVALID_DATA_ERR;
161 }
162 auto status = Sync(param, option, predicates, nullptr);
163 if (!ITypesUtil::Marshal(reply, status)) {
164 ZLOGE("Marshal status:0x%{public}x", status);
165 return IPC_STUB_WRITE_PARCEL_ERR;
166 }
167 return RDB_OK;
168 }
169
OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply)170 int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply)
171 {
172 RdbSyncerParam param;
173 SubscribeOption option;
174 if (!ITypesUtil::Unmarshal(data, param, option)) {
175 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
176 Anonymous::Change(param.storeName_).c_str());
177 return IPC_STUB_INVALID_DATA_ERR;
178 }
179
180 auto status = Subscribe(param, option, nullptr);
181 if (!ITypesUtil::Marshal(reply, status)) {
182 ZLOGE("Marshal status:0x%{public}x", status);
183 return IPC_STUB_WRITE_PARCEL_ERR;
184 }
185 return RDB_OK;
186 }
187
OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel &reply)188 int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel &reply)
189 {
190 RdbSyncerParam param;
191 SubscribeOption option;
192 if (!ITypesUtil::Unmarshal(data, param, option)) {
193 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
194 Anonymous::Change(param.storeName_).c_str());
195 return IPC_STUB_INVALID_DATA_ERR;
196 }
197
198 auto status = UnSubscribe(param, option, nullptr);
199 if (!ITypesUtil::Marshal(reply, status)) {
200 ZLOGE("Marshal status:0x%{public}x", status);
201 return IPC_STUB_WRITE_PARCEL_ERR;
202 }
203 return RDB_OK;
204 }
205
OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply)206 int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply)
207 {
208 RdbSyncerParam param;
209 std::string device;
210 std::string sql;
211 std::vector<std::string> selectionArgs;
212 if (!ITypesUtil::Unmarshal(data, param, device, sql, selectionArgs)) {
213 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s device:%{public}s sql:%{public}s "
214 "selectionArgs size:%{public}zu", param.bundleName_.c_str(),
215 Anonymous::Change(param.storeName_).c_str(), Anonymous::Change(device).c_str(),
216 Anonymous::Change(sql).c_str(), selectionArgs.size());
217 return IPC_STUB_INVALID_DATA_ERR;
218 }
219
220 auto [status, resultSet] = RemoteQuery(param, device, sql, selectionArgs);
221 sptr<RdbResultSetStub> object = new RdbResultSetStub(resultSet);
222 if (!ITypesUtil::Marshal(reply, status, object->AsObject())) {
223 ZLOGE("Marshal status:0x%{public}x", status);
224 return IPC_STUB_WRITE_PARCEL_ERR;
225 }
226 return RDB_OK;
227 }
228
CheckInterfaceToken(MessageParcel& data)229 bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data)
230 {
231 auto localDescriptor = GetDescriptor();
232 auto remoteDescriptor = data.ReadInterfaceToken();
233 if (remoteDescriptor != localDescriptor) {
234 ZLOGE("interface token is not equal");
235 return false;
236 }
237 return true;
238 }
239
OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)240 int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
241 {
242 ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
243 if (!CheckInterfaceToken(data)) {
244 return RDB_ERROR;
245 }
246 if (code >= 0 && code < static_cast<uint32_t>(RdbServiceCode::RDB_SERVICE_CMD_MAX)) {
247 return (this->*HANDLERS[code])(data, reply);
248 }
249 return RDB_ERROR;
250 }
251
OnRemoteRegisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply)252 int32_t RdbServiceStub::OnRemoteRegisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply)
253 {
254 RdbSyncerParam param;
255 if (!ITypesUtil::Unmarshal(data, param)) {
256 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
257 Anonymous::Change(param.storeName_).c_str());
258 return IPC_STUB_INVALID_DATA_ERR;
259 }
260
261 auto status = RegisterAutoSyncCallback(param, nullptr);
262 if (!ITypesUtil::Marshal(reply, status)) {
263 ZLOGE("Marshal status:0x%{public}x", status);
264 return IPC_STUB_WRITE_PARCEL_ERR;
265 }
266 return RDB_OK;
267 }
268
OnRemoteUnregisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply)269 int32_t RdbServiceStub::OnRemoteUnregisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply)
270 {
271 RdbSyncerParam param;
272 if (!ITypesUtil::Unmarshal(data, param)) {
273 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
274 Anonymous::Change(param.storeName_).c_str());
275 return IPC_STUB_INVALID_DATA_ERR;
276 }
277
278 auto status = UnregisterAutoSyncCallback(param, nullptr);
279 if (!ITypesUtil::Marshal(reply, status)) {
280 ZLOGE("Marshal status:0x%{public}x", status);
281 return IPC_STUB_WRITE_PARCEL_ERR;
282 }
283 return RDB_OK;
284 }
285
OnRemoteNotifyDataChange(MessageParcel &data, MessageParcel &reply)286 int32_t RdbServiceStub::OnRemoteNotifyDataChange(MessageParcel &data, MessageParcel &reply)
287 {
288 RdbSyncerParam param;
289 RdbChangedData rdbChangedData;
290 RdbNotifyConfig rdbNotifyConfig;
291 if (!ITypesUtil::Unmarshal(data, param, rdbChangedData, rdbNotifyConfig)) {
292 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s ", param.bundleName_.c_str(),
293 Anonymous::Change(param.storeName_).c_str());
294 return IPC_STUB_INVALID_DATA_ERR;
295 }
296
297 auto status = NotifyDataChange(param, rdbChangedData, rdbNotifyConfig);
298 if (!ITypesUtil::Marshal(reply, status)) {
299 ZLOGE("Marshal status:0x%{public}x", status);
300 return IPC_STUB_WRITE_PARCEL_ERR;
301 }
302 return RDB_OK;
303 }
304
OnRemoteSetSearchable(MessageParcel &data, MessageParcel &reply)305 int32_t RdbServiceStub::OnRemoteSetSearchable(MessageParcel &data, MessageParcel &reply)
306 {
307 RdbSyncerParam param;
308 bool isSearchable = true;
309 if (!ITypesUtil::Unmarshal(data, param, isSearchable)) {
310 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s ", param.bundleName_.c_str(),
311 Anonymous::Change(param.storeName_).c_str());
312 return IPC_STUB_INVALID_DATA_ERR;
313 }
314
315 auto status = SetSearchable(param, isSearchable);
316 if (!ITypesUtil::Marshal(reply, status)) {
317 ZLOGE("Marshal status:0x%{public}x", status);
318 return IPC_STUB_WRITE_PARCEL_ERR;
319 }
320 return RDB_OK;
321 }
322
OnRemoteQuerySharingResource(MessageParcel& data, MessageParcel& reply)323 int32_t RdbServiceStub::OnRemoteQuerySharingResource(MessageParcel& data, MessageParcel& reply)
324 {
325 RdbSyncerParam param;
326 PredicatesMemo predicates;
327 std::vector<std::string> columns;
328 if (!ITypesUtil::Unmarshal(data, param, predicates, columns)) {
329 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
330 Anonymous::Change(param.storeName_).c_str());
331 return IPC_STUB_INVALID_DATA_ERR;
332 }
333
334 auto [status, resultSet] = QuerySharingResource(param, predicates, columns);
335 sptr<RdbResultSetStub> object = new RdbResultSetStub(resultSet);
336 if (!ITypesUtil::Marshal(reply, status, object->AsObject())) {
337 ZLOGE("Marshal status:0x%{public}x", status);
338 return IPC_STUB_WRITE_PARCEL_ERR;
339 }
340 return RDB_OK;
341 }
342
OnDisable(MessageParcel& data, MessageParcel& reply)343 int32_t RdbServiceStub::OnDisable(MessageParcel& data, MessageParcel& reply)
344 {
345 RdbSyncerParam param;
346 if (!ITypesUtil::Unmarshal(data, param)) {
347 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
348 Anonymous::Change(param.storeName_).c_str());
349 return IPC_STUB_INVALID_DATA_ERR;
350 }
351
352 auto status = Disable(param);
353 if (!ITypesUtil::Marshal(reply, status)) {
354 ZLOGE("Marshal status:0x%{public}x", status);
355 return IPC_STUB_WRITE_PARCEL_ERR;
356 }
357 return RDB_OK;
358 }
359
OnEnable(MessageParcel& data, MessageParcel& reply)360 int32_t RdbServiceStub::OnEnable(MessageParcel& data, MessageParcel& reply)
361 {
362 RdbSyncerParam param;
363 if (!ITypesUtil::Unmarshal(data, param)) {
364 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
365 Anonymous::Change(param.storeName_).c_str());
366 return IPC_STUB_INVALID_DATA_ERR;
367 }
368
369 auto status = Enable(param);
370 if (!ITypesUtil::Marshal(reply, status)) {
371 ZLOGE("Marshal status:0x%{public}x", status);
372 return IPC_STUB_WRITE_PARCEL_ERR;
373 }
374 return RDB_OK;
375 }
376
OnGetPassword(MessageParcel &data, MessageParcel &reply)377 int32_t RdbServiceStub::OnGetPassword(MessageParcel &data, MessageParcel &reply)
378 {
379 RdbSyncerParam param;
380 if (!ITypesUtil::Unmarshal(data, param)) {
381 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
382 Anonymous::Change(param.storeName_).c_str());
383 return IPC_STUB_INVALID_DATA_ERR;
384 }
385
386 std::vector<uint8_t> key;
387 auto status = GetPassword(param, key);
388 if (!ITypesUtil::Marshal(reply, status, key)) {
389 key.assign(key.size(), 0);
390 ZLOGE("Marshal status:0x%{public}x", status);
391 return IPC_STUB_WRITE_PARCEL_ERR;
392 }
393 key.assign(key.size(), 0);
394 return RDB_OK;
395 }
396
OnLockCloudContainer(MessageParcel &data, MessageParcel &reply)397 int32_t RdbServiceStub::OnLockCloudContainer(MessageParcel &data, MessageParcel &reply)
398 {
399 RdbSyncerParam param;
400 uint32_t expiredTime = 0;
401 if (!ITypesUtil::Unmarshal(data, param, expiredTime)) {
402 ZLOGE("Unmarshal failed");
403 return IPC_STUB_INVALID_DATA_ERR;
404 }
405
406 auto result = LockCloudContainer(param);
407 return ITypesUtil::Marshal(reply, result.first, result.second) ? RDB_OK : IPC_STUB_WRITE_PARCEL_ERR;
408 }
409
OnUnlockCloudContainer(MessageParcel &data, MessageParcel &reply)410 int32_t RdbServiceStub::OnUnlockCloudContainer(MessageParcel &data, MessageParcel &reply)
411 {
412 RdbSyncerParam param;
413 if (!ITypesUtil::Unmarshal(data, param)) {
414 ZLOGE("Unmarshal failed");
415 return IPC_STUB_INVALID_DATA_ERR;
416 }
417
418 auto status = UnlockCloudContainer(param);
419 if (!ITypesUtil::Marshal(reply, status)) {
420 ZLOGE("Marshal status:0x%{public}x", status);
421 return IPC_STUB_WRITE_PARCEL_ERR;
422 }
423 return RDB_OK;
424 }
425
OnGetDebugInfo(MessageParcel &data, MessageParcel &reply)426 int32_t RdbServiceStub::OnGetDebugInfo(MessageParcel &data, MessageParcel &reply)
427 {
428 RdbSyncerParam param;
429 if (!ITypesUtil::Unmarshal(data, param)) {
430 ZLOGE("Unmarshal failed");
431 return IPC_STUB_INVALID_DATA_ERR;
432 }
433 std::map<std::string, RdbDebugInfo> debugInfo;
434 auto status = GetDebugInfo(param, debugInfo);
435 if (!ITypesUtil::Marshal(reply, status, debugInfo)) {
436 ZLOGE("Marshal status:0x%{public}x", status);
437 return IPC_STUB_WRITE_PARCEL_ERR;
438 }
439 return RDB_OK;
440 }
441 } // namespace OHOS::DistributedRdb
442