Chromium Code Reviews| Index: chrome/browser/chrome_content_browser_client.cc |
| diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc |
| index 6cd498bc826fc1d16924eeb66143aea99ae9848f..fa6562e076dcd4738a33542a614be665fb881765 100644 |
| --- a/chrome/browser/chrome_content_browser_client.cc |
| +++ b/chrome/browser/chrome_content_browser_client.cc |
| @@ -158,8 +158,56 @@ const char* kPredefinedAllowedSocketOrigins[] = { |
| "ghbfeebgmiidnnmeobbbaiamklmpbpii" // see crbug.com/134099 |
| }; |
| +GURL ReplaceURLHostAndPath(const GURL url, const std::string host, |
|
Alexei Svitkine (slow)
2012/08/08 18:55:55
Pass these args by ref. Put the 2nd and 3rd args o
|
| + const std::string path) { |
| + url_canon::Replacements<char> replacements; |
| + replacements.SetHost(host.c_str(), |
| + url_parse::Component(0, host.length())); |
| + replacements.SetPath(path.c_str(), |
| + url_parse::Component(0, path.length())); |
| + return url.ReplaceComponents(replacements); |
| +} |
| + |
| +// Maps "foo://bar/baz/" to "foo://chrome/bar/baz/" |
| +GURL AddUberHost(const GURL& url) { |
| + const std::string uber_host = chrome::kChromeUIUberHost; |
| + const std::string old_host = url.host(); |
| + |
| + return ReplaceURLHostAndPath(url, uber_host, old_host); |
| +} |
| + |
| +// If url->host() is "chrome", it maps "foo://chrome/bar/" to "foo://bar/" |
|
Alexei Svitkine (slow)
2012/08/08 18:55:55
Explain the return value in the comment. End the s
|
| +bool RemoveUberHost(GURL* url) { |
| + if (url->host() != chrome::kChromeUIUberHost) |
| + return false; |
| + |
| + const std::string old_path = url->path(); |
| + |
| + const int separator = old_path.find('/', 1); |
| + std::string new_host; |
| + std::string new_path; |
| + if (separator == std::string::npos) { |
| + new_host = old_path.empty() ? old_path : old_path.substr(1); |
| + } else { |
| + new_host = old_path.substr(1, separator - 1); |
| + new_path = old_path.substr(separator); |
| + } |
| + |
| + *url = ReplaceURLHostAndPath(*url, new_host, new_path); |
| + |
| + return true; |
| +} |
| + |
| // Handles rewriting Web UI URLs. |
| bool HandleWebUI(GURL* url, content::BrowserContext* browser_context) { |
| + const GURL chrome_url = AddUberHost(*url); |
| + |
| + // Handle valid "chrome://chrome/foo" URLs so the reverse handler will |
| + // be called. |
| + if (ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL( |
| + browser_context, chrome_url)) |
| + return true; |
| + |
| if (!ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL( |
| browser_context, *url)) |
| return false; |
| @@ -190,6 +238,15 @@ bool HandleWebUI(GURL* url, content::BrowserContext* browser_context) { |
| return true; |
| } |
| +// Reverse URL handler for Web UI. Maps "chrome://chrome/foo/" to |
| +// "chrome://foo/". |
| +bool HandleWebUIReverse(GURL* url, content::BrowserContext* browser_context) { |
| + if (!url->is_valid() || !url->SchemeIs(chrome::kChromeUIScheme)) |
| + return false; |
| + |
| + return RemoveUberHost(url); |
| +} |
| + |
| // Used by the GetPrivilegeRequiredByUrl() and GetProcessPrivilege() functions |
| // below. Extension, and isolated apps require different privileges to be |
| // granted to their RenderProcessHosts. This classification allows us to make |
| @@ -1539,7 +1596,7 @@ void ChromeContentBrowserClient::BrowserURLHandlerCreated( |
| BrowserURLHandler::null_handler()); |
| // chrome: & friends. |
| handler->AddHandlerPair(&HandleWebUI, |
| - BrowserURLHandler::null_handler()); |
| + &HandleWebUIReverse); |
|
Alexei Svitkine (slow)
2012/08/08 18:55:55
Nit: This can now fit on the previous line.
|
| } |
| void ChromeContentBrowserClient::ClearCache(RenderViewHost* rvh) { |