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 private: |
| 25 explicit AccessibilityControllerBindings( |
| 26 base::WeakPtr<AccessibilityController> controller); |
| 27 virtual ~AccessibilityControllerBindings(); |
| 28 |
| 29 void LogAccessibilityEvents(); |
| 30 void AddNotificationListener(v8::Handle<v8::Function> callback); |
| 31 void RemoveNotificationListener(v8::Handle<v8::Function> callback); |
| 32 // FIXME |
| 33 void FocusedElement(); |
| 34 // FIXME |
| 35 void RootElement(); |
| 36 void AccessibleElementById(); |
| 37 void Fallback(); |
| 38 |
| 39 // gin::Wrappable: |
| 40 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 41 v8::Isolate* isolate) OVERRIDE; |
| 42 |
| 43 base::WeakPtr<AccessibilityController> controller_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(AccessibilityControllerBindings); |
| 46 }; |
| 47 |
| 48 // static |
| 49 gin::WrapperInfo AccessibilityControllerBindings::kWrapperInfo = { |
| 50 gin::kEmbedderNativeGin, |
| 51 }; |
| 52 |
| 53 // static |
| 54 void AccessibilityControllerBindings::Install( |
| 55 base::WeakPtr<AccessibilityController> controller, |
| 56 blink::WebFrame* frame) { |
| 57 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 58 v8::HandleScope handle_scope(isolate); |
| 59 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 60 if (context.IsEmpty()) |
| 61 return; |
| 62 |
| 63 v8::Context::Scope context_scope(context); |
| 64 |
| 65 gin::Handle<AccessibilityControllerBindings> bindings = |
| 66 gin::CreateHandle(isolate, |
| 67 new AccessibilityControllerBindings(controller)); |
| 68 v8::Handle<v8::Object> global = context->Global(); |
| 69 global->Set(gin::StringToV8(isolate, "accessibilityController"), |
| 70 bindings.ToV8()); |
| 71 } |
| 72 |
| 73 AccessibilityControllerBindings::AccessibilityControllerBindings( |
| 74 base::WeakPtr<AccessibilityController> controller) |
| 75 : controller_(controller) { |
| 76 } |
| 77 |
| 78 gin::ObjectTemplateBuilder |
| 79 AccessibilityControllerBindings::GetObjectTemplateBuilder( |
| 80 v8::Isolate* isolate) { |
| 81 return gin::Wrappable<AccessibilityControllerBindings>:: |
| 82 GetObjectTemplateBuilder(isolate) |
| 83 .SetMethod("logAccessibilityEvents", |
| 84 &AccessibilityControllerBindings::LogAccessibilityEvents) |
| 85 .SetMethod("addNotificationListener", |
| 86 &AccessibilityControllerBindings::AddNotificationListener) |
| 87 .SetMethod("removeNotificationListener", |
| 88 &AccessibilityControllerBindings::RemoveNotificationListener) |
| 89 .SetMethod("focusedEelement", |
| 90 &AccessibilityControllerBindings::FocusedElement) |
| 91 .SetMethod("rootElement", |
| 92 &AccessibilityControllerBindings::RootElement) |
| 93 .SetMethod("accessibleElementById", |
| 94 &AccessibilityControllerBindings::AccessibleElementById); |
| 95 |
| 96 // fallback? |
| 97 } |
| 98 |
| 99 void AccessibilityControllerBindings::LogAccessibilityEvents() { |
| 100 if (controller_) |
| 101 controller_->LogAccessibilityEvents(); |
| 102 } |
| 103 |
| 104 void AccessibilityControllerBindings::AddNotificationListener( |
| 105 v8::Handle<v8::Function> callback) { |
| 106 if (controller_) |
| 107 controller_->AddNotificationListener(callback); |
| 108 } |
| 109 |
| 110 void AccessibilityControllerBindings::RemoveNotificationListener( |
| 111 v8::Handle<v8::Function> callback) { |
| 112 if (controller_) |
| 113 controller_->RemoveNotificationListener(callback); |
| 114 } |
| 115 |
| 116 void AccessibilityControllerBindings::FocusedElement() { |
| 117 // return controller_ ? controller_->FocusedElement() : NULL; |
| 118 } |
| 119 |
| 120 void AccessibilityControllerBindings::RootElement() { |
| 121 // return controller_ ? controller_->RootElement() : NULL; |
| 122 } |
| 123 |
| 124 void AccessibilityControllerBindings::AccessibleElementById() { |
| 125 } |
| 126 |
| 127 AccessibilityController::AccessibilityController() |
| 128 : log_accessibility_events_(false), |
| 129 weak_factory_(this) { |
| 130 } |
| 131 |
| 132 AccessibilityController::~AccessibilityController() { |
| 133 } |
| 134 |
| 135 WebTestRunner::WebAXObjectProxy* |
| 136 AccessibilityController::FindAccessibleElementByIdRecursive( |
| 137 const blink::WebAXObject& obj, const blink::WebString& id) |
| 138 { |
| 139 if (obj.isNull() || obj.isDetached()) |
| 140 return NULL; |
| 141 |
| 142 blink::WebNode node = obj.node(); |
| 143 if (!node.isNull() && node.isElementNode()) { |
| 144 blink::WebElement element = node.to<blink::WebElement>(); |
| 145 element.getAttribute("id"); |
| 146 if (element.getAttribute("id") == id) |
| 147 return elements_.getOrCreate(obj); |
| 148 } |
| 149 |
| 150 unsigned childCount = obj.childCount(); |
| 151 for (unsigned i = 0; i < childCount; i++) { |
| 152 if (WebTestRunner::WebAXObjectProxy* result = |
| 153 FindAccessibleElementByIdRecursive(obj.childAt(i), id)) { |
| 154 return result; |
| 155 } |
| 156 } |
| 157 |
| 158 return NULL; |
| 159 } |
| 160 |
| 161 void AccessibilityController::LogAccessibilityEvents() { |
| 162 log_accessibility_events_ = true; |
| 163 } |
| 164 |
| 165 void AccessibilityController::AddNotificationListener( |
| 166 v8::Handle<v8::Function> callback) { |
| 167 notification_callbacks_.push_back(callback); |
| 168 } |
| 169 |
| 170 void AccessibilityController::RemoveNotificationListener( |
| 171 v8::Handle<v8::Function> callback) { |
| 172 // FIXME: Implement this |
| 173 } |
| 174 |
| 175 WebTestRunner::WebAXObjectProxy* AccessibilityController::FocusedElement() { |
| 176 if (focused_element_.isNull()) |
| 177 focused_element_ = web_view_->accessibilityObject(); |
| 178 return elements_.getOrCreate(focused_element_); |
| 179 } |
| 180 |
| 181 WebTestRunner::WebAXObjectProxy* AccessibilityController::RootElement() { |
| 182 if (root_element_.isNull()) |
| 183 root_element_ = web_view_->accessibilityObject(); |
| 184 return elements_.createRoot(root_element_); |
| 185 } |
| 186 |
| 187 } // namespace content |
OLD | NEW |