OLD | NEW |
(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 if (element_handle.IsEmpty()) |
| 179 return; |
| 180 |
| 181 WebAXObjectProxy* element; |
| 182 bool result = gin::ConvertFromV8(isolate, element_handle, &element); |
| 183 DCHECK(result); |
| 184 element->NotificationReceived(frame, notification_name); |
| 185 |
| 186 if (notification_callback_.IsEmpty()) |
| 187 return; |
| 188 |
| 189 // Call global notification listeners. |
| 190 v8::Handle<v8::Value> argv[] = { |
| 191 element_handle, |
| 192 v8::String::NewFromUtf8(isolate, notification_name.data(), |
| 193 v8::String::kNormalString, |
| 194 notification_name.size()), |
| 195 }; |
| 196 frame->callFunctionEvenIfScriptDisabled( |
| 197 v8::Local<v8::Function>::New(isolate, notification_callback_), |
| 198 context->Global(), |
| 199 arraysize(argv), |
| 200 argv); |
| 201 } |
| 202 |
| 203 void AccessibilityController::SetDelegate( |
| 204 WebTestRunner::WebTestDelegate* delegate) { |
| 205 delegate_ = delegate; |
| 206 } |
| 207 |
| 208 void AccessibilityController::SetWebView(blink::WebView* web_view) { |
| 209 web_view_ = web_view; |
| 210 } |
| 211 |
| 212 void AccessibilityController::LogAccessibilityEvents() { |
| 213 log_accessibility_events_ = true; |
| 214 } |
| 215 |
| 216 void AccessibilityController::SetNotificationListener( |
| 217 v8::Handle<v8::Function> callback) { |
| 218 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 219 notification_callback_.Reset(isolate, callback); |
| 220 } |
| 221 |
| 222 void AccessibilityController::UnsetNotificationListener() { |
| 223 notification_callback_.Reset(); |
| 224 } |
| 225 |
| 226 v8::Handle<v8::Object> AccessibilityController::FocusedElement() { |
| 227 if (focused_element_.isNull()) |
| 228 focused_element_ = web_view_->accessibilityObject(); |
| 229 return elements_.GetOrCreate(focused_element_); |
| 230 } |
| 231 |
| 232 v8::Handle<v8::Object> AccessibilityController::RootElement() { |
| 233 if (root_element_.isNull()) |
| 234 root_element_ = web_view_->accessibilityObject(); |
| 235 return elements_.CreateRoot(root_element_); |
| 236 } |
| 237 |
| 238 v8::Handle<v8::Object> |
| 239 AccessibilityController::AccessibleElementById(const std::string& id) { |
| 240 if (root_element_.isNull()) |
| 241 root_element_ = web_view_->accessibilityObject(); |
| 242 |
| 243 if (!root_element_.updateBackingStoreAndCheckValidity()) |
| 244 return v8::Handle<v8::Object>(); |
| 245 |
| 246 return FindAccessibleElementByIdRecursive( |
| 247 root_element_, blink::WebString::fromUTF8(id.c_str())); |
| 248 } |
| 249 |
| 250 v8::Handle<v8::Object> |
| 251 AccessibilityController::FindAccessibleElementByIdRecursive( |
| 252 const blink::WebAXObject& obj, const blink::WebString& id) { |
| 253 if (obj.isNull() || obj.isDetached()) |
| 254 return v8::Handle<v8::Object>(); |
| 255 |
| 256 blink::WebNode node = obj.node(); |
| 257 if (!node.isNull() && node.isElementNode()) { |
| 258 blink::WebElement element = node.to<blink::WebElement>(); |
| 259 element.getAttribute("id"); |
| 260 if (element.getAttribute("id") == id) |
| 261 return elements_.GetOrCreate(obj); |
| 262 } |
| 263 |
| 264 unsigned childCount = obj.childCount(); |
| 265 for (unsigned i = 0; i < childCount; i++) { |
| 266 v8::Handle<v8::Object> result = |
| 267 FindAccessibleElementByIdRecursive(obj.childAt(i), id); |
| 268 if (*result) |
| 269 return result; |
| 270 } |
| 271 |
| 272 return v8::Handle<v8::Object>(); |
| 273 } |
| 274 |
| 275 } // namespace content |
OLD | NEW |