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 #include "data_share_service_proxy.h"
17
18 #include <cinttypes>
19 #include "data_ability_observer_interface.h"
20 #include "datashare_errno.h"
21 #include "datashare_itypes_utils.h"
22 #include "datashare_log.h"
23 #include "datashare_string_utils.h"
24 #include "distributeddata_data_share_ipc_interface_code.h"
25 #include "ishared_result_set.h"
26
27 namespace OHOS {
28 namespace DataShare {
29 using InterfaceCode = OHOS::DistributedShare::DataShare::DataShareServiceInterfaceCode;
DataShareServiceProxy(const sptr<IRemoteObject> & object)30 DataShareServiceProxy::DataShareServiceProxy(const sptr<IRemoteObject> &object)
31 : IRemoteProxy<IDataShareService>(object)
32 {
33 }
34
CleanSystem()35 void DataShareServiceProxy::CleanSystem()
36 {
37 GetSystem() = false;
38 }
39
IsSystem()40 bool DataShareServiceProxy::IsSystem()
41 {
42 return GetSystem();
43 }
44
SetSystem(bool isSystem)45 void DataShareServiceProxy::SetSystem(bool isSystem)
46 {
47 GetSystem() = isSystem;
48 }
49
GetSystem()50 bool& DataShareServiceProxy::GetSystem()
51 {
52 static thread_local bool isSystem = false;
53 return isSystem;
54 }
55
CastIPCCode(DistributedShare::DataShare::DataShareServiceInterfaceCode code)56 uint32_t DataShareServiceProxy::CastIPCCode(DistributedShare::DataShare::DataShareServiceInterfaceCode code)
57 {
58 if (IsSystem()) {
59 return static_cast<uint32_t>(code) + DistributedShare::DataShare::DATA_SHARE_CMD_SYSTEM_CODE;
60 }
61 return static_cast<uint32_t>(code);
62 }
63
Insert(const Uri & uri,const Uri & extUri,const DataShareValuesBucket & value)64 int32_t DataShareServiceProxy::Insert(const Uri &uri, const Uri &extUri, const DataShareValuesBucket &value)
65 {
66 auto [errCode, status] = InsertEx(uri, extUri, value);
67 if (errCode == NO_ERROR) {
68 return status;
69 } else if (errCode < NO_ERROR) {
70 return errCode;
71 }
72 LOG_ERROR("DataShareServiceProxy insert failed, errCode = %{public}d", errCode);
73 return DATA_SHARE_ERROR;
74 }
75
Update(const Uri & uri,const Uri & extUri,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket)76 int32_t DataShareServiceProxy::Update(const Uri &uri, const Uri &extUri,
77 const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket)
78 {
79 auto [errCode, status] = UpdateEx(uri, extUri, predicate, valuesBucket);
80 if (errCode == NO_ERROR) {
81 return status;
82 } else if (errCode < NO_ERROR) {
83 return errCode;
84 }
85 LOG_ERROR("DataShareServiceProxy update failed, errCode = %{public}d", errCode);
86 return DATA_SHARE_ERROR;
87 }
88
Delete(const Uri & uri,const Uri & extUri,const DataSharePredicates & predicate)89 int32_t DataShareServiceProxy::Delete(const Uri &uri, const Uri &extUri, const DataSharePredicates &predicate)
90 {
91 auto [errCode, status] = DeleteEx(uri, extUri, predicate);
92 if (errCode == NO_ERROR) {
93 return status;
94 } else if (errCode < NO_ERROR) {
95 return errCode;
96 }
97 LOG_ERROR("DataShareServiceProxy delete failed, errCode = %{public}d", errCode);
98 return DATA_SHARE_ERROR;
99 }
100
InsertEx(const Uri & uri,const Uri & extUri,const DataShareValuesBucket & value)101 std::pair<int32_t, int32_t> DataShareServiceProxy::InsertEx(const Uri &uri, const Uri &extUri,
102 const DataShareValuesBucket &value)
103 {
104 const std::string &uriStr = uri.ToString();
105 MessageParcel data;
106 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
107 LOG_ERROR("Write descriptor failed!");
108 return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
109 }
110 if (!ITypesUtil::Marshal(data, uriStr, extUri.ToString(), value)) {
111 LOG_ERROR("Write to message parcel failed!");
112 return std::make_pair(E_MARSHAL_ERROR, 0);
113 }
114
115 int32_t result = -1;
116 int32_t errCode = -1;
117 MessageParcel reply;
118 MessageOption option;
119 int32_t err = Remote()->SendRequest(
120 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_INSERTEX), data, reply, option);
121 if (err != NO_ERROR) {
122 LOG_ERROR("InsertEx fail to sendRequest. uri: %{public}s, err: %{public}d",
123 DataShareStringUtils::Anonymous(uriStr).c_str(), err);
124 return std::make_pair(DATA_SHARE_ERROR, 0);
125 }
126 if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
127 LOG_ERROR("fail to Unmarshal");
128 return std::make_pair(E_UNMARSHAL_ERROR, 0);
129 }
130 return std::make_pair(errCode, result);
131 }
132
UpdateEx(const Uri & uri,const Uri & extUri,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket)133 std::pair<int32_t, int32_t> DataShareServiceProxy::UpdateEx(const Uri &uri, const Uri &extUri,
134 const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket)
135 {
136 const std::string &uriStr = uri.ToString();
137 MessageParcel data;
138 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
139 LOG_ERROR("Write descriptor failed!");
140 return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
141 }
142 if (!ITypesUtil::Marshal(data, uriStr, extUri.ToString(), predicate, valuesBucket)) {
143 LOG_ERROR("Write to message parcel failed!");
144 return std::make_pair(E_MARSHAL_ERROR, 0);
145 }
146
147 int32_t result = -1;
148 int32_t errCode = -1;
149 MessageParcel reply;
150 MessageOption option;
151 int32_t err = Remote()->SendRequest(
152 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_UPDATEEX), data, reply, option);
153 if (err != NO_ERROR) {
154 LOG_ERROR("UpdateEx fail to sendRequest. uri: %{public}s, err: %{public}d",
155 DataShareStringUtils::Anonymous(uriStr).c_str(), err);
156 return std::make_pair(DATA_SHARE_ERROR, 0);
157 }
158 if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
159 LOG_ERROR("fail to Unmarshal");
160 return std::make_pair(E_UNMARSHAL_ERROR, 0);
161 }
162 return std::make_pair(errCode, result);
163 }
164
DeleteEx(const Uri & uri,const Uri & extUri,const DataSharePredicates & predicate)165 std::pair<int32_t, int32_t> DataShareServiceProxy::DeleteEx(const Uri &uri, const Uri &extUri,
166 const DataSharePredicates &predicate)
167 {
168 const std::string &uriStr = uri.ToString();
169 MessageParcel data;
170 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
171 LOG_ERROR("Write descriptor failed!");
172 return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
173 }
174 if (!ITypesUtil::Marshal(data, uriStr, extUri.ToString(), predicate)) {
175 LOG_ERROR("Write to message parcel failed!");
176 return std::make_pair(E_MARSHAL_ERROR, 0);
177 }
178
179 int32_t result = -1;
180 int32_t errCode = -1;
181 MessageParcel reply;
182 MessageOption option;
183 int32_t err = Remote()->SendRequest(
184 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_DELETEEX), data, reply, option);
185 if (err != NO_ERROR) {
186 LOG_ERROR("DeleteEx fail to sendRequest. uri: %{public}s, err: %{public}d",
187 DataShareStringUtils::Anonymous(uriStr).c_str(), err);
188 return std::make_pair(DATA_SHARE_ERROR, 0);
189 }
190 if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
191 LOG_ERROR("fail to Unmarshal");
192 return std::make_pair(E_UNMARSHAL_ERROR, 0);
193 }
194 return std::make_pair(errCode, result);
195 }
196
Query(const Uri & uri,const Uri & extUri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)197 std::shared_ptr<DataShareResultSet> DataShareServiceProxy::Query(const Uri &uri, const Uri &extUri,
198 const DataSharePredicates &predicates, std::vector<std::string> &columns, DatashareBusinessError &businessError)
199 {
200 const std::string &uriStr = uri.ToString();
201 MessageParcel data;
202 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
203 LOG_ERROR("WriteInterfaceToken failed!");
204 return nullptr;
205 }
206
207 if (!ITypesUtil::Marshal(data, uriStr, extUri.ToString(), predicates, columns)) {
208 LOG_ERROR("Write to message parcel failed!");
209 return nullptr;
210 }
211
212 MessageParcel reply;
213 MessageOption option;
214 int32_t err = Remote()->SendRequest(
215 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_QUERY), data, reply, option);
216
217 auto result = ISharedResultSet::ReadFromParcel(reply);
218 businessError.SetCode(reply.ReadInt32());
219 if (err != NO_ERROR) {
220 LOG_ERROR("Query fail to sendRequest. uri: %{public}s, err: %{public}d",
221 DataShareStringUtils::Anonymous(uriStr).c_str(), err);
222 return nullptr;
223 }
224 return result;
225 }
226
AddQueryTemplate(const std::string & uri,int64_t subscriberId,Template & tpl)227 int DataShareServiceProxy::AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl)
228 {
229 MessageParcel data;
230 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
231 LOG_ERROR("Write descriptor failed!");
232 return DATA_SHARE_ERROR;
233 }
234 if (!tpl.update_.empty()) {
235 std::string updateSqlPrefix = "UPDATE";
236 std::string checkPrefix = tpl.update_.substr(0, updateSqlPrefix.size());
237 std::for_each(std::begin(checkPrefix), std::end(checkPrefix), [](auto &c) {
238 c = std::toupper(c);
239 });
240 if (checkPrefix != updateSqlPrefix) {
241 LOG_ERROR("Parameter update only support update SQL, Parameter: %{public}s", checkPrefix.c_str());
242 return DATA_SHARE_ERROR;
243 }
244 }
245 if (!ITypesUtil::Marshal(data, uri, subscriberId, tpl.update_, tpl.predicates_, tpl.scheduler_)) {
246 LOG_ERROR("Write to message parcel failed!");
247 return DATA_SHARE_ERROR;
248 }
249
250 MessageParcel reply;
251 MessageOption option;
252 int32_t err = Remote()->SendRequest(
253 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_ADD_TEMPLATE), data, reply, option);
254 if (err != NO_ERROR) {
255 LOG_ERROR("AddTemplate fail to sendRequest. uri: %{public}s, err: %{public}d",
256 DataShareStringUtils::Anonymous(uri).c_str(), err);
257 return DATA_SHARE_ERROR;
258 }
259 return reply.ReadInt32();
260 }
261
DelQueryTemplate(const std::string & uri,int64_t subscriberId)262 int DataShareServiceProxy::DelQueryTemplate(const std::string &uri, int64_t subscriberId)
263 {
264 MessageParcel data;
265 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
266 LOG_ERROR("Write descriptor failed!");
267 return DATA_SHARE_ERROR;
268 }
269 if (!ITypesUtil::Marshal(data, uri, subscriberId)) {
270 LOG_ERROR("Write to message parcel failed!");
271 return DATA_SHARE_ERROR;
272 }
273
274 MessageParcel reply;
275 MessageOption option;
276 int32_t err = Remote()->SendRequest(
277 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_DEL_TEMPLATE), data, reply, option);
278 if (err != NO_ERROR) {
279 LOG_ERROR("Delete template fail to sendRequest. uri: %{public}s, err: %{public}d",
280 DataShareStringUtils::Anonymous(uri).c_str(), err);
281 return DATA_SHARE_ERROR;
282 }
283 return reply.ReadInt32();
284 }
285
Publish(const Data & data,const std::string & bundleName)286 std::vector<OperationResult> DataShareServiceProxy::Publish(const Data &data, const std::string &bundleName)
287 {
288 std::vector<OperationResult> results;
289 MessageParcel parcel;
290 if (!parcel.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
291 LOG_ERROR("Write descriptor failed!");
292 return results;
293 }
294 if (!ITypesUtil::Marshal(parcel, data, bundleName)) {
295 LOG_ERROR("Marshalfailed!");
296 return results;
297 }
298
299 MessageParcel reply;
300 MessageOption option;
301 int32_t err = Remote()->SendRequest(
302 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_PUBLISH), parcel, reply, option);
303 if (err != NO_ERROR) {
304 LOG_ERROR("Publish fail to sendRequest. err: %{public}d", err);
305 return results;
306 }
307
308 ITypesUtil::Unmarshal(reply, results);
309 return results;
310 }
311
GetPublishedData(const std::string & bundleName,int & resultCode)312 Data DataShareServiceProxy::GetPublishedData(const std::string &bundleName, int &resultCode)
313 {
314 Data results;
315 MessageParcel data;
316 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
317 LOG_ERROR("Write descriptor failed!");
318 return results;
319 }
320 if (!ITypesUtil::Marshal(data, bundleName)) {
321 LOG_ERROR("Write to message parcel failed!");
322 return results;
323 }
324
325 MessageParcel reply;
326 MessageOption option;
327 int32_t err = Remote()->SendRequest(
328 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_GET_DATA), data, reply, option);
329 if (err != NO_ERROR) {
330 LOG_ERROR("Get published data fail to sendRequest, err: %{public}d", err);
331 return results;
332 }
333 ITypesUtil::Unmarshal(reply, results.datas_, resultCode);
334 return results;
335 }
336
SubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId,const sptr<IDataProxyRdbObserver> & observer)337 std::vector<OperationResult> DataShareServiceProxy::SubscribeRdbData(const std::vector<std::string> &uris,
338 const TemplateId &templateId, const sptr<IDataProxyRdbObserver> &observer)
339 {
340 std::vector<OperationResult> results;
341 if (observer == nullptr) {
342 LOG_ERROR("Observer is nullptr, subscriberId: %{public}" PRId64, templateId.subscriberId_);
343 return results;
344 }
345 MessageParcel data;
346 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
347 LOG_ERROR("Write descriptor failed!");
348 return results;
349 }
350
351 if (!ITypesUtil::Marshal(data, uris, templateId.subscriberId_, templateId.bundleName_)) {
352 LOG_ERROR("Write to message parcel failed!");
353 return results;
354 }
355 if (!data.WriteRemoteObject(observer->AsObject())) {
356 LOG_ERROR("Failed to write parcelable dataObserver ");
357 return results;
358 }
359
360 MessageParcel reply;
361 MessageOption option;
362 int32_t err = Remote()->SendRequest(
363 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_SUBSCRIBE_RDB), data, reply, option);
364 if (err != NO_ERROR) {
365 LOG_ERROR("SubscribeRdbData fail to sendRequest. err: %{public}d", err);
366 return results;
367 }
368 ITypesUtil::Unmarshal(reply, results);
369 return results;
370 }
371
UnSubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId)372 std::vector<OperationResult> DataShareServiceProxy::UnSubscribeRdbData(
373 const std::vector<std::string> &uris, const TemplateId &templateId)
374 {
375 std::vector<OperationResult> results;
376 MessageParcel data;
377 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
378 LOG_ERROR("Write descriptor failed!");
379 return results;
380 }
381
382 if (!ITypesUtil::Marshal(data, uris, templateId.subscriberId_, templateId.bundleName_)) {
383 LOG_ERROR("Write to message parcel failed!");
384 return results;
385 }
386
387 MessageParcel reply;
388 MessageOption option;
389 int32_t err = Remote()->SendRequest(
390 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_UNSUBSCRIBE_RDB), data, reply, option);
391 if (err != NO_ERROR) {
392 LOG_ERROR("Fail to sendRequest. err: %{public}d", err);
393 return results;
394 }
395 ITypesUtil::Unmarshal(reply, results);
396 return results;
397 }
398
EnableSubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId)399 std::vector<OperationResult> DataShareServiceProxy::EnableSubscribeRdbData(
400 const std::vector<std::string> &uris, const TemplateId &templateId)
401 {
402 std::vector<OperationResult> results;
403 MessageParcel data;
404 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
405 LOG_ERROR("Write descriptor failed!");
406 return results;
407 }
408
409 if (!ITypesUtil::Marshal(data, uris, templateId.subscriberId_, templateId.bundleName_)) {
410 LOG_ERROR("Write to message parcel failed!");
411 return results;
412 }
413
414 MessageParcel reply;
415 MessageOption option;
416 int32_t err = Remote()->SendRequest(
417 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_ENABLE_SUBSCRIBE_RDB), data, reply, option);
418 if (err != NO_ERROR) {
419 LOG_ERROR("Fail to sendRequest. err: %{public}d", err);
420 return results;
421 }
422 ITypesUtil::Unmarshal(reply, results);
423 return results;
424 }
425
DisableSubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId)426 std::vector<OperationResult> DataShareServiceProxy::DisableSubscribeRdbData(
427 const std::vector<std::string> &uris, const TemplateId &templateId)
428 {
429 std::vector<OperationResult> results;
430 MessageParcel data;
431 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
432 LOG_ERROR("Write descriptor failed!");
433 return results;
434 }
435
436 if (!ITypesUtil::Marshal(data, uris, templateId.subscriberId_, templateId.bundleName_)) {
437 LOG_ERROR("Write to message parcel failed!");
438 return results;
439 }
440
441 MessageParcel reply;
442 MessageOption option;
443 int32_t err = Remote()->SendRequest(
444 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_DISABLE_SUBSCRIBE_RDB), data, reply, option);
445 if (err != NO_ERROR) {
446 LOG_ERROR("Disable subscribe RdbData fail to sendRequest. err: %{public}d", err);
447 return results;
448 }
449 ITypesUtil::Unmarshal(reply, results);
450 return results;
451 }
452
SubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId,const sptr<IDataProxyPublishedDataObserver> & observer)453 std::vector<OperationResult> DataShareServiceProxy::SubscribePublishedData(
454 const std::vector<std::string> &uris, int64_t subscriberId, const sptr<IDataProxyPublishedDataObserver> &observer)
455 {
456 std::vector<OperationResult> results;
457 if (observer == nullptr) {
458 LOG_ERROR("Observer is nullptr, subscriberId: %{public}" PRId64, subscriberId);
459 return results;
460 }
461 MessageParcel data;
462 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
463 LOG_ERROR("Write descriptor failed!");
464 return results;
465 }
466 if (!ITypesUtil::Marshal(data, uris, subscriberId)) {
467 LOG_ERROR("Write to message parcel failed!");
468 return results;
469 }
470 if (!data.WriteRemoteObject(observer->AsObject())) {
471 LOG_ERROR("Failed to write remote object dataObserver ");
472 return results;
473 }
474
475 MessageParcel reply;
476 MessageOption option;
477 int32_t err = Remote()->SendRequest(
478 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_SUBSCRIBE_PUBLISHED), data, reply, option);
479 if (err != NO_ERROR) {
480 LOG_ERROR("Subscribe published data fail to sendRequest. err: %{public}d", err);
481 return results;
482 }
483 ITypesUtil::Unmarshal(reply, results);
484 return results;
485 }
486
UnSubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId)487 std::vector<OperationResult> DataShareServiceProxy::UnSubscribePublishedData(
488 const std::vector<std::string> &uris, int64_t subscriberId)
489 {
490 std::vector<OperationResult> results;
491 MessageParcel data;
492 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
493 LOG_ERROR("Write descriptor failed!");
494 return results;
495 }
496 if (!ITypesUtil::Marshal(data, uris, subscriberId)) {
497 LOG_ERROR("Write to message parcel failed!");
498 return results;
499 }
500
501 MessageParcel reply;
502 MessageOption option;
503 int32_t err = Remote()->SendRequest(
504 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_UNSUBSCRIBE_PUBLISHED), data, reply, option);
505 if (err != NO_ERROR) {
506 LOG_ERROR("UnSubscribe published data fail to sendRequest. err: %{public}d", err);
507 return results;
508 }
509 ITypesUtil::Unmarshal(reply, results);
510 return results;
511 }
512
EnableSubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId)513 std::vector<OperationResult> DataShareServiceProxy::EnableSubscribePublishedData(
514 const std::vector<std::string> &uris, int64_t subscriberId)
515 {
516 std::vector<OperationResult> results;
517 MessageParcel data;
518 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
519 LOG_ERROR("Write descriptor failed!");
520 return results;
521 }
522 if (!ITypesUtil::Marshal(data, uris, subscriberId)) {
523 LOG_ERROR("Write to message parcel failed!");
524 return results;
525 }
526
527 MessageParcel reply;
528 MessageOption option;
529 int32_t err = Remote()->SendRequest(
530 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_ENABLE_SUBSCRIBE_PUBLISHED), data, reply, option);
531 if (err != NO_ERROR) {
532 LOG_ERROR("Enable subscribe published data fail to sendRequest. err: %{public}d", err);
533 return results;
534 }
535 ITypesUtil::Unmarshal(reply, results);
536 return results;
537 }
538
DisableSubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId)539 std::vector<OperationResult> DataShareServiceProxy::DisableSubscribePublishedData(
540 const std::vector<std::string> &uris, int64_t subscriberId)
541 {
542 std::vector<OperationResult> results;
543 MessageParcel data;
544 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
545 LOG_ERROR("Write descriptor failed!");
546 return results;
547 }
548 if (!ITypesUtil::Marshal(data, uris, subscriberId)) {
549 LOG_ERROR("Write to message parcel failed!");
550 return results;
551 }
552
553 MessageParcel reply;
554 MessageOption option;
555 int32_t err = Remote()->SendRequest(
556 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_DISABLE_SUBSCRIBE_PUBLISHED), data, reply, option);
557 if (err != NO_ERROR) {
558 LOG_ERROR("Disable subscribe published data fail to sendRequest. err: %{public}d", err);
559 return results;
560 }
561 ITypesUtil::Unmarshal(reply, results);
562 return results;
563 }
564
Notify(const std::string & uri)565 void DataShareServiceProxy::Notify(const std::string &uri)
566 {
567 MessageParcel data;
568 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
569 LOG_ERROR("Write descriptor failed!");
570 return;
571 }
572 if (!ITypesUtil::Marshal(data, uri)) {
573 LOG_ERROR("Write to message parcel failed!");
574 return;
575 }
576
577 MessageParcel reply;
578 MessageOption option;
579 int32_t err = Remote()->SendRequest(
580 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_NOTIFY_OBSERVERS), data, reply, option);
581 if (err != NO_ERROR) {
582 LOG_ERROR("Notify fail to sendRequest. err: %{public}d", err);
583 return;
584 }
585 }
586
SetSilentSwitch(const Uri & uri,bool enable)587 int DataShareServiceProxy::SetSilentSwitch(const Uri &uri, bool enable)
588 {
589 const std::string &uriStr = uri.ToString();
590 MessageParcel data;
591 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
592 LOG_ERROR("Write descriptor failed!");
593 return DATA_SHARE_ERROR;
594 }
595 if (!ITypesUtil::Marshal(data, uriStr, enable)) {
596 LOG_ERROR("Write to message parcel failed!");
597 return DATA_SHARE_ERROR;
598 }
599
600 MessageParcel reply;
601 MessageOption option;
602 int32_t err = Remote()->SendRequest(
603 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_SET_SILENT_SWITCH), data, reply, option);
604 if (err != NO_ERROR) {
605 LOG_ERROR("SetSilentSwitch fail to sendRequest. uri: %{public}s, err: %{public}d",
606 DataShareStringUtils::Anonymous(uriStr).c_str(), err);
607 if (err == E_NOT_SYSTEM_APP) {
608 return E_NOT_SYSTEM_APP;
609 }
610 return DATA_SHARE_ERROR;
611 }
612 return reply.ReadInt32();
613 }
614
GetSilentProxyStatus(const std::string & uri)615 int DataShareServiceProxy::GetSilentProxyStatus(const std::string &uri)
616 {
617 MessageParcel data;
618 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
619 LOG_ERROR("Write descriptor failed!");
620 return DATA_SHARE_ERROR;
621 }
622 if (!ITypesUtil::Marshal(data, uri)) {
623 LOG_ERROR("Write to message parcel failed!");
624 return DATA_SHARE_ERROR;
625 }
626
627 MessageParcel reply;
628 MessageOption option;
629 int32_t err = Remote()->SendRequest(
630 CastIPCCode(InterfaceCode::DATA_SHARE_SERVICE_CMD_GET_SILENT_PROXY_STATUS), data, reply, option);
631 if (err != NO_ERROR) {
632 LOG_ERROR("Is silent proxy enable fail to sendRequest. uri: %{public}s, err: %{public}d",
633 DataShareStringUtils::Anonymous(uri).c_str(), err);
634 return DATA_SHARE_ERROR;
635 }
636 return reply.ReadInt32();
637 }
638
RegisterObserver(const Uri & uri,const sptr<OHOS::IRemoteObject> & dataObserver)639 int DataShareServiceProxy::RegisterObserver(const Uri &uri, const sptr<OHOS::IRemoteObject> &dataObserver)
640 {
641 if (dataObserver == nullptr) {
642 LOG_ERROR("DataObserver is nullptr, uri:%{public}s",
643 DataShareStringUtils::Anonymous(uri.ToString()).c_str());
644 return DATA_SHARE_ERROR;
645 }
646 MessageParcel data;
647 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
648 LOG_ERROR("Write interface token failed");
649 return DATA_SHARE_ERROR;
650 }
651 if (!ITypesUtil::Marshal(data, uri.ToString(), dataObserver)) {
652 LOG_ERROR("Failed to marshalling");
653 return DATA_SHARE_ERROR;
654 }
655 MessageParcel reply;
656 MessageOption option;
657 int32_t err = Remote()->SendRequest(
658 static_cast<uint32_t>(InterfaceCode::DATA_SHARE_SERVICE_CMD_REGISTER_OBSERVER), data, reply, option);
659 if (err != NO_ERROR) {
660 LOG_ERROR("RegisterObserver fail to sendRequest. uri:%{public}s, err:%{public}d",
661 DataShareStringUtils::Anonymous(uri.ToString()).c_str(), err);
662 return DATA_SHARE_ERROR;
663 }
664 return reply.ReadInt32();
665 }
666
UnRegisterObserver(const Uri & uri,const sptr<OHOS::IRemoteObject> & dataObserver)667 int DataShareServiceProxy::UnRegisterObserver(const Uri &uri, const sptr<OHOS::IRemoteObject> &dataObserver)
668 {
669 if (dataObserver == nullptr) {
670 LOG_ERROR("DataObserver is nullptr, uri:%{public}s",
671 DataShareStringUtils::Anonymous(uri.ToString()).c_str());
672 return DATA_SHARE_ERROR;
673 }
674 MessageParcel data;
675 if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) {
676 LOG_ERROR("Write interface token failed");
677 return DATA_SHARE_ERROR;
678 }
679 if (!ITypesUtil::Marshal(data, uri.ToString(), dataObserver)) {
680 LOG_ERROR("Failed to Marshalling");
681 return DATA_SHARE_ERROR;
682 }
683 MessageParcel reply;
684 MessageOption option;
685 int32_t err = Remote()->SendRequest(
686 static_cast<uint32_t>(InterfaceCode::DATA_SHARE_SERVICE_CMD_UNREGISTER_OBSERVER), data, reply, option);
687 if (err != NO_ERROR) {
688 LOG_ERROR("UnRegisterObserver fail to sendRequest. uri: %{public}s, err: %{public}d",
689 DataShareStringUtils::Anonymous(uri.ToString()).c_str(), err);
690 return DATA_SHARE_ERROR;
691 }
692 return reply.ReadInt32();
693 }
694 } // namespace DataShare
695 } // namespace OHOS
696