• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "configuration.h"
17 
18 #include <nlohmann/json.hpp>
19 
20 #include "ability_base_log_wrapper.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 using json = nlohmann::json;
Configuration()26 Configuration::Configuration()
27 {}
28 
Configuration(const Configuration & other)29 Configuration::Configuration(const Configuration &other) : defaultDisplayId_(other.defaultDisplayId_)
30 {
31     configParameter_ = other.configParameter_;
32 }
33 
operator =(const Configuration & other)34 Configuration& Configuration::operator=(const Configuration &other)
35 {
36     if (this == &other) {
37         return *this;
38     }
39 
40     defaultDisplayId_ = other.defaultDisplayId_;
41     configParameter_.clear();
42     configParameter_ = other.configParameter_;
43     return *this;
44 }
45 
~Configuration()46 Configuration::~Configuration()
47 {}
48 
MakeTheKey(std::string & getKey,int id,const std::string & param) const49 bool Configuration::MakeTheKey(std::string &getKey, int id, const std::string &param) const
50 {
51     if (param.empty()) {
52         return false;
53     }
54 
55     if (std::find(ConfigurationInner::SystemConfigurationKeyStore.begin(),
56         ConfigurationInner::SystemConfigurationKeyStore.end(), param) ==
57         ConfigurationInner::SystemConfigurationKeyStore.end()) {
58         return false;
59     }
60 
61     getKey.clear();
62     getKey += std::to_string(id);
63     getKey += ConfigurationInner::CONNECTION_SYMBOL;
64     getKey += param;
65     ABILITYBASE_LOGD(" getKey [%{public}s]", getKey.c_str());
66 
67     return true;
68 }
69 
AddItem(int displayId,const std::string & key,const std::string & value)70 bool Configuration::AddItem(int displayId, const std::string &key, const std::string &value)
71 {
72     if (key.empty() || value.empty()) {
73         return false;
74     }
75 
76     std::string getKey;
77     if (!MakeTheKey(getKey, displayId, key)) {
78         return false;
79     }
80 
81     configParameter_[getKey] = value;
82     return true;
83 }
84 
GetItem(int displayId,const std::string & key) const85 std::string Configuration::GetItem(int displayId, const std::string &key) const
86 {
87     if (key.empty()) {
88         return ConfigurationInner::EMPTY_STRING;
89     }
90 
91     std::string getKey;
92     if (!MakeTheKey(getKey, displayId, key)) {
93         return ConfigurationInner::EMPTY_STRING;
94     }
95 
96     auto iter = configParameter_.find(getKey);
97     if (iter != configParameter_.end()) {
98         return iter->second;
99     }
100 
101     return ConfigurationInner::EMPTY_STRING;
102 }
103 
GetItemSize() const104 int Configuration::GetItemSize() const
105 {
106     return configParameter_.size();
107 }
108 
GetAllKey(std::vector<std::string> & keychain) const109 void Configuration::GetAllKey(std::vector<std::string> &keychain) const
110 {
111     keychain.clear();
112     for (const auto &it :configParameter_) {
113         keychain.push_back(it.first);
114     }
115 }
116 
GetValue(const std::string & key) const117 std::string Configuration::GetValue(const std::string &key) const
118 {
119     auto iter = configParameter_.find(key);
120     if (iter != configParameter_.end()) {
121         return iter->second;
122     }
123 
124     return ConfigurationInner::EMPTY_STRING;
125 }
126 
CompareDifferent(std::vector<std::string> & diffKeyV,const Configuration & other)127 void Configuration::CompareDifferent(std::vector<std::string> &diffKeyV, const Configuration &other)
128 {
129     if (other.GetItemSize() == 0) {
130         return;
131     }
132 
133     diffKeyV.clear();
134     std::vector<std::string> otherk;
135     other.GetAllKey(otherk);
136     for (const auto &iter : otherk) {
137         ABILITYBASE_LOGW(" iter : [%{public}s] | Val: [%{public}s]", iter.c_str(), other.GetValue(iter).c_str());
138         // Insert new content directly
139         auto pair = configParameter_.insert(std::make_pair(iter, other.GetValue(iter)));
140         if (pair.second) {
141             diffKeyV.push_back(iter); // One of the changes this time
142             continue;
143         }
144         // Compare what you already have
145         if (!other.GetValue(iter).empty() && other.GetValue(iter) != GetValue(iter)) {
146             diffKeyV.push_back(iter);
147         }
148     }
149 }
150 
Merge(const std::vector<std::string> & diffKeyV,const Configuration & other)151 void Configuration::Merge(const std::vector<std::string> &diffKeyV, const Configuration &other)
152 {
153     if (diffKeyV.empty()) {
154         return;
155     }
156     for (const auto &mergeItemKey : diffKeyV) {
157         auto myItem = GetValue(mergeItemKey);
158         auto otherItem = other.GetValue(mergeItemKey);
159         // myItem possible empty
160         if (!otherItem.empty() && otherItem != myItem) {
161             configParameter_[mergeItemKey] = otherItem;
162         }
163     }
164 }
165 
RemoveItem(int displayId,const std::string & key)166 int Configuration::RemoveItem(int displayId, const std::string &key)
167 {
168     if (key.empty()) {
169         return 0;
170     }
171 
172     std::string getKey;
173     if (!MakeTheKey(getKey, displayId, key)) {
174         return 0;
175     }
176 
177     return configParameter_.erase(getKey);
178 }
179 
AddItem(const std::string & key,const std::string & value)180 bool Configuration::AddItem(const std::string &key, const std::string &value)
181 {
182     return AddItem(defaultDisplayId_, key, value);
183 }
184 
GetItem(const std::string & key) const185 std::string Configuration::GetItem(const std::string &key) const
186 {
187     return GetItem(defaultDisplayId_, key);
188 }
189 
RemoveItem(const std::string & key)190 int Configuration::RemoveItem(const std::string &key)
191 {
192     return RemoveItem(defaultDisplayId_, key);
193 }
194 
GetName() const195 const std::string& Configuration::GetName() const
196 {
197     json configArray(configParameter_);
198     toStrintg_ = configArray.dump();
199     return toStrintg_;
200 }
201 
ReadFromParcel(Parcel & parcel)202 bool Configuration::ReadFromParcel(Parcel &parcel)
203 {
204     ABILITYBASE_LOGD("ReadFromParcel");
205     defaultDisplayId_ = parcel.ReadInt32();
206     int32_t configSize = parcel.ReadInt32();
207     std::vector<std::string> keys;
208     std::vector<std::string> values;
209     keys.clear();
210     values.clear();
211     if (!parcel.ReadStringVector(&keys)) {
212         ABILITYBASE_LOGE("ReadStringVector for keys failed.");
213         return false;
214     }
215     if (!parcel.ReadStringVector(&values)) {
216         ABILITYBASE_LOGE("ReadStringVector for values failed.");
217         return false;
218     }
219     size_t keySize = keys.size();
220     size_t valueSize = values.size();
221     if (keySize != valueSize || configSize != (int32_t)valueSize) {
222         ABILITYBASE_LOGE("ReadFromParcel failed, invalid size.");
223         return false;
224     }
225     std::string key;
226     std::string val;
227     for (int32_t i = 0; i < configSize; i++) {
228         key = keys.at(i);
229         val = values.at(i);
230         configParameter_.emplace(key, val);
231     }
232     return true;
233 }
234 
Unmarshalling(Parcel & parcel)235 Configuration *Configuration::Unmarshalling(Parcel &parcel)
236 {
237     Configuration *Configuration = new (std::nothrow) OHOS::AppExecFwk::Configuration();
238     if (Configuration && !Configuration->ReadFromParcel(parcel)) {
239         delete Configuration;
240         Configuration = nullptr;
241     }
242     return Configuration;
243 }
244 
Marshalling(Parcel & parcel) const245 bool Configuration::Marshalling(Parcel &parcel) const
246 {
247     ABILITYBASE_LOGW("Marshalling");
248     std::vector<std::string> keys;
249     std::vector<std::string> values;
250     keys.clear();
251     values.clear();
252     parcel.WriteInt32(defaultDisplayId_);
253     parcel.WriteInt32(configParameter_.size());
254     for (const auto &config : configParameter_) {
255         keys.emplace_back(config.first);
256         values.emplace_back(config.second);
257     }
258 
259     parcel.WriteStringVector(keys);
260     parcel.WriteStringVector(values);
261     return true;
262 }
263 }  // namespace AppExecFwk
264 }  // namespace OHOS
265