|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#ifndef FLUTTER_SHELL_PLATFORM_COMMON_ACCESSIBILITY_BRIDGE_H_ |
| 6 | +#define FLUTTER_SHELL_PLATFORM_COMMON_ACCESSIBILITY_BRIDGE_H_ |
| 7 | + |
| 8 | +#include <unordered_map> |
| 9 | + |
| 10 | +#include "flutter/fml/mapping.h" |
| 11 | +#include "flutter/shell/platform/embedder/embedder.h" |
| 12 | + |
| 13 | +#include "flutter/third_party/accessibility/ax/ax_event_generator.h" |
| 14 | +#include "flutter/third_party/accessibility/ax/ax_tree.h" |
| 15 | +#include "flutter/third_party/accessibility/ax/ax_tree_observer.h" |
| 16 | +#include "flutter/third_party/accessibility/ax/platform/ax_platform_node_delegate.h" |
| 17 | + |
| 18 | +#include "flutter_platform_node_delegate.h" |
| 19 | + |
| 20 | +namespace flutter { |
| 21 | + |
| 22 | +//------------------------------------------------------------------------------ |
| 23 | +/// Use this class to maintain an accessibility tree. This class consumes |
| 24 | +/// semantics updates from the embedder API and produces an accessibility tree |
| 25 | +/// in the native format. |
| 26 | +/// |
| 27 | +/// The bridge creates an AXTree to hold the semantics data that comes from |
| 28 | +/// Flutter semantics updates. The tree holds AXNode[s] which contain the |
| 29 | +/// semantics information for semantics node. The AXTree resemble the Flutter |
| 30 | +/// semantics tree in the Flutter framework. The bridge also uses |
| 31 | +/// FlutterPlatformNodeDelegate to wrap each AXNode in order to provide |
| 32 | +/// an accessibility tree in the native format. |
| 33 | +/// |
| 34 | +/// This class takes in a AccessibilityBridgeDelegate instance and is in charge |
| 35 | +/// of its lifecycle. The delegate are used to handle the accessibility events |
| 36 | +/// and actions. |
| 37 | +/// |
| 38 | +/// To use this class, you must provide your own implementation of |
| 39 | +/// FlutterPlatformNodeDelegate and AccessibilityBridgeDelegate. |
| 40 | +class AccessibilityBridge |
| 41 | + : public std::enable_shared_from_this<AccessibilityBridge>, |
| 42 | + public FlutterPlatformNodeDelegate::OwnerBridge, |
| 43 | + private ui::AXTreeObserver { |
| 44 | + public: |
| 45 | + //----------------------------------------------------------------------------- |
| 46 | + /// Delegate to handle requests from the accessibility bridge. The requests |
| 47 | + /// include sending accessibility event to native accessibility system, |
| 48 | + /// routing accessibility action to the Flutter framework, and creating |
| 49 | + /// platform specific FlutterPlatformNodeDelegate. |
| 50 | + /// |
| 51 | + /// The accessibility events are generated when accessibility tree changes. |
| 52 | + /// These events must be sent to the native accessibility system through |
| 53 | + /// the native API for the system to pick up the changes |
| 54 | + /// (e.g. NSAccessibilityPostNotification in MacOS). |
| 55 | + /// |
| 56 | + /// The accessibility actions are generated by the native accessibility system |
| 57 | + /// when users interacted with the assistive technologies. Those actions |
| 58 | + /// needed to be sent to the Flutter framework. |
| 59 | + /// |
| 60 | + /// Each platform needs to implement the FlutterPlatformNodeDelegate and |
| 61 | + /// returns its platform specific instance of FlutterPlatformNodeDelegate |
| 62 | + /// in this delegate. |
| 63 | + class AccessibilityBridgeDelegate { |
| 64 | + public: |
| 65 | + virtual ~AccessibilityBridgeDelegate() = default; |
| 66 | + //--------------------------------------------------------------------------- |
| 67 | + /// @brief Handle accessibility events generated due to accessibility |
| 68 | + /// tree changes. These events are generated in accessibility |
| 69 | + /// bridge and needed to be sent to native accessibility system. |
| 70 | + /// See ui::AXEventGenerator::Event for possible events. |
| 71 | + /// |
| 72 | + /// @param[in] targeted_event The object that contains both the |
| 73 | + /// generated event and the event target. |
| 74 | + virtual void OnAccessibilityEvent( |
| 75 | + ui::AXEventGenerator::TargetedEvent targeted_event) = 0; |
| 76 | + |
| 77 | + //--------------------------------------------------------------------------- |
| 78 | + /// @brief Dispatch accessibility action back to the Flutter framework. |
| 79 | + /// These actions are generated in the native accessibility |
| 80 | + /// system when users interact with the assistive technologies. |
| 81 | + /// For example, a |
| 82 | + /// FlutterSemanticsAction::kFlutterSemanticsActionTap is |
| 83 | + /// fired when user click or touch the screen. |
| 84 | + /// |
| 85 | + /// @param[in] target The semantics node id of the action |
| 86 | + /// target. |
| 87 | + /// @param[in] action The generated flutter semantics action. |
| 88 | + /// @param[in] data Additional data associated with the |
| 89 | + /// action. |
| 90 | + virtual void DispatchAccessibilityAction(AccessibilityNodeId target, |
| 91 | + FlutterSemanticsAction action, |
| 92 | + fml::MallocMapping data) = 0; |
| 93 | + |
| 94 | + //--------------------------------------------------------------------------- |
| 95 | + /// @brief Creates a platform specific FlutterPlatformNodeDelegate. |
| 96 | + /// Ownership passes to the caller. This method will be called |
| 97 | + /// by accessibility bridge whenever a new AXNode is created in |
| 98 | + /// AXTree. Each platform needs to implement this method in |
| 99 | + /// order to inject its subclass into the accessibility bridge. |
| 100 | + virtual std::shared_ptr<FlutterPlatformNodeDelegate> |
| 101 | + CreateFlutterPlatformNodeDelegate() = 0; |
| 102 | + }; |
| 103 | + |
| 104 | + //----------------------------------------------------------------------------- |
| 105 | + /// @brief Creates a new instance of a accessibility bridge. |
| 106 | + /// |
| 107 | + /// @param[in] user_data A custom pointer to the data of your |
| 108 | + /// choice. This pointer can be retrieve later |
| 109 | + /// through GetUserData(). |
| 110 | + explicit AccessibilityBridge( |
| 111 | + std::unique_ptr<AccessibilityBridgeDelegate> delegate); |
| 112 | + ~AccessibilityBridge(); |
| 113 | + |
| 114 | + //----------------------------------------------------------------------------- |
| 115 | + /// @brief The ID of the root node in the accessibility tree. In Flutter, |
| 116 | + // this is always 0. |
| 117 | + static constexpr int32_t kRootNodeId = 0; |
| 118 | + |
| 119 | + //------------------------------------------------------------------------------ |
| 120 | + /// @brief Adds a semantics node update to the pending semantics update. |
| 121 | + /// Calling this method alone will NOT update the semantics tree. |
| 122 | + /// To flush the pending updates, call the CommitUpdates(). |
| 123 | + /// |
| 124 | + /// @param[in] node A pointer to the semantics node update. |
| 125 | + void AddFlutterSemanticsNodeUpdate(const FlutterSemanticsNode* node); |
| 126 | + |
| 127 | + //------------------------------------------------------------------------------ |
| 128 | + /// @brief Adds a custom semantics action update to the pending semantics |
| 129 | + /// update. Calling this method alone will NOT update the |
| 130 | + /// semantics tree. To flush the pending updates, call the |
| 131 | + /// CommitUpdates(). |
| 132 | + /// |
| 133 | + /// @param[in] action A pointer to the custom semantics action |
| 134 | + /// update. |
| 135 | + void AddFlutterSemanticsCustomActionUpdate( |
| 136 | + const FlutterSemanticsCustomAction* action); |
| 137 | + |
| 138 | + //------------------------------------------------------------------------------ |
| 139 | + /// @brief Flushes the pending updates and applies them to this |
| 140 | + /// accessibility bridge. Calling this with no pending updates |
| 141 | + /// does nothing, and callers should call this method at the end |
| 142 | + /// of an atomic batch to avoid leaving the tree in a unstable |
| 143 | + /// state. For example if a node reparents from A to B, callers |
| 144 | + /// should only call this method when both removal from A and |
| 145 | + /// addition to B are in the pending updates. |
| 146 | + void CommitUpdates(); |
| 147 | + |
| 148 | + //------------------------------------------------------------------------------ |
| 149 | + /// @brief Get the flutter platform node delegate with the given id from |
| 150 | + /// this accessibility bridge. Returns expired weak_ptr if the |
| 151 | + /// delegate associated with the id does not exist or has been |
| 152 | + /// removed from the accessibility tree. |
| 153 | + /// |
| 154 | + /// @param[in] id The id of the flutter accessibility node you want |
| 155 | + /// to retrieve. |
| 156 | + std::weak_ptr<FlutterPlatformNodeDelegate> |
| 157 | + GetFlutterPlatformNodeDelegateFromID(AccessibilityNodeId id) const; |
| 158 | + |
| 159 | + //------------------------------------------------------------------------------ |
| 160 | + /// @brief Get the ax tree data from this accessibility bridge. The tree |
| 161 | + /// data contains information such as the id of the node that |
| 162 | + /// has the keyboard focus or the text selection range. |
| 163 | + const ui::AXTreeData& GetAXTreeData() const; |
| 164 | + |
| 165 | + //------------------------------------------------------------------------------ |
| 166 | + /// @brief Gets all pending accessibility events generated during |
| 167 | + /// semantics updates. This is useful when deciding how to handle |
| 168 | + /// events in AccessibilityBridgeDelegate::OnAccessibilityEvent in |
| 169 | + /// case one may decide to handle an event differently based on |
| 170 | + /// all pending events. |
| 171 | + const std::vector<ui::AXEventGenerator::TargetedEvent> GetPendingEvents(); |
| 172 | + |
| 173 | + //------------------------------------------------------------------------------ |
| 174 | + /// @brief Update the AccessibilityBridgeDelegate stored in the |
| 175 | + /// accessibility bridge to a new one. |
| 176 | + void UpdateDelegate(std::unique_ptr<AccessibilityBridgeDelegate> delegate); |
| 177 | + |
| 178 | + private: |
| 179 | + // See FlutterSemanticsNode in embedder.h |
| 180 | + typedef struct { |
| 181 | + int32_t id; |
| 182 | + FlutterSemanticsFlag flags; |
| 183 | + FlutterSemanticsAction actions; |
| 184 | + int32_t text_selection_base; |
| 185 | + int32_t text_selection_extent; |
| 186 | + int32_t scroll_child_count; |
| 187 | + int32_t scroll_index; |
| 188 | + double scroll_position; |
| 189 | + double scroll_extent_max; |
| 190 | + double scroll_extent_min; |
| 191 | + double elevation; |
| 192 | + double thickness; |
| 193 | + std::string label; |
| 194 | + std::string hint; |
| 195 | + std::string value; |
| 196 | + std::string increased_value; |
| 197 | + std::string decreased_value; |
| 198 | + FlutterTextDirection text_direction; |
| 199 | + FlutterRect rect; |
| 200 | + FlutterTransformation transform; |
| 201 | + std::vector<int32_t> children_in_traversal_order; |
| 202 | + std::vector<int32_t> custom_accessibility_actions; |
| 203 | + } SemanticsNode; |
| 204 | + |
| 205 | + // See FlutterSemanticsCustomAction in embedder.h |
| 206 | + typedef struct { |
| 207 | + int32_t id; |
| 208 | + FlutterSemanticsAction override_action; |
| 209 | + std::string label; |
| 210 | + std::string hint; |
| 211 | + } SemanticsCustomAction; |
| 212 | + |
| 213 | + std::unordered_map<AccessibilityNodeId, |
| 214 | + std::shared_ptr<FlutterPlatformNodeDelegate>> |
| 215 | + id_wrapper_map_; |
| 216 | + ui::AXTree tree_; |
| 217 | + ui::AXEventGenerator event_generator_; |
| 218 | + std::unordered_map<int32_t, SemanticsNode> pending_semantics_node_updates_; |
| 219 | + std::unordered_map<int32_t, SemanticsCustomAction> |
| 220 | + pending_semantics_custom_action_updates_; |
| 221 | + AccessibilityNodeId last_focused_id_ = ui::AXNode::kInvalidAXID; |
| 222 | + std::unique_ptr<AccessibilityBridgeDelegate> delegate_; |
| 223 | + |
| 224 | + void InitAXTree(const ui::AXTreeUpdate& initial_state); |
| 225 | + void GetSubTreeList(SemanticsNode target, std::vector<SemanticsNode>& result); |
| 226 | + void ConvertFluterUpdate(const SemanticsNode& node, |
| 227 | + ui::AXTreeUpdate& tree_update); |
| 228 | + void SetRoleFromFlutterUpdate(ui::AXNodeData& node_data, |
| 229 | + const SemanticsNode& node); |
| 230 | + void SetStateFromFlutterUpdate(ui::AXNodeData& node_data, |
| 231 | + const SemanticsNode& node); |
| 232 | + void SetActionsFromFlutterUpdate(ui::AXNodeData& node_data, |
| 233 | + const SemanticsNode& node); |
| 234 | + void SetBooleanAttributesFromFlutterUpdate(ui::AXNodeData& node_data, |
| 235 | + const SemanticsNode& node); |
| 236 | + void SetIntAttributesFromFlutterUpdate(ui::AXNodeData& node_data, |
| 237 | + const SemanticsNode& node); |
| 238 | + void SetIntListAttributesFromFlutterUpdate(ui::AXNodeData& node_data, |
| 239 | + const SemanticsNode& node); |
| 240 | + void SetStringListAttributesFromFlutterUpdate(ui::AXNodeData& node_data, |
| 241 | + const SemanticsNode& node); |
| 242 | + void SetNameFromFlutterUpdate(ui::AXNodeData& node_data, |
| 243 | + const SemanticsNode& node); |
| 244 | + void SetValueFromFlutterUpdate(ui::AXNodeData& node_data, |
| 245 | + const SemanticsNode& node); |
| 246 | + void SetTreeData(const SemanticsNode& node, ui::AXTreeUpdate& tree_update); |
| 247 | + SemanticsNode FromFlutterSemanticsNode( |
| 248 | + const FlutterSemanticsNode* flutter_node); |
| 249 | + SemanticsCustomAction FromFlutterSemanticsCustomAction( |
| 250 | + const FlutterSemanticsCustomAction* flutter_custom_action); |
| 251 | + |
| 252 | + // |AXTreeObserver| |
| 253 | + void OnNodeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override; |
| 254 | + |
| 255 | + // |AXTreeObserver| |
| 256 | + void OnSubtreeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override; |
| 257 | + |
| 258 | + // |AXTreeObserver| |
| 259 | + void OnNodeCreated(ui::AXTree* tree, ui::AXNode* node) override; |
| 260 | + |
| 261 | + // |AXTreeObserver| |
| 262 | + void OnNodeDeleted(ui::AXTree* tree, AccessibilityNodeId node_id) override; |
| 263 | + |
| 264 | + // |AXTreeObserver| |
| 265 | + void OnNodeReparented(ui::AXTree* tree, ui::AXNode* node) override; |
| 266 | + |
| 267 | + // |AXTreeObserver| |
| 268 | + void OnRoleChanged(ui::AXTree* tree, |
| 269 | + ui::AXNode* node, |
| 270 | + ax::mojom::Role old_role, |
| 271 | + ax::mojom::Role new_role) override; |
| 272 | + |
| 273 | + // |AXTreeObserver| |
| 274 | + void OnAtomicUpdateFinished( |
| 275 | + ui::AXTree* tree, |
| 276 | + bool root_changed, |
| 277 | + const std::vector<ui::AXTreeObserver::Change>& changes) override; |
| 278 | + |
| 279 | + // |FlutterPlatformNodeDelegate::OwnerBridge| |
| 280 | + void SetLastFocusedId(AccessibilityNodeId node_id) override; |
| 281 | + |
| 282 | + // |FlutterPlatformNodeDelegate::OwnerBridge| |
| 283 | + AccessibilityNodeId GetLastFocusedId() override; |
| 284 | + |
| 285 | + // |FlutterPlatformNodeDelegate::OwnerBridge| |
| 286 | + gfx::NativeViewAccessible GetNativeAccessibleFromId( |
| 287 | + AccessibilityNodeId id) override; |
| 288 | + |
| 289 | + // |FlutterPlatformNodeDelegate::OwnerBridge| |
| 290 | + void DispatchAccessibilityAction(AccessibilityNodeId target, |
| 291 | + FlutterSemanticsAction action, |
| 292 | + fml::MallocMapping data) override; |
| 293 | + |
| 294 | + // |FlutterPlatformNodeDelegate::OwnerBridge| |
| 295 | + gfx::RectF RelativeToGlobalBounds(const ui::AXNode* node, |
| 296 | + bool& offscreen, |
| 297 | + bool clip_bounds) override; |
| 298 | + |
| 299 | + BASE_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridge); |
| 300 | +}; |
| 301 | + |
| 302 | +} // namespace flutter |
| 303 | + |
| 304 | +#endif // FLUTTER_SHELL_PLATFORM_COMMON_ACCESSIBILITY_BRIDGE_H_ |
0 commit comments