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/command_line.h"
8 #include "base/strings/string_util.h"
9 #include "cc/base/switches.h"
10 #include "content/browser/frame_host/debug_urls.h"
11 #include "content/browser/webui/web_ui_impl.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/common/url_constants.h"
18 // Handles rewriting view-source URLs for what we'll actually load.
19 static bool HandleViewSource(GURL
* url
, BrowserContext
* browser_context
) {
20 if (url
->SchemeIs(kViewSourceScheme
)) {
21 // Load the inner URL instead.
22 *url
= GURL(url
->GetContent());
24 // Bug 26129: limit view-source to view the content and not any
25 // other kind of 'active' url scheme like 'javascript' or 'data'.
26 static const char* const default_allowed_sub_schemes
[] = {
30 kChromeDevToolsScheme
,
36 // Merge all the schemes for which view-source is allowed by default, with
37 // the WebUI schemes defined by the ContentBrowserClient.
38 std::vector
<std::string
> all_allowed_sub_schemes
;
39 for (size_t i
= 0; i
< arraysize(default_allowed_sub_schemes
); ++i
)
40 all_allowed_sub_schemes
.push_back(default_allowed_sub_schemes
[i
]);
41 GetContentClient()->browser()->GetAdditionalWebUISchemes(
42 &all_allowed_sub_schemes
);
44 bool is_sub_scheme_allowed
= false;
45 for (size_t i
= 0; i
< all_allowed_sub_schemes
.size(); ++i
) {
46 if (url
->SchemeIs(all_allowed_sub_schemes
[i
].c_str())) {
47 is_sub_scheme_allowed
= true;
52 if (!is_sub_scheme_allowed
) {
53 *url
= GURL(kAboutBlankURL
);
62 // Turns a non view-source URL into the corresponding view-source URL.
63 static bool ReverseViewSource(GURL
* url
, BrowserContext
* browser_context
) {
64 // No action necessary if the URL is already view-source:
65 if (url
->SchemeIs(kViewSourceScheme
))
68 url_canon::Replacements
<char> repl
;
69 repl
.SetScheme(kViewSourceScheme
,
70 url_parse::Component(0, strlen(kViewSourceScheme
)));
71 repl
.SetPath(url
->spec().c_str(),
72 url_parse::Component(0, url
->spec().size()));
73 *url
= url
->ReplaceComponents(repl
);
77 static bool DebugURLHandler(GURL
* url
, BrowserContext
* browser_context
) {
78 // If running inside the Telemetry test harness, allow automated
79 // navigations to access browser-side debug URLs. They must use the
80 // chrome:// scheme, since the about: scheme won't be rewritten in
82 if (CommandLine::ForCurrentProcess()->HasSwitch(
83 cc::switches::kEnableGpuBenchmarking
)) {
84 if (HandleDebugURL(*url
, PAGE_TRANSITION_FROM_ADDRESS_BAR
)) {
89 // Circumvent processing URLs that the renderer process will handle.
90 return IsRendererDebugURL(*url
);
94 BrowserURLHandler
* BrowserURLHandler::GetInstance() {
95 return BrowserURLHandlerImpl::GetInstance();
99 BrowserURLHandler::URLHandler
BrowserURLHandler::null_handler() {
100 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
105 BrowserURLHandlerImpl
* BrowserURLHandlerImpl::GetInstance() {
106 return Singleton
<BrowserURLHandlerImpl
>::get();
109 BrowserURLHandlerImpl::BrowserURLHandlerImpl() {
110 AddHandlerPair(&DebugURLHandler
, BrowserURLHandlerImpl::null_handler());
112 GetContentClient()->browser()->BrowserURLHandlerCreated(this);
115 AddHandlerPair(&HandleViewSource
, &ReverseViewSource
);
118 BrowserURLHandlerImpl::~BrowserURLHandlerImpl() {
121 void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler
,
122 URLHandler reverse_handler
) {
123 url_handlers_
.push_back(HandlerPair(handler
, reverse_handler
));
126 void BrowserURLHandlerImpl::RewriteURLIfNecessary(
128 BrowserContext
* browser_context
,
129 bool* reverse_on_redirect
) {
130 for (size_t i
= 0; i
< url_handlers_
.size(); ++i
) {
131 URLHandler handler
= *url_handlers_
[i
].first
;
132 if (handler
&& handler(url
, browser_context
)) {
133 *reverse_on_redirect
= (url_handlers_
[i
].second
!= NULL
);
139 bool BrowserURLHandlerImpl::ReverseURLRewrite(
140 GURL
* url
, const GURL
& original
, BrowserContext
* browser_context
) {
141 for (size_t i
= 0; i
< url_handlers_
.size(); ++i
) {
142 URLHandler reverse_rewriter
= *url_handlers_
[i
].second
;
143 if (reverse_rewriter
) {
144 GURL
test_url(original
);
145 URLHandler handler
= *url_handlers_
[i
].first
;
147 if (reverse_rewriter(url
, browser_context
))
149 } else if (handler(&test_url
, browser_context
)) {
150 return reverse_rewriter(url
, browser_context
);
157 } // namespace content