OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/extensions/chrome_process_manager_delegate.h" |
| 6 |
| 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 "content/public/browser/notification_service.h" |
| 15 #include "extensions/browser/extension_system.h" |
| 16 #include "extensions/browser/process_manager.h" |
| 17 #include "extensions/common/one_shot_event.h" |
| 18 |
| 19 #if !defined(OS_ANDROID) |
| 20 #include "chrome/browser/ui/browser_finder.h" |
| 21 #include "chrome/common/chrome_switches.h" |
| 22 #endif |
| 23 |
| 24 namespace extensions { |
| 25 |
| 26 ChromeProcessManagerDelegate::ChromeProcessManagerDelegate() { |
| 27 registrar_.Add(this, |
| 28 chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| 29 content::NotificationService::AllSources()); |
| 30 registrar_.Add(this, |
| 31 chrome::NOTIFICATION_PROFILE_CREATED, |
| 32 content::NotificationService::AllSources()); |
| 33 // TODO(jamescook): Move observation of NOTIFICATION_PROFILE_DESTROYED here. |
| 34 // http://crbug.com/392658 |
| 35 } |
| 36 |
| 37 ChromeProcessManagerDelegate::~ChromeProcessManagerDelegate() { |
| 38 } |
| 39 |
| 40 bool ChromeProcessManagerDelegate::IsBackgroundPageAllowed( |
| 41 content::BrowserContext* context) const { |
| 42 Profile* profile = static_cast<Profile*>(context); |
| 43 |
| 44 // Disallow if the current session is a Guest mode session but the current |
| 45 // browser context is *not* off-the-record. Such context is artificial and |
| 46 // background page shouldn't be created in it. |
| 47 // http://crbug.com/329498 |
| 48 return !(profile->IsGuestSession() && !profile->IsOffTheRecord()); |
| 49 } |
| 50 |
| 51 bool ChromeProcessManagerDelegate::DeferCreatingStartupBackgroundHosts( |
| 52 content::BrowserContext* context) const { |
| 53 Profile* profile = static_cast<Profile*>(context); |
| 54 |
| 55 // The profile may not be valid yet if it is still being initialized. |
| 56 // In that case, defer loading, since it depends on an initialized profile. |
| 57 // Background hosts will be loaded later via NOTIFICATION_PROFILE_CREATED. |
| 58 // http://crbug.com/222473 |
| 59 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) |
| 60 return true; |
| 61 |
| 62 #if defined(OS_ANDROID) |
| 63 return false; |
| 64 #else |
| 65 // There are no browser windows open and the browser process was |
| 66 // started to show the app launcher. Background hosts will be loaded later |
| 67 // via NOTIFICATION_BROWSER_WINDOW_READY. http://crbug.com/178260 |
| 68 return chrome::GetTotalBrowserCountForProfile(profile) == 0 && |
| 69 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList); |
| 70 #endif |
| 71 } |
| 72 |
| 73 void ChromeProcessManagerDelegate::Observe( |
| 74 int type, |
| 75 const content::NotificationSource& source, |
| 76 const content::NotificationDetails& details) { |
| 77 switch (type) { |
| 78 case chrome::NOTIFICATION_BROWSER_WINDOW_READY: { |
| 79 Browser* browser = content::Source<Browser>(source).ptr(); |
| 80 OnBrowserWindowReady(browser); |
| 81 break; |
| 82 } |
| 83 case chrome::NOTIFICATION_PROFILE_CREATED: { |
| 84 Profile* profile = content::Source<Profile>(source).ptr(); |
| 85 OnProfileCreated(profile); |
| 86 break; |
| 87 } |
| 88 |
| 89 default: |
| 90 NOTREACHED(); |
| 91 } |
| 92 } |
| 93 |
| 94 void ChromeProcessManagerDelegate::OnBrowserWindowReady(Browser* browser) { |
| 95 Profile* profile = browser->profile(); |
| 96 DCHECK(profile); |
| 97 |
| 98 // If the extension system isn't ready yet the background hosts will be |
| 99 // created automatically when it is. |
| 100 ExtensionSystem* system = ExtensionSystem::Get(profile); |
| 101 if (!system->ready().is_signaled()) |
| 102 return; |
| 103 |
| 104 // Inform the process manager for this profile that the window is ready. |
| 105 // We continue to observe the notification in case browser windows open for |
| 106 // a related incognito profile or other regular profiles. |
| 107 ProcessManager* manager = system->process_manager(); |
| 108 if (!manager) // Tests may not have a process manager. |
| 109 return; |
| 110 DCHECK_EQ(profile, manager->GetBrowserContext()); |
| 111 manager->MaybeCreateStartupBackgroundHosts(); |
| 112 |
| 113 // For incognito profiles also inform the original profile's process manager |
| 114 // that the window is ready. This will usually be a no-op because the |
| 115 // original profile's process manager should have been informed when the |
| 116 // non-incognito window opened. |
| 117 if (profile->IsOffTheRecord()) { |
| 118 Profile* original_profile = profile->GetOriginalProfile(); |
| 119 ProcessManager* original_manager = |
| 120 ExtensionSystem::Get(original_profile)->process_manager(); |
| 121 DCHECK(original_manager); |
| 122 DCHECK_EQ(original_profile, original_manager->GetBrowserContext()); |
| 123 original_manager->MaybeCreateStartupBackgroundHosts(); |
| 124 } |
| 125 } |
| 126 |
| 127 void ChromeProcessManagerDelegate::OnProfileCreated(Profile* profile) { |
| 128 // Incognito profiles are handled by their original profile. |
| 129 if (profile->IsOffTheRecord()) |
| 130 return; |
| 131 |
| 132 // The profile can be created before the extension system is ready. |
| 133 ProcessManager* manager = ExtensionSystem::Get(profile)->process_manager(); |
| 134 if (!manager) |
| 135 return; |
| 136 |
| 137 // The profile might have been initialized asynchronously (in parallel with |
| 138 // extension system startup). Now that initialization is complete the |
| 139 // ProcessManager can load deferred background pages. |
| 140 manager->MaybeCreateStartupBackgroundHosts(); |
| 141 } |
| 142 |
| 143 } // namespace extensions |
OLD | NEW |