[email protected] | 1640a85 | 2012-01-05 21:05:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/browser_about_handler.h" |
| 6 | |
| 7 | #include <string> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
andresantoso | 063151d | 2014-12-18 22:37:55 | [diff] [blame] | 9 | #include "base/bind.h" |
Hans Wennborg | 1790e6b | 2020-04-24 19:10:33 | [diff] [blame] | 10 | #include "base/check.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 11 | #include "base/location.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
[email protected] | e746341 | 2013-06-10 22:53:46 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | c5955d9 | 2013-07-02 00:14:16 | [diff] [blame] | 15 | #include "chrome/browser/lifetime/application_lifetime.h" |
calamity | 20fc2af | 2016-06-09 02:02:58 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profile.h" |
[email protected] | c96531e8 | 2011-01-26 03:11:14 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser_dialogs.h" |
ddoman | 61ec142 | 2016-03-17 05:03:42 | [diff] [blame] | 18 | #include "chrome/common/chrome_features.h" |
[email protected] | 727903e1 | 2014-08-14 16:51:26 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 20 | #include "chrome/common/url_constants.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 21 | #include "components/url_formatter/url_fixer.h" |
twellington | a0d282e | 2017-02-07 16:36:39 | [diff] [blame] | 22 | #include "content/public/common/content_features.h" |
Scott Violet | c8240b0 | 2018-03-08 22:03:59 | [diff] [blame] | 23 | #include "extensions/buildflags/buildflags.h" |
[email protected] | 18bc42c9 | 2009-02-04 18:59:09 | [diff] [blame] | 24 | |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 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. |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 31 | *url = url_formatter::FixupURL(url->possibly_invalid_spec(), std::string()); |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 32 | return true; |
| 33 | } |
| 34 | |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 35 | bool WillHandleBrowserAboutURL(GURL* url, |
| 36 | content::BrowserContext* browser_context) { |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 37 | // TODO(msw): Eliminate "about:*" constants and literals from code and tests, |
| 38 | // then hopefully we can remove this forced fixup. |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 39 | FixupBrowserAboutURL(url, browser_context); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 40 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 41 | // Check that about: URLs are fixed up to chrome: by url_formatter::FixupURL. |
Lukasz Anforowicz | 0bc073e | 2019-06-14 19:41:52 | [diff] [blame] | 42 | DCHECK(url->IsAboutBlank() || url->IsAboutSrcdoc() || |
| 43 | !url->SchemeIs(url::kAboutScheme)); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 44 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 45 | // Only handle chrome://foo/, url_formatter::FixupURL translates about:foo. |
[email protected] | 2d9748b2 | 2014-02-11 00:17:29 | [diff] [blame] | 46 | if (!url->SchemeIs(content::kChromeUIScheme)) |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 47 | return false; |
| 48 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 49 | std::string host(url->host()); |
[email protected] | dd86ba30 | 2011-09-19 20:10:37 | [diff] [blame] | 50 | std::string path; |
dbeam | 25b548f | 2017-05-05 18:05:24 | [diff] [blame] | 51 | |
sauski | b5f8276 | 2020-07-21 17:13:22 | [diff] [blame] | 52 | // Do not handle chrome://settings rewrites, they are handled in the |
| 53 | // HandleWebUI handler. |
thestig | 76cc70a | 2017-06-07 05:29:43 | [diff] [blame] | 54 | if (host == chrome::kChromeUISettingsHost) |
sauski | b5f8276 | 2020-07-21 17:13:22 | [diff] [blame] | 55 | return false; |
thestig | 76cc70a | 2017-06-07 05:29:43 | [diff] [blame] | 56 | |
| 57 | // Do not handle chrome://help. |
| 58 | if (host == chrome::kChromeUIHelpHost) |
| 59 | return false; // Handled in the HandleWebUI handler. |
| 60 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 61 | // Replace about with chrome-urls. |
| 62 | if (host == chrome::kChromeUIAboutHost) |
| 63 | host = chrome::kChromeUIChromeURLsHost; |
dbeam | 25b548f | 2017-05-05 18:05:24 | [diff] [blame] | 64 | |
Eric Roman | 6ebc11f | 2018-02-21 19:32:24 | [diff] [blame] | 65 | if (host == chrome::kChromeUISyncHost) { |
| 66 | // Replace sync with sync-internals (for legacy reasons). |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 67 | host = chrome::kChromeUISyncInternalsHost; |
thestig | c839b55 | 2017-03-23 02:38:56 | [diff] [blame] | 68 | } else if (host == chrome::kChromeUIHistoryHost) { |
dbeam | e1a5fa5 | 2017-06-07 02:27:37 | [diff] [blame] | 69 | // Redirect chrome://history. |
dbeam | 25b548f | 2017-05-05 18:05:24 | [diff] [blame] | 70 | path = url->path(); |
[email protected] | dd86ba30 | 2011-09-19 20:10:37 | [diff] [blame] | 71 | } |
[email protected] | eb04af8 | 2013-09-04 00:49:29 | [diff] [blame] | 72 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 73 | GURL::Replacements replacements; |
| 74 | replacements.SetHostStr(host); |
[email protected] | dd86ba30 | 2011-09-19 20:10:37 | [diff] [blame] | 75 | if (!path.empty()) |
| 76 | replacements.SetPathStr(path); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 77 | *url = url->ReplaceComponents(replacements); |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 78 | |
[email protected] | b3adbd0 | 2011-11-30 22:23:27 | [diff] [blame] | 79 | // Having re-written the URL, make the chrome: handler process it. |
| 80 | return false; |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 83 | bool HandleNonNavigationAboutURL(const GURL& url) { |
avi | 861ff75 | 2014-09-23 22:55:33 | [diff] [blame] | 84 | const std::string spec(url.spec()); |
[email protected] | 594458a | 2013-12-05 12:40:24 | [diff] [blame] | 85 | |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 86 | if (base::LowerCaseEqualsASCII(spec, chrome::kChromeUIRestartURL)) { |
[email protected] | 594458a | 2013-12-05 12:40:24 | [diff] [blame] | 87 | // Call AttemptRestart after chrome::Navigate() completes to avoid access of |
| 88 | // gtk objects after they are destroyed by BrowserWindowGtk::Close(). |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 89 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 90 | FROM_HERE, base::BindOnce(&chrome::AttemptRestart)); |
[email protected] | 594458a | 2013-12-05 12:40:24 | [diff] [blame] | 91 | return true; |
thestig | 76cc70a | 2017-06-07 05:29:43 | [diff] [blame] | 92 | } |
| 93 | if (base::LowerCaseEqualsASCII(spec, chrome::kChromeUIQuitURL)) { |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 94 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 29ea5c7 | 2017-04-20 02:16:51 | [diff] [blame] | 95 | FROM_HERE, base::BindOnce(&chrome::AttemptExit)); |
[email protected] | 594458a | 2013-12-05 12:40:24 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 99 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | } |