Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/ui/browser.cc

Issue 7466033: Fix warning prompting on closing a window that will cancel downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing trybot failures (chromeos specifically). Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/browser.h" 5 #include "chrome/browser/ui/browser.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #endif // OS_WIN 10 #endif // OS_WIN
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 #include "chrome/browser/instant/instant_controller.h" 51 #include "chrome/browser/instant/instant_controller.h"
52 #include "chrome/browser/instant/instant_unload_handler.h" 52 #include "chrome/browser/instant/instant_unload_handler.h"
53 #include "chrome/browser/net/browser_url_util.h" 53 #include "chrome/browser/net/browser_url_util.h"
54 #include "chrome/browser/net/url_fixer_upper.h" 54 #include "chrome/browser/net/url_fixer_upper.h"
55 #include "chrome/browser/notifications/notification_ui_manager.h" 55 #include "chrome/browser/notifications/notification_ui_manager.h"
56 #include "chrome/browser/platform_util.h" 56 #include "chrome/browser/platform_util.h"
57 #include "chrome/browser/prefs/incognito_mode_prefs.h" 57 #include "chrome/browser/prefs/incognito_mode_prefs.h"
58 #include "chrome/browser/prefs/pref_service.h" 58 #include "chrome/browser/prefs/pref_service.h"
59 #include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h" 59 #include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h"
60 #include "chrome/browser/profiles/profile.h" 60 #include "chrome/browser/profiles/profile.h"
61 #include "chrome/browser/profiles/profile_manager.h"
61 #include "chrome/browser/sessions/restore_tab_helper.h" 62 #include "chrome/browser/sessions/restore_tab_helper.h"
62 #include "chrome/browser/sessions/session_service.h" 63 #include "chrome/browser/sessions/session_service.h"
63 #include "chrome/browser/sessions/session_service_factory.h" 64 #include "chrome/browser/sessions/session_service_factory.h"
64 #include "chrome/browser/sessions/session_types.h" 65 #include "chrome/browser/sessions/session_types.h"
65 #include "chrome/browser/sessions/tab_restore_service.h" 66 #include "chrome/browser/sessions/tab_restore_service.h"
66 #include "chrome/browser/sessions/tab_restore_service_factory.h" 67 #include "chrome/browser/sessions/tab_restore_service_factory.h"
67 #include "chrome/browser/sync/profile_sync_service.h" 68 #include "chrome/browser/sync/profile_sync_service.h"
68 #include "chrome/browser/sync/sync_ui_util.h" 69 #include "chrome/browser/sync/sync_ui_util.h"
69 #include "chrome/browser/tab_closeable_state_watcher.h" 70 #include "chrome/browser/tab_closeable_state_watcher.h"
70 #include "chrome/browser/tab_contents/background_contents.h" 71 #include "chrome/browser/tab_contents/background_contents.h"
(...skipping 969 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 1041
1041 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to 1042 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
1042 // close again we'll show the warning again. 1043 // close again we'll show the warning again.
1043 cancel_download_confirmation_state_ = NOT_PROMPTED; 1044 cancel_download_confirmation_state_ = NOT_PROMPTED;
1044 1045
1045 // Show the download page so the user can figure-out what downloads are still 1046 // Show the download page so the user can figure-out what downloads are still
1046 // in-progress. 1047 // in-progress.
1047 ShowDownloadsTab(); 1048 ShowDownloadsTab();
1048 } 1049 }
1049 1050
1051 Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
1052 int* num_downloads_blocking) const {
1053 DCHECK(num_downloads_blocking);
1054
1055 if (is_attempting_to_close_browser_)
1056 return DOWNLOAD_CLOSE_OK;
1057
1058 // If we're not running a full browser process with a profile manager
1059 // (testing), it's ok to close the browser.
1060 if (!g_browser_process->profile_manager())
1061 return DOWNLOAD_CLOSE_OK;
1062
1063 int total_download_count =
1064 g_browser_process->profile_manager()->TotalDownloadCount();
1065 if (total_download_count == 0)
1066 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
1067
1068 // Let's figure out if we are the last window for our profile, and
1069 // the last window period.
1070 // Note that we cannot just use BrowserList::GetBrowserCount as browser
1071 // windows closing is delayed and the returned count might include windows
1072 // that are being closed.
1073 // The browser allowed to be closed only if both of the following are true:
1074 // 1. There are no downloads present for any profile or it is not the
1075 // last Browser.
1076 // 2. (Incognito Browser) There are no incognito downloads associated with
1077 // the Browser's profile or it is not the last Browser associated with
1078 // that profile.
1079 // Note the lack of parallelism: regular profiles are kept around until
1080 // browser close, whereas incognito profiles are destroyed on last
1081 // associated window close.
1082 int profile_window_count = 0;
1083 int total_window_count = 0;
1084 for (BrowserList::const_iterator iter = BrowserList::begin();
1085 iter != BrowserList::end(); ++iter) {
1086 // Don't count this browser window or any other in the process of closing.
1087 Browser* const browser = *iter;
1088 if (browser == this ||
1089 browser->is_attempting_to_close_browser_)
1090 continue;
1091
1092 if ((*iter)->profile() == profile())
1093 profile_window_count++;
1094 total_window_count++;
1095 }
1096
1097 // If there aren't any other windows, we're at browser shutdown.
1098 if (total_window_count == 0) {
1099 *num_downloads_blocking = total_download_count;
1100 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
1101 }
1102
1103 // If there aren't any other windows on our profile, we're an incognito
1104 // profile, and there are downloads associated with that profile,
1105 // indicate that.
1106 if (profile_window_count == 0 && profile()->DownloadCount() > 0 &&
1107 profile()->IsOffTheRecord()) {
1108 *num_downloads_blocking = profile()->DownloadCount();
1109 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
1110 }
1111
1112 // Those are the only conditions under which we will block shutdown.
1113 return DOWNLOAD_CLOSE_OK;
1114 }
1115
1050 //////////////////////////////////////////////////////////////////////////////// 1116 ////////////////////////////////////////////////////////////////////////////////
1051 // Browser, TabStripModel pass-thrus: 1117 // Browser, TabStripModel pass-thrus:
1052 1118
1053 int Browser::tab_count() const { 1119 int Browser::tab_count() const {
1054 return tab_handler_->GetTabStripModel()->count(); 1120 return tab_handler_->GetTabStripModel()->count();
1055 } 1121 }
1056 1122
1057 int Browser::active_index() const { 1123 int Browser::active_index() const {
1058 return tab_handler_->GetTabStripModel()->active_index(); 1124 return tab_handler_->GetTabStripModel()->active_index();
1059 } 1125 }
(...skipping 3433 matching lines...) Expand 10 before | Expand all | Expand 10 after
4493 MessageLoop::current()->PostTask( 4559 MessageLoop::current()->PostTask(
4494 FROM_HERE, 4560 FROM_HERE,
4495 method_factory_.NewRunnableMethod(&Browser::ProcessPendingTabs)); 4561 method_factory_.NewRunnableMethod(&Browser::ProcessPendingTabs));
4496 } 4562 }
4497 } 4563 }
4498 } 4564 }
4499 4565
4500 /////////////////////////////////////////////////////////////////////////////// 4566 ///////////////////////////////////////////////////////////////////////////////
4501 // Browser, In-progress download termination handling (private): 4567 // Browser, In-progress download termination handling (private):
4502 4568
4503 void Browser::CheckDownloadsInProgress(bool* normal_downloads_are_present,
4504 bool* incognito_downloads_are_present) {
4505 *normal_downloads_are_present = false;
4506 *incognito_downloads_are_present = false;
4507
4508 // If there are no download in-progress, our job is done.
4509 DownloadManager* download_manager = NULL;
4510 // But first we need to check for the existence of the download manager, as
4511 // GetDownloadManager() will unnecessarily try to create one if it does not
4512 // exist.
4513 if (profile()->HasCreatedDownloadManager())
4514 download_manager = profile()->GetDownloadManager();
4515 if (profile()->IsOffTheRecord()) {
4516 // Browser is incognito and so download_manager if present is for incognito
4517 // downloads.
4518 *incognito_downloads_are_present =
4519 (download_manager && download_manager->in_progress_count() != 0);
4520 // Check original profile.
4521 if (profile()->GetOriginalProfile()->HasCreatedDownloadManager())
4522 download_manager = profile()->GetOriginalProfile()->GetDownloadManager();
4523 }
4524
4525 *normal_downloads_are_present =
4526 (download_manager && download_manager->in_progress_count() != 0);
4527 }
4528
4529 bool Browser::CanCloseWithInProgressDownloads() { 4569 bool Browser::CanCloseWithInProgressDownloads() {
4530 if (cancel_download_confirmation_state_ != NOT_PROMPTED) { 4570 if (cancel_download_confirmation_state_ != NOT_PROMPTED) {
4531 if (cancel_download_confirmation_state_ == WAITING_FOR_RESPONSE) { 4571 if (cancel_download_confirmation_state_ == WAITING_FOR_RESPONSE) {
4532 // We need to hear from the user before we can close. 4572 // We need to hear from the user before we can close.
4533 return false; 4573 return false;
4534 } 4574 }
4535 // RESPONSE_RECEIVED case, the user decided to go along with the closing. 4575 // RESPONSE_RECEIVED case, the user decided to go along with the closing.
4536 return true; 4576 return true;
4537 } 4577 }
4538 // Indicated that normal (non-incognito) downloads are pending. 4578
4539 bool normal_downloads_are_present = false; 4579 int num_downloads_blocking;
4540 bool incognito_downloads_are_present = false; 4580 if (DOWNLOAD_CLOSE_OK ==
4541 CheckDownloadsInProgress(&normal_downloads_are_present, 4581 OkToCloseWithInProgressDownloads(&num_downloads_blocking))
4542 &incognito_downloads_are_present);
4543 if (!normal_downloads_are_present && !incognito_downloads_are_present)
4544 return true; 4582 return true;
4545 4583
4546 if (is_attempting_to_close_browser_) 4584 // Closing this window will kill some downloads; prompt to make sure
4547 return true; 4585 // that's ok.
4548
4549 if ((!normal_downloads_are_present && !profile()->IsOffTheRecord()) ||
4550 (!incognito_downloads_are_present && profile()->IsOffTheRecord()))
4551 return true;
4552
4553 // Let's figure out if we are the last window for our profile.
4554 // Note that we cannot just use BrowserList::GetBrowserCount as browser
4555 // windows closing is delayed and the returned count might include windows
4556 // that are being closed.
4557 // The browser allowed to be closed only if:
4558 // 1. It is a regular browser and there are no regular downloads present or
4559 // this is not the last regular browser window.
4560 // 2. It is an incognito browser and there are no incognito downloads present
4561 // or this is not the last incognito browser window.
4562 int count = 0;
4563 for (BrowserList::const_iterator iter = BrowserList::begin();
4564 iter != BrowserList::end(); ++iter) {
4565 // Don't count this browser window or any other in the process of closing.
4566 // Only consider tabbed browser windows, not popups.
4567 Browser* const browser = *iter;
4568 if (browser == this
4569 || browser->is_attempting_to_close_browser_
4570 || !browser->is_type_tabbed())
4571 continue;
4572
4573 // Verify that this is not the last non-incognito or incognito browser,
4574 // depending on the pending downloads.
4575 if (normal_downloads_are_present && !profile()->IsOffTheRecord() &&
4576 browser->profile()->IsOffTheRecord())
4577 continue;
4578 if (incognito_downloads_are_present && profile()->IsOffTheRecord() &&
4579 !browser->profile()->IsOffTheRecord())
4580 continue;
4581
4582 // We test the original profile, because an incognito browser window keeps
4583 // the original profile alive (and its DownloadManager).
4584 // We also need to test explicitly the profile directly so that 2 incognito
4585 // profiles count as a match.
4586 if ((*iter)->profile() == profile() ||
4587 (*iter)->profile()->GetOriginalProfile() == profile())
4588 count++;
4589 }
4590 if (count > 0)
4591 return true;
4592
4593 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE; 4586 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
4594 window_->ConfirmBrowserCloseWithPendingDownloads(); 4587 window_->ConfirmBrowserCloseWithPendingDownloads();
4595 4588
4596 // Return false so the browser does not close. We'll close if the user 4589 // Return false so the browser does not close. We'll close if the user
4597 // confirms in the dialog. 4590 // confirms in the dialog.
4598 return false; 4591 return false;
4599 } 4592 }
4600 4593
4601 /////////////////////////////////////////////////////////////////////////////// 4594 ///////////////////////////////////////////////////////////////////////////////
4602 // Browser, Assorted utility functions (private): 4595 // Browser, Assorted utility functions (private):
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
4885 } 4878 }
4886 4879
4887 void Browser::ShowSyncSetup() { 4880 void Browser::ShowSyncSetup() {
4888 ProfileSyncService* service = 4881 ProfileSyncService* service =
4889 profile()->GetOriginalProfile()->GetProfileSyncService(); 4882 profile()->GetOriginalProfile()->GetProfileSyncService();
4890 if (service->HasSyncSetupCompleted()) 4883 if (service->HasSyncSetupCompleted())
4891 ShowOptionsTab(chrome::kSyncSetupSubPage); 4884 ShowOptionsTab(chrome::kSyncSetupSubPage);
4892 else 4885 else
4893 service->ShowLoginDialog(); 4886 service->ShowLoginDialog();
4894 } 4887 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698