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