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_IS_WEARABLE = "isWearable";
31 const std::string JsonTagConstants::JSON_RECT_X = "x";
32 const std::string JsonTagConstants::JSON_RECT_Y = "y";
33 const std::string JsonTagConstants::JSON_RECT_WIDTH = "width";
34 const std::string JsonTagConstants::JSON_RECT_HEIGHT = "height";
35 const std::string JsonTagConstants::JSON_WINDOW_RECT = "windowRect";
36 const std::string JsonTagConstants::JSON_SIZE_TAG = "size";
37 const std::string JsonTagConstants::JSON_FONT_SIZE_TAG = "fontSize";
38 const std::string JsonTagConstants::JSON_ICON_SIZE_TAG = "iconSize";
39 const std::string JsonTagConstants::JSON_PADDING_SIZE_TAG = "paddingSize";
40 const std::string JsonTagConstants::JSON_LEFT_TAG = "left";
41 const std::string JsonTagConstants::JSON_TOP_TAG = "top";
42 const std::string JsonTagConstants::JSON_RIGHT_TAG = "right";
43 const std::string JsonTagConstants::JSON_BOTTOM_TAG = "bottom";
44 const std::string JsonTagConstants::JSON_BORDER_RADIUS_TAG = "borderRadius";
45 const std::string JsonTagConstants::JSON_LEFT_TOP_TAG = "leftTop";
46 const std::string JsonTagConstants::JSON_LEFT_BOTTOM_TAG = "leftBottom";
47 const std::string JsonTagConstants::JSON_RIGHT_TOP_TAG = "rightTop";
48 const std::string JsonTagConstants::JSON_RIGHT_BOTTOM_TAG = "rightBottom";
49 const std::string JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG = "textIconSpace";
50 const std::string JsonTagConstants::JSON_RECT_WIDTH_TAG = "width";
51 const std::string JsonTagConstants::JSON_RECT_HEIGHT_TAG = "height";
52 const std::string JsonTagConstants::JSON_COLORS_TAG = "colors";
53 const std::string JsonTagConstants::JSON_FONT_COLOR_TAG = "fontColor";
54 const std::string JsonTagConstants::JSON_ICON_COLOR_TAG = "iconColor";
55 const std::string JsonTagConstants::JSON_BG_COLOR_TAG = "bgColor";
56 const std::string JsonTagConstants::JSON_BORDER_TAG = "border";
57 const std::string JsonTagConstants::JSON_BORDER_WIDTH_TAG = "borderWidth";
58 const std::string JsonTagConstants::JSON_PARENT_TAG = "parent";
59 const std::string JsonTagConstants::JSON_PARENT_EFFECT_TAG = "parentEffect";
60 const std::string JsonTagConstants::JSON_IS_CLIPPED_TAG = "isClipped";
61 const std::string JsonTagConstants::JSON_TOP_CLIP_TAG = "topClip";
62 const std::string JsonTagConstants::JSON_BOTTOM_CLIP_TAG = "bottomClip";
63 const std::string JsonTagConstants::JSON_LEFT_CLIP_TAG = "leftClip";
64 const std::string JsonTagConstants::JSON_RIGHT_CLIP_TAG = "rightClip";
65 const std::string JsonTagConstants::JSON_PARENT_TAG_TAG = "parentTag";
66 const std::string JsonTagConstants::JSON_STYLE_TAG = "style";
67 const std::string JsonTagConstants::JSON_TEXT_TAG = "text";
68 const std::string JsonTagConstants::JSON_ICON_TAG = "icon";
69 const std::string JsonTagConstants::JSON_BG_TAG = "bg";
70 const std::string JsonTagConstants::JSON_WINDOW_ID = "windowId";
71 const std::string JsonTagConstants::JSON_DISPLAY_ID = "displayId";
72 const std::string JsonTagConstants::JSON_CROSS_AXIS_STATE = "crossAxisState";
73 const std::string JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG = "hasNonCompatileChange";
74 const std::string JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG = "blurRadius";
75
ParseNonCompatibleChange(const nlohmann::json & json)76 bool SecCompBase::ParseNonCompatibleChange(const nlohmann::json& json)
77 {
78 std::string tag = JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG;
79 if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) {
80 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
81 return false;
82 }
83 hasNonCompatileChange_ = json.at(tag).get<bool>();
84
85 if (!ParseDimension(json, JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG, blurRadius_)) {
86 return false;
87 }
88
89 return true;
90 }
91
ParseDimension(const nlohmann::json & json,const std::string & tag,DimensionT & res)92 bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res)
93 {
94 if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) {
95 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
96 return false;
97 }
98
99 res = json.at(tag).get<double>();
100 return true;
101 }
102
ParseColor(const nlohmann::json & json,const std::string & tag,SecCompColor & res)103 bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res)
104 {
105 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
106 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
107 return false;
108 }
109
110 res.value = json.at(tag).get<uint32_t>();
111 return true;
112 }
113
ParseBool(const nlohmann::json & json,const std::string & tag,bool & res)114 bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res)
115 {
116 if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) {
117 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
118 return false;
119 }
120
121 res = json.at(tag).get<bool>();
122 return true;
123 }
124
ParseString(const nlohmann::json & json,const std::string & tag,std::string & res)125 bool SecCompBase::ParseString(const nlohmann::json& json, const std::string& tag, std::string& res)
126 {
127 if ((json.find(tag) == json.end()) || !json.at(tag).is_string()) {
128 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
129 return false;
130 }
131
132 res = json.at(tag).get<std::string>();
133 return true;
134 }
135
ParsePadding(const nlohmann::json & json,const std::string & tag,PaddingSize & res)136 bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res)
137 {
138 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
139 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
140 return false;
141 }
142
143 auto jsonPadding = json.at(tag);
144 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_TOP_TAG, res.top)) {
145 return false;
146 }
147 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_RIGHT_TAG, res.right)) {
148 return false;
149 }
150 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_BOTTOM_TAG, res.bottom)) {
151 return false;
152 }
153 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_LEFT_TAG, res.left)) {
154 return false;
155 }
156 return true;
157 }
158
ParseBorderRadius(const nlohmann::json & json,const std::string & tag,BorderRadius & res)159 bool SecCompBase::ParseBorderRadius(const nlohmann::json& json, const std::string& tag, BorderRadius& res)
160 {
161 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
162 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
163 return false;
164 }
165
166 auto jsonBorderRadius = json.at(tag);
167 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_LEFT_TOP_TAG, res.leftTop)) {
168 return false;
169 }
170 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_RIGHT_TOP_TAG, res.rightTop)) {
171 return false;
172 }
173 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_LEFT_BOTTOM_TAG, res.leftBottom)) {
174 return false;
175 }
176 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_RIGHT_BOTTOM_TAG, res.rightBottom)) {
177 return false;
178 }
179 return true;
180 }
181
ParseColors(const nlohmann::json & json,const std::string & tag)182 bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag)
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 auto jsonColors = json.at(tag);
189 if (!ParseColor(jsonColors, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_)) {
190 return false;
191 }
192 if (!ParseColor(jsonColors, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_)) {
193 return false;
194 }
195 if (!ParseColor(jsonColors, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_)) {
196 return false;
197 }
198 return true;
199 }
200
ParseBorders(const nlohmann::json & json,const std::string & tag)201 bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag)
202 {
203 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
204 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
205 return false;
206 }
207 auto jsonBorder = json.at(tag);
208 return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_);
209 }
210
ParseSize(const nlohmann::json & json,const std::string & tag)211 bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag)
212 {
213 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
214 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
215 return false;
216 }
217
218 auto jsonSize = json.at(tag);
219 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) {
220 return false;
221 }
222
223 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_)) {
224 return false;
225 }
226
227 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) {
228 return false;
229 }
230
231 if (!ParsePadding(jsonSize, JsonTagConstants::JSON_PADDING_SIZE_TAG, padding_)) {
232 return false;
233 }
234
235 if (!ParseBorderRadius(jsonSize, JsonTagConstants::JSON_BORDER_RADIUS_TAG, borderRadius_)) {
236 return false;
237 }
238 rect_.borderRadius_ = borderRadius_;
239
240 return true;
241 }
242
ParseParent(const nlohmann::json & json,const std::string & tag)243 bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag)
244 {
245 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
246 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
247 return false;
248 }
249 auto jsonParent = json.at(tag);
250 if (!ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_)) {
251 return false;
252 }
253 if (!ParseBool(jsonParent, JsonTagConstants::JSON_IS_CLIPPED_TAG, isClipped_)) {
254 return false;
255 }
256 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_TOP_CLIP_TAG, topClip_)) {
257 return false;
258 }
259 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_BOTTOM_CLIP_TAG, bottomClip_)) {
260 return false;
261 }
262 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_LEFT_CLIP_TAG, leftClip_)) {
263 return false;
264 }
265 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_RIGHT_CLIP_TAG, rightClip_)) {
266 return false;
267 }
268 if (!ParseString(jsonParent, JsonTagConstants::JSON_PARENT_TAG_TAG, parentTag_)) {
269 return false;
270 }
271 return true;
272 }
273
ParseRect(const nlohmann::json & json,const std::string & tag,SecCompRect & rect)274 bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect)
275 {
276 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
277 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
278 return false;
279 }
280
281 auto jsonSize = json.at(tag);
282 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_X, rect.x_)) {
283 return false;
284 }
285
286 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_Y, rect.y_)) {
287 return false;
288 }
289
290 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_WIDTH, rect.width_)) {
291 return false;
292 }
293
294 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_HEIGHT, rect.height_)) {
295 return false;
296 }
297
298 return true;
299 }
300
ParseType(const nlohmann::json & json,const std::string & tag)301 bool SecCompBase::ParseType(const nlohmann::json& json, const std::string& tag)
302 {
303 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
304 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
305 return false;
306 }
307 int32_t value = json.at(tag).get<int32_t>();
308 if ((value <= static_cast<int32_t>(SecCompType::UNKNOWN_SC_TYPE)) ||
309 (value >= static_cast<int32_t>(SecCompType::MAX_SC_TYPE))) {
310 SC_LOG_ERROR(LABEL, "scType value is invalid.");
311 return false;
312 }
313 type_ = static_cast<SecCompType>(value);
314
315 return true;
316 }
317
ParseValue(const nlohmann::json & json,const std::string & tag,int32_t & value)318 bool SecCompBase::ParseValue(const nlohmann::json& json, const std::string& tag, int32_t& value)
319 {
320 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
321 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
322 return false;
323 }
324 value = json.at(tag).get<int32_t>();
325
326 return true;
327 }
328
ParseDisplayId(const nlohmann::json & json,const std::string & tag)329 bool SecCompBase::ParseDisplayId(const nlohmann::json& json, const std::string& tag)
330 {
331 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
332 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
333 return false;
334 }
335 displayId_ = json.at(tag).get<uint64_t>();
336
337 return true;
338 }
339
ParseCrossAxisState(const nlohmann::json & json,const std::string & tag)340 bool SecCompBase::ParseCrossAxisState(const nlohmann::json& json, const std::string& tag)
341 {
342 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
343 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
344 return false;
345 }
346 int32_t value = json.at(tag).get<int32_t>();
347 if ((value < static_cast<int32_t>(CrossAxisState::STATE_INVALID)) ||
348 (value > static_cast<int32_t>(CrossAxisState::STATE_NO_CROSS))) {
349 SC_LOG_ERROR(LABEL, "Cross axis state: %{public}d is invalid.", value);
350 return false;
351 }
352 crossAxisState_ = static_cast<CrossAxisState>(value);
353
354 return true;
355 }
356
ParseWearable(const nlohmann::json & json,const std::string & tag)357 bool SecCompBase::ParseWearable(const nlohmann::json& json, const std::string& tag)
358 {
359 if ((json.find(tag) == json.end()) ||
360 !json.at(tag).is_boolean()) {
361 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
362 return false;
363 }
364 isWearableDevice_ = json.at(tag).get<bool>();
365 return true;
366 }
367
FromJson(const nlohmann::json & jsonSrc,std::string & message,bool isClicked)368 bool SecCompBase::FromJson(const nlohmann::json& jsonSrc, std::string& message, bool isClicked)
369 {
370 SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str());
371 if (!ParseType(jsonSrc, JsonTagConstants::JSON_SC_TYPE)) {
372 return false;
373 }
374 if (!ParseValue(jsonSrc, JsonTagConstants::JSON_NODE_ID, nodeId_)) {
375 return false;
376 }
377 if (!ParseWearable(jsonSrc, JsonTagConstants::JSON_IS_WEARABLE)) {
378 return false;
379 }
380 if (!ParseRect(jsonSrc, JsonTagConstants::JSON_RECT, rect_)) {
381 return false;
382 }
383 if (!ParseRect(jsonSrc, JsonTagConstants::JSON_WINDOW_RECT, windowRect_)) {
384 return false;
385 }
386 if (!ParseSize(jsonSrc, JsonTagConstants::JSON_SIZE_TAG)) {
387 return false;
388 }
389 if (!ParseColors(jsonSrc, JsonTagConstants::JSON_COLORS_TAG)) {
390 return false;
391 }
392 if (!ParseBorders(jsonSrc, JsonTagConstants::JSON_BORDER_TAG)) {
393 return false;
394 }
395 if (!ParseParent(jsonSrc, JsonTagConstants::JSON_PARENT_TAG)) {
396 return false;
397 }
398 if (!ParseStyle(jsonSrc, JsonTagConstants::JSON_STYLE_TAG, message, isClicked)) {
399 return false;
400 }
401 if (!ParseValue(jsonSrc, JsonTagConstants::JSON_WINDOW_ID, windowId_)) {
402 return false;
403 }
404 if (!ParseDisplayId(jsonSrc, JsonTagConstants::JSON_DISPLAY_ID)) {
405 return false;
406 }
407 if (!ParseCrossAxisState(jsonSrc, JsonTagConstants::JSON_CROSS_AXIS_STATE)) {
408 return false;
409 }
410 if (!ParseNonCompatibleChange(jsonSrc)) {
411 return false;
412 }
413
414 return true;
415 }
416
ToJson(nlohmann::json & jsonRes) const417 void SecCompBase::ToJson(nlohmann::json& jsonRes) const
418 {
419 jsonRes[JsonTagConstants::JSON_SC_TYPE] = type_;
420 jsonRes[JsonTagConstants::JSON_NODE_ID] = nodeId_;
421 jsonRes[JsonTagConstants::JSON_IS_WEARABLE] = isWearableDevice_;
422 jsonRes[JsonTagConstants::JSON_RECT] = nlohmann::json {
423 {JsonTagConstants::JSON_RECT_X, rect_.x_},
424 {JsonTagConstants::JSON_RECT_Y, rect_.y_},
425 {JsonTagConstants::JSON_RECT_WIDTH, rect_.width_},
426 {JsonTagConstants::JSON_RECT_HEIGHT, rect_.height_}
427 };
428 jsonRes[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
429 {JsonTagConstants::JSON_RECT_X, windowRect_.x_},
430 {JsonTagConstants::JSON_RECT_Y, windowRect_.y_},
431 {JsonTagConstants::JSON_RECT_WIDTH, windowRect_.width_},
432 {JsonTagConstants::JSON_RECT_HEIGHT, windowRect_.height_}
433 };
434 nlohmann::json jsonPadding = nlohmann::json {
435 { JsonTagConstants::JSON_TOP_TAG, padding_.top },
436 { JsonTagConstants::JSON_RIGHT_TAG, padding_.right },
437 { JsonTagConstants::JSON_BOTTOM_TAG, padding_.bottom },
438 { JsonTagConstants::JSON_LEFT_TAG, padding_.left },
439 };
440
441 nlohmann::json jsonBorderRadius = nlohmann::json {
442 { JsonTagConstants::JSON_LEFT_TOP_TAG, borderRadius_.leftTop },
443 { JsonTagConstants::JSON_RIGHT_TOP_TAG, borderRadius_.rightTop },
444 { JsonTagConstants::JSON_LEFT_BOTTOM_TAG, borderRadius_.leftBottom },
445 { JsonTagConstants::JSON_RIGHT_BOTTOM_TAG, borderRadius_.rightBottom },
446 };
447
448 jsonRes[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
449 { JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_ },
450 { JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_ },
451 { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ },
452 { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
453 { JsonTagConstants::JSON_BORDER_RADIUS_TAG, jsonBorderRadius },
454 };
455
456 jsonRes[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
457 { JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value },
458 { JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value },
459 { JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value }
460 };
461
462 jsonRes[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
463 { JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_ },
464 };
465 jsonRes[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
466 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_ },
467 { JsonTagConstants::JSON_IS_CLIPPED_TAG, isClipped_ },
468 { JsonTagConstants::JSON_TOP_CLIP_TAG, topClip_ },
469 { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, bottomClip_ },
470 { JsonTagConstants::JSON_LEFT_CLIP_TAG, leftClip_ },
471 { JsonTagConstants::JSON_RIGHT_CLIP_TAG, rightClip_ },
472 { JsonTagConstants::JSON_PARENT_TAG_TAG, parentTag_ },
473 };
474
475 jsonRes[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
476 { JsonTagConstants::JSON_TEXT_TAG, text_ },
477 { JsonTagConstants::JSON_ICON_TAG, icon_ },
478 { JsonTagConstants::JSON_BG_TAG, bg_ },
479 };
480 jsonRes[JsonTagConstants::JSON_WINDOW_ID] = windowId_;
481 jsonRes[JsonTagConstants::JSON_DISPLAY_ID] = displayId_;
482 jsonRes[JsonTagConstants::JSON_CROSS_AXIS_STATE] = crossAxisState_;
483 jsonRes[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = hasNonCompatileChange_;
484 jsonRes[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = blurRadius_;
485 }
486
ToJsonStr() const487 std::string SecCompBase::ToJsonStr() const
488 {
489 nlohmann::json json;
490 ToJson(json);
491 return json.dump();
492 }
493
CompareComponentBasicInfo(SecCompBase * other,bool isRectCheck) const494 bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const
495 {
496 if (other == nullptr) {
497 SC_LOG_ERROR(LABEL, "other is nullptr.");
498 return false;
499 }
500
501 SecCompRect rect = other->rect_;
502 SecCompRect windowRect = other->windowRect_;
503 if (isRectCheck) {
504 rect = rect_;
505 windowRect = windowRect_;
506 }
507
508 auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconSpace_, padding_.top, padding_.right,
509 padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_,
510 rect, windowRect);
511 auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconSpace_,
512 other->padding_.top, other->padding_.right, other->padding_.bottom, other->padding_.left,
513 other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_,
514 other->rect_, other->windowRect_);
515
516 return (leftValue == rightValue);
517 }
518
ParseStyle(const nlohmann::json & json,const std::string & tag,std::string & message,bool isClicked)519 bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag, std::string& message, bool isClicked)
520 {
521 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
522 SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str());
523 return false;
524 }
525 auto jsonStyle = json.at(tag);
526 if (jsonStyle.find(JsonTagConstants::JSON_TEXT_TAG) == jsonStyle.end() ||
527 !jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).is_number()) {
528 SC_LOG_ERROR(LABEL, "Json=%{public}s tag is invalid.", tag.c_str());
529 return false;
530 }
531 if (jsonStyle.find(JsonTagConstants::JSON_ICON_TAG) == jsonStyle.end() ||
532 !jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).is_number()) {
533 SC_LOG_ERROR(LABEL, "Json=%{public}s tag is invalid.", tag.c_str());
534 return false;
535 }
536 if (jsonStyle.find(JsonTagConstants::JSON_BG_TAG) == jsonStyle.end() ||
537 !jsonStyle.at(JsonTagConstants::JSON_BG_TAG).is_number()) {
538 SC_LOG_ERROR(LABEL, "Json=%{public}s tag is invalid.", tag.c_str());
539 return false;
540 }
541 text_ = jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).get<int32_t>();
542 icon_ = jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).get<int32_t>();
543 if (!IsTextIconTypeValid(message, isClicked)) {
544 SC_LOG_ERROR(LABEL, "text or icon is invalid.");
545 return false;
546 }
547 bg_ = static_cast<SecCompBackground>(jsonStyle.at(JsonTagConstants::JSON_BG_TAG).get<int32_t>());
548 if ((bg_ != SecCompBackground::NO_BG_TYPE) && (bg_ != SecCompBackground::CAPSULE) &&
549 (bg_ != SecCompBackground::CIRCLE) && (bg_ != SecCompBackground::NORMAL) &&
550 (bg_ != SecCompBackground::ROUNDED_RECTANGLE)) {
551 SC_LOG_ERROR(LABEL, "bg is invalid.");
552 return false;
553 }
554 return true;
555 }
556 } // namespace base
557 } // namespace Security
558 } // namespace OHOS
559