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 (CheckRenderEffect(parentNode)) {
498             return true;
499         }
500         if (CheckLinearGradientBlur(parentNode, node)) {
501             SC_LOG_ERROR("SecurityComponentCheckFail: Parent %{public}s LinearGradientBlur is set, " \
502                 "security component is invalid", parentNode->GetTag().c_str());
503             return true;
504         }
505         RefPtr<RenderContext> parentRenderContext = parentNode->GetRenderContext();
506         if ((parentRenderContext == nullptr) ||
507             !parentRenderContext->GetClipEdge().value_or(false)) {
508             parent = parent->GetParent();
509             continue;
510         }
511         GetVisibleRect(parentNode, visibleRect);
512         bool isClipped = IsOutOfParentWithRound(visibleRect, frameRect, buttonInfo);
513         buttonInfo.isClipped_ = isClipped;
514         buttonInfo.parentTag_ = parentNode->GetTag();
515 
516         if (isClipped && (visibleRect.IsValid() || frameRect.IsValid())) {
517             SC_LOG_ERROR("SecurityComponentCheckFail: Parents clip is set, " \
518                 "security component is not completely displayed.");
519             SC_LOG_ERROR("visibleWidth: %{public}f, visibleHeight: %{public}f, " \
520                 "frameWidth: %{public}f, frameHeight: %{public}f",
521                 visibleRect.Width(), visibleRect.Height(), frameRect.Width(), frameRect.Height());
522             return true;
523         }
524         parent = parent->GetParent();
525     }
526     return false;
527 }
528 
GetVisibleRect(RefPtr<FrameNode> & node,RectF & visibleRect)529 void SecurityComponentHandler::GetVisibleRect(RefPtr<FrameNode>& node, RectF& visibleRect)
530 {
531     auto renderContext = node->GetRenderContext();
532     CHECK_NULL_VOID(renderContext);
533     RectF parentRect = renderContext->GetPaintRectWithTransform();
534     parentRect.SetOffset(node->GetPositionToScreenWithTransform());
535     visibleRect = visibleRect.Constrain(parentRect);
536 }
537 
IsOutOfParentWithRound(const RectF & visibleRect,const RectF & renderRect,OHOS::Security::SecurityComponent::SecCompBase & buttonInfo)538 bool SecurityComponentHandler::IsOutOfParentWithRound(const RectF& visibleRect, const RectF& renderRect,
539     OHOS::Security::SecurityComponent::SecCompBase& buttonInfo)
540 {
541     if (!visibleRect.IsValid() || !renderRect.IsValid()) {
542         return true;
543     }
544 
545     if (NearEqual(visibleRect.Width(), 0.0) || NearEqual(visibleRect.Height(), 0.0) ||
546         NearEqual(renderRect.Width(), 0.0) || NearEqual(renderRect.Height(), 0.0)) {
547         return true;
548     }
549 
550     buttonInfo.leftClip_ = visibleRect.Left() - renderRect.Left();
551     buttonInfo.rightClip_ = renderRect.Right() - visibleRect.Right();
552     buttonInfo.topClip_ = visibleRect.Top() - renderRect.Top();
553     buttonInfo.bottomClip_ = renderRect.Bottom() - visibleRect.Bottom();
554 
555     return LessNotEqual(renderRect.Left() + 1.0, visibleRect.Left()) ||
556         GreatNotEqual(renderRect.Right(), visibleRect.Right() + 1.0) ||
557         LessNotEqual(renderRect.Top() + 1.0, visibleRect.Top()) ||
558         GreatNotEqual(renderRect.Bottom(), visibleRect.Bottom() + 1.0);
559 }
560 
GetWindowSceneWindowId(RefPtr<FrameNode> & node,uint32_t & windId)561 bool SecurityComponentHandler::GetWindowSceneWindowId(RefPtr<FrameNode>& node, uint32_t& windId)
562 {
563     CHECK_NULL_RETURN(node, false);
564     auto parent = node->GetParent();
565     while (parent != nullptr && parent->GetTag() != V2::WINDOW_SCENE_ETS_TAG) {
566         parent = parent->GetParent();
567     }
568     CHECK_NULL_RETURN(parent, false);
569     auto windowSceneFrameNode = AceType::DynamicCast<FrameNode>(parent);
570     CHECK_NULL_RETURN(windowSceneFrameNode, false);
571     auto windowScene = windowSceneFrameNode->GetPattern<SystemWindowScene>();
572     CHECK_NULL_RETURN(windowScene, false);
573     auto session = windowScene->GetSession();
574     CHECK_NULL_RETURN(session, false);
575 
576     windId = static_cast<uint32_t>(session->GetPersistentId());
577     return true;
578 }
579 
InitBaseInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & node)580 bool SecurityComponentHandler::InitBaseInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
581     RefPtr<FrameNode>& node)
582 {
583     CHECK_NULL_RETURN(node, false);
584     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
585     CHECK_NULL_RETURN(layoutProperty, false);
586     buttonInfo.nodeId_ = node->GetId();
587 
588     auto pipeline = node->GetContextRefPtr();
589     CHECK_NULL_RETURN(pipeline, false);
590     auto theme = pipeline->GetTheme<SecurityComponentTheme>();
591     CHECK_NULL_RETURN(theme, false);
592     buttonInfo.padding_.top =
593         layoutProperty->GetBackgroundTopPadding().value_or(theme->GetBackgroundTopPadding()).ConvertToVp();
594     buttonInfo.padding_.right =
595         layoutProperty->GetBackgroundRightPadding().value_or(theme->GetBackgroundRightPadding()).ConvertToVp();
596     buttonInfo.padding_.bottom =
597         layoutProperty->GetBackgroundBottomPadding().value_or(theme->GetBackgroundBottomPadding()).ConvertToVp();
598     buttonInfo.padding_.left =
599         layoutProperty->GetBackgroundLeftPadding().value_or(theme->GetBackgroundLeftPadding()).ConvertToVp();
600     buttonInfo.textIconSpace_ =
601         layoutProperty->GetTextIconSpace().value_or(theme->GetTextIconSpace()).ConvertToVp();
602 
603     if (!GetDisplayOffset(node, buttonInfo.rect_.x_, buttonInfo.rect_.y_)) {
604         SC_LOG_WARN("InitBaseInfoWarning: Get display offset failed");
605         return false;
606     }
607 
608     if (!GetWindowRect(node, buttonInfo.windowRect_)) {
609         SC_LOG_WARN("InitBaseInfoWarning: Get window rect failed");
610         return false;
611     }
612     auto render = node->GetRenderContext();
613     CHECK_NULL_RETURN(render, false);
614     auto rect = render->GetPaintRectWithTransform();
615     buttonInfo.rect_.width_ = rect.Width();
616     buttonInfo.rect_.height_ = rect.Height();
617     auto container = AceType::DynamicCast<Platform::AceContainer>(Container::Current());
618     CHECK_NULL_RETURN(container, false);
619     uint32_t windId = container->GetWindowId();
620     if (pipeline->IsFocusWindowIdSetted()) {
621         windId = pipeline->GetFocusWindowId();
622     }
623     if (container->IsScenceBoardWindow()) {
624         GetWindowSceneWindowId(node, windId);
625     }
626     buttonInfo.windowId_ = static_cast<int32_t>(windId);
627     return true;
628 }
629 
InitSCIconInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & iconNode)630 bool InitSCIconInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
631     RefPtr<FrameNode>& iconNode)
632 {
633     if (iconNode != nullptr) {
634         CHECK_NULL_RETURN(iconNode->GetGeometryNode(), false);
635         auto iconProp = iconNode->GetLayoutProperty<ImageLayoutProperty>();
636         CHECK_NULL_RETURN(iconProp, false);
637         CHECK_NULL_RETURN(iconProp->GetCalcLayoutConstraint(), false);
638         buttonInfo.iconSize_ =
639             iconProp->GetCalcLayoutConstraint()->selfIdealSize->Width()->GetDimension().ConvertToVp();
640         buttonInfo.iconColor_.value =
641             iconProp->GetImageSourceInfo().value().GetFillColor().value().GetValue();
642     }
643     return true;
644 }
645 
InitSCTextInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & textNode)646 bool InitSCTextInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
647     RefPtr<FrameNode>& textNode)
648 {
649     if (textNode != nullptr) {
650         auto textProp = textNode->GetLayoutProperty<TextLayoutProperty>();
651         CHECK_NULL_RETURN(textProp, false);
652         auto pipeline = textNode->GetContextRefPtr();
653         CHECK_NULL_RETURN(pipeline, false);
654         auto theme = pipeline->GetTheme<SecurityComponentTheme>();
655         CHECK_NULL_RETURN(theme, false);
656         if (textProp->GetFontSize().has_value()) {
657             buttonInfo.fontSize_ = textProp->GetFontSize()->Value();
658         } else {
659             buttonInfo.fontSize_ = theme->GetFontSize().Value();
660         }
661         if (textProp->GetTextColor().has_value()) {
662             buttonInfo.fontColor_.value = textProp->GetTextColor().value().GetValue();
663         }
664     }
665     return true;
666 }
667 
InitSCButtonInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & buttonNode)668 bool InitSCButtonInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
669     RefPtr<FrameNode>& buttonNode)
670 {
671     if (buttonNode != nullptr) {
672         const auto& renderContext = buttonNode->GetRenderContext();
673         CHECK_NULL_RETURN(renderContext, false);
674         if (renderContext->GetBackgroundColor().has_value()) {
675             buttonInfo.bgColor_.value = renderContext->GetBackgroundColor().value().GetValue();
676         }
677 
678         auto bgProp = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
679         CHECK_NULL_RETURN(bgProp, false);
680         const auto& borderWidth = bgProp->GetBorderWidthProperty();
681         if (borderWidth != nullptr) {
682             if (borderWidth->leftDimen.has_value()) {
683                 buttonInfo.borderWidth_ = borderWidth->leftDimen.value().ConvertToVp();
684             }
685         }
686     }
687     return true;
688 }
689 
InitChildInfo(OHOS::Security::SecurityComponent::SecCompBase & buttonInfo,RefPtr<FrameNode> & node)690 bool SecurityComponentHandler::InitChildInfo(OHOS::Security::SecurityComponent::SecCompBase& buttonInfo,
691     RefPtr<FrameNode>& node)
692 {
693     RefPtr<FrameNode> iconNode = GetSecCompChildNode(node, V2::IMAGE_ETS_TAG);
694     if (!InitSCIconInfo(buttonInfo, iconNode)) {
695         return false;
696     }
697 
698     RefPtr<FrameNode> textNode = GetSecCompChildNode(node, V2::TEXT_ETS_TAG);
699     if (!InitSCTextInfo(buttonInfo, textNode)) {
700         return false;
701     }
702 
703     RefPtr<FrameNode> buttonNode = GetSecCompChildNode(node, V2::BUTTON_ETS_TAG);
704     if (!InitSCButtonInfo(buttonInfo, buttonNode)) {
705         return false;
706     }
707 
708     if (!InitBaseInfo(buttonInfo, node)) {
709         return false;
710     }
711     return true;
712 }
713 
WriteButtonInfo(const RefPtr<OHOS::Ace::NG::SecurityComponentLayoutProperty> & layoutProperty,RefPtr<FrameNode> & node,OHOS::Security::SecurityComponent::SecCompBase & buttonInfo)714 void SecurityComponentHandler::WriteButtonInfo(
715     const RefPtr<OHOS::Ace::NG::SecurityComponentLayoutProperty>& layoutProperty,
716     RefPtr<FrameNode>& node, OHOS::Security::SecurityComponent::SecCompBase& buttonInfo)
717 {
718     buttonInfo.parentEffect_ = CheckParentNodesEffect(node, buttonInfo);
719     buttonInfo.text_ = layoutProperty->GetSecurityComponentDescription().value();
720     buttonInfo.icon_ = layoutProperty->GetIconStyle().value();
721     buttonInfo.bg_ = static_cast<SecCompBackground>(
722         layoutProperty->GetBackgroundType().value());
723 }
724 
InitButtonInfo(std::string & componentInfo,RefPtr<FrameNode> & node,SecCompType & scType)725 bool SecurityComponentHandler::InitButtonInfo(std::string& componentInfo, RefPtr<FrameNode>& node, SecCompType& scType)
726 {
727     CHECK_NULL_RETURN(node, false);
728     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
729     CHECK_NULL_RETURN(layoutProperty, false);
730     std::string type = node->GetTag();
731     if (type == V2::LOCATION_BUTTON_ETS_TAG) {
732         LocationButton buttonInfo;
733         WriteButtonInfo(layoutProperty, node, buttonInfo);
734         buttonInfo.type_ = SecCompType::LOCATION_COMPONENT;
735         scType = SecCompType::LOCATION_COMPONENT;
736         if (!InitChildInfo(buttonInfo, node)) {
737             return false;
738         }
739         componentInfo = buttonInfo.ToJsonStr();
740     } else if (type == V2::PASTE_BUTTON_ETS_TAG) {
741         PasteButton buttonInfo;
742         WriteButtonInfo(layoutProperty, node, buttonInfo);
743         buttonInfo.type_ = SecCompType::PASTE_COMPONENT;
744         scType = SecCompType::PASTE_COMPONENT;
745         if (!InitChildInfo(buttonInfo, node)) {
746             return false;
747         }
748         componentInfo = buttonInfo.ToJsonStr();
749     } else if (type == V2::SAVE_BUTTON_ETS_TAG) {
750         SaveButton buttonInfo;
751         WriteButtonInfo(layoutProperty, node, buttonInfo);
752         buttonInfo.type_ = SecCompType::SAVE_COMPONENT;
753         scType = SecCompType::SAVE_COMPONENT;
754         if (!InitChildInfo(buttonInfo, node)) {
755             return false;
756         }
757         componentInfo = buttonInfo.ToJsonStr();
758     } else {
759         return false;
760     }
761     return true;
762 }
763 
RegisterSecurityComponent(RefPtr<FrameNode> & node,int32_t & scId)764 int32_t SecurityComponentHandler::RegisterSecurityComponent(RefPtr<FrameNode>& node, int32_t& scId)
765 {
766     SecurityComponentHandler::probe.InitProbeTask();
767     std::string componentInfo;
768     SecCompType type;
769     if (!InitButtonInfo(componentInfo, node, type)) {
770         return -1;
771     }
772     int32_t ret = SecCompKit::RegisterSecurityComponent(
773         type, componentInfo, scId);
774     return ret;
775 }
776 
UpdateSecurityComponent(RefPtr<FrameNode> & node,int32_t & scId)777 int32_t SecurityComponentHandler::UpdateSecurityComponent(RefPtr<FrameNode>& node, int32_t& scId)
778 {
779     std::string componentInfo;
780     SecCompType type;
781     if (!InitButtonInfo(componentInfo, node, type)) {
782         return -1;
783     }
784     int32_t ret = SecCompKit::UpdateSecurityComponent(scId, componentInfo);
785     return ret;
786 }
787 
UnregisterSecurityComponent(int32_t & scId)788 int32_t SecurityComponentHandler::UnregisterSecurityComponent(int32_t& scId)
789 {
790     if (scId == -1) {
791         return -1;
792     }
793     int32_t ret = SecCompKit::UnregisterSecurityComponent(scId);
794     return ret;
795 }
796 
IsContextTransparent(const RefPtr<FrameNode> & frameNode)797 bool SecurityComponentHandler::IsContextTransparent(const RefPtr<FrameNode>& frameNode)
798 {
799     const RefPtr<RenderContext> renderContext = frameNode->GetRenderContext();
800     CHECK_NULL_RETURN(renderContext, false);
801     auto layoutProperty = frameNode->GetLayoutProperty();
802     CHECK_NULL_RETURN(layoutProperty, false);
803     if (renderContext->GetOpacity().has_value() && renderContext->GetOpacity().value() == 0.0) {
804         return true;
805     }
806     if (static_cast<int32_t>(layoutProperty->GetVisibility().value_or(VisibleType::VISIBLE)) != 0) {
807         return true;
808     }
809     return false;
810 }
811 
CheckContainerTags(const RefPtr<FrameNode> & frameNode)812 bool SecurityComponentHandler::CheckContainerTags(const RefPtr<FrameNode>& frameNode)
813 {
814     static std::set<std::string> containerComponentTags = { "Flex", "Stack", "Row", "Column", "WindowScene", "root",
815         "Swiper", "Grid", "GridItem", "page", "stage", "FormComponent", "Tabs", "TabContent", "ColumnSplit",
816         "FolderStack", "GridCol", "GridRow", "RelativeContainer", "RowSplit", "List", "Scroll", "WaterFlow",
817         "SideBarContainer", "Refresh", "Navigator", "ListItemGroup", "ListItem", "Hyperlink", "FormLink", "FlowItem",
818         "Counter", "Custom", "overlay" };
819 
820     const RefPtr<RenderContext> renderContext = frameNode->GetRenderContext();
821     CHECK_NULL_RETURN(renderContext, false);
822     if (containerComponentTags.find(frameNode->GetTag()) != containerComponentTags.end() &&
823         renderContext->GetBackgroundColor()->ColorToString().compare("#00000000") == 0) {
824         return true;
825     }
826     return false;
827 }
828 
IsInModalPage(const RefPtr<UINode> & node)829 bool SecurityComponentHandler::IsInModalPage(const RefPtr<UINode>& node)
830 {
831     RefPtr<UINode> tmpNode = node;
832     while (tmpNode) {
833         if (tmpNode->GetTag() == V2::MODAL_PAGE_TAG) {
834             return true;
835         }
836         tmpNode = tmpNode->GetParent();
837     }
838     return false;
839 }
840 
CheckSecurityComponentStatus(const RefPtr<UINode> & root,std::unordered_map<int32_t,NG::RectF> & nodeId2Rect,int32_t secNodeId,std::unordered_map<int32_t,int32_t> & nodeId2Zindex)841 bool SecurityComponentHandler::CheckSecurityComponentStatus(const RefPtr<UINode>& root,
842     std::unordered_map<int32_t, NG::RectF>& nodeId2Rect, int32_t secNodeId,
843     std::unordered_map<int32_t, int32_t>& nodeId2Zindex)
844 {
845     bool res = false;
846     RectF paintRect;
847 
848     auto frameNode = AceType::DynamicCast<NG::FrameNode>(root);
849     if (frameNode) {
850         paintRect = frameNode->GetTransformRectRelativeToWindow();
851         if (IsSecurityComponent(frameNode) && (frameNode->GetId() == secNodeId)) {
852             if (IsInModalPage(root)) {
853                 return false;
854             }
855             return CheckRectIntersect(paintRect, secNodeId, nodeId2Rect, nodeId2Zindex);
856         }
857     }
858     auto& children = root->GetChildren();
859     for (auto child = children.rbegin(); child != children.rend(); ++child) {
860         auto node = AceType::DynamicCast<NG::FrameNode>(*child);
861         if (node && (IsContextTransparent(node) || !node->IsActive())) {
862             continue;
863         }
864         res |= CheckSecurityComponentStatus(*child, nodeId2Rect, secNodeId, nodeId2Zindex);
865     }
866 
867     if (frameNode && frameNode->GetTag() != V2::SHEET_WRAPPER_TAG && !CheckContainerTags(frameNode)) {
868         nodeId2Rect[frameNode->GetId()] = paintRect;
869     }
870     return res;
871 }
872 
CheckRectIntersect(const RectF & dest,int32_t secNodeId,const std::unordered_map<int32_t,NG::RectF> & nodeId2Rect,std::unordered_map<int32_t,int32_t> & nodeId2Zindex)873 bool SecurityComponentHandler::CheckRectIntersect(const RectF& dest, int32_t secNodeId,
874     const std::unordered_map<int32_t, NG::RectF>& nodeId2Rect,
875     std::unordered_map<int32_t, int32_t>& nodeId2Zindex)
876 {
877     for (const auto& originRect : nodeId2Rect) {
878         if (originRect.second.IsInnerIntersectWithRound(dest) &&
879             (nodeId2Zindex[secNodeId] <= nodeId2Zindex[originRect.first]) &&
880             (!NearEqual(originRect.second.Width(), 0.0) && !NearEqual(originRect.second.Height(), 0.0))) {
881             SC_LOG_ERROR("SecurityComponentCheckFail: Security component id = %{public}d " \
882                 "is covered by id = %{public}d.", secNodeId, originRect.first);
883             return true;
884         }
885     }
886     return false;
887 }
888 
IsSecurityComponent(RefPtr<FrameNode> & node)889 bool SecurityComponentHandler::IsSecurityComponent(RefPtr<FrameNode>& node)
890 {
891     return node->GetTag() == V2::LOCATION_BUTTON_ETS_TAG || node->GetTag() == V2::PASTE_BUTTON_ETS_TAG ||
892            node->GetTag() == V2::SAVE_BUTTON_ETS_TAG;
893 }
894 
GetNodeZIndex(const RefPtr<UINode> & root)895 int32_t SecurityComponentHandler::GetNodeZIndex(const RefPtr<UINode>& root)
896 {
897     int32_t zIndex;
898     auto node = AceType::DynamicCast<NG::FrameNode>(root);
899     if (node) {
900         const RefPtr<RenderContext> renderContext = node->GetRenderContext();
901         if (!renderContext) {
902             zIndex = 0;
903         } else {
904             zIndex = renderContext->GetZIndexValue(ZINDEX_DEFAULT_VALUE);
905         }
906     } else {
907         zIndex = 0;
908     }
909 
910     return zIndex;
911 }
912 
UpdateAllZindex(const RefPtr<UINode> & root,std::unordered_map<int32_t,int32_t> & nodeId2Zindex)913 void SecurityComponentHandler::UpdateAllZindex(const RefPtr<UINode>& root,
914     std::unordered_map<int32_t, int32_t>& nodeId2Zindex)
915 {
916     if (nodeId2Zindex.count(root->GetId()) == 0) {
917         nodeId2Zindex[root->GetId()] = GetNodeZIndex(root);
918     }
919     auto& children = root->GetChildren();
920     for (auto child = children.begin(); child != children.end(); ++child) {
921         int32_t nodeZIndex = GetNodeZIndex(*child);
922         nodeId2Zindex[(*child)->GetId()] = std::max(nodeZIndex, nodeId2Zindex[root->GetId()]);
923         UpdateAllZindex(*child, nodeId2Zindex);
924     }
925 }
926 
CheckComponentCoveredStatus(int32_t secNodeId)927 bool SecurityComponentHandler::CheckComponentCoveredStatus(int32_t secNodeId)
928 {
929     auto pipeline = PipelineContext::GetCurrentContextSafely();
930     CHECK_NULL_RETURN(pipeline, false);
931     RefPtr<UINode> root = pipeline->GetRootElement();
932     CHECK_NULL_RETURN(root, false);
933     std::unordered_map<int32_t, NG::RectF> nodeId2Rect;
934     std::unordered_map<int32_t, int32_t> nodeId2Zindex;
935     UpdateAllZindex(root, nodeId2Zindex);
936     if (CheckSecurityComponentStatus(root, nodeId2Rect, secNodeId, nodeId2Zindex)) {
937         return true;
938     }
939     return false;
940 }
941 
ReportSecurityComponentClickEventInner(int32_t & scId,RefPtr<FrameNode> & node,SecCompClickEvent & event,Security::SecurityComponent::OnFirstUseDialogCloseFunc && callback)942 int32_t SecurityComponentHandler::ReportSecurityComponentClickEventInner(int32_t& scId,
943     RefPtr<FrameNode>& node, SecCompClickEvent& event,
944     Security::SecurityComponent::OnFirstUseDialogCloseFunc&& callback)
945 {
946     std::string componentInfo;
947     SecCompType type;
948     if (!InitButtonInfo(componentInfo, node, type)) {
949         return -1;
950     }
951     auto container = AceType::DynamicCast<Platform::AceContainer>(Container::Current());
952     CHECK_NULL_RETURN(container, -1);
953     sptr<IRemoteObject> token = container->GetToken();
954     if (container->GetParentToken() != nullptr) {
955         token = container->GetParentToken();
956     }
957     return SecCompKit::ReportSecurityComponentClickEvent(scId,
958         componentInfo, event, token, std::move(callback));
959 }
960 
ReportSecurityComponentClickEvent(int32_t & scId,RefPtr<FrameNode> & node,GestureEvent & event,Security::SecurityComponent::OnFirstUseDialogCloseFunc && callback)961 int32_t SecurityComponentHandler::ReportSecurityComponentClickEvent(int32_t& scId,
962     RefPtr<FrameNode>& node, GestureEvent& event,
963     Security::SecurityComponent::OnFirstUseDialogCloseFunc&& callback)
964 {
965     SecCompClickEvent secEvent;
966     secEvent.type = ClickEventType::POINT_EVENT_TYPE;
967 #ifdef SECURITY_COMPONENT_ENABLE
968     secEvent.point.touchX = event.GetDisplayX();
969     secEvent.point.touchY = event.GetDisplayY();
970     auto pointerEvent = event.GetPointerEvent();
971     uint8_t defaultData = 0;
972     std::vector<uint8_t> dataBuffer;
973     if (pointerEvent == nullptr) {
974         SC_LOG_WARN("SecurityComponentClickEventWarning: Receive a NULL pointerEvent, set default data.");
975         secEvent.extraInfo.data = &defaultData;
976         secEvent.extraInfo.dataSize = 1;
977         secEvent.point.timestamp = 0;
978     } else {
979         dataBuffer = pointerEvent->GetEnhanceData();
980         if (dataBuffer.size() > 0) {
981             secEvent.extraInfo.data = dataBuffer.data();
982             secEvent.extraInfo.dataSize = dataBuffer.size();
983         }
984         std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
985         TimeStamp time(microseconds);
986         secEvent.point.timestamp =
987             static_cast<uint64_t>(time.time_since_epoch().count()) / SECOND_TO_MILLISECOND;
988     }
989 #endif
990     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
991     if (layoutProperty && layoutProperty->GetIsTextLimitExceeded().has_value() &&
992         layoutProperty->GetIsTextLimitExceeded().value()) {
993         SC_LOG_ERROR("SecurityComponentCheckFail: The text of the security component is out of range.");
994         return -1;
995     }
996     if (CheckComponentCoveredStatus(node->GetId())) {
997         SC_LOG_ERROR("SecurityComponentCheckFail: Security component is covered by another component.");
998         return -1;
999     }
1000 
1001     return ReportSecurityComponentClickEventInner(scId, node, secEvent, std::move(callback));
1002 }
1003 
ReportSecurityComponentClickEvent(int32_t & scId,RefPtr<FrameNode> & node,const KeyEvent & event,Security::SecurityComponent::OnFirstUseDialogCloseFunc && callback)1004 int32_t SecurityComponentHandler::ReportSecurityComponentClickEvent(int32_t& scId,
1005     RefPtr<FrameNode>& node, const KeyEvent& event,
1006     Security::SecurityComponent::OnFirstUseDialogCloseFunc&& callback)
1007 {
1008     SecCompClickEvent secEvent;
1009     secEvent.type = ClickEventType::KEY_EVENT_TYPE;
1010 
1011     secEvent.key.timestamp =
1012         static_cast<uint64_t>(event.timeStamp.time_since_epoch().count()) / SECOND_TO_MILLISECOND;
1013     secEvent.key.keyCode = static_cast<int32_t>(event.code);
1014     auto data = event.enhanceData;
1015     if (data.size() > 0) {
1016         secEvent.extraInfo.data = data.data();
1017         secEvent.extraInfo.dataSize = data.size();
1018     }
1019     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
1020     if (layoutProperty && layoutProperty->GetIsTextLimitExceeded().has_value() &&
1021         layoutProperty->GetIsTextLimitExceeded().value()) {
1022         SC_LOG_ERROR("SecurityComponentCheckFail: The text of the security component is out of range.");
1023         return -1;
1024     }
1025     if (CheckComponentCoveredStatus(node->GetId())) {
1026         SC_LOG_ERROR("SecurityComponentCheckFail: Security component is covered by another component.");
1027         return -1;
1028     }
1029     return ReportSecurityComponentClickEventInner(scId, node, secEvent, std::move(callback));
1030 }
1031 
IsSecurityComponentServiceExist()1032 bool SecurityComponentHandler::IsSecurityComponentServiceExist()
1033 {
1034     return SecCompKit::IsServiceExist();
1035 }
1036 
LoadSecurityComponentService()1037 bool SecurityComponentHandler::LoadSecurityComponentService()
1038 {
1039     return SecCompKit::LoadService();
1040 }
1041 
IsSystemAppCalling()1042 bool SecurityComponentHandler::IsSystemAppCalling()
1043 {
1044     return SecCompKit::IsSystemAppCalling();
1045 }
1046 } // namespace OHOS::Ace::NG
1047