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/signin/signin_manager_factory.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_navigator.h"
18 #include "chrome/browser/ui/browser_window.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_manager.h"
28 #include "content/public/browser/user_metrics.h"
29 #include "content/public/browser/web_contents.h"
30 #include "google_apis/gaia/gaia_urls.h"
31 #include "net/base/url_util.h"
34 #include "chrome/browser/enumerate_modules_model_win.h"
37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/genius_app/app_id.h"
39 #include "chromeos/chromeos_switches.h"
40 #include "extensions/browser/extension_registry.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 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
83 genius_app::kGeniusAppId
,
84 extensions::ExtensionRegistry::EVERYTHING
);
85 OpenApplication(AppLaunchParams(profile
, extension
, 0, host_desktop_type
));
91 case HELP_SOURCE_KEYBOARD
:
92 url
= GURL(kChromeHelpViaKeyboardURL
);
94 case HELP_SOURCE_MENU
:
95 url
= GURL(kChromeHelpViaMenuURL
);
97 case HELP_SOURCE_WEBUI
:
98 url
= GURL(kChromeHelpViaWebUIURL
);
101 NOTREACHED() << "Unhandled help source " << source
;
103 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
106 new ScopedTabbedBrowserDisplayer(profile
, host_desktop_type
));
107 browser
= displayer
->browser();
109 ShowSingletonTab(browser
, url
);
114 void ShowBookmarkManager(Browser
* browser
) {
115 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
116 content::RecordAction(UserMetricsAction("ShowBookmarks"));
117 ShowSingletonTabOverwritingNTP(
119 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIBookmarksURL
)));
122 void ShowBookmarkManagerForNode(Browser
* browser
, int64 node_id
) {
123 OpenBookmarkManagerWithHash(browser
, std::string(), node_id
);
126 void ShowHistory(Browser
* browser
) {
127 content::RecordAction(UserMetricsAction("ShowHistory"));
128 NavigateParams
params(
129 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIHistoryURL
)));
130 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
131 ShowSingletonTabOverwritingNTP(browser
, params
);
134 void ShowDownloads(Browser
* browser
) {
135 content::RecordAction(UserMetricsAction("ShowDownloads"));
136 if (browser
->window()) {
137 DownloadShelf
* shelf
= browser
->window()->GetDownloadShelf();
138 // The downloads page is always shown in response to a user action.
139 if (shelf
->IsShowing())
140 shelf
->Close(DownloadShelf::USER_ACTION
);
142 ShowSingletonTabOverwritingNTP(
144 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIDownloadsURL
)));
147 void ShowExtensions(Browser
* browser
,
148 const std::string
& extension_to_highlight
) {
149 content::RecordAction(UserMetricsAction("ShowExtensions"));
150 NavigateParams
params(
151 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIExtensionsURL
)));
152 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
153 if (!extension_to_highlight
.empty()) {
154 GURL::Replacements replacements
;
155 std::string
query("id=");
156 query
+= extension_to_highlight
;
157 replacements
.SetQueryStr(query
);
158 params
.url
= params
.url
.ReplaceComponents(replacements
);
160 ShowSingletonTabOverwritingNTP(browser
, params
);
163 void ShowConflicts(Browser
* browser
) {
165 EnumerateModulesModel
* model
= EnumerateModulesModel::GetInstance();
166 if (model
->modules_to_notify_about() > 0) {
167 GURL help_center_url
= model
->GetFirstNotableConflict();
168 if (help_center_url
.is_valid()) {
169 ShowSingletonTab(browser
, help_center_url
);
170 model
->AcknowledgeConflictNotification();
176 content::RecordAction(UserMetricsAction("AboutConflicts"));
177 ShowSingletonTab(browser
, GURL(kChromeUIConflictsURL
));
180 void ShowHelp(Browser
* browser
, HelpSource source
) {
182 browser
, browser
->profile(), browser
->host_desktop_type(), source
);
185 void ShowHelpForProfile(Profile
* profile
,
186 HostDesktopType host_desktop_type
,
188 ShowHelpImpl(NULL
, profile
, host_desktop_type
, source
);
191 void ShowPolicy(Browser
* browser
) {
192 ShowSingletonTab(browser
, GURL(kChromeUIPolicyURL
));
195 void ShowSlow(Browser
* browser
) {
196 #if defined(OS_CHROMEOS)
197 ShowSingletonTab(browser
, GURL(kChromeUISlowURL
));
201 GURL
GetSettingsUrl(const std::string
& sub_page
) {
202 std::string url
= std::string(kChromeUISettingsURL
) + sub_page
;
203 #if defined(OS_CHROMEOS)
204 if (sub_page
.find(kInternetOptionsSubPage
, 0) != std::string::npos
) {
205 std::string::size_type loc
= sub_page
.find("?", 0);
206 std::string network_page
=
207 loc
!= std::string::npos
? sub_page
.substr(loc
) : std::string();
208 url
= std::string(kChromeUISettingsURL
) + network_page
;
214 bool IsTrustedPopupWindowWithScheme(const Browser
* browser
,
215 const std::string
& scheme
) {
216 if (!browser
->is_type_popup() || !browser
->is_trusted_source())
218 if (scheme
.empty()) // Any trusted popup window
220 const content::WebContents
* web_contents
=
221 browser
->tab_strip_model()->GetWebContentsAt(0);
224 GURL
url(web_contents
->GetURL());
225 return url
.SchemeIs(scheme
.c_str());
228 void ShowSettings(Browser
* browser
) {
229 ShowSettingsSubPage(browser
, std::string());
232 void ShowSettingsSubPage(Browser
* browser
, const std::string
& sub_page
) {
233 if (::switches::SettingsWindowEnabled()) {
234 ShowSettingsSubPageForProfile(browser
->profile(), sub_page
);
237 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
240 void ShowSettingsSubPageForProfile(Profile
* profile
,
241 const std::string
& sub_page
) {
242 if (::switches::SettingsWindowEnabled()) {
243 content::RecordAction(base::UserMetricsAction("ShowOptions"));
244 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
245 profile
, GetSettingsUrl(sub_page
));
249 chrome::FindTabbedBrowser(profile
, false, HOST_DESKTOP_TYPE_NATIVE
);
251 browser
= new Browser(
252 Browser::CreateParams(profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
254 ShowSettingsSubPageInTabbedBrowser(browser
, sub_page
);
257 void ShowSettingsSubPageInTabbedBrowser(Browser
* browser
,
258 const std::string
& sub_page
) {
259 content::RecordAction(UserMetricsAction("ShowOptions"));
260 GURL gurl
= GetSettingsUrl(sub_page
);
261 NavigateParams
params(GetSingletonTabNavigateParams(browser
, gurl
));
262 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
263 ShowSingletonTabOverwritingNTP(browser
, params
);
266 void ShowContentSettings(Browser
* browser
,
267 ContentSettingsType content_settings_type
) {
270 kContentSettingsExceptionsSubPage
+ std::string(kHashMark
) +
271 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
272 content_settings_type
));
275 void ShowClearBrowsingDataDialog(Browser
* browser
) {
276 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
277 ShowSettingsSubPage(browser
, kClearBrowserDataSubPage
);
280 void ShowPasswordManager(Browser
* browser
) {
281 content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
282 ShowSettingsSubPage(browser
, kPasswordManagerSubPage
);
285 void ShowImportDialog(Browser
* browser
) {
286 content::RecordAction(UserMetricsAction("Import_ShowDlg"));
287 ShowSettingsSubPage(browser
, kImportDataSubPage
);
290 void ShowAboutChrome(Browser
* browser
) {
291 content::RecordAction(UserMetricsAction("AboutChrome"));
292 if (::switches::SettingsWindowEnabled()) {
293 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
294 browser
->profile(), GURL(kChromeUIUberURL
));
297 NavigateParams
params(
298 GetSingletonTabNavigateParams(browser
, GURL(kChromeUIUberURL
)));
299 params
.path_behavior
= NavigateParams::IGNORE_AND_NAVIGATE
;
300 ShowSingletonTabOverwritingNTP(browser
, params
);
303 void ShowSearchEngineSettings(Browser
* browser
) {
304 content::RecordAction(UserMetricsAction("EditSearchEngines"));
305 ShowSettingsSubPage(browser
, kSearchEnginesSubPage
);
308 void ShowBrowserSignin(Browser
* browser
, signin::Source source
) {
309 Profile
* original_profile
= browser
->profile()->GetOriginalProfile();
310 SigninManagerBase
* manager
=
311 SigninManagerFactory::GetForProfile(original_profile
);
312 DCHECK(manager
->IsSigninAllowed());
313 // If we're signed in, just show settings.
314 if (manager
->IsAuthenticated()) {
315 ShowSettings(browser
);
317 // If the browser's profile is an incognito profile, make sure to use
318 // a browser window from the original profile. The user cannot sign in
319 // from an incognito window.
320 scoped_ptr
<ScopedTabbedBrowserDisplayer
> displayer
;
321 if (browser
->profile()->IsOffTheRecord()) {
322 displayer
.reset(new ScopedTabbedBrowserDisplayer(
323 original_profile
, chrome::HOST_DESKTOP_TYPE_NATIVE
));
324 browser
= displayer
->browser();
327 NavigateToSingletonTab(browser
, GURL(signin::GetPromoURL(source
, false)));
328 DCHECK_GT(browser
->tab_strip_model()->count(), 0);
332 } // namespace chrome