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/browser/signin_header_helper.h"
28 #include "components/signin/core/common/profile_management_switches.h"
29 #include "content/public/browser/user_metrics.h"
30 #include "content/public/browser/web_contents.h"
31 #include "extensions/common/constants.h"
32 #include "google_apis/gaia/gaia_urls.h"
33 #include "net/base/url_util.h"
34 #include "ui/base/window_open_disposition.h"
37 #include "chrome/browser/enumerate_modules_model_win.h"
40 #if defined(OS_CHROMEOS)
41 #include "chrome/browser/chromeos/genius_app/app_id.h"
42 #include "extensions/browser/extension_registry.h"
45 #if !defined(OS_ANDROID) && !defined(OS_IOS)
46 #include "chrome/browser/signin/signin_manager_factory.h"
47 #include "components/signin/core/browser/signin_manager.h"
50 using base::UserMetricsAction
;
55 const char kHashMark
[] = "#";
57 void OpenBookmarkManagerWithHash(Browser
* browser
,
58 const std::string
& action
,
60 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
61 content::RecordAction(UserMetricsAction("ShowBookmarks"));
62 NavigateParams
params(GetSingletonTabNavigateParams(
64 GURL(kChromeUIBookmarksURL
).Resolve(base::StringPrintf(
65 "/#%s%s", action
.c_str(), base::Int64ToString(node_id
).c_str()))));
66 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
67 ShowSingletonTabOverwritingNTP(browser
, params
);
70 void NavigateToSingletonTab(Browser
* browser
, const GURL
& url
) {
71 NavigateParams
params(GetSingletonTabNavigateParams(browser
, url
));
72 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
73 ShowSingletonTabOverwritingNTP(browser
, params
);
76 // Shows either the help app or the appropriate help page for |source|. If
77 // |browser| is NULL and the help page is used (vs the app), the help page is
78 // shown in the last active browser. If there is no such browser, a new browser
80 void ShowHelpImpl(Browser
* browser
,
82 HostDesktopType host_desktop_type
,
84 content::RecordAction(UserMetricsAction("ShowHelpTab"));
85 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
86 const extensions::Extension
* extension
=
87 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
88 genius_app::kGeniusAppId
,
89 extensions::ExtensionRegistry::EVERYTHING
);
90 extensions::AppLaunchSource
app_launch_source(extensions::SOURCE_UNTRACKED
);
92 case HELP_SOURCE_KEYBOARD
:
93 app_launch_source
= extensions::SOURCE_KEYBOARD
;
95 case HELP_SOURCE_MENU
:
96 app_launch_source
= extensions::SOURCE_SYSTEM_TRAY
;
98 case HELP_SOURCE_WEBUI
:
99 app_launch_source
= extensions::SOURCE_ABOUT_PAGE
;
102 NOTREACHED() << "Unhandled help source" << source
;
104 AppLaunchParams
params(profile
, extension
, CURRENT_TAB
, host_desktop_type
,
106 OpenApplication(params
);
110 case HELP_SOURCE_KEYBOARD
:
111 url
= GURL(kChromeHelpViaKeyboardURL
);
113 case HELP_SOURCE_MENU
:
114 url
= GURL(kChromeHelpViaMenuURL
);
116 case HELP_SOURCE_WEBUI
:
117 url
= GURL(kChromeHelpViaWebUIURL
);
120 NOTREACHED() << "Unhandled help source " << source
;
122 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
125 new ScopedTabbedBrowserDisplayer(profile
, host_desktop_type
));
126 browser
= displayer
->browser();
128 ShowSingletonTab(browser
, url
);
134 void ShowBookmarkManager(Browser
* browser
) {
135 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
136 content::RecordAction(UserMetricsAction("ShowBookmarks"));
137 ShowSingletonTabOverwritingNTP(
139 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIBookmarksURL
)));
142 void ShowBookmarkManagerForNode(Browser
* browser
, int64 node_id
) {
143 OpenBookmarkManagerWithHash(browser
, std::string(), node_id
);
146 void ShowHistory(Browser
* browser
) {
147 content::RecordAction(UserMetricsAction("ShowHistory"));
148 NavigateParams
params(
149 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIHistoryURL
)));
150 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
151 ShowSingletonTabOverwritingNTP(browser
, params
);
154 void ShowDownloads(Browser
* browser
) {
155 content::RecordAction(UserMetricsAction("ShowDownloads"));
156 if (browser
->window()) {
157 DownloadShelf
* shelf
= browser
->window()->GetDownloadShelf();
158 // The downloads page is always shown in response to a user action.
159 if (shelf
->IsShowing())
160 shelf
->Close(DownloadShelf::USER_ACTION
);
162 ShowSingletonTabOverwritingNTP(
164 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIDownloadsURL
)));
167 void ShowExtensions(Browser
* browser
,
168 const std::string
& extension_to_highlight
) {
169 content::RecordAction(UserMetricsAction("ShowExtensions"));
170 NavigateParams
params(
171 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIExtensionsURL
)));
172 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
173 if (!extension_to_highlight
.empty()) {
174 GURL::Replacements replacements
;
175 std::string
query("id=");
176 query
+= extension_to_highlight
;
177 replacements
.SetQueryStr(query
);
178 params
.url
= params
.url
.ReplaceComponents(replacements
);
180 ShowSingletonTabOverwritingNTP(browser
, params
);
183 void ShowConflicts(Browser
* browser
) {
185 EnumerateModulesModel
* model
= EnumerateModulesModel::GetInstance();
186 if (model
->modules_to_notify_about() > 0) {
187 GURL help_center_url
= model
->GetFirstNotableConflict();
188 if (help_center_url
.is_valid()) {
189 ShowSingletonTab(browser
, help_center_url
);
190 model
->AcknowledgeConflictNotification();
196 content::RecordAction(UserMetricsAction("AboutConflicts"));
197 ShowSingletonTab(browser
, GURL(kChromeUIConflictsURL
));
200 void ShowHelp(Browser
* browser
, HelpSource source
) {
202 browser
, browser
->profile(), browser
->host_desktop_type(), source
);
205 void ShowHelpForProfile(Profile
* profile
,
206 HostDesktopType host_desktop_type
,
208 ShowHelpImpl(NULL
, profile
, host_desktop_type
, source
);
211 void ShowPolicy(Browser
* browser
) {
212 ShowSingletonTab(browser
, GURL(kChromeUIPolicyURL
));
215 void ShowSlow(Browser
* browser
) {
216 #if defined(OS_CHROMEOS)
217 ShowSingletonTab(browser
, GURL(kChromeUISlowURL
));
221 void ShowMemory(Browser
* browser
) {
222 ShowSingletonTab(browser
, GURL(kChromeUIMemoryURL
));
225 GURL
GetSettingsUrl(const std::string
& sub_page
) {
226 std::string url
= std::string(kChromeUISettingsURL
) + sub_page
;
227 #if defined(OS_CHROMEOS)
228 if (sub_page
.find(kInternetOptionsSubPage
, 0) != std::string::npos
) {
229 std::string::size_type loc
= sub_page
.find("?", 0);
230 std::string network_page
=
231 loc
!= std::string::npos
? sub_page
.substr(loc
) : std::string();
232 url
= std::string(kChromeUISettingsURL
) + network_page
;
238 bool IsSettingsSubPage(const GURL
& url
, const std::string
& sub_page
) {
239 return (url
.SchemeIs(content::kChromeUIScheme
) &&
240 (url
.host() == chrome::kChromeUISettingsHost
||
241 url
.host() == chrome::kChromeUISettingsFrameHost
) &&
242 url
.path() == "/" + sub_page
);
245 bool IsTrustedPopupWindowWithScheme(const Browser
* browser
,
246 const std::string
& scheme
) {
247 if (!browser
->is_type_popup() || !browser
->is_trusted_source())
249 if (scheme
.empty()) // Any trusted popup window
251 const content::WebContents
* web_contents
=
252 browser
->tab_strip_model()->GetWebContentsAt(0);
255 GURL
url(web_contents
->GetURL());
256 return url
.SchemeIs(scheme
.c_str());
260 void ShowSettings(Browser
* browser
) {
261 ShowSettingsSubPage(browser
, std::string());
264 void ShowSettingsSubPage(Browser
* browser
, const std::string
& sub_page
) {
265 if (::switches::SettingsWindowEnabled()) {
266 ShowSettingsSubPageForProfile(browser
->profile(), sub_page
);
269 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
272 void ShowSettingsSubPageForProfile(Profile
* profile
,
273 const std::string
& sub_page
) {
274 if (::switches::SettingsWindowEnabled()) {
275 content::RecordAction(base::UserMetricsAction("ShowOptions"));
276 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
277 profile
, GetSettingsUrl(sub_page
));
281 chrome::FindTabbedBrowser(profile
, false, HOST_DESKTOP_TYPE_NATIVE
);
283 browser
= new Browser(
284 Browser::CreateParams(profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
286 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
289 void ShowSettingsSubPageInTabbedBrowser(Browser
* browser
,
290 const std::string
& sub_page
) {
291 content::RecordAction(UserMetricsAction("ShowOptions"));
292 GURL gurl
= GetSettingsUrl(sub_page
);
293 NavigateParams
params(GetSingletonTabNavigateParams(browser
, gurl
));
294 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
295 ShowSingletonTabOverwritingNTP(browser
, params
);
298 void ShowContentSettingsExceptions(Browser
* browser
,
299 ContentSettingsType content_settings_type
) {
302 kContentSettingsExceptionsSubPage
+ std::string(kHashMark
) +
303 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
304 content_settings_type
));
307 void ShowContentSettings(Browser
* browser
,
308 ContentSettingsType content_settings_type
) {
311 kContentSettingsSubPage
+ std::string(kHashMark
) +
312 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
313 content_settings_type
));
316 void ShowClearBrowsingDataDialog(Browser
* browser
) {
317 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
318 ShowSettingsSubPage(browser
, kClearBrowserDataSubPage
);
321 void ShowPasswordManager(Browser
* browser
) {
322 content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
323 ShowSettingsSubPage(browser
, kPasswordManagerSubPage
);
326 void ShowImportDialog(Browser
* browser
) {
327 content::RecordAction(UserMetricsAction("Import_ShowDlg"));
328 ShowSettingsSubPage(browser
, kImportDataSubPage
);
331 void ShowAboutChrome(Browser
* browser
) {
332 content::RecordAction(UserMetricsAction("AboutChrome"));
333 if (::switches::SettingsWindowEnabled()) {
334 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
335 browser
->profile(), GURL(kChromeUIHelpURL
));
338 NavigateParams
params(
339 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIHelpURL
)));
340 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
341 ShowSingletonTabOverwritingNTP(browser
, params
);
344 void ShowSearchEngineSettings(Browser
* browser
) {
345 content::RecordAction(UserMetricsAction("EditSearchEngines"));
346 ShowSettingsSubPage(browser
, kSearchEnginesSubPage
);
349 #if !defined(OS_ANDROID) && !defined(OS_IOS)
350 void ShowBrowserSignin(Browser
* browser
, signin_metrics::Source source
) {
351 Profile
* original_profile
= browser
->profile()->GetOriginalProfile();
352 SigninManagerBase
* manager
=
353 SigninManagerFactory::GetForProfile(original_profile
);
354 DCHECK(manager
->IsSigninAllowed());
355 // If the browser's profile is an incognito profile, make sure to use
356 // a browser window from the original profile. The user cannot sign in
357 // from an incognito window.
358 bool switched_browser
= false;
359 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
360 if (browser
->profile()->IsOffTheRecord()) {
361 switched_browser
= true;
362 displayer
.reset(new ScopedTabbedBrowserDisplayer(
363 original_profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
364 browser
= displayer
->browser();
367 signin_metrics::LogSigninSource(source
);
369 // Since the app launcher is a separate application, it might steal focus
370 // away from Chrome, and accidentally close the avatar bubble. The same will
371 // happen if we had to switch browser windows to show the sign in page. In
372 // this case, fallback to the full-tab signin page.
373 if (switches::IsNewAvatarMenu() &&
374 source
!= signin_metrics::SOURCE_APP_LAUNCHER
&& !switched_browser
) {
375 browser
->window()->ShowAvatarBubbleFromAvatarButton(
376 BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN
,
377 signin::ManageAccountsParams());
379 NavigateToSingletonTab(browser
, GURL(signin::GetPromoURL(source
, false)));
380 DCHECK_GT(browser
->tab_strip_model()->count(), 0);
384 void ShowBrowserSigninOrSettings(
386 signin_metrics::Source source
) {
387 Profile
* original_profile
= browser
->profile()->GetOriginalProfile();
388 SigninManagerBase
* manager
=
389 SigninManagerFactory::GetForProfile(original_profile
);
390 DCHECK(manager
->IsSigninAllowed());
391 if (manager
->IsAuthenticated())
392 ShowSettings(browser
);
394 ShowBrowserSignin(browser
, source
);
398 } // namespace chrome