• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_ability_helper.h"
17 #include "abs_shared_result_set.h"
18 #include "datashare_helper.h"
19 #include "hilog_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "rdb_data_ability_utils.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 using namespace OHOS::RdbDataAbilityAdapter;
DataAbilityHelper(const std::shared_ptr<DataAbilityHelperImpl> & helperImpl)26 DataAbilityHelper::DataAbilityHelper(const std::shared_ptr<DataAbilityHelperImpl> &helperImpl)
27 {
28     dataAbilityHelperImpl_ = helperImpl;
29 }
30 
DataAbilityHelper(const std::shared_ptr<DataShare::DataShareHelper> & dataShareHelper)31 DataAbilityHelper::DataAbilityHelper(const std::shared_ptr<DataShare::DataShareHelper> &dataShareHelper)
32 {
33     dataShareHelper_ = dataShareHelper;
34 }
35 
36 /**
37  * @brief Creates a DataAbilityHelper instance without specifying the Uri based on the given Context.
38  *
39  * @param context Indicates the Context object on OHOS.
40  *
41  * @return Returns the created DataAbilityHelper instance where Uri is not specified.
42  */
Creator(const std::shared_ptr<Context> context)43 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(const std::shared_ptr<Context> context)
44 {
45     HILOG_INFO("Call DataAbilityHelperImpl Creator with context.");
46     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
47     std::shared_ptr<DataAbilityHelperImpl> dataAbilityHelperImpl = DataAbilityHelperImpl::Creator(context);
48     if (dataAbilityHelperImpl) {
49         ptrDataAbilityHelper = new DataAbilityHelper(dataAbilityHelperImpl);
50     }
51     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
52 }
53 
54 /**
55  * @brief Creates a DataAbilityHelper instance with the Uri specified based on the given Context.
56  *
57  * @param context Indicates the Context object on OHOS.
58  * @param uri Indicates the database table or disk file to operate.
59  *
60  * @return Returns the created DataAbilityHelper instance with a specified Uri.
61  */
Creator(const std::shared_ptr<Context> context,const std::shared_ptr<Uri> & uri)62 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
63     const std::shared_ptr<Context> context, const std::shared_ptr<Uri> &uri)
64 {
65     HILOG_INFO("Call DataAbilityHelperImpl Creator with context & uri.");
66     if (!context || !uri) {
67         HILOG_ERROR("Input param invalid, context or uri is nullptr.");
68         return nullptr;
69     }
70     auto sharedPtrDataAbilityHelper = DataAbilityHelper::Creator(context, uri, false);
71     if (sharedPtrDataAbilityHelper) {
72         return sharedPtrDataAbilityHelper;
73     }
74 
75     HILOG_INFO("Call DataAbilityHelperImpl Creator failed, Call DataShareHelper Creator with context & uri.");
76     Uri dataShareUri("");
77     if (!DataAbilityHelper::TransferScheme(*uri, dataShareUri)) {
78         return nullptr;
79     }
80     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
81     auto dataShareHelper = DataShare::DataShareHelper::Creator(context, dataShareUri.ToString());
82     if (dataShareHelper) {
83         ptrDataAbilityHelper = new DataAbilityHelper(dataShareHelper);
84     }
85     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
86 }
87 
88 /**
89  * @brief Creates a DataAbilityHelper instance with the Uri specified based on the given Context.
90  *
91  * @param context Indicates the Context object on OHOS.
92  * @param uri Indicates the database table or disk file to operate.
93  *
94  * @return Returns the created DataAbilityHelper instance with a specified Uri.
95  */
Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,const std::shared_ptr<Uri> & uri)96 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
97     const std::shared_ptr<OHOS::AbilityRuntime::Context> context, const std::shared_ptr<Uri> &uri)
98 {
99     HILOG_INFO("Call DataAbilityHelperImpl Creator with ability runtime context & uri.");
100     if (!context || !uri) {
101         HILOG_ERROR("Input param invalid, ability runtime context or uri is nullptr.");
102         return nullptr;
103     }
104     auto sharedPtrDataAbilityHelper = DataAbilityHelper::Creator(context, uri, false);
105     if (sharedPtrDataAbilityHelper) {
106         return sharedPtrDataAbilityHelper;
107     }
108 
109     HILOG_INFO("Call DataAbilityHelperImpl Creator failed, "
110         "Call DataShareHelper Creator with ability runtime context & uri.");
111     Uri dataShareUri("");
112     if (!DataAbilityHelper::TransferScheme(*uri, dataShareUri)) {
113         return nullptr;
114     }
115     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
116     auto dataShareHelper = DataShare::DataShareHelper::Creator(context, dataShareUri.ToString());
117     if (dataShareHelper) {
118         ptrDataAbilityHelper = new DataAbilityHelper(dataShareHelper);
119     }
120     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
121 }
122 
123 /**
124  * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
125  * between the ability using the Data template (Data ability for short) and the associated client process in
126  * a DataAbilityHelper instance.
127  *
128  * @param context Indicates the Context object on OHOS.
129  * @param uri Indicates the database table or disk file to operate.
130  * @param tryBind Specifies whether the exit of the corresponding Data ability process causes the exit of the
131  * client process.
132  *
133  * @return Returns the created DataAbilityHelper instance.
134  */
Creator(const std::shared_ptr<Context> context,const std::shared_ptr<Uri> & uri,const bool tryBind)135 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
136     const std::shared_ptr<Context> context, const std::shared_ptr<Uri> &uri, const bool tryBind)
137 {
138     HILOG_INFO("Call DataAbilityHelperImpl Creator with context & uri & tryBind.");
139     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
140     auto dataAbilityHelperImpl = DataAbilityHelperImpl::Creator(context, uri, tryBind);
141     if (dataAbilityHelperImpl) {
142         ptrDataAbilityHelper = new DataAbilityHelper(dataAbilityHelperImpl);
143     }
144     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
145 }
146 
147 /**
148  * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
149  * between the ability using the Data template (Data ability for short) and the associated client process in
150  * a DataAbilityHelper instance.
151  *
152  * @param context Indicates the Context object on OHOS.
153  * @param uri Indicates the database table or disk file to operate.
154  * @param tryBind Specifies whether the exit of the corresponding Data ability process causes the exit of the
155  * client process.
156  *
157  * @return Returns the created DataAbilityHelper instance.
158  */
Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,const std::shared_ptr<Uri> & uri,const bool tryBind)159 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
160     const std::shared_ptr<OHOS::AbilityRuntime::Context> context, const std::shared_ptr<Uri> &uri, const bool tryBind)
161 {
162     HILOG_INFO("Call DataAbilityHelperImpl Creator with ability runtime context & uri & tryBind.");
163     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
164     auto dataAbilityHelperImpl = DataAbilityHelperImpl::Creator(context, uri, tryBind);
165     if (dataAbilityHelperImpl) {
166         ptrDataAbilityHelper = new DataAbilityHelper(dataAbilityHelperImpl);
167     }
168     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
169 }
170 
171 /**
172  * @brief Creates a DataAbilityHelper instance without specifying the Uri based.
173  *
174  * @param token Indicates the System token.
175  *
176  * @return Returns the created DataAbilityHelper instance where Uri is not specified.
177  */
Creator(const sptr<IRemoteObject> token)178 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(const sptr<IRemoteObject> token)
179 {
180     HILOG_INFO("Call DataAbilityHelperImpl Creator with token.");
181     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
182     auto dataAbilityHelperImpl = DataAbilityHelperImpl::Creator(token);
183     if (dataAbilityHelperImpl) {
184         ptrDataAbilityHelper = new DataAbilityHelper(dataAbilityHelperImpl);
185     }
186     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
187 }
188 
189 /**
190  * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
191  * between the ability using the Data template (Data ability for short) and the associated client process in
192  * a DataAbilityHelper instance.
193  *
194  * @param token Indicates the System token.
195  * @param uri Indicates the database table or disk file to operate.
196  *
197  * @return Returns the created DataAbilityHelper instance.
198  */
Creator(const sptr<IRemoteObject> token,const std::shared_ptr<Uri> & uri)199 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
200     const sptr<IRemoteObject> token, const std::shared_ptr<Uri> &uri)
201 {
202     HILOG_INFO("Call DataAbilityHelperImpl Creator with token & uri.");
203     if (!token || !uri) {
204         HILOG_ERROR("Input param invalid, token or uri is nullptr.");
205         return nullptr;
206     }
207     DataAbilityHelper *ptrDataAbilityHelper = nullptr;
208     auto dataAbilityHelperImpl = DataAbilityHelperImpl::Creator(token, uri);
209     if (dataAbilityHelperImpl) {
210         ptrDataAbilityHelper = new DataAbilityHelper(dataAbilityHelperImpl);
211     } else {
212         HILOG_INFO("Call DataAbilityHelperImpl Creator failed, Call DataShareHelper Creator with token & uri.");
213         Uri dataShareUri("");
214         if (!DataAbilityHelper::TransferScheme(*uri, dataShareUri)) {
215             return nullptr;
216         }
217         auto dataShareHelper = DataShare::DataShareHelper::Creator(token, dataShareUri.ToString());
218         if (dataShareHelper) {
219             ptrDataAbilityHelper = new DataAbilityHelper(dataShareHelper);
220         }
221     }
222     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
223 }
224 
225 /**
226  * @brief Releases the client resource of the Data ability.
227  * You should call this method to releases client resource after the data operations are complete.
228  *
229  * @return Returns true if the resource is successfully released; returns false otherwise.
230  */
Release()231 bool DataAbilityHelper::Release()
232 {
233     HILOG_INFO("Release called.");
234     bool ret = false;
235     if (dataAbilityHelperImpl_) {
236         HILOG_INFO("Call DataAbilityHelperImpl Release.");
237         ret = dataAbilityHelperImpl_->Release();
238     }
239     if (dataShareHelper_) {
240         HILOG_INFO("Call DataShareHelper Release.");
241         ret = dataShareHelper_->Release();
242         dataShareHelper_.reset();
243     }
244     return ret;
245 }
246 
247 /**
248  * @brief Obtains the MIME types of files supported.
249  *
250  * @param uri Indicates the path of the files to obtain.
251  * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
252  *
253  * @return Returns the matched MIME types. If there is no match, null is returned.
254  */
GetFileTypes(Uri & uri,const std::string & mimeTypeFilter)255 std::vector<std::string> DataAbilityHelper::GetFileTypes(Uri &uri, const std::string &mimeTypeFilter)
256 {
257     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
258     HILOG_INFO("GetFileTypes called.");
259     std::vector<std::string> matchedMIMEs;
260     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
261     if (dataAbilityHelperImpl) {
262         HILOG_INFO("Call DataAbilityHelperImpl GetFileTypes.");
263         matchedMIMEs = dataAbilityHelperImpl->GetFileTypes(uri, mimeTypeFilter);
264     }
265     auto dataShareHelper = GetDataShareHelper();
266     if (dataShareHelper) {
267         HILOG_INFO("Call DataShareHelper GetFileTypes.");
268         Uri dataShareUri("");
269         if (TransferScheme(uri, dataShareUri)) {
270             matchedMIMEs = dataShareHelper->GetFileTypes(dataShareUri, mimeTypeFilter);
271         }
272     }
273     return matchedMIMEs;
274 }
275 
276 /**
277  * @brief Opens a file in a specified remote path.
278  *
279  * @param uri Indicates the path of the file to open.
280  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
281  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
282  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
283  *  or "rwt" for read and write access that truncates any existing file.
284  *
285  * @return Returns the file descriptor.
286  */
OpenFile(Uri & uri,const std::string & mode)287 int DataAbilityHelper::OpenFile(Uri &uri, const std::string &mode)
288 {
289     HILOG_INFO("OpenFile Called.");
290     int fd = -1;
291     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
292     if (dataAbilityHelperImpl) {
293         HILOG_INFO("Call DataAbilityHelperImpl OpenFile.");
294         fd = dataAbilityHelperImpl->OpenFile(uri, mode);
295     }
296     auto dataShareHelper = GetDataShareHelper();
297     if (dataShareHelper) {
298         if (callFromJs_) {
299             HILOG_ERROR("Call DataShareHelper OpenFile, DataShareHelper no this interface.");
300         } else {
301             HILOG_INFO("Call DataShareHelper OpenFile.");
302             Uri dataShareUri("");
303             if (TransferScheme(uri, dataShareUri)) {
304                 fd = dataShareHelper->OpenFile(dataShareUri, mode);
305             }
306         }
307     }
308     return fd;
309 }
310 
311 /**
312  * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
313  * inside of their .hap.
314  *
315  * @param uri Indicates the path of the file to open.
316  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
317  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
318  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
319  * data, or "rwt" for read and write access that truncates any existing file.
320  *
321  * @return Returns the RawFileDescriptor object containing file descriptor.
322  */
OpenRawFile(Uri & uri,const std::string & mode)323 int DataAbilityHelper::OpenRawFile(Uri &uri, const std::string &mode)
324 {
325     HILOG_INFO("OpenRawFile called.");
326     int fd = -1;
327     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
328     if (dataAbilityHelperImpl) {
329         HILOG_INFO("Call DataAbilityHelperImpl OpenRawFile.");
330         fd = dataAbilityHelperImpl->OpenRawFile(uri, mode);
331     }
332     auto dataShareHelper = GetDataShareHelper();
333     if (dataShareHelper) {
334         HILOG_INFO("Call DataShareHelper OpenFile.");
335         Uri dataShareUri("");
336         if (TransferScheme(uri, dataShareUri)) {
337             fd = dataShareHelper->OpenRawFile(dataShareUri, mode);
338         }
339     }
340     return fd;
341 }
342 
343 /**
344  * @brief Inserts a single data record into the database.
345  *
346  * @param uri Indicates the path of the data to operate.
347  * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
348  *
349  * @return Returns the index of the inserted data record.
350  */
Insert(Uri & uri,const NativeRdb::ValuesBucket & value)351 int DataAbilityHelper::Insert(Uri &uri, const NativeRdb::ValuesBucket &value)
352 {
353     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
354     HILOG_INFO("Insert called.");
355     int index = -1;
356     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
357     if (dataAbilityHelperImpl) {
358         HILOG_INFO("Call DataAbilityHelperImpl Insert.");
359         index = dataAbilityHelperImpl->Insert(uri, value);
360     }
361     auto dataShareHelper = GetDataShareHelper();
362     if (dataShareHelper) {
363         HILOG_INFO("Call DataShareHelper Insert.");
364         DataShare::DataShareValuesBucket dataShareValue = RdbDataAbilityUtils::ToDataShareValuesBucket(value);
365         Uri dataShareUri("");
366         if (TransferScheme(uri, dataShareUri)) {
367             index = dataShareHelper->Insert(dataShareUri, dataShareValue);
368         }
369     }
370     return index;
371 }
372 
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)373 std::shared_ptr<AppExecFwk::PacMap> DataAbilityHelper::Call(
374     const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap)
375 {
376     std::shared_ptr<AppExecFwk::PacMap> result = nullptr;
377     HILOG_INFO("Call called.");
378     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
379     if (dataAbilityHelperImpl) {
380         HILOG_INFO("Call DataAbilityHelperImpl Call.");
381         result = dataAbilityHelperImpl->Call(uri, method, arg, pacMap);
382     }
383     if (dataShareHelper_) {
384         HILOG_ERROR("Call DataShareHelper Call, DataShareHelper no this interface.");
385     }
386     return result;
387 }
388 
389 /**
390  * @brief Updates data records in the database.
391  *
392  * @param uri Indicates the path of data to update.
393  * @param value Indicates the data to update. This parameter can be null.
394  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
395  *
396  * @return Returns the number of data records updated.
397  */
Update(Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)398 int DataAbilityHelper::Update(
399     Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
400 {
401     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
402     HILOG_INFO("Update called.");
403     int index = -1;
404     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
405     if (dataAbilityHelperImpl) {
406         HILOG_INFO("Call DataAbilityHelperImpl Update.");
407         index = dataAbilityHelperImpl->Update(uri, value, predicates);
408     }
409     auto dataShareHelper = GetDataShareHelper();
410     if (dataShareHelper) {
411         HILOG_INFO("Call DataShareHelper Update.");
412         DataShare::DataShareValuesBucket dataShareValue = RdbDataAbilityUtils::ToDataShareValuesBucket(value);
413         DataShare::DataSharePredicates dataSharePredicates = RdbDataAbilityUtils::ToDataSharePredicates(predicates);
414         Uri dataShareUri("");
415         if (TransferScheme(uri, dataShareUri)) {
416             index = dataShareHelper->Update(dataShareUri, dataSharePredicates, dataShareValue);
417         }
418     }
419     return index;
420 }
421 
422 /**
423  * @brief Deletes one or more data records from the database.
424  *
425  * @param uri Indicates the path of the data to operate.
426  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
427  *
428  * @return Returns the number of data records deleted.
429  */
Delete(Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)430 int DataAbilityHelper::Delete(Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
431 {
432     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
433     HILOG_INFO("Delete called.");
434     int index = -1;
435     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
436     if (dataAbilityHelperImpl) {
437         HILOG_INFO("Call DataAbilityHelperImpl Delete.");
438         return dataAbilityHelperImpl->Delete(uri, predicates);
439     }
440     auto dataShareHelper = GetDataShareHelper();
441     if (dataShareHelper) {
442         HILOG_INFO("Call DataShareHelper Delete.");
443         DataShare::DataSharePredicates dataSharePredicates = RdbDataAbilityUtils::ToDataSharePredicates(predicates);
444         Uri dataShareUri("");
445         if (TransferScheme(uri, dataShareUri)) {
446             index = dataShareHelper->Delete(dataShareUri, dataSharePredicates);
447         }
448     }
449     return index;
450 }
451 
452 /**
453  * @brief Deletes one or more data records from the database.
454  *
455  * @param uri Indicates the path of data to query.
456  * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
457  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
458  *
459  * @return Returns the query result.
460  */
Query(Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)461 std::shared_ptr<NativeRdb::AbsSharedResultSet> DataAbilityHelper::Query(
462     Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
463 {
464     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
465     HILOG_INFO("Query called.");
466     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
467     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
468     if (dataAbilityHelperImpl) {
469         HILOG_INFO("Call DataAbilityHelperImpl Query.");
470         resultSet = dataAbilityHelperImpl->Query(uri, columns, predicates);
471     }
472     auto dataShareHelper = GetDataShareHelper();
473     if (dataShareHelper) {
474         HILOG_INFO("Call DataShareHelper Query.");
475         DataShare::DataSharePredicates dataSharePredicates = RdbDataAbilityUtils::ToDataSharePredicates(predicates);
476         Uri dataShareUri("");
477         if (TransferScheme(uri, dataShareUri)) {
478             std::shared_ptr<DataShare::DataShareResultSet> dataShareResultSet
479                 = dataShareHelper->Query(dataShareUri, dataSharePredicates, columns);
480             if (!dataShareResultSet) {
481                 HILOG_ERROR("Return dataShareResultSet is nullptr.");
482                 return nullptr;
483             }
484             resultSet = RdbDataAbilityUtils::ToAbsSharedResultSet(dataShareResultSet);
485             if (!resultSet) {
486                 HILOG_ERROR("Transfer to AbsSharedResultSet failed.");
487             }
488         }
489     }
490     return resultSet;
491 }
492 
493 /**
494  * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
495  * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
496  *
497  * @param uri Indicates the URI of the data.
498  *
499  * @return Returns the MIME type that matches the data specified by uri.
500  */
GetType(Uri & uri)501 std::string DataAbilityHelper::GetType(Uri &uri)
502 {
503     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
504     HILOG_INFO("GetType called.");
505     std::string type;
506     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
507     if (dataAbilityHelperImpl) {
508         HILOG_INFO("Call DataAbilityHelperImpl GetType.");
509         type = dataAbilityHelperImpl->GetType(uri);
510     }
511     auto dataShareHelper = GetDataShareHelper();
512     if (dataShareHelper) {
513         HILOG_INFO("Call DataShareHelper GetType.");
514         Uri dataShareUri("");
515         if (TransferScheme(uri, dataShareUri)) {
516             type = dataShareHelper->GetType(dataShareUri);
517         }
518     }
519     return type;
520 }
521 
522 /**
523  * @brief Reloads data in the database.
524  *
525  * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
526  * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
527  * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
528  * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
529  *
530  * @return Returns true if the data is successfully reloaded; returns false otherwise.
531  */
Reload(Uri & uri,const PacMap & extras)532 bool DataAbilityHelper::Reload(Uri &uri, const PacMap &extras)
533 {
534     HILOG_INFO("Reload called.");
535     bool ret = false;
536     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
537     if (dataAbilityHelperImpl) {
538         HILOG_INFO("Call DataAbilityHelperImpl Reload.");
539         ret = dataAbilityHelperImpl->Reload(uri, extras);
540     }
541     if (dataShareHelper_) {
542         HILOG_ERROR("Call DataShareHelper Reload, DataShareHelper no this interface.");
543     }
544     return ret;
545 }
546 
547 /**
548  * @brief Inserts multiple data records into the database.
549  *
550  * @param uri Indicates the path of the data to operate.
551  * @param values Indicates the data records to insert.
552  *
553  * @return Returns the number of data records inserted.
554  */
BatchInsert(Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)555 int DataAbilityHelper::BatchInsert(Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
556 {
557     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
558     HILOG_INFO("BatchInsert called.");
559     int ret = -1;
560     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
561     if (dataAbilityHelperImpl) {
562         HILOG_INFO("Call DataAbilityHelperImpl BatchInsert.");
563         ret = dataAbilityHelperImpl->BatchInsert(uri, values);
564     }
565     auto dataShareHelper = GetDataShareHelper();
566     if (dataShareHelper) {
567         HILOG_INFO("Call DataShareHelper BatchInsert.");
568         std::vector<DataShare::DataShareValuesBucket> dataShareValues;
569         for (auto value : values) {
570             DataShare::DataShareValuesBucket dataShareValue = RdbDataAbilityUtils::ToDataShareValuesBucket(value);
571             dataShareValues.push_back(dataShareValue);
572         }
573         Uri dataShareUri("");
574         if (TransferScheme(uri, dataShareUri)) {
575             ret = dataShareHelper->BatchInsert(dataShareUri, dataShareValues);
576         }
577     }
578     return ret;
579 }
580 
581 /**
582  * @brief Registers an observer to DataObsMgr specified by the given Uri.
583  *
584  * @param uri, Indicates the path of the data to operate.
585  * @param dataObserver, Indicates the IDataAbilityObserver object.
586  */
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)587 void DataAbilityHelper::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
588 {
589     HILOG_INFO("RegisterObserver called.");
590     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
591     if (dataAbilityHelperImpl) {
592         HILOG_INFO("Call DataAbilityHelperImpl RegisterObserver.");
593         dataAbilityHelperImpl->RegisterObserver(uri, dataObserver);
594     }
595     auto dataShareHelper = GetDataShareHelper();
596     if (dataShareHelper) {
597         HILOG_INFO("Call DataShareHelper RegisterObserver.");
598         Uri dataShareUri("");
599         if (TransferScheme(uri, dataShareUri)) {
600             dataShareHelper->RegisterObserver(dataShareUri, dataObserver);
601         }
602     }
603 }
604 
605 /**
606  * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
607  *
608  * @param uri, Indicates the path of the data to operate.
609  * @param dataObserver, Indicates the IDataAbilityObserver object.
610  */
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)611 void DataAbilityHelper::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
612 {
613     HILOG_INFO("UnregisterObserver called.");
614     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
615     if (dataAbilityHelperImpl) {
616         HILOG_INFO("Call DataAbilityHelperImpl UnregisterObserver.");
617         dataAbilityHelperImpl->UnregisterObserver(uri, dataObserver);
618     }
619     auto dataShareHelper = GetDataShareHelper();
620     if (dataShareHelper) {
621         HILOG_INFO("Call DataShareHelper UnregisterObserver.");
622         Uri dataShareUri("");
623         if (TransferScheme(uri, dataShareUri)) {
624             dataShareHelper->UnregisterObserver(dataShareUri, dataObserver);
625         }
626     }
627 }
628 
629 /**
630  * @brief Notifies the registered observers of a change to the data resource specified by Uri.
631  *
632  * @param uri, Indicates the path of the data to operate.
633  */
NotifyChange(const Uri & uri)634 void DataAbilityHelper::NotifyChange(const Uri &uri)
635 {
636     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
637     HILOG_INFO("NotifyChange called.");
638     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
639     if (dataAbilityHelperImpl) {
640         HILOG_INFO("Call DataAbilityHelperImpl NotifyChange.");
641         dataAbilityHelperImpl->NotifyChange(uri);
642     }
643     auto dataShareHelper = GetDataShareHelper();
644     if (dataShareHelper) {
645         HILOG_INFO("Call DataShareHelper NotifyChange.");
646         Uri dataShareUri("");
647         if (TransferScheme(uri, dataShareUri)) {
648             dataShareHelper->NotifyChange(dataShareUri);
649         }
650     }
651 }
652 
653 /**
654  * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
655  * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
656  * context has changed. If you implement URI normalization for a Data ability, you must also implement
657  * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
658  * method that is called on the Data ability must require normalization verification and denormalization. The default
659  * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
660  *
661  * @param uri Indicates the Uri object to normalize.
662  *
663  * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
664  */
NormalizeUri(Uri & uri)665 Uri DataAbilityHelper::NormalizeUri(Uri &uri)
666 {
667     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
668     HILOG_INFO("NormalizeUri called.");
669     Uri urivalue("");
670     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
671     if (dataAbilityHelperImpl) {
672         HILOG_INFO("Call DataAbilityHelperImpl NormalizeUri.");
673         urivalue = dataAbilityHelperImpl->NormalizeUri(uri);
674     }
675     auto dataShareHelper = GetDataShareHelper();
676     if (dataShareHelper) {
677         HILOG_INFO("Call DataShareHelper NormalizeUri.");
678         Uri dataShareUri("");
679         if (TransferScheme(uri, dataShareUri)) {
680             urivalue = dataShareHelper->NormalizeUri(dataShareUri);
681         }
682     }
683     return urivalue;
684 }
685 
686 /**
687  * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
688  * The default implementation of this method returns the original URI passed to it.
689  *
690  * @param uri uri Indicates the Uri object to denormalize.
691  *
692  * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed to
693  * this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found in the
694  * current environment.
695  */
DenormalizeUri(Uri & uri)696 Uri DataAbilityHelper::DenormalizeUri(Uri &uri)
697 {
698     HITRACE_METER_NAME(HITRACE_TAG_DISTRIBUTEDDATA, __PRETTY_FUNCTION__);
699     HILOG_INFO("DenormalizeUri called.");
700     Uri urivalue("");
701     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
702     if (dataAbilityHelperImpl) {
703         HILOG_INFO("Call DataAbilityHelperImpl DenormalizeUri.");
704         urivalue = dataAbilityHelperImpl->DenormalizeUri(uri);
705     }
706     auto dataShareHelper = GetDataShareHelper();
707     if (dataShareHelper) {
708         HILOG_INFO("Call DataShareHelper DenormalizeUri.");
709         Uri dataShareUri("");
710         if (TransferScheme(uri, dataShareUri)) {
711             urivalue = dataShareHelper->DenormalizeUri(dataShareUri);
712         }
713     }
714     return urivalue;
715 }
716 
ExecuteBatch(const Uri & uri,const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)717 std::vector<std::shared_ptr<DataAbilityResult>> DataAbilityHelper::ExecuteBatch(
718     const Uri &uri, const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
719 {
720     HILOG_INFO("ExecuteBatch called.");
721     std::vector<std::shared_ptr<DataAbilityResult>> results;
722     auto dataAbilityHelperImpl = GetDataAbilityHelperImpl();
723     if (dataAbilityHelperImpl) {
724         HILOG_INFO("Call DataAbilityHelperImpl ExecuteBatch.");
725         results = dataAbilityHelperImpl->ExecuteBatch(uri, operations);
726     }
727     if (dataShareHelper_) {
728         HILOG_ERROR("Call DataShareHelper ExecuteBatch, DataShareHelper no this interface.");
729     }
730     return results;
731 }
732 
TransferScheme(const Uri & uri,Uri & dataShareUri)733 bool DataAbilityHelper::TransferScheme(const Uri &uri, Uri &dataShareUri)
734 {
735     const std::string dataAbilityScheme = "dataability";
736     const std::string dataShareScheme = "datashare";
737 
738     Uri inputUri = uri;
739     if (inputUri.GetScheme() == dataShareScheme) {
740         dataShareUri = Uri(inputUri.ToString());
741         HILOG_INFO("input uri is data share uri, uri: %{public}s, no need transfer.", inputUri.ToString().c_str());
742         return true;
743     }
744 
745     if (inputUri.GetScheme() == dataAbilityScheme) {
746         string uriStr = inputUri.ToString();
747         uriStr.replace(0, dataAbilityScheme.length(), dataShareScheme);
748         dataShareUri = Uri(uriStr);
749         HILOG_INFO("data ability uri: %{public}s transfer to data share uri: %{public}s.",
750             inputUri.ToString().c_str(), dataShareUri.ToString().c_str());
751         return true;
752     }
753 
754     HILOG_ERROR("Input param invalid, uri: %{private}s", inputUri.ToString().c_str());
755     return false;
756 }
757 
SetCallFromJs()758 void DataAbilityHelper::SetCallFromJs()
759 {
760     callFromJs_ = true;
761 }
762 }  // namespace AppExecFwk
763 }  // namespace OHOS
764 
765