• 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_info_helper.h"
16 
17 #include "accesstoken_kit.h"
18 #include "display.h"
19 #include "display_info.h"
20 #include "display_manager.h"
21 #include "location_button.h"
22 #include "paste_button.h"
23 #include "save_button.h"
24 #include "sec_comp_err.h"
25 #include "sec_comp_log.h"
26 #include "sec_comp_tool.h"
27 #include "window_info_helper.h"
28 
29 namespace OHOS {
30 namespace Security {
31 namespace SecurityComponent {
32 namespace {
33 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelper"};
34 static constexpr double MAX_RECT_PERCENT = 0.1F; // 10%
35 static constexpr double ZERO_OFFSET = 0.0F;
36 static std::mutex g_renderLock;
37 }
38 
AdjustSecCompRect(SecCompBase * comp,float scale)39 void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale)
40 {
41     comp->rect_.width_ *= scale;
42     comp->rect_.height_ *= scale;
43     comp->rect_.x_ = comp->windowRect_.x_ + (comp->rect_.x_ - comp->windowRect_.x_) * scale;
44     comp->rect_.y_ = comp->windowRect_.y_ + (comp->rect_.y_ - comp->windowRect_.y_) * scale;
45 
46     SC_LOG_DEBUG(LABEL, "After adjust x %{public}f, y %{public}f, width %{public}f, height %{public}f",
47         comp->rect_.x_, comp->rect_.y_, comp->rect_.width_, comp->rect_.height_);
48 
49     comp->windowRect_.width_ *= scale;
50     comp->windowRect_.height_ *= scale;
51 }
52 
ParseComponent(SecCompType type,const nlohmann::json & jsonComponent)53 SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann::json& jsonComponent)
54 {
55     SecCompBase* comp = nullptr;
56     switch (type) {
57         case LOCATION_COMPONENT:
58             comp = ConstructComponent<LocationButton>(jsonComponent);
59             break;
60         case PASTE_COMPONENT:
61             comp = ConstructComponent<PasteButton>(jsonComponent);
62             break;
63         case SAVE_COMPONENT:
64             comp = ConstructComponent<SaveButton>(jsonComponent);
65             break;
66         default:
67             SC_LOG_ERROR(LABEL, "Parse component type unknown");
68             break;
69     }
70     if (comp == nullptr) {
71         SC_LOG_ERROR(LABEL, "Parse component failed");
72         return comp;
73     }
74 
75     comp->SetValid(CheckComponentValid(comp));
76     return comp;
77 }
78 
GetScreenSize(double & width,double & height,const uint64_t displayId,const CrossAxisState crossAxisState)79 static bool GetScreenSize(double& width, double& height, const uint64_t displayId, const CrossAxisState crossAxisState)
80 {
81     sptr<OHOS::Rosen::Display> display =
82         OHOS::Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
83     if (display == nullptr) {
84         SC_LOG_ERROR(LABEL, "Get display manager failed");
85         return false;
86     }
87 
88     auto info = display->GetDisplayInfo();
89     if (info == nullptr) {
90         SC_LOG_ERROR(LABEL, "Get display info failed");
91         return false;
92     }
93 
94     width = static_cast<double>(info->GetWidth());
95     if (crossAxisState == CrossAxisState::STATE_CROSS) {
96         height = static_cast<double>(info->GetPhysicalHeight());
97     } else {
98         height = static_cast<double>(info->GetHeight());
99     }
100     SC_LOG_DEBUG(LABEL, "display manager Screen width %{public}f height %{public}f",
101         width, height);
102     return true;
103 }
104 
CheckRectValid(const SecCompRect & rect,const SecCompRect & windowRect,const uint64_t displayId,const CrossAxisState crossAxisState)105 bool SecCompInfoHelper::CheckRectValid(const SecCompRect& rect, const SecCompRect& windowRect,
106     const uint64_t displayId, const CrossAxisState crossAxisState)
107 {
108     double curScreenWidth = 0.0F;
109     double curScreenHeight = 0.0F;
110     if (!GetScreenSize(curScreenWidth, curScreenHeight, displayId, crossAxisState)) {
111         SC_LOG_ERROR(LABEL, "Get screen size is invalid");
112         return false;
113     }
114 
115     if (GreatOrEqual(0.0, rect.width_) || GreatOrEqual(0.0, rect.height_)) {
116         SC_LOG_ERROR(LABEL, "width or height is <= 0");
117         return false;
118     }
119 
120     if (GreatNotEqual(ZERO_OFFSET, rect.x_) || GreatNotEqual(ZERO_OFFSET, rect.y_) ||
121         GreatNotEqual(rect.x_, curScreenWidth) || GreatNotEqual(rect.y_, curScreenHeight)) {
122         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: security component is out of screen");
123         return false;
124     }
125 
126     if (GreatNotEqual((rect.x_ + rect.width_), curScreenWidth + 1.0) ||
127         GreatNotEqual((rect.y_ + rect.height_), curScreenHeight + 1.0)) {
128         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: security component is out of screen");
129         return false;
130     }
131 
132     if (GreatNotEqual(windowRect.x_, rect.x_ + 1.0) || GreatNotEqual(windowRect.y_, rect.y_ + 1.0) ||
133         GreatNotEqual(rect.x_ + rect.width_, windowRect.x_ + windowRect.width_ + 1.0) ||
134         GreatNotEqual(rect.y_ + rect.height_, windowRect.y_ + windowRect.height_ + 1.0)) {
135         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: security component is out of window");
136         return false;
137     }
138 
139     // check rect > 10%
140     if (GreatOrEqual((rect.width_ * rect.height_), (curScreenWidth * curScreenHeight * MAX_RECT_PERCENT))) {
141         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: security component is larger than 10 percent of screen");
142         return false;
143     }
144     SC_LOG_DEBUG(LABEL, "check component rect success.");
145     return true;
146 }
147 
CheckSecCompBaseButton(const SecCompBase * comp)148 static bool CheckSecCompBaseButton(const SecCompBase* comp)
149 {
150     if ((comp->text_ < 0) && (comp->icon_ < 0)) {
151         SC_LOG_INFO(LABEL, "both text and icon do not exist.");
152         return false;
153     }
154     if (comp->text_ >= 0) {
155         DimensionT minFontSize;
156         if (comp->icon_ >= 0) {
157             minFontSize = MIN_FONT_SIZE_WITH_ICON;
158         } else {
159             minFontSize = MIN_FONT_SIZE_WITHOUT_ICON;
160         }
161 
162         if (comp->fontSize_ < minFontSize) {
163             SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: fontSize is too small.");
164             return false;
165         }
166     }
167     if ((comp->icon_ >= 0) && comp->iconSize_ < MIN_ICON_SIZE) {
168         SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: iconSize is too small.");
169         return false;
170     }
171 
172     if ((comp->bg_ != SecCompBackground::NO_BG_TYPE) && !IsColorFullTransparent(comp->bgColor_) &&
173         (((comp->text_ != NO_TEXT) && (IsColorSimilar(comp->fontColor_, comp->bgColor_))) ||
174         ((comp->icon_ != NO_ICON) && (IsColorSimilar(comp->iconColor_, comp->bgColor_))))) {
175         SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: fontColor or iconColor is similar with backgroundColor.");
176         return false;
177     }
178 
179     if (comp->bg_ == SecCompBackground::NO_BG_TYPE &&
180         ((comp->padding_.top != MIN_PADDING_WITHOUT_BG) || (comp->padding_.right != MIN_PADDING_WITHOUT_BG) ||
181         (comp->padding_.bottom != MIN_PADDING_WITHOUT_BG) || (comp->padding_.left != MIN_PADDING_WITHOUT_BG))) {
182         SC_LOG_INFO(LABEL, "padding can not change without background.");
183         return false;
184     }
185 
186     return true;
187 }
188 
CheckSecCompBase(const SecCompBase * comp)189 static bool CheckSecCompBase(const SecCompBase* comp)
190 {
191     if (comp->parentEffect_) {
192         SC_LOG_ERROR(LABEL,
193             "SecurityComponentCheckFail: the parents of security component have invalid effect.");
194         return false;
195     }
196 
197     if ((comp->padding_.top < MIN_PADDING_SIZE) || (comp->padding_.right < MIN_PADDING_SIZE) ||
198         (comp->padding_.bottom < MIN_PADDING_SIZE) || (comp->padding_.left < MIN_PADDING_SIZE) ||
199         (comp->textIconSpace_ < MIN_PADDING_SIZE)) {
200         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: padding or textIconSpace is too small.");
201         return false;
202     }
203 
204     if (((comp->text_ != NO_TEXT) && (IsColorTransparent(comp->fontColor_))) ||
205         ((comp->icon_ != NO_ICON) && (IsColorTransparent(comp->iconColor_)))) {
206         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: fontColor or iconColor is too transparent.");
207         return false;
208     }
209     if (!CheckSecCompBaseButton(comp)) {
210         return false;
211     }
212     return true;
213 }
214 
CheckComponentValid(SecCompBase * comp)215 bool SecCompInfoHelper::CheckComponentValid(SecCompBase* comp)
216 {
217     if ((comp == nullptr) || !IsComponentTypeValid(comp->type_)) {
218         SC_LOG_INFO(LABEL, "comp is null or type is invalid.");
219         return false;
220     }
221 
222     float scale = WindowInfoHelper::GetWindowScale(comp->windowId_);
223     SC_LOG_DEBUG(LABEL, "WindowScale = %{public}f", scale);
224     if (!IsEqual(scale, WindowInfoHelper::FULL_SCREEN_SCALE) && !IsEqual(scale, 0.0)) {
225         AdjustSecCompRect(comp, scale);
226     }
227 
228     if (!CheckSecCompBase(comp)) {
229         SC_LOG_INFO(LABEL, "SecComp base is invalid.");
230         return false;
231     }
232 
233     return true;
234 }
235 }  // namespace SecurityComponent
236 }  // namespace Security
237 }  // namespace OHOS
238