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: |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 41 | // |web_contents| is required to exist for the duration of |this|. |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 42 | AccessibilityBridge( |
| 43 | fuchsia::accessibility::semantics::SemanticsManagerPtr semantics_manager, |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 44 | fuchsia::ui::views::ViewRef view_ref, |
Sharon Yang | 7919fab | 2020-07-28 16:46:20 | [diff] [blame] | 45 | content::WebContents* web_contents, |
Sharon Yang | 023ea72 | 2020-08-05 23:28:11 | [diff] [blame] | 46 | base::OnceCallback<void(zx_status_t)> on_error_callback); |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 47 | ~AccessibilityBridge() final; |
| 48 | |
Sharon Yang | 7919fab | 2020-07-28 16:46:20 | [diff] [blame] | 49 | AccessibilityBridge(const AccessibilityBridge&) = delete; |
| 50 | AccessibilityBridge& operator=(const AccessibilityBridge&) = delete; |
| 51 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 52 | const ui::AXSerializableTree* ax_tree_for_test() { return &ax_tree_; } |
| 53 | |
| 54 | void set_event_received_callback_for_test(base::OnceClosure callback) { |
| 55 | event_received_callback_for_test_ = std::move(callback); |
| 56 | } |
| 57 | |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 58 | private: |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 59 | FRIEND_TEST_ALL_PREFIXES(AccessibilityBridgeTest, OnSemanticsModeChanged); |
| 60 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 61 | // A struct used for caching semantic information. This allows for updates |
| 62 | // and deletes to be stored in the same vector to preserve all ordering |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 63 | // information. |
| 64 | struct SemanticUpdateOrDelete { |
| 65 | enum Type { UPDATE, DELETE }; |
| 66 | |
| 67 | SemanticUpdateOrDelete(SemanticUpdateOrDelete&& m); |
| 68 | SemanticUpdateOrDelete(Type type, |
| 69 | fuchsia::accessibility::semantics::Node node, |
| 70 | uint32_t id_to_delete); |
| 71 | ~SemanticUpdateOrDelete() = default; |
| 72 | |
| 73 | Type type; |
| 74 | fuchsia::accessibility::semantics::Node update_node; |
| 75 | uint32_t id_to_delete; |
| 76 | }; |
| 77 | |
| 78 | // Processes pending data and commits it to the Semantic Tree. |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 79 | void TryCommit(); |
| 80 | |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 81 | // Helper function for TryCommit() that sends the contents of |to_send_| to |
| 82 | // the Semantic Tree, starting at |start|. |
| 83 | void DispatchSemanticsMessages(size_t start, size_t size); |
| 84 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 85 | // Callback for SemanticTree::CommitUpdates. |
| 86 | void OnCommitComplete(); |
| 87 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 88 | // Converts AXNode ids to Semantic Node ids, and handles special casing of |
| 89 | // the root. |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 90 | uint32_t ConvertToFuchsiaNodeId(int32_t ax_node_id); |
| 91 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 92 | // Deletes all nodes in subtree rooted at and including |node|, unless |
| 93 | // |node| is the root of the tree. |tree| and |node| are owned by the |
| 94 | // accessibility bridge. |
| 95 | void DeleteSubtree(ui::AXNode* node); |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 96 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 97 | // content::WebContentsObserver implementation. |
| 98 | void AccessibilityEventReceived( |
| 99 | const content::AXEventNotificationDetails& details) override; |
| 100 | |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 101 | // fuchsia::accessibility::semantics::SemanticListener implementation. |
| 102 | void OnAccessibilityActionRequested( |
| 103 | uint32_t node_id, |
| 104 | fuchsia::accessibility::semantics::Action action, |
| 105 | OnAccessibilityActionRequestedCallback callback) final; |
| 106 | void HitTest(fuchsia::math::PointF local_point, |
| 107 | HitTestCallback callback) final; |
| 108 | void OnSemanticsModeChanged(bool updates_enabled, |
| 109 | OnSemanticsModeChangedCallback callback) final; |
| 110 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 111 | // ui::AXTreeObserver implementation. |
| 112 | void OnNodeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override; |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 113 | void OnSubtreeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 114 | void OnAtomicUpdateFinished( |
| 115 | ui::AXTree* tree, |
| 116 | bool root_changed, |
| 117 | const std::vector<ui::AXTreeObserver::Change>& changes) override; |
| 118 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 119 | fuchsia::accessibility::semantics::SemanticTreePtr semantic_tree_; |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 120 | fidl::Binding<fuchsia::accessibility::semantics::SemanticListener> binding_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 121 | content::WebContents* web_contents_; |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 122 | ui::AXSerializableTree ax_tree_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 123 | |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 124 | // Cache for pending data to be sent to the Semantic Tree between commits. |
| 125 | std::vector<SemanticUpdateOrDelete> to_send_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 126 | bool commit_inflight_ = false; |
| 127 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 128 | // Maintain a map of callbacks as multiple hit test events can happen at |
| 129 | // once. These are keyed by the request_id field of ui::AXActionData. |
Sharon Yang | 1ab5b4c | 2019-11-21 01:23:27 | [diff] [blame] | 130 | base::flat_map<int, HitTestCallback> pending_hit_test_callbacks_; |
| 131 | |
Sharon Yang | 023ea72 | 2020-08-05 23:28:11 | [diff] [blame] | 132 | // Run in the case of an internal error that cannot be recovered from. This |
| 133 | // will cause the frame |this| is owned by to be torn down. |
| 134 | base::OnceCallback<void(zx_status_t)> on_error_callback_; |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 135 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 136 | // The root id of |ax_tree_|. |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 137 | int32_t root_id_ = 0; |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 138 | |
| 139 | base::OnceClosure event_received_callback_for_test_; |
Sharon Yang | 23a8135b | 2019-09-10 21:10:54 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #endif // FUCHSIA_ENGINE_BROWSER_ACCESSIBILITY_BRIDGE_H_ |