This source file includes following definitions.
- StartClosingBrowsers
- CancelBrowserClose
- TryToCloseBrowsers
- OnBrowserReportCloseable
- CheckForDownloadsInProgress
- ConfirmCloseWithPendingDownloads
- OnReportDownloadsCancellable
- CloseBrowsers
#include "chrome/browser/lifetime/browser_close_manager.h"
#include "chrome/browser/background/background_mode_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/download/download_service.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/web_contents.h"
BrowserCloseManager::BrowserCloseManager() : current_browser_(NULL) {}
BrowserCloseManager::~BrowserCloseManager() {}
void BrowserCloseManager::StartClosingBrowsers() {
if (browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION) {
browser_shutdown::SetTryingToQuit(true);
CloseBrowsers();
return;
}
TryToCloseBrowsers();
}
void BrowserCloseManager::CancelBrowserClose() {
browser_shutdown::SetTryingToQuit(false);
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
it->ResetBeforeUnloadHandlers();
}
}
void BrowserCloseManager::TryToCloseBrowsers() {
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
if (it->CallBeforeUnloadHandlers(
base::Bind(&BrowserCloseManager::OnBrowserReportCloseable, this))) {
current_browser_ = *it;
return;
}
}
CheckForDownloadsInProgress();
}
void BrowserCloseManager::OnBrowserReportCloseable(bool proceed) {
if (!current_browser_)
return;
current_browser_ = NULL;
if (proceed)
TryToCloseBrowsers();
else
CancelBrowserClose();
}
void BrowserCloseManager::CheckForDownloadsInProgress() {
int download_count = DownloadService::NonMaliciousDownloadCountAllProfiles();
if (download_count == 0) {
CloseBrowsers();
return;
}
ConfirmCloseWithPendingDownloads(
download_count,
base::Bind(&BrowserCloseManager::OnReportDownloadsCancellable, this));
}
void BrowserCloseManager::ConfirmCloseWithPendingDownloads(
int download_count,
const base::Callback<void(bool)>& callback) {
Browser* browser =
BrowserList::GetInstance(chrome::GetActiveDesktop())->GetLastActive();
DCHECK(browser);
browser->window()->ConfirmBrowserCloseWithPendingDownloads(
download_count,
Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
true,
callback);
}
void BrowserCloseManager::OnReportDownloadsCancellable(bool proceed) {
if (proceed) {
CloseBrowsers();
return;
}
CancelBrowserClose();
std::vector<Profile*> profiles(
g_browser_process->profile_manager()->GetLoadedProfiles());
for (std::vector<Profile*>::iterator it = profiles.begin();
it != profiles.end();
++it) {
DownloadService* download_service =
DownloadServiceFactory::GetForBrowserContext(*it);
if (download_service->NonMaliciousDownloadCount() > 0) {
chrome::ScopedTabbedBrowserDisplayer displayer(
*it, chrome::GetActiveDesktop());
chrome::ShowDownloads(displayer.browser());
}
}
}
void BrowserCloseManager::CloseBrowsers() {
#if defined(ENABLE_SESSION_SERVICE)
ProfileManager::ShutdownSessionServices();
#endif
if (!browser_shutdown::IsTryingToQuit()) {
BackgroundModeManager* background_mode_manager =
g_browser_process->background_mode_manager();
if (background_mode_manager)
background_mode_manager->SuspendBackgroundMode();
}
bool session_ending =
browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION;
for (scoped_ptr<chrome::BrowserIterator> it_ptr(
new chrome::BrowserIterator());
!it_ptr->done();) {
Browser* browser = **it_ptr;
browser->window()->Close();
if (!session_ending) {
it_ptr->Next();
} else {
while (browser->tab_strip_model()->count())
delete browser->tab_strip_model()->GetWebContentsAt(0);
browser->window()->DestroyBrowser();
it_ptr.reset(new chrome::BrowserIterator());
if (!it_ptr->done() && browser == **it_ptr) {
NOTREACHED();
return;
}
}
}
}