• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sec_comp_base.h"
16 
17 #include "sec_comp_err.h"
18 #include "sec_comp_log.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace SecurityComponent {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompBase"};
25 }
26 
27 const std::string JsonTagConstants::JSON_RECT = "rect";
28 const std::string JsonTagConstants::JSON_SC_TYPE = "type";
29 const std::string JsonTagConstants::JSON_NODE_ID = "nodeId";
30 const std::string JsonTagConstants::JSON_RECT_X = "x";
31 const std::string JsonTagConstants::JSON_RECT_Y = "y";
32 const std::string JsonTagConstants::JSON_RECT_WIDTH = "width";
33 const std::string JsonTagConstants::JSON_RECT_HEIGHT = "height";
34 const std::string JsonTagConstants::JSON_WINDOW_RECT = "windowRect";
35 const std::string JsonTagConstants::JSON_SIZE_TAG = "size";
36 const std::string JsonTagConstants::JSON_FONT_SIZE_TAG = "fontSize";
37 const std::string JsonTagConstants::JSON_ICON_SIZE_TAG = "iconSize";
38 const std::string JsonTagConstants::JSON_PADDING_SIZE_TAG = "paddingSize";
39 const std::string JsonTagConstants::JSON_PADDING_LEFT_TAG = "left";
40 const std::string JsonTagConstants::JSON_PADDING_TOP_TAG = "top";
41 const std::string JsonTagConstants::JSON_PADDING_RIGHT_TAG = "right";
42 const std::string JsonTagConstants::JSON_PADDING_BOTTOM_TAG = "bottom";
43 const std::string JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG = "textIconSpace";
44 const std::string JsonTagConstants::JSON_RECT_WIDTH_TAG = "width";
45 const std::string JsonTagConstants::JSON_RECT_HEIGHT_TAG = "height";
46 const std::string JsonTagConstants::JSON_COLORS_TAG = "colors";
47 const std::string JsonTagConstants::JSON_FONT_COLOR_TAG = "fontColor";
48 const std::string JsonTagConstants::JSON_ICON_COLOR_TAG = "iconColor";
49 const std::string JsonTagConstants::JSON_BG_COLOR_TAG = "bgColor";
50 const std::string JsonTagConstants::JSON_BORDER_TAG = "border";
51 const std::string JsonTagConstants::JSON_BORDER_WIDTH_TAG = "borderWidth";
52 const std::string JsonTagConstants::JSON_PARENT_TAG = "parent";
53 const std::string JsonTagConstants::JSON_PARENT_EFFECT_TAG = "parentEffect";
54 const std::string JsonTagConstants::JSON_STYLE_TAG = "style";
55 const std::string JsonTagConstants::JSON_TEXT_TAG = "text";
56 const std::string JsonTagConstants::JSON_ICON_TAG = "icon";
57 const std::string JsonTagConstants::JSON_BG_TAG = "bg";
58 
ParseDimension(const nlohmann::json & json,const std::string & tag,DimensionT & res)59 bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res)
60 {
61     if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) {
62         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
63         return false;
64     }
65 
66     res = json.at(tag).get<double>();
67     return true;
68 }
69 
ParseColor(const nlohmann::json & json,const std::string & tag,SecCompColor & res)70 bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res)
71 {
72     if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
73         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
74         return false;
75     }
76 
77     res.value = json.at(tag).get<uint32_t>();
78     return true;
79 }
80 
ParseBool(const nlohmann::json & json,const std::string & tag,bool & res)81 bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res)
82 {
83     if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) {
84         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
85         return false;
86     }
87 
88     res = json.at(tag).get<bool>();
89     return true;
90 }
91 
ParsePadding(const nlohmann::json & json,const std::string & tag,PaddingSize & res)92 bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res)
93 {
94     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
95         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
96         return false;
97     }
98 
99     auto jsonPadding = json.at(tag);
100     if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, res.top)) {
101         return false;
102     }
103     if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, res.right)) {
104         return false;
105     }
106     if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, res.bottom)) {
107         return false;
108     }
109     if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, res.left)) {
110         return false;
111     }
112     return true;
113 }
114 
ParseColors(const nlohmann::json & json,const std::string & tag)115 bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag)
116 {
117     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
118         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
119         return false;
120     }
121     auto jsonColors = json.at(tag);
122     if (!ParseColor(jsonColors, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_)) {
123         return false;
124     }
125     if (!ParseColor(jsonColors, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_)) {
126         return false;
127     }
128     if (!ParseColor(jsonColors, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_)) {
129         return false;
130     }
131     return true;
132 }
133 
ParseBorders(const nlohmann::json & json,const std::string & tag)134 bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag)
135 {
136     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
137         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
138         return false;
139     }
140     auto jsonBorder = json.at(tag);
141     return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_);
142 }
143 
ParseSize(const nlohmann::json & json,const std::string & tag)144 bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag)
145 {
146     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
147         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
148         return false;
149     }
150 
151     auto jsonSize = json.at(tag);
152     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) {
153         return false;
154     }
155 
156     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_)) {
157         return false;
158     }
159 
160     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) {
161         return false;
162     }
163 
164     if (!ParsePadding(jsonSize, JsonTagConstants::JSON_PADDING_SIZE_TAG, padding_)) {
165         return false;
166     }
167 
168     return true;
169 }
170 
ParseParent(const nlohmann::json & json,const std::string & tag)171 bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag)
172 {
173     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
174         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
175         return false;
176     }
177     auto jsonParent = json.at(tag);
178     return ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_);
179 }
180 
ParseRect(const nlohmann::json & json,const std::string & tag,SecCompRect & rect)181 bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect)
182 {
183     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
184         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
185         return false;
186     }
187 
188     auto jsonSize = json.at(tag);
189     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_X, rect.x_)) {
190         return false;
191     }
192 
193     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_Y, rect.y_)) {
194         return false;
195     }
196 
197     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_WIDTH, rect.width_)) {
198         return false;
199     }
200 
201     if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_HEIGHT, rect.height_)) {
202         return false;
203     }
204 
205     return true;
206 }
207 
FromJson(const nlohmann::json & jsonSrc)208 bool SecCompBase::FromJson(const nlohmann::json& jsonSrc)
209 {
210     SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str());
211     if ((jsonSrc.find(JsonTagConstants::JSON_SC_TYPE) == jsonSrc.end()) ||
212         !jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).is_number()) {
213         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_SC_TYPE.c_str());
214         return false;
215     }
216     int32_t value = jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).get<int32_t>();
217     if ((value <= static_cast<int32_t>(SecCompType::UNKNOWN_SC_TYPE)) ||
218         (value >= static_cast<int32_t>(SecCompType::MAX_SC_TYPE))) {
219         SC_LOG_ERROR(LABEL, "scType is invalid.");
220         return false;
221     }
222     type_ = static_cast<SecCompType>(value);
223 
224     if ((jsonSrc.find(JsonTagConstants::JSON_NODE_ID) == jsonSrc.end()) ||
225         !jsonSrc.at(JsonTagConstants::JSON_NODE_ID).is_number()) {
226         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_NODE_ID.c_str());
227         return false;
228     }
229     nodeId_ = jsonSrc.at(JsonTagConstants::JSON_NODE_ID).get<int32_t>();
230 
231     if (!ParseRect(jsonSrc, JsonTagConstants::JSON_RECT, rect_)) {
232         return false;
233     }
234     if (!ParseRect(jsonSrc, JsonTagConstants::JSON_WINDOW_RECT, windowRect_)) {
235         return false;
236     }
237     if (!ParseSize(jsonSrc, JsonTagConstants::JSON_SIZE_TAG)) {
238         return false;
239     }
240     if (!ParseColors(jsonSrc, JsonTagConstants::JSON_COLORS_TAG)) {
241         return false;
242     }
243     if (!ParseBorders(jsonSrc, JsonTagConstants::JSON_BORDER_TAG)) {
244         return false;
245     }
246     if (!ParseParent(jsonSrc, JsonTagConstants::JSON_PARENT_TAG)) {
247         return false;
248     }
249     if (!ParseStyle(jsonSrc, JsonTagConstants::JSON_STYLE_TAG)) {
250         return false;
251     }
252 
253     return true;
254 }
255 
ToJson(nlohmann::json & jsonRes) const256 void SecCompBase::ToJson(nlohmann::json& jsonRes) const
257 {
258     jsonRes[JsonTagConstants::JSON_SC_TYPE] = type_;
259     jsonRes[JsonTagConstants::JSON_NODE_ID] = nodeId_;
260     jsonRes[JsonTagConstants::JSON_RECT] = nlohmann::json {
261         {JsonTagConstants::JSON_RECT_X, rect_.x_},
262         {JsonTagConstants::JSON_RECT_Y, rect_.y_},
263         {JsonTagConstants::JSON_RECT_WIDTH, rect_.width_},
264         {JsonTagConstants::JSON_RECT_HEIGHT, rect_.height_}
265     };
266     jsonRes[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
267         {JsonTagConstants::JSON_RECT_X, windowRect_.x_},
268         {JsonTagConstants::JSON_RECT_Y, windowRect_.y_},
269         {JsonTagConstants::JSON_RECT_WIDTH, windowRect_.width_},
270         {JsonTagConstants::JSON_RECT_HEIGHT, windowRect_.height_}
271     };
272     nlohmann::json jsonPadding = nlohmann::json {
273         { JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top },
274         { JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right },
275         { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom },
276         { JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left },
277     };
278 
279     jsonRes[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
280         { JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_ },
281         { JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_ },
282         { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ },
283         { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
284     };
285 
286     jsonRes[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
287         { JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value },
288         { JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value },
289         { JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value }
290     };
291 
292     jsonRes[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
293         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_ },
294     };
295     jsonRes[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
296         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_ },
297     };
298 
299     jsonRes[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
300         { JsonTagConstants::JSON_TEXT_TAG, text_ },
301         { JsonTagConstants::JSON_ICON_TAG, icon_ },
302         { JsonTagConstants::JSON_BG_TAG, bg_ },
303     };
304 }
305 
ToJsonStr() const306 std::string SecCompBase::ToJsonStr() const
307 {
308     nlohmann::json json;
309     ToJson(json);
310     return json.dump();
311 }
312 
CompareComponentBasicInfo(SecCompBase * other,bool isRectCheck) const313 bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const
314 {
315     if (other == nullptr) {
316         SC_LOG_ERROR(LABEL, "other is nullptr.");
317         return false;
318     }
319 
320     SecCompRect rect = other->rect_;
321     SecCompRect windowRect = other->windowRect_;
322     if (isRectCheck) {
323         rect = rect_;
324         windowRect = windowRect_;
325     }
326 
327     auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconSpace_, padding_.top, padding_.right,
328         padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_,
329         rect, windowRect);
330     auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconSpace_,
331         other->padding_.top, other->padding_.right, other->padding_.bottom, other->padding_.left,
332         other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_,
333         other->rect_, other->windowRect_);
334 
335     return (leftValue == rightValue);
336 }
337 
ParseStyle(const nlohmann::json & json,const std::string & tag)338 bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag)
339 {
340     if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
341         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
342         return false;
343     }
344     auto jsonStyle = json.at(tag);
345     if (!(jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).is_number() &&
346         jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).is_number() &&
347         jsonStyle.at(JsonTagConstants::JSON_BG_TAG).is_number())) {
348         SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
349         return false;
350     }
351     text_ = jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).get<int32_t>();
352     icon_ = jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).get<int32_t>();
353     if (!IsParamValid()) {
354         SC_LOG_ERROR(LABEL, "text or icon is invalid.");
355         return false;
356     }
357 
358     bg_ = static_cast<SecCompBackground>(jsonStyle.at(JsonTagConstants::JSON_BG_TAG).get<int32_t>());
359     if ((bg_ <= SecCompBackground::UNKNOWN_BG) || (bg_ >= SecCompBackground::MAX_BG_TYPE)) {
360         SC_LOG_ERROR(LABEL, "bg is invalid.");
361         return false;
362     }
363 
364     return true;
365 }
366 }  // namespace base
367 }  // namespace Security
368 }  // namespace OHOS
369 
370