// static
void BrowserList::CloseAllBrowsers()
{
    bool session_ending =
        browser_shutdown::GetShutdownType() == browser_shutdown::END_SESSION;
    bool force_exit = false;
    // Tell everyone that we are shutting down.
    browser_shutdown::SetTryingToQuit(true);

    // Before we close the browsers shutdown all session services. That way an
    // exit can restore all browsers open before exiting.
    //ProfileManager::ShutdownSessionServices();

    // If there are no browsers, send the APP_TERMINATING action here. Otherwise,
    // it will be sent by RemoveBrowser() when the last browser has closed.
    if(force_exit || browsers_.empty())
    {
        NotifyAndTerminate(true);
        return;
    }
    for(BrowserList::const_iterator i=BrowserList::begin();
        i!=BrowserList::end();)
    {
        Browser* browser = *i;
        browser->window()->Close();
        if(!session_ending)
        {
            ++i;
        }
        else
        {
            // This path is hit during logoff/power-down. In this case we won't get
            // a final message and so we force the browser to be deleted.
            // Close doesn't immediately destroy the browser
            // (Browser::TabStripEmpty() uses invoke later) but when we're ending the
            // session we need to make sure the browser is destroyed now. So, invoke
            // DestroyBrowser to make sure the browser is deleted and cleanup can
            // happen.
            while(browser->tab_count())
            {
                delete browser->GetTabContentsWrapperAt(0);
            }
            browser->window()->DestroyBrowser();
            i = BrowserList::begin();
            if(i!=BrowserList::end() && browser==*i)
            {
                // Destroying the browser should have removed it from the browser list.
                // We should never get here.
                NOTREACHED();
                return;
            }
        }
    }
}