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