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/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_navigator.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/extensions/app_launch_params.h"
19 #include "chrome/browser/ui/extensions/application_launch.h"
20 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
21 #include "chrome/browser/ui/settings_window_manager.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/chrome_switches.h"
26 #include "chrome/common/url_constants.h"
27 #include "components/signin/core/common/profile_management_switches.h"
28 #include "content/public/browser/user_metrics.h"
29 #include "content/public/browser/web_contents.h"
30 #include "extensions/common/constants.h"
31 #include "google_apis/gaia/gaia_urls.h"
32 #include "net/base/url_util.h"
33 #include "ui/base/window_open_disposition.h"
36 #include "chrome/browser/enumerate_modules_model_win.h"
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/genius_app/app_id.h"
41 #include "extensions/browser/extension_registry.h"
44 #if !defined(OS_ANDROID) && !defined(OS_IOS)
45 #include "chrome/browser/signin/signin_manager_factory.h"
46 #include "components/signin/core/browser/signin_manager.h"
49 using base::UserMetricsAction
;
54 const char kHashMark
[] = "#";
56 void OpenBookmarkManagerWithHash(Browser
* browser
,
57 const std::string
& action
,
59 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
60 content::RecordAction(UserMetricsAction("ShowBookmarks"));
61 NavigateParams
params(GetSingletonTabNavigateParams(
63 GURL(kChromeUIBookmarksURL
).Resolve(base::StringPrintf(
64 "/#%s%s", action
.c_str(), base::Int64ToString(node_id
).c_str()))));
65 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
66 ShowSingletonTabOverwritingNTP(browser
, params
);
69 void NavigateToSingletonTab(Browser
* browser
, const GURL
& url
) {
70 NavigateParams
params(GetSingletonTabNavigateParams(browser
, url
));
71 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
72 ShowSingletonTabOverwritingNTP(browser
, params
);
75 // Shows either the help app or the appropriate help page for |source|. If
76 // |browser| is NULL and the help page is used (vs the app), the help page is
77 // shown in the last active browser. If there is no such browser, a new browser
79 void ShowHelpImpl(Browser
* browser
,
81 HostDesktopType host_desktop_type
,
83 content::RecordAction(UserMetricsAction("ShowHelpTab"));
84 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
85 const extensions::Extension
* extension
=
86 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
87 genius_app::kGeniusAppId
,
88 extensions::ExtensionRegistry::EVERYTHING
);
89 extensions::AppLaunchSource
app_launch_source(extensions::SOURCE_UNTRACKED
);
91 case HELP_SOURCE_KEYBOARD
:
92 app_launch_source
= extensions::SOURCE_KEYBOARD
;
94 case HELP_SOURCE_MENU
:
95 app_launch_source
= extensions::SOURCE_SYSTEM_TRAY
;
97 case HELP_SOURCE_WEBUI
:
98 app_launch_source
= extensions::SOURCE_ABOUT_PAGE
;
101 NOTREACHED() << "Unhandled help source" << source
;
103 AppLaunchParams
params(profile
, extension
, CURRENT_TAB
, host_desktop_type
,
105 OpenApplication(params
);
109 case HELP_SOURCE_KEYBOARD
:
110 url
= GURL(kChromeHelpViaKeyboardURL
);
112 case HELP_SOURCE_MENU
:
113 url
= GURL(kChromeHelpViaMenuURL
);
115 case HELP_SOURCE_WEBUI
:
116 url
= GURL(kChromeHelpViaWebUIURL
);
119 NOTREACHED() << "Unhandled help source " << source
;
121 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
124 new ScopedTabbedBrowserDisplayer(profile
, host_desktop_type
));
125 browser
= displayer
->browser();
127 ShowSingletonTab(browser
, url
);
133 void ShowBookmarkManager(Browser
* browser
) {
134 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
135 content::RecordAction(UserMetricsAction("ShowBookmarks"));
136 ShowSingletonTabOverwritingNTP(
138 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIBookmarksURL
)));
141 void ShowBookmarkManagerForNode(Browser
* browser
, int64 node_id
) {
142 OpenBookmarkManagerWithHash(browser
, std::string(), node_id
);
145 void ShowHistory(Browser
* browser
) {
146 content::RecordAction(UserMetricsAction("ShowHistory"));
147 NavigateParams
params(
148 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIHistoryURL
)));
149 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
150 ShowSingletonTabOverwritingNTP(browser
, params
);
153 void ShowDownloads(Browser
* browser
) {
154 content::RecordAction(UserMetricsAction("ShowDownloads"));
155 if (browser
->window()) {
156 DownloadShelf
* shelf
= browser
->window()->GetDownloadShelf();
157 // The downloads page is always shown in response to a user action.
158 if (shelf
->IsShowing())
159 shelf
->Close(DownloadShelf::USER_ACTION
);
161 ShowSingletonTabOverwritingNTP(
163 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIDownloadsURL
)));
166 void ShowExtensions(Browser
* browser
,
167 const std::string
& extension_to_highlight
) {
168 content::RecordAction(UserMetricsAction("ShowExtensions"));
169 NavigateParams
params(
170 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIExtensionsURL
)));
171 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
172 if (!extension_to_highlight
.empty()) {
173 GURL::Replacements replacements
;
174 std::string
query("id=");
175 query
+= extension_to_highlight
;
176 replacements
.SetQueryStr(query
);
177 params
.url
= params
.url
.ReplaceComponents(replacements
);
179 ShowSingletonTabOverwritingNTP(browser
, params
);
182 void ShowConflicts(Browser
* browser
) {
184 EnumerateModulesModel
* model
= EnumerateModulesModel::GetInstance();
185 if (model
->modules_to_notify_about() > 0) {
186 GURL help_center_url
= model
->GetFirstNotableConflict();
187 if (help_center_url
.is_valid()) {
188 ShowSingletonTab(browser
, help_center_url
);
189 model
->AcknowledgeConflictNotification();
195 content::RecordAction(UserMetricsAction("AboutConflicts"));
196 ShowSingletonTab(browser
, GURL(kChromeUIConflictsURL
));
199 void ShowHelp(Browser
* browser
, HelpSource source
) {
201 browser
, browser
->profile(), browser
->host_desktop_type(), source
);
204 void ShowHelpForProfile(Profile
* profile
,
205 HostDesktopType host_desktop_type
,
207 ShowHelpImpl(NULL
, profile
, host_desktop_type
, source
);
210 void ShowPolicy(Browser
* browser
) {
211 ShowSingletonTab(browser
, GURL(kChromeUIPolicyURL
));
214 void ShowSlow(Browser
* browser
) {
215 #if defined(OS_CHROMEOS)
216 ShowSingletonTab(browser
, GURL(kChromeUISlowURL
));
220 void ShowMemory(Browser
* browser
) {
221 ShowSingletonTab(browser
, GURL(kChromeUIMemoryURL
));
224 GURL
GetSettingsUrl(const std::string
& sub_page
) {
225 std::string url
= std::string(kChromeUISettingsURL
) + sub_page
;
226 #if defined(OS_CHROMEOS)
227 if (sub_page
.find(kInternetOptionsSubPage
, 0) != std::string::npos
) {
228 std::string::size_type loc
= sub_page
.find("?", 0);
229 std::string network_page
=
230 loc
!= std::string::npos
? sub_page
.substr(loc
) : std::string();
231 url
= std::string(kChromeUISettingsURL
) + network_page
;
237 bool IsTrustedPopupWindowWithScheme(const Browser
* browser
,
238 const std::string
& scheme
) {
239 if (!browser
->is_type_popup() || !browser
->is_trusted_source())
241 if (scheme
.empty()) // Any trusted popup window
243 const content::WebContents
* web_contents
=
244 browser
->tab_strip_model()->GetWebContentsAt(0);
247 GURL
url(web_contents
->GetURL());
248 return url
.SchemeIs(scheme
.c_str());
251 void ShowSettings(Browser
* browser
) {
252 ShowSettingsSubPage(browser
, std::string());
255 void ShowSettingsSubPage(Browser
* browser
, const std::string
& sub_page
) {
256 if (::switches::SettingsWindowEnabled()) {
257 ShowSettingsSubPageForProfile(browser
->profile(), sub_page
);
260 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
263 void ShowSettingsSubPageForProfile(Profile
* profile
,
264 const std::string
& sub_page
) {
265 if (::switches::SettingsWindowEnabled()) {
266 content::RecordAction(base::UserMetricsAction("ShowOptions"));
267 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
268 profile
, GetSettingsUrl(sub_page
));
272 chrome::FindTabbedBrowser(profile
, false, HOST_DESKTOP_TYPE_NATIVE
);
274 browser
= new Browser(
275 Browser::CreateParams(profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
277 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
280 void ShowSettingsSubPageInTabbedBrowser(Browser
* browser
,
281 const std::string
& sub_page
) {
282 content::RecordAction(UserMetricsAction("ShowOptions"));
283 GURL gurl
= GetSettingsUrl(sub_page
);
284 NavigateParams
params(GetSingletonTabNavigateParams(browser
, gurl
));
285 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
286 ShowSingletonTabOverwritingNTP(browser
, params
);
289 void ShowContentSettings(Browser
* browser
,
290 ContentSettingsType content_settings_type
) {
293 kContentSettingsExceptionsSubPage
+ std::string(kHashMark
) +
294 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
295 content_settings_type
));
298 void ShowClearBrowsingDataDialog(Browser
* browser
) {
299 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
300 ShowSettingsSubPage(browser
, kClearBrowserDataSubPage
);
303 void ShowPasswordManager(Browser
* browser
) {
304 content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
305 ShowSettingsSubPage(browser
, kPasswordManagerSubPage
);
308 void ShowImportDialog(Browser
* browser
) {
309 content::RecordAction(UserMetricsAction("Import_ShowDlg"));
310 ShowSettingsSubPage(browser
, kImportDataSubPage
);
313 void ShowAboutChrome(Browser
* browser
) {
314 content::RecordAction(UserMetricsAction("AboutChrome"));
315 if (::switches::SettingsWindowEnabled()) {
316 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
317 browser
->profile(), GURL(kChromeUIUberURL
));
320 NavigateParams
params(
321 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIUberURL
)));
322 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
323 ShowSingletonTabOverwritingNTP(browser
, params
);
326 void ShowSearchEngineSettings(Browser
* browser
) {
327 content::RecordAction(UserMetricsAction("EditSearchEngines"));
328 ShowSettingsSubPage(browser
, kSearchEnginesSubPage
);
331 #if !defined(OS_ANDROID) && !defined(OS_IOS)
332 void ShowBrowserSignin(Browser
* browser
, signin_metrics::Source source
) {
333 Profile
* original_profile
= browser
->profile()->GetOriginalProfile();
334 SigninManagerBase
* manager
=
335 SigninManagerFactory::GetForProfile(original_profile
);
336 DCHECK(manager
->IsSigninAllowed());
337 // If the browser's profile is an incognito profile, make sure to use
338 // a browser window from the original profile. The user cannot sign in
339 // from an incognito window.
340 bool switched_browser
= false;
341 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
342 if (browser
->profile()->IsOffTheRecord()) {
343 switched_browser
= true;
344 displayer
.reset(new ScopedTabbedBrowserDisplayer(
345 original_profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
346 browser
= displayer
->browser();
349 signin_metrics::LogSigninSource(source
);
351 // Since the app launcher is a separate application, it might steal focus
352 // away from Chrome, and accidentally close the avatar bubble. The same will
353 // happen if we had to switch browser windows to show the sign in page. In
354 // this case, fallback to the full-tab signin page.
355 if (switches::IsNewAvatarMenu() &&
356 source
!= signin_metrics::SOURCE_APP_LAUNCHER
&& !switched_browser
) {
357 browser
->window()->ShowAvatarBubbleFromAvatarButton(
358 BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN
,
359 signin::ManageAccountsParams());
361 NavigateToSingletonTab(browser
, GURL(signin::GetPromoURL(source
, false)));
362 DCHECK_GT(browser
->tab_strip_model()->count(), 0);
366 void ShowBrowserSigninOrSettings(
368 signin_metrics::Source source
) {
369 Profile
* original_profile
= browser
->profile()->GetOriginalProfile();
370 SigninManagerBase
* manager
=
371 SigninManagerFactory::GetForProfile(original_profile
);
372 DCHECK(manager
->IsSigninAllowed());
373 if (manager
->IsAuthenticated())
374 ShowSettings(browser
);
376 ShowBrowserSignin(browser
, source
);
380 } // namespace chrome