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