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