1 /* 2 * Copyright (c) 2021 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_context.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 class ACE_EXPORT AccessibilityNodeManager : public AccessibilityManager { 38 DECLARE_ACE_TYPE(AccessibilityNodeManager, AccessibilityManager); 39 40 public: 41 static RefPtr<AccessibilityNodeManager> Create(); 42 43 AccessibilityNodeManager() = default; 44 ~AccessibilityNodeManager() override; 45 46 // AccessibilityNodeManager functions. 47 virtual void InitializeCallback(); 48 void SetPipelineContext(const RefPtr<PipelineContext>& context); 49 void SetRunningPage(const RefPtr<JsAcePage>& page); 50 std::string GetNodeChildIds(const RefPtr<AccessibilityNode>& node); 51 void AddNodeWithId(const std::string& key, const RefPtr<AccessibilityNode>& node); 52 void AddNodeWithTarget(const std::string& key, const RefPtr<AccessibilityNode>& node); 53 RefPtr<AccessibilityNode> GetAccessibilityNodeFromPage(NodeId nodeId) const; 54 void ClearNodeRectInfo(RefPtr<AccessibilityNode>& node, bool isPopDialog) override; 55 void AddComposedElement(const std::string& key, const RefPtr<ComposedElement>& node) override; 56 void RemoveComposedElementById(const std::string& key) override; 57 WeakPtr<ComposedElement> GetComposedElementFromPage(NodeId nodeId) override; 58 void TriggerVisibleChangeEvent() override; 59 void AddVisibleChangeNode(NodeId nodeId, double ratio, VisibleRatioCallback callback) override; 60 void RemoveVisibleChangeNode(NodeId nodeId) override; IsVisibleChangeNodeExists(NodeId index)61 bool IsVisibleChangeNodeExists(NodeId index) override 62 { 63 if (index == -1) { 64 return !visibleChangeNodes_.empty(); 65 } 66 return visibleChangeNodes_.find(index) != visibleChangeNodes_.end(); 67 } 68 GetRootNodeId()69 int32_t GetRootNodeId() const 70 { 71 return rootNodeId_; 72 } 73 GetCardOffset()74 const Offset& GetCardOffset() 75 { 76 return cardOffset_; 77 } 78 GetCardId()79 int32_t GetCardId() const 80 { 81 return cardId_; 82 } 83 isOhosHostCard()84 bool isOhosHostCard() const 85 { 86 return isOhosHostCard_; 87 } 88 GetPipelineContext()89 WeakPtr<PipelineContext> GetPipelineContext() 90 { 91 return context_; 92 } 93 94 // AccessibilityNodeManager overrides functions. 95 void SendAccessibilityAsyncEvent(const AccessibilityEvent& accessibilityEvent) override; 96 int32_t GenerateNextAccessibilityId() override; 97 RefPtr<AccessibilityNode> CreateSpecializedNode( 98 const std::string& tag, int32_t nodeId, int32_t parentNodeId) override; 99 RefPtr<AccessibilityNode> CreateAccessibilityNode( 100 const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex) override; 101 RefPtr<AccessibilityNode> GetAccessibilityNodeById(NodeId nodeId) const override; 102 std::string GetInspectorNodeById(NodeId nodeId) const override; 103 void RemoveAccessibilityNodes(RefPtr<AccessibilityNode>& node) override; 104 void RemoveAccessibilityNodeById(NodeId nodeId) override; 105 void ClearPageAccessibilityNodes(int32_t pageId) override; 106 SetRootNodeId(int32_t nodeId)107 void SetRootNodeId(int32_t nodeId) override 108 { 109 rootNodeId_ = nodeId; 110 } 111 112 void TrySaveTargetAndIdNode( 113 const std::string& id, const std::string& target, const RefPtr<AccessibilityNode>& node) override; HandleComponentPostBinding()114 void HandleComponentPostBinding() override {} 115 void DumpHandleEvent(const std::vector<std::string>& params) override; 116 void DumpProperty(const std::vector<std::string>& params) override; 117 void DumpTree(int32_t depth, NodeId nodeID) override; 118 void DumpTree(int32_t depth, NodeId nodeID, std::vector<std::string>& infos) override; 119 std::unique_ptr<JsonValue> DumpComposedElementsToJson() const; 120 std::unique_ptr<JsonValue> DumpComposedElementToJson(NodeId nodeId); 121 void SetCardViewParams(const std::string& key, bool focus) override; 122 void SetCardViewPosition(int id, float offsetX, float offsetY) override; 123 SetSupportAction(uint32_t action,bool isEnable)124 void SetSupportAction(uint32_t action, bool isEnable) override {} 125 126 void UpdateEventTarget(NodeId id, BaseEventInfo& info) override; 127 128 bool IsDeclarative(); 129 130 protected: 131 static bool GetDefaultAttrsByType(const std::string& type, std::unique_ptr<JsonValue>& jsonDefaultAttrs); 132 mutable std::mutex mutex_; 133 std::unordered_map<NodeId, RefPtr<AccessibilityNode>> accessibilityNodes_; 134 std::unordered_map<std::string, WeakPtr<AccessibilityNode>> nodeWithIdMap_; 135 std::unordered_map<std::string, WeakPtr<AccessibilityNode>> nodeWithTargetMap_; 136 std::unordered_map<std::string, WeakPtr<ComposedElement>> composedElementIdMap_; 137 std::unordered_map<NodeId, std::list<VisibleCallbackInfo>> visibleChangeNodes_; 138 WeakPtr<PipelineContext> context_; 139 WeakPtr<JsAcePage> indexPage_; 140 int32_t rootNodeId_ = -1; 141 Offset cardOffset_; 142 int32_t cardId_ = 0; 143 bool isOhosHostCard_ = false; 144 145 private: 146 RefPtr<AccessibilityNode> CreateCommonAccessibilityNode( 147 const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex); 148 RefPtr<AccessibilityNode> CreateDeclarativeAccessibilityNode( 149 const std::string& tag, int32_t nodeId, int32_t parentNodeId, int32_t itemIndex); 150 }; 151 152 } // namespace OHOS::Ace::Framework 153 154 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_ACCESSIBILITY_ACCESSIBILITY_NODE_MANAGER_H 155