1 // Copyright 2013 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/lifetime/browser_close_manager.h"
7 #include "chrome/browser/background/background_mode_manager.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/browser_shutdown.h"
10 #include "chrome/browser/download/download_service.h"
11 #include "chrome/browser/download/download_service_factory.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "content/public/browser/web_contents.h"
24 // Navigates a browser window for |profile|, creating one if necessary, to the
25 // downloads page if there are downloads in progress for |profile|.
26 void ShowInProgressDownloads(Profile
* profile
) {
27 DownloadService
* download_service
=
28 DownloadServiceFactory::GetForBrowserContext(profile
);
29 if (download_service
->NonMaliciousDownloadCount() > 0) {
30 chrome::ScopedTabbedBrowserDisplayer
displayer(profile
,
31 chrome::GetActiveDesktop());
32 chrome::ShowDownloads(displayer
.browser());
38 BrowserCloseManager::BrowserCloseManager() : current_browser_(NULL
) {}
40 BrowserCloseManager::~BrowserCloseManager() {}
42 void BrowserCloseManager::StartClosingBrowsers() {
43 // If the session is ending, skip straight to closing the browsers. There's no
44 // time to wait for beforeunload dialogs.
45 if (browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION
) {
46 // Tell everyone that we are shutting down.
47 browser_shutdown::SetTryingToQuit(true);
54 void BrowserCloseManager::CancelBrowserClose() {
55 browser_shutdown::SetTryingToQuit(false);
56 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
57 it
->ResetBeforeUnloadHandlers();
61 void BrowserCloseManager::TryToCloseBrowsers() {
62 // If all browser windows can immediately be closed, fall out of this loop and
63 // close the browsers. If any browser window cannot be closed, temporarily
64 // stop closing. CallBeforeUnloadHandlers prompts the user and calls
65 // OnBrowserReportCloseable with the result. If the user confirms the close,
66 // this will trigger TryToCloseBrowsers to try again.
67 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
68 if (it
->CallBeforeUnloadHandlers(
69 base::Bind(&BrowserCloseManager::OnBrowserReportCloseable
, this))) {
70 current_browser_
= *it
;
74 CheckForDownloadsInProgress();
77 void BrowserCloseManager::OnBrowserReportCloseable(bool proceed
) {
78 if (!current_browser_
)
81 current_browser_
= NULL
;
89 void BrowserCloseManager::CheckForDownloadsInProgress() {
90 int download_count
= DownloadService::NonMaliciousDownloadCountAllProfiles();
91 if (download_count
== 0) {
95 ConfirmCloseWithPendingDownloads(
97 base::Bind(&BrowserCloseManager::OnReportDownloadsCancellable
, this));
100 void BrowserCloseManager::ConfirmCloseWithPendingDownloads(
102 const base::Callback
<void(bool)>& callback
) {
104 BrowserList::GetInstance(chrome::GetActiveDesktop())->GetLastActive();
106 browser
->window()->ConfirmBrowserCloseWithPendingDownloads(
108 Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN
,
113 void BrowserCloseManager::OnReportDownloadsCancellable(bool proceed
) {
119 CancelBrowserClose();
121 // Open the downloads page for each profile with downloads in progress.
122 std::vector
<Profile
*> profiles(
123 g_browser_process
->profile_manager()->GetLoadedProfiles());
124 for (Profile
* profile
: profiles
) {
125 ShowInProgressDownloads(profile
);
126 if (profile
->HasOffTheRecordProfile())
127 ShowInProgressDownloads(profile
->GetOffTheRecordProfile());
131 void BrowserCloseManager::CloseBrowsers() {
132 #if defined(ENABLE_SESSION_SERVICE)
133 // Before we close the browsers shutdown all session services. That way an
134 // exit can restore all browsers open before exiting.
135 ProfileManager::ShutdownSessionServices();
137 if (!browser_shutdown::IsTryingToQuit()) {
138 BackgroundModeManager
* background_mode_manager
=
139 g_browser_process
->background_mode_manager();
140 if (background_mode_manager
)
141 background_mode_manager
->SuspendBackgroundMode();
144 bool session_ending
=
145 browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION
;
146 for (scoped_ptr
<chrome::BrowserIterator
> it_ptr(
147 new chrome::BrowserIterator());
149 Browser
* browser
= **it_ptr
;
150 browser
->window()->Close();
151 if (!session_ending
) {
154 // This path is hit during logoff/power-down. In this case we won't get
155 // a final message and so we force the browser to be deleted.
156 // Close doesn't immediately destroy the browser
157 // (Browser::TabStripEmpty() uses invoke later) but when we're ending the
158 // session we need to make sure the browser is destroyed now. So, invoke
159 // DestroyBrowser to make sure the browser is deleted and cleanup can
161 while (browser
->tab_strip_model()->count())
162 delete browser
->tab_strip_model()->GetWebContentsAt(0);
163 browser
->window()->DestroyBrowser();
164 it_ptr
.reset(new chrome::BrowserIterator());
165 if (!it_ptr
->done() && browser
== **it_ptr
) {
166 // Destroying the browser should have removed it from the browser list.
167 // We should never get here.