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_focus_only.h" 6 7 #include "content/common/accessibility_node_data.h" 8 #include "content/renderer/render_view_impl.h" 9 #include "third_party/WebKit/public/web/WebDocument.h" 10 #include "third_party/WebKit/public/web/WebElement.h" 11 #include "third_party/WebKit/public/web/WebFrame.h" 12 #include "third_party/WebKit/public/web/WebNode.h" 13 #include "third_party/WebKit/public/web/WebView.h" 14 15 using WebKit::WebDocument; 16 using WebKit::WebElement; 17 using WebKit::WebFrame; 18 using WebKit::WebNode; 19 using WebKit::WebView; 20 21 namespace { 22 // The root node will always have id 1. Let each child node have a new 23 // id starting with 2. 24 const int kInitialId = 2; 25 } 26 27 namespace content { 28 29 RendererAccessibilityFocusOnly::RendererAccessibilityFocusOnly( 30 RenderViewImpl* render_view) 31 : RendererAccessibility(render_view), 32 next_id_(kInitialId) { 33 } 34 35 RendererAccessibilityFocusOnly::~RendererAccessibilityFocusOnly() { 36 } 37 38 void RendererAccessibilityFocusOnly::HandleWebAccessibilityNotification( 39 const WebKit::WebAccessibilityObject& obj, 40 WebKit::WebAccessibilityNotification notification) { 41 // Do nothing. 42 } 43 44 void RendererAccessibilityFocusOnly::FocusedNodeChanged(const WebNode& node) { 45 // Send the new accessible tree and post a native focus event. 46 HandleFocusedNodeChanged(node, true); 47 } 48 49 void RendererAccessibilityFocusOnly::DidFinishLoad(WebKit::WebFrame* frame) { 50 WebView* view = render_view()->GetWebView(); 51 if (view->focusedFrame() != frame) 52 return; 53 54 WebDocument document = frame->document(); 55 // Send an accessible tree to the browser, but do not post a native 56 // focus event. This is important so that if focus is initially in an 57 // editable text field, Windows will know to pop up the keyboard if the 58 // user touches it and focus doesn't change. 59 HandleFocusedNodeChanged(document.focusedNode(), false); 60 } 61 62 void RendererAccessibilityFocusOnly::HandleFocusedNodeChanged( 63 const WebNode& node, 64 bool send_focus_event) { 65 const WebDocument& document = GetMainDocument(); 66 if (document.isNull()) 67 return; 68 69 bool node_has_focus; 70 bool node_is_editable_text; 71 // Check HasIMETextFocus first, because it will correctly handle 72 // focus in a text box inside a ppapi plug-in. Otherwise fall back on 73 // checking the focused node in WebKit. 74 if (render_view_->HasIMETextFocus()) { 75 node_has_focus = true; 76 node_is_editable_text = true; 77 } else { 78 node_has_focus = !node.isNull(); 79 node_is_editable_text = 80 node_has_focus && render_view_->IsEditableNode(node); 81 } 82 83 std::vector<AccessibilityHostMsg_NotificationParams> notifications; 84 notifications.push_back(AccessibilityHostMsg_NotificationParams()); 85 AccessibilityHostMsg_NotificationParams& notification = notifications[0]; 86 87 // If we want to update the browser's accessibility tree but not send a 88 // native focus changed notification, we can send a LayoutComplete 89 // notification, which doesn't post a native event on Windows. 90 notification.notification_type = 91 send_focus_event ? 92 AccessibilityNotificationFocusChanged : 93 AccessibilityNotificationLayoutComplete; 94 95 // Set the id that the notification applies to: the root node if nothing 96 // has focus, otherwise the focused node. 97 notification.id = node_has_focus ? next_id_ : 1; 98 99 notification.nodes.resize(2); 100 AccessibilityNodeData& root = notification.nodes[0]; 101 AccessibilityNodeData& child = notification.nodes[1]; 102 103 // Always include the root of the tree, the document. It always has id 1. 104 root.id = 1; 105 root.role = AccessibilityNodeData::ROLE_ROOT_WEB_AREA; 106 root.state = 107 (1 << AccessibilityNodeData::STATE_READONLY) | 108 (1 << AccessibilityNodeData::STATE_FOCUSABLE); 109 if (!node_has_focus) 110 root.state |= (1 << AccessibilityNodeData::STATE_FOCUSED); 111 root.location = gfx::Rect(render_view_->size()); 112 root.child_ids.push_back(next_id_); 113 114 child.id = next_id_; 115 child.role = AccessibilityNodeData::ROLE_GROUP; 116 117 if (!node.isNull() && node.isElementNode()) { 118 child.location = gfx::Rect( 119 const_cast<WebNode&>(node).to<WebElement>().boundsInViewportSpace()); 120 } else if (render_view_->HasIMETextFocus()) { 121 child.location = root.location; 122 } else { 123 child.location = gfx::Rect(); 124 } 125 126 if (node_has_focus) { 127 child.state = 128 (1 << AccessibilityNodeData::STATE_FOCUSABLE) | 129 (1 << AccessibilityNodeData::STATE_FOCUSED); 130 if (!node_is_editable_text) 131 child.state |= (1 << AccessibilityNodeData::STATE_READONLY); 132 } 133 134 #ifndef NDEBUG 135 if (logging_) { 136 LOG(INFO) << "Accessibility update: \n" 137 << "routing id=" << routing_id() 138 << " notification=" 139 << AccessibilityNotificationToString(notification.notification_type) 140 << "\n" << notification.nodes[0].DebugString(true); 141 } 142 #endif 143 144 Send(new AccessibilityHostMsg_Notifications(routing_id(), notifications)); 145 146 // Increment the id, wrap back when we get past a million. 147 next_id_++; 148 if (next_id_ > 1000000) 149 next_id_ = kInitialId; 150 } 151 152 } // namespace content 153