Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/automation_internal_custom_bindings.h" | 5 #include "chrome/renderer/extensions/automation_internal_custom_bindings.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/chrome_extension_messages.h" | 10 #include "chrome/common/extensions/chrome_extension_messages.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 isolate, ui::ToString(static_cast<EnumType>(i)).c_str()); | 32 isolate, ui::ToString(static_cast<EnumType>(i)).c_str()); |
| 33 object->Set(value, value); | 33 object->Set(value, value); |
| 34 } | 34 } |
| 35 return object; | 35 return object; |
| 36 } | 36 } |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 namespace extensions { | 40 namespace extensions { |
| 41 | 41 |
| 42 TreeCache::TreeCache() {} | |
| 43 TreeCache::~TreeCache() {} | |
| 44 | |
| 42 class AutomationMessageFilter : public IPC::MessageFilter { | 45 class AutomationMessageFilter : public IPC::MessageFilter { |
| 43 public: | 46 public: |
| 44 explicit AutomationMessageFilter(AutomationInternalCustomBindings* owner) | 47 explicit AutomationMessageFilter(AutomationInternalCustomBindings* owner) |
| 45 : owner_(owner), | 48 : owner_(owner), |
| 46 removed_(false) { | 49 removed_(false) { |
| 47 DCHECK(owner); | 50 DCHECK(owner); |
| 48 content::RenderThread::Get()->AddFilter(this); | 51 content::RenderThread::Get()->AddFilter(this); |
| 52 task_runner_ = content::RenderThread::Get()->GetTaskRunner(); | |
| 49 } | 53 } |
| 50 | 54 |
| 51 void Detach() { | 55 void Detach() { |
| 52 owner_ = nullptr; | 56 owner_ = nullptr; |
| 53 Remove(); | 57 Remove(); |
| 54 } | 58 } |
| 55 | 59 |
| 56 // IPC::MessageFilter | 60 // IPC::MessageFilter |
| 57 bool OnMessageReceived(const IPC::Message& message) override { | 61 bool OnMessageReceived(const IPC::Message& message) override { |
| 58 if (owner_) | 62 task_runner_->PostTask( |
| 59 return owner_->OnMessageReceived(message); | 63 FROM_HERE, |
| 60 else | 64 base::Bind( |
| 61 return false; | 65 &AutomationMessageFilter::OnMessageReceivedOnRenderThread, |
| 66 this, message)); | |
| 67 | |
| 68 // Always return false in case there are multiple | |
| 69 // AutomationInternalCustomBindings instances attached to the same thread. | |
| 70 return false; | |
| 62 } | 71 } |
| 63 | 72 |
| 64 void OnFilterRemoved() override { | 73 void OnFilterRemoved() override { |
| 65 removed_ = true; | 74 removed_ = true; |
| 66 } | 75 } |
| 67 | 76 |
| 68 private: | 77 private: |
| 78 void OnMessageReceivedOnRenderThread(const IPC::Message& message) { | |
| 79 if (owner_) | |
| 80 owner_->OnMessageReceived(message); | |
| 81 } | |
| 82 | |
| 69 ~AutomationMessageFilter() override { | 83 ~AutomationMessageFilter() override { |
| 70 Remove(); | 84 Remove(); |
| 71 } | 85 } |
| 72 | 86 |
| 73 void Remove() { | 87 void Remove() { |
| 74 if (!removed_) { | 88 if (!removed_) { |
| 75 removed_ = true; | 89 removed_ = true; |
| 76 content::RenderThread::Get()->RemoveFilter(this); | 90 content::RenderThread::Get()->RemoveFilter(this); |
| 77 } | 91 } |
| 78 } | 92 } |
| 79 | 93 |
| 80 AutomationInternalCustomBindings* owner_; | 94 AutomationInternalCustomBindings* owner_; |
| 81 bool removed_; | 95 bool removed_; |
| 96 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 82 | 97 |
| 83 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter); | 98 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter); |
| 84 }; | 99 }; |
| 85 | 100 |
| 86 AutomationInternalCustomBindings::AutomationInternalCustomBindings( | 101 AutomationInternalCustomBindings::AutomationInternalCustomBindings( |
| 87 ScriptContext* context) : ObjectBackedNativeHandler(context) { | 102 ScriptContext* context) : ObjectBackedNativeHandler(context) { |
| 88 // It's safe to use base::Unretained(this) here because these bindings | 103 // It's safe to use base::Unretained(this) here because these bindings |
| 89 // will only be called on a valid AutomationInternalCustomBindings instance | 104 // will only be called on a valid AutomationInternalCustomBindings instance |
| 90 // and none of the functions have any side effects. | 105 // and none of the functions have any side effects. |
| 91 RouteFunction( | 106 #define ROUTE_FUNCTION(FN) \ |
| 92 "IsInteractPermitted", | 107 RouteFunction(#FN, \ |
| 93 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted, | 108 base::Bind(&AutomationInternalCustomBindings::FN, \ |
| 94 base::Unretained(this))); | 109 base::Unretained(this))) |
| 95 RouteFunction( | |
| 96 "GetSchemaAdditions", | |
| 97 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions, | |
| 98 base::Unretained(this))); | |
| 99 RouteFunction( | |
| 100 "GetRoutingID", | |
| 101 base::Bind(&AutomationInternalCustomBindings::GetRoutingID, | |
| 102 base::Unretained(this))); | |
| 103 | 110 |
| 104 message_filter_ = new AutomationMessageFilter(this); | 111 ROUTE_FUNCTION(IsInteractPermitted); |
| 112 ROUTE_FUNCTION(GetSchemaAdditions); | |
| 113 ROUTE_FUNCTION(GetRoutingID); | |
| 114 ROUTE_FUNCTION(StartCachingAccessibilityTrees); | |
| 115 ROUTE_FUNCTION(DestroyAccessibilityTree); | |
| 116 ROUTE_FUNCTION(GetRootID); | |
| 117 ROUTE_FUNCTION(GetParentID); | |
| 118 ROUTE_FUNCTION(GetChildCount); | |
| 119 ROUTE_FUNCTION(GetChildIDAtIndex); | |
| 120 ROUTE_FUNCTION(GetIndexInParent); | |
| 121 ROUTE_FUNCTION(GetState); | |
| 122 ROUTE_FUNCTION(GetRole); | |
| 123 ROUTE_FUNCTION(GetLocation); | |
| 124 ROUTE_FUNCTION(GetStringAttribute); | |
| 125 ROUTE_FUNCTION(GetBoolAttribute); | |
| 126 ROUTE_FUNCTION(GetIntAttribute); | |
| 127 ROUTE_FUNCTION(GetFloatAttribute); | |
| 128 ROUTE_FUNCTION(GetIntListAttribute); | |
| 129 ROUTE_FUNCTION(GetHtmlAttribute); | |
| 130 | |
| 131 #undef ROUTE_FUNCTION | |
| 105 } | 132 } |
| 106 | 133 |
| 107 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { | 134 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { |
| 108 message_filter_->Detach(); | 135 if (message_filter_) |
| 136 message_filter_->Detach(); | |
| 137 STLDeleteContainerPairSecondPointers(tree_id_to_tree_cache_map_.begin(), | |
| 138 tree_id_to_tree_cache_map_.end()); | |
| 109 } | 139 } |
| 110 | 140 |
| 111 bool AutomationInternalCustomBindings::OnMessageReceived( | 141 void AutomationInternalCustomBindings::OnMessageReceived( |
| 112 const IPC::Message& message) { | 142 const IPC::Message& message) { |
| 113 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message) | 143 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message) |
| 114 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent) | 144 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent) |
| 115 IPC_END_MESSAGE_MAP() | 145 IPC_END_MESSAGE_MAP() |
| 116 | |
| 117 // Always return false in case there are multiple | |
| 118 // AutomationInternalCustomBindings instances attached to the same thread. | |
| 119 return false; | |
| 120 } | 146 } |
| 121 | 147 |
| 122 void AutomationInternalCustomBindings::IsInteractPermitted( | 148 void AutomationInternalCustomBindings::IsInteractPermitted( |
| 123 const v8::FunctionCallbackInfo<v8::Value>& args) { | 149 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 124 const Extension* extension = context()->extension(); | 150 const Extension* extension = context()->extension(); |
| 125 CHECK(extension); | 151 CHECK(extension); |
| 126 const AutomationInfo* automation_info = AutomationInfo::Get(extension); | 152 const AutomationInfo* automation_info = AutomationInfo::Get(extension); |
| 127 CHECK(automation_info); | 153 CHECK(automation_info); |
| 128 args.GetReturnValue().Set( | 154 args.GetReturnValue().Set( |
| 129 v8::Boolean::New(GetIsolate(), automation_info->interact)); | 155 v8::Boolean::New(GetIsolate(), automation_info->interact)); |
| 130 } | 156 } |
| 131 | 157 |
| 132 void AutomationInternalCustomBindings::GetRoutingID( | 158 void AutomationInternalCustomBindings::GetRoutingID( |
| 133 const v8::FunctionCallbackInfo<v8::Value>& args) { | 159 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 134 int routing_id = context()->GetRenderView()->GetRoutingID(); | 160 int routing_id = context()->GetRenderView()->GetRoutingID(); |
| 135 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id)); | 161 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id)); |
| 136 } | 162 } |
| 137 | 163 |
| 164 void AutomationInternalCustomBindings::StartCachingAccessibilityTrees( | |
| 165 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 166 if (!message_filter_) | |
| 167 message_filter_ = new AutomationMessageFilter(this); | |
| 168 } | |
| 169 | |
| 138 void AutomationInternalCustomBindings::GetSchemaAdditions( | 170 void AutomationInternalCustomBindings::GetSchemaAdditions( |
| 139 const v8::FunctionCallbackInfo<v8::Value>& args) { | 171 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 140 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); | 172 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); |
| 141 | 173 |
| 142 additions->Set( | 174 additions->Set( |
| 143 v8::String::NewFromUtf8(GetIsolate(), "EventType"), | 175 v8::String::NewFromUtf8(GetIsolate(), "EventType"), |
| 144 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); | 176 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); |
| 145 | 177 |
| 146 additions->Set( | 178 additions->Set( |
| 147 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), | 179 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), |
| 148 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); | 180 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); |
| 149 | 181 |
| 150 additions->Set( | 182 additions->Set( |
| 151 v8::String::NewFromUtf8(GetIsolate(), "StateType"), | 183 v8::String::NewFromUtf8(GetIsolate(), "StateType"), |
| 152 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); | 184 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); |
| 153 | 185 |
| 154 additions->Set( | 186 additions->Set( |
| 155 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), | 187 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), |
| 156 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); | 188 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); |
| 157 | 189 |
| 158 args.GetReturnValue().Set(additions); | 190 args.GetReturnValue().Set(additions); |
| 159 } | 191 } |
| 160 | 192 |
| 193 void AutomationInternalCustomBindings::DestroyAccessibilityTree( | |
| 194 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 195 if (args.Length() != 1 || !args[0]->IsNumber()) { | |
| 196 ThrowInvalidArgumentsException(args); | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 int tree_id = args[0]->Int32Value(); | |
| 201 auto iter = tree_id_to_tree_cache_map_.find(tree_id); | |
| 202 if (iter == tree_id_to_tree_cache_map_.end()) | |
| 203 return; | |
| 204 | |
| 205 TreeCache* cache = iter->second; | |
| 206 tree_id_to_tree_cache_map_.erase(tree_id); | |
| 207 axtree_to_tree_cache_map_.erase(&cache->tree); | |
| 208 delete cache; | |
| 209 } | |
| 210 | |
| 211 // | |
| 212 // Access the cached accessibility trees and properties of their nodes. | |
| 213 // | |
| 214 | |
| 215 void AutomationInternalCustomBindings::GetRootID( | |
| 216 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 217 if (args.Length() != 1 || !args[0]->IsNumber()) { | |
| 218 ThrowInvalidArgumentsException(args); | |
| 219 return; | |
| 220 } | |
| 221 | |
| 222 int tree_id = args[0]->Int32Value(); | |
| 223 const auto iter = tree_id_to_tree_cache_map_.find(tree_id); | |
| 224 if (iter == tree_id_to_tree_cache_map_.end()) | |
| 225 return; | |
| 226 | |
| 227 TreeCache* cache = iter->second; | |
| 228 int root_id = cache->tree.root()->id(); | |
| 229 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), root_id)); | |
| 230 } | |
| 231 | |
| 232 void AutomationInternalCustomBindings::GetParentID( | |
| 233 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 234 ui::AXNode* node = nullptr; | |
| 235 if (!GetNodeHelper(args, nullptr, &node)) | |
| 236 return; | |
| 237 | |
| 238 if (!node->parent()) | |
| 239 return; | |
| 240 | |
| 241 int parent_id = node->parent()->id(); | |
| 242 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), parent_id)); | |
| 243 } | |
| 244 | |
| 245 void AutomationInternalCustomBindings::GetChildCount( | |
| 246 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 247 ui::AXNode* node = nullptr; | |
| 248 if (!GetNodeHelper(args, nullptr, &node)) | |
| 249 return; | |
| 250 | |
| 251 int child_count = node->child_count(); | |
| 252 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), child_count)); | |
| 253 } | |
| 254 | |
| 255 void AutomationInternalCustomBindings::GetChildIDAtIndex( | |
| 256 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 257 if (args.Length() < 3 || !args[2]->IsNumber()) { | |
| 258 ThrowInvalidArgumentsException(args); | |
| 259 return; | |
| 260 } | |
| 261 | |
| 262 ui::AXNode* node = nullptr; | |
| 263 if (!GetNodeHelper(args, nullptr, &node)) | |
| 264 return; | |
| 265 | |
| 266 int index = args[2]->Int32Value(); | |
| 267 if (index < 0 || index >= node->child_count()) | |
| 268 return; | |
| 269 | |
| 270 int child_id = node->children()[index]->id(); | |
| 271 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), child_id)); | |
| 272 } | |
| 273 | |
| 274 void AutomationInternalCustomBindings::GetIndexInParent( | |
| 275 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 276 ui::AXNode* node = nullptr; | |
| 277 if (!GetNodeHelper(args, nullptr, &node)) | |
| 278 return; | |
| 279 | |
| 280 int index_in_parent = node->index_in_parent(); | |
| 281 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), index_in_parent)); | |
| 282 } | |
| 283 | |
| 284 void AutomationInternalCustomBindings::GetState( | |
| 285 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 286 ui::AXNode* node = nullptr; | |
| 287 if (!GetNodeHelper(args, nullptr, &node)) | |
| 288 return; | |
| 289 | |
| 290 v8::Local<v8::Object> state(v8::Object::New(GetIsolate())); | |
| 291 uint32 state_pos = 0, state_shifter = node->data().state; | |
| 292 while (state_shifter) { | |
| 293 if (state_shifter & 1) { | |
| 294 std::string key = ToString(static_cast<ui::AXState>(state_pos)); | |
| 295 state->Set(CreateV8String(key), | |
| 296 v8::Boolean::New(GetIsolate(), true)); | |
| 297 } | |
| 298 state_shifter = state_shifter >> 1; | |
| 299 state_pos++; | |
| 300 } | |
| 301 | |
| 302 args.GetReturnValue().Set(state); | |
| 303 } | |
| 304 | |
| 305 void AutomationInternalCustomBindings::GetRole( | |
| 306 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 307 ui::AXNode* node = nullptr; | |
| 308 if (!GetNodeHelper(args, nullptr, &node)) | |
| 309 return; | |
| 310 | |
| 311 std::string role_name = ui::ToString(node->data().role); | |
| 312 args.GetReturnValue().Set( | |
| 313 v8::String::NewFromUtf8(GetIsolate(), role_name.c_str())); | |
| 314 } | |
| 315 | |
| 316 void AutomationInternalCustomBindings::GetLocation( | |
| 317 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 318 TreeCache* cache; | |
| 319 ui::AXNode* node = nullptr; | |
| 320 if (!GetNodeHelper(args, &cache, &node)) | |
| 321 return; | |
| 322 | |
| 323 v8::Local<v8::Object> location_obj(v8::Object::New(GetIsolate())); | |
| 324 gfx::Rect location = node->data().location; | |
| 325 location.Offset(cache->location_offset); | |
| 326 location_obj->Set(CreateV8String("left"), | |
| 327 v8::Integer::New(GetIsolate(), location.x())); | |
| 328 location_obj->Set(CreateV8String("top"), | |
| 329 v8::Integer::New(GetIsolate(), location.y())); | |
| 330 location_obj->Set(CreateV8String("width"), | |
| 331 v8::Integer::New(GetIsolate(), location.width())); | |
| 332 location_obj->Set(CreateV8String("height"), | |
| 333 v8::Integer::New(GetIsolate(), location.height())); | |
|
not at google - send to devlin
2015/06/18 17:34:38
(musing) I wish we had a class like https://code.g
dmazzoni
2015/06/18 21:31:53
Sounds nice!
In this particular case I don't anti
| |
| 334 args.GetReturnValue().Set(location_obj); | |
| 335 } | |
| 336 | |
| 337 void AutomationInternalCustomBindings::GetStringAttribute( | |
| 338 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 339 ui::AXNode* node = nullptr; | |
| 340 std::string attribute_name; | |
| 341 if (!GetAttributeHelper(args, &node, &attribute_name)) | |
| 342 return; | |
| 343 | |
| 344 ui::AXStringAttribute attribute = ui::ParseAXStringAttribute(attribute_name); | |
| 345 std::string attr_value; | |
| 346 if (!node->data().GetStringAttribute(attribute, &attr_value)) | |
| 347 return; | |
| 348 | |
| 349 args.GetReturnValue().Set( | |
| 350 v8::String::NewFromUtf8(GetIsolate(), attr_value.c_str())); | |
| 351 } | |
| 352 | |
| 353 void AutomationInternalCustomBindings::GetBoolAttribute( | |
| 354 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 355 ui::AXNode* node = nullptr; | |
| 356 std::string attribute_name; | |
| 357 if (!GetAttributeHelper(args, &node, &attribute_name)) | |
| 358 return; | |
| 359 | |
| 360 ui::AXBoolAttribute attribute = ui::ParseAXBoolAttribute(attribute_name); | |
| 361 bool attr_value; | |
| 362 if (!node->data().GetBoolAttribute(attribute, &attr_value)) | |
| 363 return; | |
| 364 | |
| 365 args.GetReturnValue().Set(v8::Boolean::New(GetIsolate(), attr_value)); | |
| 366 } | |
| 367 | |
| 368 void AutomationInternalCustomBindings::GetIntAttribute( | |
| 369 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 370 ui::AXNode* node = nullptr; | |
| 371 std::string attribute_name; | |
| 372 if (!GetAttributeHelper(args, &node, &attribute_name)) | |
| 373 return; | |
| 374 | |
| 375 ui::AXIntAttribute attribute = ui::ParseAXIntAttribute(attribute_name); | |
| 376 int attr_value; | |
| 377 if (!node->data().GetIntAttribute(attribute, &attr_value)) | |
| 378 return; | |
| 379 | |
| 380 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), attr_value)); | |
| 381 } | |
| 382 | |
| 383 void AutomationInternalCustomBindings::GetFloatAttribute( | |
| 384 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 385 ui::AXNode* node = nullptr; | |
| 386 std::string attribute_name; | |
| 387 if (!GetAttributeHelper(args, &node, &attribute_name)) | |
| 388 return; | |
| 389 | |
| 390 ui::AXFloatAttribute attribute = ui::ParseAXFloatAttribute(attribute_name); | |
| 391 float attr_value; | |
| 392 | |
| 393 if (!node->data().GetFloatAttribute(attribute, &attr_value)) | |
| 394 return; | |
| 395 | |
| 396 args.GetReturnValue().Set(v8::Number::New(GetIsolate(), attr_value)); | |
| 397 } | |
| 398 | |
| 399 void AutomationInternalCustomBindings::GetIntListAttribute( | |
| 400 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 401 ui::AXNode* node = nullptr; | |
| 402 std::string attribute_name; | |
| 403 if (!GetAttributeHelper(args, &node, &attribute_name)) | |
| 404 return; | |
| 405 | |
| 406 ui::AXIntListAttribute attribute = | |
| 407 ui::ParseAXIntListAttribute(attribute_name); | |
| 408 if (!node->data().HasIntListAttribute(attribute)) | |
| 409 return; | |
| 410 const std::vector<int32>& attr_value = | |
| 411 node->data().GetIntListAttribute(attribute); | |
| 412 | |
| 413 v8::Local<v8::Array> result(v8::Array::New(GetIsolate(), attr_value.size())); | |
| 414 for (size_t i = 0; i < attr_value.size(); ++i) | |
| 415 result->Set(static_cast<uint32>(i), | |
| 416 v8::Integer::New(GetIsolate(), attr_value[i])); | |
| 417 args.GetReturnValue().Set(result); | |
| 418 } | |
| 419 | |
| 420 void AutomationInternalCustomBindings::GetHtmlAttribute( | |
| 421 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 422 ui::AXNode* node = nullptr; | |
| 423 std::string attribute_name; | |
| 424 if (!GetAttributeHelper(args, &node, &attribute_name)) | |
| 425 return; | |
| 426 | |
| 427 std::string attr_value; | |
| 428 if (!node->data().GetHtmlAttribute(attribute_name.c_str(), &attr_value)) | |
| 429 return; | |
| 430 | |
| 431 args.GetReturnValue().Set( | |
| 432 v8::String::NewFromUtf8(GetIsolate(), attr_value.c_str())); | |
| 433 } | |
| 434 | |
| 435 // | |
| 436 // Helper functions. | |
| 437 // | |
| 438 | |
| 439 void AutomationInternalCustomBindings::ThrowInvalidArgumentsException( | |
| 440 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 441 GetIsolate()->ThrowException( | |
| 442 v8::String::NewFromUtf8( | |
| 443 GetIsolate(), | |
| 444 "Invalid arguments to AutomationInternalCustomBindings function", | |
| 445 v8::NewStringType::kNormal).ToLocalChecked()); | |
| 446 | |
| 447 LOG(FATAL) | |
| 448 << "Invalid arguments to AutomationInternalCustomBindings function" | |
| 449 << context()->GetStackTraceAsString(); | |
| 450 } | |
| 451 | |
| 452 bool AutomationInternalCustomBindings::GetNodeHelper( | |
| 453 const v8::FunctionCallbackInfo<v8::Value>& args, | |
| 454 TreeCache** out_cache, | |
| 455 ui::AXNode** out_node) { | |
| 456 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsNumber()) { | |
| 457 ThrowInvalidArgumentsException(args); | |
| 458 return false; | |
| 459 } | |
| 460 | |
| 461 int tree_id = args[0]->Int32Value(); | |
| 462 int node_id = args[1]->Int32Value(); | |
| 463 | |
| 464 const auto iter = tree_id_to_tree_cache_map_.find(tree_id); | |
| 465 if (iter == tree_id_to_tree_cache_map_.end()) | |
| 466 return false; | |
| 467 | |
| 468 TreeCache* cache = iter->second; | |
| 469 ui::AXNode* node = cache->tree.GetFromId(node_id); | |
| 470 | |
| 471 if (out_cache) | |
| 472 *out_cache = cache; | |
| 473 if (out_node) | |
| 474 *out_node = node; | |
| 475 | |
| 476 return node != nullptr; | |
| 477 } | |
| 478 | |
| 479 bool AutomationInternalCustomBindings::GetAttributeHelper( | |
| 480 const v8::FunctionCallbackInfo<v8::Value>& args, | |
| 481 ui::AXNode** out_node, | |
| 482 std::string* out_attribute_name) { | |
| 483 if (args.Length() != 3 || | |
| 484 !args[2]->IsString()) { | |
| 485 ThrowInvalidArgumentsException(args); | |
| 486 return false; | |
| 487 } | |
| 488 | |
| 489 TreeCache* cache = nullptr; | |
| 490 if (!GetNodeHelper(args, &cache, out_node)) | |
| 491 return false; | |
| 492 | |
| 493 *out_attribute_name = *v8::String::Utf8Value(args[2]); | |
| 494 return true; | |
| 495 } | |
| 496 | |
| 497 v8::Local<v8::Value> AutomationInternalCustomBindings::CreateV8String( | |
|
not at google - send to devlin
2015/06/18 17:34:38
Pity that https://codereview.chromium.org/11854430
| |
| 498 const std::string& str) { | |
|
not at google - send to devlin
2015/06/18 17:34:38
Also, it seems silly that fairly often you're conv
dmazzoni
2015/06/18 21:31:53
Done.
| |
| 499 return v8::String::NewFromUtf8( | |
| 500 GetIsolate(), | |
| 501 str.c_str(), | |
| 502 v8::String::kNormalString, str.length()); | |
| 503 } | |
| 504 | |
| 505 // | |
| 506 // Handle accessibility events from the browser process. | |
| 507 // | |
| 508 | |
| 161 void AutomationInternalCustomBindings::OnAccessibilityEvent( | 509 void AutomationInternalCustomBindings::OnAccessibilityEvent( |
| 162 const ExtensionMsg_AccessibilityEventParams& params) { | 510 const ExtensionMsg_AccessibilityEventParams& params) { |
| 163 // TODO(dmazzoni): finish implementing this. | 511 int tree_id = params.tree_id; |
| 512 TreeCache* cache; | |
| 513 auto iter = tree_id_to_tree_cache_map_.find(tree_id); | |
| 514 if (iter == tree_id_to_tree_cache_map_.end()) { | |
| 515 cache = new TreeCache(); | |
| 516 cache->tab_id = -1; | |
| 517 cache->tree_id = params.tree_id; | |
| 518 cache->tree.SetDelegate(this); | |
| 519 tree_id_to_tree_cache_map_.insert(std::make_pair(tree_id, cache)); | |
| 520 axtree_to_tree_cache_map_.insert(std::make_pair(&cache->tree, cache)); | |
| 521 } else { | |
| 522 cache = iter->second; | |
| 523 } | |
| 524 | |
| 525 cache->location_offset = params.location_offset; | |
| 526 if (!cache->tree.Unserialize(params.update)) { | |
| 527 LOG(FATAL) << cache->tree.error(); | |
| 528 return; | |
| 529 } | |
| 530 | |
| 531 api::automation_internal::AXEventParams event_params; | |
| 532 event_params.tree_id = params.tree_id; | |
| 533 event_params.target_id = params.id; | |
| 534 event_params.event_type = ToString(params.event_type); | |
| 535 base::ListValue args; | |
|
not at google - send to devlin
2015/06/18 17:34:38
Honestly, type safety be damned, I'd just put a "n
dmazzoni
2015/06/18 21:31:53
You're right, it even ends up being less code. Don
| |
| 536 args.Append(event_params.ToValue()); | |
| 537 | |
| 538 v8::HandleScope handle_scope(GetIsolate()); | |
| 539 v8::Context::Scope context_scope(context()->v8_context()); | |
| 540 scoped_ptr<content::V8ValueConverter> converter( | |
| 541 content::V8ValueConverter::create()); | |
| 542 v8::Local<v8::Value> v8_args = | |
| 543 converter->ToV8Value(&args, context()->v8_context()); | |
| 544 | |
| 545 DCHECK(v8_args->IsArray()); | |
| 546 v8::Local<v8::Array> v8_args_array = v8_args.As<v8::Array>(); | |
| 547 context()->DispatchEvent("automationInternal.onAccessibilityEvent", | |
| 548 v8_args_array); | |
| 549 } | |
| 550 | |
| 551 void AutomationInternalCustomBindings::OnNodeWillBeDeleted(ui::AXTree* tree, | |
| 552 ui::AXNode* node) { | |
| 553 SendTreeChangeEvent( | |
| 554 api::automation::TREE_CHANGE_TYPE_NODEREMOVED, | |
| 555 tree, node); | |
| 556 } | |
| 557 | |
| 558 void AutomationInternalCustomBindings::OnSubtreeWillBeDeleted( | |
| 559 ui::AXTree* tree, | |
| 560 ui::AXNode* node) { | |
| 561 // This isn't strictly needed, as OnNodeWillBeDeleted will already be | |
| 562 // called. We could send a JS event for this only if it turns out to | |
| 563 // be needed for something. | |
| 564 } | |
| 565 | |
| 566 void AutomationInternalCustomBindings::OnNodeCreated(ui::AXTree* tree, | |
| 567 ui::AXNode* node) { | |
| 568 // Not needed, this is called in the middle of an update so it's not | |
| 569 // safe to trigger JS from here. Wait for the notification in | |
| 570 // OnAtomicUpdateFinished instead. | |
| 571 } | |
| 572 | |
| 573 void AutomationInternalCustomBindings::OnNodeChanged(ui::AXTree* tree, | |
| 574 ui::AXNode* node) { | |
| 575 // Not needed, this is called in the middle of an update so it's not | |
| 576 // safe to trigger JS from here. Wait for the notification in | |
| 577 // OnAtomicUpdateFinished instead. | |
| 578 } | |
| 579 | |
| 580 void AutomationInternalCustomBindings::OnAtomicUpdateFinished( | |
| 581 ui::AXTree* tree, | |
| 582 bool root_changed, | |
| 583 const std::vector<ui::AXTreeDelegate::Change>& changes) { | |
| 584 auto iter = axtree_to_tree_cache_map_.find(tree); | |
| 585 if (iter == axtree_to_tree_cache_map_.end()) | |
| 586 return; | |
| 587 | |
| 588 for (auto change : changes) { | |
| 589 ui::AXNode* node = change.node; | |
| 590 switch (change.type) { | |
| 591 case NODE_CREATED: | |
| 592 SendTreeChangeEvent( | |
| 593 api::automation::TREE_CHANGE_TYPE_NODECREATED, | |
| 594 tree, node); | |
| 595 break; | |
| 596 case SUBTREE_CREATED: | |
| 597 SendTreeChangeEvent( | |
| 598 api::automation::TREE_CHANGE_TYPE_SUBTREECREATED, | |
| 599 tree, node); | |
| 600 break; | |
| 601 case NODE_CHANGED: | |
| 602 SendTreeChangeEvent( | |
| 603 api::automation::TREE_CHANGE_TYPE_NODECHANGED, | |
| 604 tree, node); | |
| 605 break; | |
| 606 } | |
| 607 } | |
| 608 } | |
| 609 | |
| 610 void AutomationInternalCustomBindings::SendTreeChangeEvent( | |
| 611 api::automation::TreeChangeType change_type, | |
| 612 ui::AXTree* tree, | |
| 613 ui::AXNode* node) { | |
| 614 auto iter = axtree_to_tree_cache_map_.find(tree); | |
| 615 if (iter == axtree_to_tree_cache_map_.end()) | |
| 616 return; | |
| 617 | |
| 618 int tree_id = iter->second->tree_id; | |
| 619 | |
| 620 v8::HandleScope handle_scope(GetIsolate()); | |
| 621 v8::Context::Scope context_scope(context()->v8_context()); | |
| 622 v8::Local<v8::Array> args(v8::Array::New(GetIsolate(), 3U)); | |
| 623 args->Set(0U, v8::Integer::New(GetIsolate(), tree_id)); | |
| 624 args->Set(1U, v8::Integer::New(GetIsolate(), node->id())); | |
| 625 args->Set(2U, CreateV8String(ToString(change_type))); | |
| 626 context()->DispatchEvent("automationInternal.onTreeChange", args); | |
| 164 } | 627 } |
| 165 | 628 |
| 166 } // namespace extensions | 629 } // namespace extensions |
| OLD | NEW |