1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/accessibility/browser_accessibility_manager.h"
#include "base/logging.h"
#include "content/browser/accessibility/browser_accessibility.h"
#include "content/common/view_messages.h"
using webkit_glue::WebAccessibility;
BrowserAccessibility* BrowserAccessibilityFactory::Create() {
return BrowserAccessibility::Create();
}
// Start child IDs at -1 and decrement each time, because clients use
// child IDs of 1, 2, 3, ... to access the children of an object by
// index, so we use negative IDs to clearly distinguish between indices
// and unique IDs.
// static
int32 BrowserAccessibilityManager::next_child_id_ = -1;
#if (defined(OS_POSIX) && !defined(OS_MACOSX)) || defined(USE_AURA)
// There's no OS-specific implementation of BrowserAccessibilityManager
// on Unix, so just instantiate the base class.
// static
BrowserAccessibilityManager* BrowserAccessibilityManager::Create(
gfx::NativeView parent_view,
const WebAccessibility& src,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory) {
return new BrowserAccessibilityManager(
parent_view, src, delegate, factory);
}
#endif
// static
BrowserAccessibilityManager* BrowserAccessibilityManager::CreateEmptyDocument(
gfx::NativeView parent_view,
WebAccessibility::State state,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory) {
// Use empty document to process notifications
webkit_glue::WebAccessibility empty_document;
empty_document.id = 0;
empty_document.role = WebAccessibility::ROLE_ROOT_WEB_AREA;
empty_document.state = state;
return BrowserAccessibilityManager::Create(
parent_view, empty_document, delegate, factory);
}
BrowserAccessibilityManager::BrowserAccessibilityManager(
gfx::NativeView parent_view,
const WebAccessibility& src,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory)
: parent_view_(parent_view),
delegate_(delegate),
factory_(factory),
focus_(NULL) {
root_ = CreateAccessibilityTree(NULL, src, 0, false);
if (!focus_)
SetFocus(root_, false);
}
// static
int32 BrowserAccessibilityManager::GetNextChildID() {
// Get the next child ID, and wrap around when we get near the end
// of a 32-bit integer range. It's okay to wrap around; we just want
// to avoid it as long as possible because clients may cache the ID of
// an object for a while to determine if they've seen it before.
next_child_id_--;
if (next_child_id_ == -2000000000)
next_child_id_ = -1;
return next_child_id_;
}
BrowserAccessibilityManager::~BrowserAccessibilityManager() {
// Clients could still hold references to some nodes of the tree, so
// calling InternalReleaseReference will make sure that as many nodes
// as possible are released now, and remaining nodes are marked as
// inactive so that calls to any methods on them will fail gracefully.
focus_->InternalReleaseReference(false);
root_->InternalReleaseReference(true);
}
BrowserAccessibility* BrowserAccessibilityManager::GetRoot() {
return root_;
}
BrowserAccessibility* BrowserAccessibilityManager::GetFromChildID(
int32 child_id) {
base::hash_map<int32, BrowserAccessibility*>::iterator iter =
child_id_map_.find(child_id);
if (iter != child_id_map_.end()) {
return iter->second;
} else {
return NULL;
}
}
BrowserAccessibility* BrowserAccessibilityManager::GetFromRendererID(
int32 renderer_id) {
base::hash_map<int32, int32>::iterator iter =
renderer_id_to_child_id_map_.find(renderer_id);
if (iter == renderer_id_to_child_id_map_.end())
return NULL;
int32 child_id = iter->second;
return GetFromChildID(child_id);
}
void BrowserAccessibilityManager::GotFocus() {
if (!focus_)
return;
NotifyAccessibilityEvent(ViewHostMsg_AccEvent::FOCUS_CHANGED, focus_);
}
void BrowserAccessibilityManager::Remove(int32 child_id, int32 renderer_id) {
child_id_map_.erase(child_id);
// TODO(ctguil): Investigate if hit. We should never have a newer entry.
DCHECK(renderer_id_to_child_id_map_[renderer_id] == child_id);
// Make sure we don't overwrite a newer entry (see UpdateNode for a possible
// corner case).
if (renderer_id_to_child_id_map_[renderer_id] == child_id)
renderer_id_to_child_id_map_.erase(renderer_id);
}
void BrowserAccessibilityManager::OnAccessibilityNotifications(
const std::vector<ViewHostMsg_AccessibilityNotification_Params>& params) {
for (uint32 index = 0; index < params.size(); index++) {
const ViewHostMsg_AccessibilityNotification_Params& param = params[index];
// Update the tree.
UpdateNode(param.acc_tree, param.includes_children);
// Find the node corresponding to the id that's the target of the
// notification (which may not be the root of the update tree).
base::hash_map<int32, int32>::iterator iter =
renderer_id_to_child_id_map_.find(param.id);
if (iter == renderer_id_to_child_id_map_.end()) {
continue;
}
int32 child_id = iter->second;
BrowserAccessibility* node = GetFromChildID(child_id);
if (!node) {
NOTREACHED();
continue;
}
if (param.notification_type == ViewHostMsg_AccEvent::FOCUS_CHANGED) {
SetFocus(node, false);
// Don't send a native focus event if the window itself doesn't
// have focus.
if (delegate_ && !delegate_->HasFocus())
continue;
}
// Send the notification event to the operating system.
NotifyAccessibilityEvent(param.notification_type, node);
// Set initial focus when a page is loaded.
if (param.notification_type == ViewHostMsg_AccEvent::LOAD_COMPLETE) {
if (!focus_)
SetFocus(root_, false);
if (!delegate_ || delegate_->HasFocus())
NotifyAccessibilityEvent(ViewHostMsg_AccEvent::FOCUS_CHANGED, focus_);
}
}
}
gfx::NativeView BrowserAccessibilityManager::GetParentView() {
return parent_view_;
}
BrowserAccessibility* BrowserAccessibilityManager::GetFocus(
BrowserAccessibility* root) {
if (focus_ && (!root || focus_->IsDescendantOf(root)))
return focus_;
return NULL;
}
void BrowserAccessibilityManager::SetFocus(
BrowserAccessibility* node, bool notify) {
if (focus_ != node) {
if (focus_)
focus_->InternalReleaseReference(false);
focus_ = node;
if (focus_)
focus_->InternalAddReference();
}
if (notify && node && delegate_)
delegate_->SetAccessibilityFocus(node->renderer_id());
}
void BrowserAccessibilityManager::DoDefaultAction(
const BrowserAccessibility& node) {
if (delegate_)
delegate_->AccessibilityDoDefaultAction(node.renderer_id());
}
void BrowserAccessibilityManager::ChangeScrollPosition(
const BrowserAccessibility& node, int scroll_x, int scroll_y) {
if (delegate_) {
delegate_->AccessibilityChangeScrollPosition(
node.renderer_id(), scroll_x, scroll_y);
}
}
void BrowserAccessibilityManager::SetTextSelection(
const BrowserAccessibility& node, int start_offset, int end_offset) {
if (delegate_) {
delegate_->AccessibilitySetTextSelection(
node.renderer_id(), start_offset, end_offset);
}
}
gfx::Rect BrowserAccessibilityManager::GetViewBounds() {
if (delegate_)
return delegate_->GetViewBounds();
return gfx::Rect();
}
void BrowserAccessibilityManager::UpdateNode(
const WebAccessibility& src,
bool include_children) {
BrowserAccessibility* current = NULL;
// Look for the node to replace. Either we're replacing the whole tree
// (role is ROOT_WEB_AREA) or we look it up based on its renderer ID.
if (src.role == WebAccessibility::ROLE_ROOT_WEB_AREA) {
current = root_;
} else {
base::hash_map<int32, int32>::iterator iter =
renderer_id_to_child_id_map_.find(src.id);
if (iter != renderer_id_to_child_id_map_.end()) {
int32 child_id = iter->second;
current = GetFromChildID(child_id);
}
}
// If we can't find the node to replace, we're out of sync with the
// renderer (this would be a bug).
DCHECK(current);
if (!current)
return;
// If this update is just for a single node (|include_children| is false),
// modify |current| directly and return - no tree changes are needed.
if (!include_children) {
DCHECK_EQ(0U, src.children.size());
current->PreInitialize(
this,
current->parent(),
current->child_id(),
current->index_in_parent(),
src);
current->PostInitialize();
return;
}
BrowserAccessibility* current_parent = current->parent();
int current_index_in_parent = current->index_in_parent();
// Detach all of the nodes in the old tree and get a single flat vector
// of all node pointers.
std::vector<BrowserAccessibility*> old_tree_nodes;
current->DetachTree(&old_tree_nodes);
// Build a new tree, reusing old nodes if possible. Each node that's
// reused will have its reference count incremented by one.
current = CreateAccessibilityTree(
current_parent, src, current_index_in_parent, true);
// Decrement the reference count of all nodes in the old tree, which will
// delete any nodes no longer needed.
for (int i = 0; i < static_cast<int>(old_tree_nodes.size()); i++)
old_tree_nodes[i]->InternalReleaseReference(false);
// If the only reference to the focused node is focus_ itself, then the
// focused node is no longer in the tree, so set the focus to the root.
if (focus_ && focus_->ref_count() == 1) {
SetFocus(root_, true);
if (!delegate_ || delegate_->HasFocus())
NotifyAccessibilityEvent(ViewHostMsg_AccEvent::FOCUS_CHANGED, focus_);
}
}
BrowserAccessibility* BrowserAccessibilityManager::CreateAccessibilityTree(
BrowserAccessibility* parent,
const WebAccessibility& src,
int index_in_parent,
bool send_show_events) {
BrowserAccessibility* instance = NULL;
int32 child_id = 0;
bool children_can_send_show_events = send_show_events;
base::hash_map<int32, int32>::iterator iter =
renderer_id_to_child_id_map_.find(src.id);
// If a BrowserAccessibility instance for this ID already exists, add a
// new reference to it and retrieve its children vector.
if (iter != renderer_id_to_child_id_map_.end()) {
child_id = iter->second;
instance = GetFromChildID(child_id);
}
// If the node has changed roles, don't reuse a BrowserAccessibility
// object, that could confuse a screen reader.
// TODO(dtseng): Investigate when this gets hit; See crbug.com/93095.
DCHECK(!instance || instance->role() == src.role);
// If we're reusing a node, it should already be detached from a parent
// and any children. If not, that means we have a serious bug somewhere,
// like the same child is reachable from two places in the same tree.
if (instance && (instance->parent() != NULL || instance->child_count() > 0)) {
NOTREACHED();
instance = NULL;
}
if (instance) {
// If we're reusing a node, update its parent and increment its
// reference count.
instance->UpdateParent(parent, index_in_parent);
instance->InternalAddReference();
send_show_events = false;
} else {
// Otherwise, create a new instance.
instance = factory_->Create();
child_id = GetNextChildID();
children_can_send_show_events = false;
}
instance->PreInitialize(this, parent, child_id, index_in_parent, src);
child_id_map_[child_id] = instance;
renderer_id_to_child_id_map_[src.id] = child_id;
if ((src.state >> WebAccessibility::STATE_FOCUSED) & 1)
SetFocus(instance, false);
for (int i = 0; i < static_cast<int>(src.children.size()); ++i) {
BrowserAccessibility* child = CreateAccessibilityTree(
instance, src.children[i], i, children_can_send_show_events);
instance->AddChild(child);
}
if (src.role == WebAccessibility::ROLE_ROOT_WEB_AREA)
root_ = instance;
// Note: the purpose of send_show_events and children_can_send_show_events
// is so that we send a single OBJECT_SHOW event for the root of a subtree
// that just appeared for the first time, but not on any descendant of
// that subtree.
if (send_show_events)
NotifyAccessibilityEvent(ViewHostMsg_AccEvent::OBJECT_SHOW, instance);
instance->PostInitialize();
return instance;
}
|