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 #ifndef SECURITY_COMPONENT_BASE_H 16 #define SECURITY_COMPONENT_BASE_H 17 18 #include "nlohmann/json.hpp" 19 #include "sec_comp_info.h" 20 21 namespace OHOS { 22 namespace Security { 23 namespace SecurityComponent { 24 constexpr int32_t UNKNOWN_TEXT = -2; 25 constexpr int32_t NO_TEXT = -1; 26 constexpr int32_t UNKNOWN_ICON = -2; 27 constexpr int32_t NO_ICON = -1; 28 29 enum class SecCompBackground { 30 UNKNOWN_BG = -2, 31 NO_BG_TYPE = -1, 32 CAPSULE = 0, 33 CIRCLE = 1, 34 NORMAL = 2, 35 MAX_BG_TYPE 36 }; 37 38 class JsonTagConstants final { 39 public: 40 static const std::string JSON_RECT; 41 static const std::string JSON_SC_TYPE; 42 static const std::string JSON_NODE_ID; 43 static const std::string JSON_RECT_X; 44 static const std::string JSON_RECT_Y; 45 static const std::string JSON_RECT_WIDTH; 46 static const std::string JSON_RECT_HEIGHT; 47 static const std::string JSON_WINDOW_RECT; 48 49 static const std::string JSON_SIZE_TAG; 50 static const std::string JSON_FONT_SIZE_TAG; 51 static const std::string JSON_ICON_SIZE_TAG; 52 static const std::string JSON_PADDING_SIZE_TAG; 53 static const std::string JSON_PADDING_LEFT_TAG; 54 static const std::string JSON_PADDING_TOP_TAG; 55 static const std::string JSON_PADDING_RIGHT_TAG; 56 static const std::string JSON_PADDING_BOTTOM_TAG; 57 static const std::string JSON_TEXT_ICON_PADDING_TAG; 58 static const std::string JSON_RECT_WIDTH_TAG; 59 static const std::string JSON_RECT_HEIGHT_TAG; 60 static const std::string JSON_COLORS_TAG; 61 static const std::string JSON_FONT_COLOR_TAG; 62 static const std::string JSON_ICON_COLOR_TAG; 63 static const std::string JSON_BG_COLOR_TAG; 64 65 static const std::string JSON_BORDER_TAG; 66 static const std::string JSON_BORDER_WIDTH_TAG; 67 static const std::string JSON_PARENT_TAG; 68 static const std::string JSON_PARENT_EFFECT_TAG; 69 70 static const std::string JSON_STYLE_TAG; 71 static const std::string JSON_TEXT_TAG; 72 static const std::string JSON_ICON_TAG; 73 static const std::string JSON_BG_TAG; 74 static const std::string JSON_WINDOW_ID; 75 }; 76 77 class SecCompBase { 78 public: 79 SecCompBase() = default; 80 virtual ~SecCompBase() = default; 81 bool FromJson(const nlohmann::json& jsonSrc); 82 void ToJson(nlohmann::json& jsonRes) const; 83 std::string ToJsonStr(void) const; 84 virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const; SetValid(bool valid)85 void SetValid(bool valid) 86 { 87 isValid_ = valid; 88 }; 89 GetValid()90 bool GetValid() 91 { 92 return isValid_; 93 }; 94 95 // size 96 DimensionT fontSize_ = DEFAULT_DIMENSION; 97 DimensionT iconSize_ = DEFAULT_DIMENSION; 98 PaddingSize padding_; 99 DimensionT textIconSpace_ = DEFAULT_DIMENSION; 100 101 // color 102 SecCompColor fontColor_; 103 SecCompColor iconColor_; 104 SecCompColor bgColor_; 105 106 // border 107 DimensionT borderWidth_ = DEFAULT_DIMENSION; 108 109 // parent effect 110 bool parentEffect_ = false; 111 112 SecCompType type_ = UNKNOWN_SC_TYPE; 113 SecCompRect rect_; 114 SecCompRect windowRect_; 115 bool isValid_ = false; 116 117 int32_t text_ = UNKNOWN_TEXT; 118 int32_t icon_ = UNKNOWN_ICON; 119 SecCompBackground bg_ = SecCompBackground::UNKNOWN_BG; 120 121 int32_t windowId_ = 0; 122 int32_t nodeId_ = 0; 123 protected: 124 virtual bool IsTextIconTypeValid() = 0; 125 virtual bool IsCorrespondenceType() = 0; 126 private: 127 bool ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res); 128 bool ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res); 129 bool ParseBool(const nlohmann::json& json, const std::string& tag, bool& res); 130 bool ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res); 131 bool ParseColors(const nlohmann::json& json, const std::string& tag); 132 bool ParseBorders(const nlohmann::json& json, const std::string& tag); 133 bool ParseSize(const nlohmann::json& json, const std::string& tag); 134 bool ParseParent(const nlohmann::json& json, const std::string& tag); 135 bool ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect); 136 bool ParseStyle(const nlohmann::json& json, const std::string& tag); 137 }; 138 } // namespace SecurityComponent 139 } // namespace Security 140 } // namespace OHOS 141 #endif // SECURITY_COMPONENT_BASE_H 142