Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3258)

Unified Diff: chrome/renderer/extensions/automation_internal_custom_bindings.cc

Issue 1155183006: Reimplement automation API on top of C++-backed AXTree. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@automation_faster_2
Patch Set: Rebase, delete some code that shouldn't have been commented out Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/renderer/extensions/automation_internal_custom_bindings.cc
diff --git a/chrome/renderer/extensions/automation_internal_custom_bindings.cc b/chrome/renderer/extensions/automation_internal_custom_bindings.cc
index 1704c44e5d386c5492602dc388d9f993f7b738a3..051c33c73c7d07ee3d48e6092d4b357103b6a76b 100644
--- a/chrome/renderer/extensions/automation_internal_custom_bindings.cc
+++ b/chrome/renderer/extensions/automation_internal_custom_bindings.cc
@@ -39,12 +39,16 @@ v8::Local<v8::Object> ToEnumObject(v8::Isolate* isolate,
namespace extensions {
+TreeCache::TreeCache() {}
+TreeCache::~TreeCache() {}
+
class AutomationMessageFilter : public IPC::MessageFilter {
public:
explicit AutomationMessageFilter(AutomationInternalCustomBindings* owner)
: owner_(owner) {
DCHECK(owner);
content::RenderThread::Get()->AddFilter(this);
+ task_runner_ = content::RenderThread::Get()->GetTaskRunner();
}
void Detach() {
@@ -53,18 +57,26 @@ class AutomationMessageFilter : public IPC::MessageFilter {
// IPC::MessageFilter
bool OnMessageReceived(const IPC::Message& message) override {
- if (owner_)
- return owner_->OnMessageReceived(message);
- else
- return false;
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &AutomationMessageFilter::OnMessageReceivedOnRenderThread,
+ this, message));
+ return false;
}
private:
+ void OnMessageReceivedOnRenderThread(const IPC::Message& message) {
+ if (owner_)
+ owner_->OnMessageReceived(message);
+ }
+
~AutomationMessageFilter() override {
content::RenderThread::Get()->RemoveFilter(this);
}
AutomationInternalCustomBindings* owner_;
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter);
};
@@ -74,18 +86,28 @@ AutomationInternalCustomBindings::AutomationInternalCustomBindings(
// It's safe to use base::Unretained(this) here because these bindings
// will only be called on a valid AutomationInternalCustomBindings instance
// and none of the functions have any side effects.
- RouteFunction(
- "IsInteractPermitted",
- base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted,
- base::Unretained(this)));
- RouteFunction(
- "GetSchemaAdditions",
- base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions,
- base::Unretained(this)));
- RouteFunction(
- "GetRoutingID",
- base::Bind(&AutomationInternalCustomBindings::GetRoutingID,
- base::Unretained(this)));
+ #define ROUTE_FUNCTION(FN) \
+ RouteFunction(#FN, \
+ base::Bind(&AutomationInternalCustomBindings::FN, \
+ base::Unretained(this)))
+
+ ROUTE_FUNCTION(IsInteractPermitted);
+ ROUTE_FUNCTION(GetSchemaAdditions);
+ ROUTE_FUNCTION(GetRoutingID);
+ ROUTE_FUNCTION(DestroyAccessibilityTree);
+ ROUTE_FUNCTION(GetRootID);
+ ROUTE_FUNCTION(GetParentID);
+ ROUTE_FUNCTION(GetChildCount);
+ ROUTE_FUNCTION(GetChildIDAtIndex);
+ ROUTE_FUNCTION(GetIndexInParent);
+ ROUTE_FUNCTION(GetState);
+ ROUTE_FUNCTION(GetRole);
+ ROUTE_FUNCTION(GetLocation);
+ ROUTE_FUNCTION(GetStringAttribute);
+ ROUTE_FUNCTION(GetBoolAttribute);
+ ROUTE_FUNCTION(GetIntAttribute);
+ ROUTE_FUNCTION(GetFloatAttribute);
+ ROUTE_FUNCTION(GetIntListAttribute);
message_filter_ = new AutomationMessageFilter(this);
}
@@ -144,9 +166,398 @@ void AutomationInternalCustomBindings::GetSchemaAdditions(
args.GetReturnValue().Set(additions);
}
+void AutomationInternalCustomBindings::DestroyAccessibilityTree(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ if (args.Length() != 1)
+ return;
+ if (!args[0]->IsNumber())
+ return;
+
+ int tree_id = args[0]->Int32Value();
+ auto iter = tree_id_to_tree_cache_map_.find(tree_id);
+ if (iter == tree_id_to_tree_cache_map_.end())
+ return;
+
+ TreeCache* cache = iter->second;
+ tree_id_to_tree_cache_map_.erase(tree_id);
+ axtree_to_tree_cache_map_.erase(&cache->tree);
+ delete cache;
+}
+
+void AutomationInternalCustomBindings::GetRootID(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ if (args.Length() != 1)
David Tseng 2015/06/10 17:48:16 Seems like these cases throughout should cause an
dmazzoni 2015/06/11 19:09:12 Done. I have it throw an exception for invalid arg
aboxhall 2015/06/11 20:32:42 Looks like other custom bindings code uses CHECKs
dmazzoni 2015/06/12 17:51:35 That's pretty hard to debug, though - there's no w
aboxhall 2015/06/12 20:43:10 With a CHECK, it'll be super obvious that somethin
dmazzoni 2015/06/16 18:19:48 OK, solved both problems. Now it grabs a JS stack
aboxhall 2015/06/16 18:22:44 Nice one :)
+ return;
+ if (!args[0]->IsNumber())
+ return;
+
+ int tree_id = args[0]->Int32Value();
+ const auto& iter = tree_id_to_tree_cache_map_.find(tree_id);
+ if (iter == tree_id_to_tree_cache_map_.end())
+ return;
+
+ TreeCache* cache = iter->second;
+ int root_id = cache->tree.root()->id();
+ args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), root_id));
+}
+
+bool AutomationInternalCustomBindings::GetNodeHelper(
+ const v8::FunctionCallbackInfo<v8::Value>& args,
+ TreeCache** out_cache,
+ ui::AXNode** out_node) {
+ if (args.Length() < 2)
+ return false;
+ if (!args[0]->IsNumber() || !args[1]->IsNumber())
+ return false;
+
+ int tree_id = args[0]->Int32Value();
+ int node_id = args[1]->Int32Value();
+
+ const auto& iter = tree_id_to_tree_cache_map_.find(tree_id);
+ if (iter == tree_id_to_tree_cache_map_.end())
+ return false;
+
+ TreeCache* cache = iter->second;
+ ui::AXNode* node = cache->tree.GetFromId(node_id);
+
+ if (out_cache)
+ *out_cache = cache;
+ if (out_node)
+ *out_node = node;
+
+ return node != nullptr;
+}
+
+void AutomationInternalCustomBindings::SetReturnValueFromBaseValue(
+ const v8::FunctionCallbackInfo<v8::Value>& args,
+ base::Value* value) {
+ v8::Isolate* isolate = context()->isolate();
+ v8::HandleScope handle_scope(isolate);
+ v8::Context::Scope context_scope(context()->v8_context());
+ scoped_ptr<content::V8ValueConverter> converter(
+ content::V8ValueConverter::create());
+ v8::Local<v8::Value> v8_value =
+ converter->ToV8Value(value, context()->v8_context());
+ args.GetReturnValue().Set(v8_value);
+}
+
+void AutomationInternalCustomBindings::GetParentID(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, nullptr, &node))
+ return;
+
+ if (!node->parent())
+ return;
+
+ int parent_id = node->parent()->id();
+ args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), parent_id));
+}
+
+void AutomationInternalCustomBindings::GetChildCount(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, nullptr, &node))
+ return;
+
+ int child_count = node->child_count();
+ args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), child_count));
+}
+
+void AutomationInternalCustomBindings::GetChildIDAtIndex(
David Tseng 2015/06/10 17:48:16 These functions should probably get docs; maybe js
dmazzoni 2015/06/11 19:09:11 Added concise docs to the header file now. Does th
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, nullptr, &node))
+ return;
+
+ if (args.Length() < 3 || !args[2]->IsNumber())
aboxhall 2015/06/11 15:05:38 Do args validation before getting the node?
dmazzoni 2015/06/11 19:09:12 Done.
+ return;
+
+ int index = args[2]->Int32Value();
+ if (index < 0 || index >= node->child_count())
+ return;
+
+ int child_id = node->children()[index]->id();
+ args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), child_id));
+}
+
+void AutomationInternalCustomBindings::GetIndexInParent(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, nullptr, &node))
+ return;
+
+ int index_in_parent = node->index_in_parent();
+ args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), index_in_parent));
+}
+
+void AutomationInternalCustomBindings::GetState(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, nullptr, &node))
+ return;
+
+ scoped_ptr<base::DictionaryValue> state_dict(new base::DictionaryValue());
+ uint32 state_pos = 0, state_shifter = node->data().state;
+ while (state_shifter) {
+ if (state_shifter & 1) {
+ state_dict->SetBoolean(
+ ToString(static_cast<ui::AXState>(state_pos)), true);
+ }
+ state_shifter = state_shifter >> 1;
+ state_pos++;
+ }
+
+ SetReturnValueFromBaseValue(args, state_dict.get());
+}
+
+void AutomationInternalCustomBindings::GetRole(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, nullptr, &node))
+ return;
+
+ std::string role_name = ui::ToString(node->data().role);
+ args.GetReturnValue().Set(
+ v8::String::NewFromUtf8(GetIsolate(), role_name.c_str()));
+}
+
+void AutomationInternalCustomBindings::GetLocation(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ TreeCache* cache;
+ ui::AXNode* node;
+ if (!GetNodeHelper(args, &cache, &node))
+ return;
+
+ scoped_ptr<base::DictionaryValue> location_dict(new base::DictionaryValue());
+ gfx::Rect location = node->data().location;
+ location.Offset(cache->location_offset);
+ location_dict->SetInteger("left", location.x());
+ location_dict->SetInteger("top", location.y());
+ location_dict->SetInteger("width", location.width());
+ location_dict->SetInteger("height", location.height());
+
+ SetReturnValueFromBaseValue(args, location_dict.get());
+}
+
+bool AutomationInternalCustomBindings::GetAttributeHelper(
+ const v8::FunctionCallbackInfo<v8::Value>& args,
+ ui::AXNode** out_node,
+ std::string* out_attribute_name) {
+ if (args.Length() != 3)
+ return false;
+ if (!args[0]->IsNumber() ||
+ !args[1]->IsNumber() ||
+ !args[2]->IsString()) {
+ return false;
+ }
+
+ int tree_id = args[0]->Int32Value();
+ int node_id = args[1]->Int32Value();
+ *out_attribute_name = *v8::String::Utf8Value(args[2]);
+
+ const auto& iter = tree_id_to_tree_cache_map_.find(tree_id);
+ if (iter == tree_id_to_tree_cache_map_.end())
+ return false;
+
+ TreeCache* cache = iter->second;
+ *out_node = cache->tree.GetFromId(node_id);
+ if (!*out_node)
David Tseng 2015/06/10 17:48:16 GetNodeHelper?
dmazzoni 2015/06/11 19:09:12 Done.
+ return false;
+
+ return true;
+}
+
+void AutomationInternalCustomBindings::GetStringAttribute(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ std::string attribute_name;
+ if (!GetAttributeHelper(args, &node, &attribute_name))
+ return;
+
+ ui::AXStringAttribute attribute = ui::ParseAXStringAttribute(attribute_name);
+ std::string attr_value;
+ if (!node->data().GetStringAttribute(attribute, &attr_value))
+ return;
+
+ args.GetReturnValue().Set(
+ v8::String::NewFromUtf8(GetIsolate(), attr_value.c_str()));
+}
+
+void AutomationInternalCustomBindings::GetBoolAttribute(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ std::string attribute_name;
+ if (!GetAttributeHelper(args, &node, &attribute_name))
+ return;
+
+ ui::AXBoolAttribute attribute = ui::ParseAXBoolAttribute(attribute_name);
+ bool attr_value;
+ if (!node->data().GetBoolAttribute(attribute, &attr_value))
+ return;
David Tseng 2015/06/10 17:48:16 Is return value here undefined in js?
dmazzoni 2015/06/11 19:09:11 Yes, the default will be undefined.
+
+ args.GetReturnValue().Set(v8::Boolean::New(GetIsolate(), attr_value));
+}
+
+void AutomationInternalCustomBindings::GetIntAttribute(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ std::string attribute_name;
+ if (!GetAttributeHelper(args, &node, &attribute_name))
+ return;
+
+ ui::AXIntAttribute attribute = ui::ParseAXIntAttribute(attribute_name);
+ int attr_value;
+ if (!node->data().GetIntAttribute(attribute, &attr_value))
+ return;
+
+ args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), attr_value));
+}
+
+void AutomationInternalCustomBindings::GetFloatAttribute(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ std::string attribute_name;
+ if (!GetAttributeHelper(args, &node, &attribute_name))
+ return;
+
+ ui::AXFloatAttribute attribute = ui::ParseAXFloatAttribute(attribute_name);
+ float attr_value;
+
+ if (!node->data().GetFloatAttribute(attribute, &attr_value))
+ return;
+
+ args.GetReturnValue().Set(v8::Number::New(GetIsolate(), attr_value));
+}
+
+void AutomationInternalCustomBindings::GetIntListAttribute(
+ const v8::FunctionCallbackInfo<v8::Value>& args) {
+ ui::AXNode* node;
+ std::string attribute_name;
+ if (!GetAttributeHelper(args, &node, &attribute_name))
+ return;
+
+ ui::AXIntListAttribute attribute =
+ ui::ParseAXIntListAttribute(attribute_name);
+ if (!node->data().HasIntListAttribute(attribute))
+ return;
+ const std::vector<int32>& attr_value =
+ node->data().GetIntListAttribute(attribute);
+
+ scoped_ptr<base::ListValue> list(new base::ListValue());
+ for (int32 i : attr_value)
+ list->AppendInteger(i);
+ SetReturnValueFromBaseValue(args, list.get());
+}
+
void AutomationInternalCustomBindings::OnAccessibilityEvent(
aboxhall 2015/06/11 15:05:38 I can't figure out how this event gets dispatched.
dmazzoni 2015/06/11 19:09:11 It gets called from AutomationWebContentsObserver:
const ExtensionMsg_AccessibilityEventParams& params) {
- // TODO(dmazzoni): finish implementing this.
+ int tree_id = params.tree_id;
+ TreeCache* cache;
+ auto iter = tree_id_to_tree_cache_map_.find(tree_id);
+ if (iter == tree_id_to_tree_cache_map_.end()) {
aboxhall 2015/06/11 15:05:38 I was trying to figure out where this got called s
dmazzoni 2015/06/11 19:09:11 Yeah, AutomationEventRouter keeps track of exactly
aboxhall 2015/06/11 20:32:42 Ah, I see it now. Technically the permissions chec
dmazzoni 2015/06/12 17:51:35 I'm not sure how to do that because AutomationEven
aboxhall 2015/06/12 20:43:10 Right, I see. I misunderstood the 'has permission
+ cache = new TreeCache();
+ cache->tab_id = -1;
+ cache->tree_id = params.tree_id;
+ cache->tree.SetDelegate(this);
+ tree_id_to_tree_cache_map_.insert(std::make_pair(tree_id, cache));
+ axtree_to_tree_cache_map_.insert(std::make_pair(&cache->tree, cache));
+ } else {
+ cache = iter->second;
+ }
+
+ cache->location_offset = params.location_offset;
+ CHECK(cache->tree.Unserialize(params.update));
+ api::automation_internal::AXEventParams event_params;
+ event_params.tree_id = params.tree_id;
+ event_params.target_id = params.id;
+ event_params.event_type = ToString(params.event_type);
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ args->Append(event_params.ToValue());
+
+ v8::Isolate* isolate = context()->isolate();
+ v8::HandleScope handle_scope(isolate);
+ v8::Context::Scope context_scope(context()->v8_context());
+ scoped_ptr<content::V8ValueConverter> converter(
+ content::V8ValueConverter::create());
+ v8::Local<v8::Value> v8_args =
+ converter->ToV8Value(args.get(), context()->v8_context());
+
+ CHECK(v8_args->IsArray());
+ v8::Local<v8::Array> v8_args_array = v8_args.As<v8::Array>();
+ context()->DispatchEvent("automationInternal.onAccessibilityEvent",
+ v8_args_array);
+}
+
+void AutomationInternalCustomBindings::OnNodeWillBeDeleted(ui::AXTree* tree,
+ ui::AXNode* node) {
+ SendTreeChangeEvent("nodeRemoved", tree, node);
+}
+
+void AutomationInternalCustomBindings::OnSubtreeWillBeDeleted(
+ ui::AXTree* tree,
+ ui::AXNode* node) {
+}
David Tseng 2015/06/10 17:48:15 TODO?
dmazzoni 2015/06/11 19:09:12 Not necessarily needed? This was needed to impleme
aboxhall 2015/06/11 20:32:42 Could these null implementations be moved up into
dmazzoni 2015/06/12 17:51:35 To be clear, these are pure virtual functions in A
aboxhall 2015/06/12 20:43:10 Makes sense - I wasn't sure to what extent we gene
+
+void AutomationInternalCustomBindings::OnNodeCreated(ui::AXTree* tree,
+ ui::AXNode* node) {
+
David Tseng 2015/06/10 17:48:16 TODO?
dmazzoni 2015/06/11 19:09:11 Not needed, added a comment.
+}
+
+void AutomationInternalCustomBindings::OnNodeChanged(ui::AXTree* tree,
+ ui::AXNode* node) {
+}
David Tseng 2015/06/10 17:48:15 TODO?
dmazzoni 2015/06/11 19:09:11 Done.
+
+void AutomationInternalCustomBindings::OnAtomicUpdateFinished(
David Tseng 2015/06/10 17:48:16 What calls this function?
dmazzoni 2015/06/11 19:09:11 AXTree calls this because we implement AXTreeDeleg
+ ui::AXTree* tree,
+ bool root_changed,
+ const std::vector<ui::AXTreeDelegate::Change>& changes) {
+ auto iter = axtree_to_tree_cache_map_.find(tree);
+ if (iter == axtree_to_tree_cache_map_.end())
+ return;
+
+ for (auto change : changes) {
+ ui::AXNode* node = change.node;
+ switch (change.type) {
+ case NODE_CREATED:
+ SendTreeChangeEvent("nodeCreated", tree, node);
+ break;
+ case SUBTREE_CREATED:
+ SendTreeChangeEvent("subtreeCreated", tree, node);
+ break;
+ case NODE_CHANGED:
+ SendTreeChangeEvent("nodeChanged", tree, node);
David Tseng 2015/06/10 17:48:16 Define/document these literals.
dmazzoni 2015/06/11 19:09:12 Switched to use the enum and ToString function aut
+ break;
+ }
+ }
+}
+
+void AutomationInternalCustomBindings::SendTreeChangeEvent(
+ const char *change_type,
+ ui::AXTree* tree,
+ ui::AXNode* node) {
+ auto iter = axtree_to_tree_cache_map_.find(tree);
+ if (iter == axtree_to_tree_cache_map_.end())
+ return;
+
+ int tree_id = iter->second->tree_id;
+ scoped_ptr<base::ListValue> args(new base::ListValue());
+ args->AppendInteger(tree_id);
+ args->AppendInteger(node->id());
+ args->AppendString(change_type);
+
+ v8::Isolate* isolate = context()->isolate();
+ v8::HandleScope handle_scope(isolate);
+ v8::Context::Scope context_scope(context()->v8_context());
+ scoped_ptr<content::V8ValueConverter> converter(
+ content::V8ValueConverter::create());
+ v8::Local<v8::Value> v8_args =
+ converter->ToV8Value(args.get(), context()->v8_context());
+
+ CHECK(v8_args->IsArray());
+ v8::Local<v8::Array> v8_args_array = v8_args.As<v8::Array>();
+ context()->DispatchEvent("automationInternal.onTreeChange",
+ v8_args_array);
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698