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 "extensions/browser/event_router.h" | 5 #include "extensions/browser/event_router.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 listener->GetBrowserContext()); | 248 listener->GetBrowserContext()); |
249 std::string base_event_name = GetBaseEventName(listener->event_name()); | 249 std::string base_event_name = GetBaseEventName(listener->event_name()); |
250 ObserverMap::iterator observer = observers_.find(base_event_name); | 250 ObserverMap::iterator observer = observers_.find(base_event_name); |
251 if (observer != observers_.end()) | 251 if (observer != observers_.end()) |
252 observer->second->OnListenerRemoved(details); | 252 observer->second->OnListenerRemoved(details); |
253 } | 253 } |
254 | 254 |
255 void EventRouter::AddLazyEventListener(const std::string& event_name, | 255 void EventRouter::AddLazyEventListener(const std::string& event_name, |
256 const std::string& extension_id) { | 256 const std::string& extension_id) { |
257 bool is_new = listeners_.AddListener(EventListener::ForExtension( | 257 bool is_new = listeners_.AddListener(EventListener::ForExtension( |
258 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>())); | 258 event_name, extension_id, nullptr, scoped_ptr<DictionaryValue>())); |
259 | 259 |
260 if (is_new) { | 260 if (is_new) { |
261 std::set<std::string> events = GetRegisteredEvents(extension_id); | 261 std::set<std::string> events = GetRegisteredEvents(extension_id); |
262 bool prefs_is_new = events.insert(event_name).second; | 262 bool prefs_is_new = events.insert(event_name).second; |
263 if (prefs_is_new) | 263 if (prefs_is_new) |
264 SetRegisteredEvents(extension_id, events); | 264 SetRegisteredEvents(extension_id, events); |
265 } | 265 } |
266 } | 266 } |
267 | 267 |
268 void EventRouter::RemoveLazyEventListener(const std::string& event_name, | 268 void EventRouter::RemoveLazyEventListener(const std::string& event_name, |
269 const std::string& extension_id) { | 269 const std::string& extension_id) { |
270 scoped_ptr<EventListener> listener = EventListener::ForExtension( | 270 scoped_ptr<EventListener> listener = EventListener::ForExtension( |
271 event_name, extension_id, NULL, scoped_ptr<DictionaryValue>()); | 271 event_name, extension_id, nullptr, scoped_ptr<DictionaryValue>()); |
272 bool did_exist = listeners_.RemoveListener(listener.get()); | 272 bool did_exist = listeners_.RemoveListener(listener.get()); |
273 | 273 |
274 if (did_exist) { | 274 if (did_exist) { |
275 std::set<std::string> events = GetRegisteredEvents(extension_id); | 275 std::set<std::string> events = GetRegisteredEvents(extension_id); |
276 bool prefs_did_exist = events.erase(event_name) > 0; | 276 bool prefs_did_exist = events.erase(event_name) > 0; |
277 DCHECK(prefs_did_exist); | 277 DCHECK(prefs_did_exist); |
278 SetRegisteredEvents(extension_id, events); | 278 SetRegisteredEvents(extension_id, events); |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 void EventRouter::AddFilteredEventListener(const std::string& event_name, | 282 void EventRouter::AddFilteredEventListener(const std::string& event_name, |
283 content::RenderProcessHost* process, | 283 content::RenderProcessHost* process, |
284 const std::string& extension_id, | 284 const std::string& extension_id, |
285 const base::DictionaryValue& filter, | 285 const base::DictionaryValue& filter, |
286 bool add_lazy_listener) { | 286 bool add_lazy_listener) { |
287 listeners_.AddListener(EventListener::ForExtension( | 287 listeners_.AddListener(EventListener::ForExtension( |
288 event_name, | 288 event_name, |
289 extension_id, | 289 extension_id, |
290 process, | 290 process, |
291 scoped_ptr<DictionaryValue>(filter.DeepCopy()))); | 291 scoped_ptr<DictionaryValue>(filter.DeepCopy()))); |
292 | 292 |
293 if (add_lazy_listener) { | 293 if (add_lazy_listener) { |
294 bool added = listeners_.AddListener(EventListener::ForExtension( | 294 bool added = listeners_.AddListener(EventListener::ForExtension( |
295 event_name, | 295 event_name, |
296 extension_id, | 296 extension_id, |
297 NULL, | 297 nullptr, |
298 scoped_ptr<DictionaryValue>(filter.DeepCopy()))); | 298 scoped_ptr<DictionaryValue>(filter.DeepCopy()))); |
299 | 299 |
300 if (added) | 300 if (added) |
301 AddFilterToEvent(event_name, extension_id, &filter); | 301 AddFilterToEvent(event_name, extension_id, &filter); |
302 } | 302 } |
303 } | 303 } |
304 | 304 |
305 void EventRouter::RemoveFilteredEventListener( | 305 void EventRouter::RemoveFilteredEventListener( |
306 const std::string& event_name, | 306 const std::string& event_name, |
307 content::RenderProcessHost* process, | 307 content::RenderProcessHost* process, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 listener != listeners.end(); ++listener) { | 349 listener != listeners.end(); ++listener) { |
350 if (listener->extension_id == extension_id) | 350 if (listener->extension_id == extension_id) |
351 return true; | 351 return true; |
352 } | 352 } |
353 return false; | 353 return false; |
354 } | 354 } |
355 | 355 |
356 std::set<std::string> EventRouter::GetRegisteredEvents( | 356 std::set<std::string> EventRouter::GetRegisteredEvents( |
357 const std::string& extension_id) { | 357 const std::string& extension_id) { |
358 std::set<std::string> events; | 358 std::set<std::string> events; |
359 const ListValue* events_value = NULL; | 359 const ListValue* events_value = nullptr; |
360 | 360 |
361 if (!extension_prefs_ || | 361 if (!extension_prefs_ || |
362 !extension_prefs_->ReadPrefAsList( | 362 !extension_prefs_->ReadPrefAsList( |
363 extension_id, kRegisteredEvents, &events_value)) { | 363 extension_id, kRegisteredEvents, &events_value)) { |
364 return events; | 364 return events; |
365 } | 365 } |
366 | 366 |
367 for (size_t i = 0; i < events_value->GetSize(); ++i) { | 367 for (size_t i = 0; i < events_value->GetSize(); ++i) { |
368 std::string event; | 368 std::string event; |
369 if (events_value->GetString(i, &event)) | 369 if (events_value->GetString(i, &event)) |
(...skipping 15 matching lines...) Expand all Loading... |
385 | 385 |
386 void EventRouter::AddFilterToEvent(const std::string& event_name, | 386 void EventRouter::AddFilterToEvent(const std::string& event_name, |
387 const std::string& extension_id, | 387 const std::string& extension_id, |
388 const DictionaryValue* filter) { | 388 const DictionaryValue* filter) { |
389 ExtensionPrefs::ScopedDictionaryUpdate update( | 389 ExtensionPrefs::ScopedDictionaryUpdate update( |
390 extension_prefs_, extension_id, kFilteredEvents); | 390 extension_prefs_, extension_id, kFilteredEvents); |
391 DictionaryValue* filtered_events = update.Get(); | 391 DictionaryValue* filtered_events = update.Get(); |
392 if (!filtered_events) | 392 if (!filtered_events) |
393 filtered_events = update.Create(); | 393 filtered_events = update.Create(); |
394 | 394 |
395 ListValue* filter_list = NULL; | 395 ListValue* filter_list = nullptr; |
396 if (!filtered_events->GetList(event_name, &filter_list)) { | 396 if (!filtered_events->GetList(event_name, &filter_list)) { |
397 filter_list = new ListValue; | 397 filter_list = new ListValue; |
398 filtered_events->SetWithoutPathExpansion(event_name, filter_list); | 398 filtered_events->SetWithoutPathExpansion(event_name, filter_list); |
399 } | 399 } |
400 | 400 |
401 filter_list->Append(filter->DeepCopy()); | 401 filter_list->Append(filter->DeepCopy()); |
402 } | 402 } |
403 | 403 |
404 void EventRouter::RemoveFilterFromEvent(const std::string& event_name, | 404 void EventRouter::RemoveFilterFromEvent(const std::string& event_name, |
405 const std::string& extension_id, | 405 const std::string& extension_id, |
406 const DictionaryValue* filter) { | 406 const DictionaryValue* filter) { |
407 ExtensionPrefs::ScopedDictionaryUpdate update( | 407 ExtensionPrefs::ScopedDictionaryUpdate update( |
408 extension_prefs_, extension_id, kFilteredEvents); | 408 extension_prefs_, extension_id, kFilteredEvents); |
409 DictionaryValue* filtered_events = update.Get(); | 409 DictionaryValue* filtered_events = update.Get(); |
410 ListValue* filter_list = NULL; | 410 ListValue* filter_list = nullptr; |
411 if (!filtered_events || | 411 if (!filtered_events || |
412 !filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { | 412 !filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { |
413 return; | 413 return; |
414 } | 414 } |
415 | 415 |
416 for (size_t i = 0; i < filter_list->GetSize(); i++) { | 416 for (size_t i = 0; i < filter_list->GetSize(); i++) { |
417 DictionaryValue* filter = NULL; | 417 DictionaryValue* filter = nullptr; |
418 CHECK(filter_list->GetDictionary(i, &filter)); | 418 CHECK(filter_list->GetDictionary(i, &filter)); |
419 if (filter->Equals(filter)) { | 419 if (filter->Equals(filter)) { |
420 filter_list->Remove(i, NULL); | 420 filter_list->Remove(i, nullptr); |
421 break; | 421 break; |
422 } | 422 } |
423 } | 423 } |
424 } | 424 } |
425 | 425 |
426 const DictionaryValue* EventRouter::GetFilteredEvents( | 426 const DictionaryValue* EventRouter::GetFilteredEvents( |
427 const std::string& extension_id) { | 427 const std::string& extension_id) { |
428 const DictionaryValue* events = NULL; | 428 const DictionaryValue* events = nullptr; |
429 extension_prefs_->ReadPrefAsDictionary( | 429 extension_prefs_->ReadPrefAsDictionary( |
430 extension_id, kFilteredEvents, &events); | 430 extension_id, kFilteredEvents, &events); |
431 return events; | 431 return events; |
432 } | 432 } |
433 | 433 |
434 void EventRouter::BroadcastEvent(scoped_ptr<Event> event) { | 434 void EventRouter::BroadcastEvent(scoped_ptr<Event> event) { |
435 DispatchEventImpl(std::string(), linked_ptr<Event>(event.release())); | 435 DispatchEventImpl(std::string(), linked_ptr<Event>(event.release())); |
436 } | 436 } |
437 | 437 |
438 void EventRouter::DispatchEventToExtension(const std::string& extension_id, | 438 void EventRouter::DispatchEventToExtension(const std::string& extension_id, |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 const Extension* extension, | 759 const Extension* extension, |
760 UnloadedExtensionInfo::Reason reason) { | 760 UnloadedExtensionInfo::Reason reason) { |
761 // Remove all registered lazy listeners from our cache. | 761 // Remove all registered lazy listeners from our cache. |
762 listeners_.RemoveLazyListenersForExtension(extension->id()); | 762 listeners_.RemoveLazyListenersForExtension(extension->id()); |
763 } | 763 } |
764 | 764 |
765 Event::Event(const std::string& event_name, | 765 Event::Event(const std::string& event_name, |
766 scoped_ptr<base::ListValue> event_args) | 766 scoped_ptr<base::ListValue> event_args) |
767 : event_name(event_name), | 767 : event_name(event_name), |
768 event_args(event_args.Pass()), | 768 event_args(event_args.Pass()), |
769 restrict_to_browser_context(NULL), | 769 restrict_to_browser_context(nullptr), |
770 user_gesture(EventRouter::USER_GESTURE_UNKNOWN) { | 770 user_gesture(EventRouter::USER_GESTURE_UNKNOWN) { |
771 DCHECK(this->event_args.get()); | 771 DCHECK(this->event_args.get()); |
772 } | 772 } |
773 | 773 |
774 Event::Event(const std::string& event_name, | 774 Event::Event(const std::string& event_name, |
775 scoped_ptr<base::ListValue> event_args, | 775 scoped_ptr<base::ListValue> event_args, |
776 BrowserContext* restrict_to_browser_context) | 776 BrowserContext* restrict_to_browser_context) |
777 : event_name(event_name), | 777 : event_name(event_name), |
778 event_args(event_args.Pass()), | 778 event_args(event_args.Pass()), |
779 restrict_to_browser_context(restrict_to_browser_context), | 779 restrict_to_browser_context(restrict_to_browser_context), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
813 const std::string& extension_id, | 813 const std::string& extension_id, |
814 const GURL& listener_url, | 814 const GURL& listener_url, |
815 content::BrowserContext* browser_context) | 815 content::BrowserContext* browser_context) |
816 : event_name(event_name), | 816 : event_name(event_name), |
817 extension_id(extension_id), | 817 extension_id(extension_id), |
818 listener_url(listener_url), | 818 listener_url(listener_url), |
819 browser_context(browser_context) { | 819 browser_context(browser_context) { |
820 } | 820 } |
821 | 821 |
822 } // namespace extensions | 822 } // namespace extensions |
OLD | NEW |