1 /* 2 * Copyright (c) 2023-2024 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 ROUNDED_RECTANGLE = 8, 36 MAX_BG_TYPE 37 }; 38 39 class __attribute__((visibility("default"))) JsonTagConstants final { 40 public: 41 static const std::string JSON_RECT; 42 static const std::string JSON_SC_TYPE; 43 static const std::string JSON_NODE_ID; 44 static const std::string JSON_IS_WEARABLE; 45 static const std::string JSON_RECT_X; 46 static const std::string JSON_RECT_Y; 47 static const std::string JSON_RECT_WIDTH; 48 static const std::string JSON_RECT_HEIGHT; 49 static const std::string JSON_WINDOW_RECT; 50 51 static const std::string JSON_SIZE_TAG; 52 static const std::string JSON_FONT_SIZE_TAG; 53 static const std::string JSON_ICON_SIZE_TAG; 54 static const std::string JSON_PADDING_SIZE_TAG; 55 static const std::string JSON_LEFT_TAG; 56 static const std::string JSON_TOP_TAG; 57 static const std::string JSON_RIGHT_TAG; 58 static const std::string JSON_BOTTOM_TAG; 59 static const std::string JSON_BORDER_RADIUS_TAG; 60 static const std::string JSON_LEFT_TOP_TAG; 61 static const std::string JSON_LEFT_BOTTOM_TAG; 62 static const std::string JSON_RIGHT_TOP_TAG; 63 static const std::string JSON_RIGHT_BOTTOM_TAG; 64 static const std::string JSON_TEXT_ICON_PADDING_TAG; 65 static const std::string JSON_RECT_WIDTH_TAG; 66 static const std::string JSON_RECT_HEIGHT_TAG; 67 static const std::string JSON_COLORS_TAG; 68 static const std::string JSON_FONT_COLOR_TAG; 69 static const std::string JSON_ICON_COLOR_TAG; 70 static const std::string JSON_BG_COLOR_TAG; 71 72 static const std::string JSON_BORDER_TAG; 73 static const std::string JSON_BORDER_WIDTH_TAG; 74 static const std::string JSON_PARENT_TAG; 75 static const std::string JSON_PARENT_EFFECT_TAG; 76 static const std::string JSON_IS_CLIPPED_TAG; 77 static const std::string JSON_TOP_CLIP_TAG; 78 static const std::string JSON_BOTTOM_CLIP_TAG; 79 static const std::string JSON_LEFT_CLIP_TAG; 80 static const std::string JSON_RIGHT_CLIP_TAG; 81 static const std::string JSON_PARENT_TAG_TAG; 82 83 static const std::string JSON_STYLE_TAG; 84 static const std::string JSON_TEXT_TAG; 85 static const std::string JSON_ICON_TAG; 86 static const std::string JSON_BG_TAG; 87 static const std::string JSON_WINDOW_ID; 88 static const std::string JSON_DISPLAY_ID; 89 static const std::string JSON_CROSS_AXIS_STATE; 90 static const std::string JSON_IS_ICON_EXCEEDED_TAG; 91 static const std::string JSON_IS_BORDER_COVERED_TAG; 92 static const std::string JSON_NON_COMPATIBLE_CHANGE_TAG; 93 static const std::string JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG; 94 static const std::string JSON_FOREGROUND_BLUR_RADIUS_TAG; 95 static const std::string JSON_IS_OVERLAY_TEXT_SET_TAG; 96 static const std::string JSON_IS_OVERLAY_NODE_SET_TAG; 97 static const std::string JSON_IS_CUSTOMIZABLE; 98 static const std::string JSON_TIP_POSITION; 99 }; 100 101 class __attribute__((visibility("default"))) SecCompBase { 102 public: 103 SecCompBase() = default; 104 virtual ~SecCompBase() = default; 105 bool FromJson(const nlohmann::json& jsonSrc, std::string& message, bool isClicked); 106 void ToJson(nlohmann::json& jsonRes) const; 107 std::string ToJsonStr(void) const; 108 virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const; SetValid(bool valid)109 void SetValid(bool valid) 110 { 111 isValid_ = valid; 112 }; 113 GetValid()114 bool GetValid() 115 { 116 return isValid_; 117 }; 118 119 // size 120 DimensionT fontSize_ = DEFAULT_DIMENSION; 121 DimensionT iconSize_ = DEFAULT_DIMENSION; 122 PaddingSize padding_; 123 BorderRadius borderRadius_; 124 DimensionT textIconSpace_ = DEFAULT_DIMENSION; 125 126 // color 127 SecCompColor fontColor_; 128 SecCompColor iconColor_; 129 SecCompColor bgColor_; 130 131 // border 132 DimensionT borderWidth_ = DEFAULT_DIMENSION; 133 134 // parent effect 135 bool parentEffect_ = false; 136 bool isClipped_ = false; 137 DimensionT topClip_; 138 DimensionT bottomClip_; 139 DimensionT leftClip_; 140 DimensionT rightClip_; 141 std::string parentTag_; 142 143 SecCompType type_ = UNKNOWN_SC_TYPE; 144 SecCompRect rect_; 145 SecCompRect windowRect_; 146 bool isValid_ = false; 147 148 int32_t text_ = UNKNOWN_TEXT; 149 int32_t icon_ = UNKNOWN_ICON; 150 SecCompBackground bg_ = SecCompBackground::UNKNOWN_BG; 151 152 bool hasNonCompatibleChange_ = false; 153 double blurRadius_ = 0.0; 154 double foregroundBlurRadius_ = 0.0; 155 bool isOverlayTextSet_ = false; 156 bool isOverlayNodeCovered_ = false; 157 int32_t windowId_ = 0; 158 uint64_t displayId_ = 0; 159 int32_t nodeId_ = 0; 160 CrossAxisState crossAxisState_ = CrossAxisState::STATE_INVALID; 161 bool isIconExceeded_ = false; 162 bool isBorderCovered_ = false; 163 bool isWearableDevice_ = false; 164 TipPosition tipPosition_ = TipPosition::ABOVE_BOTTOM; 165 bool isCustomizable_ = false; 166 protected: 167 virtual bool IsTextIconTypeValid(std::string& message, bool isClicked) = 0; 168 virtual bool IsCorrespondenceType() = 0; 169 private: 170 bool ParseNonCompatibleChange(const nlohmann::json& json); 171 bool ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res); 172 bool ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res); 173 bool ParseBool(const nlohmann::json& json, const std::string& tag, bool& res); 174 bool ParseString(const nlohmann::json& json, const std::string& tag, std::string& res); 175 bool ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res); 176 bool ParseBorderRadius(const nlohmann::json& json, const std::string& tag, BorderRadius& res); 177 bool ParseColors(const nlohmann::json& json, const std::string& tag); 178 bool ParseBorders(const nlohmann::json& json, const std::string& tag); 179 bool ParseSize(const nlohmann::json& json, const std::string& tag); 180 bool ParseParent(const nlohmann::json& json, const std::string& tag); 181 bool ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect); 182 bool ParseStyle(const nlohmann::json& json, const std::string& tag, std::string& message, bool isClicked); 183 bool ParseType(const nlohmann::json& json, const std::string& tag); 184 bool ParseValue(const nlohmann::json& json, const std::string& tag, int32_t& value); 185 bool ParseDisplayId(const nlohmann::json& json, const std::string& tag); 186 bool ParseCrossAxisState(const nlohmann::json& json, const std::string& tag); 187 bool ParseTipPosition(const nlohmann::json& json, const std::string& tag); 188 bool ParseWearable(const nlohmann::json& json, const std::string& tag); 189 void ToJsonRect(nlohmann::json& jsonRes) const; 190 void ToJsonSize(nlohmann::json& jsonRes) const; 191 bool ParseComponentInfo(const nlohmann::json& json, std::string& message, bool isClicked); 192 bool ParseWindowInfo(const nlohmann::json& json); 193 bool ParseDisplayInfo(const nlohmann::json& json); 194 bool ParseCustomInfo(const nlohmann::json& json); 195 }; 196 } // namespace SecurityComponent 197 } // namespace Security 198 } // namespace OHOS 199 #endif // SECURITY_COMPONENT_BASE_H 200