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_FORCE_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; UpdateVirtualNodeFocus()108 void UpdateVirtualNodeFocus() override {} 109 int64_t GenerateNextAccessibilityId() override; 110 RefPtr<AccessibilityNode> CreateSpecializedNode( 111 const std::string& tag, int32_t nodeId, int32_t parentNodeId) override; 112 RefPtr<AccessibilityNode> CreateAccessibilityNode( 113 const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex) override; 114 RefPtr<AccessibilityNode> GetAccessibilityNodeById(NodeId nodeId) const override; 115 std::string GetInspectorNodeById(NodeId nodeId) const override; 116 void RemoveAccessibilityNodes(RefPtr<AccessibilityNode>& node) override; 117 void RemoveAccessibilityNodeById(NodeId nodeId) override; 118 void ClearPageAccessibilityNodes(int32_t pageId) override; 119 SetRootNodeId(int32_t nodeId)120 void SetRootNodeId(int32_t nodeId) override 121 { 122 rootNodeId_ = nodeId; 123 } 124 125 void TrySaveTargetAndIdNode( 126 const std::string& id, const std::string& target, const RefPtr<AccessibilityNode>& node) override; HandleComponentPostBinding()127 void HandleComponentPostBinding() override {} 128 void OnDumpInfo(const std::vector<std::string>& params) override; OnDumpInfoNG(const std::vector<std::string> & params,uint32_t windowId)129 void OnDumpInfoNG(const std::vector<std::string>& params, uint32_t windowId) override {} 130 std::unique_ptr<JsonValue> DumpComposedElementsToJson() const; 131 std::unique_ptr<JsonValue> DumpComposedElementToJson(NodeId nodeId); 132 void SetCardViewParams(const std::string& key, bool focus) override; 133 void SetCardViewPosition(int id, float offsetX, float offsetY) override; 134 SetSupportAction(uint32_t action,bool isEnable)135 void SetSupportAction(uint32_t action, bool isEnable) override {} 136 137 void UpdateEventTarget(NodeId id, BaseEventInfo& info) override; 138 139 void SetWindowPos(int32_t left, int32_t top, int32_t windowId) override; GetWindowLeft(int32_t windowId)140 int32_t GetWindowLeft(int32_t windowId) 141 { 142 auto windowPos = windowPosMap_.find(windowId); 143 if (windowPos != windowPosMap_.end()) { 144 return windowPos->second.left; 145 } 146 return windowPosMap_.begin()->second.left; 147 } GetWindowTop(int32_t windowId)148 int32_t GetWindowTop(int32_t windowId) 149 { 150 auto windowPos = windowPosMap_.find(windowId); 151 if (windowPos != windowPosMap_.end()) { 152 return windowPos->second.top; 153 } 154 return windowPosMap_.begin()->second.top; 155 } 156 157 bool IsDeclarative(); 158 159 protected: 160 virtual void DumpHandleEvent(const std::vector<std::string>& params); 161 virtual void DumpProperty(const std::vector<std::string>& params); 162 virtual void DumpTree(int32_t depth, int64_t nodeID, bool isDumpSimplify = false); 163 164 static bool GetDefaultAttrsByType(const std::string& type, std::unique_ptr<JsonValue>& jsonDefaultAttrs); 165 mutable std::mutex mutex_; 166 std::unordered_map<NodeId, RefPtr<AccessibilityNode>> accessibilityNodes_; 167 std::unordered_map<std::string, WeakPtr<AccessibilityNode>> nodeWithIdMap_; 168 std::unordered_map<std::string, WeakPtr<AccessibilityNode>> nodeWithTargetMap_; 169 std::unordered_map<std::string, WeakPtr<ComposedElement>> composedElementIdMap_; 170 std::unordered_map<NodeId, std::list<VisibleCallbackInfo>> visibleChangeNodes_; 171 WeakPtr<PipelineBase> context_; 172 std::list<WeakPtr<PipelineBase>> subContexts_; 173 WeakPtr<JsAcePage> indexPage_; 174 int32_t rootNodeId_ = -1; 175 Offset cardOffset_; 176 int32_t cardId_ = 0; 177 bool isOhosHostCard_ = false; 178 std::map<int32_t, WindowPos> windowPosMap_; 179 180 static const size_t EVENT_DUMP_PARAM_LENGTH_UPPER; 181 static const size_t EVENT_DUMP_PARAM_LENGTH_LOWER; 182 static const size_t PROPERTY_DUMP_PARAM_LENGTH; 183 static const int32_t EVENT_DUMP_ORDER_INDEX; 184 static const int32_t EVENT_DUMP_ID_INDEX; 185 static const int32_t EVENT_DUMP_ACTION_INDEX; 186 static const int32_t EVENT_DUMP_ACTION_PARAM_INDEX; 187 188 private: 189 RefPtr<AccessibilityNode> CreateCommonAccessibilityNode( 190 const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex); 191 RefPtr<AccessibilityNode> CreateDeclarativeAccessibilityNode( 192 const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex); 193 RefPtr<AccessibilityNode> GetRootAccessibilityNode(); 194 // decor nodes are created before load page(SetRootNodeId) IsDecor()195 bool IsDecor() 196 { 197 return rootNodeId_ == -1; 198 } 199 }; 200 201 } // namespace OHOS::Ace::Framework 202 203 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_ACCESSIBILITY_ACCESSIBILITY_NODE_MANAGER_H 204