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