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