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

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

Powered by Google App Engine
This is Rietveld 408576698