• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         .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("kvDataManager GetSingleKvStore failed ret = 0x%{public}x", status);
73         kvStore_.reset();
74         kvDataManager_.reset();
75     }
76 
77     KvStoreFlowControlClear();
78 }
79 
CheckKvStore(void)80 bool DistributedPreferencesDatabase::CheckKvStore(void)
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("kvStore flow control.");
101         CloseKvStore();
102         return false;
103     }
104 
105     DistributedKv::Key kvStoreKey(key);
106     DistributedKv::Value kvStoreValue(value);
107     DistributedKv::Status status = kvStore_->Put(kvStoreKey, kvStoreValue);
108     CloseKvStore();
109     if (status != DistributedKv::Status::SUCCESS) {
110         ANS_LOGE("kvStore Put() failed ret = 0x%{public}x", status);
111         return false;
112     }
113 
114     return true;
115 }
116 
GetFromDistributedDB(const std::string & key,std::string & value)117 bool DistributedPreferencesDatabase::GetFromDistributedDB(const std::string &key, std::string &value)
118 {
119     std::lock_guard<std::mutex> lock(mutex_);
120     if (!CheckKvStore()) {
121         return false;
122     }
123 
124     if (!KvStoreFlowControl()) {
125         ANS_LOGE("kvStore flow control.");
126         CloseKvStore();
127         return false;
128     }
129     DistributedKv::Key kvStoreKey(key);
130     DistributedKv::Value kvStoreValue;
131     DistributedKv::Status status = kvStore_->Get(kvStoreKey, kvStoreValue);
132     CloseKvStore();
133     if (status != DistributedKv::Status::SUCCESS) {
134         ANS_LOGE("kvStore Get() failed ret = 0x%{public}x", status);
135         return false;
136     }
137     value = kvStoreValue.ToString();
138     return true;
139 }
140 
GetEntriesFromDistributedDB(const std::string & prefixKey,std::vector<Entry> & entries)141 bool DistributedPreferencesDatabase::GetEntriesFromDistributedDB(
142     const std::string &prefixKey, std::vector<Entry> &entries)
143 {
144     std::lock_guard<std::mutex> lock(mutex_);
145     if (!CheckKvStore()) {
146         return false;
147     }
148     if (!KvStoreFlowControl()) {
149         ANS_LOGE("kvStore flow control.");
150         CloseKvStore();
151         return false;
152     }
153     DistributedKv::Key kvStoreKey(prefixKey);
154     DistributedKv::Status status = kvStore_->GetEntries(kvStoreKey, entries);
155     CloseKvStore();
156     if (status != DistributedKv::Status::SUCCESS) {
157         ANS_LOGE("kvStore GetEntries() failed ret = 0x%{public}x", status);
158         return false;
159     }
160     return true;
161 }
162 
DeleteToDistributedDB(const std::string & key)163 bool DistributedPreferencesDatabase::DeleteToDistributedDB(const std::string &key)
164 {
165     std::lock_guard<std::mutex> lock(mutex_);
166 
167     if (!CheckKvStore()) {
168         return false;
169     }
170     if (!KvStoreFlowControl()) {
171         ANS_LOGE("kvStore flow control.");
172         CloseKvStore();
173         return false;
174     }
175     DistributedKv::Key kvStoreKey(key);
176     DistributedKv::Value kvStoreValue;
177     DistributedKv::Status status = kvStore_->Delete(kvStoreKey);
178     CloseKvStore();
179     if (status != DistributedKv::Status::SUCCESS) {
180         ANS_LOGE("kvStore Delete() failed ret = 0x%{public}x", status);
181         return false;
182     }
183     return true;
184 }
185 
ClearDatabase(void)186 bool DistributedPreferencesDatabase::ClearDatabase(void)
187 {
188     std::lock_guard<std::mutex> lock(mutex_);
189 
190     if (!CheckKvDataManager()) {
191         return false;
192     }
193 
194     if (!KvManagerFlowControl()) {
195         ANS_LOGE("kvDataManager flow control.");
196         return false;
197     }
198 
199     DistributedKv::AppId appId = {.appId = APP_ID};
200     DistributedKv::StoreId storeId = {.storeId = STORE_ID};
201     DistributedKv::Status status = kvDataManager_->CloseKvStore(appId, storeId);
202     if (status != DistributedKv::Status::SUCCESS) {
203         ANS_LOGE("kvDataManager CloseKvStore() failed ret = 0x%{public}x", status);
204         return false;
205     }
206 
207     status = kvDataManager_->DeleteKvStore(appId, storeId, KV_STORE_PATH);
208     if (status != DistributedKv::Status::SUCCESS) {
209         ANS_LOGE("kvDataManager DeleteKvStore() failed ret = 0x%{public}x", status);
210         return false;
211     }
212     return true;
213 }
214 
CloseKvStore()215 void DistributedPreferencesDatabase::CloseKvStore()
216 {
217     DistributedKv::AppId appId = {.appId = APP_ID};
218     kvDataManager_->CloseKvStore(appId, kvStore_);
219 }
220 }  // namespace Notification
221 }  // namespace OHOS