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 "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/common/one_shot_event.h"
20
21namespace extensions {
22
23ChromeProcessManagerDelegate::ChromeProcessManagerDelegate() {
24  registrar_.Add(this,
25                 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
26                 content::NotificationService::AllSources());
27  registrar_.Add(this,
28                 chrome::NOTIFICATION_PROFILE_CREATED,
29                 content::NotificationService::AllSources());
30  registrar_.Add(this,
31                 chrome::NOTIFICATION_PROFILE_DESTROYED,
32                 content::NotificationService::AllSources());
33}
34
35ChromeProcessManagerDelegate::~ChromeProcessManagerDelegate() {
36}
37
38bool ChromeProcessManagerDelegate::IsBackgroundPageAllowed(
39    content::BrowserContext* context) const {
40  Profile* profile = static_cast<Profile*>(context);
41
42  // Disallow if the current session is a Guest mode session but the current
43  // browser context is *not* off-the-record. Such context is artificial and
44  // background page shouldn't be created in it.
45  // http://crbug.com/329498
46  return !(profile->IsGuestSession() && !profile->IsOffTheRecord());
47}
48
49bool ChromeProcessManagerDelegate::DeferCreatingStartupBackgroundHosts(
50    content::BrowserContext* context) const {
51  Profile* profile = static_cast<Profile*>(context);
52
53  // The profile may not be valid yet if it is still being initialized.
54  // In that case, defer loading, since it depends on an initialized profile.
55  // Background hosts will be loaded later via NOTIFICATION_PROFILE_CREATED.
56  // http://crbug.com/222473
57  if (!g_browser_process->profile_manager()->IsValidProfile(profile))
58    return true;
59
60  // There are no browser windows open and the browser process was
61  // started to show the app launcher. Background hosts will be loaded later
62  // via NOTIFICATION_BROWSER_WINDOW_READY. http://crbug.com/178260
63  return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
64         CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList);
65}
66
67void ChromeProcessManagerDelegate::Observe(
68    int type,
69    const content::NotificationSource& source,
70    const content::NotificationDetails& details) {
71  switch (type) {
72    case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
73      Browser* browser = content::Source<Browser>(source).ptr();
74      OnBrowserWindowReady(browser);
75      break;
76    }
77    case chrome::NOTIFICATION_PROFILE_CREATED: {
78      Profile* profile = content::Source<Profile>(source).ptr();
79      OnProfileCreated(profile);
80      break;
81    }
82    case chrome::NOTIFICATION_PROFILE_DESTROYED: {
83      Profile* profile = content::Source<Profile>(source).ptr();
84      OnProfileDestroyed(profile);
85      break;
86    }
87    default:
88      NOTREACHED();
89  }
90}
91
92void ChromeProcessManagerDelegate::OnBrowserWindowReady(Browser* browser) {
93  Profile* profile = browser->profile();
94  DCHECK(profile);
95
96  // If the extension system isn't ready yet the background hosts will be
97  // created automatically when it is.
98  ExtensionSystem* system = ExtensionSystem::Get(profile);
99  if (!system->ready().is_signaled())
100    return;
101
102  // Inform the process manager for this profile that the window is ready.
103  // We continue to observe the notification in case browser windows open for
104  // a related incognito profile or other regular profiles.
105  ProcessManager* manager = system->process_manager();
106  if (!manager)  // Tests may not have a process manager.
107    return;
108  DCHECK_EQ(profile, manager->GetBrowserContext());
109  manager->MaybeCreateStartupBackgroundHosts();
110
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 =
118        ExtensionSystem::Get(original_profile)->process_manager();
119    DCHECK(original_manager);
120    DCHECK_EQ(original_profile, original_manager->GetBrowserContext());
121    original_manager->MaybeCreateStartupBackgroundHosts();
122  }
123}
124
125void ChromeProcessManagerDelegate::OnProfileCreated(Profile* profile) {
126  // Incognito profiles are handled by their original profile.
127  if (profile->IsOffTheRecord())
128    return;
129
130  // The profile can be created before the extension system is ready.
131  ProcessManager* manager = ExtensionSystem::Get(profile)->process_manager();
132  if (!manager)
133    return;
134
135  // The profile might have been initialized asynchronously (in parallel with
136  // extension system startup). Now that initialization is complete the
137  // ProcessManager can load deferred background pages.
138  manager->MaybeCreateStartupBackgroundHosts();
139}
140
141void ChromeProcessManagerDelegate::OnProfileDestroyed(Profile* profile) {
142  // Close background hosts when the last profile is closed so that they
143  // have time to shutdown various objects on different threads. The
144  // ProfileManager destructor is called too late in the shutdown sequence.
145  // http://crbug.com/15708
146  ProcessManager* manager = ExtensionSystem::Get(profile)->process_manager();
147  if (manager)
148    manager->CloseBackgroundHosts();
149
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        ExtensionSystem::Get(profile->GetOffTheRecordProfile())
156            ->process_manager();
157    if (incognito_manager)
158      incognito_manager->CloseBackgroundHosts();
159  }
160}
161
162}  // namespace extensions
163