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

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

Issue 425303002: Move extension notifications to extensions/browser/notification_types.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: (extension-notifications) rebase Created 6 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « extensions/browser/notification_types.h ('k') | extensions/browser/process_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/process_manager.h" 5 #include "extensions/browser/process_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/devtools_agent_host.h" 18 #include "content/public/browser/devtools_agent_host.h"
20 #include "content/public/browser/devtools_manager.h" 19 #include "content/public/browser/devtools_manager.h"
21 #include "content/public/browser/notification_service.h" 20 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/render_frame_host.h" 21 #include "content/public/browser/render_frame_host.h"
23 #include "content/public/browser/render_process_host.h" 22 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h" 23 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/site_instance.h" 24 #include "content/public/browser/site_instance.h"
26 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_contents_delegate.h" 26 #include "content/public/browser/web_contents_delegate.h"
28 #include "content/public/browser/web_contents_observer.h" 27 #include "content/public/browser/web_contents_observer.h"
29 #include "content/public/browser/web_contents_user_data.h" 28 #include "content/public/browser/web_contents_user_data.h"
30 #include "content/public/common/renderer_preferences.h" 29 #include "content/public/common/renderer_preferences.h"
31 #include "content/public/common/url_constants.h" 30 #include "content/public/common/url_constants.h"
32 #include "extensions/browser/extension_host.h" 31 #include "extensions/browser/extension_host.h"
33 #include "extensions/browser/extension_registry.h" 32 #include "extensions/browser/extension_registry.h"
34 #include "extensions/browser/extension_system.h" 33 #include "extensions/browser/extension_system.h"
35 #include "extensions/browser/extensions_browser_client.h" 34 #include "extensions/browser/extensions_browser_client.h"
35 #include "extensions/browser/notification_types.h"
36 #include "extensions/browser/process_manager_delegate.h" 36 #include "extensions/browser/process_manager_delegate.h"
37 #include "extensions/browser/process_manager_observer.h" 37 #include "extensions/browser/process_manager_observer.h"
38 #include "extensions/browser/view_type_utils.h" 38 #include "extensions/browser/view_type_utils.h"
39 #include "extensions/common/constants.h" 39 #include "extensions/common/constants.h"
40 #include "extensions/common/extension.h" 40 #include "extensions/common/extension.h"
41 #include "extensions/common/extension_messages.h" 41 #include "extensions/common/extension_messages.h"
42 #include "extensions/common/manifest_handlers/background_info.h" 42 #include "extensions/common/manifest_handlers/background_info.h"
43 #include "extensions/common/manifest_handlers/incognito_info.h" 43 #include "extensions/common/manifest_handlers/incognito_info.h"
44 #include "extensions/common/one_shot_event.h" 44 #include "extensions/common/one_shot_event.h"
45 #include "extensions/common/switches.h" 45 #include "extensions/common/switches.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 87
88 bool IsFrameInExtensionHost(ExtensionHost* extension_host, 88 bool IsFrameInExtensionHost(ExtensionHost* extension_host,
89 content::RenderFrameHost* render_frame_host) { 89 content::RenderFrameHost* render_frame_host) {
90 return WebContents::FromRenderFrameHost(render_frame_host) == 90 return WebContents::FromRenderFrameHost(render_frame_host) ==
91 extension_host->host_contents(); 91 extension_host->host_contents();
92 } 92 }
93 93
94 void OnRenderViewHostUnregistered(BrowserContext* context, 94 void OnRenderViewHostUnregistered(BrowserContext* context,
95 RenderViewHost* render_view_host) { 95 RenderViewHost* render_view_host) {
96 content::NotificationService::current()->Notify( 96 content::NotificationService::current()->Notify(
97 chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED, 97 extensions::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED,
98 content::Source<BrowserContext>(context), 98 content::Source<BrowserContext>(context),
99 content::Details<RenderViewHost>(render_view_host)); 99 content::Details<RenderViewHost>(render_view_host));
100 } 100 }
101 101
102 // Incognito profiles use this process manager. It is mostly a shim that decides 102 // Incognito profiles use this process manager. It is mostly a shim that decides
103 // whether to fall back on the original profile's ProcessManager based 103 // whether to fall back on the original profile's ProcessManager based
104 // on whether a given extension uses "split" or "spanning" incognito behavior. 104 // on whether a given extension uses "split" or "spanning" incognito behavior.
105 class IncognitoProcessManager : public ProcessManager { 105 class IncognitoProcessManager : public ProcessManager {
106 public: 106 public:
107 IncognitoProcessManager(BrowserContext* incognito_context, 107 IncognitoProcessManager(BrowserContext* incognito_context,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 ExtensionRegistry* extension_registry) 241 ExtensionRegistry* extension_registry)
242 : site_instance_(SiteInstance::Create(context)), 242 : site_instance_(SiteInstance::Create(context)),
243 extension_registry_(extension_registry), 243 extension_registry_(extension_registry),
244 startup_background_hosts_created_(false), 244 startup_background_hosts_created_(false),
245 devtools_callback_(base::Bind(&ProcessManager::OnDevToolsStateChanged, 245 devtools_callback_(base::Bind(&ProcessManager::OnDevToolsStateChanged,
246 base::Unretained(this))), 246 base::Unretained(this))),
247 last_background_close_sequence_id_(0), 247 last_background_close_sequence_id_(0),
248 weak_ptr_factory_(this) { 248 weak_ptr_factory_(this) {
249 // ExtensionRegistry is shared between incognito and regular contexts. 249 // ExtensionRegistry is shared between incognito and regular contexts.
250 DCHECK_EQ(original_context, extension_registry_->browser_context()); 250 DCHECK_EQ(original_context, extension_registry_->browser_context());
251 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, 251 registrar_.Add(this,
252 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
252 content::Source<BrowserContext>(original_context)); 253 content::Source<BrowserContext>(original_context));
253 registrar_.Add(this, 254 registrar_.Add(this,
254 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, 255 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
255 content::Source<BrowserContext>(original_context)); 256 content::Source<BrowserContext>(original_context));
256 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, 257 registrar_.Add(this,
258 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
257 content::Source<BrowserContext>(original_context)); 259 content::Source<BrowserContext>(original_context));
258 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, 260 registrar_.Add(this,
261 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
259 content::Source<BrowserContext>(context)); 262 content::Source<BrowserContext>(context));
260 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, 263 registrar_.Add(this,
264 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
261 content::Source<BrowserContext>(context)); 265 content::Source<BrowserContext>(context));
262 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED, 266 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
263 content::NotificationService::AllSources()); 267 content::NotificationService::AllSources());
264 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, 268 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
265 content::NotificationService::AllSources()); 269 content::NotificationService::AllSources());
266 270
267 // Note: event_page_idle_time_ must be sufficiently larger (e.g. 2x) than 271 // Note: event_page_idle_time_ must be sufficiently larger (e.g. 2x) than
268 // kKeepaliveThrottleIntervalInSeconds in ppapi/proxy/plugin_globals. 272 // kKeepaliveThrottleIntervalInSeconds in ppapi/proxy/plugin_globals.
269 event_page_idle_time_ = base::TimeDelta::FromSeconds(10); 273 event_page_idle_time_ = base::TimeDelta::FromSeconds(10);
270 unsigned idle_time_msec = 0; 274 unsigned idle_time_msec = 0;
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 650
647 void ProcessManager::SetKeepaliveImpulseDecrementCallbackForTesting( 651 void ProcessManager::SetKeepaliveImpulseDecrementCallbackForTesting(
648 const ImpulseCallbackForTesting& callback) { 652 const ImpulseCallbackForTesting& callback) {
649 keepalive_impulse_decrement_callback_for_testing_ = callback; 653 keepalive_impulse_decrement_callback_for_testing_ = callback;
650 } 654 }
651 655
652 void ProcessManager::Observe(int type, 656 void ProcessManager::Observe(int type,
653 const content::NotificationSource& source, 657 const content::NotificationSource& source,
654 const content::NotificationDetails& details) { 658 const content::NotificationDetails& details) {
655 switch (type) { 659 switch (type) {
656 case chrome::NOTIFICATION_EXTENSIONS_READY: { 660 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: {
657 // TODO(jamescook): Convert this to use ExtensionSystem::ready() instead 661 // TODO(jamescook): Convert this to use ExtensionSystem::ready() instead
658 // of a notification. 662 // of a notification.
659 MaybeCreateStartupBackgroundHosts(); 663 MaybeCreateStartupBackgroundHosts();
660 break; 664 break;
661 } 665 }
662 666
663 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { 667 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
664 BrowserContext* context = content::Source<BrowserContext>(source).ptr(); 668 BrowserContext* context = content::Source<BrowserContext>(source).ptr();
665 ExtensionSystem* system = ExtensionSystem::Get(context); 669 ExtensionSystem* system = ExtensionSystem::Get(context);
666 if (system->ready().is_signaled()) { 670 if (system->ready().is_signaled()) {
667 // The extension system is ready, so create the background host. 671 // The extension system is ready, so create the background host.
668 const Extension* extension = 672 const Extension* extension =
669 content::Details<const Extension>(details).ptr(); 673 content::Details<const Extension>(details).ptr();
670 CreateBackgroundHostForExtensionLoad(this, extension); 674 CreateBackgroundHostForExtensionLoad(this, extension);
671 } 675 }
672 break; 676 break;
673 } 677 }
674 678
675 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { 679 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
676 const Extension* extension = 680 const Extension* extension =
677 content::Details<UnloadedExtensionInfo>(details)->extension; 681 content::Details<UnloadedExtensionInfo>(details)->extension;
678 for (ExtensionHostSet::iterator iter = background_hosts_.begin(); 682 for (ExtensionHostSet::iterator iter = background_hosts_.begin();
679 iter != background_hosts_.end(); ++iter) { 683 iter != background_hosts_.end(); ++iter) {
680 ExtensionHost* host = *iter; 684 ExtensionHost* host = *iter;
681 if (host->extension_id() == extension->id()) { 685 if (host->extension_id() == extension->id()) {
682 CloseBackgroundHost(host); 686 CloseBackgroundHost(host);
683 break; 687 break;
684 } 688 }
685 } 689 }
686 UnregisterExtension(extension->id()); 690 UnregisterExtension(extension->id());
687 break; 691 break;
688 } 692 }
689 693
690 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { 694 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
691 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); 695 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
692 if (background_hosts_.erase(host)) { 696 if (background_hosts_.erase(host)) {
693 ClearBackgroundPageData(host->extension()->id()); 697 ClearBackgroundPageData(host->extension()->id());
694 background_page_data_[host->extension()->id()].since_suspended.reset( 698 background_page_data_[host->extension()->id()].since_suspended.reset(
695 new base::ElapsedTimer()); 699 new base::ElapsedTimer());
696 } 700 }
697 break; 701 break;
698 } 702 }
699 703
700 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { 704 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: {
701 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); 705 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
702 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { 706 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
703 CloseBackgroundHost(host); 707 CloseBackgroundHost(host);
704 } 708 }
705 break; 709 break;
706 } 710 }
707 711
708 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: { 712 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: {
709 // We get this notification both for new WebContents and when one 713 // We get this notification both for new WebContents and when one
710 // has its RenderViewHost replaced (e.g. when a user does a cross-site 714 // has its RenderViewHost replaced (e.g. when a user does a cross-site
(...skipping 23 matching lines...) Expand all
734 if (contents->GetBrowserContext() != GetBrowserContext()) 738 if (contents->GetBrowserContext() != GetBrowserContext())
735 break; 739 break;
736 const Extension* extension = GetExtensionForRenderViewHost( 740 const Extension* extension = GetExtensionForRenderViewHost(
737 contents->GetRenderViewHost()); 741 contents->GetRenderViewHost());
738 if (!extension) 742 if (!extension)
739 return; 743 return;
740 744
741 // RegisterRenderViewHost is called too early (before the process is 745 // RegisterRenderViewHost is called too early (before the process is
742 // available), so we need to wait until now to notify. 746 // available), so we need to wait until now to notify.
743 content::NotificationService::current()->Notify( 747 content::NotificationService::current()->Notify(
744 chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED, 748 extensions::NOTIFICATION_EXTENSION_VIEW_REGISTERED,
745 content::Source<BrowserContext>(GetBrowserContext()), 749 content::Source<BrowserContext>(GetBrowserContext()),
746 content::Details<RenderViewHost>(contents->GetRenderViewHost())); 750 content::Details<RenderViewHost>(contents->GetRenderViewHost()));
747 break; 751 break;
748 } 752 }
749 753
750 default: 754 default:
751 NOTREACHED(); 755 NOTREACHED();
752 } 756 }
753 } 757 }
754 758
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 799
796 CreateStartupBackgroundHosts(); 800 CreateStartupBackgroundHosts();
797 startup_background_hosts_created_ = true; 801 startup_background_hosts_created_ = true;
798 802
799 // Background pages should only be loaded once. To prevent any further loads 803 // Background pages should only be loaded once. To prevent any further loads
800 // occurring, we remove the notification listeners. 804 // occurring, we remove the notification listeners.
801 BrowserContext* original_context = 805 BrowserContext* original_context =
802 ExtensionsBrowserClient::Get()->GetOriginalContext(GetBrowserContext()); 806 ExtensionsBrowserClient::Get()->GetOriginalContext(GetBrowserContext());
803 if (registrar_.IsRegistered( 807 if (registrar_.IsRegistered(
804 this, 808 this,
805 chrome::NOTIFICATION_EXTENSIONS_READY, 809 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
806 content::Source<BrowserContext>(original_context))) { 810 content::Source<BrowserContext>(original_context))) {
807 registrar_.Remove(this, 811 registrar_.Remove(this,
808 chrome::NOTIFICATION_EXTENSIONS_READY, 812 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
809 content::Source<BrowserContext>(original_context)); 813 content::Source<BrowserContext>(original_context));
810 } 814 }
811 } 815 }
812 816
813 void ProcessManager::CreateStartupBackgroundHosts() { 817 void ProcessManager::CreateStartupBackgroundHosts() {
814 DCHECK(!startup_background_hosts_created_); 818 DCHECK(!startup_background_hosts_created_);
815 const ExtensionSet& enabled_extensions = 819 const ExtensionSet& enabled_extensions =
816 extension_registry_->enabled_extensions(); 820 extension_registry_->enabled_extensions();
817 for (ExtensionSet::const_iterator extension = enabled_extensions.begin(); 821 for (ExtensionSet::const_iterator extension = enabled_extensions.begin();
818 extension != enabled_extensions.end(); 822 extension != enabled_extensions.end();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 ProcessManager* original_manager, 895 ProcessManager* original_manager,
892 ExtensionRegistry* extension_registry) 896 ExtensionRegistry* extension_registry)
893 : ProcessManager(incognito_context, original_context, extension_registry), 897 : ProcessManager(incognito_context, original_context, extension_registry),
894 original_manager_(original_manager) { 898 original_manager_(original_manager) {
895 DCHECK(incognito_context->IsOffTheRecord()); 899 DCHECK(incognito_context->IsOffTheRecord());
896 900
897 // The original profile will have its own ProcessManager to 901 // The original profile will have its own ProcessManager to
898 // load the background pages of the spanning extensions. This process 902 // load the background pages of the spanning extensions. This process
899 // manager need only worry about the split mode extensions, which is handled 903 // manager need only worry about the split mode extensions, which is handled
900 // in the NOTIFICATION_BROWSER_WINDOW_READY notification handler. 904 // in the NOTIFICATION_BROWSER_WINDOW_READY notification handler.
901 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSIONS_READY, 905 registrar_.Remove(this,
906 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
902 content::Source<BrowserContext>(original_context)); 907 content::Source<BrowserContext>(original_context));
903 } 908 }
904 909
905 bool IncognitoProcessManager::CreateBackgroundHost(const Extension* extension, 910 bool IncognitoProcessManager::CreateBackgroundHost(const Extension* extension,
906 const GURL& url) { 911 const GURL& url) {
907 if (IncognitoInfo::IsSplitMode(extension)) { 912 if (IncognitoInfo::IsSplitMode(extension)) {
908 if (ExtensionsBrowserClient::Get()->IsExtensionIncognitoEnabled( 913 if (ExtensionsBrowserClient::Get()->IsExtensionIncognitoEnabled(
909 extension->id(), GetBrowserContext())) 914 extension->id(), GetBrowserContext()))
910 return ProcessManager::CreateBackgroundHost(extension, url); 915 return ProcessManager::CreateBackgroundHost(extension, url);
911 } else { 916 } else {
912 // Do nothing. If an extension is spanning, then its original-profile 917 // Do nothing. If an extension is spanning, then its original-profile
913 // background page is shared with incognito, so we don't create another. 918 // background page is shared with incognito, so we don't create another.
914 } 919 }
915 return false; 920 return false;
916 } 921 }
917 922
918 SiteInstance* IncognitoProcessManager::GetSiteInstanceForURL(const GURL& url) { 923 SiteInstance* IncognitoProcessManager::GetSiteInstanceForURL(const GURL& url) {
919 const Extension* extension = 924 const Extension* extension =
920 extension_registry_->enabled_extensions().GetExtensionOrAppByURL(url); 925 extension_registry_->enabled_extensions().GetExtensionOrAppByURL(url);
921 if (extension && !IncognitoInfo::IsSplitMode(extension)) 926 if (extension && !IncognitoInfo::IsSplitMode(extension))
922 return original_manager_->GetSiteInstanceForURL(url); 927 return original_manager_->GetSiteInstanceForURL(url);
923 928
924 return ProcessManager::GetSiteInstanceForURL(url); 929 return ProcessManager::GetSiteInstanceForURL(url);
925 } 930 }
926 931
927 } // namespace extensions 932 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/notification_types.h ('k') | extensions/browser/process_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698