1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_RENDERER_ACCESSIBILITY_RENDERER_ACCESSIBILITY_H_
6 #define CONTENT_RENDERER_ACCESSIBILITY_RENDERER_ACCESSIBILITY_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/common/ax_content_node_data.h"
13 #include "content/public/renderer/render_frame_observer.h"
14 #include "content/renderer/accessibility/blink_ax_tree_source.h"
15 #include "third_party/WebKit/public/web/WebAXObject.h"
16 #include "ui/accessibility/ax_tree_serializer.h"
18 struct AccessibilityHostMsg_EventParams
;
26 class RenderFrameImpl
;
28 // The browser process implements native accessibility APIs, allowing
29 // assistive technology (e.g., screen readers, magnifiers) to access and
30 // control the web contents with high-level APIs. These APIs are also used
31 // by automation tools, and Windows 8 uses them to determine when the
32 // on-screen keyboard should be shown.
34 // An instance of this class belongs to RenderFrameImpl. Accessibility is
35 // initialized based on the AccessibilityMode of RenderFrameImpl; it lazily
36 // starts as Off or EditableTextOnly depending on the operating system, and
37 // switches to Complete if assistive technology is detected or a flag is set.
39 // A tree of accessible objects is built here and sent to the browser process;
40 // the browser process maintains this as a tree of platform-native
41 // accessible objects that can be used to respond to accessibility requests
42 // from other processes.
44 // This class implements complete accessibility support for assistive
45 // technology. It turns on Blink's accessibility code and sends a serialized
46 // representation of that tree whenever it changes. It also handles requests
47 // from the browser to perform accessibility actions on nodes in the tree
48 // (e.g., change focus, or click on a button).
49 class CONTENT_EXPORT RendererAccessibility
: public RenderFrameObserver
{
51 // Request a one-time snapshot of the accessibility tree without
52 // enabling accessibility if it wasn't already enabled.
53 static void SnapshotAccessibilityTree(
54 RenderFrameImpl
* render_frame
,
55 ui::AXTreeUpdate
<AXContentNodeData
>* response
);
57 explicit RendererAccessibility(RenderFrameImpl
* render_frame
);
58 ~RendererAccessibility() override
;
60 // RenderFrameObserver implementation.
61 bool OnMessageReceived(const IPC::Message
& message
) override
;
63 // Called when an accessibility notification occurs in Blink.
64 void HandleWebAccessibilityEvent(const blink::WebAXObject
& obj
,
65 blink::WebAXEvent event
);
67 // Called when a new find in page result is highlighted.
68 void HandleAccessibilityFindInPageResult(
71 const blink::WebAXObject
& start_object
,
73 const blink::WebAXObject
& end_object
,
76 void AccessibilityFocusedNodeChanged(const blink::WebNode
& node
);
78 // This can be called before deleting a RendererAccessibility instance due
79 // to the accessibility mode changing, as opposed to during frame destruction
80 // (when there'd be no point).
81 void DisableAccessibility();
83 void HandleAXEvent(const blink::WebAXObject
& obj
, ui::AXEvent event
);
86 // Returns the main top-level document for this page, or NULL if there's
88 blink::WebDocument
GetMainDocument();
90 // Send queued events from the renderer to the browser.
91 void SendPendingAccessibilityEvents();
93 // Check the entire accessibility tree to see if any nodes have
94 // changed location, by comparing their locations to the cached
95 // versions. If any have moved, send an IPC with the new locations.
96 void SendLocationChanges();
98 // The RenderFrameImpl that owns us.
99 RenderFrameImpl
* render_frame_
;
102 // Handlers for messages from the browser to the renderer.
103 void OnDoDefaultAction(int acc_obj_id
);
106 void OnHitTest(gfx::Point point
);
107 void OnSetAccessibilityFocus(int acc_obj_id
);
108 void OnReset(int reset_token
);
109 void OnScrollToMakeVisible(int acc_obj_id
, gfx::Rect subfocus
);
110 void OnScrollToPoint(int acc_obj_id
, gfx::Point point
);
111 void OnSetScrollOffset(int acc_obj_id
, gfx::Point offset
);
112 void OnSetFocus(int acc_obj_id
);
113 void OnSetTextSelection(int acc_obj_id
, int start_offset
, int end_offset
);
114 void OnSetValue(int acc_obj_id
, base::string16 value
);
115 void OnShowContextMenu(int acc_obj_id
);
117 // Events from Blink are collected until they are ready to be
118 // sent to the browser.
119 std::vector
<AccessibilityHostMsg_EventParams
> pending_events_
;
121 // The adapter that exposes Blink's accessibility tree to AXTreeSerializer.
122 BlinkAXTreeSource tree_source_
;
124 // The serializer that sends accessibility messages to the browser process.
125 using BlinkAXTreeSerializer
=
126 ui::AXTreeSerializer
<blink::WebAXObject
, AXContentNodeData
>;
127 BlinkAXTreeSerializer serializer_
;
129 // Current location of every object, so we can detect when it moves.
130 base::hash_map
<int, gfx::Rect
> locations_
;
132 // The most recently observed scroll offset of the root document element.
133 // TODO(dmazzoni): remove once https://bugs.webkit.org/show_bug.cgi?id=73460
135 gfx::Size last_scroll_offset_
;
137 // Set if we are waiting for an accessibility event ack.
140 // Nonzero if the browser requested we reset the accessibility state.
141 // We need to return this token in the next IPC.
144 // So we can queue up tasks to be executed later.
145 base::WeakPtrFactory
<RendererAccessibility
> weak_factory_
;
147 DISALLOW_COPY_AND_ASSIGN(RendererAccessibility
);
150 } // namespace content
152 #endif // CONTENT_RENDERER_ACCESSIBILITY_RENDERER_ACCESSIBILITY_H_