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