• 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_operation_statement.h"
28 #include "datashare_predicates.h"
29 #include "datashare_result_set.h"
30 #include "datashare_template.h"
31 #include "datashare_values_bucket.h"
32 #include "uri.h"
33 
34 using Uri = OHOS::Uri;
35 
36 namespace OHOS {
37 namespace AppExecFwk {
38 class PacMap;
39 class IDataAbilityObserver;
40 } // namespace AppExecFwk
41 
42 namespace DataShare {
43 using string = std::string;
44 class DataShareObserver {
45 public:
46     DataShareObserver() = default;
47     virtual ~DataShareObserver() = default;
48     enum ChangeType : uint32_t {
49         INSERT = 0,
50         DELETE,
51         UPDATE,
52         OTHER,
53         INVAILD,
54     };
55 
56     struct ChangeInfo {
57         ChangeType changeType_ = INVAILD;
58         std::list<Uri> uris_ = {};
59         const void *data_ = nullptr;
60         uint32_t size_ = 0;
61     };
62 
63     virtual void OnChange(const ChangeInfo &changeInfo) = 0;
64 };
65 
66 class DataShareHelper : public std::enable_shared_from_this<DataShareHelper> {
67 public:
68     /**
69      * @brief Destructor.
70      */
71     virtual ~DataShareHelper() = default;
72 
73     /**
74      * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
75      * between the ability using the Data template (data share for short) and the associated client process in
76      * a DataShareHelper instance.
77      *
78      * @param token Indicates the System token.
79      * @param strUri Indicates the database table or disk file to operate.
80      *
81      * @return Returns the created DataShareHelper instance.
82      */
83     static std::shared_ptr<DataShareHelper> Creator(
84         const sptr<IRemoteObject> &token, const std::string &strUri, const std::string &extUri = "");
85 
86     /**
87      * @brief Creates a DataShareHelper instance with the Uri and {@link #CreateOptions} .
88      *
89      * @param strUri Indicates the database table or disk file to operate.
90      * @param options Indicates the optional config.
91      *
92      * @return Returns the created DataShareHelper instance with a specified Uri.
93      */
94     static std::shared_ptr<DataShareHelper> Creator(const std::string &strUri, const CreateOptions &options,
95         const std::string &bundleName = "");
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     virtual int Insert(Uri &uri, const DataShareValuesBucket &value) = 0;
151 
152     /**
153      * @brief Inserts a single data record into the database.
154      *
155      * @param uri Indicates the path of the data to operate.
156      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
157      * @param result Indicates the result string of the insert operation.
158      *
159      * @return Returns the index of the inserted data record.
160      */
161     virtual int InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result) = 0;
162 
163     /**
164      * @brief Updates data records in the database.
165      *
166      * @param uri Indicates the path of data to update.
167      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
168      * @param value Indicates the data to update. This parameter can be null.
169      *
170      * @return Returns the number of data records updated.
171      */
172     virtual int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) = 0;
173 
174     /**
175      * @brief Deletes one or more data records from the database.
176      *
177      * @param uri Indicates the path of the data to operate.
178      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
179      *
180      * @return Returns the number of data records deleted.
181      */
182     virtual int Delete(Uri &uri, const DataSharePredicates &predicates) = 0;
183 
184     /**
185      * @brief Query records from the database.
186      *
187      * @param uri Indicates the path of data to query.
188      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
189      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
190      * @param businessError Indicates the error by query.
191      *
192      * @return Returns the query result.
193      */
194     virtual std::shared_ptr<DataShareResultSet> Query(Uri &uri, const DataSharePredicates &predicates,
195         std::vector<std::string> &columns, DatashareBusinessError *businessError = nullptr) = 0;
196 
197     /**
198      * @brief Obtains the MIME type matching the data specified by the URI of the Data share. This method should be
199      * implemented by a Data share. Data abilities supports general data types, including text, HTML, and JPEG.
200      *
201      * @param uri Indicates the URI of the data.
202      *
203      * @return Returns the MIME type that matches the data specified by uri.
204      */
205     virtual std::string GetType(Uri &uri) = 0;
206 
207     /**
208      * @brief Inserts multiple data records into the database.
209      *
210      * @param uri Indicates the path of the data to operate.
211      * @param values Indicates the data records to insert.
212      *
213      * @return Returns the number of data records inserted.
214      */
215     virtual int BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values) = 0;
216 
217     /**
218      * @brief Performs batch operations on the database.
219      *
220      * @param statements Indicates a list of database operation statement on the database.
221      * @param result Indicates the result of the operation.
222      *
223      * @return Returns the ipc result.
224      */
225     virtual int ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result) = 0;
226 
227     /**
228      * @brief Registers an observer to DataObsMgr specified by the given Uri.
229      *
230      * @param uri, Indicates the path of the data to operate.
231      * @param dataObserver, Indicates the IDataAbilityObserver object.
232      */
233     virtual void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0;
234 
235     /**
236      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
237      *
238      * @param uri, Indicates the path of the data to operate.
239      * @param dataObserver, Indicates the IDataAbilityObserver object.
240      */
241     virtual void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0;
242 
243     /**
244      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
245      *
246      * @param uri, Indicates the path of the data to operate.
247      */
248     virtual void NotifyChange(const Uri &uri) = 0;
249 
250     /**
251      * Registers an observer to DataObsMgr specified by the given Uri.
252      *
253      * @param uri, Indicates the path of the data to operate.
254      * @param dataObserver, Indicates the IDataAbilityObserver object.
255      * @param isDescendants, Indicates the Whether to note the change of descendants.
256      */
257     void RegisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver, bool isDescendants);
258 
259     /**
260      * Deregisters an observer used for DataObsMgr specified by the given Uri.
261      *
262      * @param uri, Indicates the path of the data to operate.
263      * @param dataObserver, Indicates the IDataAbilityObserver object
264      */
265     void UnregisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver);
266 
267     /**
268      * Notifies the registered observers of a change to the data resource specified by Uris.
269      *
270      * @param changeInfo Indicates the info of the data to operate.
271      */
272     void NotifyChangeExt(const DataShareObserver::ChangeInfo &changeInfo);
273 
274     /**
275      * @brief Converts the given uri that refer to the Data share into a normalized URI. A normalized URI can be used
276      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data share even if the
277      * context has changed. If you implement URI normalization for a Data share, you must also implement
278      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
279      * any method that is called on the Data share must require normalization verification and denormalization. The
280      * default implementation of this method returns null, indicating that this Data share does not support URI
281      * normalization.
282      *
283      * @param uri Indicates the Uri object to normalize.
284      *
285      * @return Returns the normalized Uri object if the Data share supports URI normalization; returns null otherwise.
286      */
287     virtual Uri NormalizeUri(Uri &uri) = 0;
288 
289     /**
290      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
291      * The default implementation of this method returns the original URI passed to it.
292      *
293      * @param uri uri Indicates the Uri object to denormalize.
294      *
295      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
296      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
297      * in the current environment.
298      */
299     virtual Uri DenormalizeUri(Uri &uri) = 0;
300 
301     /**
302      * @brief Adds a template of {@link #SubscribeRdbData}.
303      * @param uri, the uri to add.
304      * @param subscriberId, the subscribe id to add.
305      * @param tpl, the template to add.
306      * @return Returns the error code.
307      */
308     virtual int AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl) = 0;
309 
310     /**
311      * @brief Deletes a template of {@link #SubscribeRdbData}
312      * @param uri, the uri to delete.
313      * @param subscriberId, the subscribe id to delete.
314      * @return Returns the error code.
315      */
316     virtual int DelQueryTemplate(const std::string &uri, int64_t subscriberId) = 0;
317 
318     /**
319      * @brief Update a single data into host data area.
320      * @param data, the data to publish.
321      * @param bundleName the bundleName of data to publish.
322      * @return Returns the error code.
323      */
324     virtual std::vector<OperationResult> Publish(const Data &data, const std::string &bundleName) = 0;
325 
326     /**
327      * @brief Get published data by bundleName.
328      * @param bundleName, the bundleName of data.
329      * @param resultCode, the errcode returned by function
330      * @return Data {@link #Data}
331      */
332     virtual Data GetPublishedData(const std::string &bundleName, int &resultCode) = 0;
333 
334     /**
335      * @brief Registers observers to observe rdb data specified by the given uris and template.
336      * @param uris, the paths of the data to operate.
337      * @param templateId, the template of observers.
338      * @param callback, the callback function of observers.
339      * @return Returns the error code.
340      */
341     virtual std::vector<OperationResult> SubscribeRdbData(const std::vector<std::string> &uris,
342         const TemplateId &templateId, const std::function<void(const RdbChangeNode &changeNode)> &callback) = 0;
343 
344     /**
345      * @brief Unregisters observers used for monitoring data specified by the given uris and template.
346      * @param uris, the paths of the data to operate, if uris is empty, Unregisters all observers.
347      * @param templateId, the template of observers.
348      * @return Returns the error code.
349      */
350     virtual std::vector<OperationResult> UnsubscribeRdbData(const std::vector<std::string> &uris,
351         const TemplateId &templateId) = 0;
352 
353     /**
354      * @brief Enable observers by the given uris and template.
355      * @param uris, the paths of the data to operate.
356      * @param templateId, the template of observers.
357      * @return Returns the error code.
358      */
359     virtual std::vector<OperationResult> EnableRdbSubs(const std::vector<std::string> &uris,
360         const TemplateId &templateId) = 0;
361 
362     /**
363      * @brief Disable observers by the given uris and template.
364      * @param uris, the paths of the data to operate.
365      * @param templateId, the template of observers.
366      * @return Returns the error code.
367      */
368     virtual std::vector<OperationResult> DisableRdbSubs(const std::vector<std::string> &uris,
369         const TemplateId &templateId) = 0;
370 
371     /**
372      * @brief Registers observers to observe published data specified by the given uris and subscriberId.
373      * @param uris, the uris of the data to operate.
374      * @param subscriberId, the subscriberId of observers.
375      * @param callback, the callback function of observers.
376      * @return Returns the error code.
377      */
378     virtual std::vector<OperationResult> SubscribePublishedData(const std::vector<std::string> &uris,
379         int64_t subscriberId, const std::function<void(const PublishedDataChangeNode &changeNode)> &callback) = 0;
380 
381     /**
382      * @brief Unregisters observers used for monitoring data specified by the given uris and subscriberId.
383      * @param uris, the uris of the data to operate, if uris is empty, Unregisters all observers.
384      * @param subscriberId, the subscriberId of observers.
385      * @return Returns the error code.
386      */
387     virtual std::vector<OperationResult> UnsubscribePublishedData(const std::vector<std::string> &uris,
388         int64_t subscriberId) = 0;
389 
390     /**
391      * @brief Enable observers by the given uris and subscriberId.
392      * @param uris, the paths of the data to operate.
393      * @param subscriberId, the subscriberId of observers.
394      * @return Returns the error code.
395      */
396     virtual std::vector<OperationResult> EnablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0;
397 
398     /**
399      * @brief Disable observers by the given uris and template.
400      * @param uris, the paths of the data to operate.
401      * @param subscriberId, the subscriberId of observers.
402      * @return Returns the error code.
403      */
404     virtual std::vector<OperationResult> DisablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0;
405 
406     /**
407      * @brief Set default switch for silent access.
408      * @param uri, the uri to disable/enable.
409      * @param enable, the enable of silent switch.
410      * @return Returns the error code.
411      */
412     static int SetSilentSwitch(Uri &uri, bool enable);
413 
414 private:
415     static std::shared_ptr<DataShareHelper> CreateServiceHelper(const std::string &bundleName = "");
416 
417     static bool IsSilentProxyEnable(const std::string &uri);
418 
419     static std::shared_ptr<DataShareHelper> CreateExtHelper(Uri &uri, const sptr<IRemoteObject> &token);
420 
421     static std::string TransferUriPrefix(const std::string &originPrefix, const std::string &replacedPrefix,
422         const std::string &originUriStr);
423 };
424 } // namespace DataShare
425 } // namespace OHOS
426 #endif // DATASHARE_HELPER_H