OLD | NEW |
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 |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/base_paths.h" | 15 #include "base/base_paths.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 20 #include "base/string_number_conversions.h" |
20 #include "base/string_util.h" | 21 #include "base/string_util.h" |
21 #include "base/string_number_conversions.h" | |
22 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
24 #include "base/time.h" | 24 #include "base/time.h" |
25 #include "base/utf_string_conversions.h" | 25 #include "base/utf_string_conversions.h" |
26 #include "chrome/app/chrome_command_ids.h" | 26 #include "chrome/app/chrome_command_ids.h" |
27 #include "chrome/browser/autofill/autofill_manager.h" | 27 #include "chrome/browser/autofill/autofill_manager.h" |
28 #include "chrome/browser/background/background_contents_service.h" | 28 #include "chrome/browser/background/background_contents_service.h" |
29 #include "chrome/browser/bookmarks/bookmark_model.h" | 29 #include "chrome/browser/bookmarks/bookmark_model.h" |
30 #include "chrome/browser/bookmarks/bookmark_utils.h" | 30 #include "chrome/browser/bookmarks/bookmark_utils.h" |
31 #include "chrome/browser/browser_process.h" | 31 #include "chrome/browser/browser_process.h" |
(...skipping 19 matching lines...) Expand all Loading... |
51 #include "chrome/browser/google/google_util.h" | 51 #include "chrome/browser/google/google_util.h" |
52 #include "chrome/browser/instant/instant_controller.h" | 52 #include "chrome/browser/instant/instant_controller.h" |
53 #include "chrome/browser/instant/instant_unload_handler.h" | 53 #include "chrome/browser/instant/instant_unload_handler.h" |
54 #include "chrome/browser/net/browser_url_util.h" | 54 #include "chrome/browser/net/browser_url_util.h" |
55 #include "chrome/browser/net/url_fixer_upper.h" | 55 #include "chrome/browser/net/url_fixer_upper.h" |
56 #include "chrome/browser/notifications/notification_ui_manager.h" | 56 #include "chrome/browser/notifications/notification_ui_manager.h" |
57 #include "chrome/browser/platform_util.h" | 57 #include "chrome/browser/platform_util.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 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1035 | 1036 |
1036 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to | 1037 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to |
1037 // close again we'll show the warning again. | 1038 // close again we'll show the warning again. |
1038 cancel_download_confirmation_state_ = NOT_PROMPTED; | 1039 cancel_download_confirmation_state_ = NOT_PROMPTED; |
1039 | 1040 |
1040 // Show the download page so the user can figure-out what downloads are still | 1041 // Show the download page so the user can figure-out what downloads are still |
1041 // in-progress. | 1042 // in-progress. |
1042 ShowDownloadsTab(); | 1043 ShowDownloadsTab(); |
1043 } | 1044 } |
1044 | 1045 |
| 1046 Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads( |
| 1047 int* num_downloads_blocking) const { |
| 1048 DCHECK(num_downloads_blocking); |
| 1049 |
| 1050 if (is_attempting_to_close_browser_) |
| 1051 return DOWNLOAD_CLOSE_OK; |
| 1052 |
| 1053 int total_download_count = |
| 1054 g_browser_process->profile_manager()->TotalDownloadCount(); |
| 1055 if (total_download_count == 0) |
| 1056 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close. |
| 1057 |
| 1058 // Let's figure out if we are the last tabbed window for our profile, and |
| 1059 // the last window period. |
| 1060 // Note that we cannot just use BrowserList::GetBrowserCount as browser |
| 1061 // windows closing is delayed and the returned count might include windows |
| 1062 // that are being closed. |
| 1063 // The browser allowed to be closed only if: |
| 1064 // 1. It is a regular browser and there are no regular downloads present or |
| 1065 // this is not the last regular browser window. |
| 1066 // 2. It is an incognito browser and there are no incognito downloads present |
| 1067 // for this incognito profile or this is not the last incognito |
| 1068 // browser window. |
| 1069 // Note the lack of parallelism: regular profiles are kept around until |
| 1070 // browser close, whereas incognito profiles are destroyed on last |
| 1071 // associated window close. |
| 1072 int profile_window_count = 0; |
| 1073 int total_window_count = 0; |
| 1074 for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 1075 iter != BrowserList::end(); ++iter) { |
| 1076 // Don't count this browser window or any other in the process of closing. |
| 1077 // Only consider tabbed browser windows, not popups. |
| 1078 Browser* const browser = *iter; |
| 1079 if (browser == this || |
| 1080 browser->is_attempting_to_close_browser_ || |
| 1081 !browser->is_type_tabbed()) |
| 1082 continue; |
| 1083 |
| 1084 if ((*iter)->profile() == profile()) |
| 1085 profile_window_count++; |
| 1086 total_window_count++; |
| 1087 } |
| 1088 |
| 1089 // If there aren't any other windows, we're at browser shutdown. |
| 1090 if (total_window_count == 0) { |
| 1091 *num_downloads_blocking = total_download_count; |
| 1092 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN; |
| 1093 } |
| 1094 |
| 1095 // If there aren't any other windows on our profile, we're an incognito |
| 1096 // profile, and there are downloads associated with that profile, |
| 1097 // indicate that. |
| 1098 if (profile_window_count == 0 && profile()->DownloadCount() > 0 && |
| 1099 profile()->IsOffTheRecord()) { |
| 1100 *num_downloads_blocking = profile()->DownloadCount(); |
| 1101 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE; |
| 1102 } |
| 1103 |
| 1104 // Those are the only conditions under which we will block shutdown. |
| 1105 return DOWNLOAD_CLOSE_OK; |
| 1106 } |
| 1107 |
1045 //////////////////////////////////////////////////////////////////////////////// | 1108 //////////////////////////////////////////////////////////////////////////////// |
1046 // Browser, TabStripModel pass-thrus: | 1109 // Browser, TabStripModel pass-thrus: |
1047 | 1110 |
1048 int Browser::tab_count() const { | 1111 int Browser::tab_count() const { |
1049 return tab_handler_->GetTabStripModel()->count(); | 1112 return tab_handler_->GetTabStripModel()->count(); |
1050 } | 1113 } |
1051 | 1114 |
1052 int Browser::active_index() const { | 1115 int Browser::active_index() const { |
1053 return tab_handler_->GetTabStripModel()->active_index(); | 1116 return tab_handler_->GetTabStripModel()->active_index(); |
1054 } | 1117 } |
(...skipping 3337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4392 MessageLoop::current()->PostTask( | 4455 MessageLoop::current()->PostTask( |
4393 FROM_HERE, | 4456 FROM_HERE, |
4394 method_factory_.NewRunnableMethod(&Browser::ProcessPendingTabs)); | 4457 method_factory_.NewRunnableMethod(&Browser::ProcessPendingTabs)); |
4395 } | 4458 } |
4396 } | 4459 } |
4397 } | 4460 } |
4398 | 4461 |
4399 /////////////////////////////////////////////////////////////////////////////// | 4462 /////////////////////////////////////////////////////////////////////////////// |
4400 // Browser, In-progress download termination handling (private): | 4463 // Browser, In-progress download termination handling (private): |
4401 | 4464 |
4402 void Browser::CheckDownloadsInProgress(bool* normal_downloads_are_present, | |
4403 bool* incognito_downloads_are_present) { | |
4404 *normal_downloads_are_present = false; | |
4405 *incognito_downloads_are_present = false; | |
4406 | |
4407 // If there are no download in-progress, our job is done. | |
4408 DownloadManager* download_manager = NULL; | |
4409 // But first we need to check for the existence of the download manager, as | |
4410 // GetDownloadManager() will unnecessarily try to create one if it does not | |
4411 // exist. | |
4412 if (profile()->HasCreatedDownloadManager()) | |
4413 download_manager = profile()->GetDownloadManager(); | |
4414 if (profile()->IsOffTheRecord()) { | |
4415 // Browser is incognito and so download_manager if present is for incognito | |
4416 // downloads. | |
4417 *incognito_downloads_are_present = | |
4418 (download_manager && download_manager->in_progress_count() != 0); | |
4419 // Check original profile. | |
4420 if (profile()->GetOriginalProfile()->HasCreatedDownloadManager()) | |
4421 download_manager = profile()->GetOriginalProfile()->GetDownloadManager(); | |
4422 } | |
4423 | |
4424 *normal_downloads_are_present = | |
4425 (download_manager && download_manager->in_progress_count() != 0); | |
4426 } | |
4427 | |
4428 bool Browser::CanCloseWithInProgressDownloads() { | 4465 bool Browser::CanCloseWithInProgressDownloads() { |
4429 if (cancel_download_confirmation_state_ != NOT_PROMPTED) { | 4466 if (cancel_download_confirmation_state_ != NOT_PROMPTED) { |
4430 if (cancel_download_confirmation_state_ == WAITING_FOR_RESPONSE) { | 4467 if (cancel_download_confirmation_state_ == WAITING_FOR_RESPONSE) { |
4431 // We need to hear from the user before we can close. | 4468 // We need to hear from the user before we can close. |
4432 return false; | 4469 return false; |
4433 } | 4470 } |
4434 // RESPONSE_RECEIVED case, the user decided to go along with the closing. | 4471 // RESPONSE_RECEIVED case, the user decided to go along with the closing. |
4435 return true; | 4472 return true; |
4436 } | 4473 } |
4437 // Indicated that normal (non-incognito) downloads are pending. | 4474 |
4438 bool normal_downloads_are_present = false; | 4475 int num_downloads_blocking; |
4439 bool incognito_downloads_are_present = false; | 4476 if (DOWNLOAD_CLOSE_OK == |
4440 CheckDownloadsInProgress(&normal_downloads_are_present, | 4477 OkToCloseWithInProgressDownloads(&num_downloads_blocking)) |
4441 &incognito_downloads_are_present); | |
4442 if (!normal_downloads_are_present && !incognito_downloads_are_present) | |
4443 return true; | 4478 return true; |
4444 | 4479 |
4445 if (is_attempting_to_close_browser_) | 4480 // Closing this window will kill some downloads; prompt to make sure |
4446 return true; | 4481 // that's ok. |
4447 | |
4448 if ((!normal_downloads_are_present && !profile()->IsOffTheRecord()) || | |
4449 (!incognito_downloads_are_present && profile()->IsOffTheRecord())) | |
4450 return true; | |
4451 | |
4452 // Let's figure out if we are the last window for our profile. | |
4453 // Note that we cannot just use BrowserList::GetBrowserCount as browser | |
4454 // windows closing is delayed and the returned count might include windows | |
4455 // that are being closed. | |
4456 // The browser allowed to be closed only if: | |
4457 // 1. It is a regular browser and there are no regular downloads present or | |
4458 // this is not the last regular browser window. | |
4459 // 2. It is an incognito browser and there are no incognito downloads present | |
4460 // or this is not the last incognito browser window. | |
4461 int count = 0; | |
4462 for (BrowserList::const_iterator iter = BrowserList::begin(); | |
4463 iter != BrowserList::end(); ++iter) { | |
4464 // Don't count this browser window or any other in the process of closing. | |
4465 // Only consider tabbed browser windows, not popups. | |
4466 Browser* const browser = *iter; | |
4467 if (browser == this | |
4468 || browser->is_attempting_to_close_browser_ | |
4469 || !browser->is_type_tabbed()) | |
4470 continue; | |
4471 | |
4472 // Verify that this is not the last non-incognito or incognito browser, | |
4473 // depending on the pending downloads. | |
4474 if (normal_downloads_are_present && !profile()->IsOffTheRecord() && | |
4475 browser->profile()->IsOffTheRecord()) | |
4476 continue; | |
4477 if (incognito_downloads_are_present && profile()->IsOffTheRecord() && | |
4478 !browser->profile()->IsOffTheRecord()) | |
4479 continue; | |
4480 | |
4481 // We test the original profile, because an incognito browser window keeps | |
4482 // the original profile alive (and its DownloadManager). | |
4483 // We also need to test explicitly the profile directly so that 2 incognito | |
4484 // profiles count as a match. | |
4485 if ((*iter)->profile() == profile() || | |
4486 (*iter)->profile()->GetOriginalProfile() == profile()) | |
4487 count++; | |
4488 } | |
4489 if (count > 0) | |
4490 return true; | |
4491 | |
4492 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE; | 4482 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE; |
4493 window_->ConfirmBrowserCloseWithPendingDownloads(); | 4483 window_->ConfirmBrowserCloseWithPendingDownloads(); |
4494 | 4484 |
4495 // Return false so the browser does not close. We'll close if the user | 4485 // Return false so the browser does not close. We'll close if the user |
4496 // confirms in the dialog. | 4486 // confirms in the dialog. |
4497 return false; | 4487 return false; |
4498 } | 4488 } |
4499 | 4489 |
4500 /////////////////////////////////////////////////////////////////////////////// | 4490 /////////////////////////////////////////////////////////////////////////////// |
4501 // Browser, Assorted utility functions (private): | 4491 // Browser, Assorted utility functions (private): |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4783 window_->BookmarkBarStateChanged(animate_type); | 4773 window_->BookmarkBarStateChanged(animate_type); |
4784 } | 4774 } |
4785 | 4775 |
4786 void Browser::ShowSyncSetup() { | 4776 void Browser::ShowSyncSetup() { |
4787 ProfileSyncService* service = profile()->GetProfileSyncService(); | 4777 ProfileSyncService* service = profile()->GetProfileSyncService(); |
4788 if (service->HasSyncSetupCompleted()) | 4778 if (service->HasSyncSetupCompleted()) |
4789 ShowOptionsTab(chrome::kSyncSetupSubPage); | 4779 ShowOptionsTab(chrome::kSyncSetupSubPage); |
4790 else | 4780 else |
4791 profile()->GetProfileSyncService()->ShowLoginDialog(); | 4781 profile()->GetProfileSyncService()->ShowLoginDialog(); |
4792 } | 4782 } |
OLD | NEW |