1 /* 2 * Copyright (c) 2021-2025 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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 18 19 #include <string> 20 #include <map> 21 22 #include "account_error_no.h" 23 #include "app_account_info.h" 24 #include "os_account_info.h" 25 #ifndef SQLITE_DLCLOSE_ENABLE 26 #include "distributed_kv_data_manager.h" 27 #else 28 #include "database_adapter_loader.h" 29 #endif // SQLITE_DLCLOSE_ENABLE 30 31 namespace OHOS { 32 namespace AccountSA { 33 #ifndef SQLITE_DLCLOSE_ENABLE 34 struct AccountDataStorageOptions { 35 bool encrypt = false; 36 bool autoSync = false; 37 DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1; 38 OHOS::DistributedKv::Area area = OHOS::DistributedKv::EL1; 39 std::string baseDir; 40 }; 41 #endif // SQLITE_DLCLOSE_ENABLE 42 43 #ifndef SQLITE_DLCLOSE_ENABLE 44 class AccountDataStorage { 45 public: 46 AccountDataStorage() = delete; 47 AccountDataStorage(const std::string &appId, const std::string &storeId, const AccountDataStorageOptions &options); 48 virtual ~AccountDataStorage(); 49 ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 50 ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo); 51 ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo); 52 ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 53 void TryTwice(const std::function<DistributedKv::Status()> &func) const; 54 virtual void SaveEntries(const std::vector<OHOS::DistributedKv::Entry> &allEntries, 55 std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0; 56 ErrCode Close(); 57 int DeleteKvStore(); 58 ErrCode GetAccountInfoById(const std::string id, OHOS::AccountSA::AppAccountInfo &accountInfo); 59 ErrCode GetAccountInfoById(const std::string id, OHOS::AccountSA::OsAccountInfo &accountInfo); 60 bool IsKeyExists(const std::string keyStr); 61 ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr); 62 ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr); 63 ErrCode RemoveValueFromKvStore(const std::string &keyStr); 64 ErrCode MoveData(const std::shared_ptr<AccountDataStorage> &ptr); 65 ErrCode StartTransaction(); 66 ErrCode Commit(); 67 ErrCode Rollback(); 68 69 protected: 70 OHOS::DistributedKv::Status GetEntries( 71 std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const; 72 OHOS::DistributedKv::Status GetKvStore(); 73 bool CheckKvStore(); 74 OHOS::DistributedKv::DistributedKvDataManager dataManager_; 75 std::shared_ptr<OHOS::DistributedKv::SingleKvStore> kvStorePtr_; 76 mutable std::recursive_mutex kvStorePtrMutex_; 77 OHOS::DistributedKv::AppId appId_; 78 OHOS::DistributedKv::StoreId storeId_; 79 AccountDataStorageOptions options_; 80 std::string baseDir_; 81 std::recursive_mutex transactionMutex_; 82 }; 83 #else 84 class AccountDataStorage { 85 public: 86 AccountDataStorage() = delete; 87 AccountDataStorage(const std::string &appId, const std::string &storeId, const DbAdapterOptions &options); 88 virtual ~AccountDataStorage(); 89 ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 90 ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo); 91 ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo); 92 ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 93 void TryTwice(const std::function<DbAdapterStatus()> &func) const; 94 virtual void SaveEntries(const std::vector<DbAdapterEntry> &allEntries, 95 std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0; 96 ErrCode Close(); 97 int DeleteKvStore(); 98 ErrCode GetAccountInfoById(const std::string id, OHOS::AccountSA::AppAccountInfo &accountInfo); 99 ErrCode GetAccountInfoById(const std::string id, OHOS::AccountSA::OsAccountInfo &accountInfo); 100 bool IsKeyExists(const std::string keyStr); 101 ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr); 102 ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr); 103 ErrCode RemoveValueFromKvStore(const std::string &keyStr); 104 ErrCode MoveData(const std::shared_ptr<AccountDataStorage> &ptr); 105 ErrCode StartTransaction(); 106 ErrCode Commit(); 107 ErrCode Rollback(); 108 109 protected: 110 DbAdapterStatus GetEntries( 111 std::string subId, std::vector<DbAdapterEntry> &allEntries) const; 112 DbAdapterStatus GetKvStore(); 113 bool CheckKvStore(); 114 std::shared_ptr<IDbAdapterDataManager> dataManager_ = nullptr; 115 std::shared_ptr<IDbAdapterSingleStore> kvStorePtr_ = nullptr; 116 mutable std::recursive_mutex kvStorePtrMutex_; 117 std::string appId_; 118 std::string storeId_; 119 DbAdapterOptions options_; 120 std::string baseDir_; 121 }; 122 #endif // SQLITE_DLCLOSE_ENABLE 123 124 typedef std::unique_ptr<bool, std::function<void(bool *)>> DatabaseTransaction; 125 126 ErrCode StartDbTransaction( 127 const std::shared_ptr<AccountDataStorage> &dataStoragePtr, DatabaseTransaction &dbTransaction); 128 ErrCode CommitDbTransaction( 129 const std::shared_ptr<AccountDataStorage> &dataStoragePtr, DatabaseTransaction &dbTransaction); 130 } // namespace AccountSA 131 } // namespace OHOS 132 133 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 134