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 
17 #include "datashare_helper_impl.h"
18 
19 #include "adaptor.h"
20 #include "concurrent_map.h"
21 #include "data_ability_observer_interface.h"
22 #include "dataobs_mgr_client.h"
23 #include "datashare_log.h"
24 #include "datashare_radar_reporter.h"
25 #include "datashare_result_set.h"
26 
27 #include "general_controller_provider_impl.h"
28 #include "general_controller_service_impl.h"
29 
30 namespace OHOS {
31 namespace DataShare {
32 using namespace AppExecFwk;
DataShareHelperImpl(const Uri & uri,const sptr<IRemoteObject> & token,std::shared_ptr<DataShareConnection> connection)33 DataShareHelperImpl::DataShareHelperImpl(const Uri &uri, const sptr<IRemoteObject> &token,
34     std::shared_ptr<DataShareConnection> connection)
35 {
36     LOG_DEBUG("starts");
37     generalCtl_ = std::make_shared<GeneralControllerProviderImpl>(connection, uri, token);
38     extSpCtl_ = std::make_shared<ExtSpecialController>(connection, uri, token);
39 }
40 
DataShareHelperImpl(std::string extUri)41 DataShareHelperImpl::DataShareHelperImpl(std::string extUri)
42 {
43     LOG_DEBUG("starts");
44     generalCtl_ = std::make_shared<GeneralControllerServiceImpl>(extUri);
45     persistentDataCtl_ = std::make_shared<PersistentDataController>();
46     publishedDataCtl_ = std::make_shared<PublishedDataController>();
47 }
48 
~DataShareHelperImpl()49 DataShareHelperImpl::~DataShareHelperImpl()
50 {
51     if (persistentDataCtl_ != nullptr && publishedDataCtl_ != nullptr) {
52         persistentDataCtl_->UnSubscribeRdbData(this, {}, {});
53         publishedDataCtl_->UnSubscribePublishedData(this, {}, {});
54     }
55 }
56 
Release()57 bool DataShareHelperImpl::Release()
58 {
59     extSpCtl_ = nullptr;
60     generalCtl_ = nullptr;
61     return true;
62 }
63 
GetFileTypes(Uri & uri,const std::string & mimeTypeFilter)64 std::vector<std::string> DataShareHelperImpl::GetFileTypes(Uri &uri, const std::string &mimeTypeFilter)
65 {
66     auto extSpCtl = extSpCtl_;
67     if (extSpCtl == nullptr) {
68         LOG_ERROR("extSpCtl is nullptr");
69         return std::vector<std::string>();
70     }
71     return extSpCtl->GetFileTypes(uri, mimeTypeFilter);
72 }
73 
OpenFile(Uri & uri,const std::string & mode)74 int DataShareHelperImpl::OpenFile(Uri &uri, const std::string &mode)
75 {
76     auto extSpCtl = extSpCtl_;
77     if (extSpCtl == nullptr) {
78         LOG_ERROR("extSpCtl is nullptr");
79         return DATA_SHARE_ERROR;
80     }
81     return extSpCtl->OpenFile(uri, mode);
82 }
83 
OpenRawFile(Uri & uri,const std::string & mode)84 int DataShareHelperImpl::OpenRawFile(Uri &uri, const std::string &mode)
85 {
86     auto extSpCtl = extSpCtl_;
87     if (extSpCtl == nullptr) {
88         LOG_ERROR("extSpCtl is nullptr");
89         return DATA_SHARE_ERROR;
90     }
91     return extSpCtl->OpenRawFile(uri, mode);
92 }
93 
Insert(Uri & uri,const DataShareValuesBucket & value)94 int DataShareHelperImpl::Insert(Uri &uri, const DataShareValuesBucket &value)
95 {
96     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
97     auto generalCtl = generalCtl_;
98     if (generalCtl == nullptr) {
99         LOG_ERROR("generalCtl_ is nullptr");
100         return DATA_SHARE_ERROR;
101     }
102     return generalCtl->Insert(uri, value);
103 }
104 
InsertExt(Uri & uri,const DataShareValuesBucket & value,std::string & result)105 int DataShareHelperImpl::InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result)
106 {
107     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
108     auto extSpCtl = extSpCtl_;
109     if (extSpCtl == nullptr) {
110         LOG_ERROR("providerSpCtl is nullptr");
111         return DATA_SHARE_ERROR;
112     }
113     return extSpCtl->InsertExt(uri, value, result);
114 }
115 
Update(Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)116 int DataShareHelperImpl::Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
117 {
118     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
119     auto generalCtl = generalCtl_;
120     if (generalCtl == nullptr) {
121         LOG_ERROR("generalCtl is nullptr");
122         return DATA_SHARE_ERROR;
123     }
124     return generalCtl->Update(uri, predicates, value);
125 }
126 
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)127 int DataShareHelperImpl::BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results)
128 {
129     auto extSpCtl = extSpCtl_;
130     if (extSpCtl == nullptr) {
131         LOG_ERROR("extSpCtl is nullptr");
132         return DATA_SHARE_ERROR;
133     }
134     return extSpCtl->BatchUpdate(operations, results);
135 }
136 
Delete(Uri & uri,const DataSharePredicates & predicates)137 int DataShareHelperImpl::Delete(Uri &uri, const DataSharePredicates &predicates)
138 {
139     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
140     auto generalCtl = generalCtl_;
141     if (generalCtl == nullptr) {
142         LOG_ERROR("generalCtl is nullptr");
143         return DATA_SHARE_ERROR;
144     }
145     return generalCtl->Delete(uri, predicates);
146 }
147 
InsertEx(Uri & uri,const DataShareValuesBucket & value)148 std::pair<int32_t, int32_t> DataShareHelperImpl::InsertEx(Uri &uri, const DataShareValuesBucket &value)
149 {
150     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
151     auto generalCtl = generalCtl_;
152     if (generalCtl == nullptr) {
153         LOG_ERROR("generalCtl_ is nullptr");
154         return std::make_pair(DATA_SHARE_ERROR, 0);
155     }
156     auto [errCode, status] = generalCtl->InsertEx(uri, value);
157     if (errCode != E_OK) {
158         LOG_ERROR("generalCtl insert failed, errCode = %{public}d", errCode);
159     }
160     return std::make_pair(errCode, status);
161 }
162 
UpdateEx(Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)163 std::pair<int32_t, int32_t> DataShareHelperImpl::UpdateEx(
164     Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
165 {
166     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
167     auto generalCtl = generalCtl_;
168     if (generalCtl == nullptr) {
169         LOG_ERROR("generalCtl is nullptr");
170         return std::make_pair(DATA_SHARE_ERROR, 0);
171     }
172     auto [errCode, status] = generalCtl->UpdateEx(uri, predicates, value);
173     if (errCode != E_OK) {
174         LOG_ERROR("generalCtl update failed, errCode = %{public}d", errCode);
175     }
176     return std::make_pair(errCode, status);
177 }
178 
DeleteEx(Uri & uri,const DataSharePredicates & predicates)179 std::pair<int32_t, int32_t> DataShareHelperImpl::DeleteEx(Uri &uri, const DataSharePredicates &predicates)
180 {
181     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
182     auto generalCtl = generalCtl_;
183     if (generalCtl == nullptr) {
184         LOG_ERROR("generalCtl is nullptr");
185         return std::make_pair(DATA_SHARE_ERROR, 0);
186     }
187     auto [errCode, status] = generalCtl->DeleteEx(uri, predicates);
188     if (errCode != E_OK) {
189         LOG_ERROR("generalCtl delete failed, errCode = %{public}d", errCode);
190     }
191     return std::make_pair(errCode, status);
192 }
193 
Query(Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError * businessError)194 std::shared_ptr<DataShareResultSet> DataShareHelperImpl::Query(Uri &uri, const DataSharePredicates &predicates,
195     std::vector<std::string> &columns, DatashareBusinessError *businessError)
196 {
197     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
198     auto generalCtl = generalCtl_;
199     if (generalCtl == nullptr) {
200         LOG_ERROR("generalCtl is nullptr");
201         return nullptr;
202     }
203     DatashareBusinessError error;
204     auto resultSet = generalCtl->Query(uri, predicates, columns, error);
205     if (businessError != nullptr) {
206         *businessError = error;
207     }
208     return resultSet;
209 }
210 
GetType(Uri & uri)211 std::string DataShareHelperImpl::GetType(Uri &uri)
212 {
213     auto extSpCtl = extSpCtl_;
214     if (extSpCtl == nullptr) {
215         LOG_ERROR("extSpCtl is nullptr");
216         return "";
217     }
218     return extSpCtl->GetType(uri);
219 }
220 
BatchInsert(Uri & uri,const std::vector<DataShareValuesBucket> & values)221 int DataShareHelperImpl::BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values)
222 {
223     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
224     auto extSpCtl = extSpCtl_;
225     if (extSpCtl == nullptr) {
226         LOG_ERROR("providerSepOperator is nullptr");
227         return DATA_SHARE_ERROR;
228     }
229     return extSpCtl->BatchInsert(uri, values);
230 }
231 
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)232 int DataShareHelperImpl::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
233 {
234     auto extSpCtl = extSpCtl_;
235     if (extSpCtl == nullptr) {
236         LOG_ERROR("extSpCtl is nullptr");
237         return DATA_SHARE_ERROR;
238     }
239     return extSpCtl->ExecuteBatch(statements, result);
240 }
241 
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)242 int DataShareHelperImpl::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
243 {
244     RadarReporter::RadarReport report(RadarReporter::OBSERVER_MANAGER,
245         RadarReporter::REGISTER_OBSERVER, __FUNCTION__);
246     if (dataObserver == nullptr) {
247         LOG_ERROR("dataObserver is nullptr");
248         report.SetError(RadarReporter::EMPTY_OBSERVER_ERROR);
249         return E_NULL_OBSERVER;
250     }
251     auto generalCtl = generalCtl_;
252     if (generalCtl == nullptr) {
253         LOG_ERROR("generalCtl is nullptr");
254         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
255         return E_HELPER_DIED;
256     }
257     return generalCtl->RegisterObserver(uri, dataObserver);
258 }
259 
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)260 int DataShareHelperImpl::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
261 {
262     RadarReporter::RadarReport report(RadarReporter::OBSERVER_MANAGER,
263         RadarReporter::UNREGISTER_OBSERVER, __FUNCTION__);
264     if (dataObserver == nullptr) {
265         LOG_ERROR("dataObserver is nullptr");
266         report.SetError(RadarReporter::EMPTY_OBSERVER_ERROR);
267         return E_NULL_OBSERVER;
268     }
269     auto generalCtl = generalCtl_;
270     if (generalCtl == nullptr) {
271         LOG_ERROR("generalCtl is nullptr");
272         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
273         return E_HELPER_DIED;
274     }
275     return generalCtl->UnregisterObserver(uri, dataObserver);
276 }
277 
NotifyChange(const Uri & uri)278 void DataShareHelperImpl::NotifyChange(const Uri &uri)
279 {
280     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
281     auto generalCtl = generalCtl_;
282     if (generalCtl == nullptr) {
283         LOG_ERROR("extSpCtl is nullptr");
284         return;
285     }
286     return generalCtl->NotifyChange(uri);
287 }
288 
NormalizeUri(Uri & uri)289 Uri DataShareHelperImpl::NormalizeUri(Uri &uri)
290 {
291     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
292     auto extSpCtl = extSpCtl_;
293     if (extSpCtl == nullptr) {
294         LOG_ERROR("extSpCtl is nullptr");
295         return Uri("");
296     }
297     return extSpCtl->NormalizeUri(uri);
298 }
299 
DenormalizeUri(Uri & uri)300 Uri DataShareHelperImpl::DenormalizeUri(Uri &uri)
301 {
302     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
303     auto extSpCtl = extSpCtl_;
304     if (extSpCtl == nullptr) {
305         LOG_ERROR("extSpCtl is nullptr");
306         return Uri("");
307     }
308     return extSpCtl->DenormalizeUri(uri);
309 }
310 
AddQueryTemplate(const std::string & uri,int64_t subscriberId,Template & tpl)311 int DataShareHelperImpl::AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl)
312 {
313     RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
314         RadarReporter::ADD_TEMPLATE, __FUNCTION__);
315     auto persistentDataCtl = persistentDataCtl_;
316     if (persistentDataCtl == nullptr) {
317         LOG_ERROR("persistentDataCtl is nullptr");
318         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
319         return DATA_SHARE_ERROR;
320     }
321     return persistentDataCtl->AddQueryTemplate(uri, subscriberId, tpl);
322 }
323 
DelQueryTemplate(const std::string & uri,int64_t subscriberId)324 int DataShareHelperImpl::DelQueryTemplate(const std::string &uri, int64_t subscriberId)
325 {
326     RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
327         RadarReporter::DELETE_TEMPLATE, __FUNCTION__);
328     auto persistentDataCtl = persistentDataCtl_;
329     if (persistentDataCtl == nullptr) {
330         LOG_ERROR("persistentDataCtl is nullptr");
331         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
332         return DATA_SHARE_ERROR;
333     }
334     return persistentDataCtl->DelQueryTemplate(uri, subscriberId);
335 }
336 
Publish(const Data & data,const std::string & bundleName)337 std::vector<OperationResult> DataShareHelperImpl::Publish(const Data &data, const std::string &bundleName)
338 {
339     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
340     auto publishedDataCtl = publishedDataCtl_;
341     if (publishedDataCtl == nullptr) {
342         LOG_ERROR("publishedDataCtl is nullptr");
343         return std::vector<OperationResult>();
344     }
345     return publishedDataCtl->Publish(data, bundleName);
346 }
347 
GetPublishedData(const std::string & bundleName,int & resultCode)348 Data DataShareHelperImpl::GetPublishedData(const std::string &bundleName, int &resultCode)
349 {
350     DISTRIBUTED_DATA_HITRACE(std::string(LOG_TAG) + "::" + std::string(__FUNCTION__));
351     auto publishedDataCtl = publishedDataCtl_;
352     if (publishedDataCtl == nullptr) {
353         LOG_ERROR("publishedDataCtl is nullptr");
354         return Data();
355     }
356     return publishedDataCtl->GetPublishedData(bundleName, resultCode);
357 }
358 
SubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId,const std::function<void (const RdbChangeNode & changeNode)> & callback)359 std::vector<OperationResult> DataShareHelperImpl::SubscribeRdbData(const std::vector<std::string> &uris,
360     const TemplateId &templateId, const std::function<void(const RdbChangeNode &changeNode)> &callback)
361 {
362     LOG_DEBUG("Start SubscribeRdbData");
363     RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
364         RadarReporter::SUBSCRIBE_RDB_DATA, __FUNCTION__);
365     auto persistentDataCtl = persistentDataCtl_;
366     if (persistentDataCtl == nullptr) {
367         LOG_ERROR("persistentDataCtl is nullptr");
368         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
369         return std::vector<OperationResult>();
370     }
371     return persistentDataCtl->SubscribeRdbData(this, uris, templateId, callback);
372 }
373 
UnsubscribeRdbData(const std::vector<std::string> & uris,const TemplateId & templateId)374 __attribute__((no_sanitize("cfi"))) std::vector<OperationResult> DataShareHelperImpl::UnsubscribeRdbData(
375     const std::vector<std::string> &uris, const TemplateId &templateId)
376 {
377     LOG_DEBUG("Start UnsubscribeRdbData");
378     RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
379         RadarReporter::UNSUBSCRIBE_RDB_DATA, __FUNCTION__);
380     auto persistentDataCtl = persistentDataCtl_;
381     if (persistentDataCtl == nullptr) {
382         LOG_ERROR("persistentDataCtl is nullptr");
383         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
384         return std::vector<OperationResult>();
385     }
386     return persistentDataCtl->UnSubscribeRdbData(this, uris, templateId);
387 }
388 
EnableRdbSubs(const std::vector<std::string> & uris,const TemplateId & templateId)389 std::vector<OperationResult> DataShareHelperImpl::EnableRdbSubs(const std::vector<std::string> &uris,
390     const TemplateId &templateId)
391 {
392     LOG_DEBUG("Start EnableSubscribeRdbData");
393     auto persistentDataCtl = persistentDataCtl_;
394     if (persistentDataCtl == nullptr) {
395         LOG_ERROR("persistentDataCtl is nullptr");
396         return std::vector<OperationResult>();
397     }
398     return persistentDataCtl->EnableSubscribeRdbData(this, uris, templateId);
399 }
400 
DisableRdbSubs(const std::vector<std::string> & uris,const TemplateId & templateId)401 std::vector<OperationResult> DataShareHelperImpl::DisableRdbSubs(const std::vector<std::string> &uris,
402     const TemplateId &templateId)
403 {
404     LOG_DEBUG("Start DisableSubscribeRdbData");
405     auto persistentDataCtl = persistentDataCtl_;
406     if (persistentDataCtl == nullptr) {
407         LOG_ERROR("persistentDataCtl is nullptr");
408         return std::vector<OperationResult>();
409     }
410     return persistentDataCtl->DisableSubscribeRdbData(this, uris, templateId);
411 }
412 
SubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId,const std::function<void (const PublishedDataChangeNode & changeNode)> & callback)413 std::vector<OperationResult> DataShareHelperImpl::SubscribePublishedData(const std::vector<std::string> &uris,
414     int64_t subscriberId, const std::function<void(const PublishedDataChangeNode &changeNode)> &callback)
415 {
416     LOG_DEBUG("Start SubscribePublishedData");
417     RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
418         RadarReporter::SUBSCRIBE_PUBLISHED_DATA, __FUNCTION__);
419     auto publishedDataCtl = publishedDataCtl_;
420     if (publishedDataCtl == nullptr) {
421         LOG_ERROR("publishedDataCtl is nullptr");
422         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
423         return std::vector<OperationResult>();
424     }
425     return publishedDataCtl->SubscribePublishedData(this, uris, subscriberId, callback);
426 }
427 
UnsubscribePublishedData(const std::vector<std::string> & uris,int64_t subscriberId)428 std::vector<OperationResult> DataShareHelperImpl::UnsubscribePublishedData(const std::vector<std::string> &uris,
429     int64_t subscriberId)
430 {
431     LOG_DEBUG("Start UnSubscribePublishedData");
432     RadarReporter::RadarReport report(RadarReporter::TEMPLATE_DATA_MANAGER,
433         RadarReporter::UNSUBSCRIBE_PUBLISHED_DATA, __FUNCTION__);
434     auto publishedDataCtl = publishedDataCtl_;
435     if (publishedDataCtl == nullptr) {
436         LOG_ERROR("publishedDataCtl is nullptr");
437         report.SetError(RadarReporter::DATA_SHARE_DIED_ERROR);
438         return std::vector<OperationResult>();
439     }
440     return publishedDataCtl->UnSubscribePublishedData(this, uris, subscriberId);
441 }
442 
EnablePubSubs(const std::vector<std::string> & uris,int64_t subscriberId)443 std::vector<OperationResult> DataShareHelperImpl::EnablePubSubs(const std::vector<std::string> &uris,
444     int64_t subscriberId)
445 {
446     LOG_DEBUG("Start enablePubSubs");
447     auto publishedDataCtl = publishedDataCtl_;
448     if (publishedDataCtl == nullptr) {
449         LOG_ERROR("publishedDataCtl is nullptr");
450         return std::vector<OperationResult>();
451     }
452     return publishedDataCtl->EnableSubscribePublishedData(this, uris, subscriberId);
453 }
454 
DisablePubSubs(const std::vector<std::string> & uris,int64_t subscriberId)455 std::vector<OperationResult> DataShareHelperImpl::DisablePubSubs(const std::vector<std::string> &uris,
456     int64_t subscriberId)
457 {
458     LOG_DEBUG("Start disablePubSubs");
459     auto publishedDataCtl = publishedDataCtl_;
460     if (publishedDataCtl == nullptr) {
461         LOG_ERROR("publishedDataCtl is nullptr");
462         return std::vector<OperationResult>();
463     }
464     return publishedDataCtl->DisableSubscribePublishedData(this, uris, subscriberId);
465 }
466 } // namespace DataShare
467 } // namespace OHOS