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