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_IS_ICON_EXCEEDED_TAG = "isIconExceeded";
74 const std::string JsonTagConstants::JSON_IS_BORDER_COVERED_TAG = "isBorderCovered";
75 const std::string JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG = "hasNonCompatibleChange";
76 const std::string JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG = "blurRadius";
77 const std::string JsonTagConstants::JSON_FOREGROUND_BLUR_RADIUS_TAG = "foregroundBlurRadius";
78 const std::string JsonTagConstants::JSON_IS_OVERLAY_TEXT_SET_TAG = "isOverlayTextSet";
79 const std::string JsonTagConstants::JSON_IS_OVERLAY_NODE_SET_TAG = "isOverlayNodeCovered";
80 const std::string JsonTagConstants::JSON_TIP_POSITION = "tipPosition";
81 const std::string JsonTagConstants::JSON_IS_CUSTOMIZABLE = "isCustomizable";
82
ParseNonCompatibleChange(const nlohmann::json & json)83 bool SecCompBase::ParseNonCompatibleChange(const nlohmann::json& json)
84 {
85 std::string tag = JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG;
86 if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) {
87 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
88 return false;
89 }
90 hasNonCompatibleChange_ = json.at(tag).get<bool>();
91
92 if (!ParseBool(json, JsonTagConstants::JSON_IS_ICON_EXCEEDED_TAG, isIconExceeded_)) {
93 return false;
94 }
95 if (!ParseBool(json, JsonTagConstants::JSON_IS_BORDER_COVERED_TAG, isBorderCovered_)) {
96 return false;
97 }
98 if (!ParseDimension(json, JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG, blurRadius_)) {
99 return false;
100 }
101
102 if (!ParseDimension(json, JsonTagConstants::JSON_FOREGROUND_BLUR_RADIUS_TAG, foregroundBlurRadius_)) {
103 return false;
104 }
105
106 if (!ParseBool(json, JsonTagConstants::JSON_IS_OVERLAY_TEXT_SET_TAG, isOverlayTextSet_)) {
107 return false;
108 }
109
110 if (!ParseBool(json, JsonTagConstants::JSON_IS_OVERLAY_NODE_SET_TAG, isOverlayNodeCovered_)) {
111 return false;
112 }
113
114 return true;
115 }
116
ParseDimension(const nlohmann::json & json,const std::string & tag,DimensionT & res)117 bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res)
118 {
119 if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) {
120 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
121 return false;
122 }
123
124 res = json.at(tag).get<double>();
125 return true;
126 }
127
ParseColor(const nlohmann::json & json,const std::string & tag,SecCompColor & res)128 bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res)
129 {
130 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
131 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
132 return false;
133 }
134
135 res.value = json.at(tag).get<uint32_t>();
136 return true;
137 }
138
ParseBool(const nlohmann::json & json,const std::string & tag,bool & res)139 bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res)
140 {
141 if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) {
142 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
143 return false;
144 }
145
146 res = json.at(tag).get<bool>();
147 return true;
148 }
149
ParseString(const nlohmann::json & json,const std::string & tag,std::string & res)150 bool SecCompBase::ParseString(const nlohmann::json& json, const std::string& tag, std::string& res)
151 {
152 if ((json.find(tag) == json.end()) || !json.at(tag).is_string()) {
153 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
154 return false;
155 }
156
157 res = json.at(tag).get<std::string>();
158 return true;
159 }
160
ParsePadding(const nlohmann::json & json,const std::string & tag,PaddingSize & res)161 bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res)
162 {
163 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
164 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
165 return false;
166 }
167
168 auto jsonPadding = json.at(tag);
169 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_TOP_TAG, res.top)) {
170 return false;
171 }
172 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_RIGHT_TAG, res.right)) {
173 return false;
174 }
175 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_BOTTOM_TAG, res.bottom)) {
176 return false;
177 }
178 if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_LEFT_TAG, res.left)) {
179 return false;
180 }
181 return true;
182 }
183
ParseBorderRadius(const nlohmann::json & json,const std::string & tag,BorderRadius & res)184 bool SecCompBase::ParseBorderRadius(const nlohmann::json& json, const std::string& tag, BorderRadius& res)
185 {
186 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
187 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
188 return false;
189 }
190
191 auto jsonBorderRadius = json.at(tag);
192 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_LEFT_TOP_TAG, res.leftTop)) {
193 return false;
194 }
195 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_RIGHT_TOP_TAG, res.rightTop)) {
196 return false;
197 }
198 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_LEFT_BOTTOM_TAG, res.leftBottom)) {
199 return false;
200 }
201 if (!ParseDimension(jsonBorderRadius, JsonTagConstants::JSON_RIGHT_BOTTOM_TAG, res.rightBottom)) {
202 return false;
203 }
204 return true;
205 }
206
ParseColors(const nlohmann::json & json,const std::string & tag)207 bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag)
208 {
209 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
210 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
211 return false;
212 }
213 auto jsonColors = json.at(tag);
214 if (!ParseColor(jsonColors, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_)) {
215 return false;
216 }
217 if (!ParseColor(jsonColors, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_)) {
218 return false;
219 }
220 if (!ParseColor(jsonColors, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_)) {
221 return false;
222 }
223 return true;
224 }
225
ParseBorders(const nlohmann::json & json,const std::string & tag)226 bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag)
227 {
228 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
229 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
230 return false;
231 }
232 auto jsonBorder = json.at(tag);
233 return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_);
234 }
235
ParseSize(const nlohmann::json & json,const std::string & tag)236 bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag)
237 {
238 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
239 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
240 return false;
241 }
242
243 auto jsonSize = json.at(tag);
244 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) {
245 return false;
246 }
247
248 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_)) {
249 return false;
250 }
251
252 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) {
253 return false;
254 }
255
256 if (!ParsePadding(jsonSize, JsonTagConstants::JSON_PADDING_SIZE_TAG, padding_)) {
257 return false;
258 }
259
260 if (!ParseBorderRadius(jsonSize, JsonTagConstants::JSON_BORDER_RADIUS_TAG, borderRadius_)) {
261 return false;
262 }
263 rect_.borderRadius_ = borderRadius_;
264
265 return true;
266 }
267
ParseParent(const nlohmann::json & json,const std::string & tag)268 bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag)
269 {
270 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
271 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
272 return false;
273 }
274 auto jsonParent = json.at(tag);
275 if (!ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_)) {
276 return false;
277 }
278 if (!ParseBool(jsonParent, JsonTagConstants::JSON_IS_CLIPPED_TAG, isClipped_)) {
279 return false;
280 }
281 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_TOP_CLIP_TAG, topClip_)) {
282 return false;
283 }
284 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_BOTTOM_CLIP_TAG, bottomClip_)) {
285 return false;
286 }
287 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_LEFT_CLIP_TAG, leftClip_)) {
288 return false;
289 }
290 if (!ParseDimension(jsonParent, JsonTagConstants::JSON_RIGHT_CLIP_TAG, rightClip_)) {
291 return false;
292 }
293 if (!ParseString(jsonParent, JsonTagConstants::JSON_PARENT_TAG_TAG, parentTag_)) {
294 return false;
295 }
296 return true;
297 }
298
ParseRect(const nlohmann::json & json,const std::string & tag,SecCompRect & rect)299 bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect)
300 {
301 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
302 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
303 return false;
304 }
305
306 auto jsonSize = json.at(tag);
307 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_X, rect.x_)) {
308 return false;
309 }
310
311 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_Y, rect.y_)) {
312 return false;
313 }
314
315 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_WIDTH, rect.width_)) {
316 return false;
317 }
318
319 if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_HEIGHT, rect.height_)) {
320 return false;
321 }
322
323 return true;
324 }
325
ParseType(const nlohmann::json & json,const std::string & tag)326 bool SecCompBase::ParseType(const nlohmann::json& json, const std::string& tag)
327 {
328 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
329 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
330 return false;
331 }
332 int32_t value = json.at(tag).get<int32_t>();
333 if ((value <= static_cast<int32_t>(SecCompType::UNKNOWN_SC_TYPE)) ||
334 (value >= static_cast<int32_t>(SecCompType::MAX_SC_TYPE))) {
335 SC_LOG_ERROR(LABEL, "scType value is invalid.");
336 return false;
337 }
338 type_ = static_cast<SecCompType>(value);
339
340 return true;
341 }
342
ParseValue(const nlohmann::json & json,const std::string & tag,int32_t & value)343 bool SecCompBase::ParseValue(const nlohmann::json& json, const std::string& tag, int32_t& value)
344 {
345 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
346 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
347 return false;
348 }
349 value = json.at(tag).get<int32_t>();
350
351 return true;
352 }
353
ParseDisplayId(const nlohmann::json & json,const std::string & tag)354 bool SecCompBase::ParseDisplayId(const nlohmann::json& json, const std::string& tag)
355 {
356 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
357 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
358 return false;
359 }
360 displayId_ = json.at(tag).get<uint64_t>();
361
362 return true;
363 }
364
ParseCrossAxisState(const nlohmann::json & json,const std::string & tag)365 bool SecCompBase::ParseCrossAxisState(const nlohmann::json& json, const std::string& tag)
366 {
367 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
368 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
369 return false;
370 }
371 int32_t value = json.at(tag).get<int32_t>();
372 if ((value < static_cast<int32_t>(CrossAxisState::STATE_INVALID)) ||
373 (value > static_cast<int32_t>(CrossAxisState::STATE_NO_CROSS))) {
374 SC_LOG_ERROR(LABEL, "Cross axis state: %{public}d is invalid.", value);
375 return false;
376 }
377 crossAxisState_ = static_cast<CrossAxisState>(value);
378
379 return true;
380 }
381
ParseWearable(const nlohmann::json & json,const std::string & tag)382 bool SecCompBase::ParseWearable(const nlohmann::json& json, const std::string& tag)
383 {
384 if ((json.find(tag) == json.end()) ||
385 !json.at(tag).is_boolean()) {
386 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
387 return false;
388 }
389 isWearableDevice_ = json.at(tag).get<bool>();
390 return true;
391 }
392
ParseTipPosition(const nlohmann::json & json,const std::string & tag)393 bool SecCompBase::ParseTipPosition(const nlohmann::json& json, const std::string& tag)
394 {
395 if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) {
396 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
397 return false;
398 }
399 int32_t value = json.at(tag).get<int32_t>();
400 if ((value < static_cast<int32_t>(TipPosition::ABOVE_BOTTOM)) ||
401 (value > static_cast<int32_t>(TipPosition::BELOW_TOP))) {
402 SC_LOG_ERROR(LABEL, "Save tip position: %{public}d is invalid.", value);
403 return false;
404 }
405
406 tipPosition_ = static_cast<TipPosition>(value);
407 return true;
408 }
409
ParseComponentInfo(const nlohmann::json & json,std::string & message,bool isClicked)410 bool SecCompBase::ParseComponentInfo(const nlohmann::json& json, std::string& message, bool isClicked)
411 {
412 if (!ParseRect(json, JsonTagConstants::JSON_RECT, rect_)) {
413 return false;
414 }
415 if (!ParseSize(json, JsonTagConstants::JSON_SIZE_TAG)) {
416 return false;
417 }
418 if (!ParseColors(json, JsonTagConstants::JSON_COLORS_TAG)) {
419 return false;
420 }
421 if (!ParseBorders(json, JsonTagConstants::JSON_BORDER_TAG)) {
422 return false;
423 }
424 if (!ParseStyle(json, JsonTagConstants::JSON_STYLE_TAG, message, isClicked)) {
425 return false;
426 }
427 return true;
428 }
429
ParseWindowInfo(const nlohmann::json & json)430 bool SecCompBase::ParseWindowInfo(const nlohmann::json& json)
431 {
432 if (!ParseRect(json, JsonTagConstants::JSON_WINDOW_RECT, windowRect_)) {
433 return false;
434 }
435 if (!ParseValue(json, JsonTagConstants::JSON_WINDOW_ID, windowId_)) {
436 return false;
437 }
438 return true;
439 }
440
ParseDisplayInfo(const nlohmann::json & json)441 bool SecCompBase::ParseDisplayInfo(const nlohmann::json& json)
442 {
443 if (!ParseDisplayId(json, JsonTagConstants::JSON_DISPLAY_ID)) {
444 return false;
445 }
446 if (!ParseCrossAxisState(json, JsonTagConstants::JSON_CROSS_AXIS_STATE)) {
447 return false;
448 }
449 return true;
450 }
451
ParseCustomInfo(const nlohmann::json & json)452 bool SecCompBase::ParseCustomInfo(const nlohmann::json& json)
453 {
454 if (!ParseBool(json, JsonTagConstants::JSON_IS_CUSTOMIZABLE, isCustomizable_)) {
455 return false;
456 }
457 if (!ParseTipPosition(json, JsonTagConstants::JSON_TIP_POSITION)) {
458 return false;
459 }
460 return true;
461 }
462
FromJson(const nlohmann::json & jsonSrc,std::string & message,bool isClicked)463 bool SecCompBase::FromJson(const nlohmann::json& jsonSrc, std::string& message, bool isClicked)
464 {
465 SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str());
466 if (!ParseType(jsonSrc, JsonTagConstants::JSON_SC_TYPE)) {
467 return false;
468 }
469 if (!ParseValue(jsonSrc, JsonTagConstants::JSON_NODE_ID, nodeId_)) {
470 return false;
471 }
472 if (!ParseWearable(jsonSrc, JsonTagConstants::JSON_IS_WEARABLE)) {
473 return false;
474 }
475 if (!ParseParent(jsonSrc, JsonTagConstants::JSON_PARENT_TAG)) {
476 return false;
477 }
478 if (!ParseComponentInfo(jsonSrc, message, isClicked)) {
479 return false;
480 }
481 if (!ParseWindowInfo(jsonSrc)) {
482 return false;
483 }
484 if (!ParseDisplayInfo(jsonSrc)) {
485 return false;
486 }
487 if (!ParseCustomInfo(jsonSrc)) {
488 return false;
489 }
490 if (!ParseNonCompatibleChange(jsonSrc)) {
491 return false;
492 }
493
494 return true;
495 }
496
ToJsonRect(nlohmann::json & jsonRes) const497 void SecCompBase::ToJsonRect(nlohmann::json& jsonRes) const
498 {
499 jsonRes[JsonTagConstants::JSON_RECT] = nlohmann::json {
500 {JsonTagConstants::JSON_RECT_X, rect_.x_},
501 {JsonTagConstants::JSON_RECT_Y, rect_.y_},
502 {JsonTagConstants::JSON_RECT_WIDTH, rect_.width_},
503 {JsonTagConstants::JSON_RECT_HEIGHT, rect_.height_}
504 };
505 jsonRes[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
506 {JsonTagConstants::JSON_RECT_X, windowRect_.x_},
507 {JsonTagConstants::JSON_RECT_Y, windowRect_.y_},
508 {JsonTagConstants::JSON_RECT_WIDTH, windowRect_.width_},
509 {JsonTagConstants::JSON_RECT_HEIGHT, windowRect_.height_}
510 };
511 }
512
ToJsonSize(nlohmann::json & jsonRes) const513 void SecCompBase::ToJsonSize(nlohmann::json& jsonRes) const
514 {
515 nlohmann::json jsonPadding = nlohmann::json {
516 { JsonTagConstants::JSON_TOP_TAG, padding_.top },
517 { JsonTagConstants::JSON_RIGHT_TAG, padding_.right },
518 { JsonTagConstants::JSON_BOTTOM_TAG, padding_.bottom },
519 { JsonTagConstants::JSON_LEFT_TAG, padding_.left },
520 };
521
522 nlohmann::json jsonBorderRadius = nlohmann::json {
523 { JsonTagConstants::JSON_LEFT_TOP_TAG, borderRadius_.leftTop },
524 { JsonTagConstants::JSON_RIGHT_TOP_TAG, borderRadius_.rightTop },
525 { JsonTagConstants::JSON_LEFT_BOTTOM_TAG, borderRadius_.leftBottom },
526 { JsonTagConstants::JSON_RIGHT_BOTTOM_TAG, borderRadius_.rightBottom },
527 };
528
529 jsonRes[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
530 { JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_ },
531 { JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_ },
532 { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ },
533 { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
534 { JsonTagConstants::JSON_BORDER_RADIUS_TAG, jsonBorderRadius },
535 };
536 }
537
ToJson(nlohmann::json & jsonRes) const538 void SecCompBase::ToJson(nlohmann::json& jsonRes) const
539 {
540 jsonRes[JsonTagConstants::JSON_SC_TYPE] = type_;
541 jsonRes[JsonTagConstants::JSON_NODE_ID] = nodeId_;
542 jsonRes[JsonTagConstants::JSON_IS_WEARABLE] = isWearableDevice_;
543
544 ToJsonRect(jsonRes);
545 ToJsonSize(jsonRes);
546
547 jsonRes[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
548 { JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value },
549 { JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value },
550 { JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value }
551 };
552
553 jsonRes[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
554 { JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_ },
555 };
556 jsonRes[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
557 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_ },
558 { JsonTagConstants::JSON_IS_CLIPPED_TAG, isClipped_ },
559 { JsonTagConstants::JSON_TOP_CLIP_TAG, topClip_ },
560 { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, bottomClip_ },
561 { JsonTagConstants::JSON_LEFT_CLIP_TAG, leftClip_ },
562 { JsonTagConstants::JSON_RIGHT_CLIP_TAG, rightClip_ },
563 { JsonTagConstants::JSON_PARENT_TAG_TAG, parentTag_ },
564 };
565
566 jsonRes[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
567 { JsonTagConstants::JSON_TEXT_TAG, text_ },
568 { JsonTagConstants::JSON_ICON_TAG, icon_ },
569 { JsonTagConstants::JSON_BG_TAG, bg_ },
570 };
571 jsonRes[JsonTagConstants::JSON_WINDOW_ID] = windowId_;
572 jsonRes[JsonTagConstants::JSON_DISPLAY_ID] = displayId_;
573 jsonRes[JsonTagConstants::JSON_CROSS_AXIS_STATE] = crossAxisState_;
574 jsonRes[JsonTagConstants::JSON_IS_ICON_EXCEEDED_TAG] = isIconExceeded_;
575 jsonRes[JsonTagConstants::JSON_IS_BORDER_COVERED_TAG] = isBorderCovered_;
576 jsonRes[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = hasNonCompatibleChange_;
577 jsonRes[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = blurRadius_;
578 jsonRes[JsonTagConstants::JSON_FOREGROUND_BLUR_RADIUS_TAG] = foregroundBlurRadius_;
579 jsonRes[JsonTagConstants::JSON_IS_OVERLAY_TEXT_SET_TAG] = isOverlayTextSet_;
580 jsonRes[JsonTagConstants::JSON_IS_OVERLAY_NODE_SET_TAG] = isOverlayNodeCovered_;
581 jsonRes[JsonTagConstants::JSON_IS_CUSTOMIZABLE] = isCustomizable_;
582 jsonRes[JsonTagConstants::JSON_TIP_POSITION] = tipPosition_;
583 }
584
ToJsonStr() const585 std::string SecCompBase::ToJsonStr() const
586 {
587 nlohmann::json json;
588 ToJson(json);
589 return json.dump();
590 }
591
CompareComponentBasicInfo(SecCompBase * other,bool isRectCheck) const592 bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const
593 {
594 if (other == nullptr) {
595 SC_LOG_ERROR(LABEL, "Other is nullptr.");
596 return false;
597 }
598
599 SecCompRect rect = other->rect_;
600 SecCompRect windowRect = other->windowRect_;
601 if (isRectCheck) {
602 rect = rect_;
603 windowRect = windowRect_;
604 }
605
606 auto leftValue = std::tie(type_, fontSize_, iconSize_, textIconSpace_, padding_.top, padding_.right,
607 padding_.bottom, padding_.left, fontColor_.value, bgColor_.value, iconColor_.value, borderWidth_,
608 rect, windowRect);
609 auto rightValue = std::tie(other->type_, other->fontSize_, other->iconSize_, other->textIconSpace_,
610 other->padding_.top, other->padding_.right, other->padding_.bottom, other->padding_.left,
611 other->fontColor_.value, other->bgColor_.value, other->iconColor_.value, other->borderWidth_,
612 other->rect_, other->windowRect_);
613
614 return (leftValue == rightValue);
615 }
616
ParseStyle(const nlohmann::json & json,const std::string & tag,std::string & message,bool isClicked)617 bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag, std::string& message, bool isClicked)
618 {
619 if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) {
620 SC_LOG_ERROR(LABEL, "Json: %{public}s tag invalid.", tag.c_str());
621 return false;
622 }
623 auto jsonStyle = json.at(tag);
624 if (jsonStyle.find(JsonTagConstants::JSON_TEXT_TAG) == jsonStyle.end() ||
625 !jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).is_number()) {
626 SC_LOG_ERROR(LABEL, "Json=%{public}s tag is invalid.", tag.c_str());
627 return false;
628 }
629 if (jsonStyle.find(JsonTagConstants::JSON_ICON_TAG) == jsonStyle.end() ||
630 !jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).is_number()) {
631 SC_LOG_ERROR(LABEL, "Json=%{public}s tag is invalid.", tag.c_str());
632 return false;
633 }
634 if (jsonStyle.find(JsonTagConstants::JSON_BG_TAG) == jsonStyle.end() ||
635 !jsonStyle.at(JsonTagConstants::JSON_BG_TAG).is_number()) {
636 SC_LOG_ERROR(LABEL, "Json=%{public}s tag is invalid.", tag.c_str());
637 return false;
638 }
639 text_ = jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).get<int32_t>();
640 icon_ = jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).get<int32_t>();
641 if (!IsTextIconTypeValid(message, isClicked)) {
642 SC_LOG_ERROR(LABEL, "Text or icon is invalid.");
643 return false;
644 }
645 bg_ = static_cast<SecCompBackground>(jsonStyle.at(JsonTagConstants::JSON_BG_TAG).get<int32_t>());
646 if ((bg_ != SecCompBackground::NO_BG_TYPE) && (bg_ != SecCompBackground::CAPSULE) &&
647 (bg_ != SecCompBackground::CIRCLE) && (bg_ != SecCompBackground::NORMAL) &&
648 (bg_ != SecCompBackground::ROUNDED_RECTANGLE)) {
649 SC_LOG_ERROR(LABEL, "Background is invalid.");
650 return false;
651 }
652 return true;
653 }
654 } // namespace base
655 } // namespace Security
656 } // namespace OHOS
657