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