+void BrowserList::CloseAllBrowsersWithProfile(Profile* profile,
+ const base::Callback<void(const base::FilePath&)>& on_close_success) {
+ BrowserVector browsers_to_close;
+ for (chrome::BrowserIterator it; !it.done(); it.Next()) {
+ if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile())
+ browsers_to_close.push_back(*it);
+ }
+
+ TryToCloseBrowserList(browsers_to_close,
+ on_close_success,
+ profile->GetPath());
+}
+
+// static
+void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close,
+ const base::Callback<void(const base::FilePath&)>& on_close_success,
+ const base::FilePath& profile_path) {
+ for (BrowserVector::const_iterator it = browsers_to_close.begin();
+ it != browsers_to_close.end(); ++it) {
+ if ((*it)->CallBeforeUnloadHandlers(
+ base::Bind(&BrowserList::PostBeforeUnloadHandlers,
+ browsers_to_close,
+ on_close_success,
+ profile_path))) {
+ return;
+ }
+ }
+
+ on_close_success.Run(profile_path);
+
+ for (Browser* b : browsers_to_close) {
+ // BeforeUnload handlers may close browser windows, so we need to explicitly
+ // check whether they still exist.
+ if (b->window())
+ b->window()->Close();
+ }
+}
+
+// static
+void BrowserList::PostBeforeUnloadHandlers(
+ const BrowserVector& browsers_to_close,
+ const base::Callback<void(const base::FilePath&)>& on_close_success,
+ const base::FilePath& profile_path,
+ bool tab_close_confirmed) {
+ // We need this bool to avoid infinite recursion when resetting the
+ // BeforeUnload handlers, since doing that will trigger calls back to this
+ // method for each affected window.
+ static bool resetting_handlers = false;
+
+ if (tab_close_confirmed) {
+ TryToCloseBrowserList(browsers_to_close, on_close_success, profile_path);
+ } else if (!resetting_handlers) {
+ base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true);
+ for (BrowserVector::const_iterator it = browsers_to_close.begin();
+ it != browsers_to_close.end(); ++it) {
+ (*it)->ResetBeforeUnloadHandlers();
+ }
+ }
+}
+
+// static