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