From a3b6dfc0989227b3519daae817ec4fc420cd7016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Fri, 13 Sep 2019 13:48:14 +0200 Subject: Use ProtocolHandlerRegistry with network service Change-Id: Ia7448fb1406536822dd245cfdb6fe7c1cf19e211 Reviewed-by: Allan Sandfeld Jensen --- src/core/content_browser_client_qt.cpp | 66 ++++++++++++++++++++++++++++++++++ src/core/content_browser_client_qt.h | 10 ++++++ src/core/profile_io_data_qt.cpp | 8 +---- src/core/profile_io_data_qt.h | 9 ++++- 4 files changed, 85 insertions(+), 8 deletions(-) (limited to 'src/core') diff --git a/src/core/content_browser_client_qt.cpp b/src/core/content_browser_client_qt.cpp index 5816749c8..71ba33957 100644 --- a/src/core/content_browser_client_qt.cpp +++ b/src/core/content_browser_client_qt.cpp @@ -45,6 +45,7 @@ #include "base/message_loop/message_loop.h" #include "base/task/post_task.h" #include "base/threading/thread_restrictions.h" +#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" #if QT_CONFIG(webengine_spellchecker) #include "chrome/browser/spellchecker/spell_check_host_chrome_impl.h" #endif @@ -768,6 +769,71 @@ bool ContentBrowserClientQt::HandleExternalProtocol( return true; } +namespace { +// Copied from chrome/browser/chrome_content_browser_client.cc +template +class ProtocolHandlerThrottle : public content::URLLoaderThrottle +{ +public: + explicit ProtocolHandlerThrottle(const HandlerRegistry &protocol_handler_registry) + : protocol_handler_registry_(protocol_handler_registry) + { + } + ~ProtocolHandlerThrottle() override = default; + + void WillStartRequest(network::ResourceRequest *request, bool *defer) override + { + TranslateUrl(&request->url); + } + + void WillRedirectRequest(net::RedirectInfo *redirect_info, + const network::ResourceResponseHead &response_head, bool *defer, + std::vector *to_be_removed_headers, + net::HttpRequestHeaders *modified_headers) override + { + TranslateUrl(&redirect_info->new_url); + } + +private: + void TranslateUrl(GURL *url) + { + if (!protocol_handler_registry_->IsHandledProtocol(url->scheme())) + return; + GURL translated_url = protocol_handler_registry_->Translate(*url); + if (!translated_url.is_empty()) + *url = translated_url; + } + + HandlerRegistry protocol_handler_registry_; +}; +} // namespace + +std::vector> +ContentBrowserClientQt::CreateURLLoaderThrottlesOnIO( + const network::ResourceRequest & /*request*/, content::ResourceContext *resource_context, + const base::RepeatingCallback & /*wc_getter*/, + content::NavigationUIData * /*navigation_ui_data*/, int /*frame_tree_node_id*/) +{ + std::vector> result; + ProfileIODataQt *ioData = ProfileIODataQt::FromResourceContext(resource_context); + result.push_back(std::make_unique>>( + ioData->protocolHandlerRegistryIOThreadDelegate())); + return result; +} + +std::vector> +ContentBrowserClientQt::CreateURLLoaderThrottles( + const network::ResourceRequest &request, content::BrowserContext *browser_context, + const base::RepeatingCallback &wc_getter, + content::NavigationUIData *navigation_ui_data, int frame_tree_node_id) +{ + std::vector> result; + result.push_back(std::make_unique>( + ProtocolHandlerRegistryFactory::GetForBrowserContext(browser_context))); + return result; +} + std::unique_ptr ContentBrowserClientQt::CreateLoginDelegate( const net::AuthChallengeInfo &authInfo, content::WebContents *web_contents, diff --git a/src/core/content_browser_client_qt.h b/src/core/content_browser_client_qt.h index 7f31b7c85..d0771cc4b 100644 --- a/src/core/content_browser_client_qt.h +++ b/src/core/content_browser_client_qt.h @@ -207,6 +207,16 @@ public: network::mojom::URLLoaderFactoryRequest *factory_request, network::mojom::URLLoaderFactory *&out_factory) override; + std::vector> CreateURLLoaderThrottlesOnIO( + const network::ResourceRequest &request, content::ResourceContext *resource_context, + const base::RepeatingCallback &wc_getter, + content::NavigationUIData *navigation_ui_data, int frame_tree_node_id) override; + + std::vector> CreateURLLoaderThrottles( + const network::ResourceRequest &request, content::BrowserContext *browser_context, + const base::RepeatingCallback &wc_getter, + content::NavigationUIData *navigation_ui_data, int frame_tree_node_id) override; + static std::string getUserAgent(); std::string GetUserAgent() const override { return getUserAgent(); } diff --git a/src/core/profile_io_data_qt.cpp b/src/core/profile_io_data_qt.cpp index 5c87d87a2..eccef0dd5 100644 --- a/src/core/profile_io_data_qt.cpp +++ b/src/core/profile_io_data_qt.cpp @@ -261,8 +261,7 @@ void ProfileIODataQt::initializeOnUIThread() ProtocolHandlerRegistry* protocolHandlerRegistry = ProtocolHandlerRegistryFactory::GetForBrowserContext(m_profile); DCHECK(protocolHandlerRegistry); - m_protocolHandlerInterceptor = - protocolHandlerRegistry->CreateJobInterceptorFactory(); + m_protocolHandlerRegistryIOThreadDelegate = protocolHandlerRegistry->io_thread_delegate(); m_cookieDelegate = new CookieMonsterDelegateQt(); m_cookieDelegate->setClient(m_profile->profileAdapter()->cookieStore()); createProxyConfig(); @@ -544,11 +543,6 @@ void ProfileIODataQt::generateJobFactory() m_requestInterceptors.clear(); - if (m_protocolHandlerInterceptor) { - m_protocolHandlerInterceptor->Chain(std::move(topJobFactory)); - topJobFactory = std::move(m_protocolHandlerInterceptor); - } - m_jobFactory = std::move(topJobFactory); m_urlRequestContext->set_job_factory(m_jobFactory.get()); diff --git a/src/core/profile_io_data_qt.h b/src/core/profile_io_data_qt.h index 00a4de2b0..83b784ce3 100644 --- a/src/core/profile_io_data_qt.h +++ b/src/core/profile_io_data_qt.h @@ -42,6 +42,7 @@ #include "profile_adapter.h" #include "content/public/browser/browsing_data_remover.h" +#include "content/public/common/url_loader_throttle.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "extensions/buildflags/buildflags.h" @@ -104,6 +105,11 @@ public: extensions::ExtensionSystemQt* GetExtensionSystem(); #endif // BUILDFLAG(ENABLE_EXTENSIONS) + ProtocolHandlerRegistry::IOThreadDelegate *protocolHandlerRegistryIOThreadDelegate() + { + return m_protocolHandlerRegistryIOThreadDelegate.get(); + } + void initializeOnIOThread(); void initializeOnUIThread(); // runs on ui thread void shutdownOnUIThread(); // runs on ui thread @@ -153,7 +159,8 @@ private: std::unique_ptr m_resourceContext; std::unique_ptr m_urlRequestContext; std::unique_ptr m_httpNetworkSession; - std::unique_ptr m_protocolHandlerInterceptor; + scoped_refptr + m_protocolHandlerRegistryIOThreadDelegate; std::unique_ptr m_dhcpPacFileFetcherFactory; std::unique_ptr m_httpAuthPreferences; std::unique_ptr m_jobFactory; -- cgit v1.2.3