| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "content/browser/browser_url_handler.h" | |
| 6 | |
| 7 #include "base/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" | |
| 11 #include "googleurl/src/gurl.h" | |
| 12 | |
| 13 // Handles rewriting view-source URLs for what we'll actually load. | |
| 14 static bool HandleViewSource(GURL* url, | |
| 15 content::BrowserContext* browser_context) { | |
| 16 if (url->SchemeIs(chrome::kViewSourceScheme)) { | |
| 17 // Load the inner URL instead. | |
| 18 *url = GURL(url->path()); | |
| 19 | |
| 20 // Bug 26129: limit view-source to view the content and not any | |
| 21 // other kind of 'active' url scheme like 'javascript' or 'data'. | |
| 22 static const char* const allowed_sub_schemes[] = { | |
| 23 chrome::kHttpScheme, chrome::kHttpsScheme, chrome::kFtpScheme, | |
| 24 chrome::kChromeDevToolsScheme, chrome::kChromeUIScheme, | |
| 25 chrome::kFileScheme | |
| 26 }; | |
| 27 | |
| 28 bool is_sub_scheme_allowed = false; | |
| 29 for (size_t i = 0; i < arraysize(allowed_sub_schemes); i++) { | |
| 30 if (url->SchemeIs(allowed_sub_schemes[i])) { | |
| 31 is_sub_scheme_allowed = true; | |
| 32 break; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 if (!is_sub_scheme_allowed) { | |
| 37 *url = GURL(chrome::kAboutBlankURL); | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 return true; | |
| 42 } | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 // Turns a non view-source URL into the corresponding view-source URL. | |
| 47 static bool ReverseViewSource(GURL* url, | |
| 48 content::BrowserContext* browser_context) { | |
| 49 // No action necessary if the URL is already view-source: | |
| 50 if (url->SchemeIs(chrome::kViewSourceScheme)) | |
| 51 return false; | |
| 52 | |
| 53 url_canon::Replacements<char> repl; | |
| 54 repl.SetScheme(chrome::kViewSourceScheme, | |
| 55 url_parse::Component(0, strlen(chrome::kViewSourceScheme))); | |
| 56 repl.SetPath(url->spec().c_str(), | |
| 57 url_parse::Component(0, url->spec().size())); | |
| 58 *url = url->ReplaceComponents(repl); | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 static bool HandleDebugUrl(GURL* url, | |
| 63 content::BrowserContext* browser_context) { | |
| 64 // Circumvent processing URLs that the renderer process will handle. | |
| 65 return *url == GURL(chrome::kChromeUICrashURL) || | |
| 66 *url == GURL(chrome::kChromeUIHangURL) || | |
| 67 *url == GURL(chrome::kChromeUIKillURL) || | |
| 68 *url == GURL(chrome::kChromeUIShorthangURL); | |
| 69 } | |
| 70 | |
| 71 // static | |
| 72 BrowserURLHandler* BrowserURLHandler::GetInstance() { | |
| 73 return Singleton<BrowserURLHandler>::get(); | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() { | |
| 78 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | |
| 79 return NULL; | |
| 80 } | |
| 81 | |
| 82 BrowserURLHandler::BrowserURLHandler() { | |
| 83 AddHandlerPair(&HandleDebugUrl, BrowserURLHandler::null_handler()); | |
| 84 | |
| 85 content::GetContentClient()->browser()->BrowserURLHandlerCreated(this); | |
| 86 | |
| 87 // view-source: | |
| 88 AddHandlerPair(&HandleViewSource, &ReverseViewSource); | |
| 89 } | |
| 90 | |
| 91 BrowserURLHandler::~BrowserURLHandler() { | |
| 92 } | |
| 93 | |
| 94 void BrowserURLHandler::AddHandlerPair(URLHandler handler, | |
| 95 URLHandler reverse_handler) { | |
| 96 url_handlers_.push_back(HandlerPair(handler, reverse_handler)); | |
| 97 } | |
| 98 | |
| 99 void BrowserURLHandler::RewriteURLIfNecessary( | |
| 100 GURL* url, | |
| 101 content::BrowserContext* browser_context, | |
| 102 bool* reverse_on_redirect) { | |
| 103 for (size_t i = 0; i < url_handlers_.size(); ++i) { | |
| 104 URLHandler handler = *url_handlers_[i].first; | |
| 105 if (handler && handler(url, browser_context)) { | |
| 106 *reverse_on_redirect = (url_handlers_[i].second != NULL); | |
| 107 return; | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 bool BrowserURLHandler::ReverseURLRewrite( | |
| 113 GURL* url, const GURL& original, content::BrowserContext* browser_context) { | |
| 114 for (size_t i = 0; i < url_handlers_.size(); ++i) { | |
| 115 URLHandler reverse_rewriter = *url_handlers_[i].second; | |
| 116 if (reverse_rewriter) { | |
| 117 GURL test_url(original); | |
| 118 URLHandler handler = *url_handlers_[i].first; | |
| 119 if (!handler) { | |
| 120 if (reverse_rewriter(url, browser_context)) | |
| 121 return true; | |
| 122 } else if (handler(&test_url, browser_context)) { | |
| 123 return reverse_rewriter(url, browser_context); | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 return false; | |
| 128 } | |
| OLD | NEW |