Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / chrome_process_manager_delegate.cc
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/browser/process_manager_factory.h"
20 #include "extensions/common/one_shot_event.h"
21
22 namespace extensions {
23
24 ChromeProcessManagerDelegate::ChromeProcessManagerDelegate() {
25   registrar_.Add(this,
26                  chrome::NOTIFICATION_BROWSER_WINDOW_READY,
27                  content::NotificationService::AllSources());
28   registrar_.Add(this,
29                  chrome::NOTIFICATION_PROFILE_CREATED,
30                  content::NotificationService::AllSources());
31   registrar_.Add(this,
32                  chrome::NOTIFICATION_PROFILE_DESTROYED,
33                  content::NotificationService::AllSources());
34 }
35
36 ChromeProcessManagerDelegate::~ChromeProcessManagerDelegate() {
37 }
38
39 bool ChromeProcessManagerDelegate::IsBackgroundPageAllowed(
40     content::BrowserContext* context) const {
41   Profile* profile = static_cast<Profile*>(context);
42
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());
48 }
49
50 bool ChromeProcessManagerDelegate::DeferCreatingStartupBackgroundHosts(
51     content::BrowserContext* context) const {
52   Profile* profile = static_cast<Profile*>(context);
53
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))
59     return true;
60
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);
66 }
67
68 void ChromeProcessManagerDelegate::Observe(
69     int type,
70     const content::NotificationSource& source,
71     const content::NotificationDetails& details) {
72   switch (type) {
73     case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
74       Browser* browser = content::Source<Browser>(source).ptr();
75       OnBrowserWindowReady(browser);
76       break;
77     }
78     case chrome::NOTIFICATION_PROFILE_CREATED: {
79       Profile* profile = content::Source<Profile>(source).ptr();
80       OnProfileCreated(profile);
81       break;
82     }
83     case chrome::NOTIFICATION_PROFILE_DESTROYED: {
84       Profile* profile = content::Source<Profile>(source).ptr();
85       OnProfileDestroyed(profile);
86       break;
87     }
88     default:
89       NOTREACHED();
90   }
91 }
92
93 void ChromeProcessManagerDelegate::OnBrowserWindowReady(Browser* browser) {
94   Profile* profile = browser->profile();
95   DCHECK(profile);
96
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())
101     return;
102
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);
107   DCHECK(manager);
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 = ProcessManager::Get(original_profile);
118     DCHECK(original_manager);
119     DCHECK_EQ(original_profile, original_manager->GetBrowserContext());
120     original_manager->MaybeCreateStartupBackgroundHosts();
121   }
122 }
123
124 void ChromeProcessManagerDelegate::OnProfileCreated(Profile* profile) {
125   // Incognito profiles are handled by their original profile.
126   if (profile->IsOffTheRecord())
127     return;
128
129   // The profile can be created before the extension system is ready.
130   if (!ExtensionSystem::Get(profile)->ready().is_signaled())
131     return;
132
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();
137 }
138
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);
146   if (manager) {
147     manager->CloseBackgroundHosts();
148   }
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         ProcessManagerFactory::GetForBrowserContextIfExists(
156             profile->GetOffTheRecordProfile());
157     if (incognito_manager) {
158       incognito_manager->CloseBackgroundHosts();
159     }
160   }
161 }
162
163 }  // namespace extensions