Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 1 | // Copyright 2019 The Chromium 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 FUCHSIA_ENGINE_BROWSER_ACCESSIBILITY_BRIDGE_H_ |
| 6 | #define FUCHSIA_ENGINE_BROWSER_ACCESSIBILITY_BRIDGE_H_ |
| 7 | |
| 8 | #include <fuchsia/accessibility/semantics/cpp/fidl.h> |
| 9 | #include <fuchsia/math/cpp/fidl.h> |
| 10 | #include <fuchsia/ui/views/cpp/fidl.h> |
| 11 | #include <lib/fidl/cpp/binding.h> |
| 12 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 13 | #include "base/callback.h" |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 14 | #include "base/macros.h" |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 15 | #include "content/public/browser/ax_event_notification_details.h" |
| 16 | #include "content/public/browser/web_contents_delegate.h" |
| 17 | #include "content/public/browser/web_contents_observer.h" |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 18 | #include "fuchsia/engine/web_engine_export.h" |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 19 | #include "ui/accessibility/ax_serializable_tree.h" |
| 20 | #include "ui/accessibility/ax_tree_id.h" |
| 21 | #include "ui/accessibility/ax_tree_observer.h" |
| 22 | |
| 23 | namespace content { |
| 24 | class WebContents; |
| 25 | } // namespace content |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 26 | |
| 27 | // This class is the intermediate for accessibility between Chrome and Fuchsia. |
| 28 | // It handles registration to the Fuchsia Semantics Manager, translating events |
| 29 | // and data structures between the two services, and forwarding actions and |
| 30 | // events. |
| 31 | // The lifetime of an instance of AccessibilityBridge is the same as that of a |
| 32 | // View created by FrameImpl. This class refers to the View via the |
| 33 | // caller-supplied ViewRef. |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 34 | // If |semantic_tree_| gets disconnected, it will cause the FrameImpl that owns |
Sharon Yang | 7919fab | 2020-07-28 16:46:20 | [diff] [blame] | 35 | // |this| to close, which will also destroy |this|. |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 36 | class WEB_ENGINE_EXPORT AccessibilityBridge |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 37 | : public content::WebContentsObserver, |
| 38 | public fuchsia::accessibility::semantics::SemanticListener, |
| 39 | public ui::AXTreeObserver { |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 40 | public: |
Wez | 7a5f5ca | 2020-08-17 07:41:08 | [diff] [blame] | 41 | // |semantics_manager| is used during construction to register the instance. |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 42 | // |web_contents| is required to exist for the duration of |this|. |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 43 | AccessibilityBridge( |
Wez | 7a5f5ca | 2020-08-17 07:41:08 | [diff] [blame] | 44 | fuchsia::accessibility::semantics::SemanticsManager* semantics_manager, |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 45 | fuchsia::ui::views::ViewRef view_ref, |
Sharon Yang | 7919fab | 2020-07-28 16:46:20 | [diff] [blame] | 46 | content::WebContents* web_contents, |
Sharon Yang | 023ea72 | 2020-08-05 23:28:11 | [diff] [blame] | 47 | base::OnceCallback<void(zx_status_t)> on_error_callback); |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 48 | ~AccessibilityBridge() final; |
| 49 | |
Sharon Yang | 7919fab | 2020-07-28 16:46:20 | [diff] [blame] | 50 | AccessibilityBridge(const AccessibilityBridge&) = delete; |
| 51 | AccessibilityBridge& operator=(const AccessibilityBridge&) = delete; |
| 52 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 53 | const ui::AXSerializableTree* ax_tree_for_test() { return &ax_tree_; } |
| 54 | |
| 55 | void set_event_received_callback_for_test(base::OnceClosure callback) { |
| 56 | event_received_callback_for_test_ = std::move(callback); |
| 57 | } |
| 58 | |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 59 | private: |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 60 | FRIEND_TEST_ALL_PREFIXES(AccessibilityBridgeTest, OnSemanticsModeChanged); |
| 61 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 62 | // A struct used for caching semantic information. This allows for updates |
| 63 | // and deletes to be stored in the same vector to preserve all ordering |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 64 | // information. |
| 65 | struct SemanticUpdateOrDelete { |
| 66 | enum Type { UPDATE, DELETE }; |
| 67 | |
| 68 | SemanticUpdateOrDelete(SemanticUpdateOrDelete&& m); |
| 69 | SemanticUpdateOrDelete(Type type, |
| 70 | fuchsia::accessibility::semantics::Node node, |
| 71 | uint32_t id_to_delete); |
| 72 | ~SemanticUpdateOrDelete() = default; |
| 73 | |
| 74 | Type type; |
| 75 | fuchsia::accessibility::semantics::Node update_node; |
| 76 | uint32_t id_to_delete; |
| 77 | }; |
| 78 | |
| 79 | // Processes pending data and commits it to the Semantic Tree. |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 80 | void TryCommit(); |
| 81 | |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 82 | // Helper function for TryCommit() that sends the contents of |to_send_| to |
| 83 | // the Semantic Tree, starting at |start|. |
| 84 | void DispatchSemanticsMessages(size_t start, size_t size); |
| 85 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 86 | // Callback for SemanticTree::CommitUpdates. |
| 87 | void OnCommitComplete(); |
| 88 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 89 | // Deletes all nodes in subtree rooted at and including |node|, unless |
| 90 | // |node| is the root of the tree. |tree| and |node| are owned by the |
| 91 | // accessibility bridge. |
| 92 | void DeleteSubtree(ui::AXNode* node); |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 93 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 94 | // content::WebContentsObserver implementation. |
| 95 | void AccessibilityEventReceived( |
| 96 | const content::AXEventNotificationDetails& details) override; |
| 97 | |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 98 | // fuchsia::accessibility::semantics::SemanticListener implementation. |
| 99 | void OnAccessibilityActionRequested( |
| 100 | uint32_t node_id, |
| 101 | fuchsia::accessibility::semantics::Action action, |
| 102 | OnAccessibilityActionRequestedCallback callback) final; |
| 103 | void HitTest(fuchsia::math::PointF local_point, |
| 104 | HitTestCallback callback) final; |
| 105 | void OnSemanticsModeChanged(bool updates_enabled, |
| 106 | OnSemanticsModeChangedCallback callback) final; |
| 107 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 108 | // ui::AXTreeObserver implementation. |
| 109 | void OnNodeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override; |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 110 | void OnSubtreeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 111 | void OnAtomicUpdateFinished( |
| 112 | ui::AXTree* tree, |
| 113 | bool root_changed, |
| 114 | const std::vector<ui::AXTreeObserver::Change>& changes) override; |
| 115 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 116 | fuchsia::accessibility::semantics::SemanticTreePtr semantic_tree_; |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 117 | fidl::Binding<fuchsia::accessibility::semantics::SemanticListener> binding_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 118 | content::WebContents* web_contents_; |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 119 | ui::AXSerializableTree ax_tree_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 120 | |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 121 | // Cache for pending data to be sent to the Semantic Tree between commits. |
| 122 | std::vector<SemanticUpdateOrDelete> to_send_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 123 | bool commit_inflight_ = false; |
| 124 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 125 | // Maintain a map of callbacks as multiple hit test events can happen at |
| 126 | // once. These are keyed by the request_id field of ui::AXActionData. |
Sharon Yang | 1ab5b4c | 2019-11-21 01:23:27 | [diff] [blame] | 127 | base::flat_map<int, HitTestCallback> pending_hit_test_callbacks_; |
| 128 | |
Sharon Yang | 023ea72 | 2020-08-05 23:28:11 | [diff] [blame] | 129 | // Run in the case of an internal error that cannot be recovered from. This |
| 130 | // will cause the frame |this| is owned by to be torn down. |
| 131 | base::OnceCallback<void(zx_status_t)> on_error_callback_; |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 132 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 133 | // The root id of |ax_tree_|. |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 134 | int32_t root_id_ = 0; |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 135 | |
| 136 | base::OnceClosure event_received_callback_for_test_; |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | #endif // FUCHSIA_ENGINE_BROWSER_ACCESSIBILITY_BRIDGE_H_ |