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