• 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(const sptr<IRemoteObject> &token, const std::string &strUri);
84 
85     /**
86      * @brief Creates a DataShareHelper instance with the Uri and {@link #CreateOptions} .
87      *
88      * @param strUri Indicates the database table or disk file to operate.
89      * @param options Indicates the optional config.
90      *
91      * @return Returns the created DataShareHelper instance with a specified Uri.
92      */
93     static std::shared_ptr<DataShareHelper> Creator(const std::string &strUri, const CreateOptions &options,
94         const std::string &bundleName = "");
95 
96     /**
97      * @brief Releases the client resource of the Data share.
98      * You should call this method to releases client resource after the data operations are complete.
99      *
100      * @return Returns true if the resource is successfully released; returns false otherwise.
101      */
102     virtual bool Release() = 0;
103 
104     /**
105      * @brief Obtains the MIME types of files supported.
106      *
107      * @param uri Indicates the path of the files to obtain.
108      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
109      *
110      * @return Returns the matched MIME types. If there is no match, null is returned.
111      */
112     virtual std::vector<std::string> GetFileTypes(Uri &uri, const std::string &mimeTypeFilter) = 0;
113 
114     /**
115      * @brief Opens a file in a specified remote path.
116      *
117      * @param uri Indicates the path of the file to open.
118      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
119      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
120      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
121      *  or "rwt" for read and write access that truncates any existing file.
122      *
123      * @return Returns the file descriptor.
124      */
125     virtual int OpenFile(Uri &uri, const std::string &mode) = 0;
126 
127     /**
128      * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
129      * inside of their .hap.
130      *
131      * @param uri Indicates the path of the file to open.
132      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
133      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
134      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
135      * data, or "rwt" for read and write access that truncates any existing file.
136      *
137      * @return Returns the RawFileDescriptor object containing file descriptor.
138      */
139     virtual int OpenRawFile(Uri &uri, const std::string &mode) = 0;
140 
141     /**
142      * @brief Inserts a single data record into the database.
143      *
144      * @param uri Indicates the path of the data to operate.
145      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
146      *
147      * @return Returns the index of the inserted data record.
148      */
149     virtual int Insert(Uri &uri, const DataShareValuesBucket &value) = 0;
150 
151     /**
152      * @brief Inserts a single data record into the database.
153      *
154      * @param uri Indicates the path of the data to operate.
155      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
156      * @param result Indicates the result string of the insert operation.
157      *
158      * @return Returns the index of the inserted data record.
159      */
160     virtual int InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result) = 0;
161 
162     /**
163      * @brief Updates data records in the database.
164      *
165      * @param uri Indicates the path of data to update.
166      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
167      * @param value Indicates the data to update. This parameter can be null.
168      *
169      * @return Returns the number of data records updated.
170      */
171     virtual int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) = 0;
172 
173     /**
174      * @brief Deletes one or more data records from the database.
175      *
176      * @param uri Indicates the path of the data to operate.
177      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
178      *
179      * @return Returns the number of data records deleted.
180      */
181     virtual int Delete(Uri &uri, const DataSharePredicates &predicates) = 0;
182 
183     /**
184      * @brief Query records from the database.
185      *
186      * @param uri Indicates the path of data to query.
187      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
188      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
189      * @param businessError Indicates the error by query.
190      *
191      * @return Returns the query result.
192      */
193     virtual std::shared_ptr<DataShareResultSet> Query(Uri &uri, const DataSharePredicates &predicates,
194         std::vector<std::string> &columns, DatashareBusinessError *businessError = nullptr) = 0;
195 
196     /**
197      * @brief Obtains the MIME type matching the data specified by the URI of the Data share. This method should be
198      * implemented by a Data share. Data abilities supports general data types, including text, HTML, and JPEG.
199      *
200      * @param uri Indicates the URI of the data.
201      *
202      * @return Returns the MIME type that matches the data specified by uri.
203      */
204     virtual std::string GetType(Uri &uri) = 0;
205 
206     /**
207      * @brief Inserts multiple data records into the database.
208      *
209      * @param uri Indicates the path of the data to operate.
210      * @param values Indicates the data records to insert.
211      *
212      * @return Returns the number of data records inserted.
213      */
214     virtual int BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values) = 0;
215 
216     /**
217      * @brief Performs batch operations on the database.
218      *
219      * @param statements Indicates a list of database operation statement on the database.
220      * @param result Indicates the result of the operation.
221      *
222      * @return Returns the ipc result.
223      */
224     virtual int ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result) = 0;
225 
226     /**
227      * @brief Registers an observer to DataObsMgr specified by the given Uri.
228      *
229      * @param uri, Indicates the path of the data to operate.
230      * @param dataObserver, Indicates the IDataAbilityObserver object.
231      */
232     virtual void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0;
233 
234     /**
235      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
236      *
237      * @param uri, Indicates the path of the data to operate.
238      * @param dataObserver, Indicates the IDataAbilityObserver object.
239      */
240     virtual void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0;
241 
242     /**
243      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
244      *
245      * @param uri, Indicates the path of the data to operate.
246      */
247     virtual void NotifyChange(const Uri &uri) = 0;
248 
249     /**
250      * Registers an observer to DataObsMgr specified by the given Uri.
251      *
252      * @param uri, Indicates the path of the data to operate.
253      * @param dataObserver, Indicates the IDataAbilityObserver object.
254      * @param isDescendants, Indicates the Whether to note the change of descendants.
255      */
256     void RegisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver, bool isDescendants);
257 
258     /**
259      * Deregisters an observer used for DataObsMgr specified by the given Uri.
260      *
261      * @param uri, Indicates the path of the data to operate.
262      * @param dataObserver, Indicates the IDataAbilityObserver object
263      */
264     void UnregisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver);
265 
266     /**
267      * Notifies the registered observers of a change to the data resource specified by Uris.
268      *
269      * @param changeInfo Indicates the info of the data to operate.
270      */
271     void NotifyChangeExt(const DataShareObserver::ChangeInfo &changeInfo);
272 
273     /**
274      * @brief Converts the given uri that refer to the Data share into a normalized URI. A normalized URI can be used
275      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data share even if the
276      * context has changed. If you implement URI normalization for a Data share, you must also implement
277      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
278      * any method that is called on the Data share must require normalization verification and denormalization. The
279      * default implementation of this method returns null, indicating that this Data share does not support URI
280      * normalization.
281      *
282      * @param uri Indicates the Uri object to normalize.
283      *
284      * @return Returns the normalized Uri object if the Data share supports URI normalization; returns null otherwise.
285      */
286     virtual Uri NormalizeUri(Uri &uri) = 0;
287 
288     /**
289      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
290      * The default implementation of this method returns the original URI passed to it.
291      *
292      * @param uri uri Indicates the Uri object to denormalize.
293      *
294      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
295      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
296      * in the current environment.
297      */
298     virtual Uri DenormalizeUri(Uri &uri) = 0;
299 
300     /**
301      * @brief Adds a template of {@link #SubscribeRdbData}.
302      * @param uri, the uri to add.
303      * @param subscriberId, the subscribe id to add.
304      * @param tpl, the template to add.
305      * @return Returns the error code.
306      */
307     virtual int AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl) = 0;
308 
309     /**
310      * @brief Deletes a template of {@link #SubscribeRdbData}
311      * @param uri, the uri to delete.
312      * @param subscriberId, the subscribe id to delete.
313      * @return Returns the error code.
314      */
315     virtual int DelQueryTemplate(const std::string &uri, int64_t subscriberId) = 0;
316 
317     /**
318      * @brief Update a single data into host data area.
319      * @param data, the data to publish.
320      * @param bundleName the bundleName of data to publish.
321      * @return Returns the error code.
322      */
323     virtual std::vector<OperationResult> Publish(const Data &data, const std::string &bundleName) = 0;
324 
325     /**
326      * @brief Get published data by bundleName.
327      * @param bundleName, the bundleName of data.
328      * @param resultCode, the errcode returned by function
329      * @return Data {@link #Data}
330      */
331     virtual Data GetPublishedData(const std::string &bundleName, int &resultCode) = 0;
332 
333     /**
334      * @brief Registers observers to observe rdb data specified by the given uris and template.
335      * @param uris, the paths of the data to operate.
336      * @param templateId, the template of observers.
337      * @param callback, the callback function of observers.
338      * @return Returns the error code.
339      */
340     virtual std::vector<OperationResult> SubscribeRdbData(const std::vector<std::string> &uris,
341         const TemplateId &templateId, const std::function<void(const RdbChangeNode &changeNode)> &callback) = 0;
342 
343     /**
344      * @brief Unregisters observers used for monitoring data specified by the given uris and template.
345      * @param uris, the paths of the data to operate, if uris is empty, Unregisters all observers.
346      * @param templateId, the template of observers.
347      * @return Returns the error code.
348      */
349     virtual std::vector<OperationResult> UnsubscribeRdbData(const std::vector<std::string> &uris,
350         const TemplateId &templateId) = 0;
351 
352     /**
353      * @brief Enable observers by the given uris and template.
354      * @param uris, the paths of the data to operate.
355      * @param templateId, the template of observers.
356      * @return Returns the error code.
357      */
358     virtual std::vector<OperationResult> EnableRdbSubs(const std::vector<std::string> &uris,
359         const TemplateId &templateId) = 0;
360 
361     /**
362      * @brief Disable observers by the given uris and template.
363      * @param uris, the paths of the data to operate.
364      * @param templateId, the template of observers.
365      * @return Returns the error code.
366      */
367     virtual std::vector<OperationResult> DisableRdbSubs(const std::vector<std::string> &uris,
368         const TemplateId &templateId) = 0;
369 
370     /**
371      * @brief Registers observers to observe published data specified by the given uris and subscriberId.
372      * @param uris, the uris of the data to operate.
373      * @param subscriberId, the subscriberId of observers.
374      * @param callback, the callback function of observers.
375      * @return Returns the error code.
376      */
377     virtual std::vector<OperationResult> SubscribePublishedData(const std::vector<std::string> &uris,
378         int64_t subscriberId, const std::function<void(const PublishedDataChangeNode &changeNode)> &callback) = 0;
379 
380     /**
381      * @brief Unregisters observers used for monitoring data specified by the given uris and subscriberId.
382      * @param uris, the uris of the data to operate, if uris is empty, Unregisters all observers.
383      * @param subscriberId, the subscriberId of observers.
384      * @return Returns the error code.
385      */
386     virtual std::vector<OperationResult> UnsubscribePublishedData(const std::vector<std::string> &uris,
387         int64_t subscriberId) = 0;
388 
389     /**
390      * @brief Enable observers by the given uris and subscriberId.
391      * @param uris, the paths of the data to operate.
392      * @param subscriberId, the subscriberId of observers.
393      * @return Returns the error code.
394      */
395     virtual std::vector<OperationResult> EnablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0;
396 
397     /**
398      * @brief Disable observers by the given uris and template.
399      * @param uris, the paths of the data to operate.
400      * @param subscriberId, the subscriberId of observers.
401      * @return Returns the error code.
402      */
403     virtual std::vector<OperationResult> DisablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0;
404 
405 private:
406     static std::shared_ptr<DataShareHelper> CreateServiceHelper(const std::string &bundleName = "");
407 
408     static std::shared_ptr<DataShareHelper> CreateExtHelper(Uri &uri, const sptr<IRemoteObject> &token);
409 
410     static std::string TransferUriPrefix(const std::string &originPrefix, const std::string &replacedPrefix,
411         const std::string &originUriStr);
412 };
413 } // namespace DataShare
414 } // namespace OHOS
415 #endif // DATASHARE_HELPER_H