Home | History | Annotate | Download | only in ui
      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/chrome_pages.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/logging.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/strings/stringprintf.h"
     11 #include "chrome/browser/download/download_shelf.h"
     12 #include "chrome/browser/extensions/extension_service.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/browser/profiles/profile_manager.h"
     15 #include "chrome/browser/signin/signin_manager.h"
     16 #include "chrome/browser/signin/signin_manager_factory.h"
     17 #include "chrome/browser/ui/browser.h"
     18 #include "chrome/browser/ui/browser_navigator.h"
     19 #include "chrome/browser/ui/browser_window.h"
     20 #include "chrome/browser/ui/extensions/application_launch.h"
     21 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
     22 #include "chrome/browser/ui/singleton_tabs.h"
     23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     24 #include "chrome/browser/ui/webui/options/content_settings_handler.h"
     25 #include "chrome/common/url_constants.h"
     26 #include "content/public/browser/user_metrics.h"
     27 #include "google_apis/gaia/gaia_urls.h"
     28 #include "net/base/url_util.h"
     29 #include "url/gurl.h"
     30 
     31 #if defined(OS_WIN)
     32 #include "chrome/browser/enumerate_modules_model_win.h"
     33 #endif
     34 
     35 #if defined(OS_CHROMEOS)
     36 #include "chrome/browser/chromeos/genius_app/app_id.h"
     37 #include "chromeos/chromeos_switches.h"
     38 #endif
     39 
     40 using content::UserMetricsAction;
     41 
     42 namespace chrome {
     43 namespace {
     44 
     45 const char kHashMark[] = "#";
     46 
     47 void OpenBookmarkManagerWithHash(Browser* browser,
     48                                  const std::string& action,
     49                                  int64 node_id) {
     50   content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
     51   content::RecordAction(UserMetricsAction("ShowBookmarks"));
     52   NavigateParams params(GetSingletonTabNavigateParams(
     53       browser,
     54       GURL(kChromeUIBookmarksURL).Resolve(
     55           base::StringPrintf("/#%s%s", action.c_str(),
     56               base::Int64ToString(node_id).c_str()))));
     57   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
     58   ShowSingletonTabOverwritingNTP(browser, params);
     59 }
     60 
     61 void NavigateToSingletonTab(Browser* browser, const GURL& url) {
     62   NavigateParams params(GetSingletonTabNavigateParams(browser, url));
     63   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
     64   ShowSingletonTabOverwritingNTP(browser, params);
     65 }
     66 
     67 // Shows either the help app or the appropriate help page for |source|. If
     68 // |browser| is NULL and the help page is used (vs the app), the help page is
     69 // shown in the last active browser. If there is no such browser, a new browser
     70 // is created.
     71 void ShowHelpImpl(Browser* browser,
     72                   Profile* profile,
     73                   HostDesktopType host_desktop_type,
     74                   HelpSource source) {
     75   content::RecordAction(UserMetricsAction("ShowHelpTab"));
     76 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
     77   const CommandLine* command_line = CommandLine::ForCurrentProcess();
     78   if (!command_line->HasSwitch(chromeos::switches::kDisableGeniusApp)) {
     79     const extensions::Extension* extension = profile->GetExtensionService()->
     80         GetInstalledExtension(genius_app::kGeniusAppId);
     81     OpenApplication(
     82         AppLaunchParams(profile, extension, 0, host_desktop_type));
     83     return;
     84   }
     85 #endif
     86   GURL url;
     87   switch (source) {
     88     case HELP_SOURCE_KEYBOARD:
     89       url = GURL(kChromeHelpViaKeyboardURL);
     90       break;
     91     case HELP_SOURCE_MENU:
     92       url = GURL(kChromeHelpViaMenuURL);
     93       break;
     94     case HELP_SOURCE_WEBUI:
     95       url = GURL(kChromeHelpViaWebUIURL);
     96       break;
     97     default:
     98       NOTREACHED() << "Unhandled help source " << source;
     99   }
    100   scoped_ptr<ScopedTabbedBrowserDisplayer> displayer;
    101   if (!browser) {
    102     displayer.reset(
    103         new ScopedTabbedBrowserDisplayer(profile, host_desktop_type));
    104     browser = displayer->browser();
    105   }
    106   ShowSingletonTab(browser, url);
    107 }
    108 
    109 }  // namespace
    110 
    111 void ShowBookmarkManager(Browser* browser) {
    112   content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
    113   content::RecordAction(UserMetricsAction("ShowBookmarks"));
    114   ShowSingletonTabOverwritingNTP(
    115       browser,
    116       GetSingletonTabNavigateParams(browser, GURL(kChromeUIBookmarksURL)));
    117 }
    118 
    119 void ShowBookmarkManagerForNode(Browser* browser, int64 node_id) {
    120   OpenBookmarkManagerWithHash(browser, std::string(), node_id);
    121 }
    122 
    123 void ShowHistory(Browser* browser) {
    124   content::RecordAction(UserMetricsAction("ShowHistory"));
    125   NavigateParams params(
    126       GetSingletonTabNavigateParams(browser, GURL(kChromeUIHistoryURL)));
    127   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
    128   ShowSingletonTabOverwritingNTP(browser, params);
    129 }
    130 
    131 void ShowDownloads(Browser* browser) {
    132   content::RecordAction(UserMetricsAction("ShowDownloads"));
    133   if (browser->window()) {
    134     DownloadShelf* shelf = browser->window()->GetDownloadShelf();
    135     // The downloads page is always shown in response to a user action.
    136     if (shelf->IsShowing())
    137       shelf->Close(DownloadShelf::USER_ACTION);
    138   }
    139   ShowSingletonTabOverwritingNTP(
    140       browser,
    141       GetSingletonTabNavigateParams(browser, GURL(kChromeUIDownloadsURL)));
    142 }
    143 
    144 void ShowExtensions(Browser* browser,
    145                     const std::string& extension_to_highlight) {
    146   content::RecordAction(UserMetricsAction("ShowExtensions"));
    147   NavigateParams params(
    148       GetSingletonTabNavigateParams(browser, GURL(kChromeUIExtensionsURL)));
    149   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
    150   if (!extension_to_highlight.empty()) {
    151     GURL::Replacements replacements;
    152     std::string query("id=");
    153     query += extension_to_highlight;
    154     replacements.SetQueryStr(query);
    155     params.url = params.url.ReplaceComponents(replacements);
    156   }
    157   ShowSingletonTabOverwritingNTP(browser, params);
    158 }
    159 
    160 void ShowConflicts(Browser* browser) {
    161 #if defined(OS_WIN)
    162   EnumerateModulesModel* model = EnumerateModulesModel::GetInstance();
    163   if (model->modules_to_notify_about() > 0) {
    164     GURL help_center_url = model->GetFirstNotableConflict();
    165     if (help_center_url.is_valid()) {
    166       EnumerateModulesModel::RecordLearnMoreStat(true);
    167       ShowSingletonTab(browser, help_center_url);
    168       model->AcknowledgeConflictNotification();
    169       return;
    170     }
    171   }
    172 #endif
    173 
    174   content::RecordAction(UserMetricsAction("AboutConflicts"));
    175   ShowSingletonTab(browser, GURL(kChromeUIConflictsURL));
    176 }
    177 
    178 void ShowHelp(Browser* browser, HelpSource source) {
    179   ShowHelpImpl(browser, browser->profile(), browser->host_desktop_type(),
    180       source);
    181 }
    182 
    183 void ShowHelpForProfile(Profile* profile,
    184                         HostDesktopType host_desktop_type,
    185                         HelpSource source) {
    186   ShowHelpImpl(NULL, profile, host_desktop_type, source);
    187 }
    188 
    189 void ShowPolicy(Browser* browser) {
    190   ShowSingletonTab(browser, GURL(kChromeUIPolicyURL));
    191 }
    192 
    193 void ShowSlow(Browser* browser) {
    194 #if defined(OS_CHROMEOS)
    195   ShowSingletonTab(browser, GURL(kChromeUISlowURL));
    196 #endif
    197 }
    198 
    199 void ShowSettings(Browser* browser) {
    200   content::RecordAction(UserMetricsAction("ShowOptions"));
    201   ShowSettingsSubPage(browser, std::string());
    202 }
    203 
    204 void ShowSettingsSubPage(Browser* browser, const std::string& sub_page) {
    205   std::string url = std::string(kChromeUISettingsURL) + sub_page;
    206 #if defined(OS_CHROMEOS)
    207   if (sub_page.find(kInternetOptionsSubPage, 0) != std::string::npos) {
    208     std::string::size_type loc = sub_page.find("?", 0);
    209     std::string network_page = loc != std::string::npos ?
    210         sub_page.substr(loc) : std::string();
    211     url = std::string(kChromeUISettingsURL) + network_page;
    212   }
    213 #endif
    214   NavigateParams params(GetSingletonTabNavigateParams(browser, GURL(url)));
    215   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
    216   ShowSingletonTabOverwritingNTP(browser, params);
    217 }
    218 
    219 void ShowContentSettings(Browser* browser,
    220                          ContentSettingsType content_settings_type) {
    221   ShowSettingsSubPage(
    222       browser,
    223       kContentSettingsExceptionsSubPage + std::string(kHashMark) +
    224       options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
    225           content_settings_type));
    226 }
    227 
    228 void ShowClearBrowsingDataDialog(Browser* browser) {
    229   content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
    230   ShowSettingsSubPage(browser, kClearBrowserDataSubPage);
    231 }
    232 
    233 void ShowPasswordManager(Browser* browser) {
    234   content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
    235   ShowSettingsSubPage(browser, kPasswordManagerSubPage);
    236 }
    237 
    238 void ShowImportDialog(Browser* browser) {
    239   content::RecordAction(UserMetricsAction("Import_ShowDlg"));
    240   ShowSettingsSubPage(browser, kImportDataSubPage);
    241 }
    242 
    243 void ShowAboutChrome(Browser* browser) {
    244   content::RecordAction(UserMetricsAction("AboutChrome"));
    245   NavigateParams params(
    246       GetSingletonTabNavigateParams(browser, GURL(kChromeUIUberURL)));
    247   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
    248   ShowSingletonTabOverwritingNTP(browser, params);
    249 }
    250 
    251 void ShowSearchEngineSettings(Browser* browser) {
    252   content::RecordAction(UserMetricsAction("EditSearchEngines"));
    253   ShowSettingsSubPage(browser, kSearchEnginesSubPage);
    254 }
    255 
    256 void ShowBrowserSignin(Browser* browser, signin::Source source) {
    257   Profile* original_profile = browser->profile()->GetOriginalProfile();
    258   SigninManagerBase* manager =
    259       SigninManagerFactory::GetForProfile(original_profile);
    260   DCHECK(manager->IsSigninAllowed());
    261   // If we're signed in, just show settings.
    262   if (!manager->GetAuthenticatedUsername().empty()) {
    263     ShowSettings(browser);
    264   } else {
    265     // If the browser's profile is an incognito profile, make sure to use
    266     // a browser window from the original profile.  The user cannot sign in
    267     // from an incognito window.
    268     scoped_ptr<ScopedTabbedBrowserDisplayer> displayer;
    269     if (browser->profile()->IsOffTheRecord()) {
    270       displayer.reset(new ScopedTabbedBrowserDisplayer(
    271           original_profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
    272       browser = displayer->browser();
    273     }
    274 
    275     NavigateToSingletonTab(browser,
    276                            GURL(signin::GetPromoURL(source, false)));
    277     DCHECK_GT(browser->tab_strip_model()->count(), 0);
    278   }
    279 }
    280 
    281 void ShowGaiaSignin(Browser* browser,
    282                     const std::string& service,
    283                     const GURL& continue_url) {
    284   GURL url(GaiaUrls::GetInstance()->service_login_url());
    285   url = net::AppendQueryParameter(url, "service", service);
    286   if (continue_url.is_valid())
    287     url = net::AppendQueryParameter(url, "continue", continue_url.spec());
    288   NavigateToSingletonTab(browser, url);
    289 }
    290 
    291 }  // namespace chrome
    292