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 "content/browser/browser_url_handler_impl.h"
7 #include "base/strings/string_util.h"
8 #include "content/browser/webui/web_ui_impl.h"
9 #include "content/public/browser/content_browser_client.h"
10 #include "content/public/common/url_constants.h"
15 // Handles rewriting view-source URLs for what we'll actually load.
16 static bool HandleViewSource(GURL
* url
, BrowserContext
* browser_context
) {
17 if (url
->SchemeIs(kViewSourceScheme
)) {
18 // Load the inner URL instead.
19 *url
= GURL(url
->path());
21 // Bug 26129: limit view-source to view the content and not any
22 // other kind of 'active' url scheme like 'javascript' or 'data'.
23 static const char* const allowed_sub_schemes
[] = {
24 kHttpScheme
, kHttpsScheme
, chrome::kFtpScheme
,
25 chrome::kChromeDevToolsScheme
, chrome::kChromeUIScheme
,
26 chrome::kFileScheme
, chrome::kFileSystemScheme
29 bool is_sub_scheme_allowed
= false;
30 for (size_t i
= 0; i
< arraysize(allowed_sub_schemes
); i
++) {
31 if (url
->SchemeIs(allowed_sub_schemes
[i
])) {
32 is_sub_scheme_allowed
= true;
37 if (!is_sub_scheme_allowed
) {
38 *url
= GURL(kAboutBlankURL
);
47 // Turns a non view-source URL into the corresponding view-source URL.
48 static bool ReverseViewSource(GURL
* url
, BrowserContext
* browser_context
) {
49 // No action necessary if the URL is already view-source:
50 if (url
->SchemeIs(kViewSourceScheme
))
53 url_canon::Replacements
<char> repl
;
54 repl
.SetScheme(kViewSourceScheme
,
55 url_parse::Component(0, strlen(kViewSourceScheme
)));
56 repl
.SetPath(url
->spec().c_str(),
57 url_parse::Component(0, url
->spec().size()));
58 *url
= url
->ReplaceComponents(repl
);
62 static bool HandleDebugUrl(GURL
* url
, BrowserContext
* browser_context
) {
63 // Circumvent processing URLs that the renderer process will handle.
64 return *url
== GURL(kChromeUICrashURL
) ||
65 *url
== GURL(kChromeUIHangURL
) ||
66 *url
== GURL(kChromeUIKillURL
) ||
67 *url
== GURL(kChromeUIShorthangURL
);
71 BrowserURLHandler
* BrowserURLHandler::GetInstance() {
72 return BrowserURLHandlerImpl::GetInstance();
76 BrowserURLHandler::URLHandler
BrowserURLHandler::null_handler() {
77 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
82 BrowserURLHandlerImpl
* BrowserURLHandlerImpl::GetInstance() {
83 return Singleton
<BrowserURLHandlerImpl
>::get();
86 BrowserURLHandlerImpl::BrowserURLHandlerImpl() {
87 AddHandlerPair(&HandleDebugUrl
, BrowserURLHandlerImpl::null_handler());
89 GetContentClient()->browser()->BrowserURLHandlerCreated(this);
92 AddHandlerPair(&HandleViewSource
, &ReverseViewSource
);
95 BrowserURLHandlerImpl::~BrowserURLHandlerImpl() {
98 void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler
,
99 URLHandler reverse_handler
) {
100 url_handlers_
.push_back(HandlerPair(handler
, reverse_handler
));
103 void BrowserURLHandlerImpl::RewriteURLIfNecessary(
105 BrowserContext
* browser_context
,
106 bool* reverse_on_redirect
) {
107 for (size_t i
= 0; i
< url_handlers_
.size(); ++i
) {
108 URLHandler handler
= *url_handlers_
[i
].first
;
109 if (handler
&& handler(url
, browser_context
)) {
110 *reverse_on_redirect
= (url_handlers_
[i
].second
!= NULL
);
116 bool BrowserURLHandlerImpl::ReverseURLRewrite(
117 GURL
* url
, const GURL
& original
, BrowserContext
* browser_context
) {
118 for (size_t i
= 0; i
< url_handlers_
.size(); ++i
) {
119 URLHandler reverse_rewriter
= *url_handlers_
[i
].second
;
120 if (reverse_rewriter
) {
121 GURL
test_url(original
);
122 URLHandler handler
= *url_handlers_
[i
].first
;
124 if (reverse_rewriter(url
, browser_context
))
126 } else if (handler(&test_url
, browser_context
)) {
127 return reverse_rewriter(url
, browser_context
);
134 } // namespace content