[email protected] | f1f8639 | 2012-04-03 13:51:58 | [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 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 5 | #include "content/browser/browser_url_handler_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 7 | #include "base/strings/string_util.h" |
[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 8 | #include "content/browser/frame_host/debug_urls.h" |
[email protected] | d235345 | 2012-01-19 19:53:56 | [diff] [blame] | 9 | #include "content/browser/webui/web_ui_impl.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 10 | #include "content/public/browser/content_browser_client.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 11 | #include "content/public/common/url_constants.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 12 | #include "url/gurl.h" |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 13 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 14 | namespace content { |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 15 | |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 16 | // Handles rewriting view-source URLs for what we'll actually load. |
[email protected] | 4654bfe | 2013-08-26 03:36:58 | [diff] [blame] | 17 | static bool HandleViewSource(GURL* url, BrowserContext* browser_context) { |
[email protected] | dbdda540 | 2013-05-30 22:13:48 | [diff] [blame] | 18 | if (url->SchemeIs(kViewSourceScheme)) { |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 19 | // Load the inner URL instead. |
[email protected] | 5f50c5d | 2013-10-24 19:05:17 | [diff] [blame] | 20 | *url = GURL(url->GetContent()); |
[email protected] | a5eb0bb | 2009-10-29 02:14:17 | [diff] [blame] | 21 | |
| 22 | // Bug 26129: limit view-source to view the content and not any |
| 23 | // other kind of 'active' url scheme like 'javascript' or 'data'. |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 24 | static const char* const default_allowed_sub_schemes[] = { |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 25 | url::kHttpScheme, |
| 26 | url::kHttpsScheme, |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 27 | url::kFtpScheme, |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 28 | kChromeDevToolsScheme, |
| 29 | kChromeUIScheme, |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 30 | url::kFileScheme, |
| 31 | url::kFileSystemScheme |
[email protected] | a5eb0bb | 2009-10-29 02:14:17 | [diff] [blame] | 32 | }; |
| 33 | |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 34 | // Merge all the schemes for which view-source is allowed by default, with |
| 35 | // the WebUI schemes defined by the ContentBrowserClient. |
| 36 | std::vector<std::string> all_allowed_sub_schemes; |
| 37 | for (size_t i = 0; i < arraysize(default_allowed_sub_schemes); ++i) |
| 38 | all_allowed_sub_schemes.push_back(default_allowed_sub_schemes[i]); |
| 39 | GetContentClient()->browser()->GetAdditionalWebUISchemes( |
| 40 | &all_allowed_sub_schemes); |
| 41 | |
[email protected] | a5eb0bb | 2009-10-29 02:14:17 | [diff] [blame] | 42 | bool is_sub_scheme_allowed = false; |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 43 | for (size_t i = 0; i < all_allowed_sub_schemes.size(); ++i) { |
| 44 | if (url->SchemeIs(all_allowed_sub_schemes[i].c_str())) { |
[email protected] | a5eb0bb | 2009-10-29 02:14:17 | [diff] [blame] | 45 | is_sub_scheme_allowed = true; |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (!is_sub_scheme_allowed) { |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 51 | *url = GURL(url::kAboutBlankURL); |
[email protected] | a5eb0bb | 2009-10-29 02:14:17 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 60 | // Turns a non view-source URL into the corresponding view-source URL. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 61 | static bool ReverseViewSource(GURL* url, BrowserContext* browser_context) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 62 | // No action necessary if the URL is already view-source: |
[email protected] | dbdda540 | 2013-05-30 22:13:48 | [diff] [blame] | 63 | if (url->SchemeIs(kViewSourceScheme)) |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 64 | return false; |
sgurun | 939a726c | 2014-10-15 20:21:18 | [diff] [blame] | 65 | // Recreate the url with the view-source scheme. |
| 66 | *url = GURL(kViewSourceScheme + std::string(":") + url->spec()); |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
[email protected] | 2abef1a | 2013-10-17 23:31:16 | [diff] [blame] | 70 | static bool DebugURLHandler(GURL* url, BrowserContext* browser_context) { |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 71 | // Circumvent processing URLs that the renderer process will handle. |
[email protected] | fe0d673 | 2014-02-13 00:51:50 | [diff] [blame] | 72 | return IsRendererDebugURL(*url); |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 75 | // static |
| 76 | BrowserURLHandler* BrowserURLHandler::GetInstance() { |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 77 | return BrowserURLHandlerImpl::GetInstance(); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 78 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | |
| 80 | // static |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 81 | BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() { |
[email protected] | f79f486 | 2012-02-16 16:52:42 | [diff] [blame] | 82 | // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 83 | return NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 86 | // static |
| 87 | BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 88 | return base::Singleton<BrowserURLHandlerImpl>::get(); |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 89 | } |
| 90 | |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 91 | BrowserURLHandlerImpl::BrowserURLHandlerImpl() : |
creis | 5c0d01db | 2015-04-24 22:16:29 | [diff] [blame] | 92 | fixup_handler_(nullptr) { |
[email protected] | 2abef1a | 2013-10-17 23:31:16 | [diff] [blame] | 93 | AddHandlerPair(&DebugURLHandler, BrowserURLHandlerImpl::null_handler()); |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 94 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 95 | GetContentClient()->browser()->BrowserURLHandlerCreated(this); |
[email protected] | b8148ac | 2011-07-13 22:03:25 | [diff] [blame] | 96 | |
| 97 | // view-source: |
| 98 | AddHandlerPair(&HandleViewSource, &ReverseViewSource); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 101 | BrowserURLHandlerImpl::~BrowserURLHandlerImpl() { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 102 | } |
| 103 | |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 104 | void BrowserURLHandlerImpl::SetFixupHandler(URLHandler handler) { |
creis | 5c0d01db | 2015-04-24 22:16:29 | [diff] [blame] | 105 | DCHECK(fixup_handler_ == nullptr); |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 106 | fixup_handler_ = handler; |
| 107 | } |
| 108 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 109 | void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler, |
| 110 | URLHandler reverse_handler) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 111 | url_handlers_.push_back(HandlerPair(handler, reverse_handler)); |
| 112 | } |
| 113 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 114 | void BrowserURLHandlerImpl::RewriteURLIfNecessary( |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 115 | GURL* url, |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 116 | BrowserContext* browser_context, |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 117 | bool* reverse_on_redirect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | for (size_t i = 0; i < url_handlers_.size(); ++i) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 119 | URLHandler handler = *url_handlers_[i].first; |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 120 | if (handler && handler(url, browser_context)) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 121 | *reverse_on_redirect = (url_handlers_[i].second != NULL); |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 122 | return; |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 123 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | } |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 126 | |
creis | 94a977f6 | 2015-02-18 23:51:05 | [diff] [blame] | 127 | void BrowserURLHandlerImpl::FixupURLBeforeRewrite( |
| 128 | GURL* url, |
| 129 | BrowserContext* browser_context) { |
| 130 | if (fixup_handler_) |
| 131 | fixup_handler_(url, browser_context); |
| 132 | } |
| 133 | |
[email protected] | d9c2e51 | 2012-10-25 18:54:36 | [diff] [blame] | 134 | bool BrowserURLHandlerImpl::ReverseURLRewrite( |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 135 | GURL* url, const GURL& original, BrowserContext* browser_context) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 136 | for (size_t i = 0; i < url_handlers_.size(); ++i) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 137 | URLHandler reverse_rewriter = *url_handlers_[i].second; |
| 138 | if (reverse_rewriter) { |
| 139 | GURL test_url(original); |
| 140 | URLHandler handler = *url_handlers_[i].first; |
| 141 | if (!handler) { |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 142 | if (reverse_rewriter(url, browser_context)) |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 143 | return true; |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 144 | } else if (handler(&test_url, browser_context)) { |
| 145 | return reverse_rewriter(url, browser_context); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 146 | } |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | return false; |
| 150 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 151 | |
| 152 | } // namespace content |