1 /*
2  * Copyright (c) 2023 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 DLP_KV_DATA_STORAGE_H
17 #define DLP_KV_DATA_STORAGE_H
18 
19 #include <map>
20 #include <string>
21 #include "distributed_kv_data_manager.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace DlpPermission {
26 struct KvDataStorageOptions {
27     bool autoSync = false;
28     DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1;
29     OHOS::DistributedKv::Area area = OHOS::DistributedKv::EL1;
30     std::string baseDir;
31 };
32 
33 class DlpKvDataStorage {
34 public:
35     DlpKvDataStorage() = delete;
36     DlpKvDataStorage(const std::string &storeId, const KvDataStorageOptions &options);
37     virtual ~DlpKvDataStorage();
38     void TryTwice(const std::function<DistributedKv::Status()> &func) const;
39     int32_t LoadAllData(std::map<std::string, std::string> &infos);
40     int32_t AddOrUpdateValue(const std::string &key, const std::string &value);
41     int DeleteKvStore();
42     bool IsKeyExists(const std::string &keyStr);
43     int32_t PutValueToKvStore(const std::string &keyStr, const std::string &valueStr);
44     int32_t GetValueFromKvStore(const std::string &keyStr, std::string &valueStr);
45     int32_t RemoveValueFromKvStore(const std::string &keyStr);
46     virtual void SaveEntries(const std::vector<OHOS::DistributedKv::Entry> &allEntries,
47         std::map<std::string, std::string> &infos) = 0;
48 
49 protected:
50     OHOS::DistributedKv::Status GetEntries(
51         std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const;
52     OHOS::DistributedKv::Status GetKvStore();
53     bool CheckKvStore();
54     OHOS::DistributedKv::DistributedKvDataManager dataManager_;
55     std::shared_ptr<OHOS::DistributedKv::SingleKvStore> kvStorePtr_;
56     mutable std::mutex kvStorePtrMutex_;
57     OHOS::DistributedKv::AppId appId_;
58     OHOS::DistributedKv::StoreId storeId_;
59     KvDataStorageOptions options_;
60     std::string baseDir_;
61 };
62 }  // namespace DlpPermission
63 }  // namespace Security
64 }  // namespace OHOS
65 #endif  // DLP_KV_DATA_STORAGE_H
66