• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef DATASHARE_HELPER_H
17 #define DATASHARE_HELPER_H
18 
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 
25 #include "data_ability_observer_interface.h"
26 #include "datashare_business_error.h"
27 #include "datashare_errno.h"
28 #include "datashare_observer.h"
29 #include "datashare_operation_statement.h"
30 #include "datashare_predicates.h"
31 #include "datashare_result_set.h"
32 #include "datashare_template.h"
33 #include "datashare_values_bucket.h"
34 #include "uri.h"
35 
36 using Uri = OHOS::Uri;
37 
38 namespace OHOS {
39 namespace AppExecFwk {
40 class PacMap;
41 class IDataAbilityObserver;
42 } // namespace AppExecFwk
43 
44 namespace DataShare {
45 using string = std::string;
46 class DataShareHelper : public std::enable_shared_from_this<DataShareHelper> {
47 public:
48     /**
49      * @brief Destructor.
50      */
51     virtual ~DataShareHelper() = default;
52 
53     /**
54      * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
55      * between the ability using the Data template (data share for short) and the associated client process in
56      * a DataShareHelper instance.
57      *
58      * @param token Indicates the System token.
59      * @param strUri Indicates the database table or disk file to operate.
60      * @param waitTime connect extension waiting time.
61      *
62      * @return Returns the created DataShareHelper instance.
63      */
64     [[deprecated(
65         "Use Create(const sptr<IRemoteObject> &, const std::string &, const std::string &, const int &) instead.")]]
66     static std::shared_ptr<DataShareHelper> Creator(const sptr<IRemoteObject> &token,
67         const std::string &strUri, const std::string &extUri = "", const int waitTime = 2);
68 
69     /**
70      * @brief Creates a DataShareHelper instance with the Uri and {@link #CreateOptions} .
71      *
72      * @param strUri Indicates the database table or disk file to operate.
73      * @param options Indicates the optional config.
74      * @param waitTime connect extension waiting time.
75      *
76      * @return Returns the created DataShareHelper instance with a specified Uri.
77      */
78     [[deprecated(
79         "Use Create(const sptr<IRemoteObject> &, const std::string &,const std::string &, const int &) instead.")]]
80     static std::shared_ptr<DataShareHelper> Creator(const std::string &strUri, const CreateOptions &options,
81         const std::string &bundleName = "", const int waitTime = 2);
82 
83     /**
84      * @brief Creates a DataShareHelper instance, priority silent access, use non-silent access when silent is not
85      * available, at this time, it is necessary to apply for communication permission with the extension.
86      *
87      * @param token Indicates the System token.
88      * @param strUri Indicates the database table or disk file to operate for silent access.
89      * @param extUri Indicates the database table or disk file to operate for non silent access.
90      * @param waitTime connect extension waiting time.
91      *
92      * @return Returns the created DataShareHelper instance with a specified Uri.
93      */
94     static std::pair<int, std::shared_ptr<DataShareHelper>> Create(const sptr<IRemoteObject> &token,
95         const std::string &strUri, const std::string &extUri, const int waitTime = 2);
96 
97     /**
98      * @brief Releases the client resource of the Data share.
99      * You should call this method to releases client resource after the data operations are complete.
100      *
101      * @return Returns true if the resource is successfully released; returns false otherwise.
102      */
103     virtual bool Release() = 0;
104 
105     /**
106      * @brief Obtains the MIME types of files supported.
107      *
108      * @param uri Indicates the path of the files to obtain.
109      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
110      *
111      * @return Returns the matched MIME types. If there is no match, null is returned.
112      */
113     virtual std::vector<std::string> GetFileTypes(Uri &uri, const std::string &mimeTypeFilter) = 0;
114 
115     /**
116      * @brief Opens a file in a specified remote path.
117      *
118      * @param uri Indicates the path of the file to open.
119      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
120      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
121      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
122      *  or "rwt" for read and write access that truncates any existing file.
123      *
124      * @return Returns the file descriptor.
125      */
126     virtual int OpenFile(Uri &uri, const std::string &mode) = 0;
127 
128     /**
129      * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
130      * inside of their .hap.
131      *
132      * @param uri Indicates the path of the file to open.
133      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
134      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
135      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
136      * data, or "rwt" for read and write access that truncates any existing file.
137      *
138      * @return Returns the RawFileDescriptor object containing file descriptor.
139      */
140     virtual int OpenRawFile(Uri &uri, const std::string &mode) = 0;
141 
142     /**
143      * @brief Inserts a single data record into the database.
144      *
145      * @param uri Indicates the path of the data to operate.
146      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
147      *
148      * @return Returns the index of the inserted data record.
149      */
150     [[deprecated("Use InsertEx(Uri &, const DataShareValuesBucket &) instead.")]]
151     virtual int Insert(Uri &uri, const DataShareValuesBucket &value) = 0;
152 
153     /**
154      * @brief Inserts a single data record into the database.
155      *
156      * @param uri Indicates the path of the data to operate.
157      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
158      * @param result Indicates the result string of the insert operation.
159      *
160      * @return Returns the index of the inserted data record.
161      */
162     virtual int InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result) = 0;
163 
164     /**
165      * @brief Updates data records in the database.
166      *
167      * @param uri Indicates the path of data to update.
168      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
169      * @param value Indicates the data to update. This parameter can be null.
170      *
171      * @return Returns the number of data records updated.
172      */
173     [[deprecated("Use UpdateEx(Uri &, const DataSharePredicates &, const DataShareValuesBucket &) instead.")]]
174     virtual int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) = 0;
175 
176     /**
177      * @brief Batch updates data records in the database.
178      *
179      * @param updateOperations Indicates the param of data to update.
180      * @param results Indicates the number of data records updated.
181      *
182      * @return Return the execution results of batch updates.
183      */
184     virtual int BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results) = 0;
185 
186     /**
187      * @brief Deletes one or more data records from the database.
188      *
189      * @param uri Indicates the path of the data to operate.
190      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
191      *
192      * @return Returns the number of data records deleted.
193      */
194     [[deprecated("Use DeleteEx(Uri &, const DataSharePredicates &) instead.")]]
195     virtual int Delete(Uri &uri, const DataSharePredicates &predicates) = 0;
196 
197     /**
198      * @brief Query records from the database.
199      *
200      * @param uri Indicates the path of data to query.
201      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
202      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
203      * @param businessError Indicates the error by query.
204      *
205      * @return Returns the query result.
206      */
207     virtual std::shared_ptr<DataShareResultSet> Query(Uri &uri, const DataSharePredicates &predicates,
208         std::vector<std::string> &columns, DatashareBusinessError *businessError = nullptr) = 0;
209 
210     /**
211      * @brief Obtains the MIME type matching the data specified by the URI of the Data share. This method should be
212      * implemented by a Data share. Data abilities supports general data types, including text, HTML, and JPEG.
213      *
214      * @param uri Indicates the URI of the data.
215      *
216      * @return Returns the MIME type that matches the data specified by uri.
217      */
218     virtual std::string GetType(Uri &uri) = 0;
219 
220     /**
221      * @brief Inserts multiple data records into the database.
222      *
223      * @param uri Indicates the path of the data to operate.
224      * @param values Indicates the data records to insert.
225      *
226      * @return Returns the number of data records inserted.
227      */
228     virtual int BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values) = 0;
229 
230     /**
231      * @brief Performs batch operations on the database.
232      *
233      * @param statements Indicates a list of database operation statement on the database.
234      * @param result Indicates the result of the operation.
235      *
236      * @return Returns the ipc result.
237      */
238     virtual int ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result) = 0;
239 
240     /**
241      * @brief Registers an observer to DataObsMgr specified by the given Uri.
242      *
243      * @param uri, Indicates the path of the data to operate.
244      * @param dataObserver, Indicates the IDataAbilityObserver object.
245      */
246     virtual void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0;
247 
248     /**
249      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
250      *
251      * @param uri, Indicates the path of the data to operate.
252      * @param dataObserver, Indicates the IDataAbilityObserver object.
253      */
254     virtual void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0;
255 
256     /**
257      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
258      *
259      * @param uri, Indicates the path of the data to operate.
260      */
261     virtual void NotifyChange(const Uri &uri) = 0;
262 
263     /**
264      * Registers an observer to DataObsMgr specified by the given Uri.
265      *
266      * @param uri, Indicates the path of the data to operate.
267      * @param dataObserver, Indicates the IDataAbilityObserver object.
268      * @param isDescendants, Indicates the Whether to note the change of descendants.
269      */
270     void RegisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver, bool isDescendants);
271 
272     /**
273      * Deregisters an observer used for DataObsMgr specified by the given Uri.
274      *
275      * @param uri, Indicates the path of the data to operate.
276      * @param dataObserver, Indicates the IDataAbilityObserver object
277      */
278     void UnregisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver);
279 
280     /**
281      * Notifies the registered observers of a change to the data resource specified by Uris.
282      *
283      * @param changeInfo Indicates the info of the data to operate.
284      */
285     void NotifyChangeExt(const DataShareObserver::ChangeInfo &changeInfo);
286 
287     /**
288      * @brief Converts the given uri that refer to the Data share into a normalized URI. A normalized URI can be used
289      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data share even if the
290      * context has changed. If you implement URI normalization for a Data share, you must also implement
291      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
292      * any method that is called on the Data share must require normalization verification and denormalization. The
293      * default implementation of this method returns null, indicating that this Data share does not support URI
294      * normalization.
295      *
296      * @param uri Indicates the Uri object to normalize.
297      *
298      * @return Returns the normalized Uri object if the Data share supports URI normalization; returns null otherwise.
299      */
300     virtual Uri NormalizeUri(Uri &uri) = 0;
301 
302     /**
303      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
304      * The default implementation of this method returns the original URI passed to it.
305      *
306      * @param uri uri Indicates the Uri object to denormalize.
307      *
308      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
309      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
310      * in the current environment.
311      */
312     virtual Uri DenormalizeUri(Uri &uri) = 0;
313 
314     /**
315      * @brief Adds a template of {@link #SubscribeRdbData}.
316      * @param uri, the uri to add.
317      * @param subscriberId, the subscribe id to add.
318      * @param tpl, the template to add.
319      * @return Returns the error code.
320      */
321     virtual int AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl) = 0;
322 
323     /**
324      * @brief Deletes a template of {@link #SubscribeRdbData}
325      * @param uri, the uri to delete.
326      * @param subscriberId, the subscribe id to delete.
327      * @return Returns the error code.
328      */
329     virtual int DelQueryTemplate(const std::string &uri, int64_t subscriberId) = 0;
330 
331     /**
332      * @brief Update a single data into host data area.
333      * @param data, the data to publish.
334      * @param bundleName the bundleName of data to publish.
335      * @return Returns the error code.
336      */
337     virtual std::vector<OperationResult> Publish(const Data &data, const std::string &bundleName) = 0;
338 
339     /**
340      * @brief Get published data by bundleName.
341      * @param bundleName, the bundleName of data.
342      * @param resultCode, the errcode returned by function
343      * @return Data {@link #Data}
344      */
345     virtual Data GetPublishedData(const std::string &bundleName, int &resultCode) = 0;
346 
347     /**
348      * @brief Registers observers to observe rdb data specified by the given uris and template.
349      * @param uris, the paths of the data to operate.
350      * @param templateId, the template of observers.
351      * @param callback, the callback function of observers.
352      * @return Returns the error code.
353      */
354     virtual std::vector<OperationResult> SubscribeRdbData(const std::vector<std::string> &uris,
355         const TemplateId &templateId, const std::function<void(const RdbChangeNode &changeNode)> &callback) = 0;
356 
357     /**
358      * @brief Unregisters observers used for monitoring data specified by the given uris and template.
359      * @param uris, the paths of the data to operate, if uris is empty, Unregisters all observers.
360      * @param templateId, the template of observers.
361      * @return Returns the error code.
362      */
363     virtual std::vector<OperationResult> UnsubscribeRdbData(const std::vector<std::string> &uris,
364         const TemplateId &templateId) = 0;
365 
366     /**
367      * @brief Enable observers by the given uris and template.
368      * @param uris, the paths of the data to operate.
369      * @param templateId, the template of observers.
370      * @return Returns the error code.
371      */
372     virtual std::vector<OperationResult> EnableRdbSubs(const std::vector<std::string> &uris,
373         const TemplateId &templateId) = 0;
374 
375     /**
376      * @brief Disable observers by the given uris and template.
377      * @param uris, the paths of the data to operate.
378      * @param templateId, the template of observers.
379      * @return Returns the error code.
380      */
381     virtual std::vector<OperationResult> DisableRdbSubs(const std::vector<std::string> &uris,
382         const TemplateId &templateId) = 0;
383 
384     /**
385      * @brief Registers observers to observe published data specified by the given uris and subscriberId.
386      * @param uris, the uris of the data to operate.
387      * @param subscriberId, the subscriberId of observers.
388      * @param callback, the callback function of observers.
389      * @return Returns the error code.
390      */
391     virtual std::vector<OperationResult> SubscribePublishedData(const std::vector<std::string> &uris,
392         int64_t subscriberId, const std::function<void(const PublishedDataChangeNode &changeNode)> &callback) = 0;
393 
394     /**
395      * @brief Unregisters observers used for monitoring data specified by the given uris and subscriberId.
396      * @param uris, the uris of the data to operate, if uris is empty, Unregisters all observers.
397      * @param subscriberId, the subscriberId of observers.
398      * @return Returns the error code.
399      */
400     virtual std::vector<OperationResult> UnsubscribePublishedData(const std::vector<std::string> &uris,
401         int64_t subscriberId) = 0;
402 
403     /**
404      * @brief Enable observers by the given uris and subscriberId.
405      * @param uris, the paths of the data to operate.
406      * @param subscriberId, the subscriberId of observers.
407      * @return Returns the error code.
408      */
409     virtual std::vector<OperationResult> EnablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0;
410 
411     /**
412      * @brief Disable observers by the given uris and template.
413      * @param uris, the paths of the data to operate.
414      * @param subscriberId, the subscriberId of observers.
415      * @return Returns the error code.
416      */
417     virtual std::vector<OperationResult> DisablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0;
418 
419     /**
420      * @brief Set default switch for silent access.
421      * @param uri, the uri to disable/enable.
422      * @param enable, the enable of silent switch.
423      * @return Returns the error code.
424      */
425     static int SetSilentSwitch(Uri &uri, bool enable);
426 
427     /**
428      * @brief Inserts a single data record into the database.
429      *
430      * @param uri Indicates the path of the data to operate.
431      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
432      *
433      * @return Returns pair<int32_t errCode, int32_t index>, the errCode and the index of the inserted data record.
434      */
435     virtual std::pair<int32_t, int32_t> InsertEx(Uri &uri, const DataShareValuesBucket &value);
436 
437     /**
438      * @brief Updates data records in the database.
439      *
440      * @param uri Indicates the path of data to update.
441      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
442      * @param value Indicates the data to update. This parameter can be null.
443      *
444      * @return Returns pair<int32_t errCode, int32_t index>, the errCode and the index of data records updated.
445      */
446     virtual std::pair<int32_t, int32_t> UpdateEx(
447         Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value);
448 
449     /**
450      * @brief Deletes one or more data records from the database.
451      *
452      * @param uri Indicates the path of the data to operate.
453      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
454      *
455      * @return Returns pair<int32_t errCode, int32_t index>, the errCode and the index of data records deleted.
456      */
457     virtual std::pair<int32_t, int32_t> DeleteEx(Uri &uri, const DataSharePredicates &predicates);
458 
459 private:
460     static std::shared_ptr<DataShareHelper> CreateServiceHelper(const std::string &extUri = "",
461         const std::string &bundleName = "");
462 
463     static int GetSilentProxyStatus(const std::string &uri);
464 
465     static std::shared_ptr<DataShareHelper> CreateExtHelper(Uri &uri, const sptr<IRemoteObject> &token,
466         const int waitTime = 2);
467 
468     static std::string TransferUriPrefix(const std::string &originPrefix, const std::string &replacedPrefix,
469         const std::string &originUriStr);
470 
471     static bool IsProxy(Uri &uri);
472 
473     static std::pair<int, std::shared_ptr<DataShareHelper>> CreateProxyHelper(const std::string &strUri,
474         const std::string &extUri);
475 };
476 } // namespace DataShare
477 } // namespace OHOS
478 #endif // DATASHARE_HELPER_H