1 /*
2 * Copyright (c) 2023 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 "DataShareServiceStub"
17
18 #include "data_share_service_stub.h"
19
20 #include <cinttypes>
21 #include "data_share_obs_proxy.h"
22 #include "ipc_skeleton.h"
23 #include "ishared_result_set.h"
24 #include "itypes_util.h"
25 #include "log_print.h"
26 #include "utils/anonymous.h"
27
28 namespace OHOS {
29 namespace DataShare {
CheckInterfaceToken(MessageParcel & data)30 bool DataShareServiceStub::CheckInterfaceToken(MessageParcel &data)
31 {
32 auto localDescriptor = IDataShareService::GetDescriptor();
33 auto remoteDescriptor = data.ReadInterfaceToken();
34 if (remoteDescriptor != localDescriptor) {
35 ZLOGE("interface token is not equal");
36 return false;
37 }
38 return true;
39 }
40
OnInsert(MessageParcel & data,MessageParcel & reply)41 int32_t DataShareServiceStub::OnInsert(MessageParcel &data, MessageParcel &reply)
42 {
43 std::string uri;
44 DataShareValuesBucket bucket;
45 if (!ITypesUtil::Unmarshal(data, uri, bucket.valuesMap)) {
46 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
47 bucket.valuesMap.size());
48 return IPC_STUB_INVALID_DATA_ERR;
49 }
50 int32_t status = Insert(uri, bucket);
51 if (!ITypesUtil::Marshal(reply, status)) {
52 ZLOGE("Marshal status:0x%{public}x", status);
53 return IPC_STUB_WRITE_PARCEL_ERR;
54 }
55 return 0;
56 }
57
OnUpdate(MessageParcel & data,MessageParcel & reply)58 int32_t DataShareServiceStub::OnUpdate(MessageParcel &data, MessageParcel &reply)
59 {
60 std::string uri;
61 DataSharePredicates predicate;
62 DataShareValuesBucket bucket;
63 if (!ITypesUtil::Unmarshal(data, uri, predicate, bucket.valuesMap)) {
64 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
65 bucket.valuesMap.size());
66 return IPC_STUB_INVALID_DATA_ERR;
67 }
68 int32_t status = Update(uri, predicate, bucket);
69 if (!ITypesUtil::Marshal(reply, status)) {
70 ZLOGE("Marshal status:0x%{public}x", status);
71 return IPC_STUB_WRITE_PARCEL_ERR;
72 }
73 return 0;
74 }
75
OnDelete(MessageParcel & data,MessageParcel & reply)76 int32_t DataShareServiceStub::OnDelete(MessageParcel &data, MessageParcel &reply)
77 {
78 std::string uri;
79 DataSharePredicates predicate;
80 if (!ITypesUtil::Unmarshal(data, uri, predicate)) {
81 ZLOGE("Unmarshal uri:%{public}s", DistributedData::Anonymous::Change(uri).c_str());
82 return IPC_STUB_INVALID_DATA_ERR;
83 }
84 int32_t status = Delete(uri, predicate);
85 if (!ITypesUtil::Marshal(reply, status)) {
86 ZLOGE("Marshal status:0x%{public}x", status);
87 return IPC_STUB_WRITE_PARCEL_ERR;
88 }
89 return 0;
90 }
91
OnInsertEx(MessageParcel & data,MessageParcel & reply)92 int32_t DataShareServiceStub::OnInsertEx(MessageParcel &data, MessageParcel &reply)
93 {
94 std::string uri;
95 std::string extUri;
96 DataShareValuesBucket bucket;
97 if (!ITypesUtil::Unmarshal(data, uri, extUri, bucket.valuesMap)) {
98 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
99 bucket.valuesMap.size());
100 return IPC_STUB_INVALID_DATA_ERR;
101 }
102 auto [errCode, status] = InsertEx(uri, extUri, bucket);
103 if (!ITypesUtil::Marshal(reply, errCode, status)) {
104 ZLOGE("Marshal errCode: 0x%{public}x, status: 0x%{public}x", errCode, status);
105 return IPC_STUB_WRITE_PARCEL_ERR;
106 }
107 return 0;
108 }
109
OnUpdateEx(MessageParcel & data,MessageParcel & reply)110 int32_t DataShareServiceStub::OnUpdateEx(MessageParcel &data, MessageParcel &reply)
111 {
112 std::string uri;
113 std::string extUri;
114 DataSharePredicates predicate;
115 DataShareValuesBucket bucket;
116 if (!ITypesUtil::Unmarshal(data, uri, extUri, predicate, bucket.valuesMap)) {
117 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
118 bucket.valuesMap.size());
119 return IPC_STUB_INVALID_DATA_ERR;
120 }
121 auto [errCode, status] = UpdateEx(uri, extUri, predicate, bucket);
122 if (!ITypesUtil::Marshal(reply, errCode, status)) {
123 ZLOGE("Marshal errCode: 0x%{public}x, status: 0x%{public}x", errCode, status);
124 return IPC_STUB_WRITE_PARCEL_ERR;
125 }
126 return 0;
127 }
128
OnDeleteEx(MessageParcel & data,MessageParcel & reply)129 int32_t DataShareServiceStub::OnDeleteEx(MessageParcel &data, MessageParcel &reply)
130 {
131 std::string uri;
132 std::string extUri;
133 DataSharePredicates predicate;
134 if (!ITypesUtil::Unmarshal(data, uri, extUri, predicate)) {
135 ZLOGE("Unmarshal uri:%{public}s", DistributedData::Anonymous::Change(uri).c_str());
136 return IPC_STUB_INVALID_DATA_ERR;
137 }
138 auto [errCode, status] = DeleteEx(uri, extUri, predicate);
139 if (!ITypesUtil::Marshal(reply, errCode, status)) {
140 ZLOGE("Marshal errCode: 0x%{public}x, status: 0x%{public}x", errCode, status);
141 return IPC_STUB_WRITE_PARCEL_ERR;
142 }
143 return 0;
144 }
145
OnQuery(MessageParcel & data,MessageParcel & reply)146 int32_t DataShareServiceStub::OnQuery(MessageParcel &data, MessageParcel &reply)
147 {
148 std::string uri;
149 std::string extUri;
150 DataSharePredicates predicate;
151 std::vector<std::string> columns;
152 if (!ITypesUtil::Unmarshal(data, uri, extUri, predicate, columns)) {
153 ZLOGE("Unmarshal uri:%{public}s columns size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
154 columns.size());
155 return IPC_STUB_INVALID_DATA_ERR;
156 }
157 int status = 0;
158 auto result = ISharedResultSet::WriteToParcel(Query(uri, extUri, predicate, columns, status), reply);
159 if (!ITypesUtil::Marshal(reply, status)) {
160 ZLOGE("Marshal status:0x%{public}x", status);
161 return IPC_STUB_WRITE_PARCEL_ERR;
162 }
163 return 0;
164 }
165
OnAddTemplate(MessageParcel & data,MessageParcel & reply)166 int32_t DataShareServiceStub::OnAddTemplate(MessageParcel &data, MessageParcel &reply)
167 {
168 std::string uri;
169 int64_t subscriberId;
170 Template tpl;
171 if (!ITypesUtil::Unmarshal(data, uri, subscriberId, tpl.predicates_, tpl.scheduler_)) {
172 ZLOGW("read device list failed.");
173 return -1;
174 }
175 int32_t status = AddTemplate(uri, subscriberId, tpl);
176 if (!ITypesUtil::Marshal(reply, status)) {
177 ZLOGE("Marshal status:0x%{public}x", status);
178 return IPC_STUB_WRITE_PARCEL_ERR;
179 }
180 return 0;
181 }
182
OnDelTemplate(MessageParcel & data,MessageParcel & reply)183 int32_t DataShareServiceStub::OnDelTemplate(MessageParcel &data, MessageParcel &reply)
184 {
185 std::string uri;
186 int64_t subscriberId;
187 if (!ITypesUtil::Unmarshal(data, uri, subscriberId)) {
188 ZLOGW("read device list failed.");
189 return -1;
190 }
191 int32_t status = DelTemplate(uri, subscriberId);
192 if (!ITypesUtil::Marshal(reply, status)) {
193 ZLOGE("Marshal status:0x%{public}x", status);
194 return IPC_STUB_WRITE_PARCEL_ERR;
195 }
196 return 0;
197 }
198
OnPublish(MessageParcel & data,MessageParcel & reply)199 int32_t DataShareServiceStub::OnPublish(MessageParcel &data, MessageParcel &reply)
200 {
201 Data publishData;
202 std::string bundleName;
203 if (!ITypesUtil::Unmarshal(data, publishData.datas_, publishData.version_, bundleName)) {
204 ZLOGW("read device list failed.");
205 return -1;
206 }
207 std::vector<OperationResult> results = Publish(publishData, bundleName);
208 if (!ITypesUtil::Marshal(reply, results)) {
209 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
210 return -1;
211 }
212 return 0;
213 }
214
OnGetData(MessageParcel & data,MessageParcel & reply)215 int32_t DataShareServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply)
216 {
217 std::string bundleName;
218 if (!ITypesUtil::Unmarshal(data, bundleName)) {
219 ZLOGW("read device list failed.");
220 return -1;
221 }
222 ZLOGI("bundleName is %{public}s", bundleName.c_str());
223 int errorCode = E_OK;
224 auto results = GetData(bundleName, errorCode);
225 if (!ITypesUtil::Marshal(reply, results.datas_, errorCode)) {
226 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
227 return -1;
228 }
229 return 0;
230 }
231
OnSubscribeRdbData(MessageParcel & data,MessageParcel & reply)232 int32_t DataShareServiceStub::OnSubscribeRdbData(MessageParcel &data, MessageParcel &reply)
233 {
234 std::vector<std::string> uris;
235 TemplateId templateId;
236 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
237 ZLOGE("read device list failed.");
238 return -1;
239 }
240 auto remoteObj = data.ReadRemoteObject();
241 sptr<IDataProxyRdbObserver> observer = new (std::nothrow)RdbObserverProxy(remoteObj);
242 if (observer == nullptr) {
243 ZLOGE("obServer is nullptr");
244 return -1;
245 }
246 std::vector<OperationResult> results = SubscribeRdbData(uris, templateId, observer);
247 if (!ITypesUtil::Marshal(reply, results)) {
248 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
249 return -1;
250 }
251 return 0;
252 }
253
OnUnsubscribeRdbData(MessageParcel & data,MessageParcel & reply)254 int32_t DataShareServiceStub::OnUnsubscribeRdbData(MessageParcel &data, MessageParcel &reply)
255 {
256 std::vector<std::string> uris;
257 TemplateId templateId;
258 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
259 ZLOGE("read device list failed.");
260 return -1;
261 }
262 std::vector<OperationResult> results = UnsubscribeRdbData(uris, templateId);
263 if (!ITypesUtil::Marshal(reply, results)) {
264 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
265 return -1;
266 }
267 return 0;
268 }
269
OnEnableRdbSubs(MessageParcel & data,MessageParcel & reply)270 int32_t DataShareServiceStub::OnEnableRdbSubs(MessageParcel &data, MessageParcel &reply)
271 {
272 std::vector<std::string> uris;
273 TemplateId templateId;
274 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
275 ZLOGE("read device list failed.");
276 return -1;
277 }
278 std::vector<OperationResult> results = EnableRdbSubs(uris, templateId);
279 if (!ITypesUtil::Marshal(reply, results)) {
280 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
281 return -1;
282 }
283 return 0;
284 }
285
OnDisableRdbSubs(MessageParcel & data,MessageParcel & reply)286 int32_t DataShareServiceStub::OnDisableRdbSubs(MessageParcel &data, MessageParcel &reply)
287 {
288 std::vector<std::string> uris;
289 TemplateId templateId;
290 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
291 ZLOGE("read device list failed.");
292 return -1;
293 }
294 std::vector<OperationResult> results = DisableRdbSubs(uris, templateId);
295 if (!ITypesUtil::Marshal(reply, results)) {
296 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
297 return -1;
298 }
299 return 0;
300 }
301
OnSubscribePublishedData(MessageParcel & data,MessageParcel & reply)302 int32_t DataShareServiceStub::OnSubscribePublishedData(MessageParcel &data, MessageParcel &reply)
303 {
304 std::vector<std::string> uris;
305 int64_t subscriberId;
306 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
307 ZLOGE("read device list failed.");
308 return -1;
309 }
310 sptr<PublishedDataObserverProxy> observer = new (std::nothrow)PublishedDataObserverProxy(data.ReadRemoteObject());
311 if (observer == nullptr) {
312 ZLOGE("obServer is nullptr");
313 return -1;
314 }
315 std::vector<OperationResult> results = SubscribePublishedData(uris, subscriberId, observer);
316 if (!ITypesUtil::Marshal(reply, results)) {
317 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
318 return -1;
319 }
320 return 0;
321 }
322
OnUnsubscribePublishedData(MessageParcel & data,MessageParcel & reply)323 int32_t DataShareServiceStub::OnUnsubscribePublishedData(MessageParcel &data, MessageParcel &reply)
324 {
325 std::vector<std::string> uris;
326 int64_t subscriberId;
327 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
328 ZLOGE("read device list failed.");
329 return -1;
330 }
331 std::vector<OperationResult> results = UnsubscribePublishedData(uris, subscriberId);
332 if (!ITypesUtil::Marshal(reply, results)) {
333 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
334 return -1;
335 }
336 return 0;
337 }
338
OnEnablePubSubs(MessageParcel & data,MessageParcel & reply)339 int32_t DataShareServiceStub::OnEnablePubSubs(MessageParcel &data, MessageParcel &reply)
340 {
341 std::vector<std::string> uris;
342 int64_t subscriberId;
343 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
344 ZLOGE("read device list failed.");
345 return -1;
346 }
347 std::vector<OperationResult> results = EnablePubSubs(uris, subscriberId);
348 if (!ITypesUtil::Marshal(reply, results)) {
349 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
350 return -1;
351 }
352 return 0;
353 }
354
OnDisablePubSubs(MessageParcel & data,MessageParcel & reply)355 int32_t DataShareServiceStub::OnDisablePubSubs(MessageParcel &data, MessageParcel &reply)
356 {
357 std::vector<std::string> uris;
358 int64_t subscriberId;
359 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
360 ZLOGE("read device list failed.");
361 return -1;
362 }
363 std::vector<OperationResult> results = DisablePubSubs(uris, subscriberId);
364 if (!ITypesUtil::Marshal(reply, results)) {
365 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
366 return -1;
367 }
368 return 0;
369 }
370
OnNotifyConnectDone(MessageParcel & data,MessageParcel & reply)371 int32_t DataShareServiceStub::OnNotifyConnectDone(MessageParcel &data, MessageParcel &reply)
372 {
373 OnConnectDone();
374 return 0;
375 }
376
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)377 int DataShareServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
378 {
379 int tryTimes = TRY_TIMES;
380 while (!isReady_.load() && tryTimes > 0) {
381 tryTimes--;
382 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
383 }
384 auto callingPid = IPCSkeleton::GetCallingPid();
385 if (code != DATA_SHARE_SERVICE_CMD_QUERY && code != DATA_SHARE_SERVICE_CMD_GET_SILENT_PROXY_STATUS) {
386 ZLOGI("code:%{public}u, callingPid:%{public}d", code, callingPid);
387 }
388 if (!CheckInterfaceToken(data)) {
389 return DATA_SHARE_ERROR;
390 }
391 int res = -1;
392 if (code < DATA_SHARE_SERVICE_CMD_MAX) {
393 auto start = std::chrono::steady_clock::now();
394 res = (this->*HANDLERS[code])(data, reply);
395 auto finish = std::chrono::steady_clock::now();
396 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start);
397 if (duration >= TIME_THRESHOLD) {
398 int64_t milliseconds = duration.count();
399 ZLOGW("over time, code:%{public}u callingPid:%{public}d, cost:%{public}" PRIi64 "ms",
400 code, callingPid, milliseconds);
401 }
402 }
403 return res;
404 }
405
OnNotifyObserver(MessageParcel & data,MessageParcel & reply)406 int32_t DataShareServiceStub::OnNotifyObserver(MessageParcel &data, MessageParcel &reply)
407 {
408 std::string uri;
409 if (!ITypesUtil::Unmarshal(data, uri)) {
410 ZLOGE("read device list failed.");
411 return -1;
412 }
413 NotifyObserver(uri);
414 return 0;
415 }
416
OnSetSilentSwitch(MessageParcel & data,MessageParcel & reply)417 int32_t DataShareServiceStub::OnSetSilentSwitch(MessageParcel &data, MessageParcel &reply)
418 {
419 std::string uri;
420 bool enable = false;
421 if (!ITypesUtil::Unmarshal(data, uri, enable)) {
422 ZLOGE("Unmarshal set silent switch failed. uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
423 return IPC_STUB_INVALID_DATA_ERR;
424 }
425 int32_t status = EnableSilentProxy(uri, enable);
426 if (!ITypesUtil::Marshal(reply, status)) {
427 ZLOGE("Marshal status:0x%{public}x", status);
428 return IPC_STUB_WRITE_PARCEL_ERR;
429 }
430 return E_OK;
431 }
432
OnGetSilentProxyStatus(MessageParcel & data,MessageParcel & reply)433 int32_t DataShareServiceStub::OnGetSilentProxyStatus(MessageParcel &data, MessageParcel &reply)
434 {
435 std::string uri;
436 if (!ITypesUtil::Unmarshal(data, uri)) {
437 ZLOGE("Unmarshal silent enable failed. uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
438 return IPC_STUB_INVALID_DATA_ERR;
439 }
440 int32_t enable = GetSilentProxyStatus(uri);
441 if (!ITypesUtil::Marshal(reply, enable)) {
442 ZLOGE("Marshal enable:%{public}d failed.", enable);
443 return IPC_STUB_WRITE_PARCEL_ERR;
444 }
445 return E_OK;
446 }
447
OnRegisterObserver(MessageParcel & data,MessageParcel & reply)448 int32_t DataShareServiceStub::OnRegisterObserver(MessageParcel &data, MessageParcel &reply)
449 {
450 std::string uri;
451 sptr<IRemoteObject> remoteObj;
452 if (!ITypesUtil::Unmarshal(data, uri, remoteObj)) {
453 ZLOGE("Unmarshal failed,uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
454 return IPC_STUB_INVALID_DATA_ERR;
455 }
456 int32_t status = RegisterObserver(uri, remoteObj);
457 if (!ITypesUtil::Marshal(reply, status)) {
458 ZLOGE("Marshal failed,status:0x%{public}x,uri:%{public}s", status,
459 DistributedData::Anonymous::Change(uri).c_str());
460 return IPC_STUB_WRITE_PARCEL_ERR;
461 }
462 return E_OK;
463 }
464
OnUnregisterObserver(MessageParcel & data,MessageParcel & reply)465 int32_t DataShareServiceStub::OnUnregisterObserver(MessageParcel &data, MessageParcel &reply)
466 {
467 std::string uri;
468 sptr<IRemoteObject> remoteObj;
469 if (!ITypesUtil::Unmarshal(data, uri, remoteObj)) {
470 ZLOGE("Unmarshal failed,uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
471 return IPC_STUB_INVALID_DATA_ERR;
472 }
473 int32_t status = UnregisterObserver(uri, remoteObj);
474 if (!ITypesUtil::Marshal(reply, status)) {
475 ZLOGE("Marshal failed,status:0x%{public}x,uri:%{public}s", status,
476 DistributedData::Anonymous::Change(uri).c_str());
477 return IPC_STUB_WRITE_PARCEL_ERR;
478 }
479 return E_OK;
480 }
481
SetServiceReady()482 void DataShareServiceStub::SetServiceReady()
483 {
484 isReady_.store(true);
485 }
486 } // namespace DataShare
487 } // namespace OHOS