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" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 event_id, event_name, event_args.get(), user_gesture, | 136 event_id, event_name, event_args.get(), user_gesture, |
139 info); | 137 info); |
140 } | 138 } |
141 | 139 |
142 EventRouter::EventRouter(BrowserContext* browser_context, | 140 EventRouter::EventRouter(BrowserContext* browser_context, |
143 ExtensionPrefs* extension_prefs) | 141 ExtensionPrefs* extension_prefs) |
144 : browser_context_(browser_context), | 142 : browser_context_(browser_context), |
145 extension_prefs_(extension_prefs), | 143 extension_prefs_(extension_prefs), |
146 extension_registry_observer_(this), | 144 extension_registry_observer_(this), |
147 listeners_(this) { | 145 listeners_(this) { |
148 registrar_.Add(this, | |
149 extensions::NOTIFICATION_EXTENSION_ENABLED, | |
150 content::Source<BrowserContext>(browser_context_)); | |
151 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 146 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
152 } | 147 } |
153 | 148 |
154 EventRouter::~EventRouter() { | 149 EventRouter::~EventRouter() { |
155 for (auto* process : observed_process_set_) | 150 for (auto* process : observed_process_set_) |
156 process->RemoveObserver(this); | 151 process->RemoveObserver(this); |
157 } | 152 } |
158 | 153 |
159 void EventRouter::AddEventListener(const std::string& event_name, | 154 void EventRouter::AddEventListener(const std::string& event_name, |
160 content::RenderProcessHost* process, | 155 content::RenderProcessHost* process, |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
746 ListValue* filter_list = nullptr; | 741 ListValue* filter_list = nullptr; |
747 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { | 742 if (!filtered_events->GetListWithoutPathExpansion(event_name, &filter_list)) { |
748 filter_list = new ListValue; | 743 filter_list = new ListValue; |
749 filtered_events->SetWithoutPathExpansion(event_name, | 744 filtered_events->SetWithoutPathExpansion(event_name, |
750 base::WrapUnique(filter_list)); | 745 base::WrapUnique(filter_list)); |
751 } | 746 } |
752 | 747 |
753 filter_list->Append(filter->CreateDeepCopy()); | 748 filter_list->Append(filter->CreateDeepCopy()); |
754 } | 749 } |
755 | 750 |
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, | 751 void EventRouter::OnExtensionLoaded(content::BrowserContext* browser_context, |
779 const Extension* extension) { | 752 const Extension* extension) { |
780 // Add all registered lazy listeners to our cache. | 753 // Add all registered lazy listeners to our cache. |
781 std::set<std::string> registered_events = | 754 std::set<std::string> registered_events = |
782 GetRegisteredEvents(extension->id()); | 755 GetRegisteredEvents(extension->id()); |
783 listeners_.LoadUnfilteredLazyListeners(extension->id(), registered_events); | 756 listeners_.LoadUnfilteredLazyListeners(extension->id(), registered_events); |
784 const DictionaryValue* filtered_events = GetFilteredEvents(extension->id()); | 757 const DictionaryValue* filtered_events = GetFilteredEvents(extension->id()); |
785 if (filtered_events) | 758 if (filtered_events) |
786 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); | 759 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); |
787 } | 760 } |
788 | 761 |
762 void EventRouter::OnExtensionEnabled(content::BrowserContext* browser_context, | |
Devlin
2017/05/17 18:37:48
Why do we do this for enabled, but not loaded?
lazyboy
2017/05/17 22:23:25
This seems to be fixing https://bugs.chromium.org/
Devlin
2017/05/18 01:34:45
Ah, thanks for digging that up! The motivation he
lazyboy
2017/05/18 18:10:52
We forcefully send an onInstalled in that case via
Devlin
2017/05/18 18:39:47
Oof, this is quite the rat's nest.
So, RuntimeAPI
| |
763 const Extension* extension) { | |
764 DCHECK_EQ(browser_context_, browser_context); | |
765 // If the extension has a lazy background page, make sure it gets loaded | |
766 // to register the events the extension is interested in. | |
767 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { | |
768 LazyBackgroundTaskQueue* queue = | |
769 LazyBackgroundTaskQueue::Get(browser_context_); | |
770 queue->AddPendingTask(browser_context_, extension->id(), | |
771 base::Bind(&DoNothing)); | |
772 } | |
773 } | |
774 | |
789 void EventRouter::OnExtensionUnloaded(content::BrowserContext* browser_context, | 775 void EventRouter::OnExtensionUnloaded(content::BrowserContext* browser_context, |
790 const Extension* extension, | 776 const Extension* extension, |
791 UnloadedExtensionReason reason) { | 777 UnloadedExtensionReason reason) { |
792 // Remove all registered listeners from our cache. | 778 // Remove all registered listeners from our cache. |
793 listeners_.RemoveListenersForExtension(extension->id()); | 779 listeners_.RemoveListenersForExtension(extension->id()); |
794 } | 780 } |
795 | 781 |
796 Event::Event(events::HistogramValue histogram_value, | 782 Event::Event(events::HistogramValue histogram_value, |
797 const std::string& event_name, | 783 const std::string& event_name, |
798 std::unique_ptr<base::ListValue> event_args) | 784 std::unique_ptr<base::ListValue> event_args) |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
848 const std::string& extension_id, | 834 const std::string& extension_id, |
849 const GURL& listener_url, | 835 const GURL& listener_url, |
850 content::BrowserContext* browser_context) | 836 content::BrowserContext* browser_context) |
851 : event_name(event_name), | 837 : event_name(event_name), |
852 extension_id(extension_id), | 838 extension_id(extension_id), |
853 listener_url(listener_url), | 839 listener_url(listener_url), |
854 browser_context(browser_context) { | 840 browser_context(browser_context) { |
855 } | 841 } |
856 | 842 |
857 } // namespace extensions | 843 } // namespace extensions |
OLD | NEW |