Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: content/shell/renderer/test_runner/accessibility_controller.cc

Issue 172263002: Move WebAXObjectProxy and AccessibleController from CppBoundClass to gin::Wrappable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: kouhei's review: {add/remove}NotificationCallback -> {set/unset}NotificationCallback Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #include "content/shell/renderer/test_runner/accessibility_controller.h"
6
7 #include "gin/handle.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/wrappable.h"
10 #include "third_party/WebKit/public/web/WebElement.h"
11 #include "third_party/WebKit/public/web/WebFrame.h"
12 #include "third_party/WebKit/public/web/WebKit.h"
13 #include "third_party/WebKit/public/web/WebView.h"
14
15 namespace content {
16
17 class AccessibilityControllerBindings
18 : public gin::Wrappable<AccessibilityControllerBindings> {
19 public:
20 static gin::WrapperInfo kWrapperInfo;
21
22 static void Install(base::WeakPtr<AccessibilityController> controller,
23 blink::WebFrame* frame);
24
25 private:
26 explicit AccessibilityControllerBindings(
27 base::WeakPtr<AccessibilityController> controller);
28 virtual ~AccessibilityControllerBindings();
29
30 // gin::Wrappable:
31 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
32 v8::Isolate* isolate) OVERRIDE;
33
34 void LogAccessibilityEvents();
35 void SetNotificationListener(v8::Handle<v8::Function> callback);
36 void UnsetNotificationListener();
37 v8::Handle<v8::Object> FocusedElement();
38 v8::Handle<v8::Object> RootElement();
39 v8::Handle<v8::Object> AccessibleElementById(const std::string& id);
40
41 base::WeakPtr<AccessibilityController> controller_;
42
43 DISALLOW_COPY_AND_ASSIGN(AccessibilityControllerBindings);
44 };
45
46 gin::WrapperInfo AccessibilityControllerBindings::kWrapperInfo = {
47 gin::kEmbedderNativeGin};
48
49 // static
50 void AccessibilityControllerBindings::Install(
51 base::WeakPtr<AccessibilityController> controller,
52 blink::WebFrame* frame) {
53 v8::Isolate* isolate = blink::mainThreadIsolate();
54 v8::HandleScope handle_scope(isolate);
55 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
56 if (context.IsEmpty())
57 return;
58
59 v8::Context::Scope context_scope(context);
60
61 gin::Handle<AccessibilityControllerBindings> bindings =
62 gin::CreateHandle(isolate,
63 new AccessibilityControllerBindings(controller));
64 v8::Handle<v8::Object> global = context->Global();
65 global->Set(gin::StringToV8(isolate, "accessibilityController"),
66 bindings.ToV8());
67 }
68
69 AccessibilityControllerBindings::AccessibilityControllerBindings(
70 base::WeakPtr<AccessibilityController> controller)
71 : controller_(controller) {
72 }
73
74 AccessibilityControllerBindings::~AccessibilityControllerBindings() {
75 }
76
77 gin::ObjectTemplateBuilder
78 AccessibilityControllerBindings::GetObjectTemplateBuilder(
79 v8::Isolate* isolate) {
80 return gin::Wrappable<AccessibilityControllerBindings>::
81 GetObjectTemplateBuilder(isolate)
82 .SetMethod("logAccessibilityEvents",
83 &AccessibilityControllerBindings::LogAccessibilityEvents)
84 .SetMethod("setNotificationListener",
85 &AccessibilityControllerBindings::SetNotificationListener)
86 .SetMethod("unsetNotificationListener",
87 &AccessibilityControllerBindings::UnsetNotificationListener)
88 .SetProperty("focusedElement",
89 &AccessibilityControllerBindings::FocusedElement)
90 .SetProperty("rootElement",
91 &AccessibilityControllerBindings::RootElement)
92 .SetMethod("accessibleElementById",
93 &AccessibilityControllerBindings::AccessibleElementById)
94 // TODO(hajimehoshi): These are for backward compatibility. Remove them.
95 .SetMethod("addNotificationListener",
96 &AccessibilityControllerBindings::SetNotificationListener)
97 .SetMethod("removeNotificationListener",
98 &AccessibilityControllerBindings::UnsetNotificationListener);
99 }
100
101 void AccessibilityControllerBindings::LogAccessibilityEvents() {
102 if (controller_)
103 controller_->LogAccessibilityEvents();
104 }
105
106 void AccessibilityControllerBindings::SetNotificationListener(
107 v8::Handle<v8::Function> callback) {
108 if (controller_)
109 controller_->SetNotificationListener(callback);
110 }
111
112 void AccessibilityControllerBindings::UnsetNotificationListener() {
113 if (controller_)
114 controller_->UnsetNotificationListener();
115 }
116
117 v8::Handle<v8::Object> AccessibilityControllerBindings::FocusedElement() {
118 return controller_ ? controller_->FocusedElement() : v8::Handle<v8::Object>();
119 }
120
121 v8::Handle<v8::Object> AccessibilityControllerBindings::RootElement() {
122 return controller_ ? controller_->RootElement() : v8::Handle<v8::Object>();
123 }
124
125 v8::Handle<v8::Object> AccessibilityControllerBindings::AccessibleElementById(
126 const std::string& id) {
127 return controller_ ? controller_->AccessibleElementById(id)
128 : v8::Handle<v8::Object>();
129 }
130
131 AccessibilityController::AccessibilityController()
132 : log_accessibility_events_(false),
133 weak_factory_(this) {
134 }
135
136 AccessibilityController::~AccessibilityController() {}
137
138 void AccessibilityController::Reset() {
139 root_element_ = blink::WebAXObject();
140 focused_element_ = blink::WebAXObject();
141 elements_.Clear();
142 notification_callback_.Reset();
143 log_accessibility_events_ = false;
144 }
145
146 void AccessibilityController::Install(blink::WebFrame* frame) {
147 blink::WebAXObject::enableAccessibility();
148 blink::WebAXObject::enableInlineTextBoxAccessibility();
149 AccessibilityControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
150 }
151
152 void AccessibilityController::SetFocusedElement(
153 const blink::WebAXObject& focused_element) {
154 focused_element_ = focused_element;
155 }
156
157 bool AccessibilityController::ShouldLogAccessibilityEvents() {
158 return log_accessibility_events_;
159 }
160
161 void AccessibilityController::NotificationReceived(
162 const blink::WebAXObject& target, const std::string& notification_name) {
163 v8::Isolate* isolate = blink::mainThreadIsolate();
164 v8::HandleScope handle_scope(isolate);
165
166 blink::WebFrame* frame = web_view_->mainFrame();
167 if (!frame)
168 return;
169
170 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
171 if (context.IsEmpty())
172 return;
173
174 v8::Context::Scope context_scope(context);
175
176 // Call notification listeners on the element.
177 v8::Handle<v8::Object> element_handle = elements_.GetOrCreate(target);
178 WebAXObjectProxy* element;
179 bool result = gin::ConvertFromV8(isolate, element_handle, &element);
180 DCHECK(result);
181 element->NotificationReceived(frame, notification_name);
182
183 if (notification_callback_.IsEmpty())
184 return;
185
186 // Call global notification listeners.
187 v8::Handle<v8::Value> argv[] = {
188 element_handle,
189 v8::String::NewFromUtf8(isolate, notification_name.data(),
190 v8::String::kNormalString,
191 notification_name.size()),
192 };
193 frame->callFunctionEvenIfScriptDisabled(
194 v8::Local<v8::Function>::New(isolate, notification_callback_),
195 context->Global(),
196 arraysize(argv),
197 argv);
198 }
199
200 void AccessibilityController::SetDelegate(
201 WebTestRunner::WebTestDelegate* delegate) {
202 delegate_ = delegate;
203 }
204
205 void AccessibilityController::SetWebView(blink::WebView* web_view) {
206 web_view_ = web_view;
207 }
208
209 void AccessibilityController::LogAccessibilityEvents() {
210 log_accessibility_events_ = true;
211 }
212
213 void AccessibilityController::SetNotificationListener(
214 v8::Handle<v8::Function> callback) {
215 v8::Isolate* isolate = blink::mainThreadIsolate();
216 notification_callback_.Reset(isolate, callback);
217 }
218
219 void AccessibilityController::UnsetNotificationListener() {
220 notification_callback_.Reset();
221 }
222
223 v8::Handle<v8::Object> AccessibilityController::FocusedElement() {
224 if (focused_element_.isNull())
225 focused_element_ = web_view_->accessibilityObject();
226 return elements_.GetOrCreate(focused_element_);
227 }
228
229 v8::Handle<v8::Object> AccessibilityController::RootElement() {
230 if (root_element_.isNull())
231 root_element_ = web_view_->accessibilityObject();
232 return elements_.CreateRoot(root_element_);
233 }
234
235 v8::Handle<v8::Object>
236 AccessibilityController::AccessibleElementById(const std::string& id) {
237 if (root_element_.isNull())
238 root_element_ = web_view_->accessibilityObject();
239
240 if (!root_element_.updateBackingStoreAndCheckValidity())
241 return v8::Handle<v8::Object>();
242
243 return FindAccessibleElementByIdRecursive(
244 root_element_, blink::WebString::fromUTF8(id.c_str()));
245 }
246
247 v8::Handle<v8::Object>
248 AccessibilityController::FindAccessibleElementByIdRecursive(
249 const blink::WebAXObject& obj, const blink::WebString& id) {
250 if (obj.isNull() || obj.isDetached())
251 return v8::Handle<v8::Object>();
252
253 blink::WebNode node = obj.node();
254 if (!node.isNull() && node.isElementNode()) {
255 blink::WebElement element = node.to<blink::WebElement>();
256 element.getAttribute("id");
257 if (element.getAttribute("id") == id)
258 return elements_.GetOrCreate(obj);
259 }
260
261 unsigned childCount = obj.childCount();
262 for (unsigned i = 0; i < childCount; i++) {
263 v8::Handle<v8::Object> result =
264 FindAccessibleElementByIdRecursive(obj.childAt(i), id);
265 if (*result)
266 return result;
267 }
268
269 return v8::Handle<v8::Object>();
270 }
271
272 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698