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 #ifndef CONTENT_RENDERER_RENDERER_ACCESSIBILITY_H_
6 #define CONTENT_RENDERER_RENDERER_ACCESSIBILITY_H_
11 #include "base/hash_tables.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/public/renderer/render_view_observer.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAccessibilityNotification.h"
19 class WebAccessibilityObject
;
24 namespace webkit_glue
{
25 struct WebAccessibility
;
28 // RendererAccessibility belongs to the RenderView. It's responsible for
29 // sending a serialized representation of WebKit's accessibility tree from
30 // the renderer to the browser and sending updates whenever it changes, and
31 // handling requests from the browser to perform accessibility actions on
32 // nodes in the tree (e.g., change focus, or click on a button).
33 class RendererAccessibility
: public content::RenderViewObserver
{
35 RendererAccessibility(RenderViewImpl
* render_view
);
36 virtual ~RendererAccessibility();
38 // RenderView::Observer implementation.
39 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
40 virtual void FocusedNodeChanged(const WebKit::WebNode
& node
) OVERRIDE
;
41 virtual void DidFinishLoad(WebKit::WebFrame
* frame
) OVERRIDE
;
43 // Called when an accessibility notification occurs in WebKit.
44 virtual void PostAccessibilityNotification(
45 const WebKit::WebAccessibilityObject
& obj
,
46 WebKit::WebAccessibilityNotification notification
);
49 // One accessibility notification from WebKit. These are queued up and
50 // used to send tree updates and notification messages from the
51 // renderer to the browser.
54 // The id of the accessibility object.
57 // The accessibility notification type.
58 WebKit::WebAccessibilityNotification type
;
61 // In order to keep track of what nodes the browser knows about, we keep a
62 // representation of the browser tree - just IDs and parent/child
64 struct BrowserTreeNode
{
65 BrowserTreeNode() : id(0) {}
68 std::vector
<BrowserTreeNode
*> children
;
71 // Send queued notifications from the renderer to the browser.
72 void SendPendingAccessibilityNotifications();
74 // Update our representation of what nodes the browser has, given a
76 void UpdateBrowserTree(const webkit_glue::WebAccessibility
& renderer_node
);
78 // Clear the given node and recursively delete all of its descendants
79 // from the browser tree. (Does not delete |browser_node|).
80 void ClearBrowserTreeNode(BrowserTreeNode
* browser_node
);
82 // Handlers for messages from the browser to the renderer.
83 void OnDoDefaultAction(int acc_obj_id
);
84 void OnNotificationsAck();
85 void OnChangeScrollPosition(int acc_obj_id
, int scroll_x
, int scroll_y
);
86 void OnScrollToMakeVisible(int acc_obj_id
, gfx::Rect subfocus
);
87 void OnScrollToPoint(int acc_obj_id
, gfx::Point point
);
89 void OnSetFocus(int acc_obj_id
);
91 void OnSetTextSelection(int acc_obj_id
, int start_offset
, int end_offset
);
93 // Whether or not this notification typically needs to send
94 // updates to its children, too.
95 bool ShouldIncludeChildren(const Notification
& notification
);
97 // Returns the main top-level document for this page, or NULL if there's
99 WebKit::WebDocument
GetMainDocument();
101 // So we can queue up tasks to be executed later.
102 base::WeakPtrFactory
<RendererAccessibility
> weak_factory_
;
104 // Notifications from WebKit are collected until they are ready to be
105 // sent to the browser.
106 std::vector
<Notification
> pending_notifications_
;
108 // Our representation of the browser tree.
109 BrowserTreeNode
* browser_root_
;
111 // A map from IDs to nodes in the browser tree.
112 base::hash_map
<int32
, BrowserTreeNode
*> browser_id_map_
;
114 // The most recently observed scroll offset of the root document element.
115 // TODO(dmazzoni): remove once https://bugs.webkit.org/show_bug.cgi?id=73460
117 gfx::Size last_scroll_offset_
;
119 // Set if we are waiting for an accessibility notification ack.
122 // True if verbose logging of accessibility events is on.
125 DISALLOW_COPY_AND_ASSIGN(RendererAccessibility
);
128 #endif // CONTENT_RENDERER_RENDERER_ACCESSIBILITY_H_