• 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 <mutex>
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include "context.h"
25 #include "datashare_connection.h"
26 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h"
27 #include "idatashare.h"
28 #include "uri.h"
29 
30 using Uri = OHOS::Uri;
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 class PacMap;
35 class IDataAbilityObserver;
36 }
37 
38 namespace DataShare {
39 using string = std::string;
40 class DataShareHelper final : public std::enable_shared_from_this<DataShareHelper> {
41 public:
42     ~DataShareHelper();
43 
44     /**
45      * @brief Creates a DataShareHelper instance with the Uri specified based on the given Context.
46      *
47      * @param context Indicates the Context object on OHOS.
48      * @param strUri Indicates the database table or disk file to operate.
49      *
50      * @return Returns the created DataShareHelper instance with a specified Uri.
51      */
52     static std::shared_ptr<DataShareHelper> Creator(const std::shared_ptr<Context> &context,
53         const std::string &strUri);
54 
55     /**
56      * @brief Creates a DataShareHelper instance with the Uri specified based on the given Context.
57      *
58      * @param context Indicates the Context object on OHOS.
59      * @param StrUri Indicates the database table or disk file to operate.
60      *
61      * @return Returns the created DataShareHelper instance with a specified Uri.
62      */
63     static std::shared_ptr<DataShareHelper> Creator(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context,
64         const std::string &strUri);
65 
66     /**
67      * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
68      * between the ability using the Data template (data share for short) and the associated client process in
69      * a DataShareHelper instance.
70      *
71      * @param token Indicates the System token.
72      * @param strUri Indicates the database table or disk file to operate.
73      *
74      * @return Returns the created DataShareHelper instance.
75      */
76     static std::shared_ptr<DataShareHelper> Creator(const sptr<IRemoteObject> &token, const std::string &strUri);
77 
78     /**
79      * @brief Releases the client resource of the Data share.
80      * You should call this method to releases client resource after the data operations are complete.
81      *
82      * @return Returns true if the resource is successfully released; returns false otherwise.
83      */
84     bool Release();
85 
86     /**
87      * @brief Obtains the MIME types of files supported.
88      *
89      * @param uri Indicates the path of the files to obtain.
90      * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
91      *
92      * @return Returns the matched MIME types. If there is no match, null is returned.
93      */
94     std::vector<std::string> GetFileTypes(Uri &uri, const std::string &mimeTypeFilter);
95 
96     /**
97      * @brief Opens a file in a specified remote path.
98      *
99      * @param uri Indicates the path of the file to open.
100      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
101      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
102      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
103      *  or "rwt" for read and write access that truncates any existing file.
104      *
105      * @return Returns the file descriptor.
106      */
107     int OpenFile(Uri &uri, const std::string &mode);
108 
109     /**
110      * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
111      * inside of their .hap.
112      *
113      * @param uri Indicates the path of the file to open.
114      * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
115      * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
116      * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
117      * data, or "rwt" for read and write access that truncates any existing file.
118      *
119      * @return Returns the RawFileDescriptor object containing file descriptor.
120      */
121     int OpenRawFile(Uri &uri, const std::string &mode);
122 
123     /**
124      * @brief Inserts a single data record into the database.
125      *
126      * @param uri Indicates the path of the data to operate.
127      * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
128      *
129      * @return Returns the index of the inserted data record.
130      */
131     int Insert(Uri &uri, const DataShareValuesBucket &value);
132 
133     /**
134      * @brief Updates data records in the database.
135      *
136      * @param uri Indicates the path of data to update.
137      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
138      * @param value Indicates the data to update. This parameter can be null.
139      *
140      * @return Returns the number of data records updated.
141      */
142     int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value);
143 
144     /**
145      * @brief Deletes one or more data records from the database.
146      *
147      * @param uri Indicates the path of the data to operate.
148      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
149      *
150      * @return Returns the number of data records deleted.
151      */
152     int Delete(Uri &uri, const DataSharePredicates &predicates);
153 
154     /**
155      * @brief Deletes one or more data records from the database.
156      *
157      * @param uri Indicates the path of data to query.
158      * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
159      * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
160      *
161      * @return Returns the query result.
162      */
163     std::shared_ptr<DataShareResultSet> Query(
164         Uri &uri, const DataSharePredicates &predicates, std::vector<std::string> &columns);
165 
166     /**
167      * @brief Obtains the MIME type matching the data specified by the URI of the Data share. This method should be
168      * implemented by a Data share. Data abilities supports general data types, including text, HTML, and JPEG.
169      *
170      * @param uri Indicates the URI of the data.
171      *
172      * @return Returns the MIME type that matches the data specified by uri.
173      */
174     std::string GetType(Uri &uri);
175 
176     /**
177      * @brief Inserts multiple data records into the database.
178      *
179      * @param uri Indicates the path of the data to operate.
180      * @param values Indicates the data records to insert.
181      *
182      * @return Returns the number of data records inserted.
183      */
184     int BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values);
185 
186     /**
187      * @brief Registers an observer to DataObsMgr specified by the given Uri.
188      *
189      * @param uri, Indicates the path of the data to operate.
190      * @param dataObserver, Indicates the IDataAbilityObserver object.
191      */
192     void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
193 
194     /**
195      * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
196      *
197      * @param uri, Indicates the path of the data to operate.
198      * @param dataObserver, Indicates the IDataAbilityObserver object.
199      */
200     void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
201 
202     /**
203      * @brief Notifies the registered observers of a change to the data resource specified by Uri.
204      *
205      * @param uri, Indicates the path of the data to operate.
206      */
207     void NotifyChange(const Uri &uri);
208 
209     /**
210      * @brief Converts the given uri that refer to the Data share into a normalized URI. A normalized URI can be used
211      * across devices, persisted, backed up, and restored. It can refer to the same item in the Data share even if the
212      * context has changed. If you implement URI normalization for a Data share, you must also implement
213      * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
214      * any method that is called on the Data share must require normalization verification and denormalization. The
215      * default implementation of this method returns null, indicating that this Data share does not support URI
216      * normalization.
217      *
218      * @param uri Indicates the Uri object to normalize.
219      *
220      * @return Returns the normalized Uri object if the Data share supports URI normalization; returns null otherwise.
221      */
222     Uri NormalizeUri(Uri &uri);
223 
224     /**
225      * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
226      * The default implementation of this method returns the original URI passed to it.
227      *
228      * @param uri uri Indicates the Uri object to denormalize.
229      *
230      * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
231      * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
232      * in the current environment.
233      */
234     Uri DenormalizeUri(Uri &uri);
235 
236 private:
237     DataShareHelper(const sptr<IRemoteObject> &token, const Uri &uri,
238         std::shared_ptr<DataShareConnection> dataShareConnection);
239     DataShareHelper(const sptr<IRemoteObject> &token, const Uri &uri);
240     bool isDataShareService_ = false;
241     sptr<IRemoteObject> token_ = {};
242     Uri uri_ = Uri("");
243     std::shared_ptr<DataShareConnection> dataShareConnection_ = nullptr;
244     static bool RegObserver (const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
245     static bool UnregObserver (const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
246 };
247 }  // namespace DataShare
248 }  // namespace OHOS
249 #endif  // DATASHARE_HELPER_H