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/check.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser_dialogs.h"
18 #include "chrome/common/chrome_features.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/url_constants.h"
21 #include "components/url_formatter/url_fixer.h"
22 #include "content/public/common/content_features.h"
23 #include "extensions/buildflags/buildflags.h"
25 bool FixupBrowserAboutURL(GURL* url,
26 content::BrowserContext* browser_context) {
27 // Ensure that any cleanup done by FixupURL happens before the rewriting
28 // phase that determines the virtual URL, by including it in an initial
29 // URLHandler. This prevents minor changes from producing a virtual URL,
30 // which could lead to a URL spoof.
31 *url = url_formatter::FixupURL(url->possibly_invalid_spec(), std::string());
35 bool WillHandleBrowserAboutURL(GURL* url,
36 content::BrowserContext* browser_context) {
37 // TODO(msw): Eliminate "about:*" constants and literals from code and tests,
38 // then hopefully we can remove this forced fixup.
39 FixupBrowserAboutURL(url, browser_context);
41 // Check that about: URLs are fixed up to chrome: by url_formatter::FixupURL.
42 DCHECK(url->IsAboutBlank() || url->IsAboutSrcdoc() ||
43 !url->SchemeIs(url::kAboutScheme));
45 // Only handle chrome://foo/, url_formatter::FixupURL translates about:foo.
46 if (!url->SchemeIs(content::kChromeUIScheme))
49 std::string host(url->host());
52 #if !defined(OS_CHROMEOS)
53 // Handle chrome://settings.
54 if (host == chrome::kChromeUISettingsHost)
55 return true; // Prevent further rewriting - this is a valid URL.
56 #endif // !defined(OS_CHROMEOS)
58 // Do not handle chrome://help.
59 if (host == chrome::kChromeUIHelpHost)
60 return false; // Handled in the HandleWebUI handler.
62 // Replace about with chrome-urls.
63 if (host == chrome::kChromeUIAboutHost)
64 host = chrome::kChromeUIChromeURLsHost;
66 if (host == chrome::kChromeUISyncHost) {
67 // Replace sync with sync-internals (for legacy reasons).
68 host = chrome::kChromeUISyncInternalsHost;
69 } else if (host == chrome::kChromeUIHistoryHost) {
70 // Redirect chrome://history.
74 GURL::Replacements replacements;
75 replacements.SetHostStr(host);
77 replacements.SetPathStr(path);
78 *url = url->ReplaceComponents(replacements);
80 // Having re-written the URL, make the chrome: handler process it.
84 bool HandleNonNavigationAboutURL(const GURL& url) {
85 const std::string spec(url.spec());
87 if (base::LowerCaseEqualsASCII(spec, chrome::kChromeUIRestartURL)) {
88 // Call AttemptRestart after chrome::Navigate() completes to avoid access of
89 // gtk objects after they are destroyed by BrowserWindowGtk::Close().
90 base::ThreadTaskRunnerHandle::Get()->PostTask(
91 FROM_HERE, base::BindOnce(&chrome::AttemptRestart));
94 if (base::LowerCaseEqualsASCII(spec, chrome::kChromeUIQuitURL)) {
95 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE, base::BindOnce(&chrome::AttemptExit));