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 return false; |
62 } | 68 } |
63 | 69 |
64 void OnFilterRemoved() override { | 70 void OnFilterRemoved() override { |
65 removed_ = true; | 71 removed_ = true; |
66 } | 72 } |
67 | 73 |
68 private: | 74 private: |
| 75 void OnMessageReceivedOnRenderThread(const IPC::Message& message) { |
| 76 if (owner_) |
| 77 owner_->OnMessageReceived(message); |
| 78 } |
| 79 |
69 ~AutomationMessageFilter() override { | 80 ~AutomationMessageFilter() override { |
70 Remove(); | 81 Remove(); |
71 } | 82 } |
72 | 83 |
73 void Remove() { | 84 void Remove() { |
74 if (!removed_) { | 85 if (!removed_) { |
75 removed_ = true; | 86 removed_ = true; |
76 content::RenderThread::Get()->RemoveFilter(this); | 87 content::RenderThread::Get()->RemoveFilter(this); |
77 } | 88 } |
78 } | 89 } |
79 | 90 |
80 AutomationInternalCustomBindings* owner_; | 91 AutomationInternalCustomBindings* owner_; |
81 bool removed_; | 92 bool removed_; |
| 93 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
82 | 94 |
83 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter); | 95 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter); |
84 }; | 96 }; |
85 | 97 |
86 AutomationInternalCustomBindings::AutomationInternalCustomBindings( | 98 AutomationInternalCustomBindings::AutomationInternalCustomBindings( |
87 ScriptContext* context) : ObjectBackedNativeHandler(context) { | 99 ScriptContext* context) : ObjectBackedNativeHandler(context) { |
88 // It's safe to use base::Unretained(this) here because these bindings | 100 // It's safe to use base::Unretained(this) here because these bindings |
89 // will only be called on a valid AutomationInternalCustomBindings instance | 101 // will only be called on a valid AutomationInternalCustomBindings instance |
90 // and none of the functions have any side effects. | 102 // and none of the functions have any side effects. |
91 RouteFunction( | 103 #define ROUTE_FUNCTION(FN) \ |
92 "IsInteractPermitted", | 104 RouteFunction(#FN, \ |
93 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted, | 105 base::Bind(&AutomationInternalCustomBindings::FN, \ |
94 base::Unretained(this))); | 106 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 | 107 |
104 message_filter_ = new AutomationMessageFilter(this); | 108 ROUTE_FUNCTION(IsInteractPermitted); |
| 109 ROUTE_FUNCTION(GetSchemaAdditions); |
| 110 ROUTE_FUNCTION(GetRoutingID); |
| 111 ROUTE_FUNCTION(StartCachingAccessibilityTrees); |
| 112 ROUTE_FUNCTION(DestroyAccessibilityTree); |
| 113 ROUTE_FUNCTION(GetRootID); |
| 114 ROUTE_FUNCTION(GetParentID); |
| 115 ROUTE_FUNCTION(GetChildCount); |
| 116 ROUTE_FUNCTION(GetChildIDAtIndex); |
| 117 ROUTE_FUNCTION(GetIndexInParent); |
| 118 ROUTE_FUNCTION(GetState); |
| 119 ROUTE_FUNCTION(GetRole); |
| 120 ROUTE_FUNCTION(GetLocation); |
| 121 ROUTE_FUNCTION(GetStringAttribute); |
| 122 ROUTE_FUNCTION(GetBoolAttribute); |
| 123 ROUTE_FUNCTION(GetIntAttribute); |
| 124 ROUTE_FUNCTION(GetFloatAttribute); |
| 125 ROUTE_FUNCTION(GetIntListAttribute); |
105 } | 126 } |
106 | 127 |
107 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { | 128 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { |
108 message_filter_->Detach(); | 129 if (message_filter_) |
| 130 message_filter_->Detach(); |
109 } | 131 } |
110 | 132 |
111 bool AutomationInternalCustomBindings::OnMessageReceived( | 133 bool AutomationInternalCustomBindings::OnMessageReceived( |
112 const IPC::Message& message) { | 134 const IPC::Message& message) { |
113 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message) | 135 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message) |
114 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent) | 136 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent) |
115 IPC_END_MESSAGE_MAP() | 137 IPC_END_MESSAGE_MAP() |
116 | 138 |
117 // Always return false in case there are multiple | 139 // Always return false in case there are multiple |
118 // AutomationInternalCustomBindings instances attached to the same thread. | 140 // AutomationInternalCustomBindings instances attached to the same thread. |
119 return false; | 141 return false; |
120 } | 142 } |
121 | 143 |
122 void AutomationInternalCustomBindings::IsInteractPermitted( | 144 void AutomationInternalCustomBindings::IsInteractPermitted( |
123 const v8::FunctionCallbackInfo<v8::Value>& args) { | 145 const v8::FunctionCallbackInfo<v8::Value>& args) { |
124 const Extension* extension = context()->extension(); | 146 const Extension* extension = context()->extension(); |
125 CHECK(extension); | 147 CHECK(extension); |
126 const AutomationInfo* automation_info = AutomationInfo::Get(extension); | 148 const AutomationInfo* automation_info = AutomationInfo::Get(extension); |
127 CHECK(automation_info); | 149 CHECK(automation_info); |
128 args.GetReturnValue().Set( | 150 args.GetReturnValue().Set( |
129 v8::Boolean::New(GetIsolate(), automation_info->interact)); | 151 v8::Boolean::New(GetIsolate(), automation_info->interact)); |
130 } | 152 } |
131 | 153 |
132 void AutomationInternalCustomBindings::GetRoutingID( | 154 void AutomationInternalCustomBindings::GetRoutingID( |
133 const v8::FunctionCallbackInfo<v8::Value>& args) { | 155 const v8::FunctionCallbackInfo<v8::Value>& args) { |
134 int routing_id = context()->GetRenderView()->GetRoutingID(); | 156 int routing_id = context()->GetRenderView()->GetRoutingID(); |
135 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id)); | 157 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id)); |
136 } | 158 } |
137 | 159 |
| 160 void AutomationInternalCustomBindings::StartCachingAccessibilityTrees( |
| 161 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 162 if (!message_filter_) |
| 163 message_filter_ = new AutomationMessageFilter(this); |
| 164 } |
| 165 |
138 void AutomationInternalCustomBindings::GetSchemaAdditions( | 166 void AutomationInternalCustomBindings::GetSchemaAdditions( |
139 const v8::FunctionCallbackInfo<v8::Value>& args) { | 167 const v8::FunctionCallbackInfo<v8::Value>& args) { |
140 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); | 168 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); |
141 | 169 |
142 additions->Set( | 170 additions->Set( |
143 v8::String::NewFromUtf8(GetIsolate(), "EventType"), | 171 v8::String::NewFromUtf8(GetIsolate(), "EventType"), |
144 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); | 172 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); |
145 | 173 |
146 additions->Set( | 174 additions->Set( |
147 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), | 175 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), |
148 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); | 176 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); |
149 | 177 |
150 additions->Set( | 178 additions->Set( |
151 v8::String::NewFromUtf8(GetIsolate(), "StateType"), | 179 v8::String::NewFromUtf8(GetIsolate(), "StateType"), |
152 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); | 180 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); |
153 | 181 |
154 additions->Set( | 182 additions->Set( |
155 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), | 183 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), |
156 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); | 184 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); |
157 | 185 |
158 args.GetReturnValue().Set(additions); | 186 args.GetReturnValue().Set(additions); |
159 } | 187 } |
160 | 188 |
| 189 void AutomationInternalCustomBindings::DestroyAccessibilityTree( |
| 190 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 191 if (args.Length() != 1) |
| 192 return; |
| 193 if (!args[0]->IsNumber()) |
| 194 return; |
| 195 |
| 196 int tree_id = args[0]->Int32Value(); |
| 197 auto iter = tree_id_to_tree_cache_map_.find(tree_id); |
| 198 if (iter == tree_id_to_tree_cache_map_.end()) |
| 199 return; |
| 200 |
| 201 TreeCache* cache = iter->second; |
| 202 tree_id_to_tree_cache_map_.erase(tree_id); |
| 203 axtree_to_tree_cache_map_.erase(&cache->tree); |
| 204 delete cache; |
| 205 } |
| 206 |
| 207 void AutomationInternalCustomBindings::GetRootID( |
| 208 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 209 if (args.Length() != 1) |
| 210 return; |
| 211 if (!args[0]->IsNumber()) |
| 212 return; |
| 213 |
| 214 int tree_id = args[0]->Int32Value(); |
| 215 const auto& iter = tree_id_to_tree_cache_map_.find(tree_id); |
| 216 if (iter == tree_id_to_tree_cache_map_.end()) |
| 217 return; |
| 218 |
| 219 TreeCache* cache = iter->second; |
| 220 int root_id = cache->tree.root()->id(); |
| 221 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), root_id)); |
| 222 } |
| 223 |
| 224 bool AutomationInternalCustomBindings::GetNodeHelper( |
| 225 const v8::FunctionCallbackInfo<v8::Value>& args, |
| 226 TreeCache** out_cache, |
| 227 ui::AXNode** out_node) { |
| 228 if (args.Length() < 2) |
| 229 return false; |
| 230 if (!args[0]->IsNumber() || !args[1]->IsNumber()) |
| 231 return false; |
| 232 |
| 233 int tree_id = args[0]->Int32Value(); |
| 234 int node_id = args[1]->Int32Value(); |
| 235 |
| 236 const auto& iter = tree_id_to_tree_cache_map_.find(tree_id); |
| 237 if (iter == tree_id_to_tree_cache_map_.end()) |
| 238 return false; |
| 239 |
| 240 TreeCache* cache = iter->second; |
| 241 ui::AXNode* node = cache->tree.GetFromId(node_id); |
| 242 |
| 243 if (out_cache) |
| 244 *out_cache = cache; |
| 245 if (out_node) |
| 246 *out_node = node; |
| 247 |
| 248 return node != nullptr; |
| 249 } |
| 250 |
| 251 void AutomationInternalCustomBindings::SetReturnValueFromBaseValue( |
| 252 const v8::FunctionCallbackInfo<v8::Value>& args, |
| 253 base::Value* value) { |
| 254 v8::Isolate* isolate = context()->isolate(); |
| 255 v8::HandleScope handle_scope(isolate); |
| 256 v8::Context::Scope context_scope(context()->v8_context()); |
| 257 scoped_ptr<content::V8ValueConverter> converter( |
| 258 content::V8ValueConverter::create()); |
| 259 v8::Local<v8::Value> v8_value = |
| 260 converter->ToV8Value(value, context()->v8_context()); |
| 261 args.GetReturnValue().Set(v8_value); |
| 262 } |
| 263 |
| 264 void AutomationInternalCustomBindings::GetParentID( |
| 265 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 266 ui::AXNode* node; |
| 267 if (!GetNodeHelper(args, nullptr, &node)) |
| 268 return; |
| 269 |
| 270 if (!node->parent()) |
| 271 return; |
| 272 |
| 273 int parent_id = node->parent()->id(); |
| 274 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), parent_id)); |
| 275 } |
| 276 |
| 277 void AutomationInternalCustomBindings::GetChildCount( |
| 278 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 279 ui::AXNode* node; |
| 280 if (!GetNodeHelper(args, nullptr, &node)) |
| 281 return; |
| 282 |
| 283 int child_count = node->child_count(); |
| 284 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), child_count)); |
| 285 } |
| 286 |
| 287 void AutomationInternalCustomBindings::GetChildIDAtIndex( |
| 288 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 289 ui::AXNode* node; |
| 290 if (!GetNodeHelper(args, nullptr, &node)) |
| 291 return; |
| 292 |
| 293 if (args.Length() < 3 || !args[2]->IsNumber()) |
| 294 return; |
| 295 |
| 296 int index = args[2]->Int32Value(); |
| 297 if (index < 0 || index >= node->child_count()) |
| 298 return; |
| 299 |
| 300 int child_id = node->children()[index]->id(); |
| 301 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), child_id)); |
| 302 } |
| 303 |
| 304 void AutomationInternalCustomBindings::GetIndexInParent( |
| 305 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 306 ui::AXNode* node; |
| 307 if (!GetNodeHelper(args, nullptr, &node)) |
| 308 return; |
| 309 |
| 310 int index_in_parent = node->index_in_parent(); |
| 311 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), index_in_parent)); |
| 312 } |
| 313 |
| 314 void AutomationInternalCustomBindings::GetState( |
| 315 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 316 ui::AXNode* node; |
| 317 if (!GetNodeHelper(args, nullptr, &node)) |
| 318 return; |
| 319 |
| 320 scoped_ptr<base::DictionaryValue> state_dict(new base::DictionaryValue()); |
| 321 uint32 state_pos = 0, state_shifter = node->data().state; |
| 322 while (state_shifter) { |
| 323 if (state_shifter & 1) { |
| 324 state_dict->SetBoolean( |
| 325 ToString(static_cast<ui::AXState>(state_pos)), true); |
| 326 } |
| 327 state_shifter = state_shifter >> 1; |
| 328 state_pos++; |
| 329 } |
| 330 |
| 331 SetReturnValueFromBaseValue(args, state_dict.get()); |
| 332 } |
| 333 |
| 334 void AutomationInternalCustomBindings::GetRole( |
| 335 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 336 ui::AXNode* node; |
| 337 if (!GetNodeHelper(args, nullptr, &node)) |
| 338 return; |
| 339 |
| 340 std::string role_name = ui::ToString(node->data().role); |
| 341 args.GetReturnValue().Set( |
| 342 v8::String::NewFromUtf8(GetIsolate(), role_name.c_str())); |
| 343 } |
| 344 |
| 345 void AutomationInternalCustomBindings::GetLocation( |
| 346 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 347 TreeCache* cache; |
| 348 ui::AXNode* node; |
| 349 if (!GetNodeHelper(args, &cache, &node)) |
| 350 return; |
| 351 |
| 352 scoped_ptr<base::DictionaryValue> location_dict(new base::DictionaryValue()); |
| 353 gfx::Rect location = node->data().location; |
| 354 location.Offset(cache->location_offset); |
| 355 location_dict->SetInteger("left", location.x()); |
| 356 location_dict->SetInteger("top", location.y()); |
| 357 location_dict->SetInteger("width", location.width()); |
| 358 location_dict->SetInteger("height", location.height()); |
| 359 |
| 360 SetReturnValueFromBaseValue(args, location_dict.get()); |
| 361 } |
| 362 |
| 363 bool AutomationInternalCustomBindings::GetAttributeHelper( |
| 364 const v8::FunctionCallbackInfo<v8::Value>& args, |
| 365 ui::AXNode** out_node, |
| 366 std::string* out_attribute_name) { |
| 367 if (args.Length() != 3) |
| 368 return false; |
| 369 if (!args[0]->IsNumber() || |
| 370 !args[1]->IsNumber() || |
| 371 !args[2]->IsString()) { |
| 372 return false; |
| 373 } |
| 374 |
| 375 int tree_id = args[0]->Int32Value(); |
| 376 int node_id = args[1]->Int32Value(); |
| 377 *out_attribute_name = *v8::String::Utf8Value(args[2]); |
| 378 |
| 379 const auto& iter = tree_id_to_tree_cache_map_.find(tree_id); |
| 380 if (iter == tree_id_to_tree_cache_map_.end()) |
| 381 return false; |
| 382 |
| 383 TreeCache* cache = iter->second; |
| 384 *out_node = cache->tree.GetFromId(node_id); |
| 385 if (!*out_node) |
| 386 return false; |
| 387 |
| 388 return true; |
| 389 } |
| 390 |
| 391 void AutomationInternalCustomBindings::GetStringAttribute( |
| 392 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 393 ui::AXNode* node; |
| 394 std::string attribute_name; |
| 395 if (!GetAttributeHelper(args, &node, &attribute_name)) |
| 396 return; |
| 397 |
| 398 ui::AXStringAttribute attribute = ui::ParseAXStringAttribute(attribute_name); |
| 399 std::string attr_value; |
| 400 if (!node->data().GetStringAttribute(attribute, &attr_value)) |
| 401 return; |
| 402 |
| 403 args.GetReturnValue().Set( |
| 404 v8::String::NewFromUtf8(GetIsolate(), attr_value.c_str())); |
| 405 } |
| 406 |
| 407 void AutomationInternalCustomBindings::GetBoolAttribute( |
| 408 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 409 ui::AXNode* node; |
| 410 std::string attribute_name; |
| 411 if (!GetAttributeHelper(args, &node, &attribute_name)) |
| 412 return; |
| 413 |
| 414 ui::AXBoolAttribute attribute = ui::ParseAXBoolAttribute(attribute_name); |
| 415 bool attr_value; |
| 416 if (!node->data().GetBoolAttribute(attribute, &attr_value)) |
| 417 return; |
| 418 |
| 419 args.GetReturnValue().Set(v8::Boolean::New(GetIsolate(), attr_value)); |
| 420 } |
| 421 |
| 422 void AutomationInternalCustomBindings::GetIntAttribute( |
| 423 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 424 ui::AXNode* node; |
| 425 std::string attribute_name; |
| 426 if (!GetAttributeHelper(args, &node, &attribute_name)) |
| 427 return; |
| 428 |
| 429 ui::AXIntAttribute attribute = ui::ParseAXIntAttribute(attribute_name); |
| 430 int attr_value; |
| 431 if (!node->data().GetIntAttribute(attribute, &attr_value)) |
| 432 return; |
| 433 |
| 434 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), attr_value)); |
| 435 } |
| 436 |
| 437 void AutomationInternalCustomBindings::GetFloatAttribute( |
| 438 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 439 ui::AXNode* node; |
| 440 std::string attribute_name; |
| 441 if (!GetAttributeHelper(args, &node, &attribute_name)) |
| 442 return; |
| 443 |
| 444 ui::AXFloatAttribute attribute = ui::ParseAXFloatAttribute(attribute_name); |
| 445 float attr_value; |
| 446 |
| 447 if (!node->data().GetFloatAttribute(attribute, &attr_value)) |
| 448 return; |
| 449 |
| 450 args.GetReturnValue().Set(v8::Number::New(GetIsolate(), attr_value)); |
| 451 } |
| 452 |
| 453 void AutomationInternalCustomBindings::GetIntListAttribute( |
| 454 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 455 ui::AXNode* node; |
| 456 std::string attribute_name; |
| 457 if (!GetAttributeHelper(args, &node, &attribute_name)) |
| 458 return; |
| 459 |
| 460 ui::AXIntListAttribute attribute = |
| 461 ui::ParseAXIntListAttribute(attribute_name); |
| 462 if (!node->data().HasIntListAttribute(attribute)) |
| 463 return; |
| 464 const std::vector<int32>& attr_value = |
| 465 node->data().GetIntListAttribute(attribute); |
| 466 |
| 467 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 468 for (int32 i : attr_value) |
| 469 list->AppendInteger(i); |
| 470 SetReturnValueFromBaseValue(args, list.get()); |
| 471 } |
| 472 |
161 void AutomationInternalCustomBindings::OnAccessibilityEvent( | 473 void AutomationInternalCustomBindings::OnAccessibilityEvent( |
162 const ExtensionMsg_AccessibilityEventParams& params) { | 474 const ExtensionMsg_AccessibilityEventParams& params) { |
163 // TODO(dmazzoni): finish implementing this. | 475 int tree_id = params.tree_id; |
| 476 TreeCache* cache; |
| 477 auto iter = tree_id_to_tree_cache_map_.find(tree_id); |
| 478 if (iter == tree_id_to_tree_cache_map_.end()) { |
| 479 cache = new TreeCache(); |
| 480 cache->tab_id = -1; |
| 481 cache->tree_id = params.tree_id; |
| 482 cache->tree.SetDelegate(this); |
| 483 tree_id_to_tree_cache_map_.insert(std::make_pair(tree_id, cache)); |
| 484 axtree_to_tree_cache_map_.insert(std::make_pair(&cache->tree, cache)); |
| 485 } else { |
| 486 cache = iter->second; |
| 487 } |
| 488 |
| 489 cache->location_offset = params.location_offset; |
| 490 if (!cache->tree.Unserialize(params.update)) { |
| 491 LOG(FATAL) << cache->tree.error(); |
| 492 return; |
| 493 } |
| 494 |
| 495 api::automation_internal::AXEventParams event_params; |
| 496 event_params.tree_id = params.tree_id; |
| 497 event_params.target_id = params.id; |
| 498 event_params.event_type = ToString(params.event_type); |
| 499 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 500 args->Append(event_params.ToValue()); |
| 501 |
| 502 v8::Isolate* isolate = context()->isolate(); |
| 503 v8::HandleScope handle_scope(isolate); |
| 504 v8::Context::Scope context_scope(context()->v8_context()); |
| 505 scoped_ptr<content::V8ValueConverter> converter( |
| 506 content::V8ValueConverter::create()); |
| 507 v8::Local<v8::Value> v8_args = |
| 508 converter->ToV8Value(args.get(), context()->v8_context()); |
| 509 |
| 510 CHECK(v8_args->IsArray()); |
| 511 v8::Local<v8::Array> v8_args_array = v8_args.As<v8::Array>(); |
| 512 context()->DispatchEvent("automationInternal.onAccessibilityEvent", |
| 513 v8_args_array); |
| 514 } |
| 515 |
| 516 void AutomationInternalCustomBindings::OnNodeWillBeDeleted(ui::AXTree* tree, |
| 517 ui::AXNode* node) { |
| 518 SendTreeChangeEvent("nodeRemoved", tree, node); |
| 519 } |
| 520 |
| 521 void AutomationInternalCustomBindings::OnSubtreeWillBeDeleted( |
| 522 ui::AXTree* tree, |
| 523 ui::AXNode* node) { |
| 524 } |
| 525 |
| 526 void AutomationInternalCustomBindings::OnNodeCreated(ui::AXTree* tree, |
| 527 ui::AXNode* node) { |
| 528 |
| 529 } |
| 530 |
| 531 void AutomationInternalCustomBindings::OnNodeChanged(ui::AXTree* tree, |
| 532 ui::AXNode* node) { |
| 533 } |
| 534 |
| 535 void AutomationInternalCustomBindings::OnAtomicUpdateFinished( |
| 536 ui::AXTree* tree, |
| 537 bool root_changed, |
| 538 const std::vector<ui::AXTreeDelegate::Change>& changes) { |
| 539 auto iter = axtree_to_tree_cache_map_.find(tree); |
| 540 if (iter == axtree_to_tree_cache_map_.end()) |
| 541 return; |
| 542 |
| 543 for (auto change : changes) { |
| 544 ui::AXNode* node = change.node; |
| 545 switch (change.type) { |
| 546 case NODE_CREATED: |
| 547 SendTreeChangeEvent("nodeCreated", tree, node); |
| 548 break; |
| 549 case SUBTREE_CREATED: |
| 550 SendTreeChangeEvent("subtreeCreated", tree, node); |
| 551 break; |
| 552 case NODE_CHANGED: |
| 553 SendTreeChangeEvent("nodeChanged", tree, node); |
| 554 break; |
| 555 } |
| 556 } |
| 557 } |
| 558 |
| 559 void AutomationInternalCustomBindings::SendTreeChangeEvent( |
| 560 const char *change_type, |
| 561 ui::AXTree* tree, |
| 562 ui::AXNode* node) { |
| 563 auto iter = axtree_to_tree_cache_map_.find(tree); |
| 564 if (iter == axtree_to_tree_cache_map_.end()) |
| 565 return; |
| 566 |
| 567 int tree_id = iter->second->tree_id; |
| 568 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 569 args->AppendInteger(tree_id); |
| 570 args->AppendInteger(node->id()); |
| 571 args->AppendString(change_type); |
| 572 |
| 573 v8::Isolate* isolate = context()->isolate(); |
| 574 v8::HandleScope handle_scope(isolate); |
| 575 v8::Context::Scope context_scope(context()->v8_context()); |
| 576 scoped_ptr<content::V8ValueConverter> converter( |
| 577 content::V8ValueConverter::create()); |
| 578 v8::Local<v8::Value> v8_args = |
| 579 converter->ToV8Value(args.get(), context()->v8_context()); |
| 580 |
| 581 CHECK(v8_args->IsArray()); |
| 582 v8::Local<v8::Array> v8_args_array = v8_args.As<v8::Array>(); |
| 583 context()->DispatchEvent("automationInternal.onTreeChange", |
| 584 v8_args_array); |
164 } | 585 } |
165 | 586 |
166 } // namespace extensions | 587 } // namespace extensions |
OLD | NEW |