Make the ChromeOS chromecast system tray integration use a private API.
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_process_manager_delegate.cc
blobd141058e816c738f36a8af8d3c4b5b28bf3efc03
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 !profile->IsSystemProfile();
46 #if defined(OS_CHROMEOS)
47 is_normal_session = is_normal_session &&
48 user_manager::UserManager::Get()->IsUserLoggedIn();
49 #endif
51 // Disallow if the current session is a Guest mode session or login screen but
52 // the current browser context is *not* off-the-record. Such context is
53 // artificial and background page shouldn't be created in it.
54 // http://crbug.com/329498
55 return is_normal_session || profile->IsOffTheRecord();
58 bool ChromeProcessManagerDelegate::DeferCreatingStartupBackgroundHosts(
59 content::BrowserContext* context) const {
60 Profile* profile = static_cast<Profile*>(context);
62 // The profile may not be valid yet if it is still being initialized.
63 // In that case, defer loading, since it depends on an initialized profile.
64 // Background hosts will be loaded later via NOTIFICATION_PROFILE_CREATED.
65 // http://crbug.com/222473
66 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
67 return true;
69 // There are no browser windows open and the browser process was
70 // started to show the app launcher. Background hosts will be loaded later
71 // via NOTIFICATION_BROWSER_WINDOW_READY. http://crbug.com/178260
72 return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
73 base::CommandLine::ForCurrentProcess()->HasSwitch(
74 switches::kShowAppList);
77 void ChromeProcessManagerDelegate::Observe(
78 int type,
79 const content::NotificationSource& source,
80 const content::NotificationDetails& details) {
81 switch (type) {
82 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
83 Browser* browser = content::Source<Browser>(source).ptr();
84 OnBrowserWindowReady(browser);
85 break;
87 case chrome::NOTIFICATION_PROFILE_CREATED: {
88 Profile* profile = content::Source<Profile>(source).ptr();
89 OnProfileCreated(profile);
90 break;
92 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
93 Profile* profile = content::Source<Profile>(source).ptr();
94 OnProfileDestroyed(profile);
95 break;
97 default:
98 NOTREACHED();
102 void ChromeProcessManagerDelegate::OnBrowserWindowReady(Browser* browser) {
103 Profile* profile = browser->profile();
104 DCHECK(profile);
106 // If the extension system isn't ready yet the background hosts will be
107 // created automatically when it is.
108 ExtensionSystem* system = ExtensionSystem::Get(profile);
109 if (!system->ready().is_signaled())
110 return;
112 // Inform the process manager for this profile that the window is ready.
113 // We continue to observe the notification in case browser windows open for
114 // a related incognito profile or other regular profiles.
115 ProcessManager* manager = ProcessManager::Get(profile);
116 DCHECK(manager);
117 DCHECK_EQ(profile, manager->browser_context());
118 manager->MaybeCreateStartupBackgroundHosts();
120 // For incognito profiles also inform the original profile's process manager
121 // that the window is ready. This will usually be a no-op because the
122 // original profile's process manager should have been informed when the
123 // non-incognito window opened.
124 if (profile->IsOffTheRecord()) {
125 Profile* original_profile = profile->GetOriginalProfile();
126 ProcessManager* original_manager = ProcessManager::Get(original_profile);
127 DCHECK(original_manager);
128 DCHECK_EQ(original_profile, original_manager->browser_context());
129 original_manager->MaybeCreateStartupBackgroundHosts();
133 void ChromeProcessManagerDelegate::OnProfileCreated(Profile* profile) {
134 // Incognito profiles are handled by their original profile.
135 if (profile->IsOffTheRecord())
136 return;
138 // The profile can be created before the extension system is ready.
139 if (!ExtensionSystem::Get(profile)->ready().is_signaled())
140 return;
142 // The profile might have been initialized asynchronously (in parallel with
143 // extension system startup). Now that initialization is complete the
144 // ProcessManager can load deferred background pages.
145 ProcessManager::Get(profile)->MaybeCreateStartupBackgroundHosts();
148 void ChromeProcessManagerDelegate::OnProfileDestroyed(Profile* profile) {
149 // Close background hosts when the last profile is closed so that they
150 // have time to shutdown various objects on different threads. The
151 // ProfileManager destructor is called too late in the shutdown sequence.
152 // http://crbug.com/15708
153 ProcessManager* manager =
154 ProcessManagerFactory::GetForBrowserContextIfExists(profile);
155 if (manager) {
156 manager->CloseBackgroundHosts();
159 // If this profile owns an incognito profile, but it is destroyed before the
160 // incognito profile is destroyed, then close the incognito background hosts
161 // as well. This happens in a few tests. http://crbug.com/138843
162 if (!profile->IsOffTheRecord() && profile->HasOffTheRecordProfile()) {
163 ProcessManager* incognito_manager =
164 ProcessManagerFactory::GetForBrowserContextIfExists(
165 profile->GetOffTheRecordProfile());
166 if (incognito_manager) {
167 incognito_manager->CloseBackgroundHosts();
172 } // namespace extensions