| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/browser_list_impl.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/browser_shutdown.h" |
| 11 #include "chrome/browser/lifetime/application_lifetime.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list_observer.h" |
| 15 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/browser/notification_service.h" |
| 19 |
| 20 // #include "build/build_config.h" |
| 21 // #include "chrome/browser/prefs/pref_service.h" |
| 22 |
| 23 |
| 24 #if defined(OS_CHROMEOS) |
| 25 #include "chrome/browser/chromeos/login/user_manager.h" |
| 26 #endif |
| 27 |
| 28 namespace chrome { |
| 29 |
| 30 // static |
| 31 BrowserListImpl* BrowserListImpl::native_instance_ = NULL; |
| 32 BrowserListImpl* BrowserListImpl::ash_instance_ = NULL; |
| 33 |
| 34 //////////////////////////////////////////////////////////////////////////////// |
| 35 // BrowserListImpl, public: |
| 36 |
| 37 // static |
| 38 BrowserListImpl* BrowserListImpl::GetInstance(HostDesktopType type) { |
| 39 BrowserListImpl** list = NULL; |
| 40 if (type == HOST_DESKTOP_TYPE_NATIVE) |
| 41 list = &native_instance_; |
| 42 else if (type == HOST_DESKTOP_TYPE_ASH) |
| 43 list = &ash_instance_; |
| 44 else |
| 45 NOTREACHED(); |
| 46 if (!*list) |
| 47 *list = new BrowserListImpl; |
| 48 return *list; |
| 49 } |
| 50 |
| 51 void BrowserListImpl::AddBrowser(Browser* browser) { |
| 52 DCHECK(browser); |
| 53 browsers_.push_back(browser); |
| 54 |
| 55 g_browser_process->AddRefModule(); |
| 56 |
| 57 content::NotificationService::current()->Notify( |
| 58 chrome::NOTIFICATION_BROWSER_OPENED, |
| 59 content::Source<Browser>(browser), |
| 60 content::NotificationService::NoDetails()); |
| 61 |
| 62 // Send out notifications after add has occurred. Do some basic checking to |
| 63 // try to catch evil observers that change the list from under us. |
| 64 size_t original_count = observers_.size(); |
| 65 FOR_EACH_OBSERVER(BrowserListObserver, observers_, OnBrowserAdded(browser)); |
| 66 DCHECK_EQ(original_count, observers_.size()) |
| 67 << "observer list modified during notification"; |
| 68 } |
| 69 |
| 70 void BrowserListImpl::RemoveBrowser(Browser* browser) { |
| 71 RemoveBrowserFrom(browser, &last_active_browsers_); |
| 72 |
| 73 // Many UI tests rely on closing the last browser window quitting the |
| 74 // application. |
| 75 // Mac: Closing all windows does not indicate quitting the application. Lie |
| 76 // for now and ignore behavior outside of unit tests. |
| 77 // ChromeOS: Force closing last window to close app with flag. |
| 78 // TODO(andybons | pkotwicz): Fix the UI tests to Do The Right Thing. |
| 79 #if defined(OS_CHROMEOS) |
| 80 bool closing_app; |
| 81 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 82 switches::kDisableZeroBrowsersOpenForTests)) |
| 83 closing_app = (browsers_.size() == 1); |
| 84 else |
| 85 closing_app = (browsers_.size() == 1 && |
| 86 browser_shutdown::IsTryingToQuit()); |
| 87 #else |
| 88 bool closing_app = (browsers_.size() == 1); |
| 89 #endif // OS_CHROMEOS |
| 90 |
| 91 content::NotificationService::current()->Notify( |
| 92 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 93 content::Source<Browser>(browser), |
| 94 content::Details<bool>(&closing_app)); |
| 95 |
| 96 RemoveBrowserFrom(browser, &browsers_); |
| 97 |
| 98 // Do some basic checking to try to catch evil observers |
| 99 // that change the list from under us. |
| 100 size_t original_count = observers_.size(); |
| 101 FOR_EACH_OBSERVER(BrowserListObserver, observers_, OnBrowserRemoved(browser)); |
| 102 DCHECK_EQ(original_count, observers_.size()) |
| 103 << "observer list modified during notification"; |
| 104 |
| 105 g_browser_process->ReleaseModule(); |
| 106 |
| 107 // If we're exiting, send out the APP_TERMINATING notification to allow other |
| 108 // modules to shut themselves down. |
| 109 if (browsers_.empty() && |
| 110 (browser_shutdown::IsTryingToQuit() || |
| 111 g_browser_process->IsShuttingDown())) { |
| 112 // Last browser has just closed, and this is a user-initiated quit or there |
| 113 // is no module keeping the app alive, so send out our notification. No need |
| 114 // to call ProfileManager::ShutdownSessionServices() as part of the |
| 115 // shutdown, because Browser::WindowClosing() already makes sure that the |
| 116 // SessionService is created and notified. |
| 117 browser::NotifyAppTerminating(); |
| 118 browser::OnAppExiting(); |
| 119 } |
| 120 } |
| 121 |
| 122 void BrowserListImpl::AddObserver(BrowserListObserver* observer) { |
| 123 observers_.AddObserver(observer); |
| 124 } |
| 125 |
| 126 void BrowserListImpl::RemoveObserver(BrowserListObserver* observer) { |
| 127 observers_.RemoveObserver(observer); |
| 128 } |
| 129 |
| 130 void BrowserListImpl::SetLastActive(Browser* browser) { |
| 131 // If the browser is currently trying to quit, we don't want to set the last |
| 132 // active browser because that can alter the last active browser that the user |
| 133 // intended depending on the order in which the windows close. |
| 134 if (browser_shutdown::IsTryingToQuit()) |
| 135 return; |
| 136 RemoveBrowserFrom(browser, &last_active_browsers_); |
| 137 last_active_browsers_.push_back(browser); |
| 138 |
| 139 FOR_EACH_OBSERVER(BrowserListObserver, observers_, |
| 140 OnBrowserSetLastActive(browser)); |
| 141 } |
| 142 |
| 143 Browser* BrowserListImpl::GetLastActive() { |
| 144 if (!last_active_browsers_.empty()) |
| 145 return *(last_active_browsers_.rbegin()); |
| 146 return NULL; |
| 147 } |
| 148 |
| 149 void BrowserListImpl::CloseAllBrowsersWithProfile(Profile* profile) { |
| 150 BrowserVector browsers_to_close; |
| 151 for (BrowserListImpl::const_iterator i = BrowserListImpl::begin(); |
| 152 i != BrowserListImpl::end(); ++i) { |
| 153 if ((*i)->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) |
| 154 browsers_to_close.push_back(*i); |
| 155 } |
| 156 |
| 157 for (BrowserVector::const_iterator i = browsers_to_close.begin(); |
| 158 i != browsers_to_close.end(); ++i) { |
| 159 (*i)->window()->Close(); |
| 160 } |
| 161 } |
| 162 |
| 163 bool BrowserListImpl::IsIncognitoWindowOpen() { |
| 164 for (BrowserListImpl::const_iterator i = BrowserListImpl::begin(); |
| 165 i != BrowserListImpl::end(); ++i) { |
| 166 if ((*i)->profile()->IsOffTheRecord()) |
| 167 return true; |
| 168 } |
| 169 return false; |
| 170 } |
| 171 |
| 172 bool BrowserListImpl::IsIncognitoWindowOpenForProfile(Profile* profile) { |
| 173 #if defined(OS_CHROMEOS) |
| 174 // In ChromeOS, we assume that the default profile is always valid, so if |
| 175 // we are in guest mode, keep the OTR profile active so it won't be deleted. |
| 176 if (chromeos::UserManager::Get()->IsLoggedInAsGuest()) |
| 177 return true; |
| 178 #endif |
| 179 for (BrowserListImpl::const_iterator i = BrowserListImpl::begin(); |
| 180 i != BrowserListImpl::end(); ++i) { |
| 181 if ((*i)->profile()->IsSameProfile(profile) && |
| 182 (*i)->profile()->IsOffTheRecord()) { |
| 183 return true; |
| 184 } |
| 185 } |
| 186 return false; |
| 187 } |
| 188 |
| 189 //////////////////////////////////////////////////////////////////////////////// |
| 190 // BrowserListImpl, private: |
| 191 |
| 192 BrowserListImpl::BrowserListImpl() { |
| 193 } |
| 194 |
| 195 BrowserListImpl::~BrowserListImpl() { |
| 196 } |
| 197 |
| 198 void BrowserListImpl::RemoveBrowserFrom(Browser* browser, |
| 199 BrowserVector* browser_list) { |
| 200 const iterator remove_browser = |
| 201 std::find(browser_list->begin(), browser_list->end(), browser); |
| 202 if (remove_browser != browser_list->end()) |
| 203 browser_list->erase(remove_browser); |
| 204 } |
| 205 |
| 206 } // namespace chrome |
| OLD | NEW |