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/manifest_handlers/automation.h" | 11 #include "chrome/common/extensions/manifest_handlers/automation.h" |
11 #include "content/public/child/v8_value_converter.h" | 12 #include "content/public/child/v8_value_converter.h" |
| 13 #include "content/public/renderer/render_thread.h" |
| 14 #include "content/public/renderer/render_view.h" |
12 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
13 #include "extensions/common/manifest.h" | 16 #include "extensions/common/manifest.h" |
14 #include "extensions/renderer/script_context.h" | 17 #include "extensions/renderer/script_context.h" |
| 18 #include "ipc/message_filter.h" |
15 #include "ui/accessibility/ax_enums.h" | 19 #include "ui/accessibility/ax_enums.h" |
| 20 #include "ui/accessibility/ax_node.h" |
16 | 21 |
17 namespace { | 22 namespace { |
18 | 23 |
19 // Helper to convert an enum to a V8 object. | 24 // Helper to convert an enum to a V8 object. |
20 template <typename EnumType> | 25 template <typename EnumType> |
21 v8::Local<v8::Object> ToEnumObject(v8::Isolate* isolate, | 26 v8::Local<v8::Object> ToEnumObject(v8::Isolate* isolate, |
22 EnumType start_after, | 27 EnumType start_after, |
23 EnumType end_at) { | 28 EnumType end_at) { |
24 v8::Local<v8::Object> object = v8::Object::New(isolate); | 29 v8::Local<v8::Object> object = v8::Object::New(isolate); |
25 for (int i = start_after + 1; i <= end_at; ++i) { | 30 for (int i = start_after + 1; i <= end_at; ++i) { |
26 v8::Local<v8::String> value = v8::String::NewFromUtf8( | 31 v8::Local<v8::String> value = v8::String::NewFromUtf8( |
27 isolate, ui::ToString(static_cast<EnumType>(i)).c_str()); | 32 isolate, ui::ToString(static_cast<EnumType>(i)).c_str()); |
28 object->Set(value, value); | 33 object->Set(value, value); |
29 } | 34 } |
30 return object; | 35 return object; |
31 } | 36 } |
32 | 37 |
33 } // namespace | 38 } // namespace |
34 | 39 |
35 namespace extensions { | 40 namespace extensions { |
36 | 41 |
| 42 class AutomationMessageFilter : public IPC::MessageFilter { |
| 43 public: |
| 44 explicit AutomationMessageFilter(AutomationInternalCustomBindings* owner) |
| 45 : owner_(owner) { |
| 46 DCHECK(owner); |
| 47 content::RenderThread::Get()->AddFilter(this); |
| 48 } |
| 49 |
| 50 void Detach() { |
| 51 owner_ = nullptr; |
| 52 } |
| 53 |
| 54 // IPC::MessageFilter |
| 55 bool OnMessageReceived(const IPC::Message& message) override { |
| 56 if (owner_) |
| 57 return owner_->OnMessageReceived(message); |
| 58 else |
| 59 return false; |
| 60 } |
| 61 |
| 62 private: |
| 63 ~AutomationMessageFilter() override { |
| 64 content::RenderThread::Get()->RemoveFilter(this); |
| 65 } |
| 66 |
| 67 AutomationInternalCustomBindings* owner_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter); |
| 70 }; |
| 71 |
37 AutomationInternalCustomBindings::AutomationInternalCustomBindings( | 72 AutomationInternalCustomBindings::AutomationInternalCustomBindings( |
38 ScriptContext* context) : ObjectBackedNativeHandler(context) { | 73 ScriptContext* context) : ObjectBackedNativeHandler(context) { |
| 74 // It's safe to use base::Unretained(this) here because these bindings |
| 75 // will only be called on a valid AutomationInternalCustomBindings instance |
| 76 // and none of the functions have any side effects. |
39 RouteFunction( | 77 RouteFunction( |
40 "IsInteractPermitted", | 78 "IsInteractPermitted", |
41 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted, | 79 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted, |
42 base::Unretained(this))); | 80 base::Unretained(this))); |
43 RouteFunction( | 81 RouteFunction( |
44 "GetSchemaAdditions", | 82 "GetSchemaAdditions", |
45 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions, | 83 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions, |
46 base::Unretained(this))); | 84 base::Unretained(this))); |
| 85 RouteFunction( |
| 86 "GetRoutingID", |
| 87 base::Bind(&AutomationInternalCustomBindings::GetRoutingID, |
| 88 base::Unretained(this))); |
| 89 |
| 90 message_filter_ = new AutomationMessageFilter(this); |
47 } | 91 } |
48 | 92 |
49 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { | 93 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { |
| 94 message_filter_->Detach(); |
| 95 } |
| 96 |
| 97 bool AutomationInternalCustomBindings::OnMessageReceived( |
| 98 const IPC::Message& message) { |
| 99 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message) |
| 100 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent) |
| 101 IPC_END_MESSAGE_MAP() |
| 102 |
| 103 // Always return false in case there are multiple |
| 104 // AutomationInternalCustomBindings instances attached to the same thread. |
| 105 return false; |
50 } | 106 } |
51 | 107 |
52 void AutomationInternalCustomBindings::IsInteractPermitted( | 108 void AutomationInternalCustomBindings::IsInteractPermitted( |
53 const v8::FunctionCallbackInfo<v8::Value>& args) { | 109 const v8::FunctionCallbackInfo<v8::Value>& args) { |
54 const Extension* extension = context()->extension(); | 110 const Extension* extension = context()->extension(); |
55 CHECK(extension); | 111 CHECK(extension); |
56 const AutomationInfo* automation_info = AutomationInfo::Get(extension); | 112 const AutomationInfo* automation_info = AutomationInfo::Get(extension); |
57 CHECK(automation_info); | 113 CHECK(automation_info); |
58 args.GetReturnValue().Set( | 114 args.GetReturnValue().Set( |
59 v8::Boolean::New(GetIsolate(), automation_info->interact)); | 115 v8::Boolean::New(GetIsolate(), automation_info->interact)); |
60 } | 116 } |
61 | 117 |
| 118 void AutomationInternalCustomBindings::GetRoutingID( |
| 119 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 120 int routing_id = context()->GetRenderView()->GetRoutingID(); |
| 121 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id)); |
| 122 } |
| 123 |
62 void AutomationInternalCustomBindings::GetSchemaAdditions( | 124 void AutomationInternalCustomBindings::GetSchemaAdditions( |
63 const v8::FunctionCallbackInfo<v8::Value>& args) { | 125 const v8::FunctionCallbackInfo<v8::Value>& args) { |
64 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); | 126 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); |
65 | 127 |
66 additions->Set( | 128 additions->Set( |
67 v8::String::NewFromUtf8(GetIsolate(), "EventType"), | 129 v8::String::NewFromUtf8(GetIsolate(), "EventType"), |
68 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); | 130 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); |
69 | 131 |
70 additions->Set( | 132 additions->Set( |
71 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), | 133 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), |
72 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); | 134 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); |
73 | 135 |
74 additions->Set( | 136 additions->Set( |
75 v8::String::NewFromUtf8(GetIsolate(), "StateType"), | 137 v8::String::NewFromUtf8(GetIsolate(), "StateType"), |
76 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); | 138 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); |
77 | 139 |
78 additions->Set( | 140 additions->Set( |
79 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), | 141 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), |
80 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); | 142 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); |
81 | 143 |
82 args.GetReturnValue().Set(additions); | 144 args.GetReturnValue().Set(additions); |
83 } | 145 } |
84 | 146 |
| 147 void AutomationInternalCustomBindings::OnAccessibilityEvent( |
| 148 const ExtensionMsg_AccessibilityEventParams& params) { |
| 149 // TODO(dmazzoni): finish implementing this. |
| 150 } |
| 151 |
85 } // namespace extensions | 152 } // namespace extensions |
OLD | NEW |