OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/event_bindings.h" | 5 #include "chrome/renderer/extensions/event_bindings.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "chrome/common/extensions/value_counter.h" | 17 #include "chrome/common/extensions/value_counter.h" |
18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
19 #include "chrome/renderer/extensions/chrome_v8_context.h" | 19 #include "chrome/renderer/extensions/chrome_v8_context.h" |
20 #include "chrome/renderer/extensions/chrome_v8_context_set.h" | 20 #include "chrome/renderer/extensions/chrome_v8_context_set.h" |
21 #include "chrome/renderer/extensions/chrome_v8_extension.h" | |
22 #include "chrome/renderer/extensions/dispatcher.h" | 21 #include "chrome/renderer/extensions/dispatcher.h" |
23 #include "chrome/renderer/extensions/extension_helper.h" | 22 #include "chrome/renderer/extensions/extension_helper.h" |
24 #include "chrome/renderer/extensions/user_script_slave.h" | 23 #include "chrome/renderer/extensions/user_script_slave.h" |
25 #include "content/public/renderer/render_thread.h" | 24 #include "content/public/renderer/render_thread.h" |
26 #include "content/public/renderer/render_view.h" | 25 #include "content/public/renderer/render_view.h" |
27 #include "content/public/renderer/v8_value_converter.h" | 26 #include "content/public/renderer/v8_value_converter.h" |
28 #include "extensions/common/event_filter.h" | 27 #include "extensions/common/event_filter.h" |
29 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
30 #include "extensions/common/extension_messages.h" | 29 #include "extensions/common/extension_messages.h" |
31 #include "extensions/common/manifest_handlers/background_info.h" | 30 #include "extensions/common/manifest_handlers/background_info.h" |
(...skipping 30 matching lines...) Expand all Loading... |
62 // between 0 and 1. | 61 // between 0 and 1. |
63 typedef std::map<std::string, linked_ptr<ValueCounter> > | 62 typedef std::map<std::string, linked_ptr<ValueCounter> > |
64 FilteredEventListenerCounts; | 63 FilteredEventListenerCounts; |
65 | 64 |
66 // A map of extension IDs to filtered listener counts for that extension. | 65 // A map of extension IDs to filtered listener counts for that extension. |
67 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > | 66 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > |
68 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; | 67 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; |
69 | 68 |
70 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; | 69 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; |
71 | 70 |
72 // TODO(koz): Merge this into EventBindings. | 71 bool IsLazyBackgroundPage(content::RenderView* render_view, |
73 class ExtensionImpl : public ChromeV8Extension { | 72 const Extension* extension) { |
74 public: | 73 if (!render_view) |
75 explicit ExtensionImpl(Dispatcher* dispatcher, ChromeV8Context* context) | 74 return false; |
76 : ChromeV8Extension(dispatcher, context) { | 75 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
77 RouteFunction("AttachEvent", | 76 return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) && |
78 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this))); | 77 helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
79 RouteFunction("DetachEvent", | 78 } |
80 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this))); | 79 |
81 RouteFunction("AttachFilteredEvent", | 80 EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object, |
82 base::Bind(&ExtensionImpl::AttachFilteredEvent, | 81 v8::Isolate* isolate) { |
83 base::Unretained(this))); | 82 EventFilteringInfo info; |
84 RouteFunction("DetachFilteredEvent", | 83 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url")); |
85 base::Bind(&ExtensionImpl::DetachFilteredEvent, | 84 if (object->Has(url)) { |
86 base::Unretained(this))); | 85 v8::Handle<v8::Value> url_value(object->Get(url)); |
87 RouteFunction("MatchAgainstEventFilter", | 86 info.SetURL(GURL(*v8::String::Utf8Value(url_value))); |
88 base::Bind(&ExtensionImpl::MatchAgainstEventFilter, | 87 } |
89 base::Unretained(this))); | 88 v8::Handle<v8::String> instance_id( |
90 } | 89 v8::String::NewFromUtf8(isolate, "instanceId")); |
91 | 90 if (object->Has(instance_id)) { |
92 virtual ~ExtensionImpl() {} | 91 v8::Handle<v8::Value> instance_id_value(object->Get(instance_id)); |
93 | 92 info.SetInstanceID(instance_id_value->IntegerValue()); |
94 // Attach an event name to an object. | 93 } |
95 void AttachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | 94 v8::Handle<v8::String> service_type( |
96 CHECK_EQ(1, args.Length()); | 95 v8::String::NewFromUtf8(isolate, "serviceType")); |
97 CHECK(args[0]->IsString()); | 96 if (object->Has(service_type)) { |
98 | 97 v8::Handle<v8::Value> service_type_value(object->Get(service_type)); |
99 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); | 98 info.SetServiceType(*v8::String::Utf8Value(service_type_value)); |
100 | 99 } |
101 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) | 100 return info; |
102 return; | 101 } |
103 | 102 |
104 std::string extension_id = context()->GetExtensionID(); | 103 // Add a filter to |event_name| in |extension_id|, returning true if it |
105 EventListenerCounts& listener_counts = | 104 // was the first filter for that event in that extension. |
106 g_listener_counts.Get()[extension_id]; | 105 bool AddFilter(const std::string& event_name, |
107 if (++listener_counts[event_name] == 1) { | 106 const std::string& extension_id, |
108 content::RenderThread::Get()->Send( | 107 base::DictionaryValue* filter) { |
109 new ExtensionHostMsg_AddListener(extension_id, event_name)); | 108 FilteredEventListenerCounts& counts = |
110 } | 109 g_filtered_listener_counts.Get()[extension_id]; |
111 | 110 FilteredEventListenerCounts::iterator it = counts.find(event_name); |
112 // This is called the first time the page has added a listener. Since | 111 if (it == counts.end()) |
113 // the background page is the only lazy page, we know this is the first | 112 counts[event_name].reset(new ValueCounter); |
114 // time this listener has been registered. | 113 |
115 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) { | 114 int result = counts[event_name]->Add(*filter); |
116 content::RenderThread::Get()->Send( | 115 return 1 == result; |
117 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); | 116 } |
118 } | 117 |
119 } | 118 // Remove a filter from |event_name| in |extension_id|, returning true if it |
120 | 119 // was the last filter for that event in that extension. |
121 void DetachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | 120 bool RemoveFilter(const std::string& event_name, |
122 CHECK_EQ(2, args.Length()); | 121 const std::string& extension_id, |
123 CHECK(args[0]->IsString()); | 122 base::DictionaryValue* filter) { |
124 CHECK(args[1]->IsBoolean()); | 123 FilteredEventListenerCounts& counts = |
125 | 124 g_filtered_listener_counts.Get()[extension_id]; |
126 std::string event_name = *v8::String::Utf8Value(args[0]); | 125 FilteredEventListenerCounts::iterator it = counts.find(event_name); |
127 bool is_manual = args[1]->BooleanValue(); | 126 if (it == counts.end()) |
128 | 127 return false; |
129 std::string extension_id = context()->GetExtensionID(); | 128 return 0 == it->second->Remove(*filter); |
130 EventListenerCounts& listener_counts = | 129 } |
131 g_listener_counts.Get()[extension_id]; | |
132 | |
133 if (--listener_counts[event_name] == 0) { | |
134 content::RenderThread::Get()->Send( | |
135 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); | |
136 } | |
137 | |
138 // DetachEvent is called when the last listener for the context is | |
139 // removed. If the context is the background page, and it removes the | |
140 // last listener manually, then we assume that it is no longer interested | |
141 // in being awakened for this event. | |
142 if (is_manual && IsLazyBackgroundPage(GetRenderView(), | |
143 context()->extension())) { | |
144 content::RenderThread::Get()->Send( | |
145 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); | |
146 } | |
147 } | |
148 | |
149 // MatcherID AttachFilteredEvent(string event_name, object filter) | |
150 // event_name - Name of the event to attach. | |
151 // filter - Which instances of the named event are we interested in. | |
152 // returns the id assigned to the listener, which will be returned from calls | |
153 // to MatchAgainstEventFilter where this listener matches. | |
154 void AttachFilteredEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
155 CHECK_EQ(2, args.Length()); | |
156 CHECK(args[0]->IsString()); | |
157 CHECK(args[1]->IsObject()); | |
158 | |
159 std::string event_name = *v8::String::Utf8Value(args[0]); | |
160 | |
161 // This method throws an exception if it returns false. | |
162 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) | |
163 return; | |
164 | |
165 std::string extension_id = context()->GetExtensionID(); | |
166 if (extension_id.empty()) { | |
167 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | |
168 return; | |
169 } | |
170 | |
171 scoped_ptr<base::DictionaryValue> filter; | |
172 scoped_ptr<content::V8ValueConverter> converter( | |
173 content::V8ValueConverter::create()); | |
174 | |
175 base::DictionaryValue* filter_dict = NULL; | |
176 base::Value* filter_value = | |
177 converter->FromV8Value(args[1]->ToObject(), context()->v8_context()); | |
178 if (!filter_value) { | |
179 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | |
180 return; | |
181 } | |
182 if (!filter_value->GetAsDictionary(&filter_dict)) { | |
183 delete filter_value; | |
184 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | |
185 return; | |
186 } | |
187 | |
188 filter.reset(filter_dict); | |
189 EventFilter& event_filter = g_event_filter.Get(); | |
190 int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher( | |
191 filter.get())); | |
192 | |
193 // Only send IPCs the first time a filter gets added. | |
194 if (AddFilter(event_name, extension_id, filter.get())) { | |
195 bool lazy = IsLazyBackgroundPage(GetRenderView(), context()->extension()); | |
196 content::RenderThread::Get()->Send( | |
197 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name, | |
198 *filter, lazy)); | |
199 } | |
200 | |
201 args.GetReturnValue().Set(static_cast<int32_t>(id)); | |
202 } | |
203 | |
204 // Add a filter to |event_name| in |extension_id|, returning true if it | |
205 // was the first filter for that event in that extension. | |
206 static bool AddFilter(const std::string& event_name, | |
207 const std::string& extension_id, | |
208 base::DictionaryValue* filter) { | |
209 FilteredEventListenerCounts& counts = | |
210 g_filtered_listener_counts.Get()[extension_id]; | |
211 FilteredEventListenerCounts::iterator it = counts.find(event_name); | |
212 if (it == counts.end()) | |
213 counts[event_name].reset(new ValueCounter); | |
214 | |
215 int result = counts[event_name]->Add(*filter); | |
216 return 1 == result; | |
217 } | |
218 | |
219 // Remove a filter from |event_name| in |extension_id|, returning true if it | |
220 // was the last filter for that event in that extension. | |
221 static bool RemoveFilter(const std::string& event_name, | |
222 const std::string& extension_id, | |
223 base::DictionaryValue* filter) { | |
224 FilteredEventListenerCounts& counts = | |
225 g_filtered_listener_counts.Get()[extension_id]; | |
226 FilteredEventListenerCounts::iterator it = counts.find(event_name); | |
227 if (it == counts.end()) | |
228 return false; | |
229 return 0 == it->second->Remove(*filter); | |
230 } | |
231 | |
232 // void DetachFilteredEvent(int id, bool manual) | |
233 // id - Id of the event to detach. | |
234 // manual - false if this is part of the extension unload process where all | |
235 // listeners are automatically detached. | |
236 void DetachFilteredEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
237 CHECK_EQ(2, args.Length()); | |
238 CHECK(args[0]->IsInt32()); | |
239 CHECK(args[1]->IsBoolean()); | |
240 bool is_manual = args[1]->BooleanValue(); | |
241 | |
242 std::string extension_id = context()->GetExtensionID(); | |
243 if (extension_id.empty()) | |
244 return; | |
245 | |
246 int matcher_id = args[0]->Int32Value(); | |
247 EventFilter& event_filter = g_event_filter.Get(); | |
248 EventMatcher* event_matcher = | |
249 event_filter.GetEventMatcher(matcher_id); | |
250 | |
251 const std::string& event_name = event_filter.GetEventName(matcher_id); | |
252 | |
253 // Only send IPCs the last time a filter gets removed. | |
254 if (RemoveFilter(event_name, extension_id, event_matcher->value())) { | |
255 bool lazy = is_manual && IsLazyBackgroundPage(GetRenderView(), | |
256 context()->extension()); | |
257 content::RenderThread::Get()->Send( | |
258 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name, | |
259 *event_matcher->value(), | |
260 lazy)); | |
261 } | |
262 | |
263 event_filter.RemoveEventMatcher(matcher_id); | |
264 } | |
265 | |
266 void MatchAgainstEventFilter( | |
267 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
268 v8::Isolate* isolate = args.GetIsolate(); | |
269 typedef std::set<EventFilter::MatcherID> MatcherIDs; | |
270 EventFilter& event_filter = g_event_filter.Get(); | |
271 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); | |
272 EventFilteringInfo info = | |
273 ParseFromObject(args[1]->ToObject(), isolate); | |
274 // Only match events routed to this context's RenderView or ones that don't | |
275 // have a routingId in their filter. | |
276 MatcherIDs matched_event_filters = event_filter.MatchEvent( | |
277 event_name, info, context()->GetRenderView()->GetRoutingID()); | |
278 v8::Handle<v8::Array> array( | |
279 v8::Array::New(isolate, matched_event_filters.size())); | |
280 int i = 0; | |
281 for (MatcherIDs::iterator it = matched_event_filters.begin(); | |
282 it != matched_event_filters.end(); ++it) { | |
283 array->Set(v8::Integer::New(isolate, i++), | |
284 v8::Integer::New(isolate, *it)); | |
285 } | |
286 args.GetReturnValue().Set(array); | |
287 } | |
288 | |
289 static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object, | |
290 v8::Isolate* isolate) { | |
291 EventFilteringInfo info; | |
292 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url")); | |
293 if (object->Has(url)) { | |
294 v8::Handle<v8::Value> url_value(object->Get(url)); | |
295 info.SetURL(GURL(*v8::String::Utf8Value(url_value))); | |
296 } | |
297 v8::Handle<v8::String> instance_id( | |
298 v8::String::NewFromUtf8(isolate, "instanceId")); | |
299 if (object->Has(instance_id)) { | |
300 v8::Handle<v8::Value> instance_id_value(object->Get(instance_id)); | |
301 info.SetInstanceID(instance_id_value->IntegerValue()); | |
302 } | |
303 v8::Handle<v8::String> service_type( | |
304 v8::String::NewFromUtf8(isolate, "serviceType")); | |
305 if (object->Has(service_type)) { | |
306 v8::Handle<v8::Value> service_type_value(object->Get(service_type)); | |
307 info.SetServiceType(*v8::String::Utf8Value(service_type_value)); | |
308 } | |
309 return info; | |
310 } | |
311 | |
312 private: | |
313 static bool IsLazyBackgroundPage(content::RenderView* render_view, | |
314 const Extension* extension) { | |
315 if (!render_view) | |
316 return false; | |
317 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | |
318 return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) && | |
319 helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | |
320 } | |
321 | |
322 scoped_ptr<EventMatcher> ParseEventMatcher( | |
323 base::DictionaryValue* filter_dict) { | |
324 return scoped_ptr<EventMatcher>(new EventMatcher( | |
325 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()), | |
326 context()->GetRenderView()->GetRoutingID())); | |
327 } | |
328 }; | |
329 | 130 |
330 } // namespace | 131 } // namespace |
331 | 132 |
332 // static | 133 EventBindings::EventBindings(Dispatcher* dispatcher, ChromeV8Context* context) |
333 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, | 134 : ChromeV8Extension(dispatcher, context) { |
334 ChromeV8Context* context) { | 135 RouteFunction( |
335 return new ExtensionImpl(dispatcher, context); | 136 "AttachEvent", |
| 137 base::Bind(&EventBindings::AttachEvent, base::Unretained(this))); |
| 138 RouteFunction( |
| 139 "DetachEvent", |
| 140 base::Bind(&EventBindings::DetachEvent, base::Unretained(this))); |
| 141 RouteFunction( |
| 142 "AttachFilteredEvent", |
| 143 base::Bind(&EventBindings::AttachFilteredEvent, base::Unretained(this))); |
| 144 RouteFunction( |
| 145 "DetachFilteredEvent", |
| 146 base::Bind(&EventBindings::DetachFilteredEvent, base::Unretained(this))); |
| 147 RouteFunction("MatchAgainstEventFilter", |
| 148 base::Bind(&EventBindings::MatchAgainstEventFilter, |
| 149 base::Unretained(this))); |
| 150 } |
| 151 |
| 152 EventBindings::~EventBindings() {} |
| 153 |
| 154 // Attach an event name to an object. |
| 155 void EventBindings::AttachEvent( |
| 156 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 157 CHECK_EQ(1, args.Length()); |
| 158 CHECK(args[0]->IsString()); |
| 159 |
| 160 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 161 |
| 162 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) |
| 163 return; |
| 164 |
| 165 std::string extension_id = context()->GetExtensionID(); |
| 166 EventListenerCounts& listener_counts = g_listener_counts.Get()[extension_id]; |
| 167 if (++listener_counts[event_name] == 1) { |
| 168 content::RenderThread::Get()->Send( |
| 169 new ExtensionHostMsg_AddListener(extension_id, event_name)); |
| 170 } |
| 171 |
| 172 // This is called the first time the page has added a listener. Since |
| 173 // the background page is the only lazy page, we know this is the first |
| 174 // time this listener has been registered. |
| 175 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) { |
| 176 content::RenderThread::Get()->Send( |
| 177 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); |
| 178 } |
| 179 } |
| 180 |
| 181 void EventBindings::DetachEvent( |
| 182 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 183 CHECK_EQ(2, args.Length()); |
| 184 CHECK(args[0]->IsString()); |
| 185 CHECK(args[1]->IsBoolean()); |
| 186 |
| 187 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 188 bool is_manual = args[1]->BooleanValue(); |
| 189 |
| 190 std::string extension_id = context()->GetExtensionID(); |
| 191 EventListenerCounts& listener_counts = g_listener_counts.Get()[extension_id]; |
| 192 |
| 193 if (--listener_counts[event_name] == 0) { |
| 194 content::RenderThread::Get()->Send( |
| 195 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); |
| 196 } |
| 197 |
| 198 // DetachEvent is called when the last listener for the context is |
| 199 // removed. If the context is the background page, and it removes the |
| 200 // last listener manually, then we assume that it is no longer interested |
| 201 // in being awakened for this event. |
| 202 if (is_manual && |
| 203 IsLazyBackgroundPage(GetRenderView(), context()->extension())) { |
| 204 content::RenderThread::Get()->Send( |
| 205 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); |
| 206 } |
| 207 } |
| 208 |
| 209 // MatcherID AttachFilteredEvent(string event_name, object filter) |
| 210 // event_name - Name of the event to attach. |
| 211 // filter - Which instances of the named event are we interested in. |
| 212 // returns the id assigned to the listener, which will be returned from calls |
| 213 // to MatchAgainstEventFilter where this listener matches. |
| 214 void EventBindings::AttachFilteredEvent( |
| 215 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 216 CHECK_EQ(2, args.Length()); |
| 217 CHECK(args[0]->IsString()); |
| 218 CHECK(args[1]->IsObject()); |
| 219 |
| 220 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 221 |
| 222 // This method throws an exception if it returns false. |
| 223 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) |
| 224 return; |
| 225 |
| 226 std::string extension_id = context()->GetExtensionID(); |
| 227 if (extension_id.empty()) { |
| 228 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 229 return; |
| 230 } |
| 231 |
| 232 scoped_ptr<base::DictionaryValue> filter; |
| 233 scoped_ptr<content::V8ValueConverter> converter( |
| 234 content::V8ValueConverter::create()); |
| 235 |
| 236 base::DictionaryValue* filter_dict = NULL; |
| 237 base::Value* filter_value = |
| 238 converter->FromV8Value(args[1]->ToObject(), context()->v8_context()); |
| 239 if (!filter_value) { |
| 240 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 241 return; |
| 242 } |
| 243 if (!filter_value->GetAsDictionary(&filter_dict)) { |
| 244 delete filter_value; |
| 245 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 246 return; |
| 247 } |
| 248 |
| 249 filter.reset(filter_dict); |
| 250 EventFilter& event_filter = g_event_filter.Get(); |
| 251 int id = |
| 252 event_filter.AddEventMatcher(event_name, ParseEventMatcher(filter.get())); |
| 253 |
| 254 // Only send IPCs the first time a filter gets added. |
| 255 if (AddFilter(event_name, extension_id, filter.get())) { |
| 256 bool lazy = IsLazyBackgroundPage(GetRenderView(), context()->extension()); |
| 257 content::RenderThread::Get()->Send(new ExtensionHostMsg_AddFilteredListener( |
| 258 extension_id, event_name, *filter, lazy)); |
| 259 } |
| 260 |
| 261 args.GetReturnValue().Set(static_cast<int32_t>(id)); |
| 262 } |
| 263 |
| 264 void EventBindings::DetachFilteredEvent( |
| 265 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 266 CHECK_EQ(2, args.Length()); |
| 267 CHECK(args[0]->IsInt32()); |
| 268 CHECK(args[1]->IsBoolean()); |
| 269 bool is_manual = args[1]->BooleanValue(); |
| 270 |
| 271 std::string extension_id = context()->GetExtensionID(); |
| 272 if (extension_id.empty()) |
| 273 return; |
| 274 |
| 275 int matcher_id = args[0]->Int32Value(); |
| 276 EventFilter& event_filter = g_event_filter.Get(); |
| 277 EventMatcher* event_matcher = event_filter.GetEventMatcher(matcher_id); |
| 278 |
| 279 const std::string& event_name = event_filter.GetEventName(matcher_id); |
| 280 |
| 281 // Only send IPCs the last time a filter gets removed. |
| 282 if (RemoveFilter(event_name, extension_id, event_matcher->value())) { |
| 283 bool lazy = is_manual && |
| 284 IsLazyBackgroundPage(GetRenderView(), context()->extension()); |
| 285 content::RenderThread::Get()->Send( |
| 286 new ExtensionHostMsg_RemoveFilteredListener( |
| 287 extension_id, event_name, *event_matcher->value(), lazy)); |
| 288 } |
| 289 |
| 290 event_filter.RemoveEventMatcher(matcher_id); |
| 291 } |
| 292 |
| 293 void EventBindings::MatchAgainstEventFilter( |
| 294 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 295 v8::Isolate* isolate = args.GetIsolate(); |
| 296 typedef std::set<EventFilter::MatcherID> MatcherIDs; |
| 297 EventFilter& event_filter = g_event_filter.Get(); |
| 298 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 299 EventFilteringInfo info = ParseFromObject(args[1]->ToObject(), isolate); |
| 300 // Only match events routed to this context's RenderView or ones that don't |
| 301 // have a routingId in their filter. |
| 302 MatcherIDs matched_event_filters = event_filter.MatchEvent( |
| 303 event_name, info, context()->GetRenderView()->GetRoutingID()); |
| 304 v8::Handle<v8::Array> array( |
| 305 v8::Array::New(isolate, matched_event_filters.size())); |
| 306 int i = 0; |
| 307 for (MatcherIDs::iterator it = matched_event_filters.begin(); |
| 308 it != matched_event_filters.end(); |
| 309 ++it) { |
| 310 array->Set(v8::Integer::New(isolate, i++), v8::Integer::New(isolate, *it)); |
| 311 } |
| 312 args.GetReturnValue().Set(array); |
| 313 } |
| 314 |
| 315 scoped_ptr<EventMatcher> EventBindings::ParseEventMatcher( |
| 316 base::DictionaryValue* filter_dict) { |
| 317 return scoped_ptr<EventMatcher>(new EventMatcher( |
| 318 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()), |
| 319 context()->GetRenderView()->GetRoutingID())); |
336 } | 320 } |
337 | 321 |
338 } // namespace extensions | 322 } // namespace extensions |
OLD | NEW |