OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/renderer/accessibility/renderer_accessibility_complete.h" | |
6 | |
7 #include <queue> | |
8 | |
9 #include "base/bind.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" | |
23 | |
24 using blink::WebAXObject; | |
25 using blink::WebDocument; | |
26 using blink::WebFrame; | |
27 using blink::WebNode; | |
28 using blink::WebPoint; | |
29 using blink::WebRect; | |
30 using blink::WebSettings; | |
31 using blink::WebView; | |
32 | |
33 namespace content { | |
34 | |
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()), | |
41 ack_pending_(false), | |
42 reset_token_(0), | |
43 weak_factory_(this) { | |
44 WebView* web_view = render_frame_->GetRenderView()->GetWebView(); | |
45 WebSettings* settings = web_view->settings(); | |
46 settings->setAccessibilityEnabled(true); | |
47 | |
48 #if !defined(OS_ANDROID) | |
49 // Skip inline text boxes on Android - since there are no native Android | |
50 // APIs that compute the bounds of a range of text, it's a waste to | |
51 // include these in the AX tree. | |
52 settings->setInlineTextBoxAccessibilityEnabled(true); | |
53 #endif | |
54 | |
55 const WebDocument& document = GetMainDocument(); | |
56 if (!document.isNull()) { | |
57 // It's possible that the webview has already loaded a webpage without | |
58 // accessibility being enabled. Initialize the browser's cached | |
59 // accessibility tree by sending it a notification. | |
60 HandleAXEvent(document.accessibilityObject(), | |
61 ui::AX_EVENT_LAYOUT_COMPLETE); | |
62 } | |
63 } | |
64 | |
65 RendererAccessibilityComplete::~RendererAccessibilityComplete() { | |
66 } | |
67 | |
68 bool RendererAccessibilityComplete::OnMessageReceived( | |
69 const IPC::Message& message) { | |
70 bool handled = true; | |
71 IPC_BEGIN_MESSAGE_MAP(RendererAccessibilityComplete, message) | |
72 IPC_MESSAGE_HANDLER(AccessibilityMsg_SetFocus, OnSetFocus) | |
73 IPC_MESSAGE_HANDLER(AccessibilityMsg_DoDefaultAction, | |
74 OnDoDefaultAction) | |
75 IPC_MESSAGE_HANDLER(AccessibilityMsg_Events_ACK, | |
76 OnEventsAck) | |
77 IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToMakeVisible, | |
78 OnScrollToMakeVisible) | |
79 IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToPoint, | |
80 OnScrollToPoint) | |
81 IPC_MESSAGE_HANDLER(AccessibilityMsg_SetTextSelection, | |
82 OnSetTextSelection) | |
83 IPC_MESSAGE_HANDLER(AccessibilityMsg_HitTest, OnHitTest) | |
84 IPC_MESSAGE_HANDLER(AccessibilityMsg_Reset, OnReset) | |
85 IPC_MESSAGE_HANDLER(AccessibilityMsg_FatalError, OnFatalError) | |
86 IPC_MESSAGE_UNHANDLED(handled = false) | |
87 IPC_END_MESSAGE_MAP() | |
88 return handled; | |
89 } | |
90 | |
91 void RendererAccessibilityComplete::FocusedNodeChanged(const WebNode& node) { | |
92 const WebDocument& document = GetMainDocument(); | |
93 if (document.isNull()) | |
94 return; | |
95 | |
96 if (node.isNull()) { | |
97 // When focus is cleared, implicitly focus the document. | |
98 // TODO(dmazzoni): Make Blink send this notification instead. | |
99 HandleAXEvent(document.accessibilityObject(), ui::AX_EVENT_BLUR); | |
100 } | |
101 } | |
102 | |
103 void RendererAccessibilityComplete::DisableAccessibility() { | |
104 RenderView* render_view = render_frame_->GetRenderView(); | |
105 if (!render_view) | |
106 return; | |
107 | |
108 WebView* web_view = render_view->GetWebView(); | |
109 if (!web_view) | |
110 return; | |
111 | |
112 WebSettings* settings = web_view->settings(); | |
113 if (!settings) | |
114 return; | |
115 | |
116 settings->setAccessibilityEnabled(false); | |
117 } | |
118 | |
119 void RendererAccessibilityComplete::HandleWebAccessibilityEvent( | |
120 const blink::WebAXObject& obj, blink::WebAXEvent event) { | |
121 HandleAXEvent(obj, AXEventFromBlink(event)); | |
122 } | |
123 | |
124 void RendererAccessibilityComplete::HandleAXEvent( | |
125 const blink::WebAXObject& obj, ui::AXEvent event) { | |
126 const WebDocument& document = GetMainDocument(); | |
127 if (document.isNull()) | |
128 return; | |
129 | |
130 gfx::Size scroll_offset = document.frame()->scrollOffset(); | |
131 if (scroll_offset != last_scroll_offset_) { | |
132 // Make sure the browser is always aware of the scroll position of | |
133 // the root document element by posting a generic notification that | |
134 // will update it. | |
135 // TODO(dmazzoni): remove this as soon as | |
136 // https://bugs.webkit.org/show_bug.cgi?id=73460 is fixed. | |
137 last_scroll_offset_ = scroll_offset; | |
138 if (!obj.equals(document.accessibilityObject())) { | |
139 HandleAXEvent(document.accessibilityObject(), | |
140 ui::AX_EVENT_LAYOUT_COMPLETE); | |
141 } | |
142 } | |
143 | |
144 // Add the accessibility object to our cache and ensure it's valid. | |
145 AccessibilityHostMsg_EventParams acc_event; | |
146 acc_event.id = obj.axID(); | |
147 acc_event.event_type = event; | |
148 | |
149 // Discard duplicate accessibility events. | |
150 for (uint32 i = 0; i < pending_events_.size(); ++i) { | |
151 if (pending_events_[i].id == acc_event.id && | |
152 pending_events_[i].event_type == | |
153 acc_event.event_type) { | |
154 return; | |
155 } | |
156 } | |
157 pending_events_.push_back(acc_event); | |
158 | |
159 if (!ack_pending_ && !weak_factory_.HasWeakPtrs()) { | |
160 // When no accessibility events are in-flight post a task to send | |
161 // the events to the browser. We use PostTask so that we can queue | |
162 // up additional events. | |
163 base::MessageLoop::current()->PostTask( | |
164 FROM_HERE, | |
165 base::Bind(&RendererAccessibilityComplete:: | |
166 SendPendingAccessibilityEvents, | |
167 weak_factory_.GetWeakPtr())); | |
168 } | |
169 } | |
170 | |
171 RendererAccessibilityType RendererAccessibilityComplete::GetType() { | |
172 return RendererAccessibilityTypeComplete; | |
173 } | |
174 | |
175 void RendererAccessibilityComplete::SendPendingAccessibilityEvents() { | |
176 const WebDocument& document = GetMainDocument(); | |
177 if (document.isNull()) | |
178 return; | |
179 | |
180 if (pending_events_.empty()) | |
181 return; | |
182 | |
183 if (render_frame_->is_swapped_out()) | |
184 return; | |
185 | |
186 ack_pending_ = true; | |
187 | |
188 // Make a copy of the events, because it's possible that | |
189 // actions inside this loop will cause more events to be | |
190 // queued up. | |
191 std::vector<AccessibilityHostMsg_EventParams> src_events = | |
192 pending_events_; | |
193 pending_events_.clear(); | |
194 | |
195 // Generate an event message from each Blink event. | |
196 std::vector<AccessibilityHostMsg_EventParams> event_msgs; | |
197 | |
198 // If there's a layout complete message, we need to send location changes. | |
199 bool had_layout_complete_messages = false; | |
200 | |
201 // Loop over each event and generate an updated event message. | |
202 for (size_t i = 0; i < src_events.size(); ++i) { | |
203 AccessibilityHostMsg_EventParams& event = src_events[i]; | |
204 if (event.event_type == ui::AX_EVENT_LAYOUT_COMPLETE) | |
205 had_layout_complete_messages = true; | |
206 | |
207 WebAXObject obj = document.accessibilityObjectFromID(event.id); | |
208 | |
209 // Make sure the object still exists. | |
210 if (!obj.updateLayoutAndCheckValidity()) | |
211 continue; | |
212 | |
213 // If it's ignored, find the first ancestor that's not ignored. | |
214 while (!obj.isDetached() && obj.accessibilityIsIgnored()) | |
215 obj = obj.parentObject(); | |
216 | |
217 // Make sure it's a descendant of our root node - exceptions include the | |
218 // scroll area that's the parent of the main document (we ignore it), and | |
219 // possibly nodes attached to a different document. | |
220 if (!tree_source_.IsInTree(obj)) | |
221 continue; | |
222 | |
223 // When we get a "selected children changed" event, Blink | |
224 // doesn't also send us events for each child that changed | |
225 // selection state, so make sure we re-send that whole subtree. | |
226 if (event.event_type == | |
227 ui::AX_EVENT_SELECTED_CHILDREN_CHANGED) { | |
228 serializer_.DeleteClientSubtree(obj); | |
229 } | |
230 | |
231 AccessibilityHostMsg_EventParams event_msg; | |
232 tree_source_.CollectChildFrameIdMapping( | |
233 &event_msg.node_to_frame_routing_id_map, | |
234 &event_msg.node_to_browser_plugin_instance_id_map); | |
235 event_msg.event_type = event.event_type; | |
236 event_msg.id = event.id; | |
237 serializer_.SerializeChanges(obj, &event_msg.update); | |
238 event_msgs.push_back(event_msg); | |
239 | |
240 // For each node in the update, set the location in our map from | |
241 // ids to locations. | |
242 for (size_t i = 0; i < event_msg.update.nodes.size(); ++i) { | |
243 locations_[event_msg.update.nodes[i].id] = | |
244 event_msg.update.nodes[i].location; | |
245 } | |
246 | |
247 VLOG(0) << "Accessibility event: " << ui::ToString(event.event_type) | |
248 << " on node id " << event_msg.id | |
249 << "\n" << event_msg.update.ToString(); | |
250 } | |
251 | |
252 Send(new AccessibilityHostMsg_Events(routing_id(), event_msgs, reset_token_)); | |
253 reset_token_ = 0; | |
254 | |
255 if (had_layout_complete_messages) | |
256 SendLocationChanges(); | |
257 } | |
258 | |
259 void RendererAccessibilityComplete::SendLocationChanges() { | |
260 std::vector<AccessibilityHostMsg_LocationChangeParams> messages; | |
261 | |
262 // Do a breadth-first explore of the whole blink AX tree. | |
263 base::hash_map<int, gfx::Rect> new_locations; | |
264 std::queue<WebAXObject> objs_to_explore; | |
265 objs_to_explore.push(tree_source_.GetRoot()); | |
266 while (objs_to_explore.size()) { | |
267 WebAXObject obj = objs_to_explore.front(); | |
268 objs_to_explore.pop(); | |
269 | |
270 // See if we had a previous location. If not, this whole subtree must | |
271 // be new, so don't continue to explore this branch. | |
272 int id = obj.axID(); | |
273 base::hash_map<int, gfx::Rect>::iterator iter = locations_.find(id); | |
274 if (iter == locations_.end()) | |
275 continue; | |
276 | |
277 // If the location has changed, append it to the IPC message. | |
278 gfx::Rect new_location = obj.boundingBoxRect(); | |
279 if (iter != locations_.end() && iter->second != new_location) { | |
280 AccessibilityHostMsg_LocationChangeParams message; | |
281 message.id = id; | |
282 message.new_location = new_location; | |
283 messages.push_back(message); | |
284 } | |
285 | |
286 // Save the new location. | |
287 new_locations[id] = new_location; | |
288 | |
289 // Explore children of this object. | |
290 std::vector<blink::WebAXObject> children; | |
291 tree_source_.GetChildren(obj, &children); | |
292 for (size_t i = 0; i < children.size(); ++i) | |
293 objs_to_explore.push(children[i]); | |
294 } | |
295 locations_.swap(new_locations); | |
296 | |
297 Send(new AccessibilityHostMsg_LocationChanges(routing_id(), messages)); | |
298 } | |
299 | |
300 void RendererAccessibilityComplete::OnDoDefaultAction(int acc_obj_id) { | |
301 const WebDocument& document = GetMainDocument(); | |
302 if (document.isNull()) | |
303 return; | |
304 | |
305 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id); | |
306 if (obj.isDetached()) { | |
307 #ifndef NDEBUG | |
308 LOG(WARNING) << "DoDefaultAction on invalid object id " << acc_obj_id; | |
309 #endif | |
310 return; | |
311 } | |
312 | |
313 obj.performDefaultAction(); | |
314 } | |
315 | |
316 void RendererAccessibilityComplete::OnScrollToMakeVisible( | |
317 int acc_obj_id, gfx::Rect subfocus) { | |
318 const WebDocument& document = GetMainDocument(); | |
319 if (document.isNull()) | |
320 return; | |
321 | |
322 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id); | |
323 if (obj.isDetached()) { | |
324 #ifndef NDEBUG | |
325 LOG(WARNING) << "ScrollToMakeVisible on invalid object id " << acc_obj_id; | |
326 #endif | |
327 return; | |
328 } | |
329 | |
330 obj.scrollToMakeVisibleWithSubFocus( | |
331 WebRect(subfocus.x(), subfocus.y(), | |
332 subfocus.width(), subfocus.height())); | |
333 | |
334 // Make sure the browser gets an event when the scroll | |
335 // position actually changes. | |
336 // TODO(dmazzoni): remove this once this bug is fixed: | |
337 // https://bugs.webkit.org/show_bug.cgi?id=73460 | |
338 HandleAXEvent(document.accessibilityObject(), | |
339 ui::AX_EVENT_LAYOUT_COMPLETE); | |
340 } | |
341 | |
342 void RendererAccessibilityComplete::OnScrollToPoint( | |
343 int acc_obj_id, gfx::Point point) { | |
344 const WebDocument& document = GetMainDocument(); | |
345 if (document.isNull()) | |
346 return; | |
347 | |
348 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id); | |
349 if (obj.isDetached()) { | |
350 #ifndef NDEBUG | |
351 LOG(WARNING) << "ScrollToPoint on invalid object id " << acc_obj_id; | |
352 #endif | |
353 return; | |
354 } | |
355 | |
356 obj.scrollToGlobalPoint(WebPoint(point.x(), point.y())); | |
357 | |
358 // Make sure the browser gets an event when the scroll | |
359 // position actually changes. | |
360 // TODO(dmazzoni): remove this once this bug is fixed: | |
361 // https://bugs.webkit.org/show_bug.cgi?id=73460 | |
362 HandleAXEvent(document.accessibilityObject(), | |
363 ui::AX_EVENT_LAYOUT_COMPLETE); | |
364 } | |
365 | |
366 void RendererAccessibilityComplete::OnSetTextSelection( | |
367 int acc_obj_id, int start_offset, int end_offset) { | |
368 const WebDocument& document = GetMainDocument(); | |
369 if (document.isNull()) | |
370 return; | |
371 | |
372 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id); | |
373 if (obj.isDetached()) { | |
374 #ifndef NDEBUG | |
375 LOG(WARNING) << "SetTextSelection on invalid object id " << acc_obj_id; | |
376 #endif | |
377 return; | |
378 } | |
379 | |
380 // TODO(dmazzoni): support elements other than <input>. | |
381 blink::WebNode node = obj.node(); | |
382 if (!node.isNull() && node.isElementNode()) { | |
383 blink::WebElement element = node.to<blink::WebElement>(); | |
384 blink::WebInputElement* input_element = | |
385 blink::toWebInputElement(&element); | |
386 if (input_element && input_element->isTextField()) | |
387 input_element->setSelectionRange(start_offset, end_offset); | |
388 } | |
389 } | |
390 | |
391 void RendererAccessibilityComplete::OnHitTest(gfx::Point point) { | |
392 const WebDocument& document = GetMainDocument(); | |
393 if (document.isNull()) | |
394 return; | |
395 WebAXObject root_obj = document.accessibilityObject(); | |
396 if (!root_obj.updateLayoutAndCheckValidity()) | |
397 return; | |
398 | |
399 WebAXObject obj = root_obj.hitTest(point); | |
400 if (!obj.isDetached()) | |
401 HandleAXEvent(obj, ui::AX_EVENT_HOVER); | |
402 } | |
403 | |
404 void RendererAccessibilityComplete::OnEventsAck() { | |
405 DCHECK(ack_pending_); | |
406 ack_pending_ = false; | |
407 SendPendingAccessibilityEvents(); | |
408 } | |
409 | |
410 void RendererAccessibilityComplete::OnSetFocus(int acc_obj_id) { | |
411 const WebDocument& document = GetMainDocument(); | |
412 if (document.isNull()) | |
413 return; | |
414 | |
415 WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id); | |
416 if (obj.isDetached()) { | |
417 #ifndef NDEBUG | |
418 LOG(WARNING) << "OnSetAccessibilityFocus on invalid object id " | |
419 << acc_obj_id; | |
420 #endif | |
421 return; | |
422 } | |
423 | |
424 WebAXObject root = document.accessibilityObject(); | |
425 if (root.isDetached()) { | |
426 #ifndef NDEBUG | |
427 LOG(WARNING) << "OnSetAccessibilityFocus but root is invalid"; | |
428 #endif | |
429 return; | |
430 } | |
431 | |
432 // By convention, calling SetFocus on the root of the tree should clear the | |
433 // current focus. Otherwise set the focus to the new node. | |
434 if (acc_obj_id == root.axID()) | |
435 render_frame_->GetRenderView()->GetWebView()->clearFocusedElement(); | |
436 else | |
437 obj.setFocused(true); | |
438 } | |
439 | |
440 void RendererAccessibilityComplete::OnReset(int reset_token) { | |
441 reset_token_ = reset_token; | |
442 serializer_.Reset(); | |
443 pending_events_.clear(); | |
444 | |
445 const WebDocument& document = GetMainDocument(); | |
446 if (!document.isNull()) | |
447 HandleAXEvent(document.accessibilityObject(), ui::AX_EVENT_LAYOUT_COMPLETE); | |
448 } | |
449 | |
450 void RendererAccessibilityComplete::OnFatalError() { | |
451 CHECK(false) << "Invalid accessibility tree."; | |
452 } | |
453 | |
454 } // namespace content | |
OLD | NEW |