diff options
author | cem.kocagil@gmail.com <cem.kocagil@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-25 03:06:24 +0000 |
---|---|---|
committer | cem.kocagil@gmail.com <cem.kocagil@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-25 03:06:24 +0000 |
commit | f8f93eb2f80a882e6d46c2e69d6da5b693ca388c (patch) | |
tree | 301d221fd085789979265b92a4070f04f2311959 /chrome/browser | |
parent | 7d5f2285f300d2b40f4c1fe6a20a399af0e9da28 (diff) | |
download | chromium_src-f8f93eb2f80a882e6d46c2e69d6da5b693ca388c.zip chromium_src-f8f93eb2f80a882e6d46c2e69d6da5b693ca388c.tar.gz chromium_src-f8f93eb2f80a882e6d46c2e69d6da5b693ca388c.tar.bz2 |
Second try of 157576 https://chromiumcodereview.appspot.com/10780013/
Add reverse URL handler for shortening uber URLs
BUG=120493
TEST=Open the settings page. The address bar should show "chrome://settings". Click the "extensions" button on the left. The address bar should show "chrome://extensions". Click the "settings" button on the left, "show advanced settings", "content settings". The address bar should show "chrome://settings/content".
Review URL: https://chromiumcodereview.appspot.com/10941050
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@158504 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
5 files changed, 149 insertions, 18 deletions
diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index 1a17810..cebf14e 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -170,8 +170,63 @@ const char* kPredefinedAllowedSocketOrigins[] = { "jdfhpkjeckflbbleddjlpimecpbjdeep" // see crbug.com/142514 }; +// Returns a copy of the given url with its host set to given host and path set +// to given path. Other parts of the url will be the same. +GURL ReplaceURLHostAndPath(const GURL& url, + const std::string& host, + 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 new_path = url.host() + url.path(); + + return ReplaceURLHostAndPath(url, uber_host, new_path); +} + +// If url->host() is "chrome", changes the url from "foo://chrome/bar/" to +// "foo://bar/" and returns true. Otherwise returns false. +bool RemoveUberHost(GURL* url) { + if (url->host() != chrome::kChromeUIUberHost) + return false; + + const std::string old_path = url->path(); + + const std::string::size_type 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) { + // Do not handle special URLs such as "about:foo" + if (!url->host().empty()) { + 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; @@ -202,6 +257,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 @@ -1576,8 +1640,7 @@ void ChromeContentBrowserClient::BrowserURLHandlerCreated( handler->AddHandlerPair(&WillHandleBrowserAboutURL, BrowserURLHandler::null_handler()); // chrome: & friends. - handler->AddHandlerPair(&HandleWebUI, - BrowserURLHandler::null_handler()); + handler->AddHandlerPair(&HandleWebUI, &HandleWebUIReverse); } void ChromeContentBrowserClient::ClearCache(RenderViewHost* rvh) { diff --git a/chrome/browser/chrome_content_browser_client_browsertest.cc b/chrome/browser/chrome_content_browser_client_browsertest.cc new file mode 100644 index 0000000..171d4f6 --- /dev/null +++ b/chrome/browser/chrome_content_browser_client_browsertest.cc @@ -0,0 +1,54 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "chrome/browser/ui/browser_tabstrip.h" +#include "chrome/browser/ui/tab_contents/tab_contents.h" +#include "content/public/browser/navigation_controller.h" +#include "content/public/browser/navigation_entry.h" +#include "content/public/browser/web_contents.h" +#include "googleurl/src/gurl.h" + +namespace content { + +class ChromeContentBrowserClientBrowserTest : public InProcessBrowserTest { + public: + // Returns the last committed navigation entry of the first tab. May be NULL + // if there is no such entry. + NavigationEntry* GetLastCommittedEntry() { + return chrome::GetTabContentsAt(browser(), 0)->web_contents() + ->GetController().GetLastCommittedEntry(); + } +}; + +IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest, + UberURLHandler_SettingsPage) { + const GURL url_short(std::string("chrome://settings/")); + const GURL url_long(std::string("chrome://chrome/settings/")); + + ui_test_utils::NavigateToURL(browser(), url_short); + NavigationEntry* entry = GetLastCommittedEntry(); + + ASSERT_TRUE(entry != NULL); + EXPECT_EQ(url_long, entry->GetURL()); + EXPECT_EQ(url_short, entry->GetVirtualURL()); +} + +IN_PROC_BROWSER_TEST_F(ChromeContentBrowserClientBrowserTest, + UberURLHandler_ContentSettingsPage) { + const GURL url_short(std::string("chrome://settings/content")); + const GURL url_long(std::string("chrome://chrome/settings/content")); + + ui_test_utils::NavigateToURL(browser(), url_short); + NavigationEntry* entry = GetLastCommittedEntry(); + + ASSERT_TRUE(entry != NULL); + EXPECT_EQ(url_long, entry->GetURL()); + EXPECT_EQ(url_short, entry->GetVirtualURL()); +} + +} // namespace content diff --git a/chrome/browser/extensions/lazy_background_page_apitest.cc b/chrome/browser/extensions/lazy_background_page_apitest.cc index ad3a4af..cba9aa4 100644 --- a/chrome/browser/extensions/lazy_background_page_apitest.cc +++ b/chrome/browser/extensions/lazy_background_page_apitest.cc @@ -21,6 +21,7 @@ #include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension.h" +#include "chrome/common/url_constants.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/web_contents.h" @@ -111,7 +112,7 @@ IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) { // Background page created a new tab before it closed. EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_)); EXPECT_EQ(num_tabs_before + 1, browser()->tab_count()); - EXPECT_EQ("chrome://chrome/extensions/", + EXPECT_EQ(std::string(chrome::kChromeUIExtensionsURL), chrome::GetActiveWebContents(browser())->GetURL().spec()); } diff --git a/chrome/browser/history/history_browsertest.cc b/chrome/browser/history/history_browsertest.cc index f97bad8..d839dcc 100644 --- a/chrome/browser/history/history_browsertest.cc +++ b/chrome/browser/history/history_browsertest.cc @@ -476,7 +476,7 @@ IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SubmitFormAddsTargetPage) { // Verify history shortcut opens only one history tab per window. Also, make // sure that existing history tab is activated. IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, OneHistoryTabPerWindow) { - GURL history_url("chrome://chrome/history/"); + GURL history_url(chrome::kChromeUIHistoryURL); // Even after navigate completes, the currently-active tab title is // 'Loading...' for a brief time while the history page loads. diff --git a/chrome/browser/ui/browser_navigator_browsertest.cc b/chrome/browser/ui/browser_navigator_browsertest.cc index 04be5f7..3b0510d 100644 --- a/chrome/browser/ui/browser_navigator_browsertest.cc +++ b/chrome/browser/ui/browser_navigator_browsertest.cc @@ -40,8 +40,7 @@ GURL GetGoogleURL() { } GURL GetSettingsURL() { - return GURL(chrome::kChromeUIUberURL).Resolve( - chrome::kChromeUISettingsHost + std::string("/")); + return GURL(chrome::kChromeUISettingsURL); } GURL GetContentSettingsURL() { @@ -52,6 +51,20 @@ GURL GetClearBrowsingDataURL() { return GetSettingsURL().Resolve(chrome::kClearBrowserDataSubPage); } +// Converts long uber URLs ("chrome://chrome/foo/") to short (virtual) URLs +// ("chrome://foo/"). This should be used to convert the return value of +// WebContentsImpl::GetURL before comparison because it can return either the +// real URL or the virtual URL. +GURL ShortenUberURL(const GURL& url) { + std::string url_string = url.spec(); + const std::string long_prefix = "chrome://chrome/"; + const std::string short_prefix = "chrome://"; + if (url_string.find(long_prefix) != 0) + return url; + url_string.replace(0, long_prefix.length(), short_prefix); + return GURL(url_string); +} + } // namespace chrome::NavigateParams BrowserNavigatorTest::MakeNavigateParams() const { @@ -722,7 +735,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, EXPECT_EQ(3, browser()->tab_count()); EXPECT_EQ(2, browser()->active_index()); EXPECT_EQ(GetContentSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // This test verifies that constructing params with disposition = SINGLETON_TAB @@ -755,7 +768,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, EXPECT_EQ(3, browser()->tab_count()); EXPECT_EQ(1, browser()->active_index()); EXPECT_EQ(GetContentSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // This test verifies that constructing params with disposition = SINGLETON_TAB @@ -788,7 +801,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, EXPECT_EQ(3, browser()->tab_count()); EXPECT_EQ(1, browser()->active_index()); EXPECT_EQ(GetClearBrowsingDataURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // This test verifies that constructing params with disposition = SINGLETON_TAB @@ -820,7 +833,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, EXPECT_EQ(3, browser()->tab_count()); EXPECT_EQ(1, browser()->active_index()); EXPECT_EQ(singleton_url1, - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // This test verifies that constructing params with disposition = SINGLETON_TAB @@ -851,7 +864,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, EXPECT_EQ(2, browser()->tab_count()); EXPECT_EQ(1, browser()->active_index()); EXPECT_EQ(singleton_url_target, - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // This test verifies that constructing params with disposition = SINGLETON_TAB @@ -910,7 +923,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, EXPECT_EQ(1u, BrowserList::size()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // Settings page is expected to always open in normal mode regardless @@ -994,7 +1007,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, } EXPECT_EQ(1, browser()->tab_count()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, @@ -1012,7 +1025,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, } EXPECT_EQ(1, browser()->tab_count()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, @@ -1033,7 +1046,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, } EXPECT_EQ(1, browser()->tab_count()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, @@ -1053,7 +1066,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, } EXPECT_EQ(2, browser()->tab_count()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, @@ -1079,7 +1092,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, } EXPECT_EQ(2, browser()->tab_count()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, @@ -1144,7 +1157,7 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, } EXPECT_EQ(2, browser()->tab_count()); EXPECT_EQ(GetSettingsURL(), - chrome::GetActiveWebContents(browser())->GetURL()); + ShortenUberURL(chrome::GetActiveWebContents(browser())->GetURL())); } // Tests that when a new tab is opened from the omnibox, the focus is moved from |