1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "content/public/browser/notification_service.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/browser/process_manager.h"
19 #include "extensions/browser/process_manager_factory.h"
20 #include "extensions/common/one_shot_event.h"
22 namespace extensions {
24 ChromeProcessManagerDelegate::ChromeProcessManagerDelegate() {
26 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
27 content::NotificationService::AllSources());
29 chrome::NOTIFICATION_PROFILE_CREATED,
30 content::NotificationService::AllSources());
32 chrome::NOTIFICATION_PROFILE_DESTROYED,
33 content::NotificationService::AllSources());
36 ChromeProcessManagerDelegate::~ChromeProcessManagerDelegate() {
39 bool ChromeProcessManagerDelegate::IsBackgroundPageAllowed(
40 content::BrowserContext* context) const {
41 Profile* profile = static_cast<Profile*>(context);
43 // Disallow if the current session is a Guest mode session but the current
44 // browser context is *not* off-the-record. Such context is artificial and
45 // background page shouldn't be created in it.
46 // http://crbug.com/329498
47 return !(profile->IsGuestSession() && !profile->IsOffTheRecord());
50 bool ChromeProcessManagerDelegate::DeferCreatingStartupBackgroundHosts(
51 content::BrowserContext* context) const {
52 Profile* profile = static_cast<Profile*>(context);
54 // The profile may not be valid yet if it is still being initialized.
55 // In that case, defer loading, since it depends on an initialized profile.
56 // Background hosts will be loaded later via NOTIFICATION_PROFILE_CREATED.
57 // http://crbug.com/222473
58 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
61 // There are no browser windows open and the browser process was
62 // started to show the app launcher. Background hosts will be loaded later
63 // via NOTIFICATION_BROWSER_WINDOW_READY. http://crbug.com/178260
64 return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
65 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList);
68 void ChromeProcessManagerDelegate::Observe(
70 const content::NotificationSource& source,
71 const content::NotificationDetails& details) {
73 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
74 Browser* browser = content::Source<Browser>(source).ptr();
75 OnBrowserWindowReady(browser);
78 case chrome::NOTIFICATION_PROFILE_CREATED: {
79 Profile* profile = content::Source<Profile>(source).ptr();
80 OnProfileCreated(profile);
83 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
84 Profile* profile = content::Source<Profile>(source).ptr();
85 OnProfileDestroyed(profile);
93 void ChromeProcessManagerDelegate::OnBrowserWindowReady(Browser* browser) {
94 Profile* profile = browser->profile();
97 // If the extension system isn't ready yet the background hosts will be
98 // created automatically when it is.
99 ExtensionSystem* system = ExtensionSystem::Get(profile);
100 if (!system->ready().is_signaled())
103 // Inform the process manager for this profile that the window is ready.
104 // We continue to observe the notification in case browser windows open for
105 // a related incognito profile or other regular profiles.
106 ProcessManager* manager = ProcessManager::Get(profile);
108 DCHECK_EQ(profile, manager->GetBrowserContext());
109 manager->MaybeCreateStartupBackgroundHosts();
111 // For incognito profiles also inform the original profile's process manager
112 // that the window is ready. This will usually be a no-op because the
113 // original profile's process manager should have been informed when the
114 // non-incognito window opened.
115 if (profile->IsOffTheRecord()) {
116 Profile* original_profile = profile->GetOriginalProfile();
117 ProcessManager* original_manager = ProcessManager::Get(original_profile);
118 DCHECK(original_manager);
119 DCHECK_EQ(original_profile, original_manager->GetBrowserContext());
120 original_manager->MaybeCreateStartupBackgroundHosts();
124 void ChromeProcessManagerDelegate::OnProfileCreated(Profile* profile) {
125 // Incognito profiles are handled by their original profile.
126 if (profile->IsOffTheRecord())
129 // The profile can be created before the extension system is ready.
130 if (!ExtensionSystem::Get(profile)->ready().is_signaled())
133 // The profile might have been initialized asynchronously (in parallel with
134 // extension system startup). Now that initialization is complete the
135 // ProcessManager can load deferred background pages.
136 ProcessManager::Get(profile)->MaybeCreateStartupBackgroundHosts();
139 void ChromeProcessManagerDelegate::OnProfileDestroyed(Profile* profile) {
140 // Close background hosts when the last profile is closed so that they
141 // have time to shutdown various objects on different threads. The
142 // ProfileManager destructor is called too late in the shutdown sequence.
143 // http://crbug.com/15708
144 ProcessManager* manager =
145 ProcessManagerFactory::GetForBrowserContextIfExists(profile);
147 manager->CloseBackgroundHosts();
150 // If this profile owns an incognito profile, but it is destroyed before the
151 // incognito profile is destroyed, then close the incognito background hosts
152 // as well. This happens in a few tests. http://crbug.com/138843
153 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) {
154 ProcessManager* incognito_manager =
155 ProcessManagerFactory::GetForBrowserContextIfExists(
156 profile->GetOffTheRecordProfile());
157 if (incognito_manager) {
158 incognito_manager->CloseBackgroundHosts();
163 } // namespace extensions