diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-12 19:07:31 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-12 19:07:31 +0000 |
commit | 825b16687e0de3ef4ec3b7dd60fc1dfb237947c9 (patch) | |
tree | 8831f3f038a28b69c8dd976a7381aa66c1406ff4 | |
parent | a60de956ec71ec185684f6d6167d5c8547f3639f (diff) | |
download | chromium_src-825b16687e0de3ef4ec3b7dd60fc1dfb237947c9.zip chromium_src-825b16687e0de3ef4ec3b7dd60fc1dfb237947c9.tar.gz chromium_src-825b16687e0de3ef4ec3b7dd60fc1dfb237947c9.tar.bz2 |
Add a Content API around BrowserURLHandler.
BUG=98716
Review URL: https://chromiumcodereview.appspot.com/9688019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126180 0039d316-1c4b-4281-b951-d872f2087c98
15 files changed, 118 insertions, 76 deletions
diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index 0513fe3..e0adfab 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -72,8 +72,8 @@ #include "chrome/common/pref_names.h" #include "chrome/common/render_messages.h" #include "chrome/common/url_constants.h" -#include "content/browser/browser_url_handler.h" #include "content/public/browser/browser_main_parts.h" +#include "content/public/browser/browser_url_handler.h" #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" @@ -141,6 +141,7 @@ using content::AccessTokenStore; using content::BrowserThread; +using content::BrowserURLHandler; using content::ChildProcessSecurityPolicy; using content::QuotaPermissionContext; using content::RenderViewHost; diff --git a/chrome/browser/chrome_content_browser_client.h b/chrome/browser/chrome_content_browser_client.h index 748eef9..880bcd0 100644 --- a/chrome/browser/chrome_content_browser_client.h +++ b/chrome/browser/chrome_content_browser_client.h @@ -158,7 +158,8 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient { const std::string& key, const std::string& value) OVERRIDE; virtual void ClearInspectorSettings(content::RenderViewHost* rvh) OVERRIDE; - virtual void BrowserURLHandlerCreated(BrowserURLHandler* handler) OVERRIDE; + virtual void BrowserURLHandlerCreated( + content::BrowserURLHandler* handler) OVERRIDE; virtual void ClearCache(content::RenderViewHost* rvh) OVERRIDE; virtual void ClearCookies(content::RenderViewHost* rvh) OVERRIDE; virtual FilePath GetDefaultDownloadDirectory() OVERRIDE; diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index bfb1dec..01774f16 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -150,7 +150,6 @@ #include "chrome/common/profiling.h" #include "chrome/common/url_constants.h" #include "chrome/common/web_apps.h" -#include "content/browser/browser_url_handler.h" #include "content/public/browser/devtools_manager.h" #include "content/public/browser/download_item.h" #include "content/public/browser/download_manager.h" diff --git a/chrome/browser/ui/browser_navigator.cc b/chrome/browser/ui/browser_navigator.cc index 80aa319..9298bc3 100644 --- a/chrome/browser/ui/browser_navigator.cc +++ b/chrome/browser/ui/browser_navigator.cc @@ -33,7 +33,7 @@ #include "chrome/common/extensions/extension.h" #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" -#include "content/browser/browser_url_handler.h" +#include "content/public/browser/browser_url_handler.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host_delegate.h" @@ -634,7 +634,7 @@ int GetIndexOfSingletonTab(browser::NavigateParams* params) { // URL. GURL rewritten_url(params->url); bool reverse_on_redirect = false; - BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( + content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( &rewritten_url, params->browser->profile(), &reverse_on_redirect); diff --git a/content/browser/browser_url_handler.cc b/content/browser/browser_url_handler_impl.cc index 15f947b..7eef43f 100644 --- a/content/browser/browser_url_handler.cc +++ b/content/browser/browser_url_handler_impl.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "content/browser/browser_url_handler.h" +#include "content/browser/browser_url_handler_impl.h" #include "base/string_util.h" #include "content/browser/webui/web_ui_impl.h" @@ -10,6 +10,8 @@ #include "content/public/common/url_constants.h" #include "googleurl/src/gurl.h" +using content::BrowserURLHandler; + // Handles rewriting view-source URLs for what we'll actually load. static bool HandleViewSource(GURL* url, content::BrowserContext* browser_context) { @@ -70,7 +72,7 @@ static bool HandleDebugUrl(GURL* url, // static BrowserURLHandler* BrowserURLHandler::GetInstance() { - return Singleton<BrowserURLHandler>::get(); + return BrowserURLHandlerImpl::GetInstance(); } // static @@ -79,8 +81,13 @@ BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() { return NULL; } -BrowserURLHandler::BrowserURLHandler() { - AddHandlerPair(&HandleDebugUrl, BrowserURLHandler::null_handler()); +// static +BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() { + return Singleton<BrowserURLHandlerImpl>::get(); +} + +BrowserURLHandlerImpl::BrowserURLHandlerImpl() { + AddHandlerPair(&HandleDebugUrl, BrowserURLHandlerImpl::null_handler()); content::GetContentClient()->browser()->BrowserURLHandlerCreated(this); @@ -88,15 +95,15 @@ BrowserURLHandler::BrowserURLHandler() { AddHandlerPair(&HandleViewSource, &ReverseViewSource); } -BrowserURLHandler::~BrowserURLHandler() { +BrowserURLHandlerImpl::~BrowserURLHandlerImpl() { } -void BrowserURLHandler::AddHandlerPair(URLHandler handler, - URLHandler reverse_handler) { +void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler, + URLHandler reverse_handler) { url_handlers_.push_back(HandlerPair(handler, reverse_handler)); } -void BrowserURLHandler::RewriteURLIfNecessary( +void BrowserURLHandlerImpl::RewriteURLIfNecessary( GURL* url, content::BrowserContext* browser_context, bool* reverse_on_redirect) { @@ -109,7 +116,7 @@ void BrowserURLHandler::RewriteURLIfNecessary( } } -bool BrowserURLHandler::ReverseURLRewrite( +bool BrowserURLHandlerImpl::ReverseURLRewrite( GURL* url, const GURL& original, content::BrowserContext* browser_context) { for (size_t i = 0; i < url_handlers_.size(); ++i) { URLHandler reverse_rewriter = *url_handlers_[i].second; diff --git a/content/browser/browser_url_handler_impl.h b/content/browser/browser_url_handler_impl.h new file mode 100644 index 0000000..d6136a6 --- /dev/null +++ b/content/browser/browser_url_handler_impl.h @@ -0,0 +1,55 @@ +// Copyright (c) 2011 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. + +#ifndef CONTENT_BROWSER_BROWSER_URL_HANDLER_IMPL_H_ +#define CONTENT_BROWSER_BROWSER_URL_HANDLER_IMPL_H_ +#pragma once + +#include <vector> +#include <utility> + +#include "base/gtest_prod_util.h" +#include "base/memory/singleton.h" +#include "content/public/browser/browser_url_handler.h" + +class GURL; + +namespace content { +class BrowserContext; +} + +class CONTENT_EXPORT BrowserURLHandlerImpl : public content::BrowserURLHandler { + public: + // Returns the singleton instance. + static BrowserURLHandlerImpl* GetInstance(); + + // BrowserURLHandler implementation: + virtual void RewriteURLIfNecessary(GURL* url, + content::BrowserContext* browser_context, + bool* reverse_on_redirect) OVERRIDE; + // Add the specified handler pair to the list of URL handlers. + virtual void AddHandlerPair(URLHandler handler, + URLHandler reverse_handler) OVERRIDE; + + // Reverses the rewriting that was done for |original| using the new |url|. + bool ReverseURLRewrite(GURL* url, const GURL& original, + content::BrowserContext* browser_context); + + private: + // This object is a singleton: + BrowserURLHandlerImpl(); + virtual ~BrowserURLHandlerImpl(); + friend struct DefaultSingletonTraits<BrowserURLHandlerImpl>; + + // The list of known URLHandlers, optionally with reverse-rewriters. + typedef std::pair<URLHandler, URLHandler> HandlerPair; + std::vector<HandlerPair> url_handlers_; + + FRIEND_TEST_ALL_PREFIXES(BrowserURLHandlerImplTest, BasicRewriteAndReverse); + FRIEND_TEST_ALL_PREFIXES(BrowserURLHandlerImplTest, NullHandlerReverse); + + DISALLOW_COPY_AND_ASSIGN(BrowserURLHandlerImpl); +}; + +#endif // CONTENT_BROWSER_BROWSER_URL_HANDLER_IMPL_H_ diff --git a/content/browser/browser_url_handler_unittest.cc b/content/browser/browser_url_handler_impl_unittest.cc index ed133a2..7109d5d 100644 --- a/content/browser/browser_url_handler_unittest.cc +++ b/content/browser/browser_url_handler_impl_unittest.cc @@ -2,12 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "content/browser/browser_url_handler.h" +#include "content/browser/browser_url_handler_impl.h" #include "content/test/test_browser_context.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" -class BrowserURLHandlerTest : public testing::Test { +class BrowserURLHandlerImplTest : public testing::Test { }; // Test URL rewriter that rewrites all "foo://" URLs to "bar://bar". @@ -28,9 +28,9 @@ static bool BarRewriter(GURL* url, content::BrowserContext* browser_context) { return false; } -TEST_F(BrowserURLHandlerTest, BasicRewriteAndReverse) { +TEST_F(BrowserURLHandlerImplTest, BasicRewriteAndReverse) { TestBrowserContext browser_context; - BrowserURLHandler handler; + BrowserURLHandlerImpl handler; handler.AddHandlerPair(FooRewriter, BarRewriter); @@ -57,21 +57,21 @@ TEST_F(BrowserURLHandlerTest, BasicRewriteAndReverse) { ASSERT_EQ(saved_url, url); } -TEST_F(BrowserURLHandlerTest, NullHandlerReverse) { +TEST_F(BrowserURLHandlerImplTest, NullHandlerReverse) { TestBrowserContext browser_context; - BrowserURLHandler handler; + BrowserURLHandlerImpl handler; GURL url("bar://foo"); GURL original_url(url); - handler.AddHandlerPair(BrowserURLHandler::null_handler(), FooRewriter); + handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), FooRewriter); bool reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); ASSERT_FALSE(reversed); ASSERT_EQ(original_url, url); - handler.AddHandlerPair(BrowserURLHandler::null_handler(), BarRewriter); + handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), BarRewriter); reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); ASSERT_TRUE(reversed); ASSERT_EQ("foo://foo", url.spec()); diff --git a/content/browser/renderer_host/test_render_view_host.cc b/content/browser/renderer_host/test_render_view_host.cc index 0d96498..0f28eafa8 100644 --- a/content/browser/renderer_host/test_render_view_host.cc +++ b/content/browser/renderer_host/test_render_view_host.cc @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "content/browser/browser_url_handler.h" #include "content/browser/renderer_host/test_backing_store.h" #include "content/browser/renderer_host/test_render_view_host.h" #include "content/browser/site_instance_impl.h" diff --git a/content/browser/tab_contents/navigation_controller_impl.cc b/content/browser/tab_contents/navigation_controller_impl.cc index 1f2d8a6..3de187b 100644 --- a/content/browser/tab_contents/navigation_controller_impl.cc +++ b/content/browser/tab_contents/navigation_controller_impl.cc @@ -10,7 +10,7 @@ #include "base/string_util.h" #include "base/time.h" #include "base/utf_string_conversions.h" -#include "content/browser/browser_url_handler.h" +#include "content/browser/browser_url_handler_impl.h" #include "content/browser/child_process_security_policy_impl.h" #include "content/browser/in_process_webkit/dom_storage_context_impl.h" #include "content/browser/in_process_webkit/session_storage_namespace_impl.h" @@ -143,7 +143,7 @@ NavigationEntry* NavigationController::CreateNavigationEntry( // used internally. GURL loaded_url(url); bool reverse_on_redirect = false; - BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( + BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( &loaded_url, browser_context, &reverse_on_redirect); NavigationEntryImpl* entry = new NavigationEntryImpl( @@ -534,7 +534,7 @@ void NavigationControllerImpl::RemoveEntryAtIndex(int index) { void NavigationControllerImpl::UpdateVirtualURLToURL( NavigationEntryImpl* entry, const GURL& new_url) { GURL new_virtual_url(new_url); - if (BrowserURLHandler::GetInstance()->ReverseURLRewrite( + if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite( &new_virtual_url, entry->GetVirtualURL(), browser_context_)) { entry->SetVirtualURL(new_virtual_url); } diff --git a/content/browser/tab_contents/render_view_host_manager_unittest.cc b/content/browser/tab_contents/render_view_host_manager_unittest.cc index bffb7bd..20ef0c2 100644 --- a/content/browser/tab_contents/render_view_host_manager_unittest.cc +++ b/content/browser/tab_contents/render_view_host_manager_unittest.cc @@ -4,7 +4,6 @@ #include "base/utf_string_conversions.h" #include "content/browser/browser_thread_impl.h" -#include "content/browser/browser_url_handler.h" #include "content/browser/mock_content_browser_client.h" #include "content/browser/renderer_host/test_render_view_host.h" #include "content/browser/site_instance_impl.h" diff --git a/content/browser/tab_contents/test_tab_contents.cc b/content/browser/tab_contents/test_tab_contents.cc index fc6bf57..16f43ad 100644 --- a/content/browser/tab_contents/test_tab_contents.cc +++ b/content/browser/tab_contents/test_tab_contents.cc @@ -6,7 +6,7 @@ #include <utility> -#include "content/browser/browser_url_handler.h" +#include "content/browser/browser_url_handler_impl.h" #include "content/browser/renderer_host/mock_render_process_host.h" #include "content/browser/renderer_host/render_view_host_impl.h" #include "content/browser/renderer_host/test_render_view_host.h" @@ -101,7 +101,7 @@ void TestTabContents::NavigateAndCommit(const GURL& url) { url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); GURL loaded_url(url); bool reverse_on_redirect = false; - BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( + BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary( &loaded_url, GetBrowserContext(), &reverse_on_redirect); // LoadURL created a navigation entry, now simulate the RenderView sending diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 5f6edfa..20a48208 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -40,6 +40,7 @@ 'public/browser/browser_message_filter.cc', 'public/browser/browser_message_filter.h', 'public/browser/browser_shutdown.h', + 'public/browser/browser_url_handler.h', 'public/browser/browser_thread.h', 'public/browser/browser_thread_delegate.h', 'public/browser/child_process_data.h', @@ -182,8 +183,8 @@ 'browser/browser_process_sub_thread.h', 'browser/browser_thread_impl.cc', 'browser/browser_thread_impl.h', - 'browser/browser_url_handler.cc', - 'browser/browser_url_handler.h', + 'browser/browser_url_handler_impl.cc', + 'browser/browser_url_handler_impl.h', 'browser/browsing_instance.cc', 'browser/browsing_instance.h', 'browser/cert_store.cc', diff --git a/content/content_tests.gypi b/content/content_tests.gypi index d72a9b8..7fcc221 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -194,7 +194,7 @@ 'browser/accessibility/browser_accessibility_win_unittest.cc', 'browser/appcache/chrome_appcache_service_unittest.cc', 'browser/browser_thread_unittest.cc', - 'browser/browser_url_handler_unittest.cc', + 'browser/browser_url_handler_impl_unittest.cc', 'browser/child_process_security_policy_unittest.cc', 'browser/debugger/devtools_manager_unittest.cc', 'browser/device_orientation/provider_unittest.cc', diff --git a/content/browser/browser_url_handler.h b/content/public/browser/browser_url_handler.h index 4fd625b..56d93066 100644 --- a/content/browser/browser_url_handler.h +++ b/content/public/browser/browser_url_handler.h @@ -2,30 +2,23 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// We handle some special browser-level URLs (like "about:version") -// before they're handed to a renderer. This lets us do the URL handling -// on the browser side (which has access to more information than the -// renderers do) as well as sidestep the risk of exposing data to -// random web pages (because from the resource loader's perspective, these -// URL schemes don't exist). - -#ifndef CONTENT_BROWSER_BROWSER_URL_HANDLER_H_ -#define CONTENT_BROWSER_BROWSER_URL_HANDLER_H_ +#ifndef CONTENT_PUBLIC_BROWSER_BROWSER_URL_HANDLER_H_ +#define CONTENT_PUBLIC_BROWSER_BROWSER_URL_HANDLER_H_ #pragma once -#include <vector> -#include <utility> - -#include "base/gtest_prod_util.h" -#include "base/memory/singleton.h" #include "content/common/content_export.h" class GURL; namespace content { class BrowserContext; -} +// We handle some special browser-level URLs (like "about:version") +// before they're handed to a renderer. This lets us do the URL handling +// on the browser side (which has access to more information than the +// renderers do) as well as sidestep the risk of exposing data to +// random web pages (because from the resource loader's perspective, these +// URL schemes don't exist). // BrowserURLHandler manages the list of all special URLs and manages // dispatching the URL handling to registered handlers. class CONTENT_EXPORT BrowserURLHandler { @@ -35,43 +28,30 @@ class CONTENT_EXPORT BrowserURLHandler { // - optionally modify |url| to the URL that should be sent to the renderer // If the URL is not handled by a handler, it should return false. typedef bool (*URLHandler)(GURL* url, - content::BrowserContext* browser_context); + BrowserContext* browser_context); + + // Returns the null handler for use with |AddHandlerPair()|. + static URLHandler null_handler(); // Returns the singleton instance. - static BrowserURLHandler* GetInstance(); + static BrowserURLHandler* GetInstance(); // RewriteURLIfNecessary gives all registered URLHandlers a shot at processing // the given URL, and modifies it in place. // If the original URL needs to be adjusted if the modified URL is redirected, // this function sets |reverse_on_redirect| to true. - void RewriteURLIfNecessary(GURL* url, - content::BrowserContext* browser_context, - bool* reverse_on_redirect); - - // Reverses the rewriting that was done for |original| using the new |url|. - bool ReverseURLRewrite(GURL* url, const GURL& original, - content::BrowserContext* browser_context); + virtual void RewriteURLIfNecessary(GURL* url, + BrowserContext* browser_context, + bool* reverse_on_redirect) = 0; // Add the specified handler pair to the list of URL handlers. - void AddHandlerPair(URLHandler handler, URLHandler reverse_handler); - - // Returns the null handler for use with |AddHandlerPair()|. - static URLHandler null_handler(); - - private: - // This object is a singleton: - BrowserURLHandler(); - ~BrowserURLHandler(); - friend struct DefaultSingletonTraits<BrowserURLHandler>; + virtual void AddHandlerPair(URLHandler handler, + URLHandler reverse_handler) = 0; - // The list of known URLHandlers, optionally with reverse-rewriters. - typedef std::pair<URLHandler, URLHandler> HandlerPair; - std::vector<HandlerPair> url_handlers_; - - FRIEND_TEST_ALL_PREFIXES(BrowserURLHandlerTest, BasicRewriteAndReverse); - FRIEND_TEST_ALL_PREFIXES(BrowserURLHandlerTest, NullHandlerReverse); - - DISALLOW_COPY_AND_ASSIGN(BrowserURLHandler); + protected: + virtual ~BrowserURLHandler() {} }; -#endif // CONTENT_BROWSER_BROWSER_URL_HANDLER_H_ +} // namespace content + +#endif // CONTENT_PUBLIC_BROWSER_BROWSER_URL_HANDLER_H_ diff --git a/content/public/browser/content_browser_client.h b/content/public/browser/content_browser_client.h index 1441efd..03c0d52 100644 --- a/content/public/browser/content_browser_client.h +++ b/content/public/browser/content_browser_client.h @@ -16,7 +16,6 @@ #include "content/public/common/window_container_type.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" -class BrowserURLHandler; class CommandLine; class FilePath; class GURL; @@ -28,6 +27,7 @@ struct WebPreferences; namespace content { class AccessTokenStore; class BrowserMainParts; +class BrowserURLHandler; class RenderProcessHost; class SiteInstance; class SpeechRecognitionManagerDelegate; |