Generate ax enums from idl.
[chromium-blink-merge.git] / content / browser / browser_url_handler_impl.cc
blob10a5c8838f47446d5209c5f1a543a881f7518b8a
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"
14 #include "url/gurl.h"
16 namespace content {
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[] = {
27 kHttpScheme, kHttpsScheme, kFtpScheme,
28 chrome::kChromeDevToolsScheme, chrome::kChromeUIScheme,
29 kFileScheme, kFileSystemScheme
32 // Merge all the schemes for which view-source is allowed by default, with
33 // the WebUI schemes defined by the ContentBrowserClient.
34 std::vector<std::string> all_allowed_sub_schemes;
35 for (size_t i = 0; i < arraysize(default_allowed_sub_schemes); ++i)
36 all_allowed_sub_schemes.push_back(default_allowed_sub_schemes[i]);
37 GetContentClient()->browser()->GetAdditionalWebUISchemes(
38 &all_allowed_sub_schemes);
40 bool is_sub_scheme_allowed = false;
41 for (size_t i = 0; i < all_allowed_sub_schemes.size(); ++i) {
42 if (url->SchemeIs(all_allowed_sub_schemes[i].c_str())) {
43 is_sub_scheme_allowed = true;
44 break;
48 if (!is_sub_scheme_allowed) {
49 *url = GURL(kAboutBlankURL);
50 return false;
53 return true;
55 return false;
58 // Turns a non view-source URL into the corresponding view-source URL.
59 static bool ReverseViewSource(GURL* url, BrowserContext* browser_context) {
60 // No action necessary if the URL is already view-source:
61 if (url->SchemeIs(kViewSourceScheme))
62 return false;
64 url_canon::Replacements<char> repl;
65 repl.SetScheme(kViewSourceScheme,
66 url_parse::Component(0, strlen(kViewSourceScheme)));
67 repl.SetPath(url->spec().c_str(),
68 url_parse::Component(0, url->spec().size()));
69 *url = url->ReplaceComponents(repl);
70 return true;
73 static bool DebugURLHandler(GURL* url, BrowserContext* browser_context) {
74 // If running inside the Telemetry test harness, allow automated
75 // navigations to access browser-side debug URLs. They must use the
76 // chrome:// scheme, since the about: scheme won't be rewritten in
77 // this code path.
78 if (CommandLine::ForCurrentProcess()->HasSwitch(
79 cc::switches::kEnableGpuBenchmarking)) {
80 if (HandleDebugURL(*url, PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
81 return true;
85 // Circumvent processing URLs that the renderer process will handle.
86 return *url == GURL(kChromeUICrashURL) ||
87 *url == GURL(kChromeUIHangURL) ||
88 *url == GURL(kChromeUIKillURL) ||
89 *url == GURL(kChromeUIShorthangURL);
92 // static
93 BrowserURLHandler* BrowserURLHandler::GetInstance() {
94 return BrowserURLHandlerImpl::GetInstance();
97 // static
98 BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() {
99 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
100 return NULL;
103 // static
104 BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() {
105 return Singleton<BrowserURLHandlerImpl>::get();
108 BrowserURLHandlerImpl::BrowserURLHandlerImpl() {
109 AddHandlerPair(&DebugURLHandler, BrowserURLHandlerImpl::null_handler());
111 GetContentClient()->browser()->BrowserURLHandlerCreated(this);
113 // view-source:
114 AddHandlerPair(&HandleViewSource, &ReverseViewSource);
117 BrowserURLHandlerImpl::~BrowserURLHandlerImpl() {
120 void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler,
121 URLHandler reverse_handler) {
122 url_handlers_.push_back(HandlerPair(handler, reverse_handler));
125 void BrowserURLHandlerImpl::RewriteURLIfNecessary(
126 GURL* url,
127 BrowserContext* browser_context,
128 bool* reverse_on_redirect) {
129 for (size_t i = 0; i < url_handlers_.size(); ++i) {
130 URLHandler handler = *url_handlers_[i].first;
131 if (handler && handler(url, browser_context)) {
132 *reverse_on_redirect = (url_handlers_[i].second != NULL);
133 return;
138 bool BrowserURLHandlerImpl::ReverseURLRewrite(
139 GURL* url, const GURL& original, BrowserContext* browser_context) {
140 for (size_t i = 0; i < url_handlers_.size(); ++i) {
141 URLHandler reverse_rewriter = *url_handlers_[i].second;
142 if (reverse_rewriter) {
143 GURL test_url(original);
144 URLHandler handler = *url_handlers_[i].first;
145 if (!handler) {
146 if (reverse_rewriter(url, browser_context))
147 return true;
148 } else if (handler(&test_url, browser_context)) {
149 return reverse_rewriter(url, browser_context);
153 return false;
156 } // namespace content