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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
// Copyright 2014 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/accessibility_messages.h"
namespace content {
ui::AXTreeUpdate MakeAXTreeUpdate(
const ui::AXNodeData& node1,
const ui::AXNodeData& node2 /* = ui::AXNodeData() */,
const ui::AXNodeData& node3 /* = ui::AXNodeData() */,
const ui::AXNodeData& node4 /* = ui::AXNodeData() */,
const ui::AXNodeData& node5 /* = ui::AXNodeData() */,
const ui::AXNodeData& node6 /* = ui::AXNodeData() */,
const ui::AXNodeData& node7 /* = ui::AXNodeData() */,
const ui::AXNodeData& node8 /* = ui::AXNodeData() */,
const ui::AXNodeData& node9 /* = ui::AXNodeData() */) {
CR_DEFINE_STATIC_LOCAL(ui::AXNodeData, empty_data, ());
int32 no_id = empty_data.id;
ui::AXTreeUpdate update;
update.nodes.push_back(node1);
if (node2.id != no_id)
update.nodes.push_back(node2);
if (node3.id != no_id)
update.nodes.push_back(node3);
if (node4.id != no_id)
update.nodes.push_back(node4);
if (node5.id != no_id)
update.nodes.push_back(node5);
if (node6.id != no_id)
update.nodes.push_back(node6);
if (node7.id != no_id)
update.nodes.push_back(node7);
if (node8.id != no_id)
update.nodes.push_back(node8);
if (node9.id != no_id)
update.nodes.push_back(node9);
return update;
}
BrowserAccessibility* BrowserAccessibilityFactory::Create() {
return BrowserAccessibility::Create();
}
#if !defined(OS_MACOSX) && \
!defined(OS_WIN) && \
!defined(OS_ANDROID) \
// We have subclassess of BrowserAccessibilityManager on Mac, Win, and Android.
// These are the default implementations of these functions
// static
BrowserAccessibilityManager* BrowserAccessibilityManager::Create(
const ui::AXTreeUpdate& initial_tree,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory) {
return new BrowserAccessibilityManager(initial_tree, delegate, factory);
}
// static
ui::AXTreeUpdate BrowserAccessibilityManager::GetEmptyDocument() {
ui::AXNodeData empty_document;
empty_document.id = 0;
empty_document.role = ui::AX_ROLE_ROOT_WEB_AREA;
ui::AXTreeUpdate update;
update.nodes.push_back(empty_document);
return update;
}
#endif
BrowserAccessibilityManager::BrowserAccessibilityManager(
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory)
: delegate_(delegate),
factory_(factory),
tree_(new ui::AXTree()),
focus_(NULL),
osk_state_(OSK_ALLOWED) {
tree_->SetDelegate(this);
}
BrowserAccessibilityManager::BrowserAccessibilityManager(
const ui::AXTreeUpdate& initial_tree,
BrowserAccessibilityDelegate* delegate,
BrowserAccessibilityFactory* factory)
: delegate_(delegate),
factory_(factory),
tree_(new ui::AXTree()),
focus_(NULL),
osk_state_(OSK_ALLOWED) {
tree_->SetDelegate(this);
Initialize(initial_tree);
}
BrowserAccessibilityManager::~BrowserAccessibilityManager() {
tree_.reset(NULL);
}
void BrowserAccessibilityManager::Initialize(
const ui::AXTreeUpdate& initial_tree) {
if (!tree_->Unserialize(initial_tree)) {
if (delegate_) {
LOG(ERROR) << tree_->error();
delegate_->AccessibilityFatalError();
} else {
LOG(FATAL) << tree_->error();
}
}
if (!focus_)
SetFocus(tree_->GetRoot(), false);
}
BrowserAccessibility* BrowserAccessibilityManager::GetRoot() {
return GetFromAXNode(tree_->GetRoot());
}
BrowserAccessibility* BrowserAccessibilityManager::GetFromAXNode(
ui::AXNode* node) {
return GetFromID(node->id());
}
BrowserAccessibility* BrowserAccessibilityManager::GetFromID(int32 id) {
base::hash_map<int32, BrowserAccessibility*>::iterator iter =
id_wrapper_map_.find(id);
if (iter != id_wrapper_map_.end())
return iter->second;
return NULL;
}
void BrowserAccessibilityManager::OnWindowFocused() {
if (focus_)
NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, GetFromAXNode(focus_));
}
void BrowserAccessibilityManager::OnWindowBlurred() {
if (focus_)
NotifyAccessibilityEvent(ui::AX_EVENT_BLUR, GetFromAXNode(focus_));
}
void BrowserAccessibilityManager::OnNavigation(bool is_reload) {
// Exit if we don't even have the first document loaded yet.
if (GetRoot()->GetId() == 0)
return;
LOG(ERROR) << "AX: BrowserAccessibilityManager::OnNavigation " << is_reload;
// Create an update that replaces the current tree with an empty document
// (which should be in the "busy" state by default) and apply it.
ui::AXTreeUpdate update = GetEmptyDocument();
LOG(ERROR) << "AX: OnNavigation empty doc update state: "
<< update.nodes[0].state;
update.nodes[0].id = GetRoot()->GetId();
LOG(ERROR) << "AX: State before unserializing the empty doc: "
<< GetRoot()->GetState();
LOG(ERROR) << "AX: Root id before unserializing the empty doc: "
<< GetRoot()->GetId();
LOG(ERROR) << "AX: Root children before: "
<< GetRoot()->PlatformChildCount();
LOG(ERROR) << "AX: State of first node in update: "
<< update.nodes[0].state;
CHECK(tree_->Unserialize(update));
LOG(ERROR) << "AX: State after unserializing the empty doc: "
<< GetRoot()->GetState();
LOG(ERROR) << "AX: Root id after unserializing the empty doc: "
<< GetRoot()->GetId();
LOG(ERROR) << "AX: Root children after: "
<< GetRoot()->PlatformChildCount();
}
void BrowserAccessibilityManager::GotMouseDown() {
osk_state_ = OSK_ALLOWED_WITHIN_FOCUSED_OBJECT;
NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, GetFromAXNode(focus_));
}
bool BrowserAccessibilityManager::UseRootScrollOffsetsWhenComputingBounds() {
return true;
}
void BrowserAccessibilityManager::OnAccessibilityEvents(
const std::vector<AccessibilityHostMsg_EventParams>& params) {
bool should_send_initial_focus = false;
// Process all changes to the accessibility tree first.
for (uint32 index = 0; index < params.size(); index++) {
const AccessibilityHostMsg_EventParams& param = params[index];
if (!tree_->Unserialize(param.update)) {
if (delegate_) {
LOG(ERROR) << tree_->error();
delegate_->AccessibilityFatalError();
} else {
CHECK(false) << tree_->error();
}
return;
}
// Set focus to the root if it's not anywhere else.
if (!focus_) {
SetFocus(tree_->GetRoot(), false);
should_send_initial_focus = true;
}
}
OnTreeUpdateFinished();
if (should_send_initial_focus &&
(!delegate_ || delegate_->AccessibilityViewHasFocus())) {
NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, GetFromAXNode(focus_));
}
// Now iterate over the events again and fire the events.
for (uint32 index = 0; index < params.size(); index++) {
const AccessibilityHostMsg_EventParams& param = params[index];
// Find the node corresponding to the id that's the target of the
// event (which may not be the root of the update tree).
ui::AXNode* node = tree_->GetFromId(param.id);
if (!node)
continue;
ui::AXEvent event_type = param.event_type;
if (event_type == ui::AX_EVENT_FOCUS ||
event_type == ui::AX_EVENT_BLUR) {
SetFocus(node, false);
if (osk_state_ != OSK_DISALLOWED_BECAUSE_TAB_HIDDEN &&
osk_state_ != OSK_DISALLOWED_BECAUSE_TAB_JUST_APPEARED)
osk_state_ = OSK_ALLOWED;
// Don't send a native focus event if the window itself doesn't
// have focus.
if (delegate_ && !delegate_->AccessibilityViewHasFocus())
continue;
}
// Send the event event to the operating system.
NotifyAccessibilityEvent(event_type, GetFromAXNode(node));
}
}
void BrowserAccessibilityManager::OnLocationChanges(
const std::vector<AccessibilityHostMsg_LocationChangeParams>& params) {
for (size_t i = 0; i < params.size(); ++i) {
BrowserAccessibility* obj = GetFromID(params[i].id);
if (!obj)
continue;
ui::AXNode* node = obj->node();
node->SetLocation(params[i].new_location);
obj->OnLocationChanged();
}
}
BrowserAccessibility* BrowserAccessibilityManager::GetActiveDescendantFocus(
BrowserAccessibility* root) {
BrowserAccessibility* node = BrowserAccessibilityManager::GetFocus(root);
if (!node)
return NULL;
int active_descendant_id;
if (node->GetIntAttribute(ui::AX_ATTR_ACTIVEDESCENDANT_ID,
&active_descendant_id)) {
BrowserAccessibility* active_descendant =
node->manager()->GetFromID(active_descendant_id);
if (active_descendant)
return active_descendant;
}
return node;
}
BrowserAccessibility* BrowserAccessibilityManager::GetFocus(
BrowserAccessibility* root) {
if (focus_ && (!root || focus_->IsDescendantOf(root->node())))
return GetFromAXNode(focus_);
return NULL;
}
void BrowserAccessibilityManager::SetFocus(ui::AXNode* node, bool notify) {
if (focus_ != node)
focus_ = node;
if (notify && node && delegate_)
delegate_->AccessibilitySetFocus(node->id());
}
void BrowserAccessibilityManager::SetFocus(
BrowserAccessibility* obj, bool notify) {
if (obj->node())
SetFocus(obj->node(), notify);
}
void BrowserAccessibilityManager::DoDefaultAction(
const BrowserAccessibility& node) {
if (delegate_)
delegate_->AccessibilityDoDefaultAction(node.GetId());
}
void BrowserAccessibilityManager::ScrollToMakeVisible(
const BrowserAccessibility& node, gfx::Rect subfocus) {
if (delegate_) {
delegate_->AccessibilityScrollToMakeVisible(node.GetId(), subfocus);
}
}
void BrowserAccessibilityManager::ScrollToPoint(
const BrowserAccessibility& node, gfx::Point point) {
if (delegate_) {
delegate_->AccessibilityScrollToPoint(node.GetId(), point);
}
}
void BrowserAccessibilityManager::SetTextSelection(
const BrowserAccessibility& node, int start_offset, int end_offset) {
if (delegate_) {
delegate_->AccessibilitySetTextSelection(
node.GetId(), start_offset, end_offset);
}
}
gfx::Rect BrowserAccessibilityManager::GetViewBounds() {
if (delegate_)
return delegate_->AccessibilityGetViewBounds();
return gfx::Rect();
}
BrowserAccessibility* BrowserAccessibilityManager::NextInTreeOrder(
BrowserAccessibility* node) {
if (!node)
return NULL;
if (node->PlatformChildCount() > 0)
return node->PlatformGetChild(0);
while (node) {
if (node->GetParent() &&
node->GetIndexInParent() <
static_cast<int>(node->GetParent()->PlatformChildCount()) - 1) {
return node->GetParent()->PlatformGetChild(node->GetIndexInParent() + 1);
}
node = node->GetParent();
}
return NULL;
}
BrowserAccessibility* BrowserAccessibilityManager::PreviousInTreeOrder(
BrowserAccessibility* node) {
if (!node)
return NULL;
if (node->GetParent() && node->GetIndexInParent() > 0) {
node = node->GetParent()->PlatformGetChild(node->GetIndexInParent() - 1);
while (node->PlatformChildCount() > 0)
node = node->PlatformGetChild(node->PlatformChildCount() - 1);
return node;
}
return node->GetParent();
}
void BrowserAccessibilityManager::OnNodeWillBeDeleted(ui::AXNode* node) {
if (node == focus_ && tree_) {
if (node != tree_->GetRoot())
SetFocus(tree_->GetRoot(), false);
else
focus_ = NULL;
}
if (id_wrapper_map_.find(node->id()) == id_wrapper_map_.end())
return;
GetFromAXNode(node)->Destroy();
id_wrapper_map_.erase(node->id());
}
void BrowserAccessibilityManager::OnNodeCreated(ui::AXNode* node) {
BrowserAccessibility* wrapper = factory_->Create();
wrapper->Init(this, node);
id_wrapper_map_[node->id()] = wrapper;
wrapper->OnDataChanged();
}
void BrowserAccessibilityManager::OnNodeChanged(ui::AXNode* node) {
GetFromAXNode(node)->OnDataChanged();
}
void BrowserAccessibilityManager::OnNodeCreationFinished(ui::AXNode* node) {
GetFromAXNode(node)->OnUpdateFinished();
}
void BrowserAccessibilityManager::OnNodeChangeFinished(ui::AXNode* node) {
GetFromAXNode(node)->OnUpdateFinished();
}
} // namespace content
|