diff options
author | Jüri Valdmann <juri.valdmann@qt.io> | 2019-09-13 13:48:14 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-10-11 10:15:08 +0200 |
commit | a3b6dfc0989227b3519daae817ec4fc420cd7016 (patch) | |
tree | e15d92cbd0740dd4b4827892e0ba754676e37d86 | |
parent | 320a7a8522ccff5155cbb9563428b26071266ebc (diff) |
Use ProtocolHandlerRegistry with network service
Change-Id: Ia7448fb1406536822dd245cfdb6fe7c1cf19e211
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r-- | src/core/content_browser_client_qt.cpp | 66 | ||||
-rw-r--r-- | src/core/content_browser_client_qt.h | 10 | ||||
-rw-r--r-- | src/core/profile_io_data_qt.cpp | 8 | ||||
-rw-r--r-- | src/core/profile_io_data_qt.h | 9 |
4 files changed, 85 insertions, 8 deletions
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 HandlerRegistry> +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<std::string> *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<std::unique_ptr<content::URLLoaderThrottle>> +ContentBrowserClientQt::CreateURLLoaderThrottlesOnIO( + const network::ResourceRequest & /*request*/, content::ResourceContext *resource_context, + const base::RepeatingCallback<content::WebContents *()> & /*wc_getter*/, + content::NavigationUIData * /*navigation_ui_data*/, int /*frame_tree_node_id*/) +{ + std::vector<std::unique_ptr<content::URLLoaderThrottle>> result; + ProfileIODataQt *ioData = ProfileIODataQt::FromResourceContext(resource_context); + result.push_back(std::make_unique<ProtocolHandlerThrottle< + scoped_refptr<ProtocolHandlerRegistry::IOThreadDelegate>>>( + ioData->protocolHandlerRegistryIOThreadDelegate())); + return result; +} + +std::vector<std::unique_ptr<content::URLLoaderThrottle>> +ContentBrowserClientQt::CreateURLLoaderThrottles( + const network::ResourceRequest &request, content::BrowserContext *browser_context, + const base::RepeatingCallback<content::WebContents *()> &wc_getter, + content::NavigationUIData *navigation_ui_data, int frame_tree_node_id) +{ + std::vector<std::unique_ptr<content::URLLoaderThrottle>> result; + result.push_back(std::make_unique<ProtocolHandlerThrottle<ProtocolHandlerRegistry *>>( + ProtocolHandlerRegistryFactory::GetForBrowserContext(browser_context))); + return result; +} + std::unique_ptr<content::LoginDelegate> 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<std::unique_ptr<content::URLLoaderThrottle>> CreateURLLoaderThrottlesOnIO( + const network::ResourceRequest &request, content::ResourceContext *resource_context, + const base::RepeatingCallback<content::WebContents *()> &wc_getter, + content::NavigationUIData *navigation_ui_data, int frame_tree_node_id) override; + + std::vector<std::unique_ptr<content::URLLoaderThrottle>> CreateURLLoaderThrottles( + const network::ResourceRequest &request, content::BrowserContext *browser_context, + const base::RepeatingCallback<content::WebContents *()> &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<content::ResourceContext> m_resourceContext; std::unique_ptr<net::URLRequestContext> m_urlRequestContext; std::unique_ptr<net::HttpNetworkSession> m_httpNetworkSession; - std::unique_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> m_protocolHandlerInterceptor; + scoped_refptr<ProtocolHandlerRegistry::IOThreadDelegate> + m_protocolHandlerRegistryIOThreadDelegate; std::unique_ptr<net::DhcpPacFileFetcherFactory> m_dhcpPacFileFetcherFactory; std::unique_ptr<net::HttpAuthPreferences> m_httpAuthPreferences; std::unique_ptr<net::URLRequestJobFactory> m_jobFactory; |