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