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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SECURITY_COMPONENT_SECURITY_COMPONENT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SECURITY_COMPONENT_SECURITY_COMPONENT_PATTERN_H
18
19 #include "core/components_ng/base/view_stack_processor.h"
20 #include "core/components_ng/pattern/image/image_layout_property.h"
21 #include "core/components_ng/pattern/security_component/security_component_accessibility_property.h"
22 #include "core/components_ng/pattern/security_component/security_component_layout_algorithm.h"
23 #include "core/components_ng/pattern/security_component/security_component_layout_property.h"
24 #include "core/components_ng/pattern/security_component/security_component_paint_property.h"
25 #include "core/components_ng/pattern/pattern.h"
26
27 namespace OHOS::Ace::NG {
28 constexpr int32_t DEFAULT_SECURITY_COMPONENT_CLICK_DISTANCE = 15;
29 constexpr uint64_t MAX_REGISTER_WAITING_TIME = 3000; // 3000ms
30 constexpr int32_t MAX_RETRY_TIMES = 3;
31 constexpr int64_t REGISTER_RETRY_INTERVAL = 30; // 30ms
32
GetSecCompChildNode(RefPtr<FrameNode> & parent,const std::string & tag)33 static inline RefPtr<FrameNode> GetSecCompChildNode(RefPtr<FrameNode>& parent, const std::string& tag)
34 {
35 for (const auto& child : parent->GetChildren()) {
36 auto node = AceType::DynamicCast<FrameNode, UINode>(child);
37 CHECK_NULL_RETURN(node, nullptr);
38 if (node->GetTag() == tag) {
39 return node;
40 }
41 }
42 return nullptr;
43 }
44
GetCurSecCompChildNode(const std::string & tag)45 static inline RefPtr<FrameNode> GetCurSecCompChildNode(const std::string& tag)
46 {
47 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
48 CHECK_NULL_RETURN(frameNode, nullptr);
49 return GetSecCompChildNode(frameNode, tag);
50 }
51
52 class SecurityComponentPattern : public Pattern {
53 DECLARE_ACE_TYPE(SecurityComponentPattern, Pattern);
54
55 public:
SecurityComponentPattern()56 SecurityComponentPattern() {};
57 ~SecurityComponentPattern() override = default;
58
CreateLayoutProperty()59 RefPtr<LayoutProperty> CreateLayoutProperty() override
60 {
61 return MakeRefPtr<SecurityComponentLayoutProperty>();
62 }
63
CreatePaintProperty()64 RefPtr<PaintProperty> CreatePaintProperty() override
65 {
66 return MakeRefPtr<SecurityComponentPaintProperty>();
67 }
68
CreateLayoutAlgorithm()69 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
70 {
71 return MakeRefPtr<SecurityComponentLayoutAlgorithm>();
72 }
73
CreateAccessibilityProperty()74 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
75 {
76 return MakeRefPtr<SecurityComponentAccessibilityProperty>();
77 }
78
79 FocusPattern GetFocusPattern() const override;
80
81 void OnWindowHide() override;
82 void OnWindowShow() override;
83
84 SecurityComponentRegisterStatus regStatus_ = SecurityComponentRegisterStatus::UNREGISTERED;
85 std::timed_mutex regMutex_;
86 int32_t scId_ = -1;
87 bool isAppear_ = true;
88
89 protected:
90 void InitOnTouch(RefPtr<FrameNode>& secCompNode);
91 void InitOnKeyEvent(RefPtr<FrameNode>& secCompNode);
92 bool OnKeyEvent(const KeyEvent& event);
93 void OnTouch(const TouchEventInfo& info);
94 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
95 void OnModifyDone() override;
96 void SetNodeHitTestMode(RefPtr<FrameNode>& node, HitTestMode mode);
97 void InitOnClick(RefPtr<FrameNode>& secCompNode, RefPtr<FrameNode>& icon,
98 RefPtr<FrameNode>& text, RefPtr<FrameNode>& button);
99 void InitAppearCallback(RefPtr<FrameNode>& frameNode);
100 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
101 void ToJsonValueRect(std::unique_ptr<JsonValue>& json) const;
102 bool IsParentMenu(RefPtr<FrameNode>& secCompNode);
103 private:
104 void HandleClickEventFromTouch(const TouchEventInfo& info);
105 void UpdateIconProperty(RefPtr<FrameNode>& scNode, RefPtr<FrameNode>& iconNode);
106 void UpdateTextProperty(RefPtr<FrameNode>& scNode, RefPtr<FrameNode>& textNode);
107 void UpdateButtonProperty(RefPtr<FrameNode>& scNode, RefPtr<FrameNode>& buttonNode);
108 #ifdef SECURITY_COMPONENT_ENABLE
109 void RegisterSecurityComponentAsync();
110 void UnregisterSecurityComponent();
111 int32_t ReportSecurityComponentClickEvent(GestureEvent& event);
112 int32_t ReportSecurityComponentClickEvent(const KeyEvent& event);
113 #endif
114 std::unique_ptr<Offset> lastTouchOffset_;
115 RefPtr<ClickEvent> clickListener_;
116 RefPtr<TouchEventImpl> onTouchListener_;
117 bool isSetOnKeyEvent = false;
118 bool isAppearCallback_ = false;
119 ACE_DISALLOW_COPY_AND_MOVE(SecurityComponentPattern);
120 };
121 } // namespace OHOS::Ace::NG
122
123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SECURITY_COMPONENT_SECURITY_COMPONENT_PATTERN_H
124