1 // Copyright (c) 2020 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 CONTENT_RENDERER_ACCESSIBILITY_RENDER_ACCESSIBILITY_MANAGER_H_
6 #define CONTENT_RENDERER_ACCESSIBILITY_RENDER_ACCESSIBILITY_MANAGER_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "content/common/content_export.h"
12 #include "content/common/render_accessibility.mojom.h"
13 #include "mojo/public/cpp/bindings/associated_receiver.h"
14 #include "mojo/public/cpp/bindings/associated_remote.h"
15 #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
16 #include "ui/accessibility/ax_action_data.h"
17 #include "ui/accessibility/ax_enums.mojom-forward.h"
18 #include "ui/accessibility/ax_event.h"
19 #include "ui/accessibility/ax_mode.h"
20 #include "ui/accessibility/ax_tree_update.h"
21 
22 namespace content {
23 
24 class RenderFrameImpl;
25 class RenderAccessibilityImpl;
26 
27 // This class implements the RenderAccessibility mojo interface that the browser
28 // process uses to communicate with the renderer for any accessibility-related
29 // operations such as enabling/disabling accessibility support in the renderer,
30 // requesting snapshots of the accessibility tree or performing actions.
31 //
32 // Instances of this class will be owned by one RenderFrameImpl object, which
33 // will keep it alive for its entire lifetime. In addition, this class will own
34 // an instance of the RenderAccessibilityImpl class, which will only be alive
35 // while accessibility support in the renderer is enabled (i.e whenever the
36 // ui::AXMode set from the browser includes the |kWebContents| mode).
37 //
38 // Thus, when renderer accessibility is enabled, |render_accessibility_| will
39 // point to a valid RenderAccessibilityImpl object which will be used to enable
40 // bi-directional communication between the RenderFrameHostImpl object in the
41 // browser process and Blink.
42 class CONTENT_EXPORT RenderAccessibilityManager
43     : public mojom::RenderAccessibility {
44  public:
45   RenderAccessibilityManager(RenderFrameImpl* const render_frame);
46   ~RenderAccessibilityManager() override;
47 
48   // Binds the |receiver| to process mojo messages. This method is expected to
49   // be called only while |receiver_| is in an unbound state.
50   void BindReceiver(
51       mojo::PendingAssociatedReceiver<mojom::RenderAccessibility> receiver);
52 
53   // Returns a pointer to the RenderAccessibilityImpl object owned by this
54   // class. Can return nullptr if accessibility is not enabled in the renderer.
55   RenderAccessibilityImpl* GetRenderAccessibilityImpl();
56 
57   // Returns the current accessibility mode for the associated RenderFrameImpl.
58   ui::AXMode GetAccessibilityMode() const;
59 
60   // mojom::RenderAccessibility implementation.
61   void SetMode(uint32_t ax_mode) override;
62   void FatalError() override;
63   void HitTest(const gfx::Point& point,
64                ax::mojom::Event event_to_fire,
65                int request_id,
66                mojom::RenderAccessibility::HitTestCallback callback) override;
67   void PerformAction(const ui::AXActionData& data) override;
68   void Reset(int32_t reset_token) override;
69 
70   // Communication with the browser process.
71   void HandleAccessibilityEvents(
72       const std::vector<ui::AXTreeUpdate>& updates,
73       const std::vector<ui::AXEvent>& events,
74       int32_t reset_token,
75       mojom::RenderAccessibilityHost::HandleAXEventsCallback callback);
76   void HandleLocationChanges(std::vector<mojom::LocationChangesPtr> changes);
77 
78  private:
79   // Returns the associated remote used to send messages to the browser process,
80   // lazily initializing it the first time it's used.
81   mojo::AssociatedRemote<mojom::RenderAccessibilityHost>&
82   GetOrCreateRemoteRenderAccessibilityHost();
83 
84   // The RenderFrameImpl that owns us.
85   RenderFrameImpl* render_frame_;
86 
87   // Valid only while an accessibility mode including kWebContents is set.
88   std::unique_ptr<RenderAccessibilityImpl> render_accessibility_;
89 
90   // Endpoint to receive and handle messages from the browser process.
91   mojo::AssociatedReceiver<mojom::RenderAccessibility> receiver_{this};
92 
93   // Endpoint to send messages to the browser process.
94   mojo::AssociatedRemote<mojom::RenderAccessibilityHost>
95       render_accessibility_host_;
96 
97   DISALLOW_COPY_AND_ASSIGN(RenderAccessibilityManager);
98 };
99 
100 }  // namespace content
101 
102 #endif  // CONTENT_RENDERER_ACCESSIBILITY_RENDER_ACCESSIBILITY_MANAGER_H_
103