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 #include "content/renderer/accessibility/renderer_accessibility_complete.h"
10 #include "base/message_loop/message_loop.h"
11 #include "content/renderer/accessibility/blink_ax_enum_conversion.h"
12 #include "content/renderer/render_frame_impl.h"
13 #include "content/renderer/render_view_impl.h"
14 #include "third_party/WebKit/public/web/WebAXObject.h"
15 #include "third_party/WebKit/public/web/WebDocument.h"
16 #include "third_party/WebKit/public/web/WebFrame.h"
17 #include "third_party/WebKit/public/web/WebInputElement.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 #include "third_party/WebKit/public/web/WebNode.h"
20 #include "third_party/WebKit/public/web/WebSettings.h"
21 #include "third_party/WebKit/public/web/WebView.h"
22 #include "ui/accessibility/ax_tree.h"
24 using blink::WebAXObject;
25 using blink::WebDocument;
26 using blink::WebFrame;
28 using blink::WebPoint;
30 using blink::WebSettings;
35 RendererAccessibilityComplete::RendererAccessibilityComplete(
36 RenderFrameImpl* render_frame)
37 : RendererAccessibility(render_frame),
38 tree_source_(render_frame),
39 serializer_(&tree_source_),
40 last_scroll_offset_(gfx::Size()),
43 WebView* web_view = render_frame_->GetRenderView()->GetWebView();
44 WebSettings* settings = web_view->settings();
45 settings->setAccessibilityEnabled(true);
47 #if !defined(OS_ANDROID)
48 // Skip inline text boxes on Android - since there are no native Android
49 // APIs that compute the bounds of a range of text, it's a waste to
50 // include these in the AX tree.
51 settings->setInlineTextBoxAccessibilityEnabled(true);
54 const WebDocument& document = GetMainDocument();
55 if (!document.isNull()) {
56 // It's possible that the webview has already loaded a webpage without
57 // accessibility being enabled. Initialize the browser's cached
58 // accessibility tree by sending it a notification.
59 HandleAXEvent(document.accessibilityObject(),
60 ui::AX_EVENT_LAYOUT_COMPLETE);
64 RendererAccessibilityComplete::~RendererAccessibilityComplete() {
67 bool RendererAccessibilityComplete::OnMessageReceived(
68 const IPC::Message& message) {
70 IPC_BEGIN_MESSAGE_MAP(RendererAccessibilityComplete, message)
71 IPC_MESSAGE_HANDLER(AccessibilityMsg_SetFocus, OnSetFocus)
72 IPC_MESSAGE_HANDLER(AccessibilityMsg_DoDefaultAction,
74 IPC_MESSAGE_HANDLER(AccessibilityMsg_Events_ACK,
76 IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToMakeVisible,
77 OnScrollToMakeVisible)
78 IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToPoint,
80 IPC_MESSAGE_HANDLER(AccessibilityMsg_SetTextSelection,
82 IPC_MESSAGE_HANDLER(AccessibilityMsg_HitTest, OnHitTest)
83 IPC_MESSAGE_HANDLER(AccessibilityMsg_FatalError, OnFatalError)
84 IPC_MESSAGE_UNHANDLED(handled = false)
89 void RendererAccessibilityComplete::FocusedNodeChanged(const WebNode& node) {
90 const WebDocument& document = GetMainDocument();
91 if (document.isNull())
95 // When focus is cleared, implicitly focus the document.
96 // TODO(dmazzoni): Make Blink send this notification instead.
97 HandleAXEvent(document.accessibilityObject(), ui::AX_EVENT_BLUR);
101 void RendererAccessibilityComplete::HandleWebAccessibilityEvent(
102 const blink::WebAXObject& obj, blink::WebAXEvent event) {
103 HandleAXEvent(obj, AXEventFromBlink(event));
106 void RendererAccessibilityComplete::HandleAXEvent(
107 const blink::WebAXObject& obj, ui::AXEvent event) {
108 const WebDocument& document = GetMainDocument();
109 if (document.isNull())
112 gfx::Size scroll_offset = document.frame()->scrollOffset();
113 if (scroll_offset != last_scroll_offset_) {
114 // Make sure the browser is always aware of the scroll position of
115 // the root document element by posting a generic notification that
117 // TODO(dmazzoni): remove this as soon as
118 // https://bugs.webkit.org/show_bug.cgi?id=73460 is fixed.
119 last_scroll_offset_ = scroll_offset;
120 if (!obj.equals(document.accessibilityObject())) {
121 HandleAXEvent(document.accessibilityObject(),
122 ui::AX_EVENT_LAYOUT_COMPLETE);
126 // Add the accessibility object to our cache and ensure it's valid.
127 AccessibilityHostMsg_EventParams acc_event;
128 acc_event.id = obj.axID();
129 acc_event.event_type = event;
131 // Discard duplicate accessibility events.
132 for (uint32 i = 0; i < pending_events_.size(); ++i) {
133 if (pending_events_[i].id == acc_event.id &&
134 pending_events_[i].event_type ==
135 acc_event.event_type) {
139 pending_events_.push_back(acc_event);
141 if (!ack_pending_ && !weak_factory_.HasWeakPtrs()) {
142 // When no accessibility events are in-flight post a task to send
143 // the events to the browser. We use PostTask so that we can queue
144 // up additional events.
145 base::MessageLoop::current()->PostTask(
147 base::Bind(&RendererAccessibilityComplete::
148 SendPendingAccessibilityEvents,
149 weak_factory_.GetWeakPtr()));
153 RendererAccessibilityType RendererAccessibilityComplete::GetType() {
154 return RendererAccessibilityTypeComplete;
157 void RendererAccessibilityComplete::SendPendingAccessibilityEvents() {
158 const WebDocument& document = GetMainDocument();
159 if (document.isNull())
162 if (pending_events_.empty())
165 if (render_frame_->is_swapped_out())
170 // Make a copy of the events, because it's possible that
171 // actions inside this loop will cause more events to be
173 std::vector<AccessibilityHostMsg_EventParams> src_events =
175 pending_events_.clear();
177 // Generate an event message from each Blink event.
178 std::vector<AccessibilityHostMsg_EventParams> event_msgs;
180 // If there's a layout complete message, we need to send location changes.
181 bool had_layout_complete_messages = false;
183 // Loop over each event and generate an updated event message.
184 for (size_t i = 0; i < src_events.size(); ++i) {
185 AccessibilityHostMsg_EventParams& event = src_events[i];
186 if (event.event_type == ui::AX_EVENT_LAYOUT_COMPLETE)
187 had_layout_complete_messages = true;
189 WebAXObject obj = document.accessibilityObjectFromID(event.id);
191 // Make sure the object still exists.
192 if (!obj.updateBackingStoreAndCheckValidity())
195 // Make sure it's a descendant of our root node - exceptions include the
196 // scroll area that's the parent of the main document (we ignore it), and
197 // possibly nodes attached to a different document.
198 if (!tree_source_.IsInTree(obj))
201 // When we get a "selected children changed" event, Blink
202 // doesn't also send us events for each child that changed
203 // selection state, so make sure we re-send that whole subtree.
204 if (event.event_type ==
205 ui::AX_EVENT_SELECTED_CHILDREN_CHANGED) {
206 serializer_.DeleteClientSubtree(obj);
209 AccessibilityHostMsg_EventParams event_msg;
210 tree_source_.CollectChildFrameIdMapping(
211 &event_msg.node_to_frame_routing_id_map,
212 &event_msg.node_to_browser_plugin_instance_id_map);
213 event_msg.event_type = event.event_type;
214 event_msg.id = event.id;
215 serializer_.SerializeChanges(obj, &event_msg.update);
216 event_msgs.push_back(event_msg);
218 // For each node in the update, set the location in our map from
220 for (size_t i = 0; i < event_msg.update.nodes.size(); ++i) {
221 locations_[event_msg.update.nodes[i].id] =
222 event_msg.update.nodes[i].location;
225 VLOG(0) << "Accessibility event: " << ui::ToString(event.event_type)
226 << " on node id " << event_msg.id
227 << "\n" << event_msg.update.ToString();
230 Send(new AccessibilityHostMsg_Events(routing_id(), event_msgs));
232 if (had_layout_complete_messages)
233 SendLocationChanges();
236 void RendererAccessibilityComplete::SendLocationChanges() {
237 std::vector<AccessibilityHostMsg_LocationChangeParams> messages;
239 // Do a breadth-first explore of the whole blink AX tree.
240 base::hash_map<int, gfx::Rect> new_locations;
241 std::queue<WebAXObject> objs_to_explore;
242 objs_to_explore.push(tree_source_.GetRoot());
243 while (objs_to_explore.size()) {
244 WebAXObject obj = objs_to_explore.front();
245 objs_to_explore.pop();
247 // See if we had a previous location. If not, this whole subtree must
248 // be new, so don't continue to explore this branch.
250 base::hash_map<int, gfx::Rect>::iterator iter = locations_.find(id);
251 if (iter == locations_.end())
254 // If the location has changed, append it to the IPC message.
255 gfx::Rect new_location = obj.boundingBoxRect();
256 if (iter != locations_.end() && iter->second != new_location) {
257 AccessibilityHostMsg_LocationChangeParams message;
259 message.new_location = new_location;
260 messages.push_back(message);
263 // Save the new location.
264 new_locations[id] = new_location;
266 // Explore children of this object.
267 std::vector<blink::WebAXObject> children;
268 tree_source_.GetChildren(obj, &children);
269 for (size_t i = 0; i < children.size(); ++i)
270 objs_to_explore.push(children[i]);
272 locations_.swap(new_locations);
274 Send(new AccessibilityHostMsg_LocationChanges(routing_id(), messages));
277 void RendererAccessibilityComplete::OnDoDefaultAction(int acc_obj_id) {
278 const WebDocument& document = GetMainDocument();
279 if (document.isNull())
282 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
283 if (obj.isDetached()) {
285 LOG(WARNING) << "DoDefaultAction on invalid object id " << acc_obj_id;
290 obj.performDefaultAction();
293 void RendererAccessibilityComplete::OnScrollToMakeVisible(
294 int acc_obj_id, gfx::Rect subfocus) {
295 const WebDocument& document = GetMainDocument();
296 if (document.isNull())
299 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
300 if (obj.isDetached()) {
302 LOG(WARNING) << "ScrollToMakeVisible on invalid object id " << acc_obj_id;
307 obj.scrollToMakeVisibleWithSubFocus(
308 WebRect(subfocus.x(), subfocus.y(),
309 subfocus.width(), subfocus.height()));
311 // Make sure the browser gets an event when the scroll
312 // position actually changes.
313 // TODO(dmazzoni): remove this once this bug is fixed:
314 // https://bugs.webkit.org/show_bug.cgi?id=73460
315 HandleAXEvent(document.accessibilityObject(),
316 ui::AX_EVENT_LAYOUT_COMPLETE);
319 void RendererAccessibilityComplete::OnScrollToPoint(
320 int acc_obj_id, gfx::Point point) {
321 const WebDocument& document = GetMainDocument();
322 if (document.isNull())
325 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
326 if (obj.isDetached()) {
328 LOG(WARNING) << "ScrollToPoint on invalid object id " << acc_obj_id;
333 obj.scrollToGlobalPoint(WebPoint(point.x(), point.y()));
335 // Make sure the browser gets an event when the scroll
336 // position actually changes.
337 // TODO(dmazzoni): remove this once this bug is fixed:
338 // https://bugs.webkit.org/show_bug.cgi?id=73460
339 HandleAXEvent(document.accessibilityObject(),
340 ui::AX_EVENT_LAYOUT_COMPLETE);
343 void RendererAccessibilityComplete::OnSetTextSelection(
344 int acc_obj_id, int start_offset, int end_offset) {
345 const WebDocument& document = GetMainDocument();
346 if (document.isNull())
349 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
350 if (obj.isDetached()) {
352 LOG(WARNING) << "SetTextSelection on invalid object id " << acc_obj_id;
357 // TODO(dmazzoni): support elements other than <input>.
358 blink::WebNode node = obj.node();
359 if (!node.isNull() && node.isElementNode()) {
360 blink::WebElement element = node.to<blink::WebElement>();
361 blink::WebInputElement* input_element =
362 blink::toWebInputElement(&element);
363 if (input_element && input_element->isTextField())
364 input_element->setSelectionRange(start_offset, end_offset);
368 void RendererAccessibilityComplete::OnHitTest(gfx::Point point) {
369 const WebDocument& document = GetMainDocument();
370 if (document.isNull())
372 WebAXObject root_obj = document.accessibilityObject();
373 if (!root_obj.updateBackingStoreAndCheckValidity())
376 WebAXObject obj = root_obj.hitTest(point);
377 if (!obj.isDetached())
378 HandleAXEvent(obj, ui::AX_EVENT_HOVER);
381 void RendererAccessibilityComplete::OnEventsAck() {
382 DCHECK(ack_pending_);
383 ack_pending_ = false;
384 SendPendingAccessibilityEvents();
387 void RendererAccessibilityComplete::OnSetFocus(int acc_obj_id) {
388 const WebDocument& document = GetMainDocument();
389 if (document.isNull())
392 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
393 if (obj.isDetached()) {
395 LOG(WARNING) << "OnSetAccessibilityFocus on invalid object id "
401 WebAXObject root = document.accessibilityObject();
402 if (root.isDetached()) {
404 LOG(WARNING) << "OnSetAccessibilityFocus but root is invalid";
409 // By convention, calling SetFocus on the root of the tree should clear the
410 // current focus. Otherwise set the focus to the new node.
411 if (acc_obj_id == root.axID())
412 render_frame_->GetRenderView()->GetWebView()->clearFocusedElement();
414 obj.setFocused(true);
417 void RendererAccessibilityComplete::OnFatalError() {
418 CHECK(false) << "Invalid accessibility tree.";
421 } // namespace content