blob: 4f1ed350da61946db3bfa873385c94ba27449d23 [file] [log] [blame]
[email protected]f1f86392012-04-03 13:51:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]825b1662012-03-12 19:07:315#include "content/browser/browser_url_handler_impl.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]10994d132013-06-11 07:16:187#include "base/strings/string_util.h"
[email protected]d4a8ca482013-10-30 21:06:408#include "content/browser/frame_host/debug_urls.h"
[email protected]d2353452012-01-19 19:53:569#include "content/browser/webui/web_ui_impl.h"
[email protected]87f3c082011-10-19 18:07:4410#include "content/public/browser/content_browser_client.h"
[email protected]a1d29162011-10-14 17:14:0311#include "content/public/common/url_constants.h"
[email protected]707e1c42013-07-09 21:18:5812#include "url/gurl.h"
[email protected]cd3d7892009-03-04 23:55:0613
[email protected]46488322012-10-30 03:22:2014namespace content {
[email protected]825b1662012-03-12 19:07:3115
[email protected]cd3d7892009-03-04 23:55:0616// Handles rewriting view-source URLs for what we'll actually load.
[email protected]4654bfe2013-08-26 03:36:5817static bool HandleViewSource(GURL* url, BrowserContext* browser_context) {
[email protected]dbdda5402013-05-30 22:13:4818 if (url->SchemeIs(kViewSourceScheme)) {
[email protected]cd3d7892009-03-04 23:55:0619 // Load the inner URL instead.
[email protected]5f50c5d2013-10-24 19:05:1720 *url = GURL(url->GetContent());
[email protected]a5eb0bb2009-10-29 02:14:1721
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]73b718f2014-01-27 02:59:4624 static const char* const default_allowed_sub_schemes[] = {
[email protected]e8ca69c2014-05-07 15:31:1925 url::kHttpScheme,
26 url::kHttpsScheme,
[email protected]cca6f392014-05-28 21:32:2627 url::kFtpScheme,
[email protected]e8ca69c2014-05-07 15:31:1928 kChromeDevToolsScheme,
29 kChromeUIScheme,
[email protected]cca6f392014-05-28 21:32:2630 url::kFileScheme,
31 url::kFileSystemScheme
[email protected]a5eb0bb2009-10-29 02:14:1732 };
33
[email protected]73b718f2014-01-27 02:59:4634 // 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]a5eb0bb2009-10-29 02:14:1742 bool is_sub_scheme_allowed = false;
[email protected]73b718f2014-01-27 02:59:4643 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]a5eb0bb2009-10-29 02:14:1745 is_sub_scheme_allowed = true;
46 break;
47 }
48 }
49
50 if (!is_sub_scheme_allowed) {
[email protected]8e09c7af2014-06-10 11:46:1751 *url = GURL(url::kAboutBlankURL);
[email protected]a5eb0bb2009-10-29 02:14:1752 return false;
53 }
54
[email protected]cd3d7892009-03-04 23:55:0655 return true;
56 }
57 return false;
58}
initial.commit09911bf2008-07-26 23:55:2959
[email protected]38178a42009-12-17 18:58:3260// Turns a non view-source URL into the corresponding view-source URL.
[email protected]46488322012-10-30 03:22:2061static bool ReverseViewSource(GURL* url, BrowserContext* browser_context) {
[email protected]38178a42009-12-17 18:58:3262 // No action necessary if the URL is already view-source:
[email protected]dbdda5402013-05-30 22:13:4863 if (url->SchemeIs(kViewSourceScheme))
[email protected]38178a42009-12-17 18:58:3264 return false;
sgurun939a726c2014-10-15 20:21:1865 // Recreate the url with the view-source scheme.
66 *url = GURL(kViewSourceScheme + std::string(":") + url->spec());
[email protected]38178a42009-12-17 18:58:3267 return true;
68}
69
[email protected]2abef1a2013-10-17 23:31:1670static bool DebugURLHandler(GURL* url, BrowserContext* browser_context) {
[email protected]8bf1048012012-02-08 01:22:1871 // Circumvent processing URLs that the renderer process will handle.
[email protected]fe0d6732014-02-13 00:51:5072 return IsRendererDebugURL(*url);
[email protected]8bf1048012012-02-08 01:22:1873}
74
[email protected]f1eb87a2011-05-06 17:49:4175// static
76BrowserURLHandler* BrowserURLHandler::GetInstance() {
[email protected]825b1662012-03-12 19:07:3177 return BrowserURLHandlerImpl::GetInstance();
[email protected]f1eb87a2011-05-06 17:49:4178}
initial.commit09911bf2008-07-26 23:55:2979
80// static
[email protected]f1eb87a2011-05-06 17:49:4181BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() {
[email protected]f79f4862012-02-16 16:52:4282 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
[email protected]f1eb87a2011-05-06 17:49:4183 return NULL;
initial.commit09911bf2008-07-26 23:55:2984}
85
[email protected]825b1662012-03-12 19:07:3186// static
87BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:2288 return base::Singleton<BrowserURLHandlerImpl>::get();
[email protected]825b1662012-03-12 19:07:3189}
90
creis94a977f62015-02-18 23:51:0591BrowserURLHandlerImpl::BrowserURLHandlerImpl() :
creis5c0d01db2015-04-24 22:16:2992 fixup_handler_(nullptr) {
[email protected]2abef1a2013-10-17 23:31:1693 AddHandlerPair(&DebugURLHandler, BrowserURLHandlerImpl::null_handler());
[email protected]8bf1048012012-02-08 01:22:1894
[email protected]46488322012-10-30 03:22:2095 GetContentClient()->browser()->BrowserURLHandlerCreated(this);
[email protected]b8148ac2011-07-13 22:03:2596
97 // view-source:
98 AddHandlerPair(&HandleViewSource, &ReverseViewSource);
[email protected]f1eb87a2011-05-06 17:49:4199}
100
[email protected]825b1662012-03-12 19:07:31101BrowserURLHandlerImpl::~BrowserURLHandlerImpl() {
[email protected]f1eb87a2011-05-06 17:49:41102}
103
creis94a977f62015-02-18 23:51:05104void BrowserURLHandlerImpl::SetFixupHandler(URLHandler handler) {
creis5c0d01db2015-04-24 22:16:29105 DCHECK(fixup_handler_ == nullptr);
creis94a977f62015-02-18 23:51:05106 fixup_handler_ = handler;
107}
108
[email protected]825b1662012-03-12 19:07:31109void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler,
110 URLHandler reverse_handler) {
[email protected]f1eb87a2011-05-06 17:49:41111 url_handlers_.push_back(HandlerPair(handler, reverse_handler));
112}
113
[email protected]825b1662012-03-12 19:07:31114void BrowserURLHandlerImpl::RewriteURLIfNecessary(
[email protected]3d7474ff2011-07-27 17:47:37115 GURL* url,
[email protected]46488322012-10-30 03:22:20116 BrowserContext* browser_context,
[email protected]3d7474ff2011-07-27 17:47:37117 bool* reverse_on_redirect) {
initial.commit09911bf2008-07-26 23:55:29118 for (size_t i = 0; i < url_handlers_.size(); ++i) {
[email protected]f1eb87a2011-05-06 17:49:41119 URLHandler handler = *url_handlers_[i].first;
[email protected]3d7474ff2011-07-27 17:47:37120 if (handler && handler(url, browser_context)) {
[email protected]38178a42009-12-17 18:58:32121 *reverse_on_redirect = (url_handlers_[i].second != NULL);
[email protected]7f0005a2009-04-15 03:25:11122 return;
[email protected]38178a42009-12-17 18:58:32123 }
initial.commit09911bf2008-07-26 23:55:29124 }
initial.commit09911bf2008-07-26 23:55:29125}
[email protected]38178a42009-12-17 18:58:32126
creis94a977f62015-02-18 23:51:05127void BrowserURLHandlerImpl::FixupURLBeforeRewrite(
128 GURL* url,
129 BrowserContext* browser_context) {
130 if (fixup_handler_)
131 fixup_handler_(url, browser_context);
132}
133
[email protected]d9c2e512012-10-25 18:54:36134bool BrowserURLHandlerImpl::ReverseURLRewrite(
[email protected]46488322012-10-30 03:22:20135 GURL* url, const GURL& original, BrowserContext* browser_context) {
[email protected]38178a42009-12-17 18:58:32136 for (size_t i = 0; i < url_handlers_.size(); ++i) {
[email protected]f1eb87a2011-05-06 17:49:41137 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]3d7474ff2011-07-27 17:47:37142 if (reverse_rewriter(url, browser_context))
[email protected]f1eb87a2011-05-06 17:49:41143 return true;
[email protected]3d7474ff2011-07-27 17:47:37144 } else if (handler(&test_url, browser_context)) {
145 return reverse_rewriter(url, browser_context);
[email protected]f1eb87a2011-05-06 17:49:41146 }
[email protected]38178a42009-12-17 18:58:32147 }
148 }
149 return false;
150}
[email protected]46488322012-10-30 03:22:20151
152} // namespace content