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