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_view_impl.h"
13 #include "third_party/WebKit/public/web/WebAXObject.h"
14 #include "third_party/WebKit/public/web/WebDocument.h"
15 #include "third_party/WebKit/public/web/WebInputElement.h"
16 #include "third_party/WebKit/public/web/WebLocalFrame.h"
17 #include "third_party/WebKit/public/web/WebNode.h"
18 #include "third_party/WebKit/public/web/WebView.h"
19 #include "ui/accessibility/ax_tree.h"
21 using blink::WebAXObject;
22 using blink::WebDocument;
24 using blink::WebPoint;
30 RendererAccessibilityComplete::RendererAccessibilityComplete(
31 RenderViewImpl* render_view)
32 : RendererAccessibility(render_view),
34 tree_source_(render_view),
35 serializer_(&tree_source_),
36 last_scroll_offset_(gfx::Size()),
38 WebAXObject::enableAccessibility();
40 #if !defined(OS_ANDROID)
41 // Skip inline text boxes on Android - since there are no native Android
42 // APIs that compute the bounds of a range of text, it's a waste to
43 // include these in the AX tree.
44 WebAXObject::enableInlineTextBoxAccessibility();
47 const WebDocument& document = GetMainDocument();
48 if (!document.isNull()) {
49 // It's possible that the webview has already loaded a webpage without
50 // accessibility being enabled. Initialize the browser's cached
51 // accessibility tree by sending it a notification.
52 HandleAXEvent(document.accessibilityObject(),
53 ui::AX_EVENT_LAYOUT_COMPLETE);
57 RendererAccessibilityComplete::~RendererAccessibilityComplete() {
60 bool RendererAccessibilityComplete::OnMessageReceived(
61 const IPC::Message& message) {
63 IPC_BEGIN_MESSAGE_MAP(RendererAccessibilityComplete, message)
64 IPC_MESSAGE_HANDLER(AccessibilityMsg_SetFocus, OnSetFocus)
65 IPC_MESSAGE_HANDLER(AccessibilityMsg_DoDefaultAction,
67 IPC_MESSAGE_HANDLER(AccessibilityMsg_Events_ACK,
69 IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToMakeVisible,
70 OnScrollToMakeVisible)
71 IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToPoint,
73 IPC_MESSAGE_HANDLER(AccessibilityMsg_SetTextSelection,
75 IPC_MESSAGE_HANDLER(AccessibilityMsg_HitTest, OnHitTest)
76 IPC_MESSAGE_HANDLER(AccessibilityMsg_FatalError, OnFatalError)
77 IPC_MESSAGE_UNHANDLED(handled = false)
82 void RendererAccessibilityComplete::FocusedNodeChanged(const WebNode& node) {
83 const WebDocument& document = GetMainDocument();
84 if (document.isNull())
88 // When focus is cleared, implicitly focus the document.
89 // TODO(dmazzoni): Make Blink send this notification instead.
90 HandleAXEvent(document.accessibilityObject(), ui::AX_EVENT_BLUR);
94 void RendererAccessibilityComplete::DidFinishLoad(blink::WebLocalFrame* frame) {
95 const WebDocument& document = GetMainDocument();
96 if (document.isNull())
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_view_->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 // Loop over each event and generate an updated event message.
181 for (size_t i = 0; i < src_events.size(); ++i) {
182 AccessibilityHostMsg_EventParams& event =
185 WebAXObject obj = document.accessibilityObjectFromID(
187 // Make sure the object still exists.
188 if (!obj.updateBackingStoreAndCheckValidity())
190 // Make sure it's a descendant of our root node - exceptions include the
191 // scroll area that's the parent of the main document (we ignore it), and
192 // possibly nodes attached to a different document.
193 if (!tree_source_.IsInTree(obj))
196 // When we get a "selected children changed" event, Blink
197 // doesn't also send us events for each child that changed
198 // selection state, so make sure we re-send that whole subtree.
199 if (event.event_type ==
200 ui::AX_EVENT_SELECTED_CHILDREN_CHANGED) {
201 serializer_.DeleteClientSubtree(obj);
204 AccessibilityHostMsg_EventParams event_msg;
205 event_msg.event_type = event.event_type;
206 event_msg.id = event.id;
207 serializer_.SerializeChanges(obj, &event_msg.update);
208 event_msgs.push_back(event_msg);
210 VLOG(0) << "Accessibility event: " << ui::ToString(event.event_type)
211 << " on node id " << event_msg.id
212 << "\n" << event_msg.update.ToString();
215 Send(new AccessibilityHostMsg_Events(routing_id(), event_msgs));
217 SendLocationChanges();
220 void RendererAccessibilityComplete::SendLocationChanges() {
221 std::vector<AccessibilityHostMsg_LocationChangeParams> messages;
223 // Do a breadth-first explore of the whole blink AX tree.
224 base::hash_map<int, gfx::Rect> new_locations;
225 std::queue<WebAXObject> objs_to_explore;
226 objs_to_explore.push(tree_source_.GetRoot());
227 while (objs_to_explore.size()) {
228 WebAXObject obj = objs_to_explore.front();
229 objs_to_explore.pop();
231 // See if we had a previous location. If not, this whole subtree must
232 // be new, so don't continue to explore this branch.
234 base::hash_map<int, gfx::Rect>::iterator iter = locations_.find(id);
235 if (iter == locations_.end())
238 // If the location has changed, append it to the IPC message.
239 gfx::Rect new_location = obj.boundingBoxRect();
240 if (iter != locations_.end() && iter->second != new_location) {
241 AccessibilityHostMsg_LocationChangeParams message;
243 message.new_location = new_location;
244 messages.push_back(message);
247 // Save the new location.
248 new_locations[id] = new_location;
250 locations_.swap(new_locations);
252 Send(new AccessibilityHostMsg_LocationChanges(routing_id(), messages));
255 void RendererAccessibilityComplete::OnDoDefaultAction(int acc_obj_id) {
256 const WebDocument& document = GetMainDocument();
257 if (document.isNull())
260 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
261 if (obj.isDetached()) {
263 LOG(WARNING) << "DoDefaultAction on invalid object id " << acc_obj_id;
268 obj.performDefaultAction();
271 void RendererAccessibilityComplete::OnScrollToMakeVisible(
272 int acc_obj_id, gfx::Rect subfocus) {
273 const WebDocument& document = GetMainDocument();
274 if (document.isNull())
277 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
278 if (obj.isDetached()) {
280 LOG(WARNING) << "ScrollToMakeVisible on invalid object id " << acc_obj_id;
285 obj.scrollToMakeVisibleWithSubFocus(
286 WebRect(subfocus.x(), subfocus.y(),
287 subfocus.width(), subfocus.height()));
289 // Make sure the browser gets an event when the scroll
290 // position actually changes.
291 // TODO(dmazzoni): remove this once this bug is fixed:
292 // https://bugs.webkit.org/show_bug.cgi?id=73460
293 HandleAXEvent(document.accessibilityObject(),
294 ui::AX_EVENT_LAYOUT_COMPLETE);
297 void RendererAccessibilityComplete::OnScrollToPoint(
298 int acc_obj_id, gfx::Point point) {
299 const WebDocument& document = GetMainDocument();
300 if (document.isNull())
303 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
304 if (obj.isDetached()) {
306 LOG(WARNING) << "ScrollToPoint on invalid object id " << acc_obj_id;
311 obj.scrollToGlobalPoint(WebPoint(point.x(), point.y()));
313 // Make sure the browser gets an event when the scroll
314 // position actually changes.
315 // TODO(dmazzoni): remove this once this bug is fixed:
316 // https://bugs.webkit.org/show_bug.cgi?id=73460
317 HandleAXEvent(document.accessibilityObject(),
318 ui::AX_EVENT_LAYOUT_COMPLETE);
321 void RendererAccessibilityComplete::OnSetTextSelection(
322 int acc_obj_id, int start_offset, int end_offset) {
323 const WebDocument& document = GetMainDocument();
324 if (document.isNull())
327 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
328 if (obj.isDetached()) {
330 LOG(WARNING) << "SetTextSelection on invalid object id " << acc_obj_id;
335 // TODO(dmazzoni): support elements other than <input>.
336 blink::WebNode node = obj.node();
337 if (!node.isNull() && node.isElementNode()) {
338 blink::WebElement element = node.to<blink::WebElement>();
339 blink::WebInputElement* input_element =
340 blink::toWebInputElement(&element);
341 if (input_element && input_element->isTextField())
342 input_element->setSelectionRange(start_offset, end_offset);
346 void RendererAccessibilityComplete::OnHitTest(gfx::Point point) {
347 const WebDocument& document = GetMainDocument();
348 if (document.isNull())
350 WebAXObject root_obj = document.accessibilityObject();
351 if (!root_obj.updateBackingStoreAndCheckValidity())
354 WebAXObject obj = root_obj.hitTest(point);
355 if (!obj.isDetached())
356 HandleAXEvent(obj, ui::AX_EVENT_HOVER);
359 void RendererAccessibilityComplete::OnEventsAck() {
360 DCHECK(ack_pending_);
361 ack_pending_ = false;
362 SendPendingAccessibilityEvents();
365 void RendererAccessibilityComplete::OnSetFocus(int acc_obj_id) {
366 const WebDocument& document = GetMainDocument();
367 if (document.isNull())
370 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
371 if (obj.isDetached()) {
373 LOG(WARNING) << "OnSetAccessibilityFocus on invalid object id "
379 WebAXObject root = document.accessibilityObject();
380 if (root.isDetached()) {
382 LOG(WARNING) << "OnSetAccessibilityFocus but root is invalid";
387 // By convention, calling SetFocus on the root of the tree should clear the
388 // current focus. Otherwise set the focus to the new node.
389 if (acc_obj_id == root.axID())
390 render_view()->GetWebView()->clearFocusedElement();
392 obj.setFocused(true);
395 void RendererAccessibilityComplete::OnFatalError() {
396 CHECK(false) << "Invalid accessibility tree.";
399 } // namespace content