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

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

Issue 10694108: Make EventBindings use static routed methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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/bind.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 // track of which filters are in effect for which events. 57 // track of which filters are in effect for which events.
58 // We notify the browser about filtered event listeners when we transition 58 // We notify the browser about filtered event listeners when we transition
59 // between 0 and 1. 59 // between 0 and 1.
60 typedef std::map<std::string, linked_ptr<extensions::ValueCounter> > 60 typedef std::map<std::string, linked_ptr<extensions::ValueCounter> >
61 FilteredEventListenerCounts; 61 FilteredEventListenerCounts;
62 62
63 // A map of extension IDs to filtered listener counts for that extension. 63 // A map of extension IDs to filtered listener counts for that extension.
64 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > 64 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
65 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; 65 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER;
66 66
67 base::LazyInstance<extensions::EventFilter> g_event_filter =
68 LAZY_INSTANCE_INITIALIZER;
69
67 // TODO(koz): Merge this into EventBindings. 70 // TODO(koz): Merge this into EventBindings.
68 class ExtensionImpl : public ChromeV8Extension { 71 class ExtensionImpl : public ChromeV8Extension {
69 public: 72 public:
70 73
71 ExtensionImpl(ExtensionDispatcher* dispatcher, 74 explicit ExtensionImpl(ExtensionDispatcher* dispatcher)
72 extensions::EventFilter* event_filter) 75 : ChromeV8Extension(dispatcher) {
73 : ChromeV8Extension(dispatcher), 76 RouteStaticFunction("AttachEvent", &AttachEvent);
74 event_filter_(event_filter) { 77 RouteStaticFunction("DetachEvent", &DetachEvent);
75 RouteFunction("AttachEvent", 78 RouteStaticFunction("AttachFilteredEvent", &AttachFilteredEvent);
76 base::Bind(&ExtensionImpl::AttachEvent, 79 RouteStaticFunction("DetachFilteredEvent", &DetachFilteredEvent);
77 base::Unretained(this))); 80 RouteStaticFunction("MatchAgainstEventFilter", &MatchAgainstEventFilter);
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)));
90 } 81 }
82
91 ~ExtensionImpl() {} 83 ~ExtensionImpl() {}
92 84
93 // Attach an event name to an object. 85 // Attach an event name to an object.
94 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { 86 static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
95 DCHECK(args.Length() == 1); 87 DCHECK(args.Length() == 1);
96 // TODO(erikkay) should enforce that event name is a string in the bindings 88 // TODO(erikkay) should enforce that event name is a string in the bindings
97 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 89 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
98 90
99 if (args[0]->IsString()) { 91 if (args[0]->IsString()) {
92 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
100 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); 93 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
94 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher();
101 const ChromeV8ContextSet& context_set = 95 const ChromeV8ContextSet& context_set =
102 extension_dispatcher()->v8_context_set(); 96 extension_dispatcher->v8_context_set();
103 ChromeV8Context* context = context_set.GetCurrent(); 97 ChromeV8Context* context = context_set.GetCurrent();
104 CHECK(context); 98 CHECK(context);
105 99
106 if (!extension_dispatcher()->CheckCurrentContextAccessToExtensionAPI( 100 if (!extension_dispatcher->CheckCurrentContextAccessToExtensionAPI(
107 event_name)) 101 event_name))
108 return v8::Undefined(); 102 return v8::Undefined();
109 103
110 std::string extension_id = context->GetExtensionID(); 104 std::string extension_id = context->GetExtensionID();
111 EventListenerCounts& listener_counts = 105 EventListenerCounts& listener_counts =
112 g_listener_counts.Get()[extension_id]; 106 g_listener_counts.Get()[extension_id];
113 if (++listener_counts[event_name] == 1) { 107 if (++listener_counts[event_name] == 1) {
114 content::RenderThread::Get()->Send( 108 content::RenderThread::Get()->Send(
115 new ExtensionHostMsg_AddListener(extension_id, event_name)); 109 new ExtensionHostMsg_AddListener(extension_id, event_name));
116 } 110 }
117 111
118 // This is called the first time the page has added a listener. Since 112 // This is called the first time the page has added a listener. Since
119 // the background page is the only lazy page, we know this is the first 113 // the background page is the only lazy page, we know this is the first
120 // time this listener has been registered. 114 // time this listener has been registered.
121 if (IsLazyBackgroundPage(context->extension())) { 115 if (IsLazyBackgroundPage(context->extension())) {
122 content::RenderThread::Get()->Send( 116 content::RenderThread::Get()->Send(
123 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); 117 new ExtensionHostMsg_AddLazyListener(extension_id, event_name));
124 } 118 }
125 } 119 }
126 return v8::Undefined(); 120 return v8::Undefined();
127 } 121 }
128 122
129 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { 123 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) {
130 DCHECK(args.Length() == 2); 124 DCHECK(args.Length() == 2);
131 // TODO(erikkay) should enforce that event name is a string in the bindings 125 // TODO(erikkay) should enforce that event name is a string in the bindings
132 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 126 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
133 127
134 if (args[0]->IsString() && args[1]->IsBoolean()) { 128 if (args[0]->IsString() && args[1]->IsBoolean()) {
135 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); 129 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
136 bool is_manual = args[1]->BooleanValue(); 130 bool is_manual = args[1]->BooleanValue();
137 131
132 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
133 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher();
138 const ChromeV8ContextSet& context_set = 134 const ChromeV8ContextSet& context_set =
139 extension_dispatcher()->v8_context_set(); 135 extension_dispatcher->v8_context_set();
140 ChromeV8Context* context = context_set.GetCurrent(); 136 ChromeV8Context* context = context_set.GetCurrent();
141 if (!context) 137 if (!context)
142 return v8::Undefined(); 138 return v8::Undefined();
143 139
144 std::string extension_id = context->GetExtensionID(); 140 std::string extension_id = context->GetExtensionID();
145 EventListenerCounts& listener_counts = 141 EventListenerCounts& listener_counts =
146 g_listener_counts.Get()[extension_id]; 142 g_listener_counts.Get()[extension_id];
147 143
148 if (--listener_counts[event_name] == 0) { 144 if (--listener_counts[event_name] == 0) {
149 content::RenderThread::Get()->Send( 145 content::RenderThread::Get()->Send(
(...skipping 10 matching lines...) Expand all
160 } 156 }
161 } 157 }
162 return v8::Undefined(); 158 return v8::Undefined();
163 } 159 }
164 160
165 // MatcherID AttachFilteredEvent(string event_name, object filter) 161 // MatcherID AttachFilteredEvent(string event_name, object filter)
166 // event_name - Name of the event to attach. 162 // event_name - Name of the event to attach.
167 // filter - Which instances of the named event are we interested in. 163 // 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 164 // returns the id assigned to the listener, which will be returned from calls
169 // to MatchAgainstEventFilter where this listener matches. 165 // to MatchAgainstEventFilter where this listener matches.
170 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) { 166 static v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) {
171 DCHECK_EQ(2, args.Length()); 167 DCHECK_EQ(2, args.Length());
172 DCHECK(args[0]->IsString()); 168 DCHECK(args[0]->IsString());
173 DCHECK(args[1]->IsObject()); 169 DCHECK(args[1]->IsObject());
174 170
171 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
172 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher();
175 const ChromeV8ContextSet& context_set = 173 const ChromeV8ContextSet& context_set =
176 extension_dispatcher()->v8_context_set(); 174 extension_dispatcher->v8_context_set();
177 ChromeV8Context* context = context_set.GetCurrent(); 175 ChromeV8Context* context = context_set.GetCurrent();
178 DCHECK(context); 176 DCHECK(context);
179 if (!context) 177 if (!context)
180 return v8::Integer::New(-1); 178 return v8::Integer::New(-1);
181 179
182 std::string event_name = *v8::String::AsciiValue(args[0]); 180 std::string event_name = *v8::String::AsciiValue(args[0]);
183 // This method throws an exception if it returns false. 181 // This method throws an exception if it returns false.
184 if (!extension_dispatcher()->CheckCurrentContextAccessToExtensionAPI( 182 if (!extension_dispatcher->CheckCurrentContextAccessToExtensionAPI(
185 event_name)) 183 event_name))
186 return v8::Undefined(); 184 return v8::Undefined();
187 185
188 std::string extension_id = context->GetExtensionID(); 186 std::string extension_id = context->GetExtensionID();
189 if (extension_id.empty()) 187 if (extension_id.empty())
190 return v8::Integer::New(-1); 188 return v8::Integer::New(-1);
191 189
192 scoped_ptr<base::DictionaryValue> filter; 190 scoped_ptr<base::DictionaryValue> filter;
193 scoped_ptr<content::V8ValueConverter> converter( 191 scoped_ptr<content::V8ValueConverter> converter(
194 content::V8ValueConverter::create()); 192 content::V8ValueConverter::create());
195 193
196 base::DictionaryValue* filter_dict = NULL; 194 base::DictionaryValue* filter_dict = NULL;
197 base::Value* filter_value = converter->FromV8Value(args[1]->ToObject(), 195 base::Value* filter_value = converter->FromV8Value(args[1]->ToObject(),
198 v8::Context::GetCurrent()); 196 v8::Context::GetCurrent());
199 if (!filter_value->GetAsDictionary(&filter_dict)) { 197 if (!filter_value->GetAsDictionary(&filter_dict)) {
200 delete filter_value; 198 delete filter_value;
201 return v8::Integer::New(-1); 199 return v8::Integer::New(-1);
202 } 200 }
203 201
204 filter.reset(filter_dict); 202 filter.reset(filter_dict);
205 int id = event_filter_->AddEventMatcher(event_name, ParseEventMatcher( 203 extensions::EventFilter& event_filter = g_event_filter.Get();
204 int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher(
206 filter.get())); 205 filter.get()));
207 206
208 // Only send IPCs the first time a filter gets added. 207 // Only send IPCs the first time a filter gets added.
209 if (AddFilter(event_name, extension_id, filter.get())) { 208 if (AddFilter(event_name, extension_id, filter.get())) {
210 bool lazy = IsLazyBackgroundPage(context->extension()); 209 bool lazy = IsLazyBackgroundPage(context->extension());
211 content::RenderThread::Get()->Send( 210 content::RenderThread::Get()->Send(
212 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name, 211 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name,
213 *filter, lazy)); 212 *filter, lazy));
214 } 213 }
215 214
216 return v8::Integer::New(id); 215 return v8::Integer::New(id);
217 } 216 }
218 217
219 // Add a filter to |event_name| in |extension_id|, returning true if it 218 // Add a filter to |event_name| in |extension_id|, returning true if it
220 // was the first filter for that event in that extension. 219 // was the first filter for that event in that extension.
221 bool AddFilter(const std::string& event_name, 220 static bool AddFilter(const std::string& event_name,
222 const std::string& extension_id, 221 const std::string& extension_id,
223 base::DictionaryValue* filter) { 222 base::DictionaryValue* filter) {
224 FilteredEventListenerCounts& counts = 223 FilteredEventListenerCounts& counts =
225 g_filtered_listener_counts.Get()[extension_id]; 224 g_filtered_listener_counts.Get()[extension_id];
226 FilteredEventListenerCounts::iterator it = counts.find(event_name); 225 FilteredEventListenerCounts::iterator it = counts.find(event_name);
227 if (it == counts.end()) 226 if (it == counts.end())
228 counts[event_name].reset(new extensions::ValueCounter); 227 counts[event_name].reset(new extensions::ValueCounter);
229 228
230 int result = counts[event_name]->Add(*filter); 229 int result = counts[event_name]->Add(*filter);
231 return 1 == result; 230 return 1 == result;
232 } 231 }
233 232
234 // Remove a filter from |event_name| in |extension_id|, returning true if it 233 // Remove a filter from |event_name| in |extension_id|, returning true if it
235 // was the last filter for that event in that extension. 234 // was the last filter for that event in that extension.
236 bool RemoveFilter(const std::string& event_name, 235 static bool RemoveFilter(const std::string& event_name,
237 const std::string& extension_id, 236 const std::string& extension_id,
238 base::DictionaryValue* filter) { 237 base::DictionaryValue* filter) {
239 FilteredEventListenerCounts& counts = 238 FilteredEventListenerCounts& counts =
240 g_filtered_listener_counts.Get()[extension_id]; 239 g_filtered_listener_counts.Get()[extension_id];
241 FilteredEventListenerCounts::iterator it = counts.find(event_name); 240 FilteredEventListenerCounts::iterator it = counts.find(event_name);
242 if (it == counts.end()) 241 if (it == counts.end())
243 return false; 242 return false;
244 return 0 == it->second->Remove(*filter); 243 return 0 == it->second->Remove(*filter);
245 } 244 }
246 245
247 // void DetachFilteredEvent(int id, bool manual) 246 // void DetachFilteredEvent(int id, bool manual)
248 // id - Id of the event to detach. 247 // id - Id of the event to detach.
249 // manual - false if this is part of the extension unload process where all 248 // manual - false if this is part of the extension unload process where all
250 // listeners are automatically detached. 249 // listeners are automatically detached.
251 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) { 250 static v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) {
252 DCHECK_EQ(2, args.Length()); 251 DCHECK_EQ(2, args.Length());
253 DCHECK(args[0]->IsInt32()); 252 DCHECK(args[0]->IsInt32());
254 DCHECK(args[1]->IsBoolean()); 253 DCHECK(args[1]->IsBoolean());
255 bool is_manual = args[1]->BooleanValue(); 254 bool is_manual = args[1]->BooleanValue();
255 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
256 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher();
256 const ChromeV8ContextSet& context_set = 257 const ChromeV8ContextSet& context_set =
257 extension_dispatcher()->v8_context_set(); 258 extension_dispatcher->v8_context_set();
258 ChromeV8Context* context = context_set.GetCurrent(); 259 ChromeV8Context* context = context_set.GetCurrent();
259 if (!context) 260 if (!context)
260 return v8::Undefined(); 261 return v8::Undefined();
261 262
262 std::string extension_id = context->GetExtensionID(); 263 std::string extension_id = context->GetExtensionID();
263 if (extension_id.empty()) 264 if (extension_id.empty())
264 return v8::Undefined(); 265 return v8::Undefined();
265 266
266 int matcher_id = args[0]->Int32Value(); 267 int matcher_id = args[0]->Int32Value();
268 extensions::EventFilter& event_filter = g_event_filter.Get();
267 extensions::EventMatcher* event_matcher = 269 extensions::EventMatcher* event_matcher =
268 event_filter_->GetEventMatcher(matcher_id); 270 event_filter.GetEventMatcher(matcher_id);
269 271
270 const std::string& event_name = event_filter_->GetEventName(matcher_id); 272 const std::string& event_name = event_filter.GetEventName(matcher_id);
271 273
272 // Only send IPCs the last time a filter gets removed. 274 // Only send IPCs the last time a filter gets removed.
273 if (RemoveFilter(event_name, extension_id, event_matcher->value())) { 275 if (RemoveFilter(event_name, extension_id, event_matcher->value())) {
274 bool lazy = is_manual && IsLazyBackgroundPage(context->extension()); 276 bool lazy = is_manual && IsLazyBackgroundPage(context->extension());
275 content::RenderThread::Get()->Send( 277 content::RenderThread::Get()->Send(
276 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name, 278 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name,
277 *event_matcher->value(), 279 *event_matcher->value(),
278 lazy)); 280 lazy));
279 } 281 }
280 282
281 event_filter_->RemoveEventMatcher(matcher_id); 283 event_filter.RemoveEventMatcher(matcher_id);
282 284
283 return v8::Undefined(); 285 return v8::Undefined();
284 } 286 }
285 287
286 v8::Handle<v8::Value> MatchAgainstEventFilter(const v8::Arguments& args) { 288 static v8::Handle<v8::Value> MatchAgainstEventFilter(
289 const v8::Arguments& args) {
287 typedef std::set<extensions::EventFilter::MatcherID> MatcherIDs; 290 typedef std::set<extensions::EventFilter::MatcherID> MatcherIDs;
288 291
292 extensions::EventFilter& event_filter = g_event_filter.Get();
289 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); 293 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
290 extensions::EventFilteringInfo info = ParseFromObject(args[1]->ToObject()); 294 extensions::EventFilteringInfo info = ParseFromObject(args[1]->ToObject());
291 MatcherIDs matched_event_filters = event_filter_->MatchEvent( 295 MatcherIDs matched_event_filters = event_filter.MatchEvent(
292 event_name, info); 296 event_name, info);
293 v8::Handle<v8::Array> array(v8::Array::New(matched_event_filters.size())); 297 v8::Handle<v8::Array> array(v8::Array::New(matched_event_filters.size()));
294 int i = 0; 298 int i = 0;
295 for (MatcherIDs::iterator it = matched_event_filters.begin(); 299 for (MatcherIDs::iterator it = matched_event_filters.begin();
296 it != matched_event_filters.end(); ++it) { 300 it != matched_event_filters.end(); ++it) {
297 array->Set(v8::Integer::New(i++), v8::Integer::New(*it)); 301 array->Set(v8::Integer::New(i++), v8::Integer::New(*it));
298 } 302 }
299 return array; 303 return array;
300 } 304 }
301 305
302 extensions::EventFilteringInfo ParseFromObject( 306 static extensions::EventFilteringInfo ParseFromObject(
303 v8::Handle<v8::Object> object) { 307 v8::Handle<v8::Object> object) {
304 extensions::EventFilteringInfo info; 308 extensions::EventFilteringInfo info;
305 v8::Handle<v8::String> url(v8::String::New("url")); 309 v8::Handle<v8::String> url(v8::String::New("url"));
306 if (object->Has(url)) { 310 if (object->Has(url)) {
307 v8::Handle<v8::Value> url_value(object->Get(url)); 311 v8::Handle<v8::Value> url_value(object->Get(url));
308 info.SetURL(GURL(*v8::String::AsciiValue(url_value))); 312 info.SetURL(GURL(*v8::String::AsciiValue(url_value)));
309 } 313 }
310 return info; 314 return info;
311 } 315 }
312 316
313 private: 317 private:
314 extensions::EventFilter* event_filter_; 318 static bool IsLazyBackgroundPage(const Extension* extension) {
315 bool IsLazyBackgroundPage(const Extension* extension) {
316 content::RenderView* render_view = GetCurrentRenderView(); 319 content::RenderView* render_view = GetCurrentRenderView();
317 if (!render_view) 320 if (!render_view)
318 return false; 321 return false;
319 322
320 ExtensionHelper* helper = ExtensionHelper::Get(render_view); 323 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
321 return (extension && extension->has_lazy_background_page() && 324 return (extension && extension->has_lazy_background_page() &&
322 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); 325 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
323 } 326 }
324 327
325 scoped_ptr<extensions::EventMatcher> ParseEventMatcher( 328 static scoped_ptr<extensions::EventMatcher> ParseEventMatcher(
326 base::DictionaryValue* filter_dict) { 329 base::DictionaryValue* filter_dict) {
327 return scoped_ptr<extensions::EventMatcher>(new extensions::EventMatcher( 330 return scoped_ptr<extensions::EventMatcher>(new extensions::EventMatcher(
328 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()))); 331 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy())));
329 } 332 }
330 }; 333 };
331 334
332 } // namespace 335 } // namespace
333 336
334 // static 337 // static
335 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher, 338 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) {
336 extensions::EventFilter* event_filter) { 339 return new ExtensionImpl(dispatcher);
337 return new ExtensionImpl(dispatcher, event_filter);
338 } 340 }
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/event_bindings.h ('k') | chrome/renderer/extensions/extension_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698