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.
5 #include "chrome/browser/ui/chrome_pages.h"
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"
32 #include "chrome/browser/enumerate_modules_model_win.h"
35 #if defined(OS_CHROMEOS)
36 #include "chrome/browser/chromeos/genius_app/app_id.h"
37 #include "chromeos/chromeos_switches.h"
40 using base::UserMetricsAction
;
45 const char kHashMark
[] = "#";
47 void OpenBookmarkManagerWithHash(Browser
* browser
,
48 const std::string
& action
,
50 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
51 content::RecordAction(UserMetricsAction("ShowBookmarks"));
52 NavigateParams
params(GetSingletonTabNavigateParams(
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
);
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
);
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
71 void ShowHelpImpl(Browser
* browser
,
73 HostDesktopType host_desktop_type
,
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
);
82 AppLaunchParams(profile
, extension
, 0, host_desktop_type
));
88 case HELP_SOURCE_KEYBOARD
:
89 url
= GURL(kChromeHelpViaKeyboardURL
);
91 case HELP_SOURCE_MENU
:
92 url
= GURL(kChromeHelpViaMenuURL
);
94 case HELP_SOURCE_WEBUI
:
95 url
= GURL(kChromeHelpViaWebUIURL
);
98 NOTREACHED() << "Unhandled help source " << source
;
100 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
103 new ScopedTabbedBrowserDisplayer(profile
, host_desktop_type
));
104 browser
= displayer
->browser();
106 ShowSingletonTab(browser
, url
);
111 void ShowBookmarkManager(Browser
* browser
) {
112 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
113 content::RecordAction(UserMetricsAction("ShowBookmarks"));
114 ShowSingletonTabOverwritingNTP(
116 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIBookmarksURL
)));
119 void ShowBookmarkManagerForNode(Browser
* browser
, int64 node_id
) {
120 OpenBookmarkManagerWithHash(browser
, std::string(), node_id
);
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
);
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
);
139 ShowSingletonTabOverwritingNTP(
141 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIDownloadsURL
)));
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
);
157 ShowSingletonTabOverwritingNTP(browser
, params
);
160 void ShowConflicts(Browser
* browser
) {
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();
174 content::RecordAction(UserMetricsAction("AboutConflicts"));
175 ShowSingletonTab(browser
, GURL(kChromeUIConflictsURL
));
178 void ShowHelp(Browser
* browser
, HelpSource source
) {
179 ShowHelpImpl(browser
, browser
->profile(), browser
->host_desktop_type(),
183 void ShowHelpForProfile(Profile
* profile
,
184 HostDesktopType host_desktop_type
,
186 ShowHelpImpl(NULL
, profile
, host_desktop_type
, source
);
189 void ShowPolicy(Browser
* browser
) {
190 ShowSingletonTab(browser
, GURL(kChromeUIPolicyURL
));
193 void ShowSlow(Browser
* browser
) {
194 #if defined(OS_CHROMEOS)
195 ShowSingletonTab(browser
, GURL(kChromeUISlowURL
));
199 void ShowSettings(Browser
* browser
) {
200 content::RecordAction(UserMetricsAction("ShowOptions"));
201 ShowSettingsSubPage(browser
, std::string());
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
;
214 NavigateParams
params(GetSingletonTabNavigateParams(browser
, GURL(url
)));
215 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
216 ShowSingletonTabOverwritingNTP(browser
, params
);
219 void ShowContentSettings(Browser
* browser
,
220 ContentSettingsType content_settings_type
) {
223 kContentSettingsExceptionsSubPage
+ std::string(kHashMark
) +
224 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
225 content_settings_type
));
228 void ShowClearBrowsingDataDialog(Browser
* browser
) {
229 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
230 ShowSettingsSubPage(browser
, kClearBrowserDataSubPage
);
233 void ShowPasswordManager(Browser
* browser
) {
234 content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
235 ShowSettingsSubPage(browser
, kPasswordManagerSubPage
);
238 void ShowImportDialog(Browser
* browser
) {
239 content::RecordAction(UserMetricsAction("Import_ShowDlg"));
240 ShowSettingsSubPage(browser
, kImportDataSubPage
);
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
);
251 void ShowSearchEngineSettings(Browser
* browser
) {
252 content::RecordAction(UserMetricsAction("EditSearchEngines"));
253 ShowSettingsSubPage(browser
, kSearchEnginesSubPage
);
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
);
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();
275 NavigateToSingletonTab(browser
,
276 GURL(signin::GetPromoURL(source
, false)));
277 DCHECK_GT(browser
->tab_strip_model()->count(), 0);
281 } // namespace chrome