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) { | |
dcheng
2015/06/06 00:00:52
Is it valid to call this constructor with no owner
dmazzoni
2015/06/08 18:02:58
Done.
| |
46 content::RenderThread::Get()->AddFilter(this); | |
47 } | |
48 | |
49 void Detach() { | |
dcheng
2015/06/06 00:00:52
Who calls Detach()?
dmazzoni
2015/06/08 18:02:58
Oops, meant to call this in ~AutomationInternalCus
| |
50 owner_ = nullptr; | |
51 } | |
52 | |
53 // IPC::MessageFilter | |
54 bool OnMessageReceived(const IPC::Message& message) override { | |
55 if (owner_) | |
56 return owner_->OnMessageReceived(message); | |
57 else | |
58 return false; | |
59 } | |
60 | |
61 private: | |
62 ~AutomationMessageFilter() override { | |
63 content::RenderThread* render_thread = content::RenderThread::Get(); | |
dcheng
2015/06/06 00:00:52
Why would this be null? To me, this conditional ma
dmazzoni
2015/06/08 18:02:57
Looks like it shouldn't be null, removed the check
| |
64 if (render_thread) | |
65 render_thread->RemoveFilter(this); | |
66 } | |
67 | |
68 AutomationInternalCustomBindings* owner_; | |
dcheng
2015/06/06 00:00:52
Since we don't use Detach() anymore, make this an
dmazzoni
2015/06/08 18:02:58
I think we need Detach() because the message filte
| |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(AutomationMessageFilter); | |
71 }; | |
72 | |
37 AutomationInternalCustomBindings::AutomationInternalCustomBindings( | 73 AutomationInternalCustomBindings::AutomationInternalCustomBindings( |
38 ScriptContext* context) : ObjectBackedNativeHandler(context) { | 74 ScriptContext* context) : ObjectBackedNativeHandler(context) { |
39 RouteFunction( | 75 RouteFunction( |
40 "IsInteractPermitted", | 76 "IsInteractPermitted", |
41 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted, | 77 base::Bind(&AutomationInternalCustomBindings::IsInteractPermitted, |
42 base::Unretained(this))); | 78 base::Unretained(this))); |
43 RouteFunction( | 79 RouteFunction( |
44 "GetSchemaAdditions", | 80 "GetSchemaAdditions", |
45 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions, | 81 base::Bind(&AutomationInternalCustomBindings::GetSchemaAdditions, |
46 base::Unretained(this))); | 82 base::Unretained(this))); |
83 RouteFunction( | |
84 "GetRoutingID", | |
85 base::Bind(&AutomationInternalCustomBindings::GetRoutingID, | |
86 base::Unretained(this))); | |
dcheng
2015/06/06 00:00:52
In general, it's nice to document why base::Unreta
dmazzoni
2015/06/08 18:02:58
Done.
| |
87 | |
88 message_filter_ = new AutomationMessageFilter(this); | |
47 } | 89 } |
48 | 90 |
49 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { | 91 AutomationInternalCustomBindings::~AutomationInternalCustomBindings() { |
50 } | 92 } |
51 | 93 |
94 bool AutomationInternalCustomBindings::OnMessageReceived( | |
95 const IPC::Message& message) { | |
96 IPC_BEGIN_MESSAGE_MAP(AutomationInternalCustomBindings, message) | |
97 IPC_MESSAGE_HANDLER(ExtensionMsg_AccessibilityEvent, OnAccessibilityEvent) | |
98 IPC_END_MESSAGE_MAP() | |
99 | |
100 // Always return false in case there are multiple | |
101 // AutomationInternalCustomBindings instances attached to the same thread. | |
102 return false; | |
103 } | |
104 | |
52 void AutomationInternalCustomBindings::IsInteractPermitted( | 105 void AutomationInternalCustomBindings::IsInteractPermitted( |
53 const v8::FunctionCallbackInfo<v8::Value>& args) { | 106 const v8::FunctionCallbackInfo<v8::Value>& args) { |
54 const Extension* extension = context()->extension(); | 107 const Extension* extension = context()->extension(); |
55 CHECK(extension); | 108 CHECK(extension); |
56 const AutomationInfo* automation_info = AutomationInfo::Get(extension); | 109 const AutomationInfo* automation_info = AutomationInfo::Get(extension); |
57 CHECK(automation_info); | 110 CHECK(automation_info); |
58 args.GetReturnValue().Set( | 111 args.GetReturnValue().Set( |
59 v8::Boolean::New(GetIsolate(), automation_info->interact)); | 112 v8::Boolean::New(GetIsolate(), automation_info->interact)); |
60 } | 113 } |
61 | 114 |
115 void AutomationInternalCustomBindings::GetRoutingID( | |
116 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
117 int routing_id = context()->GetRenderView()->GetRoutingID(); | |
118 args.GetReturnValue().Set(v8::Integer::New(GetIsolate(), routing_id)); | |
119 } | |
120 | |
62 void AutomationInternalCustomBindings::GetSchemaAdditions( | 121 void AutomationInternalCustomBindings::GetSchemaAdditions( |
63 const v8::FunctionCallbackInfo<v8::Value>& args) { | 122 const v8::FunctionCallbackInfo<v8::Value>& args) { |
64 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); | 123 v8::Local<v8::Object> additions = v8::Object::New(GetIsolate()); |
65 | 124 |
66 additions->Set( | 125 additions->Set( |
67 v8::String::NewFromUtf8(GetIsolate(), "EventType"), | 126 v8::String::NewFromUtf8(GetIsolate(), "EventType"), |
68 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); | 127 ToEnumObject(GetIsolate(), ui::AX_EVENT_NONE, ui::AX_EVENT_LAST)); |
69 | 128 |
70 additions->Set( | 129 additions->Set( |
71 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), | 130 v8::String::NewFromUtf8(GetIsolate(), "RoleType"), |
72 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); | 131 ToEnumObject(GetIsolate(), ui::AX_ROLE_NONE, ui::AX_ROLE_LAST)); |
73 | 132 |
74 additions->Set( | 133 additions->Set( |
75 v8::String::NewFromUtf8(GetIsolate(), "StateType"), | 134 v8::String::NewFromUtf8(GetIsolate(), "StateType"), |
76 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); | 135 ToEnumObject(GetIsolate(), ui::AX_STATE_NONE, ui::AX_STATE_LAST)); |
77 | 136 |
78 additions->Set( | 137 additions->Set( |
79 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), | 138 v8::String::NewFromUtf8(GetIsolate(), "TreeChangeType"), |
80 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); | 139 ToEnumObject(GetIsolate(), ui::AX_MUTATION_NONE, ui::AX_MUTATION_LAST)); |
81 | 140 |
82 args.GetReturnValue().Set(additions); | 141 args.GetReturnValue().Set(additions); |
83 } | 142 } |
84 | 143 |
144 void AutomationInternalCustomBindings::OnAccessibilityEvent( | |
145 const ExtensionMsg_AccessibilityEventParams& params) { | |
146 // TODO(dmazzoni): finish implementing this. | |
dcheng
2015/06/06 00:00:52
Please add me as a reviewer on the change where yo
dmazzoni
2015/06/08 18:02:57
Done: https://codereview.chromium.org/1155183006/
| |
147 } | |
148 | |
85 } // namespace extensions | 149 } // namespace extensions |
OLD | NEW |