Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/renderer/extensions/event_bindings.cc

Issue 10514013: Filtered events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase, reland Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <vector> 7 #include <vector>
8 8
9 #include "base/bind.h"
9 #include "base/basictypes.h" 10 #include "base/basictypes.h"
10 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 13 #include "base/message_loop.h"
12 #include "chrome/common/extensions/extension_messages.h" 14 #include "chrome/common/extensions/extension_messages.h"
13 #include "chrome/common/extensions/extension_set.h" 15 #include "chrome/common/extensions/extension_set.h"
16 #include "chrome/common/extensions/event_filter.h"
17 #include "chrome/common/extensions/value_counter.h"
14 #include "chrome/common/url_constants.h" 18 #include "chrome/common/url_constants.h"
15 #include "chrome/common/view_type.h" 19 #include "chrome/common/view_type.h"
16 #include "chrome/renderer/extensions/chrome_v8_context.h" 20 #include "chrome/renderer/extensions/chrome_v8_context.h"
17 #include "chrome/renderer/extensions/chrome_v8_context_set.h" 21 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
18 #include "chrome/renderer/extensions/chrome_v8_extension.h" 22 #include "chrome/renderer/extensions/chrome_v8_extension.h"
19 #include "chrome/renderer/extensions/event_bindings.h" 23 #include "chrome/renderer/extensions/event_bindings.h"
20 #include "chrome/renderer/extensions/extension_dispatcher.h" 24 #include "chrome/renderer/extensions/extension_dispatcher.h"
21 #include "chrome/renderer/extensions/extension_helper.h" 25 #include "chrome/renderer/extensions/extension_helper.h"
22 #include "chrome/renderer/extensions/user_script_slave.h" 26 #include "chrome/renderer/extensions/user_script_slave.h"
23 #include "content/public/renderer/render_thread.h" 27 #include "content/public/renderer/render_thread.h"
28 #include "content/public/renderer/v8_value_converter.h"
24 #include "googleurl/src/gurl.h" 29 #include "googleurl/src/gurl.h"
25 #include "grit/renderer_resources.h" 30 #include "grit/renderer_resources.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" 34 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" 35 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
32 #include "v8/include/v8.h" 37 #include "v8/include/v8.h"
33 38
34 using WebKit::WebFrame; 39 using WebKit::WebFrame;
35 using WebKit::WebSecurityOrigin; 40 using WebKit::WebSecurityOrigin;
36 using WebKit::WebURL; 41 using WebKit::WebURL;
37 using content::RenderThread; 42 using content::RenderThread;
38 using extensions::Extension; 43 using extensions::Extension;
39 44
40 namespace { 45 namespace {
41 46
42 // A map of event names to the number of contexts listening to that event. 47 // A map of event names to the number of contexts listening to that event.
43 // We notify the browser about event listeners when we transition between 0 48 // We notify the browser about event listeners when we transition between 0
44 // and 1. 49 // and 1.
45 typedef std::map<std::string, int> EventListenerCounts; 50 typedef std::map<std::string, int> EventListenerCounts;
46 51
47 // A map of extension IDs to listener counts for that extension. 52 // A map of extension IDs to listener counts for that extension.
48 base::LazyInstance<std::map<std::string, EventListenerCounts> > 53 base::LazyInstance<std::map<std::string, EventListenerCounts> >
49 g_listener_counts = LAZY_INSTANCE_INITIALIZER; 54 g_listener_counts = LAZY_INSTANCE_INITIALIZER;
50 55
56 // A map of event names to a (filter -> count) map. The map is used to keep
57 // track of which filters are in effect for which events.
58 // We notify the browser about filtered event listeners when we transition
59 // between 0 and 1.
60 typedef std::map<std::string, linked_ptr<extensions::ValueCounter> >
61 FilteredEventListenerCounts;
62
63 // A map of extension IDs to filtered listener counts for that extension.
64 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
65 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER;
66
51 // TODO(koz): Merge this into EventBindings. 67 // TODO(koz): Merge this into EventBindings.
52 class ExtensionImpl : public ChromeV8Extension { 68 class ExtensionImpl : public ChromeV8Extension {
53 public: 69 public:
54 70
55 explicit ExtensionImpl(ExtensionDispatcher* dispatcher) 71 ExtensionImpl(ExtensionDispatcher* dispatcher,
56 : ChromeV8Extension(dispatcher) { 72 extensions::EventFilter* event_filter)
57 RouteStaticFunction("AttachEvent", &AttachEvent); 73 : ChromeV8Extension(dispatcher),
58 RouteStaticFunction("DetachEvent", &DetachEvent); 74 event_filter_(event_filter) {
75 RouteFunction("AttachEvent",
76 base::Bind(&ExtensionImpl::AttachEvent,
77 base::Unretained(this)));
78 RouteFunction("DetachEvent",
79 base::Bind(&ExtensionImpl::DetachEvent,
80 base::Unretained(this)));
81 RouteFunction("AttachFilteredEvent",
82 base::Bind(&ExtensionImpl::AttachFilteredEvent,
83 base::Unretained(this)));
84 RouteFunction("DetachFilteredEvent",
85 base::Bind(&ExtensionImpl::DetachFilteredEvent,
86 base::Unretained(this)));
87 RouteFunction("MatchAgainstEventFilter",
88 base::Bind(&ExtensionImpl::MatchAgainstEventFilter,
89 base::Unretained(this)));
59 } 90 }
60 ~ExtensionImpl() {} 91 ~ExtensionImpl() {}
61 92
62 // Attach an event name to an object. 93 // Attach an event name to an object.
63 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { 94 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
64 DCHECK(args.Length() == 1); 95 DCHECK(args.Length() == 1);
65 // TODO(erikkay) should enforce that event name is a string in the bindings 96 // TODO(erikkay) should enforce that event name is a string in the bindings
66 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 97 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
67 98
68 if (args[0]->IsString()) { 99 if (args[0]->IsString()) {
69 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); 100 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
70 const ChromeV8ContextSet& context_set = 101 const ChromeV8ContextSet& context_set =
71 self->extension_dispatcher()->v8_context_set(); 102 extension_dispatcher()->v8_context_set();
72 ChromeV8Context* context = context_set.GetCurrent(); 103 ChromeV8Context* context = context_set.GetCurrent();
73 CHECK(context); 104 CHECK(context);
74 std::string event_name(*v8::String::AsciiValue(args[0]));
75 105
76 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher(); 106 if (!extension_dispatcher()->CheckCurrentContextAccessToExtensionAPI(
77 if (!extension_dispatcher->CheckCurrentContextAccessToExtensionAPI(
78 event_name)) 107 event_name))
79 return v8::Undefined(); 108 return v8::Undefined();
80 109
81 std::string extension_id = context->GetExtensionID(); 110 std::string extension_id = context->GetExtensionID();
82 EventListenerCounts& listener_counts = 111 EventListenerCounts& listener_counts =
83 g_listener_counts.Get()[extension_id]; 112 g_listener_counts.Get()[extension_id];
84 if (++listener_counts[event_name] == 1) { 113 if (++listener_counts[event_name] == 1) {
85 content::RenderThread::Get()->Send( 114 content::RenderThread::Get()->Send(
86 new ExtensionHostMsg_AddListener(extension_id, event_name)); 115 new ExtensionHostMsg_AddListener(extension_id, event_name));
87 } 116 }
88 117
89 // This is called the first time the page has added a listener. Since 118 // This is called the first time the page has added a listener. Since
90 // the background page is the only lazy page, we know this is the first 119 // the background page is the only lazy page, we know this is the first
91 // time this listener has been registered. 120 // time this listener has been registered.
92 if (self->IsLazyBackgroundPage(context->extension())) { 121 if (IsLazyBackgroundPage(context->extension())) {
93 content::RenderThread::Get()->Send( 122 content::RenderThread::Get()->Send(
94 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); 123 new ExtensionHostMsg_AddLazyListener(extension_id, event_name));
95 } 124 }
96 } 125 }
97
98 return v8::Undefined(); 126 return v8::Undefined();
99 } 127 }
100 128
101 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { 129 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) {
102 DCHECK(args.Length() == 2); 130 DCHECK(args.Length() == 2);
103 // TODO(erikkay) should enforce that event name is a string in the bindings 131 // TODO(erikkay) should enforce that event name is a string in the bindings
104 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 132 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
105 133
106 if (args[0]->IsString() && args[1]->IsBoolean()) { 134 if (args[0]->IsString() && args[1]->IsBoolean()) {
107 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); 135 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
136 bool is_manual = args[1]->BooleanValue();
137
108 const ChromeV8ContextSet& context_set = 138 const ChromeV8ContextSet& context_set =
109 self->extension_dispatcher()->v8_context_set(); 139 extension_dispatcher()->v8_context_set();
110 ChromeV8Context* context = context_set.GetCurrent(); 140 ChromeV8Context* context = context_set.GetCurrent();
111 if (!context) 141 if (!context)
112 return v8::Undefined(); 142 return v8::Undefined();
113 143
114 std::string extension_id = context->GetExtensionID(); 144 std::string extension_id = context->GetExtensionID();
115 EventListenerCounts& listener_counts = 145 EventListenerCounts& listener_counts =
116 g_listener_counts.Get()[extension_id]; 146 g_listener_counts.Get()[extension_id];
117 std::string event_name(*v8::String::AsciiValue(args[0]));
118 bool is_manual = args[1]->BooleanValue();
119 147
120 if (--listener_counts[event_name] == 0) { 148 if (--listener_counts[event_name] == 0) {
121 content::RenderThread::Get()->Send( 149 content::RenderThread::Get()->Send(
122 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); 150 new ExtensionHostMsg_RemoveListener(extension_id, event_name));
123 } 151 }
124 152
125 // DetachEvent is called when the last listener for the context is 153 // DetachEvent is called when the last listener for the context is
126 // removed. If the context is the background page, and it removes the 154 // removed. If the context is the background page, and it removes the
127 // last listener manually, then we assume that it is no longer interested 155 // last listener manually, then we assume that it is no longer interested
128 // in being awakened for this event. 156 // in being awakened for this event.
129 if (is_manual && self->IsLazyBackgroundPage(context->extension())) { 157 if (is_manual && IsLazyBackgroundPage(context->extension())) {
130 content::RenderThread::Get()->Send( 158 content::RenderThread::Get()->Send(
131 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); 159 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name));
132 } 160 }
133 } 161 }
162 return v8::Undefined();
163 }
164
165 // MatcherID AttachFilteredEvent(string event_name, object filter)
166 // event_name - Name of the event to attach.
167 // filter - Which instances of the named event are we interested in.
168 // returns the id assigned to the listener, which will be returned from calls
169 // to MatchAgainstEventFilter where this listener matches.
170 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) {
171 DCHECK_EQ(2, args.Length());
172 DCHECK(args[0]->IsString());
173 DCHECK(args[1]->IsObject());
174
175 const ChromeV8ContextSet& context_set =
176 extension_dispatcher()->v8_context_set();
177 ChromeV8Context* context = context_set.GetCurrent();
178 DCHECK(context);
179 if (!context)
180 return v8::Integer::New(-1);
181
182 std::string event_name = *v8::String::AsciiValue(args[0]);
183 // This method throws an exception if it returns false.
184 if (!extension_dispatcher()->CheckCurrentContextAccessToExtensionAPI(
185 event_name))
186 return v8::Undefined();
187
188 std::string extension_id = context->GetExtensionID();
189 if (extension_id.empty())
190 return v8::Integer::New(-1);
191
192 scoped_ptr<base::DictionaryValue> filter;
193 scoped_ptr<content::V8ValueConverter> converter(
194 content::V8ValueConverter::create());
195
196 base::DictionaryValue* filter_dict = NULL;
197 base::Value* filter_value = converter->FromV8Value(args[1]->ToObject(),
198 v8::Context::GetCurrent());
199 if (!filter_value->GetAsDictionary(&filter_dict)) {
200 delete filter_value;
201 return v8::Integer::New(-1);
202 }
203
204 filter.reset(filter_dict);
205 int id = event_filter_->AddEventMatcher(event_name, ParseEventMatcher(
206 filter.get()));
207
208 // Only send IPCs the first time a filter gets added.
209 if (AddFilter(event_name, extension_id, filter.get())) {
210 bool lazy = IsLazyBackgroundPage(context->extension());
211 content::RenderThread::Get()->Send(
212 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name,
213 *filter, lazy));
214 }
215
216 return v8::Integer::New(id);
217 }
218
219 // Add a filter to |event_name| in |extension_id|, returning true if it
220 // was the first filter for that event in that extension.
221 bool AddFilter(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 counts[event_name].reset(new extensions::ValueCounter);
229
230 int result = counts[event_name]->Add(*filter);
231 return 1 == result;
232 }
233
234 // Remove a filter from |event_name| in |extension_id|, returning true if it
235 // was the last filter for that event in that extension.
236 bool RemoveFilter(const std::string& event_name,
237 const std::string& extension_id,
238 base::DictionaryValue* filter) {
239 FilteredEventListenerCounts& counts =
240 g_filtered_listener_counts.Get()[extension_id];
241 FilteredEventListenerCounts::iterator it = counts.find(event_name);
242 if (it == counts.end())
243 return false;
244 return 0 == it->second->Remove(*filter);
245 }
246
247 // void DetachFilteredEvent(int id, bool manual)
248 // id - Id of the event to detach.
249 // manual - false if this is part of the extension unload process where all
250 // listeners are automatically detached.
251 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) {
252 DCHECK_EQ(2, args.Length());
253 DCHECK(args[0]->IsInt32());
254 DCHECK(args[1]->IsBoolean());
255 bool is_manual = args[1]->BooleanValue();
256 const ChromeV8ContextSet& context_set =
257 extension_dispatcher()->v8_context_set();
258 ChromeV8Context* context = context_set.GetCurrent();
259 if (!context)
260 return v8::Undefined();
261
262 std::string extension_id = context->GetExtensionID();
263 if (extension_id.empty())
264 return v8::Undefined();
265
266 int matcher_id = args[0]->Int32Value();
267 extensions::EventMatcher* event_matcher =
268 event_filter_->GetEventMatcher(matcher_id);
269
270 const std::string& event_name = event_filter_->GetEventName(matcher_id);
271
272 // Only send IPCs the last time a filter gets removed.
273 if (RemoveFilter(event_name, extension_id, event_matcher->value())) {
274 bool lazy = is_manual && IsLazyBackgroundPage(context->extension());
275 content::RenderThread::Get()->Send(
276 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name,
277 *event_matcher->value(),
278 lazy));
279 }
280
281 event_filter_->RemoveEventMatcher(matcher_id);
134 282
135 return v8::Undefined(); 283 return v8::Undefined();
136 } 284 }
137 285
286 v8::Handle<v8::Value> MatchAgainstEventFilter(const v8::Arguments& args) {
287 typedef std::set<extensions::EventFilter::MatcherID> MatcherIDs;
288
289 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
290 extensions::EventFilteringInfo info = ParseFromObject(args[1]->ToObject());
291 MatcherIDs matched_event_filters = event_filter_->MatchEvent(
292 event_name, info);
293 v8::Handle<v8::Array> array(v8::Array::New(matched_event_filters.size()));
294 int i = 0;
295 for (MatcherIDs::iterator it = matched_event_filters.begin();
296 it != matched_event_filters.end(); ++it) {
297 array->Set(v8::Integer::New(i++), v8::Integer::New(*it));
298 }
299 return array;
300 }
301
302 extensions::EventFilteringInfo ParseFromObject(
303 v8::Handle<v8::Object> object) {
304 extensions::EventFilteringInfo info;
305 v8::Handle<v8::String> url(v8::String::New("url"));
306 if (object->Has(url)) {
307 v8::Handle<v8::Value> url_value(object->Get(url));
308 info.SetURL(GURL(*v8::String::AsciiValue(url_value)));
309 }
310 return info;
311 }
312
138 private: 313 private:
139 314 extensions::EventFilter* event_filter_;
140 bool IsLazyBackgroundPage(const Extension* extension) { 315 bool IsLazyBackgroundPage(const Extension* extension) {
141 content::RenderView* render_view = GetCurrentRenderView(); 316 content::RenderView* render_view = GetCurrentRenderView();
142 if (!render_view) 317 if (!render_view)
143 return false; 318 return false;
144 319
145 ExtensionHelper* helper = ExtensionHelper::Get(render_view); 320 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
146 return (extension && extension->has_lazy_background_page() && 321 return (extension && extension->has_lazy_background_page() &&
147 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); 322 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
148 } 323 }
324
325 scoped_ptr<extensions::EventMatcher> ParseEventMatcher(
326 base::DictionaryValue* filter_dict) {
327 return scoped_ptr<extensions::EventMatcher>(new extensions::EventMatcher(
328 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy())));
329 }
149 }; 330 };
150 331
151 } // namespace 332 } // namespace
152 333
153 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) { 334 // static
154 return new ExtensionImpl(dispatcher); 335 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher,
336 extensions::EventFilter* event_filter) {
337 return new ExtensionImpl(dispatcher, event_filter);
155 } 338 }
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/event_bindings.h ('k') | chrome/renderer/extensions/event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698