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/browser_about_handler.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/ui/browser_dialogs.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/url_constants.h"
19 #include "components/url_formatter/url_fixer.h"
21 bool FixupBrowserAboutURL(GURL
* url
,
22 content::BrowserContext
* browser_context
) {
23 // Ensure that any cleanup done by FixupURL happens before the rewriting
24 // phase that determines the virtual URL, by including it in an initial
25 // URLHandler. This prevents minor changes from producing a virtual URL,
26 // which could lead to a URL spoof.
27 *url
= url_formatter::FixupURL(url
->possibly_invalid_spec(), std::string());
31 bool WillHandleBrowserAboutURL(GURL
* url
,
32 content::BrowserContext
* browser_context
) {
33 // TODO(msw): Eliminate "about:*" constants and literals from code and tests,
34 // then hopefully we can remove this forced fixup.
35 FixupBrowserAboutURL(url
, browser_context
);
37 // Check that about: URLs are fixed up to chrome: by url_formatter::FixupURL.
38 DCHECK((*url
== GURL(url::kAboutBlankURL
)) ||
39 !url
->SchemeIs(url::kAboutScheme
));
41 // Only handle chrome://foo/, url_formatter::FixupURL translates about:foo.
42 if (!url
->SchemeIs(content::kChromeUIScheme
))
45 std::string
host(url
->host());
47 // Replace about with chrome-urls.
48 if (host
== chrome::kChromeUIAboutHost
)
49 host
= chrome::kChromeUIChromeURLsHost
;
50 // Replace cache with view-http-cache.
51 if (host
== chrome::kChromeUICacheHost
) {
52 host
= content::kChromeUINetworkViewCacheHost
;
53 // Replace sync with sync-internals (for legacy reasons).
54 } else if (host
== chrome::kChromeUISyncHost
) {
55 host
= chrome::kChromeUISyncInternalsHost
;
56 // Redirect chrome://extensions.
57 } else if (host
== chrome::kChromeUIExtensionsHost
) {
58 host
= chrome::kChromeUIUberHost
;
59 path
= chrome::kChromeUIExtensionsHost
+ url
->path();
60 // Redirect chrome://settings/extensions (legacy URL).
61 } else if (host
== chrome::kChromeUISettingsHost
&&
62 url
->path() == std::string("/") + chrome::kExtensionsSubPage
) {
63 host
= chrome::kChromeUIUberHost
;
64 path
= chrome::kChromeUIExtensionsHost
;
65 // Redirect chrome://history.
66 } else if (host
== chrome::kChromeUIHistoryHost
) {
67 #if defined(OS_ANDROID)
68 // On Android, redirect directly to chrome://history-frame since
69 // uber page is unsupported.
70 host
= chrome::kChromeUIHistoryFrameHost
;
72 host
= chrome::kChromeUIUberHost
;
73 path
= chrome::kChromeUIHistoryHost
+ url
->path();
75 // Redirect chrome://settings
76 } else if (host
== chrome::kChromeUISettingsHost
) {
77 if (::switches::AboutInSettingsEnabled()) {
78 host
= chrome::kChromeUISettingsFrameHost
;
80 host
= chrome::kChromeUIUberHost
;
81 path
= chrome::kChromeUISettingsHost
+ url
->path();
83 // Redirect chrome://help
84 } else if (host
== chrome::kChromeUIHelpHost
) {
85 if (::switches::AboutInSettingsEnabled()) {
86 host
= chrome::kChromeUISettingsFrameHost
;
87 if (url
->path().empty() || url
->path() == "/")
88 path
= chrome::kChromeUIHelpHost
;
90 host
= chrome::kChromeUIUberHost
;
91 path
= chrome::kChromeUIHelpHost
+ url
->path();
95 GURL::Replacements replacements
;
96 replacements
.SetHostStr(host
);
98 replacements
.SetPathStr(path
);
99 *url
= url
->ReplaceComponents(replacements
);
101 // Having re-written the URL, make the chrome: handler process it.
105 bool HandleNonNavigationAboutURL(const GURL
& url
) {
106 const std::string
spec(url
.spec());
108 if (base::LowerCaseEqualsASCII(spec
, chrome::kChromeUIRestartURL
)) {
109 // Call AttemptRestart after chrome::Navigate() completes to avoid access of
110 // gtk objects after they are destroyed by BrowserWindowGtk::Close().
111 base::ThreadTaskRunnerHandle::Get()->PostTask(
112 FROM_HERE
, base::Bind(&chrome::AttemptRestart
));
114 } else if (base::LowerCaseEqualsASCII(spec
, chrome::kChromeUIQuitURL
)) {
115 base::ThreadTaskRunnerHandle::Get()->PostTask(
116 FROM_HERE
, base::Bind(&chrome::AttemptExit
));