1 /* 2 * Copyright (c) 2021 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_APPEXECFWK_OHOS_DATA_ABILITY_HELPER_H 17 #define FOUNDATION_APPEXECFWK_OHOS_DATA_ABILITY_HELPER_H 18 19 #include <mutex> 20 #include <map> 21 #include <string> 22 23 #include "context.h" 24 #include "uri.h" 25 26 using Uri = OHOS::Uri; 27 28 namespace OHOS { 29 namespace NativeRdb { 30 class AbsSharedResultSet; 31 class DataAbilityPredicates; 32 class ValuesBucket; 33 } // namespace NativeRdb 34 namespace AppExecFwk { 35 using string = std::string; 36 class DataAbilityResult; 37 class DataAbilityOperation; 38 class PacMap; 39 class IDataAbilityObserver; 40 class DataAbilityHelper final : public std::enable_shared_from_this<DataAbilityHelper> { 41 public: 42 ~DataAbilityHelper() = default; 43 44 /** 45 * @brief Creates a DataAbilityHelper instance without specifying the Uri based on the given Context. 46 * 47 * @param context Indicates the Context object on OHOS. 48 * 49 * @return Returns the created DataAbilityHelper instance where Uri is not specified. 50 */ 51 static std::shared_ptr<DataAbilityHelper> Creator(const std::shared_ptr<Context> &context); 52 53 /** 54 * @brief Creates a DataAbilityHelper instance with the Uri specified based on the given Context. 55 * 56 * @param context Indicates the Context object on OHOS. 57 * @param uri Indicates the database table or disk file to operate. 58 * 59 * @return Returns the created DataAbilityHelper instance with a specified Uri. 60 */ 61 static std::shared_ptr<DataAbilityHelper> Creator( 62 const std::shared_ptr<Context> &context, const std::shared_ptr<Uri> &uri); 63 64 /** 65 * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship 66 * between the ability using the Data template (Data ability for short) and the associated client process in 67 * a DataAbilityHelper instance. 68 * 69 * @param context Indicates the Context object on OHOS. 70 * @param uri Indicates the database table or disk file to operate. 71 * @param tryBind Specifies whether the exit of the corresponding Data ability process causes the exit of the 72 * client process. 73 * 74 * @return Returns the created DataAbilityHelper instance. 75 */ 76 static std::shared_ptr<DataAbilityHelper> Creator( 77 const std::shared_ptr<Context> &context, const std::shared_ptr<Uri> &uri, const bool tryBind); 78 79 /** 80 * @brief Creates a DataAbilityHelper instance without specifying the Uri based. 81 * 82 * @param token Indicates the System token. 83 * 84 * @return Returns the created DataAbilityHelper instance where Uri is not specified. 85 */ 86 static std::shared_ptr<DataAbilityHelper> Creator(const sptr<IRemoteObject> &token); 87 88 /** 89 * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship 90 * between the ability using the Data template (Data ability for short) and the associated client process in 91 * a DataAbilityHelper instance. 92 * 93 * @param token Indicates the System token. 94 * @param uri Indicates the database table or disk file to operate. 95 * 96 * @return Returns the created DataAbilityHelper instance. 97 */ 98 static std::shared_ptr<DataAbilityHelper> Creator( 99 const sptr<IRemoteObject> &token, const std::shared_ptr<Uri> &uri); 100 101 /** 102 * @brief Releases the client resource of the Data ability. 103 * You should call this method to releases client resource after the data operations are complete. 104 * 105 * @return Returns true if the resource is successfully released; returns false otherwise. 106 */ 107 bool Release(); 108 /** 109 * @brief Obtains the MIME types of files supported. 110 * 111 * @param uri Indicates the path of the files to obtain. 112 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. 113 * 114 * @return Returns the matched MIME types. If there is no match, null is returned. 115 */ 116 std::vector<std::string> GetFileTypes(Uri &uri, const std::string &mimeTypeFilter); 117 118 /** 119 * @brief Opens a file in a specified remote path. 120 * 121 * @param uri Indicates the path of the file to open. 122 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 123 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 124 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, 125 * or "rwt" for read and write access that truncates any existing file. 126 * 127 * @return Returns the file descriptor. 128 */ 129 int OpenFile(Uri &uri, const std::string &mode); 130 131 /** 132 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 133 * inside of their .hap. 134 * 135 * @param uri Indicates the path of the file to open. 136 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 137 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 138 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 139 * data, or "rwt" for read and write access that truncates any existing file. 140 * 141 * @return Returns the RawFileDescriptor object containing file descriptor. 142 */ 143 int OpenRawFile(Uri &uri, const std::string &mode); 144 145 /** 146 * @brief Inserts a single data record into the database. 147 * 148 * @param uri Indicates the path of the data to operate. 149 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 150 * 151 * @return Returns the index of the inserted data record. 152 */ 153 int Insert(Uri &uri, const NativeRdb::ValuesBucket &value); 154 155 /** 156 * @brief Updates data records in the database. 157 * 158 * @param uri Indicates the path of data to update. 159 * @param value Indicates the data to update. This parameter can be null. 160 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 161 * 162 * @return Returns the number of data records updated. 163 */ 164 int Update(Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates); 165 166 /** 167 * @brief Deletes one or more data records from the database. 168 * 169 * @param uri Indicates the path of the data to operate. 170 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 171 * 172 * @return Returns the number of data records deleted. 173 */ 174 int Delete(Uri &uri, const NativeRdb::DataAbilityPredicates &predicates); 175 176 /** 177 * @brief Deletes one or more data records from the database. 178 * 179 * @param uri Indicates the path of data to query. 180 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 181 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 182 * 183 * @return Returns the query result. 184 */ 185 std::shared_ptr<NativeRdb::AbsSharedResultSet> Query( 186 Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates); 187 188 /** 189 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be 190 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG. 191 * 192 * @param uri Indicates the URI of the data. 193 * 194 * @return Returns the MIME type that matches the data specified by uri. 195 */ 196 std::string GetType(Uri &uri); 197 198 /** 199 * @brief Reloads data in the database. 200 * 201 * @param uri Indicates the position where the data is to reload. This parameter is mandatory. 202 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This 203 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across 204 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object. 205 * 206 * @return Returns true if the data is successfully reloaded; returns false otherwise. 207 */ 208 bool Reload(Uri &uri, const PacMap &extras); 209 210 /** 211 * @brief Inserts multiple data records into the database. 212 * 213 * @param uri Indicates the path of the data to operate. 214 * @param values Indicates the data records to insert. 215 * 216 * @return Returns the number of data records inserted. 217 */ 218 int BatchInsert(Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values); 219 220 /** 221 * @brief Registers an observer to DataObsMgr specified by the given Uri. 222 * 223 * @param uri, Indicates the path of the data to operate. 224 * @param dataObserver, Indicates the IDataAbilityObserver object. 225 */ 226 void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 227 228 /** 229 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 230 * 231 * @param uri, Indicates the path of the data to operate. 232 * @param dataObserver, Indicates the IDataAbilityObserver object. 233 */ 234 void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 235 236 /** 237 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 238 * 239 * @param uri, Indicates the path of the data to operate. 240 */ 241 void NotifyChange(const Uri &uri); 242 243 /** 244 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used 245 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the 246 * context has changed. If you implement URI normalization for a Data ability, you must also implement 247 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 248 * any method that is called on the Data ability must require normalization verification and denormalization. The 249 * default implementation of this method returns null, indicating that this Data ability does not support URI 250 * normalization. 251 * 252 * @param uri Indicates the Uri object to normalize. 253 * 254 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. 255 */ 256 Uri NormalizeUri(Uri &uri); 257 258 /** 259 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 260 * The default implementation of this method returns the original URI passed to it. 261 * 262 * @param uri uri Indicates the Uri object to denormalize. 263 * 264 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed 265 * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found 266 * in the current environment. 267 */ 268 Uri DenormalizeUri(Uri &uri); 269 270 /** 271 * @brief Performs batch operations on the database. 272 * 273 * @param uri Indicates the path of data to operate. 274 * @param operations Indicates a list of database operations on the database. 275 * @return Returns the result of each operation, in array. 276 */ 277 std::vector<std::shared_ptr<DataAbilityResult>> ExecuteBatch( 278 const Uri &uri, const std::vector<std::shared_ptr<DataAbilityOperation>> &operations); 279 280 private: 281 DataAbilityHelper(const std::shared_ptr<Context> &context, const std::shared_ptr<Uri> &uri, 282 const sptr<AAFwk::IAbilityScheduler> &dataAbilityProxy, bool tryBind = false); 283 DataAbilityHelper(const std::shared_ptr<Context> &context); 284 DataAbilityHelper(const sptr<IRemoteObject> &token, const std::shared_ptr<Uri> &uri, 285 const sptr<AAFwk::IAbilityScheduler> &dataAbilityProxy); 286 DataAbilityHelper(const sptr<IRemoteObject> &token); 287 288 void AddDataAbilityDeathRecipient(const sptr<IRemoteObject> &token); 289 void OnSchedulerDied(const wptr<IRemoteObject> &remote); 290 291 bool CheckUriParam(const Uri &uri); 292 bool CheckOhosUri(const Uri &uri); 293 294 sptr<IRemoteObject> token_; 295 std::weak_ptr<Context> context_; 296 std::shared_ptr<Uri> uri_ = nullptr; 297 bool tryBind_ = false; 298 bool isSystemCaller_ = false; 299 sptr<AAFwk::IAbilityScheduler> dataAbilityProxy_ = nullptr; 300 std::mutex lock_; 301 static std::mutex oplock_; 302 303 sptr<IRemoteObject::DeathRecipient> callerDeathRecipient_ = nullptr; // caller binderDied Recipient 304 305 std::map<sptr<AAFwk::IDataAbilityObserver>, sptr<AAFwk::IAbilityScheduler>> registerMap_; 306 307 std::map<sptr<AAFwk::IDataAbilityObserver>, std::string> uriMap_; 308 309 }; 310 311 class DataAbilityDeathRecipient : public IRemoteObject::DeathRecipient { 312 public: 313 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 314 315 explicit DataAbilityDeathRecipient(RemoteDiedHandler handler); 316 317 virtual ~DataAbilityDeathRecipient(); 318 319 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 320 321 private: 322 RemoteDiedHandler handler_; 323 }; 324 325 } // namespace AppExecFwk 326 } // namespace OHOS 327 #endif // FOUNDATION_APPEXECFWK_OHOS_DATA_ABILITY_HELPER_H