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
OnGetSchema(MessageParcel & data,MessageParcel & reply)44 int32_t RdbServiceStub::OnGetSchema(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 = GetSchema(param);
53 if (!ITypesUtil::Marshal(reply, status)) {
54 ZLOGE("Marshal status:0x%{public}x", status);
55 return IPC_STUB_WRITE_PARCEL_ERR;
56 }
57 return RDB_OK;
58 }
59
OnDelete(MessageParcel & data,MessageParcel & reply)60 int32_t RdbServiceStub::OnDelete(MessageParcel &data, MessageParcel &reply)
61 {
62 RdbSyncerParam param;
63 if (!ITypesUtil::Unmarshal(data, param)) {
64 ZLOGE("Unmarshal storeName_:%{public}s", Anonymous::Change(param.storeName_).c_str());
65 return IPC_STUB_INVALID_DATA_ERR;
66 }
67 auto status = Delete(param);
68 if (!ITypesUtil::Marshal(reply, status)) {
69 ZLOGE("Marshal status:0x%{public}x", status);
70 return IPC_STUB_WRITE_PARCEL_ERR;
71 }
72 return RDB_OK;
73 }
74
OnRemoteInitNotifier(MessageParcel & data,MessageParcel & reply)75 int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply)
76 {
77 RdbSyncerParam param;
78 sptr<IRemoteObject> notifier;
79 if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) {
80 ZLOGE("Unmarshal bundleName:%{public}s storeName_:%{public}s notifier is nullptr:%{public}d",
81 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), notifier == nullptr);
82 return IPC_STUB_INVALID_DATA_ERR;
83 }
84 auto status = InitNotifier(param, notifier);
85 if (!ITypesUtil::Marshal(reply, status)) {
86 ZLOGE("Marshal status:0x%{public}x", status);
87 return IPC_STUB_WRITE_PARCEL_ERR;
88 }
89 return RDB_OK;
90 }
91
OnRemoteSetDistributedTables(MessageParcel & data,MessageParcel & reply)92 int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply)
93 {
94 RdbSyncerParam param;
95 std::vector<std::string> tables;
96 std::vector<Reference> references;
97 int32_t type;
98 if (!ITypesUtil::Unmarshal(data, param, tables, references, type)) {
99 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables size:%{public}zu type:%{public}d",
100 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), tables.size(), type);
101 return IPC_STUB_INVALID_DATA_ERR;
102 }
103
104 auto status = SetDistributedTables(param, tables, references, type);
105 if (!ITypesUtil::Marshal(reply, status)) {
106 ZLOGE("Marshal status:0x%{public}x", status);
107 return IPC_STUB_WRITE_PARCEL_ERR;
108 }
109 return RDB_OK;
110 }
111
OnRemoteDoSync(MessageParcel & data,MessageParcel & reply)112 int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply)
113 {
114 RdbSyncerParam param;
115 Option option {};
116 PredicatesMemo predicates;
117 if (!ITypesUtil::Unmarshal(data, param, option, predicates)) {
118 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}zu", param.bundleName_.c_str(),
119 Anonymous::Change(param.storeName_).c_str(), predicates.tables_.size());
120 return IPC_STUB_INVALID_DATA_ERR;
121 }
122
123 Details result = {};
124 auto status = Sync(param, option, predicates, [&result](Details &&details) { result = std::move(details); });
125 if (!ITypesUtil::Marshal(reply, status, result)) {
126 ZLOGE("Marshal status:0x%{public}x result size:%{public}zu", status, result.size());
127 return IPC_STUB_WRITE_PARCEL_ERR;
128 }
129 return RDB_OK;
130 }
131
OnRemoteDoAsync(MessageParcel & data,MessageParcel & reply)132 int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply)
133 {
134 RdbSyncerParam param;
135 Option option {};
136 PredicatesMemo predicates;
137 if (!ITypesUtil::Unmarshal(data, param, option, predicates)) {
138 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s seqNum:%{public}u table:%{public}s",
139 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), option.seqNum,
140 predicates.tables_.empty() ? "null" : predicates.tables_.begin()->c_str());
141 return IPC_STUB_INVALID_DATA_ERR;
142 }
143 auto status = Sync(param, option, predicates, nullptr);
144 if (!ITypesUtil::Marshal(reply, status)) {
145 ZLOGE("Marshal status:0x%{public}x", status);
146 return IPC_STUB_WRITE_PARCEL_ERR;
147 }
148 return RDB_OK;
149 }
150
OnRemoteDoSubscribe(MessageParcel & data,MessageParcel & reply)151 int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply)
152 {
153 RdbSyncerParam param;
154 SubscribeOption option;
155 if (!ITypesUtil::Unmarshal(data, param, option)) {
156 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
157 Anonymous::Change(param.storeName_).c_str());
158 return IPC_STUB_INVALID_DATA_ERR;
159 }
160
161 auto status = Subscribe(param, option, nullptr);
162 if (!ITypesUtil::Marshal(reply, status)) {
163 ZLOGE("Marshal status:0x%{public}x", status);
164 return IPC_STUB_WRITE_PARCEL_ERR;
165 }
166 return RDB_OK;
167 }
168
OnRemoteDoUnSubscribe(MessageParcel & data,MessageParcel & reply)169 int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel &reply)
170 {
171 RdbSyncerParam param;
172 SubscribeOption option;
173 if (!ITypesUtil::Unmarshal(data, param)) {
174 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
175 Anonymous::Change(param.storeName_).c_str());
176 return IPC_STUB_INVALID_DATA_ERR;
177 }
178
179 auto status = UnSubscribe(param, option, nullptr);
180 if (!ITypesUtil::Marshal(reply, status)) {
181 ZLOGE("Marshal status:0x%{public}x", status);
182 return IPC_STUB_WRITE_PARCEL_ERR;
183 }
184 return RDB_OK;
185 }
186
OnRemoteDoRemoteQuery(MessageParcel & data,MessageParcel & reply)187 int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply)
188 {
189 RdbSyncerParam param;
190 std::string device;
191 std::string sql;
192 std::vector<std::string> selectionArgs;
193 if (!ITypesUtil::Unmarshal(data, param, device, sql, selectionArgs)) {
194 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s device:%{public}s sql:%{public}s "
195 "selectionArgs size:%{public}zu", param.bundleName_.c_str(),
196 Anonymous::Change(param.storeName_).c_str(), Anonymous::Change(device).c_str(),
197 Anonymous::Change(sql).c_str(), selectionArgs.size());
198 return IPC_STUB_INVALID_DATA_ERR;
199 }
200
201 auto [status, resultSet] = RemoteQuery(param, device, sql, selectionArgs);
202 sptr<RdbResultSetStub> object = new RdbResultSetStub(resultSet);
203 if (!ITypesUtil::Marshal(reply, status, object->AsObject())) {
204 ZLOGE("Marshal status:0x%{public}x", status);
205 return IPC_STUB_WRITE_PARCEL_ERR;
206 }
207 return RDB_OK;
208 }
209
CheckInterfaceToken(MessageParcel & data)210 bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data)
211 {
212 auto localDescriptor = GetDescriptor();
213 auto remoteDescriptor = data.ReadInterfaceToken();
214 if (remoteDescriptor != localDescriptor) {
215 ZLOGE("interface token is not equal");
216 return false;
217 }
218 return true;
219 }
220
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)221 int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
222 {
223 ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
224 if (!CheckInterfaceToken(data)) {
225 return RDB_ERROR;
226 }
227 if (code >= 0 && code < static_cast<uint32_t>(RdbServiceCode::RDB_SERVICE_CMD_MAX)) {
228 return (this->*HANDLERS[code])(data, reply);
229 }
230 return RDB_ERROR;
231 }
232
OnRemoteRegisterDetailProgressObserver(MessageParcel & data,MessageParcel & reply)233 int32_t RdbServiceStub::OnRemoteRegisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply)
234 {
235 RdbSyncerParam param;
236 if (!ITypesUtil::Unmarshal(data, param)) {
237 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
238 Anonymous::Change(param.storeName_).c_str());
239 return IPC_STUB_INVALID_DATA_ERR;
240 }
241
242 auto status = RegisterAutoSyncCallback(param, nullptr);
243 if (!ITypesUtil::Marshal(reply, status)) {
244 ZLOGE("Marshal status:0x%{public}x", status);
245 return IPC_STUB_WRITE_PARCEL_ERR;
246 }
247 return RDB_OK;
248 }
249
OnRemoteUnregisterDetailProgressObserver(MessageParcel & data,MessageParcel & reply)250 int32_t RdbServiceStub::OnRemoteUnregisterDetailProgressObserver(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 = UnregisterAutoSyncCallback(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
OnRemoteNotifyDataChange(MessageParcel & data,MessageParcel & reply)267 int32_t RdbServiceStub::OnRemoteNotifyDataChange(MessageParcel &data, MessageParcel &reply)
268 {
269 RdbSyncerParam param;
270 RdbChangedData rdbChangedData;
271 if (!ITypesUtil::Unmarshal(data, param, rdbChangedData)) {
272 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s ", param.bundleName_.c_str(),
273 Anonymous::Change(param.storeName_).c_str());
274 return IPC_STUB_INVALID_DATA_ERR;
275 }
276
277 auto status = NotifyDataChange(param, rdbChangedData);
278 if (!ITypesUtil::Marshal(reply, status)) {
279 ZLOGE("Marshal status:0x%{public}x", status);
280 return IPC_STUB_WRITE_PARCEL_ERR;
281 }
282 return RDB_OK;
283 }
284
OnRemoteQuerySharingResource(MessageParcel & data,MessageParcel & reply)285 int32_t RdbServiceStub::OnRemoteQuerySharingResource(MessageParcel& data, MessageParcel& reply)
286 {
287 RdbSyncerParam param;
288 PredicatesMemo predicates;
289 std::vector<std::string> columns;
290 if (!ITypesUtil::Unmarshal(data, param, predicates, columns)) {
291 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
292 Anonymous::Change(param.storeName_).c_str());
293 return IPC_STUB_INVALID_DATA_ERR;
294 }
295
296 auto [status, resultSet] = QuerySharingResource(param, predicates, columns);
297 sptr<RdbResultSetStub> object = new RdbResultSetStub(resultSet);
298 if (!ITypesUtil::Marshal(reply, status, object->AsObject())) {
299 ZLOGE("Marshal status:0x%{public}x", status);
300 return IPC_STUB_WRITE_PARCEL_ERR;
301 }
302 return RDB_OK;
303 }
304 } // namespace OHOS::DistributedRdb
305