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_EXT_ABILITY_H 17 #define DATASHARE_EXT_ABILITY_H 18 19 #include <memory> 20 #include "extension_base.h" 21 #include "data_ability_observer_interface.h" 22 #include "datashare_business_error.h" 23 #include "datashare_values_bucket.h" 24 #include "datashare_predicates.h" 25 #include "datashare_result_set.h" 26 #include "datashare_operation_statement.h" 27 28 namespace OHOS { 29 namespace AAFwk { 30 class IDataAbilityObserver; 31 } 32 namespace AbilityRuntime { 33 class Runtime; 34 } 35 namespace DataShare { 36 using namespace AbilityRuntime; 37 class DataShareExtAbilityContext; 38 class DataShareExtAbility; 39 using CreatorFunc = std::function<DataShareExtAbility* (const std::unique_ptr<Runtime>& runtime)>; 40 using ChangeInfo = AAFwk::ChangeInfo; 41 /** 42 * @brief Basic datashare extension ability components. 43 */ 44 class DataShareExtAbility : public ExtensionBase<DataShareExtAbilityContext> { 45 public: 46 DataShareExtAbility() = default; 47 virtual ~DataShareExtAbility() = default; 48 49 /** 50 * @brief Init the extension. 51 * 52 * @param record the extension record. 53 * @param application the application info. 54 * @param handler the extension handler. 55 * @param token the remote token. 56 */ 57 virtual void Init(const std::shared_ptr<AbilityLocalRecord> &record, 58 const std::shared_ptr<OHOSApplication> &application, 59 std::shared_ptr<AbilityHandler> &handler, 60 const sptr<IRemoteObject> &token) override; 61 62 /** 63 * @brief Create Extension. 64 * 65 * @param runtime The runtime. 66 * @return The DataShareExtAbility instance. 67 */ 68 static DataShareExtAbility* Create(const std::unique_ptr<Runtime>& runtime); 69 70 /** 71 * @brief Obtains the MIME types of files supported. 72 * 73 * @param uri Indicates the path of the files to obtain. 74 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. 75 * 76 * @return Returns the matched MIME types. If there is no match, null is returned. 77 */ 78 virtual std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter); 79 80 /** 81 * @brief Opens a file in a specified remote path. 82 * 83 * @param uri Indicates the path of the file to open. 84 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 85 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 86 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, 87 * or "rwt" for read and write access that truncates any existing file. 88 * 89 * @return Returns the file descriptor. 90 */ 91 virtual int OpenFile(const Uri &uri, const std::string &mode); 92 93 /** 94 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 95 * inside of their .hap. 96 * 97 * @param uri Indicates the path of the file to open. 98 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 99 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 100 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 101 * data, or "rwt" for read and write access that truncates any existing file. 102 * 103 * @return Returns the RawFileDescriptor object containing file descriptor. 104 */ 105 virtual int OpenRawFile(const Uri &uri, const std::string &mode); 106 107 /** 108 * @brief Inserts a single data record into the database. 109 * 110 * @param uri Indicates the path of the data to operate. 111 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 112 * 113 * @return Returns the index of the inserted data record. 114 */ 115 virtual int Insert(const Uri &uri, const DataShareValuesBucket &value); 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 * @param result Indicates the data result to insert. 123 * 124 * @return Returns the index of the inserted data record. 125 */ 126 virtual int InsertExt(const Uri &uri, const DataShareValuesBucket &value, std::string &result); 127 128 /** 129 * @brief Updates data records in the database. 130 * 131 * @param uri Indicates the path of data to update. 132 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 133 * @param value Indicates the data to update. This parameter can be null. 134 * 135 * @return Returns the number of data records updated. 136 */ 137 virtual int Update(const Uri &uri, const DataSharePredicates &predicates, 138 const DataShareValuesBucket &value); 139 140 /** 141 * @brief Batch updates data records in the database. 142 * 143 * @param updateOperations Indicates the param of data to update. 144 * @param results Indicates the number of data records updated. 145 * 146 * @return Return the execution results of batch updates. 147 */ 148 virtual int BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results); 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 virtual int Delete(const Uri &uri, const DataSharePredicates &predicates); 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 predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 165 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 166 * @param businessError Indicates the error by query. 167 * 168 * @return Returns the query result. 169 */ 170 virtual std::shared_ptr<DataShareResultSet> Query(const Uri &uri, const DataSharePredicates &predicates, 171 std::vector<std::string> &columns, DatashareBusinessError &businessError); 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 virtual std::string GetType(const Uri &uri); 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 virtual int BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values); 192 193 /** 194 * @brief Performs batch operations on the database. 195 * 196 * @param statements Indicates a list of database operation statement on the database. 197 * @param result Indicates the result of the operation. 198 * 199 * @return Returns the ipc result. 200 */ 201 virtual int ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result); 202 203 /** 204 * @brief Registers an observer to 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 virtual bool RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 210 211 /** 212 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 213 * 214 * @param uri, Indicates the path of the data to operate. 215 * @param dataObserver, Indicates the IDataAbilityObserver object. 216 */ 217 virtual bool UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 218 219 /** 220 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 221 * 222 * @param uri, Indicates the path of the data to operate. 223 * 224 * @return Return true if success. otherwise return false. 225 */ 226 virtual bool NotifyChange(const Uri &uri); 227 228 /** 229 * @brief Registers an observer to DataObsMgr specified by the given Uri. 230 * Return true, need override by users. 231 * 232 * @param uri, Indicates the path of the data to operate. 233 * @param dataObserver, Indicates the IDataAbilityObserver object. 234 */ 235 virtual bool RegisterObserverExtProvider(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver, 236 bool isDescendants); 237 238 /** 239 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 240 * Return true, need override by users. 241 * 242 * @param uri, Indicates the path of the data to operate. 243 * @param dataObserver, Indicates the IDataAbilityObserver object. 244 */ 245 virtual bool UnregisterObserverExtProvider(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 246 247 /** 248 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 249 * Return true, need override by users. 250 * 251 * @param uri, Indicates the path of the data to operate. 252 * 253 * @return Return true if success. otherwise return false. 254 */ 255 virtual bool NotifyChangeExtProvider(const ChangeInfo changeInfo); 256 257 /** 258 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used 259 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if 260 * the context has changed. If you implement URI normalization for a Data ability, you must also implement 261 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 262 * any method that is called on the Data ability must require normalization verification and denormalization. The 263 * default implementation of this method returns null, indicating that this Data ability does not support URI 264 * normalization. 265 * 266 * @param uri Indicates the Uri object to normalize. 267 * 268 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. 269 */ 270 virtual Uri NormalizeUri(const Uri &uri); 271 272 /** 273 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 274 * The default implementation of this method returns the original URI passed to it. 275 * 276 * @param uri uri Indicates the Uri object to denormalize. 277 * 278 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri 279 * passed to this method if there is nothing to do; returns null if the data identified by the original Uri cannot 280 * be found in the current environment. 281 */ 282 virtual Uri DenormalizeUri(const Uri &uri); 283 284 /** 285 * @brief Set a creator function. 286 * 287 * @param creator The function for create a datashare extension ability. 288 */ 289 static void SetCreator(const CreatorFunc& creator); 290 private: 291 static CreatorFunc creator_; 292 }; 293 } // namespace DataShare 294 } // namespace OHOS 295 #endif // DATASHARE_EXT_ABILITY_H