OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/lifetime/browser_close_manager.h" | 5 #include "chrome/browser/lifetime/browser_close_manager.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 #include <iterator> |
| 9 #include <vector> |
| 10 |
7 #include "build/build_config.h" | 11 #include "build/build_config.h" |
8 #include "chrome/browser/background/background_mode_manager.h" | 12 #include "chrome/browser/background/background_mode_manager.h" |
9 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/browser_shutdown.h" | 14 #include "chrome/browser/browser_shutdown.h" |
11 #include "chrome/browser/download/download_service.h" | 15 #include "chrome/browser/download/download_service.h" |
12 #include "chrome/browser/download/download_service_factory.h" | 16 #include "chrome/browser/download/download_service_factory.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_iterator.h" | |
16 #include "chrome/browser/ui/browser_list.h" | 19 #include "chrome/browser/ui/browser_list.h" |
17 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
18 #include "chrome/browser/ui/chrome_pages.h" | 21 #include "chrome/browser/ui/chrome_pages.h" |
19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 22 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 // Navigates a browser window for |profile|, creating one if necessary, to the | 28 // Navigates a browser window for |profile|, creating one if necessary, to the |
(...skipping 23 matching lines...) Expand all Loading... |
49 // Tell everyone that we are shutting down. | 52 // Tell everyone that we are shutting down. |
50 browser_shutdown::SetTryingToQuit(true); | 53 browser_shutdown::SetTryingToQuit(true); |
51 CloseBrowsers(); | 54 CloseBrowsers(); |
52 return; | 55 return; |
53 } | 56 } |
54 TryToCloseBrowsers(); | 57 TryToCloseBrowsers(); |
55 } | 58 } |
56 | 59 |
57 void BrowserCloseManager::CancelBrowserClose() { | 60 void BrowserCloseManager::CancelBrowserClose() { |
58 browser_shutdown::SetTryingToQuit(false); | 61 browser_shutdown::SetTryingToQuit(false); |
59 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 62 for (auto* browser : *BrowserList::GetInstance()) { |
60 it->ResetBeforeUnloadHandlers(); | 63 browser->ResetBeforeUnloadHandlers(); |
61 } | 64 } |
62 } | 65 } |
63 | 66 |
64 void BrowserCloseManager::TryToCloseBrowsers() { | 67 void BrowserCloseManager::TryToCloseBrowsers() { |
65 // If all browser windows can immediately be closed, fall out of this loop and | 68 // If all browser windows can immediately be closed, fall out of this loop and |
66 // close the browsers. If any browser window cannot be closed, temporarily | 69 // close the browsers. If any browser window cannot be closed, temporarily |
67 // stop closing. CallBeforeUnloadHandlers prompts the user and calls | 70 // stop closing. CallBeforeUnloadHandlers prompts the user and calls |
68 // OnBrowserReportCloseable with the result. If the user confirms the close, | 71 // OnBrowserReportCloseable with the result. If the user confirms the close, |
69 // this will trigger TryToCloseBrowsers to try again. | 72 // this will trigger TryToCloseBrowsers to try again. |
70 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 73 for (auto* browser : *BrowserList::GetInstance()) { |
71 if (it->CallBeforeUnloadHandlers( | 74 if (browser->CallBeforeUnloadHandlers( |
72 base::Bind(&BrowserCloseManager::OnBrowserReportCloseable, this))) { | 75 base::Bind(&BrowserCloseManager::OnBrowserReportCloseable, this))) { |
73 current_browser_ = *it; | 76 current_browser_ = browser; |
74 return; | 77 return; |
75 } | 78 } |
76 } | 79 } |
77 CheckForDownloadsInProgress(); | 80 CheckForDownloadsInProgress(); |
78 } | 81 } |
79 | 82 |
80 void BrowserCloseManager::OnBrowserReportCloseable(bool proceed) { | 83 void BrowserCloseManager::OnBrowserReportCloseable(bool proceed) { |
81 if (!current_browser_) | 84 if (!current_browser_) |
82 return; | 85 return; |
83 | 86 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // exit can restore all browsers open before exiting. | 146 // exit can restore all browsers open before exiting. |
144 ProfileManager::ShutdownSessionServices(); | 147 ProfileManager::ShutdownSessionServices(); |
145 #endif | 148 #endif |
146 if (!browser_shutdown::IsTryingToQuit()) { | 149 if (!browser_shutdown::IsTryingToQuit()) { |
147 BackgroundModeManager* background_mode_manager = | 150 BackgroundModeManager* background_mode_manager = |
148 g_browser_process->background_mode_manager(); | 151 g_browser_process->background_mode_manager(); |
149 if (background_mode_manager) | 152 if (background_mode_manager) |
150 background_mode_manager->SuspendBackgroundMode(); | 153 background_mode_manager->SuspendBackgroundMode(); |
151 } | 154 } |
152 | 155 |
| 156 // Make a copy of the BrowserList to simplify the case where we need to |
| 157 // destroy a Browser during the loop. |
| 158 std::vector<Browser*> browser_list_copy; |
| 159 std::copy(BrowserList::GetInstance()->begin(), |
| 160 BrowserList::GetInstance()->end(), |
| 161 std::back_inserter(browser_list_copy)); |
| 162 |
153 bool session_ending = | 163 bool session_ending = |
154 browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION; | 164 browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION; |
155 for (scoped_ptr<chrome::BrowserIterator> it_ptr( | 165 |
156 new chrome::BrowserIterator()); | 166 for (auto* browser : browser_list_copy) { |
157 !it_ptr->done();) { | |
158 Browser* browser = **it_ptr; | |
159 browser->window()->Close(); | 167 browser->window()->Close(); |
160 if (!session_ending) { | 168 if (session_ending) { |
161 it_ptr->Next(); | |
162 } else { | |
163 // This path is hit during logoff/power-down. In this case we won't get | 169 // This path is hit during logoff/power-down. In this case we won't get |
164 // a final message and so we force the browser to be deleted. | 170 // a final message and so we force the browser to be deleted. |
165 // Close doesn't immediately destroy the browser | 171 // Close doesn't immediately destroy the browser |
166 // (Browser::TabStripEmpty() uses invoke later) but when we're ending the | 172 // (Browser::TabStripEmpty() uses invoke later) but when we're ending the |
167 // session we need to make sure the browser is destroyed now. So, invoke | 173 // session we need to make sure the browser is destroyed now. So, invoke |
168 // DestroyBrowser to make sure the browser is deleted and cleanup can | 174 // DestroyBrowser to make sure the browser is deleted and cleanup can |
169 // happen. | 175 // happen. |
170 while (browser->tab_strip_model()->count()) | 176 while (browser->tab_strip_model()->count()) |
171 delete browser->tab_strip_model()->GetWebContentsAt(0); | 177 delete browser->tab_strip_model()->GetWebContentsAt(0); |
172 browser->window()->DestroyBrowser(); | 178 browser->window()->DestroyBrowser(); |
173 it_ptr.reset(new chrome::BrowserIterator()); | 179 // Destroying the browser should have removed it from the browser list. |
174 if (!it_ptr->done() && browser == **it_ptr) { | 180 DCHECK(BrowserList::GetInstance()->end() == |
175 // Destroying the browser should have removed it from the browser list. | 181 std::find(BrowserList::GetInstance()->begin(), |
176 // We should never get here. | 182 BrowserList::GetInstance()->end(), browser)); |
177 NOTREACHED(); | |
178 return; | |
179 } | |
180 } | 183 } |
181 } | 184 } |
182 } | 185 } |
OLD | NEW |