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