| 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 |
| 11 #include "base/atomic_sequence_num.h" | 11 #include "base/atomic_sequence_num.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "content/public/browser/notification_service.h" | |
| 21 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 22 #include "extensions/browser/api_activity_monitor.h" | 21 #include "extensions/browser/api_activity_monitor.h" |
| 23 #include "extensions/browser/event_router_factory.h" | 22 #include "extensions/browser/event_router_factory.h" |
| 24 #include "extensions/browser/extension_host.h" | 23 #include "extensions/browser/extension_host.h" |
| 25 #include "extensions/browser/extension_prefs.h" | 24 #include "extensions/browser/extension_prefs.h" |
| 26 #include "extensions/browser/extension_registry.h" | 25 #include "extensions/browser/extension_registry.h" |
| 27 #include "extensions/browser/extension_system.h" | 26 #include "extensions/browser/extension_system.h" |
| 28 #include "extensions/browser/extensions_browser_client.h" | 27 #include "extensions/browser/extensions_browser_client.h" |
| 29 #include "extensions/browser/lazy_background_task_queue.h" | 28 #include "extensions/browser/lazy_background_task_queue.h" |
| 30 #include "extensions/browser/notification_types.h" | |
| 31 #include "extensions/browser/process_manager.h" | 29 #include "extensions/browser/process_manager.h" |
| 32 #include "extensions/browser/process_map.h" | 30 #include "extensions/browser/process_map.h" |
| 33 #include "extensions/common/constants.h" | 31 #include "extensions/common/constants.h" |
| 34 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
| 35 #include "extensions/common/extension_api.h" | 33 #include "extensions/common/extension_api.h" |
| 36 #include "extensions/common/extension_messages.h" | 34 #include "extensions/common/extension_messages.h" |
| 37 #include "extensions/common/extension_urls.h" | 35 #include "extensions/common/extension_urls.h" |
| 38 #include "extensions/common/features/feature.h" | 36 #include "extensions/common/features/feature.h" |
| 39 #include "extensions/common/features/feature_provider.h" | 37 #include "extensions/common/features/feature_provider.h" |
| 40 #include "extensions/common/manifest_handlers/background_info.h" | 38 #include "extensions/common/manifest_handlers/background_info.h" |
| 41 #include "extensions/common/manifest_handlers/incognito_info.h" | 39 #include "extensions/common/manifest_handlers/incognito_info.h" |
| 42 #include "extensions/common/permissions/permissions_data.h" | 40 #include "extensions/common/permissions/permissions_data.h" |
| 43 | 41 |
| 44 using base::DictionaryValue; | 42 using base::DictionaryValue; |
| 45 using base::ListValue; | 43 using base::ListValue; |
| 46 using content::BrowserContext; | 44 using content::BrowserContext; |
| 47 using content::BrowserThread; | 45 using content::BrowserThread; |
| 48 | 46 |
| 49 namespace extensions { | 47 namespace extensions { |
| 50 | 48 |
| 51 namespace { | 49 namespace { |
| 52 | 50 |
| 53 void DoNothing(ExtensionHost* host) {} | |
| 54 | |
| 55 // A dictionary of event names to lists of filters that this extension has | 51 // A dictionary of event names to lists of filters that this extension has |
| 56 // registered from its lazy background page. | 52 // registered from its lazy background page. |
| 57 const char kFilteredEvents[] = "filtered_events"; | 53 const char kFilteredEvents[] = "filtered_events"; |
| 58 | 54 |
| 59 // Sends a notification about an event to the API activity monitor and the | 55 // Sends a notification about an event to the API activity monitor and the |
| 60 // ExtensionHost for |extension_id| on the UI thread. Can be called from any | 56 // ExtensionHost for |extension_id| on the UI thread. Can be called from any |
| 61 // thread. | 57 // thread. |
| 62 void NotifyEventDispatched(void* browser_context_id, | 58 void NotifyEventDispatched(void* browser_context_id, |
| 63 const std::string& extension_id, | 59 const std::string& extension_id, |
| 64 const std::string& event_name, | 60 const std::string& event_name, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 DispatchExtensionMessage(ipc_sender, browser_context_id, extension_id, | 133 DispatchExtensionMessage(ipc_sender, browser_context_id, extension_id, |
| 138 event_id, event_name, event_args.get(), user_gesture, | 134 event_id, event_name, event_args.get(), user_gesture, |
| 139 info); | 135 info); |
| 140 } | 136 } |
| 141 | 137 |
| 142 EventRouter::EventRouter(BrowserContext* browser_context, | 138 EventRouter::EventRouter(BrowserContext* browser_context, |
| 143 ExtensionPrefs* extension_prefs) | 139 ExtensionPrefs* extension_prefs) |
| 144 : browser_context_(browser_context), | 140 : browser_context_(browser_context), |
| 145 extension_prefs_(extension_prefs), | 141 extension_prefs_(extension_prefs), |
| 146 extension_registry_observer_(this), | 142 extension_registry_observer_(this), |
| 147 listeners_(this) { | 143 listeners_(this), |
| 148 registrar_.Add(this, | 144 lazy_event_dispatch_util_(browser_context_) { |
| 149 extensions::NOTIFICATION_EXTENSION_ENABLED, | |
| 150 content::Source<BrowserContext>(browser_context_)); | |
| 151 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 145 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 152 } | 146 } |
| 153 | 147 |
| 154 EventRouter::~EventRouter() { | 148 EventRouter::~EventRouter() { |
| 155 for (auto* process : observed_process_set_) | 149 for (auto* process : observed_process_set_) |
| 156 process->RemoveObserver(this); | 150 process->RemoveObserver(this); |
| 157 } | 151 } |
| 158 | 152 |
| 159 void EventRouter::AddEventListener(const std::string& event_name, | 153 void EventRouter::AddEventListener(const std::string& event_name, |
| 160 content::RenderProcessHost* process, | 154 content::RenderProcessHost* process, |
| (...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 ListValue* filter_list = nullptr; | 740 ListValue* filter_list = nullptr; |
| 747 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { | 741 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { |
| 748 filter_list = new ListValue; | 742 filter_list = new ListValue; |
| 749 filtered_events->SetWithoutPathExpansion(event_name, | 743 filtered_events->SetWithoutPathExpansion(event_name, |
| 750 base::WrapUnique(filter_list)); | 744 base::WrapUnique(filter_list)); |
| 751 } | 745 } |
| 752 | 746 |
| 753 filter_list->Append(filter->CreateDeepCopy()); | 747 filter_list->Append(filter->CreateDeepCopy()); |
| 754 } | 748 } |
| 755 | 749 |
| 756 void EventRouter::Observe(int type, | |
| 757 const content::NotificationSource& source, | |
| 758 const content::NotificationDetails& details) { | |
| 759 switch (type) { | |
| 760 case extensions::NOTIFICATION_EXTENSION_ENABLED: { | |
| 761 // If the extension has a lazy background page, make sure it gets loaded | |
| 762 // to register the events the extension is interested in. | |
| 763 const Extension* extension = | |
| 764 content::Details<const Extension>(details).ptr(); | |
| 765 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { | |
| 766 LazyBackgroundTaskQueue* queue = | |
| 767 LazyBackgroundTaskQueue::Get(browser_context_); | |
| 768 queue->AddPendingTask(browser_context_, extension->id(), | |
| 769 base::Bind(&DoNothing)); | |
| 770 } | |
| 771 break; | |
| 772 } | |
| 773 default: | |
| 774 NOTREACHED(); | |
| 775 } | |
| 776 } | |
| 777 | |
| 778 void EventRouter::OnExtensionLoaded(content::BrowserContext* browser_context, | 750 void EventRouter::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 779 const Extension* extension) { | 751 const Extension* extension) { |
| 780 // Add all registered lazy listeners to our cache. | 752 // Add all registered lazy listeners to our cache. |
| 781 std::set<std::string> registered_events = | 753 std::set<std::string> registered_events = |
| 782 GetRegisteredEvents(extension->id()); | 754 GetRegisteredEvents(extension->id()); |
| 783 listeners_.LoadUnfilteredLazyListeners(extension->id(), registered_events); | 755 listeners_.LoadUnfilteredLazyListeners(extension->id(), registered_events); |
| 784 const DictionaryValue* filtered_events = GetFilteredEvents(extension->id()); | 756 const DictionaryValue* filtered_events = GetFilteredEvents(extension->id()); |
| 785 if (filtered_events) | 757 if (filtered_events) |
| 786 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); | 758 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); |
| 787 } | 759 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 const std::string& extension_id, | 820 const std::string& extension_id, |
| 849 const GURL& listener_url, | 821 const GURL& listener_url, |
| 850 content::BrowserContext* browser_context) | 822 content::BrowserContext* browser_context) |
| 851 : event_name(event_name), | 823 : event_name(event_name), |
| 852 extension_id(extension_id), | 824 extension_id(extension_id), |
| 853 listener_url(listener_url), | 825 listener_url(listener_url), |
| 854 browser_context(browser_context) { | 826 browser_context(browser_context) { |
| 855 } | 827 } |
| 856 | 828 |
| 857 } // namespace extensions | 829 } // namespace extensions |
| OLD | NEW |