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 #define LOG_TAG "DataShareHelperImpl"
16 #include <cinttypes>
17 #include "datashare_helper_impl.h"
18
19 #include "adaptor.h"
20 #include "dataobs_mgr_client.h"
21 #include "datashare_log.h"
22 #include "datashare_string_utils.h"
23 #include "datashare_radar_reporter.h"
24 #include "datashare_result_set.h"
25 #include "datashare_string_utils.h"
26 #include "general_controller_provider_impl.h"
27 #include "general_controller_service_impl.h"
28
29 namespace OHOS {
30 namespace DataShare {
31 using namespace AppExecFwk;
32 // non-silent access
DataShareHelperImpl(const Uri & uri,const sptr<IRemoteObject> & token,std::shared_ptr<DataShareConnection> connection,bool isSystem)33 DataShareHelperImpl::DataShareHelperImpl(const Uri &uri, const sptr<IRemoteObject> &token,
34 std::shared_ptr<DataShareConnection> connection, bool isSystem)
35 {
36 LOG_DEBUG("starts");
37 isSystem_ = isSystem;
38 generalCtl_ = std::make_shared<GeneralControllerProviderImpl>(connection, uri, token);
39 extSpCtl_ = std::make_shared<ExtSpecialController>(connection, uri, token);
40 }
41
42 // silent access
DataShareHelperImpl(std::string extUri,bool isSystem)43 DataShareHelperImpl::DataShareHelperImpl(std::string extUri, bool isSystem)
44 {
45 LOG_DEBUG("starts");
46 isSystem_ = isSystem;
47 generalCtl_ = std::make_shared<GeneralControllerServiceImpl>(extUri);
48 persistentDataCtl_ = std::make_shared<PersistentDataController>();
49 publishedDataCtl_ = std::make_shared<PublishedDataController>();
50 }
51
~DataShareHelperImpl()52 DataShareHelperImpl::~DataShareHelperImpl()
53 {
54 if (persistentDataCtl_ != nullptr && publishedDataCtl_ != nullptr) {
55 persistentDataCtl_->UnSubscribeRdbData(this, {}, {});
56 publishedDataCtl_->UnSubscribePublishedData(this, {}, {});
57 }
58 }
59
Release()60 bool DataShareHelperImpl::Release()
61 {
62 extSpCtl_ = nullptr;
63 generalCtl_ = nullptr;
64 return true;
65 }
66
GetFileTypes(Uri & uri,const std::string & mimeTypeFilter)67 std::vector<std::string> DataShareHelperImpl::GetFileTypes(Uri &uri, const std::string &mimeTypeFilter)
68 {
69 auto extSpCtl = extSpCtl_;
70 if (extSpCtl == nullptr) {
71 LOG_ERROR("extSpCtl is nullptr");
72 return std::vector<std::string>();
73 }
74 return extSpCtl->GetFileTypes(uri, mimeTypeFilter);
75 }
76
OpenFile(Uri & uri,const std::string & mode)77 int DataShareHelperImpl::OpenFile(Uri &uri, const std::string &mode)
78 {
79 auto extSpCtl = extSpCtl_;
80 if (extSpCtl == nullptr) {
81 LOG_ERROR("extSpCtl is nullptr");
82 return DATA_SHARE_ERROR;
83 }
84 return extSpCtl->OpenFile(uri, mode);
85 }
86
OpenFileWithErrCode(Uri & uri,const std::string & mode,int32_t & errCode)87 int DataShareHelperImpl::OpenFileWithErrCode(Uri &uri, const std::string &mode, int32_t &errCode)
88 {
89 auto extSpCtl = extSpCtl_;
90 if (extSpCtl == nullptr) {
91 LOG_ERROR("extSpCtl is nullptr");
92 return DATA_SHARE_ERROR;
93 }
94 return extSpCtl->OpenFileWithErrCode(uri, mode, errCode);
95 }
96
OpenRawFile(Uri & uri,const std::string & mode)97 int DataShareHelperImpl::OpenRawFile(Uri &uri, const std::string &mode)
98 {
99 auto extSpCtl = extSpCtl_;
100 if (extSpCtl == nullptr) {
101 LOG_ERROR("extSpCtl is nullptr");
102 return DATA_SHARE_ERROR;
103 }
104 return extSpCtl->OpenRawFile(uri, mode);
105 }
106
Insert(Uri & uri,const DataShareValuesBucket & value)107 int DataShareHelperImpl::Insert(Uri &uri, const DataShareValuesBucket &value)
108 {
109 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
110 auto generalCtl = generalCtl_;
111 if (generalCtl == nullptr) {
112 LOG_ERROR("generalCtl_ is nullptr");
113 return DATA_SHARE_ERROR;
114 }
115 DataShareServiceProxy::SetSystem(isSystem_);
116 auto res = generalCtl->Insert(uri, value);
117 DataShareServiceProxy::CleanSystem();
118 return res;
119 }
120
InsertExt(Uri & uri,const DataShareValuesBucket & value,std::string & result)121 int DataShareHelperImpl::InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result)
122 {
123 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
124 auto extSpCtl = extSpCtl_;
125 if (extSpCtl == nullptr) {
126 LOG_ERROR("providerSpCtl is nullptr");
127 return DATA_SHARE_ERROR;
128 }
129 return extSpCtl->InsertExt(uri, value, result);
130 }
131
Update(Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)132 int DataShareHelperImpl::Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
133 {
134 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
135 auto generalCtl = generalCtl_;
136 if (generalCtl == nullptr) {
137 LOG_ERROR("generalCtl is nullptr");
138 return DATA_SHARE_ERROR;
139 }
140 DataShareServiceProxy::SetSystem(isSystem_);
141 auto res = generalCtl->Update(uri, predicates, value);
142 DataShareServiceProxy::CleanSystem();
143 return res;
144 }
145
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)146 int DataShareHelperImpl::BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results)
147 {
148 auto extSpCtl = extSpCtl_;
149 if (extSpCtl == nullptr) {
150 LOG_ERROR("extSpCtl is nullptr");
151 return DATA_SHARE_ERROR;
152 }
153 return extSpCtl->BatchUpdate(operations, results);
154 }
155
Delete(Uri & uri,const DataSharePredicates & predicates)156 int DataShareHelperImpl::Delete(Uri &uri, const DataSharePredicates &predicates)
157 {
158 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
159 auto generalCtl = generalCtl_;
160 if (generalCtl == nullptr) {
161 LOG_ERROR("generalCtl is nullptr");
162 return DATA_SHARE_ERROR;
163 }
164 DataShareServiceProxy::SetSystem(isSystem_);
165 auto res = generalCtl->Delete(uri, predicates);
166 DataShareServiceProxy::CleanSystem();
167 return res;
168 }
169
InsertEx(Uri & uri,const DataShareValuesBucket & value)170 std::pair<int32_t, int32_t> DataShareHelperImpl::InsertEx(Uri &uri, const DataShareValuesBucket &value)
171 {
172 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
173 auto generalCtl = generalCtl_;
174 if (generalCtl == nullptr) {
175 LOG_ERROR("generalCtl_ is nullptr");
176 return std::make_pair(DATA_SHARE_ERROR, 0);
177 }
178 DataShareServiceProxy::SetSystem(isSystem_);
179 auto [errCode, status] = generalCtl->InsertEx(uri, value);
180 DataShareServiceProxy::CleanSystem();
181 if (errCode != E_OK) {
182 LOG_ERROR("generalCtl insert failed, errCode = %{public}d", errCode);
183 }
184 return std::make_pair(errCode, status);
185 }
186
UpdateEx(Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)187 std::pair<int32_t, int32_t> DataShareHelperImpl::UpdateEx(
188 Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
189 {
190 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
191 auto generalCtl = generalCtl_;
192 if (generalCtl == nullptr) {
193 LOG_ERROR("generalCtl is nullptr");
194 return std::make_pair(DATA_SHARE_ERROR, 0);
195 }
196 DataShareServiceProxy::SetSystem(isSystem_);
197 auto [errCode, status] = generalCtl->UpdateEx(uri, predicates, value);
198 DataShareServiceProxy::CleanSystem();
199 if (errCode != E_OK) {
200 LOG_ERROR("generalCtl update failed, errCode = %{public}d", errCode);
201 }
202 return std::make_pair(errCode, status);
203 }
204
DeleteEx(Uri & uri,const DataSharePredicates & predicates)205 std::pair<int32_t, int32_t> DataShareHelperImpl::DeleteEx(Uri &uri, const DataSharePredicates &predicates)
206 {
207 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
208 auto generalCtl = generalCtl_;
209 if (generalCtl == nullptr) {
210 LOG_ERROR("generalCtl is nullptr");
211 return std::make_pair(DATA_SHARE_ERROR, 0);
212 }
213 DataShareServiceProxy::SetSystem(isSystem_);
214 auto [errCode, status] = generalCtl->DeleteEx(uri, predicates);
215 DataShareServiceProxy::CleanSystem();
216 if (errCode != E_OK) {
217 LOG_ERROR("generalCtl delete failed, errCode = %{public}d", errCode);
218 }
219 return std::make_pair(errCode, status);
220 }
221
Query(Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError * businessError)222 std::shared_ptr<DataShareResultSet> DataShareHelperImpl::Query(Uri &uri, const DataSharePredicates &predicates,
223 std::vector<std::string> &columns, DatashareBusinessError *businessError)
224 {
225 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
226 auto generalCtl = generalCtl_;
227 if (generalCtl == nullptr) {
228 LOG_ERROR("generalCtl is nullptr");
229 return nullptr;
230 }
231 DataShareOption option;
232 DatashareBusinessError error;
233 DataShareServiceProxy::SetSystem(isSystem_);
234 auto resultSet = generalCtl->Query(uri, predicates, columns, error, option);
235 DataShareServiceProxy::CleanSystem();
236 if (businessError != nullptr) {
237 *businessError = error;
238 }
239 return resultSet;
240 }
241
Query(Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DataShareOption & option,DatashareBusinessError * businessError)242 std::shared_ptr<DataShareResultSet> DataShareHelperImpl::Query(Uri &uri, const DataSharePredicates &predicates,
243 std::vector<std::string> &columns, DataShareOption &option, DatashareBusinessError *businessError)
244 {
245 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
246 auto generalCtl = generalCtl_;
247 if (generalCtl == nullptr) {
248 LOG_ERROR("generalCtl is nullptr");
249 return nullptr;
250 }
251 DatashareBusinessError error;
252 DataShareServiceProxy::SetSystem(isSystem_);
253 auto resultSet = generalCtl->Query(uri, predicates, columns, error, option);
254 DataShareServiceProxy::CleanSystem();
255 if (businessError != nullptr) {
256 *businessError = error;
257 }
258 return resultSet;
259 }
260
GetType(Uri & uri)261 std::string DataShareHelperImpl::GetType(Uri &uri)
262 {
263 auto extSpCtl = extSpCtl_;
264 if (extSpCtl == nullptr) {
265 LOG_ERROR("extSpCtl is nullptr");
266 return "";
267 }
268 return extSpCtl->GetType(uri);
269 }
270
BatchInsert(Uri & uri,const std::vector<DataShareValuesBucket> & values)271 int DataShareHelperImpl::BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values)
272 {
273 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
274 auto extSpCtl = extSpCtl_;
275 if (extSpCtl == nullptr) {
276 LOG_ERROR("providerSepOperator is nullptr");
277 return DATA_SHARE_ERROR;
278 }
279 return extSpCtl->BatchInsert(uri, values);
280 }
281
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)282 int DataShareHelperImpl::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
283 {
284 auto extSpCtl = extSpCtl_;
285 if (extSpCtl == nullptr) {
286 LOG_ERROR("extSpCtl is nullptr");
287 return DATA_SHARE_ERROR;
288 }
289 return extSpCtl->ExecuteBatch(statements, result);
290 }
291
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)292 int DataShareHelperImpl::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
293 {
294 RadarReporter::RadarReport report(RadarReporter::OBSERVER_MANAGER,
295 RadarReporter::REGISTER_OBSERVER, __FUNCTION__);
296 if (dataObserver == nullptr) {
297 LOG_ERROR("dataObserver is nullptr");
298 report.SetError(RadarReporter::EMPTY_OBSERVER_ERROR);
299 return E_NULL_OBSERVER;
300 }
301 auto generalCtl = generalCtl_;
302 if (generalCtl == nullptr) {
303 LOG_ERROR("generalCtl is nullptr");
304 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
305 return E_HELPER_DIED;
306 }
307 DataShareServiceProxy::SetSystem(isSystem_);
308 int ret = generalCtl->RegisterObserver(uri, dataObserver);
309 DataShareServiceProxy::CleanSystem();
310 return ret;
311 }
312
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)313 int DataShareHelperImpl::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
314 {
315 RadarReporter::RadarReport report(RadarReporter::OBSERVER_MANAGER,
316 RadarReporter::UNREGISTER_OBSERVER, __FUNCTION__);
317 if (dataObserver == nullptr) {
318 LOG_ERROR("dataObserver is nullptr");
319 report.SetError(RadarReporter::EMPTY_OBSERVER_ERROR);
320 return E_NULL_OBSERVER;
321 }
322 auto generalCtl = generalCtl_;
323 if (generalCtl == nullptr) {
324 LOG_ERROR("generalCtl is nullptr");
325 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
326 return E_HELPER_DIED;
327 }
328 DataShareServiceProxy::SetSystem(isSystem_);
329 int ret = generalCtl->UnregisterObserver(uri, dataObserver);
330 DataShareServiceProxy::CleanSystem();
331 return ret;
332 }
333
NotifyChange(const Uri & uri)334 void DataShareHelperImpl::NotifyChange(const Uri &uri)
335 {
336 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
337 auto generalCtl = generalCtl_;
338 if (generalCtl == nullptr) {
339 LOG_ERROR("extSpCtl is nullptr");
340 return;
341 }
342 DataShareServiceProxy::SetSystem(isSystem_);
343 generalCtl->NotifyChange(uri);
344 DataShareServiceProxy::CleanSystem();
345 return;
346 }
347
348 /**
349 * Registers an observer to DataObsMgr specified by the given Uri. Only non-silent is supported, and there is no
350 * default implemention for the provider. It needs to be handled by the user.
351 *
352 * @param uri, Indicates the path of the data to operate.
353 * @param dataObserver, Indicates the DataShareObserver object.
354 * @param isDescendants, Indicates the Whether to note the change of descendants.
355 */
RegisterObserverExtProvider(const Uri & uri,std::shared_ptr<DataShareObserver> dataObserver,bool isDescendants)356 int DataShareHelperImpl::RegisterObserverExtProvider(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver,
357 bool isDescendants)
358 {
359 if (dataObserver == nullptr) {
360 LOG_ERROR("dataObserver is nullptr");
361 return E_NULL_OBSERVER;
362 }
363 sptr<ObserverImpl> obs = ObserverImpl::GetObserver(uri, dataObserver);
364 if (obs == nullptr) {
365 LOG_ERROR("new ObserverImpl failed");
366 return E_NULL_OBSERVER;
367 }
368 auto generalCtl = generalCtl_;
369 if (generalCtl == nullptr) {
370 LOG_ERROR("generalCtl is nullptr");
371 return E_HELPER_DIED;
372 }
373 DataShareServiceProxy::SetSystem(isSystem_);
374 // only support non-silent access
375 ErrCode ret = generalCtl->RegisterObserverExtProvider(uri, obs, isDescendants);
376 DataShareServiceProxy::CleanSystem();
377 LOG_INFO("Register observerExt, ret:%{public}d, uri:%{public}s",
378 ret, DataShareStringUtils::Anonymous(uri.ToString()).c_str());
379 if (ret != E_OK) {
380 ObserverImpl::DeleteObserver(uri, dataObserver);
381 }
382 return ret;
383 }
384
385 /**
386 * Deregisters an observer used for DataObsMgr specified by the given Uri. Only non-silent is supported, and there is
387 * no default implemention for the provider. It needs to be handled by the user.
388 *
389 * @param uri, Indicates the path of the data to operate.
390 * @param dataObserver, Indicates the DataShareObserver object.
391 */
UnregisterObserverExtProvider(const Uri & uri,std::shared_ptr<DataShareObserver> dataObserver)392 int DataShareHelperImpl::UnregisterObserverExtProvider(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver)
393 {
394 if (dataObserver == nullptr) {
395 LOG_ERROR("dataObserver is nullptr");
396 return E_NULL_OBSERVER;
397 }
398 if (!ObserverImpl::FindObserver(uri, dataObserver)) {
399 LOG_ERROR("observer not exit!");
400 return E_NULL_OBSERVER;
401 }
402 sptr<ObserverImpl> obs = ObserverImpl::GetObserver(uri, dataObserver);
403 if (obs == nullptr) {
404 LOG_ERROR("new ObserverImpl failed");
405 return E_NULL_OBSERVER;
406 }
407 auto generalCtl = generalCtl_;
408 if (generalCtl == nullptr) {
409 LOG_ERROR("generalCtl is nullptr");
410 return E_HELPER_DIED;
411 }
412 DataShareServiceProxy::SetSystem(isSystem_);
413 // only support non-silent access
414 ErrCode ret = generalCtl->UnregisterObserverExtProvider(uri, obs);
415 DataShareServiceProxy::CleanSystem();
416 if (ret != E_OK) {
417 return ret;
418 }
419 ObserverImpl::DeleteObserver(uri, dataObserver);
420 return E_OK;
421 }
422
423 /**
424 * Notifies the registered observers of a change to the data resource specified by Uris. Only non-silent is supported,
425 * and there is no default implemention for the provider. It needs to be handled by the user.
426 *
427 * @param changeInfo Indicates the info of the data to operate.
428 */
NotifyChangeExtProvider(const DataShareObserver::ChangeInfo & changeInfo)429 void DataShareHelperImpl::NotifyChangeExtProvider(const DataShareObserver::ChangeInfo &changeInfo)
430 {
431 auto generalCtl = generalCtl_;
432 if (generalCtl == nullptr) {
433 LOG_ERROR("extSpCtl is nullptr");
434 return;
435 }
436 DataShareServiceProxy::SetSystem(isSystem_);
437 // only support non-silent access
438 ErrCode ret = generalCtl->NotifyChangeExtProvider(ObserverImpl::ConvertInfo(changeInfo));
439 DataShareServiceProxy::CleanSystem();
440 LOG_INFO("Notify changeExt, ret:%{public}d", ret);
441 return;
442 }
443
NormalizeUri(Uri & uri)444 Uri DataShareHelperImpl::NormalizeUri(Uri &uri)
445 {
446 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
447 auto extSpCtl = extSpCtl_;
448 if (extSpCtl == nullptr) {
449 LOG_ERROR("extSpCtl is nullptr");
450 return Uri("");
451 }
452 return extSpCtl->NormalizeUri(uri);
453 }
454
DenormalizeUri(Uri & uri)455 Uri DataShareHelperImpl::DenormalizeUri(Uri &uri)
456 {
457 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
458 auto extSpCtl = extSpCtl_;
459 if (extSpCtl == nullptr) {
460 LOG_ERROR("extSpCtl is nullptr");
461 return Uri("");
462 }
463 return extSpCtl->DenormalizeUri(uri);
464 }
465
AddQueryTemplate(const std::string & uri,int64_t subscriberId,Template & tpl)466 int DataShareHelperImpl::AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl)
467 {
468 RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
469 RadarReporter::ADD_TEMPLATE, __FUNCTION__);
470 auto persistentDataCtl = persistentDataCtl_;
471 if (persistentDataCtl == nullptr) {
472 LOG_ERROR("persistentDataCtl is nullptr");
473 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
474 return DATA_SHARE_ERROR;
475 }
476 DataShareServiceProxy::SetSystem(isSystem_);
477 auto res = persistentDataCtl->AddQueryTemplate(uri, subscriberId, tpl);
478 DataShareServiceProxy::CleanSystem();
479 return res;
480 }
481
DelQueryTemplate(const std::string & uri,int64_t subscriberId)482 int DataShareHelperImpl::DelQueryTemplate(const std::string &uri, int64_t subscriberId)
483 {
484 RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
485 RadarReporter::DELETE_TEMPLATE, __FUNCTION__);
486 auto persistentDataCtl = persistentDataCtl_;
487 if (persistentDataCtl == nullptr) {
488 LOG_ERROR("persistentDataCtl is nullptr");
489 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
490 return DATA_SHARE_ERROR;
491 }
492 DataShareServiceProxy::SetSystem(isSystem_);
493 auto res = persistentDataCtl->DelQueryTemplate(uri, subscriberId);
494 DataShareServiceProxy::CleanSystem();
495 return res;
496 }
497
Publish(const Data & data,const std::string & bundleName)498 std::vector<OperationResult> DataShareHelperImpl::Publish(const Data &data, const std::string &bundleName)
499 {
500 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
501 auto publishedDataCtl = publishedDataCtl_;
502 if (publishedDataCtl == nullptr) {
503 LOG_ERROR("publishedDataCtl is nullptr");
504 return std::vector<OperationResult>();
505 }
506 DataShareServiceProxy::SetSystem(isSystem_);
507 auto res = publishedDataCtl->Publish(data, bundleName);
508 DataShareServiceProxy::CleanSystem();
509 return res;
510 }
511
GetPublishedData(const std::string & bundleName,int & resultCode)512 Data DataShareHelperImpl::GetPublishedData(const std::string &bundleName, int &resultCode)
513 {
514 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
515 auto publishedDataCtl = publishedDataCtl_;
516 if (publishedDataCtl == nullptr) {
517 LOG_ERROR("publishedDataCtl is nullptr");
518 return Data();
519 }
520 DataShareServiceProxy::SetSystem(isSystem_);
521 auto res = publishedDataCtl->GetPublishedData(bundleName, resultCode);
522 DataShareServiceProxy::CleanSystem();
523 return res;
524 }
525
SubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId,const std::function<void (const RdbChangeNode & changeNode)> & callback)526 std::vector<OperationResult> DataShareHelperImpl::SubscribeRdbData(const std::vector<std::string> &uris,
527 const TemplateId &templateId, const std::function<void(const RdbChangeNode &changeNode)> &callback)
528 {
529 LOG_DEBUG("Start SubscribeRdbData");
530 RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
531 RadarReporter::SUBSCRIBE_RDB_DATA, __FUNCTION__);
532 auto persistentDataCtl = persistentDataCtl_;
533 if (persistentDataCtl == nullptr) {
534 LOG_ERROR("persistentDataCtl is nullptr");
535 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
536 return std::vector<OperationResult>();
537 }
538 DataShareServiceProxy::SetSystem(isSystem_);
539 auto res = persistentDataCtl->SubscribeRdbData(this, uris, templateId, callback);
540 DataShareServiceProxy::CleanSystem();
541 return res;
542 }
543
UnsubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId)544 __attribute__((no_sanitize("cfi"))) std::vector<OperationResult> DataShareHelperImpl::UnsubscribeRdbData(
545 const std::vector<std::string> &uris, const TemplateId &templateId)
546 {
547 LOG_DEBUG("Start UnsubscribeRdbData");
548 RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
549 RadarReporter::UNSUBSCRIBE_RDB_DATA, __FUNCTION__);
550 auto persistentDataCtl = persistentDataCtl_;
551 if (persistentDataCtl == nullptr) {
552 LOG_ERROR("persistentDataCtl is nullptr");
553 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
554 return std::vector<OperationResult>();
555 }
556 DataShareServiceProxy::SetSystem(isSystem_);
557 auto res = persistentDataCtl->UnSubscribeRdbData(this, uris, templateId);
558 DataShareServiceProxy::CleanSystem();
559 return res;
560 }
561
EnableRdbSubs(const std::vector<std::string> & uris,const TemplateId & templateId)562 std::vector<OperationResult> DataShareHelperImpl::EnableRdbSubs(const std::vector<std::string> &uris,
563 const TemplateId &templateId)
564 {
565 LOG_DEBUG("Start EnableSubscribeRdbData");
566 std::string uriAll = "";
567 for (auto uri : uris) {
568 uriAll += (DataShareStringUtils::Anonymous(uri) + ",");
569 }
570 LOG_INFO("uri is %{public}s bundleName is %{public}s, subscriberId is %{public}" PRId64 "",
571 DataShareStringUtils::Anonymous(uriAll).c_str(), templateId.bundleName_.c_str(), templateId.subscriberId_);
572 auto persistentDataCtl = persistentDataCtl_;
573 if (persistentDataCtl == nullptr) {
574 LOG_ERROR("persistentDataCtl is nullptr");
575 return std::vector<OperationResult>();
576 }
577 DataShareServiceProxy::SetSystem(isSystem_);
578 auto res = persistentDataCtl->EnableSubscribeRdbData(this, uris, templateId);
579 DataShareServiceProxy::CleanSystem();
580 return res;
581 }
582
DisableRdbSubs(const std::vector<std::string> & uris,const TemplateId & templateId)583 std::vector<OperationResult> DataShareHelperImpl::DisableRdbSubs(const std::vector<std::string> &uris,
584 const TemplateId &templateId)
585 {
586 LOG_DEBUG("Start DisableSubscribeRdbData");
587 std::string uriAll = "";
588 for (auto uri : uris) {
589 uriAll += (DataShareStringUtils::Anonymous(uri) + ",");
590 }
591 LOG_INFO("uri is %{public}s bundleName is %{public}s, subscriberId is %{public}" PRId64 "",
592 DataShareStringUtils::Anonymous(uriAll).c_str(), templateId.bundleName_.c_str(), templateId.subscriberId_);
593 auto persistentDataCtl = persistentDataCtl_;
594 if (persistentDataCtl == nullptr) {
595 LOG_ERROR("persistentDataCtl is nullptr");
596 return std::vector<OperationResult>();
597 }
598 DataShareServiceProxy::SetSystem(isSystem_);
599 auto res = persistentDataCtl->DisableSubscribeRdbData(this, uris, templateId);
600 DataShareServiceProxy::CleanSystem();
601 return res;
602 }
603
SubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId,const std::function<void (const PublishedDataChangeNode & changeNode)> & callback)604 std::vector<OperationResult> DataShareHelperImpl::SubscribePublishedData(const std::vector<std::string> &uris,
605 int64_t subscriberId, const std::function<void(const PublishedDataChangeNode &changeNode)> &callback)
606 {
607 LOG_DEBUG("Start SubscribePublishedData");
608 RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
609 RadarReporter::SUBSCRIBE_PUBLISHED_DATA, __FUNCTION__);
610 auto publishedDataCtl = publishedDataCtl_;
611 if (publishedDataCtl == nullptr) {
612 LOG_ERROR("publishedDataCtl is nullptr");
613 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
614 return std::vector<OperationResult>();
615 }
616 DataShareServiceProxy::SetSystem(isSystem_);
617 auto res = publishedDataCtl->SubscribePublishedData(this, uris, subscriberId, callback);
618 DataShareServiceProxy::CleanSystem();
619 return res;
620 }
621
UnsubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId)622 std::vector<OperationResult> DataShareHelperImpl::UnsubscribePublishedData(const std::vector<std::string> &uris,
623 int64_t subscriberId)
624 {
625 LOG_DEBUG("Start UnSubscribePublishedData");
626 RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
627 RadarReporter::UNSUBSCRIBE_PUBLISHED_DATA, __FUNCTION__);
628 auto publishedDataCtl = publishedDataCtl_;
629 if (publishedDataCtl == nullptr) {
630 LOG_ERROR("publishedDataCtl is nullptr");
631 report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
632 return std::vector<OperationResult>();
633 }
634 DataShareServiceProxy::SetSystem(isSystem_);
635 auto res = publishedDataCtl->UnSubscribePublishedData(this, uris, subscriberId);
636 DataShareServiceProxy::CleanSystem();
637 return res;
638 }
639
EnablePubSubs(const std::vector<std::string> & uris,int64_t subscriberId)640 std::vector<OperationResult> DataShareHelperImpl::EnablePubSubs(const std::vector<std::string> &uris,
641 int64_t subscriberId)
642 {
643 LOG_DEBUG("Start enablePubSubs");
644 std::string uriAll = "";
645 for (auto uri : uris) {
646 uriAll += (DataShareStringUtils::Anonymous(uri) + ",");
647 }
648 LOG_INFO("uri is %{public}s subscriberId is %{public}" PRId64 "",
649 DataShareStringUtils::Anonymous(uriAll).c_str(), subscriberId);
650 auto publishedDataCtl = publishedDataCtl_;
651 if (publishedDataCtl == nullptr) {
652 LOG_ERROR("publishedDataCtl is nullptr");
653 return std::vector<OperationResult>();
654 }
655 DataShareServiceProxy::SetSystem(isSystem_);
656 auto res = publishedDataCtl->EnableSubscribePublishedData(this, uris, subscriberId);
657 DataShareServiceProxy::CleanSystem();
658 return res;
659 }
660
DisablePubSubs(const std::vector<std::string> & uris,int64_t subscriberId)661 std::vector<OperationResult> DataShareHelperImpl::DisablePubSubs(const std::vector<std::string> &uris,
662 int64_t subscriberId)
663 {
664 LOG_DEBUG("Start disablePubSubs");
665 std::string uriAll = "";
666 for (auto uri : uris) {
667 uriAll += (DataShareStringUtils::Anonymous(uri) + ",");
668 }
669 LOG_INFO("uri is %{public}s subscriberId is %{public}" PRId64 "",
670 DataShareStringUtils::Anonymous(uriAll).c_str(), subscriberId);
671 auto publishedDataCtl = publishedDataCtl_;
672 if (publishedDataCtl == nullptr) {
673 LOG_ERROR("publishedDataCtl is nullptr");
674 return std::vector<OperationResult>();
675 }
676 DataShareServiceProxy::SetSystem(isSystem_);
677 auto res = publishedDataCtl->DisableSubscribePublishedData(this, uris, subscriberId);
678 DataShareServiceProxy::CleanSystem();
679 return res;
680 }
681
UserDefineFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)682 int32_t DataShareHelperImpl::UserDefineFunc(
683 MessageParcel &data, MessageParcel &reply, MessageOption &option)
684 {
685 DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
686 auto extSpCtl = extSpCtl_;
687 if (extSpCtl == nullptr) {
688 LOG_ERROR("providerSpCtl is nullptr");
689 return DATA_SHARE_ERROR;
690 }
691 return extSpCtl->UserDefineFunc(data, reply, option);
692 }
693 } // namespace DataShare
694 } // namespace OHOS