Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / browser_url_handler_impl.cc
blob63ddf4924b8a3457dfb4e361519650e2c2a37d98
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/frame_host/debug_urls.h"
9 #include "content/browser/webui/web_ui_impl.h"
10 #include "content/public/browser/content_browser_client.h"
11 #include "content/public/common/url_constants.h"
12 #include "url/gurl.h"
14 namespace content {
16 // Handles rewriting view-source URLs for what we'll actually load.
17 static bool HandleViewSource(GURL* url, BrowserContext* browser_context) {
18 if (url->SchemeIs(kViewSourceScheme)) {
19 // Load the inner URL instead.
20 *url = GURL(url->GetContent());
22 // Bug 26129: limit view-source to view the content and not any
23 // other kind of 'active' url scheme like 'javascript' or 'data'.
24 static const char* const default_allowed_sub_schemes[] = {
25 url::kHttpScheme,
26 url::kHttpsScheme,
27 url::kFtpScheme,
28 kChromeDevToolsScheme,
29 kChromeUIScheme,
30 url::kFileScheme,
31 url::kFileSystemScheme
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);
42 bool is_sub_scheme_allowed = false;
43 for (size_t i = 0; i < all_allowed_sub_schemes.size(); ++i) {
44 if (url->SchemeIs(all_allowed_sub_schemes[i].c_str())) {
45 is_sub_scheme_allowed = true;
46 break;
50 if (!is_sub_scheme_allowed) {
51 *url = GURL(url::kAboutBlankURL);
52 return false;
55 return true;
57 return false;
60 // Turns a non view-source URL into the corresponding view-source URL.
61 static bool ReverseViewSource(GURL* url, BrowserContext* browser_context) {
62 // No action necessary if the URL is already view-source:
63 if (url->SchemeIs(kViewSourceScheme))
64 return false;
65 // Recreate the url with the view-source scheme.
66 *url = GURL(kViewSourceScheme + std::string(":") + url->spec());
67 return true;
70 static bool DebugURLHandler(GURL* url, BrowserContext* browser_context) {
71 // Circumvent processing URLs that the renderer process will handle.
72 return IsRendererDebugURL(*url);
75 // static
76 BrowserURLHandler* BrowserURLHandler::GetInstance() {
77 return BrowserURLHandlerImpl::GetInstance();
80 // static
81 BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() {
82 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
83 return NULL;
86 // static
87 BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() {
88 return Singleton<BrowserURLHandlerImpl>::get();
91 BrowserURLHandlerImpl::BrowserURLHandlerImpl() :
92 fixup_handler_(nullptr) {
93 AddHandlerPair(&DebugURLHandler, BrowserURLHandlerImpl::null_handler());
95 GetContentClient()->browser()->BrowserURLHandlerCreated(this);
97 // view-source:
98 AddHandlerPair(&HandleViewSource, &ReverseViewSource);
101 BrowserURLHandlerImpl::~BrowserURLHandlerImpl() {
104 void BrowserURLHandlerImpl::SetFixupHandler(URLHandler handler) {
105 DCHECK(fixup_handler_ == nullptr);
106 fixup_handler_ = handler;
109 void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler,
110 URLHandler reverse_handler) {
111 url_handlers_.push_back(HandlerPair(handler, reverse_handler));
114 void BrowserURLHandlerImpl::RewriteURLIfNecessary(
115 GURL* url,
116 BrowserContext* browser_context,
117 bool* reverse_on_redirect) {
118 for (size_t i = 0; i < url_handlers_.size(); ++i) {
119 URLHandler handler = *url_handlers_[i].first;
120 if (handler && handler(url, browser_context)) {
121 *reverse_on_redirect = (url_handlers_[i].second != NULL);
122 return;
127 void BrowserURLHandlerImpl::FixupURLBeforeRewrite(
128 GURL* url,
129 BrowserContext* browser_context) {
130 if (fixup_handler_)
131 fixup_handler_(url, browser_context);
134 bool BrowserURLHandlerImpl::ReverseURLRewrite(
135 GURL* url, const GURL& original, BrowserContext* browser_context) {
136 for (size_t i = 0; i < url_handlers_.size(); ++i) {
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) {
142 if (reverse_rewriter(url, browser_context))
143 return true;
144 } else if (handler(&test_url, browser_context)) {
145 return reverse_rewriter(url, browser_context);
149 return false;
152 } // namespace content