forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccessibility_controller.cc
340 lines (284 loc) · 11.8 KB
/
accessibility_controller.cc
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/web_test/renderer/accessibility_controller.h"
#include "content/web_test/renderer/web_frame_test_proxy.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
#include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h"
#include "third_party/blink/public/web/web_ax_context.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_view.h"
#include "ui/accessibility/ax_mode.h"
namespace content {
class AccessibilityControllerBindings
: public gin::Wrappable<AccessibilityControllerBindings> {
public:
static gin::WrapperInfo kWrapperInfo;
AccessibilityControllerBindings(const AccessibilityControllerBindings&) =
delete;
AccessibilityControllerBindings& operator=(
const AccessibilityControllerBindings&) = delete;
static void Install(base::WeakPtr<AccessibilityController> controller,
blink::WebLocalFrame* frame);
private:
explicit AccessibilityControllerBindings(
base::WeakPtr<AccessibilityController> controller);
~AccessibilityControllerBindings() override;
// gin::Wrappable:
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
void LogAccessibilityEvents();
void SetNotificationListener(v8::Local<v8::Function> callback);
void UnsetNotificationListener();
v8::Local<v8::Object> FocusedElement();
v8::Local<v8::Object> RootElement();
v8::Local<v8::Object> AccessibleElementById(const std::string& id);
void Reset();
base::WeakPtr<AccessibilityController> controller_;
};
gin::WrapperInfo AccessibilityControllerBindings::kWrapperInfo = {
gin::kEmbedderNativeGin};
// static
void AccessibilityControllerBindings::Install(
base::WeakPtr<AccessibilityController> controller,
blink::WebLocalFrame* frame) {
v8::Isolate* isolate = frame->GetAgentGroupScheduler()->Isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = frame->MainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
gin::Handle<AccessibilityControllerBindings> bindings = gin::CreateHandle(
isolate, new AccessibilityControllerBindings(controller));
if (bindings.IsEmpty())
return;
v8::Local<v8::Object> global = context->Global();
global
->Set(context, gin::StringToV8(isolate, "accessibilityController"),
bindings.ToV8())
.Check();
}
AccessibilityControllerBindings::AccessibilityControllerBindings(
base::WeakPtr<AccessibilityController> controller)
: controller_(controller) {}
AccessibilityControllerBindings::~AccessibilityControllerBindings() {}
gin::ObjectTemplateBuilder
AccessibilityControllerBindings::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<
AccessibilityControllerBindings>::GetObjectTemplateBuilder(isolate)
.SetMethod("logAccessibilityEvents",
&AccessibilityControllerBindings::LogAccessibilityEvents)
.SetMethod("setNotificationListener",
&AccessibilityControllerBindings::SetNotificationListener)
.SetMethod("unsetNotificationListener",
&AccessibilityControllerBindings::UnsetNotificationListener)
.SetProperty("focusedElement",
&AccessibilityControllerBindings::FocusedElement)
.SetProperty("rootElement", &AccessibilityControllerBindings::RootElement)
.SetMethod("accessibleElementById",
&AccessibilityControllerBindings::AccessibleElementById)
// TODO(hajimehoshi): These are for backward compatibility. Remove them.
.SetMethod("addNotificationListener",
&AccessibilityControllerBindings::SetNotificationListener)
.SetMethod("removeNotificationListener",
&AccessibilityControllerBindings::UnsetNotificationListener)
.SetMethod("reset", &AccessibilityControllerBindings::Reset);
}
void AccessibilityControllerBindings::LogAccessibilityEvents() {
if (controller_)
controller_->LogAccessibilityEvents();
}
void AccessibilityControllerBindings::SetNotificationListener(
v8::Local<v8::Function> callback) {
if (controller_)
controller_->SetNotificationListener(callback);
}
void AccessibilityControllerBindings::UnsetNotificationListener() {
if (controller_)
controller_->UnsetNotificationListener();
}
v8::Local<v8::Object> AccessibilityControllerBindings::FocusedElement() {
return controller_ ? controller_->FocusedElement() : v8::Local<v8::Object>();
}
v8::Local<v8::Object> AccessibilityControllerBindings::RootElement() {
return controller_ ? controller_->RootElement() : v8::Local<v8::Object>();
}
v8::Local<v8::Object> AccessibilityControllerBindings::AccessibleElementById(
const std::string& id) {
return controller_ ? controller_->AccessibleElementById(id)
: v8::Local<v8::Object>();
}
void AccessibilityControllerBindings::Reset() {
if (controller_)
controller_->Reset();
}
AccessibilityController::AccessibilityController(
WebFrameTestProxy* web_frame_test_proxy)
: log_accessibility_events_(false),
web_frame_test_proxy_(web_frame_test_proxy) {}
AccessibilityController::~AccessibilityController() {
// v8::Persistent will leak on destroy, due to the default
// NonCopyablePersistentTraits (it claims this may change in the future).
notification_callback_.Reset();
}
void AccessibilityController::Reset() {
if (!IsInstalled())
return;
elements_->Clear();
notification_callback_.Reset();
log_accessibility_events_ = false;
ax_context_.reset();
}
void AccessibilityController::Install(blink::WebLocalFrame* frame) {
ax_context_ = std::make_unique<blink::WebAXContext>(frame->GetDocument(),
ui::kAXModeComplete);
elements_ = std::make_unique<WebAXObjectProxyList>(
frame->GetAgentGroupScheduler()->Isolate(), *ax_context_);
AccessibilityControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
}
bool AccessibilityController::ShouldLogAccessibilityEvents() {
return log_accessibility_events_;
}
void AccessibilityController::NotificationReceived(
blink::WebLocalFrame* frame,
const blink::WebAXObject& target,
const std::string& notification_name,
const std::vector<ui::AXEventIntent>& event_intents) {
frame->GetTaskRunner(blink::TaskType::kInternalTest)
->PostTask(FROM_HERE,
base::BindOnce(&AccessibilityController::PostNotification,
weak_factory_.GetWeakPtr(), target,
notification_name, event_intents));
}
void AccessibilityController::PostNotification(
const blink::WebAXObject& target,
const std::string& notification_name,
const std::vector<ui::AXEventIntent>& event_intents) {
if (!IsInstalled())
return;
blink::WebFrame* frame = web_view()->MainFrame();
if (!frame || frame->IsWebRemoteFrame())
return;
blink::WebLocalFrame* local_frame = frame->ToWebLocalFrame();
v8::Isolate* isolate = local_frame->GetAgentGroupScheduler()->Isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = local_frame->MainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
// Call notification listeners on the element.
v8::Local<v8::Object> element_handle = elements_->GetOrCreate(target);
if (element_handle.IsEmpty())
return;
WebAXObjectProxy* element;
bool result = gin::ConvertFromV8(isolate, element_handle, &element);
DCHECK(result);
element->NotificationReceived(local_frame, notification_name, event_intents);
if (notification_callback_.IsEmpty())
return;
// Call global notification listeners.
v8::Local<v8::Value> argv[] = {
element_handle,
v8::String::NewFromUtf8(isolate, notification_name.data(),
v8::NewStringType::kNormal,
notification_name.size())
.ToLocalChecked(),
};
local_frame->CallFunctionEvenIfScriptDisabled(
v8::Local<v8::Function>::New(isolate, notification_callback_),
context->Global(), std::size(argv), argv);
}
void AccessibilityController::LogAccessibilityEvents() {
log_accessibility_events_ = true;
}
void AccessibilityController::SetNotificationListener(
v8::Local<v8::Function> callback) {
blink::WebFrame* frame = web_view()->MainFrame();
if (!frame || frame->IsWebRemoteFrame()) {
return;
}
blink::WebLocalFrame* local_frame = frame->ToWebLocalFrame();
v8::Isolate* isolate = local_frame->GetAgentGroupScheduler()->Isolate();
notification_callback_.Reset(isolate, callback);
}
void AccessibilityController::UnsetNotificationListener() {
notification_callback_.Reset();
}
v8::Local<v8::Object> AccessibilityController::FocusedElement() {
blink::WebFrame* frame = web_view()->MainFrame();
if (!frame || !IsInstalled())
return v8::Local<v8::Object>();
// TODO(lukasza): Finish adding OOPIF support to the web tests harness.
CHECK(frame->IsWebLocalFrame())
<< "This function cannot be called if the main frame is not a "
"local frame.";
ax_context_->UpdateAXForAllDocuments();
blink::WebAXObject focused_element =
blink::WebAXObject::FromWebDocumentFocused(
frame->ToWebLocalFrame()->GetDocument());
if (focused_element.IsNull())
focused_element = GetAccessibilityObjectForMainFrame();
return elements_->GetOrCreate(focused_element);
}
v8::Local<v8::Object> AccessibilityController::RootElement() {
if (!IsInstalled())
return v8::Local<v8::Object>();
ax_context_->UpdateAXForAllDocuments();
return elements_->GetOrCreate(GetAccessibilityObjectForMainFrame());
}
v8::Local<v8::Object> AccessibilityController::AccessibleElementById(
const std::string& id) {
if (!IsInstalled())
return v8::Local<v8::Object>();
ax_context_->UpdateAXForAllDocuments();
blink::WebAXObject root_element = GetAccessibilityObjectForMainFrame();
return FindAccessibleElementByIdRecursive(
root_element, blink::WebString::FromUTF8(id.c_str()));
}
v8::Local<v8::Object>
AccessibilityController::FindAccessibleElementByIdRecursive(
const blink::WebAXObject& obj,
const blink::WebString& id) {
if (obj.IsNull() || obj.IsDetached())
return v8::Local<v8::Object>();
blink::WebNode node = obj.GetNode();
if (!node.IsNull() && node.IsElementNode()) {
blink::WebElement element = node.To<blink::WebElement>();
if (element.GetAttribute("id") == id)
return elements_->GetOrCreate(obj);
}
unsigned childCount = obj.ChildCount();
for (unsigned i = 0; i < childCount; i++) {
v8::Local<v8::Object> result =
FindAccessibleElementByIdRecursive(obj.ChildAt(i), id);
if (!result.IsEmpty())
return result;
}
return v8::Local<v8::Object>();
}
blink::WebView* AccessibilityController::web_view() const {
return web_frame_test_proxy_->GetWebFrame()->View();
}
blink::WebAXObject AccessibilityController::GetAccessibilityObjectForMainFrame()
const {
blink::WebFrame* frame = web_view()->MainFrame();
// TODO(lukasza): Finish adding OOPIF support to the web tests harness.
CHECK(frame && frame->IsWebLocalFrame())
<< "This function cannot be called if the main frame is not a "
"local frame.";
return blink::WebAXObject::FromWebDocument(
web_view()->MainFrame()->ToWebLocalFrame()->GetDocument());
}
void AccessibilityController::Remove(unsigned axid) {
if (IsInstalled()) {
elements_->Remove(axid);
}
}
} // namespace content