Chromium Code Reviews| 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 // TODO(hajimehoshi): Remove this when UnsafePersistent is removed. | |
| 6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | |
| 7 | |
| 8 #include "content/shell/renderer/test_runner/accessibility_controller.h" | |
| 9 | |
| 10 #include "gin/handle.h" | |
| 11 #include "gin/object_template_builder.h" | |
| 12 #include "gin/wrappable.h" | |
| 13 #include "third_party/WebKit/public/web/WebElement.h" | |
| 14 #include "third_party/WebKit/public/web/WebFrame.h" | |
| 15 #include "third_party/WebKit/public/web/WebKit.h" | |
| 16 #include "third_party/WebKit/public/web/WebView.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 class AccessibilityControllerBindings | |
| 21 : public gin::Wrappable<AccessibilityControllerBindings> { | |
| 22 public: | |
| 23 static gin::WrapperInfo kWrapperInfo; | |
| 24 | |
| 25 static void Install(base::WeakPtr<AccessibilityController> controller, | |
| 26 blink::WebFrame* frame); | |
| 27 | |
| 28 private: | |
| 29 explicit AccessibilityControllerBindings( | |
| 30 base::WeakPtr<AccessibilityController> controller); | |
| 31 virtual ~AccessibilityControllerBindings(); | |
| 32 | |
| 33 // gin::Wrappable: | |
| 34 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
| 35 v8::Isolate* isolate) OVERRIDE; | |
| 36 | |
| 37 void LogAccessibilityEvents(); | |
| 38 void AddNotificationListener(v8::Handle<v8::Function> callback); | |
| 39 void RemoveNotificationListener(v8::Handle<v8::Function> callback); | |
| 40 v8::Handle<v8::Object> FocusedElement(); | |
| 41 v8::Handle<v8::Object> RootElement(); | |
| 42 v8::Handle<v8::Object> AccessibleElementById(const std::string& id); | |
| 43 | |
| 44 base::WeakPtr<AccessibilityController> controller_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(AccessibilityControllerBindings); | |
| 47 }; | |
| 48 | |
| 49 // static | |
|
jochen (gone - plz use gerrit)
2014/02/24 11:21:52
nit. we only use this comment for methods.
hajimehoshi
2014/02/25 04:27:47
Done. (WebAXObjectProxy as well)
| |
| 50 gin::WrapperInfo AccessibilityControllerBindings::kWrapperInfo = { | |
| 51 gin::kEmbedderNativeGin, | |
|
jochen (gone - plz use gerrit)
2014/02/24 11:21:52
nit. no , at end and }; should be in this line, an
hajimehoshi
2014/02/25 04:27:47
Done. (WebAXObjectProxy as well)
| |
| 52 }; | |
| 53 | |
| 54 // static | |
| 55 void AccessibilityControllerBindings::Install( | |
| 56 base::WeakPtr<AccessibilityController> controller, | |
| 57 blink::WebFrame* frame) { | |
| 58 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 59 v8::HandleScope handle_scope(isolate); | |
| 60 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | |
| 61 if (context.IsEmpty()) | |
| 62 return; | |
| 63 | |
| 64 v8::Context::Scope context_scope(context); | |
| 65 | |
| 66 gin::Handle<AccessibilityControllerBindings> bindings = | |
| 67 gin::CreateHandle(isolate, | |
| 68 new AccessibilityControllerBindings(controller)); | |
| 69 v8::Handle<v8::Object> global = context->Global(); | |
| 70 global->Set(gin::StringToV8(isolate, "accessibilityController"), | |
| 71 bindings.ToV8()); | |
| 72 } | |
| 73 | |
| 74 AccessibilityControllerBindings::AccessibilityControllerBindings( | |
| 75 base::WeakPtr<AccessibilityController> controller) | |
| 76 : controller_(controller) { | |
| 77 } | |
| 78 | |
| 79 AccessibilityControllerBindings::~AccessibilityControllerBindings() { | |
| 80 } | |
| 81 | |
| 82 gin::ObjectTemplateBuilder | |
| 83 AccessibilityControllerBindings::GetObjectTemplateBuilder( | |
| 84 v8::Isolate* isolate) { | |
| 85 return gin::Wrappable<AccessibilityControllerBindings>:: | |
| 86 GetObjectTemplateBuilder(isolate) | |
| 87 .SetMethod("logAccessibilityEvents", | |
| 88 &AccessibilityControllerBindings::LogAccessibilityEvents) | |
| 89 .SetMethod("addNotificationListener", | |
| 90 &AccessibilityControllerBindings::AddNotificationListener) | |
| 91 .SetMethod("removeNotificationListener", | |
| 92 &AccessibilityControllerBindings::RemoveNotificationListener) | |
| 93 .SetProperty("focusedElement", | |
| 94 &AccessibilityControllerBindings::FocusedElement) | |
| 95 .SetProperty("rootElement", | |
| 96 &AccessibilityControllerBindings::RootElement) | |
| 97 .SetMethod("accessibleElementById", | |
| 98 &AccessibilityControllerBindings::AccessibleElementById); | |
| 99 } | |
| 100 | |
| 101 void AccessibilityControllerBindings::LogAccessibilityEvents() { | |
| 102 if (controller_) | |
| 103 controller_->LogAccessibilityEvents(); | |
| 104 } | |
| 105 | |
| 106 void AccessibilityControllerBindings::AddNotificationListener( | |
| 107 v8::Handle<v8::Function> callback) { | |
| 108 if (controller_) | |
| 109 controller_->AddNotificationListener(callback); | |
| 110 } | |
| 111 | |
| 112 void AccessibilityControllerBindings::RemoveNotificationListener( | |
| 113 v8::Handle<v8::Function> callback) { | |
| 114 if (controller_) | |
| 115 controller_->RemoveNotificationListener(callback); | |
| 116 } | |
| 117 | |
| 118 v8::Handle<v8::Object> AccessibilityControllerBindings::FocusedElement() { | |
| 119 return controller_ ? controller_->FocusedElement() : v8::Handle<v8::Object>(); | |
| 120 } | |
| 121 | |
| 122 v8::Handle<v8::Object> AccessibilityControllerBindings::RootElement() { | |
| 123 return controller_ ? controller_->RootElement() : v8::Handle<v8::Object>(); | |
| 124 } | |
| 125 | |
| 126 v8::Handle<v8::Object> AccessibilityControllerBindings::AccessibleElementById( | |
| 127 const std::string& id) { | |
| 128 return controller_ ? controller_->AccessibleElementById(id) | |
|
jochen (gone - plz use gerrit)
2014/02/24 11:21:52
is this clang-formated?
hajimehoshi
2014/02/25 04:27:47
Done.
| |
| 129 : v8::Handle<v8::Object>(); | |
| 130 } | |
| 131 | |
| 132 AccessibilityController::AccessibilityController() | |
| 133 : log_accessibility_events_(false), | |
| 134 weak_factory_(this) { | |
| 135 } | |
| 136 | |
| 137 AccessibilityController::~AccessibilityController() { | |
| 138 ClearNotificationCallbacks(); | |
| 139 } | |
| 140 | |
| 141 void AccessibilityController::Reset() { | |
| 142 root_element_ = blink::WebAXObject(); | |
| 143 focused_element_ = blink::WebAXObject(); | |
| 144 elements_.Clear(); | |
| 145 ClearNotificationCallbacks(); | |
| 146 log_accessibility_events_ = false; | |
| 147 } | |
| 148 | |
| 149 void AccessibilityController::Install(blink::WebFrame* frame) { | |
| 150 blink::WebAXObject::enableAccessibility(); | |
| 151 blink::WebAXObject::enableInlineTextBoxAccessibility(); | |
| 152 AccessibilityControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | |
| 153 } | |
| 154 | |
| 155 void AccessibilityController::SetFocusedElement( | |
| 156 const blink::WebAXObject& focused_element) { | |
| 157 focused_element_ = focused_element; | |
| 158 } | |
| 159 | |
| 160 bool AccessibilityController::ShouldLogAccessibilityEvents() { | |
| 161 return log_accessibility_events_; | |
| 162 } | |
| 163 | |
| 164 void AccessibilityController::NotificationReceived( | |
| 165 const blink::WebAXObject& target, const std::string& notification_name) { | |
| 166 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 167 v8::HandleScope handle_scope(isolate); | |
| 168 | |
| 169 blink::WebFrame* frame = web_view_->mainFrame(); | |
| 170 if (!frame) | |
| 171 return; | |
| 172 | |
| 173 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | |
| 174 if (context.IsEmpty()) | |
| 175 return; | |
| 176 | |
| 177 v8::Context::Scope context_scope(context); | |
| 178 | |
| 179 // Call notification listeners on the element. | |
| 180 v8::Handle<v8::Object> element_handle = elements_.GetOrCreate(target); | |
| 181 WebAXObjectProxy* element; | |
| 182 bool result = gin::ConvertFromV8(isolate, element_handle, &element); | |
| 183 DCHECK(result); | |
| 184 element->NotificationReceived(context, notification_name); | |
| 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 for (CallbackList::iterator it = notification_callbacks_.begin(); | |
| 194 it != notification_callbacks_.end(); ++it) { | |
| 195 (*it)->Call(context->Global(), arraysize(argv), argv); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void AccessibilityController::SetDelegate( | |
| 200 WebTestRunner::WebTestDelegate* delegate) { | |
| 201 delegate_ = delegate; | |
| 202 } | |
| 203 | |
| 204 void AccessibilityController::SetWebView(blink::WebView* web_view) { | |
| 205 web_view_ = web_view; | |
| 206 } | |
| 207 | |
| 208 void AccessibilityController::LogAccessibilityEvents() { | |
| 209 log_accessibility_events_ = true; | |
| 210 } | |
| 211 | |
| 212 void AccessibilityController::AddNotificationListener( | |
| 213 v8::Handle<v8::Function> callback) { | |
| 214 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 215 notification_callbacks_.push_back( | |
| 216 gin::UnsafePersistent<v8::Function>(isolate, callback)); | |
| 217 } | |
| 218 | |
| 219 void AccessibilityController::RemoveNotificationListener( | |
| 220 v8::Handle<v8::Function> callback) { | |
| 221 // FIXME: Implement this | |
| 222 } | |
| 223 | |
| 224 v8::Handle<v8::Object> AccessibilityController::FocusedElement() { | |
| 225 if (focused_element_.isNull()) | |
| 226 focused_element_ = web_view_->accessibilityObject(); | |
| 227 return elements_.GetOrCreate(focused_element_); | |
| 228 } | |
| 229 | |
| 230 v8::Handle<v8::Object> AccessibilityController::RootElement() { | |
| 231 if (root_element_.isNull()) | |
| 232 root_element_ = web_view_->accessibilityObject(); | |
| 233 return elements_.CreateRoot(root_element_); | |
| 234 } | |
| 235 | |
| 236 v8::Handle<v8::Object> | |
| 237 AccessibilityController::AccessibleElementById(const std::string& id) { | |
| 238 if (root_element_.isNull()) | |
| 239 root_element_ = web_view_->accessibilityObject(); | |
| 240 | |
| 241 if (!root_element_.updateBackingStoreAndCheckValidity()) | |
| 242 return v8::Handle<v8::Object>(); | |
| 243 | |
| 244 return FindAccessibleElementByIdRecursive( | |
| 245 root_element_, blink::WebString::fromUTF8(id.c_str())); | |
| 246 } | |
| 247 | |
| 248 v8::Handle<v8::Object> | |
| 249 AccessibilityController::FindAccessibleElementByIdRecursive( | |
| 250 const blink::WebAXObject& obj, const blink::WebString& id) { | |
| 251 if (obj.isNull() || obj.isDetached()) | |
| 252 return v8::Handle<v8::Object>(); | |
| 253 | |
| 254 blink::WebNode node = obj.node(); | |
| 255 if (!node.isNull() && node.isElementNode()) { | |
| 256 blink::WebElement element = node.to<blink::WebElement>(); | |
| 257 element.getAttribute("id"); | |
| 258 if (element.getAttribute("id") == id) | |
| 259 return elements_.GetOrCreate(obj); | |
| 260 } | |
| 261 | |
| 262 unsigned childCount = obj.childCount(); | |
| 263 for (unsigned i = 0; i < childCount; i++) { | |
| 264 v8::Handle<v8::Object> result = | |
| 265 FindAccessibleElementByIdRecursive(obj.childAt(i), id); | |
| 266 if (*result) | |
| 267 return result; | |
| 268 } | |
| 269 | |
| 270 return v8::Handle<v8::Object>(); | |
| 271 } | |
| 272 | |
| 273 void AccessibilityController::ClearNotificationCallbacks() { | |
| 274 for (CallbackList::iterator it = notification_callbacks_.begin(); | |
| 275 it != notification_callbacks_.end(); ++it) { | |
| 276 it->Dispose(); | |
| 277 } | |
| 278 notification_callbacks_.clear(); | |
| 279 } | |
| 280 | |
| 281 } // namespace content | |
| OLD | NEW |