| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 bool EventRouter::HasEventListener(const std::string& event_name) { | 317 bool EventRouter::HasEventListener(const std::string& event_name) { |
| 318 return listeners_.HasListenerForEvent(event_name); | 318 return listeners_.HasListenerForEvent(event_name); |
| 319 } | 319 } |
| 320 | 320 |
| 321 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id, | 321 bool EventRouter::ExtensionHasEventListener(const std::string& extension_id, |
| 322 const std::string& event_name) { | 322 const std::string& event_name) { |
| 323 return listeners_.HasListenerForExtension(extension_id, event_name); | 323 return listeners_.HasListenerForExtension(extension_id, event_name); |
| 324 } | 324 } |
| 325 | 325 |
| 326 std::set<std::string> EventRouter::GetRegisteredEvents( | 326 std::set<std::string> EventRouter::GetRegisteredEvents( |
| 327 const std::string& extension_id) { | 327 const std::string& extension_id) const { |
| 328 std::set<std::string> events; | 328 std::set<std::string> events; |
| 329 const ListValue* events_value = NULL; | 329 const ListValue* events_value = NULL; |
| 330 | 330 |
| 331 if (!extension_prefs_ || | 331 if (!extension_prefs_ || |
| 332 !extension_prefs_->ReadPrefAsList( | 332 !extension_prefs_->ReadPrefAsList( |
| 333 extension_id, kRegisteredEvents, &events_value)) { | 333 extension_id, kRegisteredEvents, &events_value)) { |
| 334 return events; | 334 return events; |
| 335 } | 335 } |
| 336 | 336 |
| 337 for (size_t i = 0; i < events_value->GetSize(); ++i) { | 337 for (size_t i = 0; i < events_value->GetSize(); ++i) { |
| 338 std::string event; | 338 std::string event; |
| 339 if (events_value->GetString(i, &event)) | 339 if (events_value->GetString(i, &event)) |
| 340 events.insert(event); | 340 events.insert(event); |
| 341 } | 341 } |
| 342 return events; | 342 return events; |
| 343 } | 343 } |
| 344 | 344 |
| 345 void EventRouter::SetRegisteredEvents(const std::string& extension_id, | |
| 346 const std::set<std::string>& events) { | |
| 347 auto events_value = base::MakeUnique<base::ListValue>(); | |
| 348 for (std::set<std::string>::const_iterator iter = events.begin(); | |
| 349 iter != events.end(); ++iter) { | |
| 350 events_value->AppendString(*iter); | |
| 351 } | |
| 352 extension_prefs_->UpdateExtensionPref(extension_id, kRegisteredEvents, | |
| 353 std::move(events_value)); | |
| 354 } | |
| 355 | |
| 356 void EventRouter::AddFilterToEvent(const std::string& event_name, | |
| 357 const std::string& extension_id, | |
| 358 const DictionaryValue* filter) { | |
| 359 ExtensionPrefs::ScopedDictionaryUpdate update( | |
| 360 extension_prefs_, extension_id, kFilteredEvents); | |
| 361 DictionaryValue* filtered_events = update.Get(); | |
| 362 if (!filtered_events) | |
| 363 filtered_events = update.Create(); | |
| 364 | |
| 365 ListValue* filter_list = nullptr; | |
| 366 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { | |
| 367 filter_list = new ListValue; | |
| 368 filtered_events->SetWithoutPathExpansion(event_name, | |
| 369 base::WrapUnique(filter_list)); | |
| 370 } | |
| 371 | |
| 372 filter_list->Append(filter->CreateDeepCopy()); | |
| 373 } | |
| 374 | |
| 375 void EventRouter::RemoveFilterFromEvent(const std::string& event_name, | 345 void EventRouter::RemoveFilterFromEvent(const std::string& event_name, |
| 376 const std::string& extension_id, | 346 const std::string& extension_id, |
| 377 const DictionaryValue* filter) { | 347 const DictionaryValue* filter) { |
| 378 ExtensionPrefs::ScopedDictionaryUpdate update( | 348 ExtensionPrefs::ScopedDictionaryUpdate update( |
| 379 extension_prefs_, extension_id, kFilteredEvents); | 349 extension_prefs_, extension_id, kFilteredEvents); |
| 380 DictionaryValue* filtered_events = update.Get(); | 350 DictionaryValue* filtered_events = update.Get(); |
| 381 ListValue* filter_list = NULL; | 351 ListValue* filter_list = NULL; |
| 382 if (!filtered_events || | 352 if (!filtered_events || |
| 383 !filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { | 353 !filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { |
| 384 return; | 354 return; |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 return; | 717 return; |
| 748 | 718 |
| 749 if (listeners_.HasProcessListener(host->render_process_host(), | 719 if (listeners_.HasProcessListener(host->render_process_host(), |
| 750 host->extension()->id())) { | 720 host->extension()->id())) { |
| 751 DispatchEventToProcess(host->extension()->id(), host->GetURL(), | 721 DispatchEventToProcess(host->extension()->id(), host->GetURL(), |
| 752 host->render_process_host(), event, nullptr, | 722 host->render_process_host(), event, nullptr, |
| 753 true /* did_enqueue */); | 723 true /* did_enqueue */); |
| 754 } | 724 } |
| 755 } | 725 } |
| 756 | 726 |
| 727 void EventRouter::SetRegisteredEvents(const std::string& extension_id, |
| 728 const std::set<std::string>& events) { |
| 729 auto events_value = base::MakeUnique<base::ListValue>(); |
| 730 for (std::set<std::string>::const_iterator iter = events.begin(); |
| 731 iter != events.end(); ++iter) { |
| 732 events_value->AppendString(*iter); |
| 733 } |
| 734 extension_prefs_->UpdateExtensionPref(extension_id, kRegisteredEvents, |
| 735 std::move(events_value)); |
| 736 } |
| 737 |
| 738 void EventRouter::AddFilterToEvent(const std::string& event_name, |
| 739 const std::string& extension_id, |
| 740 const DictionaryValue* filter) { |
| 741 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs_, extension_id, |
| 742 kFilteredEvents); |
| 743 DictionaryValue* filtered_events = update.Get(); |
| 744 if (!filtered_events) |
| 745 filtered_events = update.Create(); |
| 746 |
| 747 ListValue* filter_list = nullptr; |
| 748 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { |
| 749 filter_list = new ListValue; |
| 750 filtered_events->SetWithoutPathExpansion(event_name, |
| 751 base::WrapUnique(filter_list)); |
| 752 } |
| 753 |
| 754 filter_list->Append(filter->CreateDeepCopy()); |
| 755 } |
| 756 |
| 757 void EventRouter::Observe(int type, | 757 void EventRouter::Observe(int type, |
| 758 const content::NotificationSource& source, | 758 const content::NotificationSource& source, |
| 759 const content::NotificationDetails& details) { | 759 const content::NotificationDetails& details) { |
| 760 switch (type) { | 760 switch (type) { |
| 761 case extensions::NOTIFICATION_EXTENSION_ENABLED: { | 761 case extensions::NOTIFICATION_EXTENSION_ENABLED: { |
| 762 // If the extension has a lazy background page, make sure it gets loaded | 762 // If the extension has a lazy background page, make sure it gets loaded |
| 763 // to register the events the extension is interested in. | 763 // to register the events the extension is interested in. |
| 764 const Extension* extension = | 764 const Extension* extension = |
| 765 content::Details<const Extension>(details).ptr(); | 765 content::Details<const Extension>(details).ptr(); |
| 766 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { | 766 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 const std::string& extension_id, | 849 const std::string& extension_id, |
| 850 const GURL& listener_url, | 850 const GURL& listener_url, |
| 851 content::BrowserContext* browser_context) | 851 content::BrowserContext* browser_context) |
| 852 : event_name(event_name), | 852 : event_name(event_name), |
| 853 extension_id(extension_id), | 853 extension_id(extension_id), |
| 854 listener_url(listener_url), | 854 listener_url(listener_url), |
| 855 browser_context(browser_context) { | 855 browser_context(browser_context) { |
| 856 } | 856 } |
| 857 | 857 |
| 858 } // namespace extensions | 858 } // namespace extensions |
| OLD | NEW |