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_focus_only.h"
7 #include "content/renderer/render_view_impl.h"
8 #include "third_party/WebKit/public/web/WebDocument.h"
9 #include "third_party/WebKit/public/web/WebElement.h"
10 #include "third_party/WebKit/public/web/WebLocalFrame.h"
11 #include "third_party/WebKit/public/web/WebNode.h"
12 #include "third_party/WebKit/public/web/WebView.h"
13 #include "ui/accessibility/ax_node_data.h"
15 using blink::WebDocument
;
16 using blink::WebElement
;
21 // The root node will always have id 1. Let each child node have a new
22 // id starting with 2.
23 const int kInitialId
= 2;
28 RendererAccessibilityFocusOnly::RendererAccessibilityFocusOnly(
29 RenderViewImpl
* render_view
)
30 : RendererAccessibility(render_view
),
31 next_id_(kInitialId
) {
34 RendererAccessibilityFocusOnly::~RendererAccessibilityFocusOnly() {
37 void RendererAccessibilityFocusOnly::HandleWebAccessibilityEvent(
38 const blink::WebAXObject
& obj
, blink::WebAXEvent event
) {
42 RendererAccessibilityType
RendererAccessibilityFocusOnly::GetType() {
43 return RendererAccessibilityTypeFocusOnly
;
46 void RendererAccessibilityFocusOnly::FocusedNodeChanged(const WebNode
& node
) {
47 // Send the new accessible tree and post a native focus event.
48 HandleFocusedNodeChanged(node
, true);
51 void RendererAccessibilityFocusOnly::DidFinishLoad(
52 blink::WebLocalFrame
* frame
) {
53 WebView
* view
= render_view()->GetWebView();
54 if (view
->focusedFrame() != frame
)
57 WebDocument document
= frame
->document();
58 // Send an accessible tree to the browser, but do not post a native
59 // focus event. This is important so that if focus is initially in an
60 // editable text field, Windows will know to pop up the keyboard if the
61 // user touches it and focus doesn't change.
62 HandleFocusedNodeChanged(document
.focusedElement(), false);
65 void RendererAccessibilityFocusOnly::HandleFocusedNodeChanged(
67 bool send_focus_event
) {
68 const WebDocument
& document
= GetMainDocument();
69 if (document
.isNull())
73 bool node_is_editable_text
;
74 // Check HasIMETextFocus first, because it will correctly handle
75 // focus in a text box inside a ppapi plug-in. Otherwise fall back on
76 // checking the focused node in Blink.
77 if (render_view_
->HasIMETextFocus()) {
78 node_has_focus
= true;
79 node_is_editable_text
= true;
81 node_has_focus
= !node
.isNull();
82 node_is_editable_text
=
83 node_has_focus
&& render_view_
->IsEditableNode(node
);
86 std::vector
<AccessibilityHostMsg_EventParams
> events
;
87 events
.push_back(AccessibilityHostMsg_EventParams());
88 AccessibilityHostMsg_EventParams
& event
= events
[0];
90 // If we want to update the browser's accessibility tree but not send a
91 // native focus changed event, we can send a LayoutComplete
92 // event, which doesn't post a native event on Windows.
94 send_focus_event
? ui::AX_EVENT_FOCUS
: ui::AX_EVENT_LAYOUT_COMPLETE
;
96 // Set the id that the event applies to: the root node if nothing
97 // has focus, otherwise the focused node.
98 event
.id
= node_has_focus
? next_id_
: 1;
100 event
.update
.nodes
.resize(2);
101 ui::AXNodeData
& root
= event
.update
.nodes
[0];
102 ui::AXNodeData
& child
= event
.update
.nodes
[1];
104 // Always include the root of the tree, the document. It always has id 1.
106 root
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
108 (1 << ui::AX_STATE_READ_ONLY
) |
109 (1 << ui::AX_STATE_FOCUSABLE
);
111 root
.state
|= (1 << ui::AX_STATE_FOCUSED
);
112 root
.location
= gfx::Rect(render_view_
->size());
113 root
.child_ids
.push_back(next_id_
);
116 child
.role
= ui::AX_ROLE_GROUP
;
118 if (!node
.isNull() && node
.isElementNode()) {
119 child
.location
= gfx::Rect(
120 const_cast<WebNode
&>(node
).to
<WebElement
>().boundsInViewportSpace());
121 } else if (render_view_
->HasIMETextFocus()) {
122 child
.location
= root
.location
;
124 child
.location
= gfx::Rect();
127 if (node_has_focus
) {
129 (1 << ui::AX_STATE_FOCUSABLE
) |
130 (1 << ui::AX_STATE_FOCUSED
);
131 if (!node_is_editable_text
)
132 child
.state
|= (1 << ui::AX_STATE_READ_ONLY
);
138 VLOG(0) << "Accessibility update: \n"
139 << "routing id=" << routing_id()
141 << AccessibilityEventToString(event.event_type)
142 << "\n" << event.nodes[0].DebugString(true);
147 Send(new AccessibilityHostMsg_Events(routing_id(), events
));
149 // Increment the id, wrap back when we get past a million.
151 if (next_id_
> 1000000)
152 next_id_
= kInitialId
;
155 } // namespace content