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

Side by Side Diff: extensions/browser/event_router.cc

Issue 2879673002: Restrict EventRouter::Get/Set-RegisteredEvents. (Closed)
Patch Set: Created 3 years, 7 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
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 "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
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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 // The event ACK is routed to the background host, so this should never be 652 // The event ACK is routed to the background host, so this should never be
683 // NULL. 653 // NULL.
684 CHECK(host); 654 CHECK(host);
685 // TODO(mpcomplete): We should never get this message unless 655 // TODO(mpcomplete): We should never get this message unless
686 // HasLazyBackgroundPage is true. Find out why we're getting it anyway. 656 // HasLazyBackgroundPage is true. Find out why we're getting it anyway.
687 if (host->extension() && 657 if (host->extension() &&
688 BackgroundInfo::HasLazyBackgroundPage(host->extension())) 658 BackgroundInfo::HasLazyBackgroundPage(host->extension()))
689 pm->DecrementLazyKeepaliveCount(host->extension()); 659 pm->DecrementLazyKeepaliveCount(host->extension());
690 } 660 }
691 661
662 bool EventRouter::HasRegisteredEvents(const ExtensionId& extension_id) const {
663 return !GetRegisteredEvents(extension_id).empty();
664 }
665
666 void EventRouter::ClearRegisteredEventsForTest(
667 const ExtensionId& extension_id) {
668 SetRegisteredEvents(extension_id, std::set<std::string>());
669 }
670
692 void EventRouter::ReportEvent(events::HistogramValue histogram_value, 671 void EventRouter::ReportEvent(events::HistogramValue histogram_value,
693 const Extension* extension, 672 const Extension* extension,
694 bool did_enqueue) { 673 bool did_enqueue) {
695 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 674 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
696 675
697 // Record every event fired. 676 // Record every event fired.
698 UMA_HISTOGRAM_ENUMERATION("Extensions.Events.Dispatch", histogram_value, 677 UMA_HISTOGRAM_ENUMERATION("Extensions.Events.Dispatch", histogram_value,
699 events::ENUM_BOUNDARY); 678 events::ENUM_BOUNDARY);
700 679
701 bool is_component = Manifest::IsComponentLocation(extension->location()); 680 bool is_component = Manifest::IsComponentLocation(extension->location());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 return; 726 return;
748 727
749 if (listeners_.HasProcessListener(host->render_process_host(), 728 if (listeners_.HasProcessListener(host->render_process_host(),
750 host->extension()->id())) { 729 host->extension()->id())) {
751 DispatchEventToProcess(host->extension()->id(), host->GetURL(), 730 DispatchEventToProcess(host->extension()->id(), host->GetURL(),
752 host->render_process_host(), event, nullptr, 731 host->render_process_host(), event, nullptr,
753 true /* did_enqueue */); 732 true /* did_enqueue */);
754 } 733 }
755 } 734 }
756 735
736 void EventRouter::SetRegisteredEvents(const std::string& extension_id,
lazyboy 2017/05/11 20:58:49 note: This block is moved from line 345.
737 const std::set<std::string>& events) {
738 auto events_value = base::MakeUnique<base::ListValue>();
739 for (std::set<std::string>::const_iterator iter = events.begin();
740 iter != events.end(); ++iter) {
741 events_value->AppendString(*iter);
742 }
743 extension_prefs_->UpdateExtensionPref(extension_id, kRegisteredEvents,
744 std::move(events_value));
745 }
746
747 void EventRouter::AddFilterToEvent(const std::string& event_name,
748 const std::string& extension_id,
749 const DictionaryValue* filter) {
750 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs_, extension_id,
751 kFilteredEvents);
752 DictionaryValue* filtered_events = update.Get();
753 if (!filtered_events)
754 filtered_events = update.Create();
755
756 ListValue* filter_list = nullptr;
757 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) {
758 filter_list = new ListValue;
759 filtered_events->SetWithoutPathExpansion(event_name,
760 base::WrapUnique(filter_list));
761 }
762
763 filter_list->Append(filter->CreateDeepCopy());
764 }
765
757 void EventRouter::Observe(int type, 766 void EventRouter::Observe(int type,
758 const content::NotificationSource& source, 767 const content::NotificationSource& source,
759 const content::NotificationDetails& details) { 768 const content::NotificationDetails& details) {
760 switch (type) { 769 switch (type) {
761 case extensions::NOTIFICATION_EXTENSION_ENABLED: { 770 case extensions::NOTIFICATION_EXTENSION_ENABLED: {
762 // If the extension has a lazy background page, make sure it gets loaded 771 // If the extension has a lazy background page, make sure it gets loaded
763 // to register the events the extension is interested in. 772 // to register the events the extension is interested in.
764 const Extension* extension = 773 const Extension* extension =
765 content::Details<const Extension>(details).ptr(); 774 content::Details<const Extension>(details).ptr();
766 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { 775 if (BackgroundInfo::HasLazyBackgroundPage(extension)) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 const std::string& extension_id, 858 const std::string& extension_id,
850 const GURL& listener_url, 859 const GURL& listener_url,
851 content::BrowserContext* browser_context) 860 content::BrowserContext* browser_context)
852 : event_name(event_name), 861 : event_name(event_name),
853 extension_id(extension_id), 862 extension_id(extension_id),
854 listener_url(listener_url), 863 listener_url(listener_url),
855 browser_context(browser_context) { 864 browser_context(browser_context) {
856 } 865 }
857 866
858 } // namespace extensions 867 } // namespace extensions
OLDNEW
« extensions/browser/event_router.h ('K') | « extensions/browser/event_router.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698