1 /*
2  * Copyright (c) 2021-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 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 "distributed_kv_data_manager.h"
23 #include "account_error_no.h"
24 #include "iaccount_info.h"
25 
26 namespace OHOS {
27 namespace AccountSA {
28 struct AccountDataStorageOptions {
29     bool encrypt = false;
30     bool autoSync = false;
31     DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1;
32     OHOS::DistributedKv::Area area = OHOS::DistributedKv::EL1;
33     std::string baseDir;
34 };
35 
36 class AccountDataStorage {
37 public:
38     AccountDataStorage() = delete;
39     AccountDataStorage(const std::string &appId, const std::string &storeId, const AccountDataStorageOptions &options);
40     virtual ~AccountDataStorage();
41     ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos);
42     ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo);
43     ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo);
44     ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos);
45     void TryTwice(const std::function<DistributedKv::Status()> &func) const;
46     virtual void SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries,
47         std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0;
48     int DeleteKvStore();
49     ErrCode GetAccountInfoById(const std::string id, IAccountInfo &iAccountInfo);
50     bool IsKeyExists(const std::string keyStr);
51     ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr);
52     ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr);
53     ErrCode RemoveValueFromKvStore(const std::string &keyStr);
54     ErrCode MoveData(const std::shared_ptr<AccountDataStorage> &ptr);
55 
56 protected:
57     OHOS::DistributedKv::Status GetEntries(
58         std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const;
59     OHOS::DistributedKv::Status GetKvStore();
60     bool CheckKvStore();
61     OHOS::DistributedKv::DistributedKvDataManager dataManager_;
62     std::shared_ptr<OHOS::DistributedKv::SingleKvStore> kvStorePtr_;
63     mutable std::mutex kvStorePtrMutex_;
64     OHOS::DistributedKv::AppId appId_;
65     OHOS::DistributedKv::StoreId storeId_;
66     AccountDataStorageOptions options_;
67     std::string baseDir_;
68 };
69 }  // namespace AccountSA
70 }  // namespace OHOS
71 
72 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H
73