Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_process_manager_delegate.cc
blobe0d98752277247edf30d23739c2305a17e5a1851
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 "components/user_manager/user_manager.h"
17 #include "content/public/browser/notification_service.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/browser/process_manager_factory.h"
21 #include "extensions/common/one_shot_event.h"
23 namespace extensions {
25 ChromeProcessManagerDelegate::ChromeProcessManagerDelegate() {
26 registrar_.Add(this,
27 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
28 content::NotificationService::AllSources());
29 registrar_.Add(this,
30 chrome::NOTIFICATION_PROFILE_CREATED,
31 content::NotificationService::AllSources());
32 registrar_.Add(this,
33 chrome::NOTIFICATION_PROFILE_DESTROYED,
34 content::NotificationService::AllSources());
37 ChromeProcessManagerDelegate::~ChromeProcessManagerDelegate() {
40 bool ChromeProcessManagerDelegate::IsBackgroundPageAllowed(
41 content::BrowserContext* context) const {
42 Profile* profile = static_cast<Profile*>(context);
44 bool is_normal_session = !profile->IsGuestSession();
45 #if defined(OS_CHROMEOS)
46 is_normal_session = is_normal_session &&
47 user_manager::UserManager::Get()->IsUserLoggedIn();
48 #endif
50 // Disallow if the current session is a Guest mode session or login screen but
51 // the current browser context is *not* off-the-record. Such context is
52 // artificial and background page shouldn't be created in it.
53 // http://crbug.com/329498
54 return is_normal_session || profile->IsOffTheRecord();
57 bool ChromeProcessManagerDelegate::DeferCreatingStartupBackgroundHosts(
58 content::BrowserContext* context) const {
59 Profile* profile = static_cast<Profile*>(context);
61 // The profile may not be valid yet if it is still being initialized.
62 // In that case, defer loading, since it depends on an initialized profile.
63 // Background hosts will be loaded later via NOTIFICATION_PROFILE_CREATED.
64 // http://crbug.com/222473
65 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
66 return true;
68 // There are no browser windows open and the browser process was
69 // started to show the app launcher. Background hosts will be loaded later
70 // via NOTIFICATION_BROWSER_WINDOW_READY. http://crbug.com/178260
71 return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
72 base::CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kShowAppList);
76 void ChromeProcessManagerDelegate::Observe(
77 int type,
78 const content::NotificationSource& source,
79 const content::NotificationDetails& details) {
80 switch (type) {
81 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
82 Browser* browser = content::Source<Browser>(source).ptr();
83 OnBrowserWindowReady(browser);
84 break;
86 case chrome::NOTIFICATION_PROFILE_CREATED: {
87 Profile* profile = content::Source<Profile>(source).ptr();
88 OnProfileCreated(profile);
89 break;
91 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
92 Profile* profile = content::Source<Profile>(source).ptr();
93 OnProfileDestroyed(profile);
94 break;
96 default:
97 NOTREACHED();
101 void ChromeProcessManagerDelegate::OnBrowserWindowReady(Browser* browser) {
102 Profile* profile = browser->profile();
103 DCHECK(profile);
105 // If the extension system isn't ready yet the background hosts will be
106 // created automatically when it is.
107 ExtensionSystem* system = ExtensionSystem::Get(profile);
108 if (!system->ready().is_signaled())
109 return;
111 // Inform the process manager for this profile that the window is ready.
112 // We continue to observe the notification in case browser windows open for
113 // a related incognito profile or other regular profiles.
114 ProcessManager* manager = ProcessManager::Get(profile);
115 DCHECK(manager);
116 DCHECK_EQ(profile, manager->GetBrowserContext());
117 manager->MaybeCreateStartupBackgroundHosts();
119 // For incognito profiles also inform the original profile's process manager
120 // that the window is ready. This will usually be a no-op because the
121 // original profile's process manager should have been informed when the
122 // non-incognito window opened.
123 if (profile->IsOffTheRecord()) {
124 Profile* original_profile = profile->GetOriginalProfile();
125 ProcessManager* original_manager = ProcessManager::Get(original_profile);
126 DCHECK(original_manager);
127 DCHECK_EQ(original_profile, original_manager->GetBrowserContext());
128 original_manager->MaybeCreateStartupBackgroundHosts();
132 void ChromeProcessManagerDelegate::OnProfileCreated(Profile* profile) {
133 // Incognito profiles are handled by their original profile.
134 if (profile->IsOffTheRecord())
135 return;
137 // The profile can be created before the extension system is ready.
138 if (!ExtensionSystem::Get(profile)->ready().is_signaled())
139 return;
141 // The profile might have been initialized asynchronously (in parallel with
142 // extension system startup). Now that initialization is complete the
143 // ProcessManager can load deferred background pages.
144 ProcessManager::Get(profile)->MaybeCreateStartupBackgroundHosts();
147 void ChromeProcessManagerDelegate::OnProfileDestroyed(Profile* profile) {
148 // Close background hosts when the last profile is closed so that they
149 // have time to shutdown various objects on different threads. The
150 // ProfileManager destructor is called too late in the shutdown sequence.
151 // http://crbug.com/15708
152 ProcessManager* manager =
153 ProcessManagerFactory::GetForBrowserContextIfExists(profile);
154 if (manager) {
155 manager->CloseBackgroundHosts();
158 // If this profile owns an incognito profile, but it is destroyed before the
159 // incognito profile is destroyed, then close the incognito background hosts
160 // as well. This happens in a few tests. http://crbug.com/138843
161 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) {
162 ProcessManager* incognito_manager =
163 ProcessManagerFactory::GetForBrowserContextIfExists(
164 profile->GetOffTheRecordProfile());
165 if (incognito_manager) {
166 incognito_manager->CloseBackgroundHosts();
171 } // namespace extensions