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