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_HELPER_H 17 #define DATASHARE_HELPER_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 25 #include "data_ability_observer_interface.h" 26 #include "datashare_business_error.h" 27 #include "datashare_errno.h" 28 #include "datashare_observer.h" 29 #include "datashare_operation_statement.h" 30 #include "datashare_predicates.h" 31 #include "datashare_result_set.h" 32 #include "datashare_template.h" 33 #include "datashare_values_bucket.h" 34 #include "uri.h" 35 36 using Uri = OHOS::Uri; 37 38 namespace OHOS { 39 namespace AppExecFwk { 40 class PacMap; 41 class IDataAbilityObserver; 42 } // namespace AppExecFwk 43 44 namespace DataShare { 45 using string = std::string; 46 class DataShareHelper : public std::enable_shared_from_this<DataShareHelper> { 47 public: 48 /** 49 * @brief Destructor. 50 */ 51 virtual ~DataShareHelper() = default; 52 53 /** 54 * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship 55 * between the ability using the Data template (data share for short) and the associated client process in 56 * a DataShareHelper instance. 57 * 58 * @param token Indicates the System token. 59 * @param strUri Indicates the database table or disk file to operate. 60 * @param waitTime connect extension waiting time. 61 * 62 * @return Returns the created DataShareHelper instance. 63 */ 64 [[deprecated( 65 "Use Create(const sptr<IRemoteObject> &, const std::string &, const std::string &, const int &) instead.")]] 66 static std::shared_ptr<DataShareHelper> Creator(const sptr<IRemoteObject> &token, 67 const std::string &strUri, const std::string &extUri = "", const int waitTime = 2, bool isSystem = false); 68 69 /** 70 * @brief Creates a DataShareHelper instance with the Uri and {@link #CreateOptions} . 71 * 72 * @param strUri Indicates the database table or disk file to operate. 73 * @param options Indicates the optional config. 74 * @param waitTime connect extension waiting time. 75 * 76 * @return Returns the created DataShareHelper instance with a specified Uri. 77 */ 78 [[deprecated( 79 "Use Create(const sptr<IRemoteObject> &, const std::string &,const std::string &, const int &) instead.")]] 80 static std::shared_ptr<DataShareHelper> Creator(const std::string &strUri, const CreateOptions &options, 81 const std::string &bundleName = "", const int waitTime = 2, bool isSystem = false); 82 83 /** 84 * @brief Creates a DataShareHelper instance, priority silent access, use non-silent access when silent is not 85 * available, at this time, it is necessary to apply for communication permission with the extension. 86 * 87 * @param token Indicates the System token. 88 * @param strUri Indicates the database table or disk file to operate for silent access. 89 * @param extUri Indicates the database table or disk file to operate for non silent access. 90 * @param waitTime connect extension waiting time. 91 * 92 * @return Returns the created DataShareHelper instance with a specified Uri. 93 */ 94 static std::pair<int, std::shared_ptr<DataShareHelper>> Create(const sptr<IRemoteObject> &token, 95 const std::string &strUri, const std::string &extUri, const int waitTime = 2); 96 97 /** 98 * @brief Releases the client resource of the Data share. 99 * You should call this method to releases client resource after the data operations are complete. 100 * 101 * @return Returns true if the resource is successfully released; returns false otherwise. 102 */ 103 virtual bool Release() = 0; 104 105 /** 106 * @brief Obtains the MIME types of files supported. 107 * 108 * @param uri Indicates the path of the files to obtain. 109 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. 110 * 111 * @return Returns the matched MIME types. If there is no match, null is returned. 112 */ 113 virtual std::vector<std::string> GetFileTypes(Uri &uri, const std::string &mimeTypeFilter) = 0; 114 115 /** 116 * @brief Opens a file in a specified remote path. 117 * 118 * @param uri Indicates the path of the file to open. 119 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 120 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 121 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, 122 * or "rwt" for read and write access that truncates any existing file. 123 * 124 * @return Returns the file descriptor. 125 */ 126 virtual int OpenFile(Uri &uri, const std::string &mode) = 0; 127 128 /** 129 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 130 * inside of their .hap. 131 * 132 * @param uri Indicates the path of the file to open. 133 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 134 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 135 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 136 * data, or "rwt" for read and write access that truncates any existing file. 137 * 138 * @return Returns the RawFileDescriptor object containing file descriptor. 139 */ 140 virtual int OpenRawFile(Uri &uri, const std::string &mode) = 0; 141 142 /** 143 * @brief Inserts a single data record into the database. 144 * 145 * @param uri Indicates the path of the data to operate. 146 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 147 * 148 * @return Returns the index of the inserted data record. 149 */ 150 [[deprecated("Use InsertEx(Uri &, const DataShareValuesBucket &) instead.")]] 151 virtual int Insert(Uri &uri, const DataShareValuesBucket &value) = 0; 152 153 /** 154 * @brief Inserts a single data record into the database. 155 * 156 * @param uri Indicates the path of the data to operate. 157 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 158 * @param result Indicates the result string of the insert operation. 159 * 160 * @return Returns the index of the inserted data record. 161 */ 162 virtual int InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result) = 0; 163 164 /** 165 * @brief Updates data records in the database. 166 * 167 * @param uri Indicates the path of data to update. 168 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 169 * @param value Indicates the data to update. This parameter can be null. 170 * 171 * @return Returns the number of data records updated. 172 */ 173 [[deprecated("Use UpdateEx(Uri &, const DataSharePredicates &, const DataShareValuesBucket &) instead.")]] 174 virtual int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) = 0; 175 176 /** 177 * @brief Batch updates data records in the database. 178 * 179 * @param updateOperations Indicates the param of data to update. 180 * @param results Indicates the number of data records updated. 181 * 182 * @return Return the execution results of batch updates. 183 */ 184 virtual int BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results) = 0; 185 186 /** 187 * @brief Deletes one or more data records from the database. 188 * 189 * @param uri Indicates the path of the data to operate. 190 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 191 * 192 * @return Returns the number of data records deleted. 193 */ 194 [[deprecated("Use DeleteEx(Uri &, const DataSharePredicates &) instead.")]] 195 virtual int Delete(Uri &uri, const DataSharePredicates &predicates) = 0; 196 197 /** 198 * @brief Query records from the database. 199 * 200 * @param uri Indicates the path of data to query. 201 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 202 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 203 * @param businessError Indicates the error by query. 204 * 205 * @return Returns the query result. 206 */ 207 virtual std::shared_ptr<DataShareResultSet> Query(Uri &uri, const DataSharePredicates &predicates, 208 std::vector<std::string> &columns, DatashareBusinessError *businessError = nullptr) = 0; 209 210 /** 211 * @brief Obtains the MIME type matching the data specified by the URI of the Data share. This method should be 212 * implemented by a Data share. Data abilities supports general data types, including text, HTML, and JPEG. 213 * 214 * @param uri Indicates the URI of the data. 215 * 216 * @return Returns the MIME type that matches the data specified by uri. 217 */ 218 virtual std::string GetType(Uri &uri) = 0; 219 220 /** 221 * @brief Inserts multiple data records into the database. 222 * 223 * @param uri Indicates the path of the data to operate. 224 * @param values Indicates the data records to insert. 225 * 226 * @return Returns the number of data records inserted. 227 */ 228 virtual int BatchInsert(Uri &uri, const std::vector<DataShareValuesBucket> &values) = 0; 229 230 /** 231 * @brief Performs batch operations on the database. 232 * 233 * @param statements Indicates a list of database operation statement on the database. 234 * @param result Indicates the result of the operation. 235 * 236 * @return Returns the ipc result. 237 */ 238 virtual int ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result) = 0; 239 240 /** 241 * @brief Registers an observer to DataObsMgr specified by the given Uri. 242 * 243 * @param uri, Indicates the path of the data to operate. 244 * @param dataObserver, Indicates the IDataAbilityObserver object. 245 * 246 * @return Returns the result. Error codes are listed in DataShare datashare_errno.h and 247 * DataObs dataobs_mgr_errors.h. 248 */ 249 virtual int RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0; 250 251 /** 252 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 253 * 254 * @param uri, Indicates the path of the data to operate. 255 * @param dataObserver, Indicates the IDataAbilityObserver object. 256 * 257 * @return Returns the result. Error codes are listed in DataShare datashare_errno.h and 258 * DataObs dataobs_mgr_errors.h. 259 */ 260 virtual int UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) = 0; 261 262 /** 263 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 264 * 265 * @param uri, Indicates the path of the data to operate. 266 */ 267 virtual void NotifyChange(const Uri &uri) = 0; 268 269 /** 270 * Registers an observer to DataObsMgr specified by the given Uri. 271 * 272 * @param uri, Indicates the path of the data to operate. 273 * @param dataObserver, Indicates the IDataAbilityObserver object. 274 * @param isDescendants, Indicates the Whether to note the change of descendants. 275 */ 276 void RegisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver, bool isDescendants, 277 bool isSystem = false); 278 279 /** 280 * Deregisters an observer used for DataObsMgr specified by the given Uri. 281 * 282 * @param uri, Indicates the path of the data to operate. 283 * @param dataObserver, Indicates the IDataAbilityObserver object 284 */ 285 void UnregisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver, bool isSystem = false); 286 287 /** 288 * Notifies the registered observers of a change to the data resource specified by Uris. 289 * 290 * @param changeInfo Indicates the info of the data to operate. 291 */ 292 void NotifyChangeExt(const DataShareObserver::ChangeInfo &changeInfo, bool isSystem = false); 293 294 /** 295 * @brief Converts the given uri that refer to the Data share into a normalized URI. A normalized URI can be used 296 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data share even if the 297 * context has changed. If you implement URI normalization for a Data share, you must also implement 298 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 299 * any method that is called on the Data share must require normalization verification and denormalization. The 300 * default implementation of this method returns null, indicating that this Data share does not support URI 301 * normalization. 302 * 303 * @param uri Indicates the Uri object to normalize. 304 * 305 * @return Returns the normalized Uri object if the Data share supports URI normalization; returns null otherwise. 306 */ 307 virtual Uri NormalizeUri(Uri &uri) = 0; 308 309 /** 310 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 311 * The default implementation of this method returns the original URI passed to it. 312 * 313 * @param uri uri Indicates the Uri object to denormalize. 314 * 315 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed 316 * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found 317 * in the current environment. 318 */ 319 virtual Uri DenormalizeUri(Uri &uri) = 0; 320 321 /** 322 * @brief Adds a template of {@link #SubscribeRdbData}. 323 * @param uri, the uri to add. 324 * @param subscriberId, the subscribe id to add. 325 * @param tpl, the template to add. 326 * @return Returns the error code. 327 */ 328 virtual int AddQueryTemplate(const std::string &uri, int64_t subscriberId, Template &tpl) = 0; 329 330 /** 331 * @brief Deletes a template of {@link #SubscribeRdbData} 332 * @param uri, the uri to delete. 333 * @param subscriberId, the subscribe id to delete. 334 * @return Returns the error code. 335 */ 336 virtual int DelQueryTemplate(const std::string &uri, int64_t subscriberId) = 0; 337 338 /** 339 * @brief Update a single data into host data area. 340 * @param data, the data to publish. 341 * @param bundleName the bundleName of data to publish. 342 * @return Returns the error code. 343 */ 344 virtual std::vector<OperationResult> Publish(const Data &data, const std::string &bundleName) = 0; 345 346 /** 347 * @brief Get published data by bundleName. 348 * @param bundleName, the bundleName of data. 349 * @param resultCode, the errcode returned by function 350 * @return Data {@link #Data} 351 */ 352 virtual Data GetPublishedData(const std::string &bundleName, int &resultCode) = 0; 353 354 /** 355 * @brief Registers observers to observe rdb data specified by the given uris and template. 356 * @param uris, the paths of the data to operate. 357 * @param templateId, the template of observers. 358 * @param callback, the callback function of observers. 359 * @return Returns the error code. 360 */ 361 virtual std::vector<OperationResult> SubscribeRdbData(const std::vector<std::string> &uris, 362 const TemplateId &templateId, const std::function<void(const RdbChangeNode &changeNode)> &callback) = 0; 363 364 /** 365 * @brief Unregisters observers used for monitoring data specified by the given uris and template. 366 * @param uris, the paths of the data to operate, if uris is empty, Unregisters all observers. 367 * @param templateId, the template of observers. 368 * @return Returns the error code. 369 */ 370 virtual std::vector<OperationResult> UnsubscribeRdbData(const std::vector<std::string> &uris, 371 const TemplateId &templateId) = 0; 372 373 /** 374 * @brief Enable observers by the given uris and template. 375 * @param uris, the paths of the data to operate. 376 * @param templateId, the template of observers. 377 * @return Returns the error code. 378 */ 379 virtual std::vector<OperationResult> EnableRdbSubs(const std::vector<std::string> &uris, 380 const TemplateId &templateId) = 0; 381 382 /** 383 * @brief Disable observers by the given uris and template. 384 * @param uris, the paths of the data to operate. 385 * @param templateId, the template of observers. 386 * @return Returns the error code. 387 */ 388 virtual std::vector<OperationResult> DisableRdbSubs(const std::vector<std::string> &uris, 389 const TemplateId &templateId) = 0; 390 391 /** 392 * @brief Registers observers to observe published data specified by the given uris and subscriberId. 393 * @param uris, the uris of the data to operate. 394 * @param subscriberId, the subscriberId of observers. 395 * @param callback, the callback function of observers. 396 * @return Returns the error code. 397 */ 398 virtual std::vector<OperationResult> SubscribePublishedData(const std::vector<std::string> &uris, 399 int64_t subscriberId, const std::function<void(const PublishedDataChangeNode &changeNode)> &callback) = 0; 400 401 /** 402 * @brief Unregisters observers used for monitoring data specified by the given uris and subscriberId. 403 * @param uris, the uris of the data to operate, if uris is empty, Unregisters all observers. 404 * @param subscriberId, the subscriberId of observers. 405 * @return Returns the error code. 406 */ 407 virtual std::vector<OperationResult> UnsubscribePublishedData(const std::vector<std::string> &uris, 408 int64_t subscriberId) = 0; 409 410 /** 411 * @brief Enable observers by the given uris and subscriberId. 412 * @param uris, the paths of the data to operate. 413 * @param subscriberId, the subscriberId of observers. 414 * @return Returns the error code. 415 */ 416 virtual std::vector<OperationResult> EnablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0; 417 418 /** 419 * @brief Disable observers by the given uris and template. 420 * @param uris, the paths of the data to operate. 421 * @param subscriberId, the subscriberId of observers. 422 * @return Returns the error code. 423 */ 424 virtual std::vector<OperationResult> DisablePubSubs(const std::vector<std::string> &uris, int64_t subscriberId) = 0; 425 426 /** 427 * @brief Set default switch for silent access. 428 * @param uri, the uri to disable/enable. 429 * @param enable, the enable of silent switch. 430 * @param isSystem, is system app or not. 431 * @return Returns the error code. 432 */ 433 static int SetSilentSwitch(Uri &uri, bool enable, bool isSystem = false); 434 435 /** 436 * @brief Inserts a single data record into the database. 437 * 438 * @param uri Indicates the path of the data to operate. 439 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 440 * 441 * @return Returns pair<int32_t errCode, int32_t index>, the errCode and the index of the inserted data record. 442 */ 443 virtual std::pair<int32_t, int32_t> InsertEx(Uri &uri, const DataShareValuesBucket &value); 444 445 /** 446 * @brief Updates data records in the database. 447 * 448 * @param uri Indicates the path of data to update. 449 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 450 * @param value Indicates the data to update. This parameter can be null. 451 * 452 * @return Returns pair<int32_t errCode, int32_t index>, the errCode and the index of data records updated. 453 */ 454 virtual std::pair<int32_t, int32_t> UpdateEx( 455 Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value); 456 457 /** 458 * @brief Deletes one or more data records from the database. 459 * 460 * @param uri Indicates the path of the data to operate. 461 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 462 * 463 * @return Returns pair<int32_t errCode, int32_t index>, the errCode and the index of data records deleted. 464 */ 465 virtual std::pair<int32_t, int32_t> DeleteEx(Uri &uri, const DataSharePredicates &predicates); 466 467 /** 468 * @brief UserDefineFunc supports user-defined serialization of data and deserialization of reply. 469 * It directly passes IPC parameters without any processing. 470 * Through this interface, users can implement their own business logic. 471 * 472 * @param data Data sent from the consumer to the provider 473 * @param reply Data returned from the provider to the consumer, including errCode 474 * @param option The options of IPC call 475 * 476 * @return Returns int32_t, the errCode of IPC call. 477 */ 478 virtual int32_t UserDefineFunc(MessageParcel &data, MessageParcel &reply, MessageOption &option); 479 480 private: 481 static std::shared_ptr<DataShareHelper> CreateServiceHelper(const std::string &extUri = "", 482 const std::string &bundleName = "", bool isSystem = false); 483 484 static int GetSilentProxyStatus(const std::string &uri, bool isSystem); 485 486 static std::shared_ptr<DataShareHelper> CreateExtHelper(Uri &uri, const sptr<IRemoteObject> &token, 487 const int waitTime = 2, bool isSystem = false); 488 489 static std::string TransferUriPrefix(const std::string &originPrefix, const std::string &replacedPrefix, 490 const std::string &originUriStr); 491 492 static bool IsProxy(Uri &uri); 493 494 static std::pair<int, std::shared_ptr<DataShareHelper>> CreateProxyHelper(const std::string &strUri, 495 const std::string &extUri); 496 }; 497 } // namespace DataShare 498 } // namespace OHOS 499 #endif // DATASHARE_HELPER_H