• 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 
16 #include "core/components_ng/pattern/security_component/security_component_handler.h"
17 
18 #include <securec.h>
19 
20 #include "adapter/ohos/entrance/ace_container.h"
21 #include "base/log/ace_scoring_log.h"
22 #include "base/utils/system_properties.h"
23 #include "base/utils/utils.h"
24 #include "core/common/container.h"
25 #include "base/geometry/dimension.h"
26 #include "core/components_ng/pattern/button/button_layout_property.h"
27 #include "core/components_ng/pattern/security_component/security_component_log.h"
28 #include "core/components_ng/pattern/text/text_layout_property.h"
29 #include "core/components_ng/pattern/window_scene/scene/system_window_scene.h"
30 #include "core/components_ng/property/gradient_property.h"
31 #include "core/components_v2/inspector/inspector_constants.h"
32 
33 namespace OHOS::Ace::NG {
34 using namespace OHOS::Security;
35 using namespace OHOS::Security::SecurityComponent;
36 namespace {
37 constexpr uint64_t SECOND_TO_MILLISECOND = 1000;
38 constexpr float HALF = 2.0f;
39 }
40 
41 static std::vector<uintptr_t> g_callList = {
42     reinterpret_cast<uintptr_t>(SecurityComponentHandler::RegisterSecurityComponent),
43     reinterpret_cast<uintptr_t>(SecurityComponentHandler::UpdateSecurityComponent),
44     reinterpret_cast<uintptr_t>(SecurityComponentHandler::ReportSecurityComponentClickEventInner)
45 };
46 
47 SecurityComponentProbe SecurityComponentHandler::probe;
48 SecurityComponent::SecCompUiRegister uiRegister(g_callList, &SecurityComponentHandler::probe);
49 
GetDisplayOffset(RefPtr<FrameNode> & node,double & offsetX,double & offsetY)50 bool SecurityComponentHandler::GetDisplayOffset(RefPtr<FrameNode>& node, double& offsetX, double& offsetY)
51 {
52     double x = node->GetTransformRelativeOffset().GetX();
53     double y = node->GetTransformRelativeOffset().GetY();
54     auto container = Container::Current();
55     CHECK_NULL_RETURN(container, false);
56     auto pipelineContext = container->GetPipelineContext();
57     CHECK_NULL_RETURN(pipelineContext, false);
58     auto windowOffset = pipelineContext->GetDisplayWindowRectInfo().GetOffset();
59     offsetX = x + windowOffset.GetX();
60     offsetY = y + windowOffset.GetY();
61     return true;
62 }
63 
GetWindowRect(RefPtr<FrameNode> & node,OHOS::Security::SecurityComponent::SecCompRect & winRect)64 bool SecurityComponentHandler::GetWindowRect(RefPtr<FrameNode>& node,
65     OHOS::Security::SecurityComponent::SecCompRect& winRect)
66 {
67     auto container = Container::Current();
68     CHECK_NULL_RETURN(container, false);
69     auto pipelineContext = container->GetPipelineContext();
70     CHECK_NULL_RETURN(pipelineContext, false);
71     auto rect = pipelineContext->GetDisplayWindowRectInfo();
72     winRect.x_ = rect.Left();
73     winRect.y_ = rect.Top();
74     winRect.width_ = rect.Right() - rect.Left();
75     winRect.height_ = rect.Bottom() - rect.Top();
76     return true;
77 }
78 
CheckOpacity(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)79 bool SecurityComponentHandler::CheckOpacity(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
80 {
81     if (node->GetTag() == V2::MENU_WRAPPER_ETS_TAG) {
82         return false;
83     }
84     if (renderContext->GetOpacity().has_value() &&
85         !NearEqual(renderContext->GetOpacity().value(), 1.0f)) {
86         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s opacity = %{public}f, " \
87             "security component is invalid", node->GetTag().c_str(), renderContext->GetOpacity().value());
88         return true;
89     }
90     return false;
91 }
92 
CheckBrightness(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)93 bool SecurityComponentHandler::CheckBrightness(const RefPtr<FrameNode>& node,
94     const RefPtr<RenderContext>& renderContext)
95 {
96     if (renderContext->GetFrontBrightness().has_value() &&
97         !NearEqual(renderContext->GetFrontBrightness().value().Value(), 1.0f)) {
98         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s brightness = %{public}f, " \
99             "security component is invalid", node->GetTag().c_str(),
100             renderContext->GetFrontBrightness().value().Value());
101         return true;
102     }
103     return false;
104 }
105 
CheckVisibility(const RefPtr<FrameNode> & node,RefPtr<LayoutProperty> & layoutProperty)106 bool SecurityComponentHandler::CheckVisibility(const RefPtr<FrameNode>& node, RefPtr<LayoutProperty>& layoutProperty)
107 {
108     if (layoutProperty->GetVisibility().has_value() &&
109         (layoutProperty->GetVisibility().value() != VisibleType::VISIBLE)) {
110         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s is not visible, security component is invalid",
111             node->GetTag().c_str());
112         return true;
113     }
114     return false;
115 }
116 
CheckBlur(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)117 bool SecurityComponentHandler::CheckBlur(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
118 {
119     if (renderContext->GetFrontBlurRadius().has_value() &&
120         GreatNotEqual(renderContext->GetFrontBlurRadius().value().ConvertToPx(), 0.0f)) {
121         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s blur is set, security component is invalid",
122             node->GetTag().c_str());
123         return true;
124     }
125     return false;
126 }
127 
CheckForegroundBlurStyle(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)128 bool SecurityComponentHandler::CheckForegroundBlurStyle(const RefPtr<FrameNode>& node,
129     const RefPtr<RenderContext>& renderContext)
130 {
131     auto blurStyleOption = renderContext->GetFrontBlurStyle();
132     if (blurStyleOption.has_value() && (blurStyleOption->blurStyle != BlurStyle::NO_MATERIAL) &&
133         (!NearEqual(blurStyleOption->scale, 0.0))) {
134         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s foregroundBlurStyle is set, " \
135             "security component is invalid", node->GetTag().c_str());
136         return true;
137     }
138     return false;
139 }
140 
CheckBlendMode(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)141 bool SecurityComponentHandler::CheckBlendMode(const RefPtr<FrameNode>& node,
142     const RefPtr<RenderContext>& renderContext)
143 {
144     auto blendMode = renderContext->GetBackBlendMode();
145     if (blendMode.has_value() && blendMode != BlendMode::NONE && blendMode != BlendMode::SRC_OVER) {
146         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s blendMode is set, " \
147             "security component is invalid", node->GetTag().c_str());
148         return true;
149     }
150     return false;
151 }
152 
GetLinearGradientBlurRatio(std::vector<std::pair<float,float>> & fractionStops)153 float SecurityComponentHandler::GetLinearGradientBlurRatio(std::vector<std::pair<float, float>>& fractionStops)
154 {
155     float ratio = 1.0;
156     int32_t size = static_cast<int32_t>(fractionStops.size());
157     for (auto i = 0; i < size; i++) {
158         auto fraction = fractionStops[i];
159         if (NearEqual(fraction.first, 0.0)) {
160             ratio = fraction.second;
161         } else {
162             break;
163         }
164     }
165     return ratio;
166 }
167 
CheckDistance(const float & deltaY,const float & radius,const float & distance,const int32_t & multiplier)168 bool SecurityComponentHandler::CheckDistance(const float& deltaY, const float& radius, const float& distance,
169     const int32_t& multiplier)
170 {
171     if (NearEqual(radius, 0.0)) {
172         if (GreatNotEqual(deltaY * multiplier, 0.0)) {
173             if (GreatNotEqual(distance, 1.0)) {
174                 return true;
175             }
176         }
177         return false;
178     }
179 
180     if (GreatOrEqual(deltaY * multiplier, 0.0)) {
181         return true;
182     }
183     if (LessNotEqual(distance, (radius - 1) * (radius - 1))) {
184         return true;
185     }
186     return false;
187 }
188 
CheckDiagonalLinearGradientBlur(const RectF & parentRect,const RectF & rect,const NG::GradientDirection direction,const float & ratio,const float & radius)189 bool SecurityComponentHandler::CheckDiagonalLinearGradientBlur(const RectF& parentRect, const RectF& rect,
190     const NG::GradientDirection direction, const float& ratio, const float& radius)
191 {
192     Point dest;
193     Point src;
194     float gradient;
195     int32_t multiplier = 0;
196     switch (direction) {
197         case GradientDirection::LEFT_TOP:
198             dest.SetX(rect.GetX() + radius);
199             dest.SetY(rect.GetY() + radius);
200             src.SetX(parentRect.GetX() + (1 - ratio) * parentRect.Width());
201             src.SetY(parentRect.GetY() + (1 - ratio) * parentRect.Height());
202             gradient = (0 - parentRect.Width()) / parentRect.Height();
203             multiplier = 1;
204             break;
205         case GradientDirection::LEFT_BOTTOM:
206             dest.SetX(rect.GetX() + radius);
207             dest.SetY(rect.GetY() + rect.Height() - radius);
208             src.SetX(parentRect.GetX() + (1 - ratio) * parentRect.Width());
209             src.SetY(parentRect.GetY() + ratio * parentRect.Height());
210             gradient = parentRect.Width() / parentRect.Height();
211             multiplier = -1;
212             break;
213         case GradientDirection::RIGHT_TOP:
214             dest.SetX(rect.GetX() + rect.Width() - radius);
215             dest.SetY(rect.GetY() + radius);
216             src.SetX(parentRect.GetX() + ratio * parentRect.Width());
217             src.SetY(parentRect.GetY() + (1 - ratio) * parentRect.Height());
218             gradient = parentRect.Width() / parentRect.Height();
219             multiplier = 1;
220             break;
221         case GradientDirection::RIGHT_BOTTOM:
222             dest.SetX(rect.GetX() + rect.Width() - radius);
223             dest.SetY(rect.GetY() + rect.Height() - radius);
224             src.SetX(parentRect.GetX() + ratio * parentRect.Width());
225             src.SetY(parentRect.GetY() + ratio * parentRect.Height());
226             gradient = (0 - parentRect.Width()) / parentRect.Height();
227             multiplier = -1;
228             break;
229         default:
230             return false;
231     }
232 
233     float deltaY = gradient * dest.GetX() + src.GetY() - gradient * src.GetX() - dest.GetY();
234     auto distance = (deltaY * deltaY) / (1 + gradient * gradient);
235     return CheckDistance(deltaY, radius, distance, multiplier);
236 }
237 
GetBorderRadius(RefPtr<FrameNode> & node,const NG::GradientDirection direction)238 float SecurityComponentHandler::GetBorderRadius(RefPtr<FrameNode>& node, const NG::GradientDirection direction)
239 {
240     RectF rect = node->GetTransformRectRelativeToWindow();
241     auto maxRadius = std::min(rect.Width(), rect.Height()) / HALF;
242     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
243     CHECK_NULL_RETURN(layoutProperty, 0.0);
244     if (layoutProperty->GetBackgroundType() == static_cast<int32_t>(ButtonType::CIRCLE) ||
245         layoutProperty->GetBackgroundType() == static_cast<int32_t>(ButtonType::CAPSULE)) {
246         return maxRadius;
247     }
248 
249     RefPtr<FrameNode> buttonNode = GetSecCompChildNode(node, V2::BUTTON_ETS_TAG);
250     CHECK_NULL_RETURN(buttonNode, 0.0);
251     auto bgProp = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
252     CHECK_NULL_RETURN(bgProp, 0.0);
253     auto borderRadius = bgProp->GetBorderRadius();
254     float radius = 0.0;
255 
256     switch (direction) {
257         case GradientDirection::LEFT_TOP:
258             if (borderRadius.has_value() && borderRadius->radiusTopLeft.has_value()) {
259                 auto obtainedRadius = borderRadius->radiusTopLeft.value().ConvertToPx();
260                 radius = GreatNotEqual(obtainedRadius, maxRadius) ? maxRadius : obtainedRadius;
261             }
262             return radius;
263         case GradientDirection::LEFT_BOTTOM:
264             if (borderRadius.has_value() && borderRadius->radiusBottomLeft.has_value()) {
265                 auto obtainedRadius = borderRadius->radiusBottomLeft.value().ConvertToPx();
266                 radius = GreatNotEqual(obtainedRadius, maxRadius) ? maxRadius : obtainedRadius;
267             }
268             return radius;
269         case GradientDirection::RIGHT_TOP:
270             if (borderRadius.has_value() && borderRadius->radiusTopRight.has_value()) {
271                 auto obtainedRadius = borderRadius->radiusTopRight.value().ConvertToPx();
272                 radius = GreatNotEqual(obtainedRadius, maxRadius) ? maxRadius : obtainedRadius;
273             }
274             return radius;
275         case GradientDirection::RIGHT_BOTTOM:
276             if (borderRadius.has_value() && borderRadius->radiusBottomRight.has_value()) {
277                 auto obtainedRadius = borderRadius->radiusBottomRight.value().ConvertToPx();
278                 radius = GreatNotEqual(obtainedRadius, maxRadius) ? maxRadius : obtainedRadius;
279             }
280             return radius;
281         default:
282             return radius;
283     }
284     return radius;
285 }
286 
CheckLinearGradientBlur(const RefPtr<FrameNode> & parentNode,RefPtr<FrameNode> & node)287 bool SecurityComponentHandler::CheckLinearGradientBlur(const RefPtr<FrameNode>& parentNode,
288     RefPtr<FrameNode>& node)
289 {
290     RectF parentRect = parentNode->GetTransformRectRelativeToWindow();
291     if (NearEqual(parentRect.Width(), 0.0) || NearEqual(parentRect.Height(), 0.0)) {
292         return false;
293     }
294 
295     RectF rect = node->GetTransformRectRelativeToWindow();
296     const auto& parentRender = parentNode->GetRenderContext();
297     CHECK_NULL_RETURN(parentRender, false);
298     auto linearGradientBlurPara = parentRender->GetLinearGradientBlur();
299     CHECK_NULL_RETURN(linearGradientBlurPara, false);
300     float ratio = GetLinearGradientBlurRatio(linearGradientBlurPara->fractionStops_);
301     if (NearEqual(ratio, 1.0)) {
302         return false;
303     }
304 
305     float radius = 0.0;
306     switch (linearGradientBlurPara->direction_) {
307         case GradientDirection::LEFT:
308             return GreatNotEqual((parentRect.GetX() + parentRect.Width() - rect.GetX()) / parentRect.Width(), ratio);
309         case GradientDirection::TOP:
310             return GreatNotEqual((parentRect.GetY() + parentRect.Height() - rect.GetY()) / parentRect.Height(), ratio);
311         case GradientDirection::RIGHT:
312             return GreatNotEqual((rect.GetX() + rect.Width() - parentRect.GetX()) / parentRect.Width(), ratio);
313         case GradientDirection::BOTTOM:
314             return GreatNotEqual((rect.GetY() + rect.Height() - parentRect.GetY()) / parentRect.Height(), ratio);
315         case GradientDirection::LEFT_TOP:
316         case GradientDirection::LEFT_BOTTOM:
317         case GradientDirection::RIGHT_TOP:
318         case GradientDirection::RIGHT_BOTTOM:
319             radius = GetBorderRadius(node, linearGradientBlurPara->direction_);
320             return CheckDiagonalLinearGradientBlur(parentRect, rect,
321                 linearGradientBlurPara->direction_, ratio, radius);
322         default:
323             return false;
324     }
325 }
326 
CheckGrayScale(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)327 bool SecurityComponentHandler::CheckGrayScale(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
328 {
329     if (renderContext->GetFrontGrayScale().has_value() &&
330         GreatNotEqual(renderContext->GetFrontGrayScale().value().ConvertToVp(), 0.0f)) {
331         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s grayscale is set, security component is invalid",
332             node->GetTag().c_str());
333         return true;
334     }
335     return false;
336 }
337 
CheckSaturate(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)338 bool SecurityComponentHandler::CheckSaturate(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
339 {
340     if (renderContext->GetFrontSaturate().has_value() &&
341         !NearEqual(renderContext->GetFrontSaturate().value().ConvertToVp(), 1.0f)) {
342         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s saturate is set, security component is invalid",
343             node->GetTag().c_str());
344         return true;
345     }
346     return false;
347 }
348 
CheckContrast(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)349 bool SecurityComponentHandler::CheckContrast(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
350 {
351     if (renderContext->GetFrontContrast().has_value() &&
352         !NearEqual(renderContext->GetFrontContrast().value().ConvertToVp(), 1.0f)) {
353         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s contrast is set, security component is invalid",
354             node->GetTag().c_str());
355         return true;
356     }
357     return false;
358 }
359 
CheckInvert(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)360 bool SecurityComponentHandler::CheckInvert(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
361 {
362     if (renderContext->GetFrontInvert().has_value() && renderContext->GetFrontInvert()->index() == 0 &&
363         !NearEqual(std::get<float>(renderContext->GetFrontInvert().value()), 0.0f)) {
364         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s invert is set, security component is invalid",
365             node->GetTag().c_str());
366         return true;
367     }
368     return false;
369 }
370 
CheckSepia(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)371 bool SecurityComponentHandler::CheckSepia(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
372 {
373     if (renderContext->GetFrontSepia().has_value() &&
374         !NearEqual(renderContext->GetFrontSepia().value().ConvertToVp(), 0.0f)) {
375         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s sepia is set, security component is invalid",
376             node->GetTag().c_str());
377         return true;
378     }
379     return false;
380 }
381 
CheckHueRotate(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)382 bool SecurityComponentHandler::CheckHueRotate(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
383 {
384     if (renderContext->GetFrontHueRotate().has_value() &&
385         !NearEqual(renderContext->GetFrontHueRotate().value(), 0.0f) &&
386         !NearEqual(renderContext->GetFrontHueRotate().value(), 360.0f)) {
387         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s hueRotate is set, security component is invalid",
388             node->GetTag().c_str());
389         return true;
390     }
391     return false;
392 }
393 
CheckColorBlend(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)394 bool SecurityComponentHandler::CheckColorBlend(const RefPtr<FrameNode>& node,
395     const RefPtr<RenderContext>& renderContext)
396 {
397     if (renderContext->GetFrontColorBlend().has_value() &&
398         (renderContext->GetFrontColorBlend().value() != Color::TRANSPARENT)) {
399         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s colorBlend is set, security component is invalid",
400             node->GetTag().c_str());
401         return true;
402     }
403     return false;
404 }
405 
CheckClipMask(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)406 bool SecurityComponentHandler::CheckClipMask(const RefPtr<FrameNode>& node, const RefPtr<RenderContext>& renderContext)
407 {
408     if (renderContext->GetClipMask().has_value()) {
409         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s clip mask is set, security component is invalid",
410             node->GetTag().c_str());
411         return true;
412     }
413     return false;
414 }
415 
CheckForegroundColor(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)416 bool SecurityComponentHandler::CheckForegroundColor(const RefPtr<FrameNode>& node,
417     const RefPtr<RenderContext>& renderContext)
418 {
419     if (renderContext->GetForegroundColor().has_value()) {
420         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s foregroundColor is set, " \
421             "security component is invalid", node->GetTag().c_str());
422         return true;
423     }
424     return false;
425 }
426 
CheckSphericalEffect(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)427 bool SecurityComponentHandler::CheckSphericalEffect(const RefPtr<FrameNode>& node,
428     const RefPtr<RenderContext>& renderContext)
429 {
430     if (renderContext->GetSphericalEffect().has_value() &&
431         !NearEqual(renderContext->GetSphericalEffect().value(), 0.0f)) {
432         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s sphericalEffect is set, " \
433             "security component is invalid", node->GetTag().c_str());
434         return true;
435     }
436     return false;
437 }
438 
CheckLightUpEffect(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)439 bool SecurityComponentHandler::CheckLightUpEffect(const RefPtr<FrameNode>& node,
440     const RefPtr<RenderContext>& renderContext)
441 {
442     if (renderContext->GetLightUpEffect().has_value()) {
443         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s lightUpEffect is set, " \
444             "security component is invalid", node->GetTag().c_str());
445         return true;
446     }
447     return false;
448 }
449 
CheckPixelStretchEffect(const RefPtr<FrameNode> & node,const RefPtr<RenderContext> & renderContext)450 bool SecurityComponentHandler::CheckPixelStretchEffect(const RefPtr<FrameNode>& node,
451     const RefPtr<RenderContext>& renderContext)
452 {
453     if (renderContext->GetPixelStretchEffect().has_value()) {
454         SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s pixelStretchEffect is set, " \
455             "security component is invalid", node->GetTag().c_str());
456         return true;
457     }
458     return false;
459 }
460 
CheckRenderEffect(RefPtr<FrameNode> & node)461 bool SecurityComponentHandler::CheckRenderEffect(RefPtr<FrameNode>& node)
462 {
463     const auto& renderContext = node->GetRenderContext();
464     CHECK_NULL_RETURN(renderContext, false);
465     auto layoutProperty = node->GetLayoutProperty();
466     CHECK_NULL_RETURN(layoutProperty, false);
467 
468     if (CheckOpacity(node, renderContext) || CheckBrightness(node, renderContext) ||
469         CheckVisibility(node, layoutProperty) || CheckBlur(node, renderContext) ||
470         CheckGrayScale(node, renderContext) || CheckSaturate(node, renderContext) ||
471         CheckContrast(node, renderContext) || CheckInvert(node, renderContext) ||
472         CheckSepia(node, renderContext) || CheckHueRotate(node, renderContext) ||
473         CheckColorBlend(node, renderContext) || CheckClipMask(node, renderContext) ||
474         CheckForegroundColor(node, renderContext) || CheckSphericalEffect(node, renderContext) ||
475         CheckLightUpEffect(node, renderContext) || CheckPixelStretchEffect(node, renderContext) ||
476         CheckForegroundBlurStyle(node, renderContext) || CheckBlendMode(node, renderContext)) {
477         return true;
478     }
479     return false;
480 }
481 
CheckParentNodesEffect(RefPtr<FrameNode> & node,OHOS::Security::SecurityComponent::SecCompBase & buttonInfo)482 bool SecurityComponentHandler::CheckParentNodesEffect(RefPtr<FrameNode>& node,
483     OHOS::Security::SecurityComponent::SecCompBase& buttonInfo)
484 {
485     RefPtr<RenderContext> renderContext = node->GetRenderContext();
486     CHECK_NULL_RETURN(renderContext, false);
487     auto frameRect = renderContext->GetPaintRectWithTransform();
488     frameRect.SetOffset(node->GetPositionToScreenWithTransform());
489     auto visibleRect = frameRect;
490     auto parent = node->GetParent();
491     while (parent != nullptr) {
492         auto parentNode = AceType::DynamicCast<FrameNode>(parent);
493         if (parentNode == nullptr) {
494             parent = parent->GetParent();
495             continue;
496         }
497         if (parentNode->CheckTopWindowBoundary()) {
498             break;
499         }
500         if (CheckRenderEffect(parentNode)) {
501             return true;
502         }
503         if (CheckLinearGradientBlur(parentNode, node)) {
504             SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s LinearGradientBlur is set, " \
505                 "security component is invalid", parentNode->GetTag().c_str());
506             return true;
507         }
508         RefPtr<RenderContext> parentRenderContext = parentNode->GetRenderContext();
509         if ((parentRenderContext == nullptr) ||
510             !parentRenderContext->GetClipEdge().value_or(false)) {
511             parent = parent->GetParent();
512             continue;
513         }
514         GetVisibleRect(parentNode, visibleRect);
515         bool isClipped = IsOutOfParentWithRound(visibleRect, frameRect, buttonInfo);
516         buttonInfo.isClipped_ = isClipped;
517         buttonInfo.parentTag_ = parentNode->GetTag();
518 
519         if (isClipped && (visibleRect.IsValid() || frameRect.IsValid())) {
520             SC_LOG_ERROR("SecurityComponentCheckFail: Parents clip is set, " \
521                 "security component is not completely displayed.");
522             SC_LOG_ERROR("visibleWidth: %{public}f, visibleHeight: %{public}f, " \
523                 "frameWidth: %{public}f, frameHeight: %{public}f",
524                 visibleRect.Width(), visibleRect.Height(), frameRect.Width(), frameRect.Height());
525             return true;
526         }
527         parent = parent->GetParent();
528     }
529     return false;
530 }
531 
GetVisibleRect(RefPtr<FrameNode> & node,RectF & visibleRect)532 void SecurityComponentHandler::GetVisibleRect(RefPtr<FrameNode>& node, RectF& visibleRect)
533 {
534     auto renderContext = node->GetRenderContext();
535     CHECK_NULL_VOID(renderContext);
536     RectF parentRect = renderContext->GetPaintRectWithTransform();
537     parentRect.SetOffset(node->GetPositionToScreenWithTransform());
538     visibleRect = visibleRect.Constrain(parentRect);
539 }
540 
IsOutOfParentWithRound(const RectF & visibleRect,const RectF & renderRect,OHOS::Security::SecurityComponent::SecCompBase & buttonInfo)541 bool SecurityComponentHandler::IsOutOfParentWithRound(const RectF& visibleRect, const RectF& renderRect,
542     OHOS::Security::SecurityComponent::SecCompBase& buttonInfo)
543 {
544     if (!visibleRect.IsValid() || !renderRect.IsValid()) {
545         return true;
546     }
547 
548     if (NearEqual(visibleRect.Width(), 0.0) || NearEqual(visibleRect.Height(), 0.0) ||
549         NearEqual(renderRect.Width(), 0.0) || NearEqual(renderRect.Height(), 0.0)) {
550         return true;
551     }
552 
553     buttonInfo.leftClip_ = visibleRect.Left() - renderRect.Left();
554     buttonInfo.rightClip_ = renderRect.Right() - visibleRect.Right();
555     buttonInfo.topClip_ = visibleRect.Top() - renderRect.Top();
556     buttonInfo.bottomClip_ = renderRect.Bottom() - visibleRect.Bottom();
557 
558     return LessNotEqual(renderRect.Left() + 1.0, visibleRect.Left()) ||
559         GreatNotEqual(renderRect.Right(), visibleRect.Right() + 1.0) ||
560         LessNotEqual(renderRect.Top() + 1.0, visibleRect.Top()) ||
561         GreatNotEqual(renderRect.Bottom(), visibleRect.Bottom() + 1.0);
562 }
563 
GetWindowSceneWindowId(RefPtr<FrameNode> & node,uint32_t & windId)564 bool SecurityComponentHandler::GetWindowSceneWindowId(RefPtr<FrameNode>& node, uint32_t& windId)
565 {
566     CHECK_NULL_RETURN(node, false);
567     auto parent = node->GetParent();
568     while (parent != nullptr && parent->GetTag() != V2::WINDOW_SCENE_ETS_TAG) {
569         parent = parent->GetParent();
570     }
571     CHECK_NULL_RETURN(parent, false);
572     auto windowSceneFrameNode = AceType::DynamicCast<FrameNode>(parent);
573     CHECK_NULL_RETURN(windowSceneFrameNode, false);
574     auto windowScene = windowSceneFrameNode->GetPattern<SystemWindowScene>();
575     CHECK_NULL_RETURN(windowScene, false);
576     auto session = windowScene->GetSession();
577     CHECK_NULL_RETURN(session, false);
578 
579     windId = static_cast<uint32_t>(session->GetPersistentId());
580     return true;
581 }
582 
GetPaddingInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & node)583 bool SecurityComponentHandler::GetPaddingInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
584     RefPtr<FrameNode>& node)
585 {
586     CHECK_NULL_RETURN(node, false);
587     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
588     CHECK_NULL_RETURN(layoutProperty, false);
589     auto pipeline = node->GetContextRefPtr();
590     CHECK_NULL_RETURN(pipeline, false);
591     auto theme = pipeline->GetTheme<SecurityComponentTheme>();
592     CHECK_NULL_RETURN(theme, false);
593     buttonInfo.padding_.top =
594         layoutProperty->GetBackgroundTopPadding().value_or(theme->GetBackgroundTopPadding()).ConvertToVp();
595     buttonInfo.padding_.right =
596         layoutProperty->GetBackgroundRightPadding().value_or(theme->GetBackgroundRightPadding()).ConvertToVp();
597     buttonInfo.padding_.bottom =
598         layoutProperty->GetBackgroundBottomPadding().value_or(theme->GetBackgroundBottomPadding()).ConvertToVp();
599     buttonInfo.padding_.left =
600         layoutProperty->GetBackgroundLeftPadding().value_or(theme->GetBackgroundLeftPadding()).ConvertToVp();
601     buttonInfo.textIconSpace_ =
602         layoutProperty->GetTextIconSpace().value_or(theme->GetTextIconSpace()).ConvertToVp();
603     return true;
604 }
605 
InitBaseInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & node)606 bool SecurityComponentHandler::InitBaseInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
607     RefPtr<FrameNode>& node)
608 {
609     CHECK_NULL_RETURN(node, false);
610     buttonInfo.nodeId_ = node->GetId();
611     if (!GetPaddingInfo(buttonInfo, node)) {
612         SC_LOG_WARN("InitBaseInfoWarning: Get padding info failed");
613         return false;
614     }
615 
616     if (!GetDisplayOffset(node, buttonInfo.rect_.x_, buttonInfo.rect_.y_)) {
617         SC_LOG_WARN("InitBaseInfoWarning: Get display offset failed");
618         return false;
619     }
620 
621     if (!GetWindowRect(node, buttonInfo.windowRect_)) {
622         SC_LOG_WARN("InitBaseInfoWarning: Get window rect failed");
623         return false;
624     }
625     auto render = node->GetRenderContext();
626     CHECK_NULL_RETURN(render, false);
627     auto rect = render->GetPaintRectWithTransform();
628     buttonInfo.rect_.width_ = rect.Width();
629     buttonInfo.rect_.height_ = rect.Height();
630     auto container = AceType::DynamicCast<Platform::AceContainer>(Container::Current());
631     CHECK_NULL_RETURN(container, false);
632     uint32_t windId = container->GetWindowId();
633     auto pipeline = node->GetContextRefPtr();
634     CHECK_NULL_RETURN(pipeline, false);
635     if (pipeline->IsFocusWindowIdSetted()) {
636         windId = pipeline->GetFocusWindowId();
637     }
638     if (container->IsScenceBoardWindow()) {
639         GetWindowSceneWindowId(node, windId);
640     }
641     buttonInfo.windowId_ = static_cast<int32_t>(windId);
642     buttonInfo.crossAxisState_ = CrossAxisState::STATE_INVALID;
643     auto instanceId = pipeline->GetInstanceId();
644     auto window = Platform::AceContainer::GetUIWindow(instanceId);
645     if (window) {
646         buttonInfo.crossAxisState_ = static_cast<CrossAxisState>(window->GetCrossAxisState());
647         buttonInfo.displayId_ = window->GetDisplayId();
648     }
649     return true;
650 }
651 
InitSCIconInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & iconNode)652 bool InitSCIconInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
653     RefPtr<FrameNode>& iconNode)
654 {
655     if (iconNode != nullptr) {
656         CHECK_NULL_RETURN(iconNode->GetGeometryNode(), false);
657         auto iconProp = iconNode->GetLayoutProperty<ImageLayoutProperty>();
658         CHECK_NULL_RETURN(iconProp, false);
659         CHECK_NULL_RETURN(iconProp->GetCalcLayoutConstraint(), false);
660         buttonInfo.iconSize_ =
661             iconProp->GetCalcLayoutConstraint()->selfIdealSize->Width()->GetDimension().ConvertToVp();
662         buttonInfo.iconColor_.value =
663             iconProp->GetImageSourceInfo().value().GetFillColor().value().GetValue();
664     }
665     return true;
666 }
667 
InitSCTextInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & textNode)668 bool InitSCTextInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
669     RefPtr<FrameNode>& textNode)
670 {
671     if (textNode != nullptr) {
672         auto textProp = textNode->GetLayoutProperty<TextLayoutProperty>();
673         CHECK_NULL_RETURN(textProp, false);
674         auto pipeline = textNode->GetContextRefPtr();
675         CHECK_NULL_RETURN(pipeline, false);
676         auto theme = pipeline->GetTheme<SecurityComponentTheme>();
677         CHECK_NULL_RETURN(theme, false);
678         if (textProp->GetFontSize().has_value()) {
679             buttonInfo.fontSize_ = textProp->GetFontSize()->Value();
680         } else {
681             buttonInfo.fontSize_ = theme->GetFontSize().Value();
682         }
683         if (textProp->GetTextColor().has_value()) {
684             buttonInfo.fontColor_.value = textProp->GetTextColor().value().GetValue();
685         }
686     }
687     return true;
688 }
689 
InitSCButtonInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & buttonNode)690 bool InitSCButtonInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
691     RefPtr<FrameNode>& buttonNode)
692 {
693     if (buttonNode != nullptr) {
694         const auto& renderContext = buttonNode->GetRenderContext();
695         CHECK_NULL_RETURN(renderContext, false);
696         if (renderContext->GetBackgroundColor().has_value()) {
697             buttonInfo.bgColor_.value = renderContext->GetBackgroundColor().value().GetValue();
698         }
699 
700         auto bgProp = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
701         CHECK_NULL_RETURN(bgProp, false);
702         const auto& borderWidth = bgProp->GetBorderWidthProperty();
703         if (borderWidth != nullptr) {
704             if (borderWidth->leftDimen.has_value()) {
705                 buttonInfo.borderWidth_ = borderWidth->leftDimen.value().ConvertToVp();
706             }
707         }
708     }
709     return true;
710 }
711 
InitChildInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & node)712 bool SecurityComponentHandler::InitChildInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
713     RefPtr<FrameNode>& node)
714 {
715     RefPtr<FrameNode> iconNode = GetSecCompChildNode(node, V2::IMAGE_ETS_TAG);
716     if (!InitSCIconInfo(buttonInfo, iconNode)) {
717         return false;
718     }
719 
720     RefPtr<FrameNode> textNode = GetSecCompChildNode(node, V2::TEXT_ETS_TAG);
721     if (!InitSCTextInfo(buttonInfo, textNode)) {
722         return false;
723     }
724 
725     RefPtr<FrameNode> buttonNode = GetSecCompChildNode(node, V2::BUTTON_ETS_TAG);
726     if (!InitSCButtonInfo(buttonInfo, buttonNode)) {
727         return false;
728     }
729 
730     if (!InitBaseInfo(buttonInfo, node)) {
731         return false;
732     }
733     return true;
734 }
735 
WriteButtonInfo(const RefPtr<OHOS::Ace::NG::SecurityComponentLayoutProperty> & layoutProperty,RefPtr<FrameNode> & node,OHOS::Security::SecurityComponent::SecCompBase & buttonInfo)736 void SecurityComponentHandler::WriteButtonInfo(
737     const RefPtr<OHOS::Ace::NG::SecurityComponentLayoutProperty>& layoutProperty,
738     RefPtr<FrameNode>& node, OHOS::Security::SecurityComponent::SecCompBase& buttonInfo)
739 {
740     buttonInfo.parentEffect_ = CheckParentNodesEffect(node, buttonInfo);
741     buttonInfo.text_ = layoutProperty->GetSecurityComponentDescription().value();
742     buttonInfo.icon_ = layoutProperty->GetIconStyle().value();
743     buttonInfo.bg_ = static_cast<SecCompBackground>(
744         layoutProperty->GetBackgroundType().value());
745 }
746 
InitButtonInfo(std::string & componentInfo,RefPtr<FrameNode> & node,SecCompType & scType)747 bool SecurityComponentHandler::InitButtonInfo(std::string& componentInfo, RefPtr<FrameNode>& node, SecCompType& scType)
748 {
749     CHECK_NULL_RETURN(node, false);
750     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
751     CHECK_NULL_RETURN(layoutProperty, false);
752     std::string type = node->GetTag();
753     if (type == V2::LOCATION_BUTTON_ETS_TAG) {
754         LocationButton buttonInfo;
755         WriteButtonInfo(layoutProperty, node, buttonInfo);
756         buttonInfo.type_ = SecCompType::LOCATION_COMPONENT;
757         scType = SecCompType::LOCATION_COMPONENT;
758         if (!InitChildInfo(buttonInfo, node)) {
759             return false;
760         }
761         componentInfo = buttonInfo.ToJsonStr();
762     } else if (type == V2::PASTE_BUTTON_ETS_TAG) {
763         PasteButton buttonInfo;
764         WriteButtonInfo(layoutProperty, node, buttonInfo);
765         buttonInfo.type_ = SecCompType::PASTE_COMPONENT;
766         scType = SecCompType::PASTE_COMPONENT;
767         if (!InitChildInfo(buttonInfo, node)) {
768             return false;
769         }
770         componentInfo = buttonInfo.ToJsonStr();
771     } else if (type == V2::SAVE_BUTTON_ETS_TAG) {
772         SaveButton buttonInfo;
773         WriteButtonInfo(layoutProperty, node, buttonInfo);
774         buttonInfo.type_ = SecCompType::SAVE_COMPONENT;
775         scType = SecCompType::SAVE_COMPONENT;
776         if (!InitChildInfo(buttonInfo, node)) {
777             return false;
778         }
779         componentInfo = buttonInfo.ToJsonStr();
780     } else {
781         return false;
782     }
783     return true;
784 }
785 
UpdateClipRect(NG::RectF & clipRect,NG::RectF & paintRect)786 NG::RectF SecurityComponentHandler::UpdateClipRect(NG::RectF& clipRect, NG::RectF& paintRect)
787 {
788     if (clipRect.IsIntersectWith(paintRect)) {
789         return clipRect.IntersectRectT(paintRect);
790     }
791 
792     return NG::RectF(0.0, 0.0, 0.0, 0.0);
793 }
794 
UpdatePaintRect(NG::RectF & paintRect,NG::RectF & clipRect)795 NG::RectF SecurityComponentHandler::UpdatePaintRect(NG::RectF& paintRect, NG::RectF& clipRect)
796 {
797     if (NearEqual(clipRect.Width(), -1.0) && NearEqual(clipRect.Height(), -1.0)) {
798         return paintRect;
799     }
800 
801     if (paintRect.IsIntersectWith(clipRect)) {
802         return paintRect.IntersectRectT(clipRect);
803     }
804 
805     return NG::RectF(0.0, 0.0, 0.0, 0.0);
806 }
807 
RegisterSecurityComponent(RefPtr<FrameNode> & node,int32_t & scId)808 int32_t SecurityComponentHandler::RegisterSecurityComponent(RefPtr<FrameNode>& node, int32_t& scId)
809 {
810     SecurityComponentHandler::probe.InitProbeTask();
811     std::string componentInfo;
812     SecCompType type;
813     if (!InitButtonInfo(componentInfo, node, type)) {
814         return -1;
815     }
816     int32_t ret = SecCompKit::RegisterSecurityComponent(
817         type, componentInfo, scId);
818     return ret;
819 }
820 
UpdateSecurityComponent(RefPtr<FrameNode> & node,int32_t & scId)821 int32_t SecurityComponentHandler::UpdateSecurityComponent(RefPtr<FrameNode>& node, int32_t& scId)
822 {
823     std::string componentInfo;
824     SecCompType type;
825     if (!InitButtonInfo(componentInfo, node, type)) {
826         return -1;
827     }
828     int32_t ret = SecCompKit::UpdateSecurityComponent(scId, componentInfo);
829     return ret;
830 }
831 
UnregisterSecurityComponent(int32_t & scId)832 int32_t SecurityComponentHandler::UnregisterSecurityComponent(int32_t& scId)
833 {
834     if (scId == -1) {
835         return -1;
836     }
837     int32_t ret = SecCompKit::UnregisterSecurityComponent(scId);
838     return ret;
839 }
840 
IsContextTransparent(const RefPtr<FrameNode> & frameNode)841 bool SecurityComponentHandler::IsContextTransparent(const RefPtr<FrameNode>& frameNode)
842 {
843     const RefPtr<RenderContext> renderContext = frameNode->GetRenderContext();
844     CHECK_NULL_RETURN(renderContext, false);
845     auto layoutProperty = frameNode->GetLayoutProperty();
846     CHECK_NULL_RETURN(layoutProperty, false);
847     if (renderContext->GetOpacity().has_value() && renderContext->GetOpacity().value() == 0.0) {
848         return true;
849     }
850     if (static_cast<int32_t>(layoutProperty->GetVisibility().value_or(VisibleType::VISIBLE)) != 0) {
851         return true;
852     }
853     return false;
854 }
855 
CheckContainerTags(const RefPtr<FrameNode> & frameNode)856 bool SecurityComponentHandler::CheckContainerTags(const RefPtr<FrameNode>& frameNode)
857 {
858     static std::set<std::string> containerComponentTags = { "Flex", "Stack", "Row", "Column", "WindowScene", "root",
859         "Swiper", "Grid", "GridItem", "page", "stage", "FormComponent", "Tabs", "TabContent", "ColumnSplit",
860         "FolderStack", "GridCol", "GridRow", "RelativeContainer", "RowSplit", "List", "Scroll", "WaterFlow",
861         "SideBarContainer", "Refresh", "Navigator", "ListItemGroup", "ListItem", "Hyperlink", "FormLink", "FlowItem",
862         "Counter", "Custom", "overlay" };
863 
864     const RefPtr<RenderContext> renderContext = frameNode->GetRenderContext();
865     CHECK_NULL_RETURN(renderContext, false);
866     if (containerComponentTags.find(frameNode->GetTag()) != containerComponentTags.end() &&
867         renderContext->GetBackgroundColor()->ColorToString().compare("#00000000") == 0) {
868         return true;
869     }
870     return false;
871 }
872 
IsInModalPage(const RefPtr<UINode> & node)873 bool SecurityComponentHandler::IsInModalPage(const RefPtr<UINode>& node)
874 {
875     RefPtr<UINode> tmpNode = node;
876     while (tmpNode) {
877         if (tmpNode->GetTag() == V2::MODAL_PAGE_TAG) {
878             return true;
879         }
880         tmpNode = tmpNode->GetParent();
881     }
882     return false;
883 }
884 
CheckSecurityComponentStatus(const RefPtr<UINode> & root,NodeMaps & maps,int32_t secNodeId,NG::RectF & clipRect)885 bool SecurityComponentHandler::CheckSecurityComponentStatus(const RefPtr<UINode>& root, NodeMaps& maps,
886     int32_t secNodeId, NG::RectF& clipRect)
887 {
888     bool res = false;
889     RectF paintRect;
890 
891     auto frameNode = AceType::DynamicCast<NG::FrameNode>(root);
892     if (frameNode) {
893         paintRect = frameNode->GetTransformRectRelativeToWindow();
894         if (IsSecurityComponent(frameNode) && (frameNode->GetId() == secNodeId)) {
895             if (IsInModalPage(root)) {
896                 return false;
897             }
898             return CheckRectIntersect(paintRect, secNodeId, maps.nodeId2Rect, maps.nodeId2Zindex);
899         }
900     }
901     auto& children = root->GetChildren();
902     for (auto child = children.rbegin(); child != children.rend(); ++child) {
903         auto node = AceType::DynamicCast<NG::FrameNode>(*child);
904         if (node && (IsContextTransparent(node) || !node->IsActive())) {
905             continue;
906         }
907         NG::RectF bakClipRect = clipRect;
908         if (frameNode && frameNode->GetRenderContext() &&
909             frameNode->GetRenderContext()->GetClipEdge().has_value() && frameNode->GetRenderContext()->GetClipEdge()) {
910             if (NearEqual(clipRect.Width(), -1.0) && NearEqual(clipRect.Height(), -1.0)) {
911                 clipRect = paintRect;
912             } else {
913                 clipRect = UpdateClipRect(clipRect, paintRect);
914             }
915         }
916         res |= CheckSecurityComponentStatus(*child, maps, secNodeId, clipRect);
917         clipRect = bakClipRect;
918     }
919 
920     if (frameNode && frameNode->GetTag() != V2::SHEET_WRAPPER_TAG && !CheckContainerTags(frameNode)) {
921         paintRect = UpdatePaintRect(paintRect, clipRect);
922         maps.nodeId2Rect[frameNode->GetId()] = paintRect;
923     }
924     return res;
925 }
926 
CheckRectIntersect(const RectF & dest,int32_t secNodeId,const std::unordered_map<int32_t,NG::RectF> & nodeId2Rect,std::unordered_map<int32_t,int32_t> & nodeId2Zindex)927 bool SecurityComponentHandler::CheckRectIntersect(const RectF& dest, int32_t secNodeId,
928     const std::unordered_map<int32_t, NG::RectF>& nodeId2Rect,
929     std::unordered_map<int32_t, int32_t>& nodeId2Zindex)
930 {
931     for (const auto& originRect : nodeId2Rect) {
932         if (originRect.second.IsInnerIntersectWithRound(dest) &&
933             (nodeId2Zindex[secNodeId] <= nodeId2Zindex[originRect.first]) &&
934             (!NearEqual(originRect.second.Width(), 0.0) && !NearEqual(originRect.second.Height(), 0.0))) {
935             SC_LOG_ERROR("SecurityComponentCheckFail: Security component id = %{public}d " \
936                 "is covered by id = %{public}d.", secNodeId, originRect.first);
937             return true;
938         }
939     }
940     return false;
941 }
942 
IsSecurityComponent(RefPtr<FrameNode> & node)943 bool SecurityComponentHandler::IsSecurityComponent(RefPtr<FrameNode>& node)
944 {
945     return node->GetTag() == V2::LOCATION_BUTTON_ETS_TAG || node->GetTag() == V2::PASTE_BUTTON_ETS_TAG ||
946            node->GetTag() == V2::SAVE_BUTTON_ETS_TAG;
947 }
948 
GetNodeZIndex(const RefPtr<UINode> & root)949 int32_t SecurityComponentHandler::GetNodeZIndex(const RefPtr<UINode>& root)
950 {
951     int32_t zIndex;
952     auto node = AceType::DynamicCast<NG::FrameNode>(root);
953     if (node) {
954         const RefPtr<RenderContext> renderContext = node->GetRenderContext();
955         if (!renderContext) {
956             zIndex = 0;
957         } else {
958             zIndex = renderContext->GetZIndexValue(ZINDEX_DEFAULT_VALUE);
959         }
960     } else {
961         zIndex = 0;
962     }
963 
964     return zIndex;
965 }
966 
UpdateAllZindex(const RefPtr<UINode> & root,std::unordered_map<int32_t,int32_t> & nodeId2Zindex)967 void SecurityComponentHandler::UpdateAllZindex(const RefPtr<UINode>& root,
968     std::unordered_map<int32_t, int32_t>& nodeId2Zindex)
969 {
970     if (nodeId2Zindex.count(root->GetId()) == 0) {
971         nodeId2Zindex[root->GetId()] = GetNodeZIndex(root);
972     }
973     auto& children = root->GetChildren();
974     for (auto child = children.begin(); child != children.end(); ++child) {
975         int32_t nodeZIndex = GetNodeZIndex(*child);
976         nodeId2Zindex[(*child)->GetId()] = std::max(nodeZIndex, nodeId2Zindex[root->GetId()]);
977         UpdateAllZindex(*child, nodeId2Zindex);
978     }
979 }
980 
CheckComponentCoveredStatus(int32_t secNodeId)981 bool SecurityComponentHandler::CheckComponentCoveredStatus(int32_t secNodeId)
982 {
983     auto pipeline = PipelineContext::GetCurrentContextSafely();
984     CHECK_NULL_RETURN(pipeline, false);
985     RefPtr<UINode> root = pipeline->GetRootElement();
986     CHECK_NULL_RETURN(root, false);
987     NodeMaps maps;
988     UpdateAllZindex(root, maps.nodeId2Zindex);
989     NG::RectF clipRect = NG::RectF(-1.0, -1.0, -1.0, -1.0);
990     if (CheckSecurityComponentStatus(root, maps, secNodeId, clipRect)) {
991         return true;
992     }
993     return false;
994 }
995 
ReportSecurityComponentClickEventInner(int32_t & scId,RefPtr<FrameNode> & node,SecCompClickEvent & event,Security::SecurityComponent::OnFirstUseDialogCloseFunc && callback)996 int32_t SecurityComponentHandler::ReportSecurityComponentClickEventInner(int32_t& scId,
997     RefPtr<FrameNode>& node, SecCompClickEvent& event,
998     Security::SecurityComponent::OnFirstUseDialogCloseFunc&& callback)
999 {
1000     std::string componentInfo;
1001     SecCompType type;
1002     if (!InitButtonInfo(componentInfo, node, type)) {
1003         return -1;
1004     }
1005     auto container = AceType::DynamicCast<Platform::AceContainer>(Container::Current());
1006     CHECK_NULL_RETURN(container, -1);
1007     sptr<IRemoteObject> token = container->GetToken();
1008     if (container->GetParentToken() != nullptr) {
1009         token = container->GetParentToken();
1010     }
1011     return SecCompKit::ReportSecurityComponentClickEvent(scId,
1012         componentInfo, event, token, std::move(callback));
1013 }
1014 
ReportSecurityComponentClickEvent(int32_t & scId,RefPtr<FrameNode> & node,GestureEvent & event,Security::SecurityComponent::OnFirstUseDialogCloseFunc && callback)1015 int32_t SecurityComponentHandler::ReportSecurityComponentClickEvent(int32_t& scId,
1016     RefPtr<FrameNode>& node, GestureEvent& event,
1017     Security::SecurityComponent::OnFirstUseDialogCloseFunc&& callback)
1018 {
1019     SecCompClickEvent secEvent;
1020     secEvent.type = ClickEventType::POINT_EVENT_TYPE;
1021 #ifdef SECURITY_COMPONENT_ENABLE
1022     secEvent.point.touchX = event.GetDisplayX();
1023     secEvent.point.touchY = event.GetDisplayY();
1024     auto pointerEvent = event.GetPointerEvent();
1025     uint8_t defaultData = 0;
1026     std::vector<uint8_t> dataBuffer;
1027     if (pointerEvent == nullptr) {
1028         SC_LOG_WARN("SecurityComponentClickEventWarning: Receive a NULL pointerEvent, set default data.");
1029         secEvent.extraInfo.data = &defaultData;
1030         secEvent.extraInfo.dataSize = 1;
1031         secEvent.point.timestamp = 0;
1032     } else {
1033         dataBuffer = pointerEvent->GetEnhanceData();
1034         if (dataBuffer.size() > 0) {
1035             secEvent.extraInfo.data = dataBuffer.data();
1036             secEvent.extraInfo.dataSize = dataBuffer.size();
1037         }
1038         std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
1039         TimeStamp time(microseconds);
1040         secEvent.point.timestamp =
1041             static_cast<uint64_t>(time.time_since_epoch().count()) / SECOND_TO_MILLISECOND;
1042     }
1043 #endif
1044     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
1045     if (layoutProperty && layoutProperty->GetIsTextLimitExceeded().has_value() &&
1046         layoutProperty->GetIsTextLimitExceeded().value()) {
1047         SC_LOG_ERROR("SecurityComponentCheckFail: The text of the security component is out of range.");
1048         return -1;
1049     }
1050     if (CheckComponentCoveredStatus(node->GetId())) {
1051         SC_LOG_ERROR("SecurityComponentCheckFail: Security component is covered by another component.");
1052         return -1;
1053     }
1054 
1055     return ReportSecurityComponentClickEventInner(scId, node, secEvent, std::move(callback));
1056 }
1057 
ReportSecurityComponentClickEvent(int32_t & scId,RefPtr<FrameNode> & node,const KeyEvent & event,Security::SecurityComponent::OnFirstUseDialogCloseFunc && callback)1058 int32_t SecurityComponentHandler::ReportSecurityComponentClickEvent(int32_t& scId,
1059     RefPtr<FrameNode>& node, const KeyEvent& event,
1060     Security::SecurityComponent::OnFirstUseDialogCloseFunc&& callback)
1061 {
1062     SecCompClickEvent secEvent;
1063     secEvent.type = ClickEventType::KEY_EVENT_TYPE;
1064 
1065     secEvent.key.timestamp =
1066         static_cast<uint64_t>(event.timeStamp.time_since_epoch().count()) / SECOND_TO_MILLISECOND;
1067     secEvent.key.keyCode = static_cast<int32_t>(event.code);
1068     auto data = event.enhanceData;
1069     if (data.size() > 0) {
1070         secEvent.extraInfo.data = data.data();
1071         secEvent.extraInfo.dataSize = data.size();
1072     }
1073     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
1074     if (layoutProperty && layoutProperty->GetIsTextLimitExceeded().has_value() &&
1075         layoutProperty->GetIsTextLimitExceeded().value()) {
1076         SC_LOG_ERROR("SecurityComponentCheckFail: The text of the security component is out of range.");
1077         return -1;
1078     }
1079     if (CheckComponentCoveredStatus(node->GetId())) {
1080         SC_LOG_ERROR("SecurityComponentCheckFail: Security component is covered by another component.");
1081         return -1;
1082     }
1083     return ReportSecurityComponentClickEventInner(scId, node, secEvent, std::move(callback));
1084 }
1085 
IsSecurityComponentServiceExist()1086 bool SecurityComponentHandler::IsSecurityComponentServiceExist()
1087 {
1088     return SecCompKit::IsServiceExist();
1089 }
1090 
LoadSecurityComponentService()1091 bool SecurityComponentHandler::LoadSecurityComponentService()
1092 {
1093     return SecCompKit::LoadService();
1094 }
1095 
IsSystemAppCalling()1096 bool SecurityComponentHandler::IsSystemAppCalling()
1097 {
1098     return SecCompKit::IsSystemAppCalling();
1099 }
1100 } // namespace OHOS::Ace::NG
1101