Home | History | Annotate | Download | only in ui
      1 // Copyright (c) 2014 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/settings_window_manager.h"
      6 
      7 #include "chrome/browser/profiles/profile.h"
      8 #include "chrome/browser/ui/browser_finder.h"
      9 #include "chrome/browser/ui/browser_finder.h"
     10 #include "chrome/browser/ui/browser_navigator.h"
     11 #include "chrome/browser/ui/browser_window.h"
     12 #include "chrome/browser/ui/chrome_pages.h"
     13 #include "chrome/browser/ui/settings_window_manager_observer.h"
     14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     15 #include "content/public/browser/user_metrics.h"
     16 #include "content/public/browser/web_contents.h"
     17 #include "url/gurl.h"
     18 
     19 namespace chrome {
     20 
     21 // static
     22 SettingsWindowManager* SettingsWindowManager::GetInstance() {
     23   return Singleton<SettingsWindowManager>::get();
     24 }
     25 
     26 void SettingsWindowManager::AddObserver(
     27     SettingsWindowManagerObserver* observer) {
     28   observers_.AddObserver(observer);
     29 }
     30 
     31 void SettingsWindowManager::RemoveObserver(
     32     SettingsWindowManagerObserver* observer) {
     33   observers_.RemoveObserver(observer);
     34 }
     35 
     36 void SettingsWindowManager::ShowChromePageForProfile(Profile* profile,
     37                                                      const GURL& gurl) {
     38   // Use the original (non off-the-record) profile for settings unless
     39   // this is a guest session.
     40   if (!profile->IsGuestSession() && profile->IsOffTheRecord())
     41     profile = profile->GetOriginalProfile();
     42 
     43   // Look for an existing browser window.
     44   Browser* browser = FindBrowserForProfile(profile);
     45   if (browser) {
     46     DCHECK(browser->profile() == profile);
     47     const content::WebContents* web_contents =
     48         browser->tab_strip_model()->GetWebContentsAt(0);
     49     if (web_contents && web_contents->GetURL() == gurl) {
     50       browser->window()->Show();
     51       return;
     52     }
     53     NavigateParams params(browser, gurl,
     54                           ui::PAGE_TRANSITION_AUTO_BOOKMARK);
     55     params.window_action = NavigateParams::SHOW_WINDOW;
     56     params.user_gesture = true;
     57     chrome::Navigate(&params);
     58     return;
     59   }
     60 
     61   // No existing browser window, create one.
     62   NavigateParams params(profile, gurl, ui::PAGE_TRANSITION_AUTO_BOOKMARK);
     63   params.disposition = NEW_POPUP;
     64   params.trusted_source = true;
     65   params.window_action = NavigateParams::SHOW_WINDOW;
     66   params.user_gesture = true;
     67   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
     68   chrome::Navigate(&params);
     69   settings_session_map_[profile] = params.browser->session_id().id();
     70   DCHECK(params.browser->is_trusted_source());
     71 
     72   FOR_EACH_OBSERVER(SettingsWindowManagerObserver,
     73                     observers_, OnNewSettingsWindow(params.browser));
     74 }
     75 
     76 Browser* SettingsWindowManager::FindBrowserForProfile(Profile* profile) {
     77   ProfileSessionMap::iterator iter = settings_session_map_.find(profile);
     78   if (iter != settings_session_map_.end())
     79     return chrome::FindBrowserWithID(iter->second);
     80   return NULL;
     81 }
     82 
     83 bool SettingsWindowManager::IsSettingsBrowser(Browser* browser) const {
     84   ProfileSessionMap::const_iterator iter =
     85       settings_session_map_.find(browser->profile());
     86   return (iter != settings_session_map_.end() &&
     87           iter->second == browser->session_id().id());
     88 }
     89 
     90 SettingsWindowManager::SettingsWindowManager() {
     91 }
     92 
     93 SettingsWindowManager::~SettingsWindowManager() {
     94 }
     95 
     96 }  // namespace chrome
     97