blob: 936d2baf5cf0c37c97d814767c27d335a5266c49 [file] [log] [blame]
Sharon Yang23a8135b2019-09-10 21:10:541// 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 Yangfbb9ba4a2019-11-18 23:59:5613#include "base/callback.h"
Sharon Yang23a8135b2019-09-10 21:10:5414#include "base/macros.h"
Sharon Yangfbb9ba4a2019-11-18 23:59:5615#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 Yang23a8135b2019-09-10 21:10:5418#include "fuchsia/engine/web_engine_export.h"
Sharon Yangfbb9ba4a2019-11-18 23:59:5619#include "ui/accessibility/ax_serializable_tree.h"
20#include "ui/accessibility/ax_tree_id.h"
21#include "ui/accessibility/ax_tree_observer.h"
22
23namespace content {
24class WebContents;
25} // namespace content
Sharon Yang23a8135b2019-09-10 21:10:5426
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.
34class WEB_ENGINE_EXPORT AccessibilityBridge
Sharon Yangfbb9ba4a2019-11-18 23:59:5635 : public content::WebContentsObserver,
36 public fuchsia::accessibility::semantics::SemanticListener,
37 public ui::AXTreeObserver {
Sharon Yang23a8135b2019-09-10 21:10:5438 public:
Sharon Yangfbb9ba4a2019-11-18 23:59:5639 // |web_contents| is required to exist for the duration of |this|.
Sharon Yang23a8135b2019-09-10 21:10:5440 AccessibilityBridge(
41 fuchsia::accessibility::semantics::SemanticsManagerPtr semantics_manager,
Sharon Yangfbb9ba4a2019-11-18 23:59:5642 fuchsia::ui::views::ViewRef view_ref,
43 content::WebContents* web_contents);
Sharon Yang23a8135b2019-09-10 21:10:5444 ~AccessibilityBridge() final;
45
Sharon Yangfbb9ba4a2019-11-18 23:59:5646 void set_semantic_tree_for_test(
47 fidl::InterfaceRequest<fuchsia::accessibility::semantics::SemanticTree>
48 tree_request);
49
Sharon Yang23a8135b2019-09-10 21:10:5450 private:
Sharon Yangfbb9ba4a2019-11-18 23:59:5651 FRIEND_TEST_ALL_PREFIXES(AccessibilityBridgeTest, OnSemanticsModeChanged);
52
Sharon Yangca616422019-12-18 23:52:3953 // 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 Yangfbb9ba4a2019-11-18 23:59:5671 void TryCommit();
72
Sharon Yangca616422019-12-18 23:52:3973 // 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 Yangfbb9ba4a2019-11-18 23:59:5677 // 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 Yangca616422019-12-18 23:52:3984 // 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 Yangfbb9ba4a2019-11-18 23:59:5689 // content::WebContentsObserver implementation.
90 void AccessibilityEventReceived(
91 const content::AXEventNotificationDetails& details) override;
92
Sharon Yang23a8135b2019-09-10 21:10:5493 // 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 Yangfbb9ba4a2019-11-18 23:59:56103 // ui::AXTreeObserver implementation.
104 void OnNodeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override;
Sharon Yangca616422019-12-18 23:52:39105 void OnSubtreeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override;
Sharon Yangfbb9ba4a2019-11-18 23:59:56106 void OnAtomicUpdateFinished(
107 ui::AXTree* tree,
108 bool root_changed,
109 const std::vector<ui::AXTreeObserver::Change>& changes) override;
110
Sharon Yang23a8135b2019-09-10 21:10:54111 fuchsia::accessibility::semantics::SemanticTreePtr tree_ptr_;
112 fidl::Binding<fuchsia::accessibility::semantics::SemanticListener> binding_;
Sharon Yangfbb9ba4a2019-11-18 23:59:56113 content::WebContents* web_contents_;
114 ui::AXSerializableTree tree_;
115
Sharon Yangca616422019-12-18 23:52:39116 // Cache for pending data to be sent to the Semantic Tree between commits.
117 std::vector<SemanticUpdateOrDelete> to_send_;
Sharon Yangfbb9ba4a2019-11-18 23:59:56118 bool commit_inflight_ = false;
119
Sharon Yang1ab5b4c2019-11-21 01:23:27120 // 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 Yangfbb9ba4a2019-11-18 23:59:56124 // The root id of |tree_|.
125 int32_t root_id_ = 0;
Sharon Yang23a8135b2019-09-10 21:10:54126
127 DISALLOW_COPY_AND_ASSIGN(AccessibilityBridge);
128};
129
130#endif // FUCHSIA_ENGINE_BROWSER_ACCESSIBILITY_BRIDGE_H_