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.
4
5 #include "chrome/browser/ui/chrome_pages.h"
6
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"
32
33 #if defined(OS_WIN)
34 #include "chrome/browser/enumerate_modules_model_win.h"
35 #endif
36
37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/genius_app/app_id.h"
39 #include "extensions/browser/extension_registry.h"
40 #endif
41
42 using base::UserMetricsAction;
43
44 namespace chrome {
45 namespace {
46
47 const char kHashMark[] = "#";
48
OpenBookmarkManagerWithHash(Browser * browser,const std::string & action,int64 node_id)49 void OpenBookmarkManagerWithHash(Browser* browser,
50 const std::string& action,
51 int64 node_id) {
52 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
53 content::RecordAction(UserMetricsAction("ShowBookmarks"));
54 NavigateParams params(GetSingletonTabNavigateParams(
55 browser,
56 GURL(kChromeUIBookmarksURL).Resolve(base::StringPrintf(
57 "/#%s%s", action.c_str(), base::Int64ToString(node_id).c_str()))));
58 params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
59 ShowSingletonTabOverwritingNTP(browser, params);
60 }
61
NavigateToSingletonTab(Browser * browser,const GURL & url)62 void NavigateToSingletonTab(Browser* browser, const GURL& url) {
63 NavigateParams params(GetSingletonTabNavigateParams(browser, url));
64 params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
65 ShowSingletonTabOverwritingNTP(browser, params);
66 }
67
68 // Shows either the help app or the appropriate help page for |source|. If
69 // |browser| is NULL and the help page is used (vs the app), the help page is
70 // shown in the last active browser. If there is no such browser, a new browser
71 // is created.
ShowHelpImpl(Browser * browser,Profile * profile,HostDesktopType host_desktop_type,HelpSource source)72 void ShowHelpImpl(Browser* browser,
73 Profile* profile,
74 HostDesktopType host_desktop_type,
75 HelpSource source) {
76 content::RecordAction(UserMetricsAction("ShowHelpTab"));
77 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
78 const extensions::Extension* extension =
79 extensions::ExtensionRegistry::Get(profile)->GetExtensionById(
80 genius_app::kGeniusAppId,
81 extensions::ExtensionRegistry::EVERYTHING);
82 OpenApplication(AppLaunchParams(profile, extension, 0, host_desktop_type));
83 #else
84 GURL url;
85 switch (source) {
86 case HELP_SOURCE_KEYBOARD:
87 url = GURL(kChromeHelpViaKeyboardURL);
88 break;
89 case HELP_SOURCE_MENU:
90 url = GURL(kChromeHelpViaMenuURL);
91 break;
92 case HELP_SOURCE_WEBUI:
93 url = GURL(kChromeHelpViaWebUIURL);
94 break;
95 default:
96 NOTREACHED() << "Unhandled help source " << source;
97 }
98 scoped_ptr<ScopedTabbedBrowserDisplayer> displayer;
99 if (!browser) {
100 displayer.reset(
101 new ScopedTabbedBrowserDisplayer(profile, host_desktop_type));
102 browser = displayer->browser();
103 }
104 ShowSingletonTab(browser, url);
105 #endif
106 }
107
108 } // namespace
109
ShowBookmarkManager(Browser * browser)110 void ShowBookmarkManager(Browser* browser) {
111 content::RecordAction(UserMetricsAction("ShowBookmarkManager"));
112 content::RecordAction(UserMetricsAction("ShowBookmarks"));
113 ShowSingletonTabOverwritingNTP(
114 browser,
115 GetSingletonTabNavigateParams(browser, GURL(kChromeUIBookmarksURL)));
116 }
117
ShowBookmarkManagerForNode(Browser * browser,int64 node_id)118 void ShowBookmarkManagerForNode(Browser* browser, int64 node_id) {
119 OpenBookmarkManagerWithHash(browser, std::string(), node_id);
120 }
121
ShowHistory(Browser * browser)122 void ShowHistory(Browser* browser) {
123 content::RecordAction(UserMetricsAction("ShowHistory"));
124 NavigateParams params(
125 GetSingletonTabNavigateParams(browser, GURL(kChromeUIHistoryURL)));
126 params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
127 ShowSingletonTabOverwritingNTP(browser, params);
128 }
129
ShowDownloads(Browser * browser)130 void ShowDownloads(Browser* browser) {
131 content::RecordAction(UserMetricsAction("ShowDownloads"));
132 if (browser->window()) {
133 DownloadShelf* shelf = browser->window()->GetDownloadShelf();
134 // The downloads page is always shown in response to a user action.
135 if (shelf->IsShowing())
136 shelf->Close(DownloadShelf::USER_ACTION);
137 }
138 ShowSingletonTabOverwritingNTP(
139 browser,
140 GetSingletonTabNavigateParams(browser, GURL(kChromeUIDownloadsURL)));
141 }
142
ShowExtensions(Browser * browser,const std::string & extension_to_highlight)143 void ShowExtensions(Browser* browser,
144 const std::string& extension_to_highlight) {
145 content::RecordAction(UserMetricsAction("ShowExtensions"));
146 NavigateParams params(
147 GetSingletonTabNavigateParams(browser, GURL(kChromeUIExtensionsURL)));
148 params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
149 if (!extension_to_highlight.empty()) {
150 GURL::Replacements replacements;
151 std::string query("id=");
152 query += extension_to_highlight;
153 replacements.SetQueryStr(query);
154 params.url = params.url.ReplaceComponents(replacements);
155 }
156 ShowSingletonTabOverwritingNTP(browser, params);
157 }
158
ShowConflicts(Browser * browser)159 void ShowConflicts(Browser* browser) {
160 #if defined(OS_WIN)
161 EnumerateModulesModel* model = EnumerateModulesModel::GetInstance();
162 if (model->modules_to_notify_about() > 0) {
163 GURL help_center_url = model->GetFirstNotableConflict();
164 if (help_center_url.is_valid()) {
165 ShowSingletonTab(browser, help_center_url);
166 model->AcknowledgeConflictNotification();
167 return;
168 }
169 }
170 #endif
171
172 content::RecordAction(UserMetricsAction("AboutConflicts"));
173 ShowSingletonTab(browser, GURL(kChromeUIConflictsURL));
174 }
175
ShowHelp(Browser * browser,HelpSource source)176 void ShowHelp(Browser* browser, HelpSource source) {
177 ShowHelpImpl(
178 browser, browser->profile(), browser->host_desktop_type(), source);
179 }
180
ShowHelpForProfile(Profile * profile,HostDesktopType host_desktop_type,HelpSource source)181 void ShowHelpForProfile(Profile* profile,
182 HostDesktopType host_desktop_type,
183 HelpSource source) {
184 ShowHelpImpl(NULL, profile, host_desktop_type, source);
185 }
186
ShowPolicy(Browser * browser)187 void ShowPolicy(Browser* browser) {
188 ShowSingletonTab(browser, GURL(kChromeUIPolicyURL));
189 }
190
ShowSlow(Browser * browser)191 void ShowSlow(Browser* browser) {
192 #if defined(OS_CHROMEOS)
193 ShowSingletonTab(browser, GURL(kChromeUISlowURL));
194 #endif
195 }
196
GetSettingsUrl(const std::string & sub_page)197 GURL GetSettingsUrl(const std::string& sub_page) {
198 std::string url = std::string(kChromeUISettingsURL) + sub_page;
199 #if defined(OS_CHROMEOS)
200 if (sub_page.find(kInternetOptionsSubPage, 0) != std::string::npos) {
201 std::string::size_type loc = sub_page.find("?", 0);
202 std::string network_page =
203 loc != std::string::npos ? sub_page.substr(loc) : std::string();
204 url = std::string(kChromeUISettingsURL) + network_page;
205 }
206 #endif
207 return GURL(url);
208 }
209
IsTrustedPopupWindowWithScheme(const Browser * browser,const std::string & scheme)210 bool IsTrustedPopupWindowWithScheme(const Browser* browser,
211 const std::string& scheme) {
212 if (!browser->is_type_popup() || !browser->is_trusted_source())
213 return false;
214 if (scheme.empty()) // Any trusted popup window
215 return true;
216 const content::WebContents* web_contents =
217 browser->tab_strip_model()->GetWebContentsAt(0);
218 if (!web_contents)
219 return false;
220 GURL url(web_contents->GetURL());
221 return url.SchemeIs(scheme.c_str());
222 }
223
ShowSettings(Browser * browser)224 void ShowSettings(Browser* browser) {
225 ShowSettingsSubPage(browser, std::string());
226 }
227
ShowSettingsSubPage(Browser * browser,const std::string & sub_page)228 void ShowSettingsSubPage(Browser* browser, const std::string& sub_page) {
229 if (::switches::SettingsWindowEnabled()) {
230 ShowSettingsSubPageForProfile(browser->profile(), sub_page);
231 return;
232 }
233 ShowSettingsSubPageInTabbedBrowser(browser, sub_page);
234 }
235
ShowSettingsSubPageForProfile(Profile * profile,const std::string & sub_page)236 void ShowSettingsSubPageForProfile(Profile* profile,
237 const std::string& sub_page) {
238 if (::switches::SettingsWindowEnabled()) {
239 content::RecordAction(base::UserMetricsAction("ShowOptions"));
240 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
241 profile, GetSettingsUrl(sub_page));
242 return;
243 }
244 Browser* browser =
245 chrome::FindTabbedBrowser(profile, false, HOST_DESKTOP_TYPE_NATIVE);
246 if (!browser) {
247 browser = new Browser(
248 Browser::CreateParams(profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
249 }
250 ShowSettingsSubPageInTabbedBrowser(browser, sub_page);
251 }
252
ShowSettingsSubPageInTabbedBrowser(Browser * browser,const std::string & sub_page)253 void ShowSettingsSubPageInTabbedBrowser(Browser* browser,
254 const std::string& sub_page) {
255 content::RecordAction(UserMetricsAction("ShowOptions"));
256 GURL gurl = GetSettingsUrl(sub_page);
257 NavigateParams params(GetSingletonTabNavigateParams(browser, gurl));
258 params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
259 ShowSingletonTabOverwritingNTP(browser, params);
260 }
261
ShowContentSettings(Browser * browser,ContentSettingsType content_settings_type)262 void ShowContentSettings(Browser* browser,
263 ContentSettingsType content_settings_type) {
264 ShowSettingsSubPage(
265 browser,
266 kContentSettingsExceptionsSubPage + std::string(kHashMark) +
267 options::ContentSettingsHandler::ContentSettingsTypeToGroupName(
268 content_settings_type));
269 }
270
ShowClearBrowsingDataDialog(Browser * browser)271 void ShowClearBrowsingDataDialog(Browser* browser) {
272 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
273 ShowSettingsSubPage(browser, kClearBrowserDataSubPage);
274 }
275
ShowPasswordManager(Browser * browser)276 void ShowPasswordManager(Browser* browser) {
277 content::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
278 ShowSettingsSubPage(browser, kPasswordManagerSubPage);
279 }
280
ShowImportDialog(Browser * browser)281 void ShowImportDialog(Browser* browser) {
282 content::RecordAction(UserMetricsAction("Import_ShowDlg"));
283 ShowSettingsSubPage(browser, kImportDataSubPage);
284 }
285
ShowAboutChrome(Browser * browser)286 void ShowAboutChrome(Browser* browser) {
287 content::RecordAction(UserMetricsAction("AboutChrome"));
288 if (::switches::SettingsWindowEnabled()) {
289 SettingsWindowManager::GetInstance()->ShowChromePageForProfile(
290 browser->profile(), GURL(kChromeUIUberURL));
291 return;
292 }
293 NavigateParams params(
294 GetSingletonTabNavigateParams(browser, GURL(kChromeUIUberURL)));
295 params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
296 ShowSingletonTabOverwritingNTP(browser, params);
297 }
298
ShowSearchEngineSettings(Browser * browser)299 void ShowSearchEngineSettings(Browser* browser) {
300 content::RecordAction(UserMetricsAction("EditSearchEngines"));
301 ShowSettingsSubPage(browser, kSearchEnginesSubPage);
302 }
303
ShowBrowserSignin(Browser * browser,signin::Source source)304 void ShowBrowserSignin(Browser* browser, signin::Source source) {
305 Profile* original_profile = browser->profile()->GetOriginalProfile();
306 SigninManagerBase* manager =
307 SigninManagerFactory::GetForProfile(original_profile);
308 DCHECK(manager->IsSigninAllowed());
309 // If we're signed in, just show settings.
310 if (manager->IsAuthenticated()) {
311 ShowSettings(browser);
312 } else {
313 // If the browser's profile is an incognito profile, make sure to use
314 // a browser window from the original profile. The user cannot sign in
315 // from an incognito window.
316 scoped_ptr<ScopedTabbedBrowserDisplayer> displayer;
317 if (browser->profile()->IsOffTheRecord()) {
318 displayer.reset(new ScopedTabbedBrowserDisplayer(
319 original_profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
320 browser = displayer->browser();
321 }
322
323 NavigateToSingletonTab(browser, GURL(signin::GetPromoURL(source, false)));
324 DCHECK_GT(browser->tab_strip_model()->count(), 0);
325 }
326 }
327
328 } // namespace chrome
329