1 /*
2  * Copyright (c) 2022-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 #include "distributed_preferences_database.h"
17 
18 #include "ans_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Notification {
22 namespace {
23 const std::string APP_ID = "notification_service";
24 const std::string STORE_ID = "distributed_preferences";
25 constexpr char KV_STORE_PATH[] = "/data/service/el1/public/database/notification_service";
26 }  // namespace
27 
DistributedPreferencesDatabase()28 DistributedPreferencesDatabase::DistributedPreferencesDatabase() : DistributedFlowControl()
29 {
30     GetKvDataManager();
31 }
32 
~DistributedPreferencesDatabase()33 DistributedPreferencesDatabase::~DistributedPreferencesDatabase()
34 {}
35 
GetKvDataManager()36 void DistributedPreferencesDatabase::GetKvDataManager()
37 {
38     kvDataManager_ = std::make_unique<DistributedKv::DistributedKvDataManager>();
39 
40     KvManagerFlowControlClear();
41 }
42 
CheckKvDataManager()43 bool DistributedPreferencesDatabase::CheckKvDataManager()
44 {
45     if (kvDataManager_ == nullptr) {
46         GetKvDataManager();
47     }
48     if (kvDataManager_ == nullptr) {
49         return false;
50     }
51     return true;
52 }
53 
GetKvStore()54 void DistributedPreferencesDatabase::GetKvStore()
55 {
56     if (!CheckKvDataManager()) {
57         return;
58     }
59 
60     DistributedKv::Options options = {
61         .createIfMissing = true,
62         .autoSync = false,
63         .securityLevel = DistributedKv::SecurityLevel::S1,
64         .area = DistributedKv::EL1,
65         .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
66         .baseDir = KV_STORE_PATH
67     };
68     DistributedKv::AppId appId = {.appId = APP_ID};
69     DistributedKv::StoreId storeId = {.storeId = STORE_ID};
70     DistributedKv::Status status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_);
71     if (status != DistributedKv::Status::SUCCESS) {
72         ANS_LOGE("GetSingleKvStore failed ret = 0x%{public}x", status);
73         kvStore_.reset();
74         kvDataManager_.reset();
75     }
76 
77     KvStoreFlowControlClear();
78 }
79 
CheckKvStore()80 bool DistributedPreferencesDatabase::CheckKvStore()
81 {
82     if (kvStore_ == nullptr) {
83         GetKvStore();
84     }
85     if (kvStore_ == nullptr) {
86         return false;
87     }
88     return true;
89 }
90 
PutToDistributedDB(const std::string & key,const std::string & value)91 bool DistributedPreferencesDatabase::PutToDistributedDB(const std::string &key, const std::string &value)
92 {
93     std::lock_guard<std::mutex> lock(mutex_);
94 
95     if (!CheckKvStore()) {
96         return false;
97     }
98 
99     if (!KvStoreFlowControl()) {
100         ANS_LOGE("KvStoreFlowControl is false.");
101         return false;
102     }
103 
104     DistributedKv::Key kvStoreKey(key);
105     DistributedKv::Value kvStoreValue(value);
106     DistributedKv::Status status = kvStore_->Put(kvStoreKey, kvStoreValue);
107     if (status != DistributedKv::Status::SUCCESS) {
108         ANS_LOGE("kvStore Put() failed ret = 0x%{public}x", status);
109         return false;
110     }
111 
112     return true;
113 }
114 
GetFromDistributedDB(const std::string & key,std::string & value)115 bool DistributedPreferencesDatabase::GetFromDistributedDB(const std::string &key, std::string &value)
116 {
117     std::lock_guard<std::mutex> lock(mutex_);
118     if (!CheckKvStore()) {
119         return false;
120     }
121 
122     if (!KvStoreFlowControl()) {
123         ANS_LOGE("KvStoreFlowControl is defeat.");
124         return false;
125     }
126     DistributedKv::Key kvStoreKey(key);
127     DistributedKv::Value kvStoreValue;
128     DistributedKv::Status status = kvStore_->Get(kvStoreKey, kvStoreValue);
129     if (status != DistributedKv::Status::SUCCESS) {
130         ANS_LOGE("kvStore Get() failed ret = 0x%{public}x", status);
131         return false;
132     }
133     value = kvStoreValue.ToString();
134     return true;
135 }
136 
GetEntriesFromDistributedDB(const std::string & prefixKey,std::vector<Entry> & entries)137 bool DistributedPreferencesDatabase::GetEntriesFromDistributedDB(
138     const std::string &prefixKey, std::vector<Entry> &entries)
139 {
140     std::lock_guard<std::mutex> lock(mutex_);
141     if (!CheckKvStore()) {
142         return false;
143     }
144     if (!KvStoreFlowControl()) {
145         ANS_LOGE("KvStoreFlowControl is false.");
146         return false;
147     }
148     DistributedKv::Key kvStoreKey(prefixKey);
149     DistributedKv::Status status = kvStore_->GetEntries(kvStoreKey, entries);
150     if (status != DistributedKv::Status::SUCCESS) {
151         ANS_LOGE("kvStore GetEntries() failed ret = 0x%{public}x", status);
152         return false;
153     }
154     return true;
155 }
156 
DeleteToDistributedDB(const std::string & key)157 bool DistributedPreferencesDatabase::DeleteToDistributedDB(const std::string &key)
158 {
159     std::lock_guard<std::mutex> lock(mutex_);
160 
161     if (!CheckKvStore()) {
162         return false;
163     }
164     if (!KvStoreFlowControl()) {
165         ANS_LOGE("kvStore flow control.");
166         return false;
167     }
168     DistributedKv::Key kvStoreKey(key);
169     DistributedKv::Value kvStoreValue;
170     DistributedKv::Status status = kvStore_->Delete(kvStoreKey);
171     if (status != DistributedKv::Status::SUCCESS) {
172         ANS_LOGE("kvStore Delete() failed ret = 0x%{public}x", status);
173         return false;
174     }
175     return true;
176 }
177 
ClearDatabase()178 bool DistributedPreferencesDatabase::ClearDatabase()
179 {
180     std::lock_guard<std::mutex> lock(mutex_);
181 
182     if (!CheckKvDataManager()) {
183         return false;
184     }
185 
186     if (!KvManagerFlowControl()) {
187         ANS_LOGE("kvDataManager flow control.");
188         return false;
189     }
190 
191     DistributedKv::AppId appId = {.appId = APP_ID};
192     DistributedKv::StoreId storeId = {.storeId = STORE_ID};
193     DistributedKv::Status status = kvDataManager_->CloseKvStore(appId, storeId);
194     if (status != DistributedKv::Status::SUCCESS) {
195         ANS_LOGE("CloseKvStore failed ret = 0x%{public}x", status);
196         return false;
197     }
198 
199     status = kvDataManager_->DeleteKvStore(appId, storeId, KV_STORE_PATH);
200     if (status != DistributedKv::Status::SUCCESS) {
201         ANS_LOGE("DeleteKvStore failed ret = 0x%{public}x", status);
202         return false;
203     }
204     return true;
205 }
206 }  // namespace Notification
207 }  // namespace OHOS