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