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"
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"
25 // Helper to convert an enum to a V8 object.
26 template <typename EnumType
>
27 v8::Local
<v8::Object
> ToEnumObject(v8::Isolate
* isolate
,
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
);
41 namespace extensions
{
43 class AutomationMessageFilter
: public IPC::MessageFilter
{
45 explicit AutomationMessageFilter(AutomationInternalCustomBindings
* owner
)
49 content::RenderThread::Get()->AddFilter(this);
58 bool OnMessageReceived(const IPC::Message
& message
) override
{
60 return owner_
->OnMessageReceived(message
);
65 void OnFilterRemoved() override
{
70 ~AutomationMessageFilter() override
{
77 content::RenderThread::Get()->RemoveFilter(this);
81 AutomationInternalCustomBindings
* owner_
;
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.
93 "IsInteractPermitted",
94 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted
,
95 base::Unretained(this)));
98 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions
,
99 base::Unretained(this)));
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.
123 void AutomationInternalCustomBindings::IsInteractPermitted(
124 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
125 const Extension
* extension
= context()->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());
144 v8::String::NewFromUtf8(GetIsolate(), "EventType"),
145 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE
, ui::AX_EVENT_LAST
));
148 v8::String::NewFromUtf8(GetIsolate(), "RoleType"),
149 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE
, ui::AX_ROLE_LAST
));
152 v8::String::NewFromUtf8(GetIsolate(), "StateType"),
153 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE
, ui::AX_STATE_LAST
));
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