Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / renderer / extensions / automation_internal_custom_bindings.cc
blob7f70eb553270d64ed0f552ecde24696df1a1a58a
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.
5 #include "chrome/renderer/extensions/automation_internal_custom_bindings.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/common/extensions/chrome_extension_messages.h"
11 #include "chrome/common/extensions/manifest_handlers/automation.h"
12 #include "content/public/child/v8_value_converter.h"
13 #include "content/public/renderer/render_frame.h"
14 #include "content/public/renderer/render_thread.h"
15 #include "content/public/renderer/render_view.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/manifest.h"
18 #include "extensions/renderer/script_context.h"
19 #include "ipc/message_filter.h"
20 #include "ui/accessibility/ax_enums.h"
21 #include "ui/accessibility/ax_node.h"
23 namespace {
25 // Helper to convert an enum to a V8 object.
26 template <typename EnumType>
27 v8::Local<v8::Object> ToEnumObject(v8::Isolate* isolate,
28 EnumType start_after,
29 EnumType end_at) {
30 v8::Local<v8::Object> object = v8::Object::New(isolate);
31 for (int i = start_after + 1; i <= end_at; ++i) {
32 v8::Local<v8::String> value = v8::String::NewFromUtf8(
33 isolate, ui::ToString(static_cast<EnumType>(i)).c_str());
34 object->Set(value, value);
36 return object;
39 } // namespace
41 namespace extensions {
43 class AutomationMessageFilter : public IPC::MessageFilter {
44 public:
45 explicit AutomationMessageFilter(AutomationInternalCustomBindings* owner)
46 : owner_(owner),
47 removed_(false) {
48 DCHECK(owner);
49 content::RenderThread::Get()->AddFilter(this);
52 void Detach() {
53 owner_ = nullptr;
54 Remove();
57 // IPC::MessageFilter
58 bool OnMessageReceived(const IPC::Message& message) override {
59 if (owner_)
60 return owner_->OnMessageReceived(message);
61 else
62 return false;
65 void OnFilterRemoved() override {
66 removed_ = true;
69 private:
70 ~AutomationMessageFilter() override {
71 Remove();
74 void Remove() {
75 if (!removed_) {
76 removed_ = true;
77 content::RenderThread::Get()->RemoveFilter(this);
81 AutomationInternalCustomBindings* owner_;
82 bool removed_;
84 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter);
87 AutomationInternalCustomBindings::AutomationInternalCustomBindings(
88 ScriptContext* context) : ObjectBackedNativeHandler(context) {
89 // It's safe to use base::Unretained(this) here because these bindings
90 // will only be called on a valid AutomationInternalCustomBindings instance
91 // and none of the functions have any side effects.
92 RouteFunction(
93 "IsInteractPermitted",
94 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted,
95 base::Unretained(this)));
96 RouteFunction(
97 "GetSchemaAdditions",
98 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions,
99 base::Unretained(this)));
100 RouteFunction(
101 "GetRoutingID",
102 base::Bind(&AutomationInternalCustomBindings::GetRoutingID,
103 base::Unretained(this)));
105 message_filter_ = new AutomationMessageFilter(this);
108 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() {
109 message_filter_->Detach();
112 bool AutomationInternalCustomBindings::OnMessageReceived(
113 const IPC::Message& message) {
114 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message)
115 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent)
116 IPC_END_MESSAGE_MAP()
118 // Always return false in case there are multiple
119 // AutomationInternalCustomBindings instances attached to the same thread.
120 return false;
123 void AutomationInternalCustomBindings::IsInteractPermitted(
124 const v8::FunctionCallbackInfo<v8::Value>& args) {
125 const Extension* extension = context()->extension();
126 CHECK(extension);
127 const AutomationInfo* automation_info = AutomationInfo::Get(extension);
128 CHECK(automation_info);
129 args.GetReturnValue().Set(
130 v8::Boolean::New(GetIsolate(), automation_info->interact));
133 void AutomationInternalCustomBindings::GetRoutingID(
134 const v8::FunctionCallbackInfo<v8::Value>& args) {
135 int routing_id = context()->GetRenderFrame()->GetRenderView()->GetRoutingID();
136 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id));
139 void AutomationInternalCustomBindings::GetSchemaAdditions(
140 const v8::FunctionCallbackInfo<v8::Value>& args) {
141 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate());
143 additions->Set(
144 v8::String::NewFromUtf8(GetIsolate(), "EventType"),
145 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST));
147 additions->Set(
148 v8::String::NewFromUtf8(GetIsolate(), "RoleType"),
149 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST));
151 additions->Set(
152 v8::String::NewFromUtf8(GetIsolate(), "StateType"),
153 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST));
155 additions->Set(
156 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"),
157 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST));
159 args.GetReturnValue().Set(additions);
162 void AutomationInternalCustomBindings::OnAccessibilityEvent(
163 const ExtensionMsg_AccessibilityEventParams& params) {
164 // TODO(dmazzoni): finish implementing this.
167 } // namespace extensions