• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_BRIDGE_COMMON_ACCESSIBILITY_ACCESSIBILITY_NODE_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_ACCESSIBILITY_ACCESSIBILITY_NODE_MANAGER_H
18 
19 #include <unordered_map>
20 #include <unordered_set>
21 #include <vector>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/utils/macros.h"
25 #include "core/pipeline/base/composed_element.h"
26 #include "core/pipeline/pipeline_base.h"
27 #include "frameworks/bridge/js_frontend/js_ace_page.h"
28 
29 namespace OHOS::Ace::Framework {
30 
31 struct VisibleCallbackInfo {
32     VisibleRatioCallback callback;
33     double visibleRatio = 1.0;
34     bool currentVisibleType = false;
35 };
36 
37 struct WindowPos {
38     int32_t left = 0;
39     int32_t top = 0;
40 };
41 
42 class ACE_EXPORT AccessibilityNodeManager : public AccessibilityManager {
43     DECLARE_ACE_TYPE(AccessibilityNodeManager, AccessibilityManager);
44 
45 public:
46     static RefPtr<AccessibilityNodeManager> Create();
47 
48     AccessibilityNodeManager() = default;
49     ~AccessibilityNodeManager() override;
50 
51     // AccessibilityNodeManager functions.
52     virtual void InitializeCallback();
53     virtual void RegisterSubWindowInteractionOperation(int windowId);
54     virtual void SetPipelineContext(const RefPtr<PipelineBase>& context);
55     void AddSubPipelineContext(const RefPtr<PipelineBase>& context);
56     void SetRunningPage(const RefPtr<JsAcePage>& page);
57     std::string GetNodeChildIds(const RefPtr<AccessibilityNode>& node);
58     void AddNodeWithId(const std::string& key, const RefPtr<AccessibilityNode>& node);
59     void AddNodeWithTarget(const std::string& key, const RefPtr<AccessibilityNode>& node);
60     RefPtr<AccessibilityNode> GetAccessibilityNodeFromPage(NodeId nodeId) const;
61     void ClearNodeRectInfo(RefPtr<AccessibilityNode>& node, bool isPopDialog) override;
62     void AddComposedElement(const std::string& key, const RefPtr<ComposedElement>& node) override;
63     void RemoveComposedElementById(const std::string& key) override;
64     WeakPtr<ComposedElement> GetComposedElementFromPage(NodeId nodeId) override;
65     void TriggerVisibleChangeEvent() override;
66     void AddVisibleChangeNode(NodeId nodeId, double ratio, VisibleRatioCallback callback) override;
67     void RemoveVisibleChangeNode(NodeId nodeId) override;
IsVisibleChangeNodeExists(NodeId index)68     bool IsVisibleChangeNodeExists(NodeId index) override
69     {
70         if (index == -1) {
71             return !visibleChangeNodes_.empty();
72         }
73         return visibleChangeNodes_.find(index) != visibleChangeNodes_.end();
74     }
75 
GetRootNodeId()76     int32_t GetRootNodeId() const
77     {
78         return rootNodeId_;
79     }
80 
GetCardOffset()81     const Offset& GetCardOffset()
82     {
83         return cardOffset_;
84     }
85 
GetCardId()86     int32_t GetCardId() const
87     {
88         return cardId_;
89     }
90 
isOhosHostCard()91     bool isOhosHostCard() const
92     {
93         return isOhosHostCard_;
94     }
95 
GetPipelineContext()96     WeakPtr<PipelineBase> GetPipelineContext()
97     {
98         return context_;
99     }
100 
GetSubPipelineContexts()101     std::list<WeakPtr<PipelineBase>>& GetSubPipelineContexts()
102     {
103         return subContexts_;
104     }
105 
106     // AccessibilityNodeManager overrides functions.
107     void SendAccessibilityAsyncEvent(const AccessibilityEvent& accessibilityEvent) override;
108     int64_t GenerateNextAccessibilityId() override;
109     RefPtr<AccessibilityNode> CreateSpecializedNode(
110         const std::string& tag, int32_t nodeId, int32_t parentNodeId) override;
111     RefPtr<AccessibilityNode> CreateAccessibilityNode(
112         const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex) override;
113     RefPtr<AccessibilityNode> GetAccessibilityNodeById(NodeId nodeId) const override;
114     std::string GetInspectorNodeById(NodeId nodeId) const override;
115     void RemoveAccessibilityNodes(RefPtr<AccessibilityNode>& node) override;
116     void RemoveAccessibilityNodeById(NodeId nodeId) override;
117     void ClearPageAccessibilityNodes(int32_t pageId) override;
118 
SetRootNodeId(int32_t nodeId)119     void SetRootNodeId(int32_t nodeId) override
120     {
121         rootNodeId_ = nodeId;
122     }
123 
124     void TrySaveTargetAndIdNode(
125         const std::string& id, const std::string& target, const RefPtr<AccessibilityNode>& node) override;
HandleComponentPostBinding()126     void HandleComponentPostBinding() override {}
127     void OnDumpInfo(const std::vector<std::string>& params) override;
128     std::unique_ptr<JsonValue> DumpComposedElementsToJson() const;
129     std::unique_ptr<JsonValue> DumpComposedElementToJson(NodeId nodeId);
130     void SetCardViewParams(const std::string& key, bool focus) override;
131     void SetCardViewPosition(int id, float offsetX, float offsetY) override;
132 
SetSupportAction(uint32_t action,bool isEnable)133     void SetSupportAction(uint32_t action, bool isEnable) override {}
134 
135     void UpdateEventTarget(NodeId id, BaseEventInfo& info) override;
136 
137     void SetWindowPos(int32_t left, int32_t top, int32_t windowId) override;
GetWindowLeft(int32_t windowId)138     int32_t GetWindowLeft(int32_t windowId)
139     {
140         auto windowPos = windowPosMap_.find(windowId);
141         if (windowPos != windowPosMap_.end()) {
142             return windowPos->second.left;
143         }
144         return windowPosMap_.begin()->second.left;
145     }
GetWindowTop(int32_t windowId)146     int32_t GetWindowTop(int32_t windowId)
147     {
148         auto windowPos = windowPosMap_.find(windowId);
149         if (windowPos != windowPosMap_.end()) {
150             return windowPos->second.top;
151         }
152         return windowPosMap_.begin()->second.top;
153     }
154 
155     bool IsDeclarative();
156 
157 protected:
158     virtual void DumpHandleEvent(const std::vector<std::string>& params);
159     virtual void DumpProperty(const std::vector<std::string>& params);
160     virtual void DumpTree(int32_t depth, int64_t nodeID);
161 
162     static bool GetDefaultAttrsByType(const std::string& type, std::unique_ptr<JsonValue>& jsonDefaultAttrs);
163     mutable std::mutex mutex_;
164     std::unordered_map<NodeId, RefPtr<AccessibilityNode>> accessibilityNodes_;
165     std::unordered_map<std::string, WeakPtr<AccessibilityNode>> nodeWithIdMap_;
166     std::unordered_map<std::string, WeakPtr<AccessibilityNode>> nodeWithTargetMap_;
167     std::unordered_map<std::string, WeakPtr<ComposedElement>> composedElementIdMap_;
168     std::unordered_map<NodeId, std::list<VisibleCallbackInfo>> visibleChangeNodes_;
169     WeakPtr<PipelineBase> context_;
170     std::list<WeakPtr<PipelineBase>> subContexts_;
171     WeakPtr<JsAcePage> indexPage_;
172     int32_t rootNodeId_ = -1;
173     Offset cardOffset_;
174     int32_t cardId_ = 0;
175     bool isOhosHostCard_ = false;
176     std::map<int32_t, WindowPos> windowPosMap_;
177 
178     static const size_t EVENT_DUMP_PARAM_LENGTH_UPPER;
179     static const size_t EVENT_DUMP_PARAM_LENGTH_LOWER;
180     static const size_t PROPERTY_DUMP_PARAM_LENGTH;
181     static const int32_t EVENT_DUMP_ORDER_INDEX;
182     static const int32_t EVENT_DUMP_ID_INDEX;
183     static const int32_t EVENT_DUMP_ACTION_INDEX;
184     static const int32_t EVENT_DUMP_ACTION_PARAM_INDEX;
185 
186 private:
187     RefPtr<AccessibilityNode> CreateCommonAccessibilityNode(
188         const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex);
189     RefPtr<AccessibilityNode> CreateDeclarativeAccessibilityNode(
190         const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex);
191     RefPtr<AccessibilityNode> GetRootAccessibilityNode();
192     // decor nodes are created before load page(SetRootNodeId)
IsDecor()193     bool IsDecor()
194     {
195         return rootNodeId_ == -1;
196     }
197 };
198 
199 } // namespace OHOS::Ace::Framework
200 
201 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_ACCESSIBILITY_ACCESSIBILITY_NODE_MANAGER_H
202