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..4c097df2fcf25e06c488706434ddc6bf17445e1d 100644 |
| --- a/chrome/browser/chrome_content_browser_client.cc |
| +++ b/chrome/browser/chrome_content_browser_client.cc |
| @@ -158,8 +158,57 @@ const char* kPredefinedAllowedSocketOrigins[] = { |
| "ghbfeebgmiidnnmeobbbaiamklmpbpii" // see crbug.com/134099 |
| }; |
| +// 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(); |
| + |
| + url_canon::Replacements<char> replacements; |
| + replacements.SetHost(uber_host.c_str(), |
| + url_parse::Component(0, uber_host.length())); |
| + replacements.SetPath(old_host.c_str(), |
| + url_parse::Component(0, old_host.length())); |
| + |
| + return url.ReplaceComponents(replacements); |
| +} |
| + |
| +// If url->host() is "chrome", it maps "foo://chrome/bar/" to "foo://bar/" |
| +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.substr(1); |
|
Alexei Svitkine (slow)
2012/08/08 15:07:40
Even if find() is okay when |old_path| is empty, t
|
| + } else { |
| + new_host = old_path.substr(1, separator - 1); |
| + new_path = old_path.substr(separator); |
| + } |
| + |
| + url_canon::Replacements<char> replacements; |
| + replacements.SetHost(new_host.c_str(), |
| + url_parse::Component(0, new_host.length())); |
| + replacements.SetPath(new_path.c_str(), |
| + url_parse::Component(0, new_path.length())); |
| + *url = url->ReplaceComponents(replacements); |
|
Alexei Svitkine (slow)
2012/08/08 15:07:40
Can you extract this block into a helper function,
|
| + |
| + 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 +239,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 +1597,7 @@ void ChromeContentBrowserClient::BrowserURLHandlerCreated( |
| BrowserURLHandler::null_handler()); |
| // chrome: & friends. |
| handler->AddHandlerPair(&HandleWebUI, |
| - BrowserURLHandler::null_handler()); |
| + &HandleWebUIReverse); |
| } |
| void ChromeContentBrowserClient::ClearCache(RenderViewHost* rvh) { |