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_factory.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_finder.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/settings_window_manager.h"
23 #include "chrome/browser/ui/singleton_tabs.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/browser/ui/webui/options/content_settings_handler.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/url_constants.h"
28 #include "components/signin/core/browser/signin_manager.h"
29 #include "content/public/browser/user_metrics.h"
30 #include "content/public/browser/web_contents.h"
31 #include "google_apis/gaia/gaia_urls.h"
32 #include "net/base/url_util.h"
35 #include "chrome/browser/enumerate_modules_model_win.h"
38 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/chromeos/genius_app/app_id.h"
40 #include "chromeos/chromeos_switches.h"
43 using base::UserMetricsAction
;
48 const char kHashMark
[] = "#";
50 void OpenBookmarkManagerWithHash(Browser
* browser
,
51 const std::string
& action
,
53 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
54 content::RecordAction(UserMetricsAction("ShowBookmarks"));
55 NavigateParams
params(GetSingletonTabNavigateParams(
57 GURL(kChromeUIBookmarksURL
).Resolve(base::StringPrintf(
58 "/#%s%s", action
.c_str(), base::Int64ToString(node_id
).c_str()))));
59 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
60 ShowSingletonTabOverwritingNTP(browser
, params
);
63 void NavigateToSingletonTab(Browser
* browser
, const GURL
& url
) {
64 NavigateParams
params(GetSingletonTabNavigateParams(browser
, url
));
65 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
66 ShowSingletonTabOverwritingNTP(browser
, params
);
69 // Shows either the help app or the appropriate help page for |source|. If
70 // |browser| is NULL and the help page is used (vs the app), the help page is
71 // shown in the last active browser. If there is no such browser, a new browser
73 void ShowHelpImpl(Browser
* browser
,
75 HostDesktopType host_desktop_type
,
77 content::RecordAction(UserMetricsAction("ShowHelpTab"));
78 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
79 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
80 if (!command_line
->HasSwitch(chromeos::switches::kDisableGeniusApp
)) {
81 const extensions::Extension
* extension
=
82 profile
->GetExtensionService()->GetInstalledExtension(
83 genius_app::kGeniusAppId
);
84 OpenApplication(AppLaunchParams(profile
, extension
, 0, host_desktop_type
));
90 case HELP_SOURCE_KEYBOARD
:
91 url
= GURL(kChromeHelpViaKeyboardURL
);
93 case HELP_SOURCE_MENU
:
94 url
= GURL(kChromeHelpViaMenuURL
);
96 case HELP_SOURCE_WEBUI
:
97 url
= GURL(kChromeHelpViaWebUIURL
);
100 NOTREACHED() << "Unhandled help source " << source
;
102 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
105 new ScopedTabbedBrowserDisplayer(profile
, host_desktop_type
));
106 browser
= displayer
->browser();
108 ShowSingletonTab(browser
, url
);
111 bool SettingsWindowEnabled() {
112 return CommandLine::ForCurrentProcess()->HasSwitch(
113 ::switches::kEnableSettingsWindow
);
118 void ShowBookmarkManager(Browser
* browser
) {
119 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
120 content::RecordAction(UserMetricsAction("ShowBookmarks"));
121 ShowSingletonTabOverwritingNTP(
123 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIBookmarksURL
)));
126 void ShowBookmarkManagerForNode(Browser
* browser
, int64 node_id
) {
127 OpenBookmarkManagerWithHash(browser
, std::string(), node_id
);
130 void ShowHistory(Browser
* browser
) {
131 content::RecordAction(UserMetricsAction("ShowHistory"));
132 if (SettingsWindowEnabled()) {
133 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
134 browser
->profile(), GURL(kChromeUIHistoryURL
));
137 NavigateParams
params(
138 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIHistoryURL
)));
139 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
140 ShowSingletonTabOverwritingNTP(browser
, params
);
143 void ShowDownloads(Browser
* browser
) {
144 content::RecordAction(UserMetricsAction("ShowDownloads"));
145 if (browser
->window()) {
146 DownloadShelf
* shelf
= browser
->window()->GetDownloadShelf();
147 // The downloads page is always shown in response to a user action.
148 if (shelf
->IsShowing())
149 shelf
->Close(DownloadShelf::USER_ACTION
);
151 ShowSingletonTabOverwritingNTP(
153 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIDownloadsURL
)));
156 void ShowExtensions(Browser
* browser
,
157 const std::string
& extension_to_highlight
) {
158 content::RecordAction(UserMetricsAction("ShowExtensions"));
159 if (SettingsWindowEnabled()) {
160 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
161 browser
->profile(), GURL(kChromeUIExtensionsURL
));
164 NavigateParams
params(
165 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIExtensionsURL
)));
166 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
167 if (!extension_to_highlight
.empty()) {
168 GURL::Replacements replacements
;
169 std::string
query("id=");
170 query
+= extension_to_highlight
;
171 replacements
.SetQueryStr(query
);
172 params
.url
= params
.url
.ReplaceComponents(replacements
);
174 ShowSingletonTabOverwritingNTP(browser
, params
);
177 void ShowConflicts(Browser
* browser
) {
179 EnumerateModulesModel
* model
= EnumerateModulesModel::GetInstance();
180 if (model
->modules_to_notify_about() > 0) {
181 GURL help_center_url
= model
->GetFirstNotableConflict();
182 if (help_center_url
.is_valid()) {
183 EnumerateModulesModel::RecordLearnMoreStat(true);
184 ShowSingletonTab(browser
, help_center_url
);
185 model
->AcknowledgeConflictNotification();
191 content::RecordAction(UserMetricsAction("AboutConflicts"));
192 ShowSingletonTab(browser
, GURL(kChromeUIConflictsURL
));
195 void ShowHelp(Browser
* browser
, HelpSource source
) {
197 browser
, browser
->profile(), browser
->host_desktop_type(), source
);
200 void ShowHelpForProfile(Profile
* profile
,
201 HostDesktopType host_desktop_type
,
203 ShowHelpImpl(NULL
, profile
, host_desktop_type
, source
);
206 void ShowPolicy(Browser
* browser
) {
207 ShowSingletonTab(browser
, GURL(kChromeUIPolicyURL
));
210 void ShowSlow(Browser
* browser
) {
211 #if defined(OS_CHROMEOS)
212 ShowSingletonTab(browser
, GURL(kChromeUISlowURL
));
216 GURL
GetSettingsUrl(const std::string
& sub_page
) {
217 std::string url
= std::string(kChromeUISettingsURL
) + sub_page
;
218 #if defined(OS_CHROMEOS)
219 if (sub_page
.find(kInternetOptionsSubPage
, 0) != std::string::npos
) {
220 std::string::size_type loc
= sub_page
.find("?", 0);
221 std::string network_page
=
222 loc
!= std::string::npos
? sub_page
.substr(loc
) : std::string();
223 url
= std::string(kChromeUISettingsURL
) + network_page
;
229 bool IsTrustedPopupWindowWithScheme(const Browser
* browser
,
230 const std::string
& scheme
) {
231 if (!browser
->is_type_popup() || !browser
->is_trusted_source())
233 if (scheme
.empty()) // Any trusted popup window
235 const content::WebContents
* web_contents
=
236 browser
->tab_strip_model()->GetWebContentsAt(0);
239 GURL
url(web_contents
->GetURL());
240 return url
.SchemeIs(scheme
.c_str());
243 void ShowSettings(Browser
* browser
) {
244 ShowSettingsSubPage(browser
, std::string());
247 void ShowSettingsSubPage(Browser
* browser
, const std::string
& sub_page
) {
248 if (SettingsWindowEnabled()) {
249 ShowSettingsSubPageForProfile(browser
->profile(), sub_page
);
252 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
255 void ShowSettingsSubPageForProfile(Profile
* profile
,
256 const std::string
& sub_page
) {
257 if (SettingsWindowEnabled()) {
258 content::RecordAction(base::UserMetricsAction("ShowOptions"));
259 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
260 profile
, GetSettingsUrl(sub_page
));
264 chrome::FindTabbedBrowser(profile
, false, HOST_DESKTOP_TYPE_NATIVE
);
266 browser
= new Browser(
267 Browser::CreateParams(profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
269 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
272 void ShowSettingsSubPageInTabbedBrowser(Browser
* browser
,
273 const std::string
& sub_page
) {
274 content::RecordAction(UserMetricsAction("ShowOptions"));
275 GURL gurl
= GetSettingsUrl(sub_page
);
276 NavigateParams
params(GetSingletonTabNavigateParams(browser
, gurl
));
277 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
278 ShowSingletonTabOverwritingNTP(browser
, params
);
281 void ShowContentSettings(Browser
* browser
,
282 ContentSettingsType content_settings_type
) {
285 kContentSettingsExceptionsSubPage
+ std::string(kHashMark
) +
286 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
287 content_settings_type
));
290 void ShowClearBrowsingDataDialog(Browser
* browser
) {
291 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
292 ShowSettingsSubPage(browser
, kClearBrowserDataSubPage
);
295 void ShowPasswordManager(Browser
* browser
) {
296 content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
297 ShowSettingsSubPage(browser
, kPasswordManagerSubPage
);
300 void ShowImportDialog(Browser
* browser
) {
301 content::RecordAction(UserMetricsAction("Import_ShowDlg"));
302 ShowSettingsSubPage(browser
, kImportDataSubPage
);
305 void ShowAboutChrome(Browser
* browser
) {
306 content::RecordAction(UserMetricsAction("AboutChrome"));
307 NavigateParams
params(
308 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIUberURL
)));
309 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
310 ShowSingletonTabOverwritingNTP(browser
, params
);
313 void ShowSearchEngineSettings(Browser
* browser
) {
314 content::RecordAction(UserMetricsAction("EditSearchEngines"));
315 ShowSettingsSubPage(browser
, kSearchEnginesSubPage
);
318 void ShowBrowserSignin(Browser
* browser
, signin::Source source
) {
319 Profile
* original_profile
= browser
->profile()->GetOriginalProfile();
320 SigninManagerBase
* manager
=
321 SigninManagerFactory::GetForProfile(original_profile
);
322 DCHECK(manager
->IsSigninAllowed());
323 // If we're signed in, just show settings.
324 if (!manager
->GetAuthenticatedUsername().empty()) {
325 ShowSettings(browser
);
327 // If the browser's profile is an incognito profile, make sure to use
328 // a browser window from the original profile. The user cannot sign in
329 // from an incognito window.
330 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
331 if (browser
->profile()->IsOffTheRecord()) {
332 displayer
.reset(new ScopedTabbedBrowserDisplayer(
333 original_profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
334 browser
= displayer
->browser();
337 NavigateToSingletonTab(browser
, GURL(signin::GetPromoURL(source
, false)));
338 DCHECK_GT(browser
->tab_strip_model()->count(), 0);
342 } // namespace chrome