diff options
20 files changed, 2451 insertions, 47 deletions
diff --git a/src/core/api/qwebengineurlrequestinfo.h b/src/core/api/qwebengineurlrequestinfo.h index 75b6e3a81..69033cdfd 100644 --- a/src/core/api/qwebengineurlrequestinfo.h +++ b/src/core/api/qwebengineurlrequestinfo.h @@ -46,6 +46,7 @@ #include <QtCore/qurl.h> namespace QtWebEngineCore { +class InterceptedRequest; class NetworkDelegateQt; class URLRequestNotification; } // namespace QtWebEngineCore @@ -110,6 +111,7 @@ public: private: friend class QtWebEngineCore::NetworkDelegateQt; friend class QtWebEngineCore::URLRequestNotification; + friend class QtWebEngineCore::InterceptedRequest; Q_DISABLE_COPY(QWebEngineUrlRequestInfo) Q_DECLARE_PRIVATE(QWebEngineUrlRequestInfo) diff --git a/src/core/content_browser_client_qt.cpp b/src/core/content_browser_client_qt.cpp index f39e757fd..170fa3193 100644 --- a/src/core/content_browser_client_qt.cpp +++ b/src/core/content_browser_client_qt.cpp @@ -41,6 +41,7 @@ #include "base/memory/ptr_util.h" #include "base/optional.h" +#include "base/path_service.h" #include "base/strings/utf_string_conversions.h" #include "base/message_loop/message_loop.h" #include "base/task/post_task.h" @@ -60,6 +61,7 @@ #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/client_certificate_delegate.h" #include "content/public/browser/media_observer.h" +#include "content/public/browser/network_service_instance.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" @@ -68,6 +70,7 @@ #include "content/public/browser/storage_partition.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_user_data.h" +#include "content/public/browser/web_ui_url_loader_factory.h" #include "content/public/common/content_switches.h" #include "content/public/common/main_function_params.h" #include "content/public/common/service_manager_connection.h" @@ -76,6 +79,8 @@ #include "content/public/common/user_agent.h" #include "media/media_buildflags.h" #include "extensions/buildflags/buildflags.h" +#include "extensions/browser/extension_protocols.h" +#include "extensions/browser/guest_view/web_view/web_view_guest.h" #include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/remote.h" @@ -86,6 +91,8 @@ #include "qtwebengine/browser/qtwebengine_renderer_manifest.h" #include "net/ssl/client_cert_identity.h" #include "net/ssl/client_cert_store.h" +#include "services/network/network_service.h" +#include "services/network/public/cpp/features.h" #include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/service.h" #include "services/service_manager/sandbox/switches.h" @@ -111,8 +118,14 @@ #include "devtools_manager_delegate_qt.h" #include "login_delegate_qt.h" #include "media_capture_devices_dispatcher.h" +#include "net/cookie_monster_delegate_qt.h" +#include "net/custom_url_loader_factory.h" #include "net/network_delegate_qt.h" +#include "net/proxying_restricted_cookie_manager_qt.h" +#include "net/proxying_url_loader_factory_qt.h" +#include "net/qrc_url_scheme_handler.h" #include "net/url_request_context_getter_qt.h" +#include "net/system_network_context_manager.h" #include "platform_notification_service_qt.h" #if QT_CONFIG(webengine_printing_and_pdf) #include "printing/printing_message_filter_qt.h" @@ -162,6 +175,7 @@ #include <QGuiApplication> #include <QLocale> +#include <QStandardPaths> #if QT_CONFIG(opengl) # include <QOpenGLContext> # include <QOpenGLExtraFunctions> @@ -279,10 +293,12 @@ void ContentBrowserClientQt::RenderProcessWillLaunch(content::RenderProcessHost* { const int id = host->GetID(); Profile *profile = Profile::FromBrowserContext(host->GetBrowserContext()); - base::PostTaskWithTraitsAndReplyWithResult( - FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&net::URLRequestContextGetter::GetURLRequestContext, base::Unretained(profile->GetRequestContext())), - base::BindOnce(&ContentBrowserClientQt::AddNetworkHintsMessageFilter, base::Unretained(this), id)); + if (profile->GetRequestContext()) { + base::PostTaskWithTraitsAndReplyWithResult( + FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&net::URLRequestContextGetter::GetURLRequestContext, base::Unretained(profile->GetRequestContext())), + base::BindOnce(&ContentBrowserClientQt::AddNetworkHintsMessageFilter, base::Unretained(this), id)); + } // Allow requesting custom schemes. const auto policy = content::ChildProcessSecurityPolicy::GetInstance(); @@ -697,13 +713,27 @@ bool ContentBrowserClientQt::WillCreateRestrictedCookieManager(network::mojom::R int routing_id, network::mojom::RestrictedCookieManagerRequest *request) { - base::PostTaskWithTraits( - FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&ProfileIODataQt::CreateRestrictedCookieManager, - ProfileIODataQt::FromBrowserContext(browser_context)->getWeakPtrOnUIThread(), - std::move(*request), - role, origin, is_service_worker, process_id, routing_id)); - return true; + if (Profile::FromBrowserContext(browser_context)->GetRequestContext()) { + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&ProfileIODataQt::CreateRestrictedCookieManager, + ProfileIODataQt::FromBrowserContext(browser_context)->getWeakPtrOnUIThread(), + std::move(*request), + role, origin, is_service_worker, process_id, routing_id)); + return true; + } + + network::mojom::RestrictedCookieManagerRequest orig_request = std::move(*request); + network::mojom::RestrictedCookieManagerPtrInfo target_rcm_info; + *request = mojo::MakeRequest(&target_rcm_info); + + ProxyingRestrictedCookieManagerQt::CreateAndBind( + ProfileIODataQt::FromBrowserContext(browser_context), + std::move(target_rcm_info), + is_service_worker, process_id, routing_id, + std::move(orig_request)); + + return false; // only made a proxy, still need the actual impl to be made. } bool ContentBrowserClientQt::AllowAppCacheOnIO(const GURL &manifest_url, @@ -776,7 +806,7 @@ bool ContentBrowserClientQt::HandleExternalProtocol( bool has_user_gesture, network::mojom::URLLoaderFactoryPtr *out_factory) { - Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); +// Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); Q_UNUSED(child_id); Q_UNUSED(navigation_data); Q_UNUSED(out_factory); @@ -971,4 +1001,122 @@ std::string ContentBrowserClientQt::GetProduct() return productName.toStdString(); } +scoped_refptr<network::SharedURLLoaderFactory> ContentBrowserClientQt::GetSystemSharedURLLoaderFactory() +{ + if (!SystemNetworkContextManager::GetInstance()) + return nullptr; + return SystemNetworkContextManager::GetInstance()->GetSharedURLLoaderFactory(); +} + +network::mojom::NetworkContext *ContentBrowserClientQt::GetSystemNetworkContext() +{ + if (!SystemNetworkContextManager::GetInstance()) + return nullptr; + return SystemNetworkContextManager::GetInstance()->GetContext(); +} + +void ContentBrowserClientQt::OnNetworkServiceCreated(network::mojom::NetworkService *network_service) +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) + return; + + if (!SystemNetworkContextManager::GetInstance()) + SystemNetworkContextManager::CreateInstance(); + + // Need to set up global NetworkService state before anything else uses it. + SystemNetworkContextManager::GetInstance()->OnNetworkServiceCreated(network_service); +} + +network::mojom::NetworkContextPtr ContentBrowserClientQt::CreateNetworkContext(content::BrowserContext *context, + bool in_memory, + const base::FilePath &relative_partition_path) +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) + return nullptr; + + network::mojom::NetworkContextPtr network_context; + // ### do we need to pass in_memory and relative_partition_path to ProfileIODataQt::CreateNetworkContextParams() ? + network::mojom::NetworkContextParamsPtr context_params = ProfileIODataQt::FromBrowserContext(context)->CreateNetworkContextParams(); + content::GetNetworkService()->CreateNetworkContext(mojo::MakeRequest(&network_context), std::move(context_params)); + + network::mojom::CookieManagerPtrInfo cookie_manager_info; + network_context->GetCookieManager(mojo::MakeRequest(&cookie_manager_info)); + ProfileIODataQt::FromBrowserContext(context)->cookieDelegate()->setMojoCookieManager(std::move(cookie_manager_info)); + + return network_context; +} + +std::vector<base::FilePath> ContentBrowserClientQt::GetNetworkContextsParentDirectory() +{ + return { + toFilePath(QStandardPaths::writableLocation(QStandardPaths::DataLocation)), + toFilePath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) }; +} + +void ContentBrowserClientQt::RegisterNonNetworkNavigationURLLoaderFactories(int frame_tree_node_id, + NonNetworkURLLoaderFactoryMap *factories) +{ + DCHECK(base::FeatureList::IsEnabled(network::features::kNetworkService)); + content::WebContents *web_contents = content::WebContents::FromFrameTreeNodeId(frame_tree_node_id); + Profile *profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); + ProfileAdapter *profileAdapter = static_cast<ProfileQt *>(profile)->profileAdapter(); + + for (const QByteArray &scheme : profileAdapter->customUrlSchemes()) + factories->emplace(scheme.toStdString(), CreateCustomURLLoaderFactory(profileAdapter)); + +#if BUILDFLAG(ENABLE_EXTENSIONS) + factories->emplace( + extensions::kExtensionScheme, + extensions::CreateExtensionNavigationURLLoaderFactory(profile, + !!extensions::WebViewGuest::FromWebContents(web_contents))); +#endif +} + +void ContentBrowserClientQt::RegisterNonNetworkSubresourceURLLoaderFactories(int render_process_id, int render_frame_id, + NonNetworkURLLoaderFactoryMap *factories) +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) + return; + content::RenderProcessHost *process_host = content::RenderProcessHost::FromID(render_process_id); + Profile *profile = Profile::FromBrowserContext(process_host->GetBrowserContext()); + ProfileAdapter *profileAdapter = static_cast<ProfileQt *>(profile)->profileAdapter(); + + for (const QByteArray &scheme : profileAdapter->customUrlSchemes()) + factories->emplace(scheme.toStdString(), CreateCustomURLLoaderFactory(profileAdapter)); + +#if BUILDFLAG(ENABLE_EXTENSIONS) + auto factory = extensions::CreateExtensionURLLoaderFactory(render_process_id, render_frame_id); + if (factory) + factories->emplace(extensions::kExtensionScheme, std::move(factory)); +#endif +} + +bool ContentBrowserClientQt::WillCreateURLLoaderFactory( + content::BrowserContext *browser_context, + content::RenderFrameHost *frame, + int render_process_id, + bool is_navigation, + bool is_download, + const url::Origin &request_initiator, + mojo::PendingReceiver<network::mojom::URLLoaderFactory> *factory_receiver, + network::mojom::TrustedURLLoaderHeaderClientPtrInfo *header_client, + bool *bypass_redirect_checks) +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) + return false; + + auto proxied_receiver = std::move(*factory_receiver); + network::mojom::URLLoaderFactoryPtrInfo target_factory_info; + *factory_receiver = mojo::MakeRequest(&target_factory_info); + int process_id = is_navigation ? 0 : render_process_id; + + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&ProxyingURLLoaderFactoryQt::CreateProxy, process_id, + browser_context->GetResourceContext(), + std::move(proxied_receiver), + std::move(target_factory_info))); + return true; +} + } // namespace QtWebEngineCore diff --git a/src/core/content_browser_client_qt.h b/src/core/content_browser_client_qt.h index 55514afac..b58ccb8c4 100644 --- a/src/core/content_browser_client_qt.h +++ b/src/core/content_browser_client_qt.h @@ -223,6 +223,26 @@ public: bool IsHandledURL(const GURL &url) override; + bool WillCreateURLLoaderFactory(content::BrowserContext *browser_context, + content::RenderFrameHost *frame, + int render_process_id, + bool is_navigation, + bool is_download, + const url::Origin &request_initiator, + mojo::PendingReceiver<network::mojom::URLLoaderFactory> *factory_receiver, + network::mojom::TrustedURLLoaderHeaderClientPtrInfo *header_client, + bool *bypass_redirect_checks) override; + scoped_refptr<network::SharedURLLoaderFactory> GetSystemSharedURLLoaderFactory() override; + network::mojom::NetworkContext *GetSystemNetworkContext() override; + void OnNetworkServiceCreated(network::mojom::NetworkService *network_service) override; + network::mojom::NetworkContextPtr CreateNetworkContext(content::BrowserContext *context, + bool in_memory, + const base::FilePath &relative_partition_path) override; + std::vector<base::FilePath> GetNetworkContextsParentDirectory() override; + void RegisterNonNetworkNavigationURLLoaderFactories(int frame_tree_node_id, NonNetworkURLLoaderFactoryMap *factories) override; + void RegisterNonNetworkSubresourceURLLoaderFactories(int render_process_id, int render_frame_id, + NonNetworkURLLoaderFactoryMap* factories) override; + static std::string getUserAgent(); std::string GetUserAgent() override { return getUserAgent(); } diff --git a/src/core/core_chromium.pri b/src/core/core_chromium.pri index 9eab15456..bf8e6b71e 100644 --- a/src/core/core_chromium.pri +++ b/src/core/core_chromium.pri @@ -83,11 +83,15 @@ SOURCES = \ net/client_cert_store_data.cpp \ net/cookie_monster_delegate_qt.cpp \ net/custom_protocol_handler.cpp \ + net/custom_url_loader_factory.cpp \ net/network_delegate_qt.cpp \ net/proxy_config_service_qt.cpp \ + net/proxying_url_loader_factory_qt.cpp \ + net/proxying_restricted_cookie_manager_qt.cpp \ net/qrc_url_scheme_handler.cpp \ net/restricted_cookie_manager_qt.cpp \ net/ssl_host_state_delegate_qt.cpp \ + net/system_network_context_manager.cpp \ net/url_request_context_getter_qt.cpp \ net/url_request_custom_job.cpp \ net/url_request_custom_job_delegate.cpp \ @@ -194,10 +198,14 @@ HEADERS = \ net/client_cert_store_data.h \ net/cookie_monster_delegate_qt.h \ net/custom_protocol_handler.h \ + net/custom_url_loader_factory.h \ net/network_delegate_qt.h \ + net/proxying_url_loader_factory_qt.h \ + net/proxying_restricted_cookie_manager_qt.h \ net/qrc_url_scheme_handler.h \ net/restricted_cookie_manager_qt.h \ net/ssl_host_state_delegate_qt.h \ + net/system_network_context_manager.h \ net/url_request_context_getter_qt.h \ net/url_request_custom_job.h \ net/url_request_custom_job_delegate.h \ diff --git a/src/core/net/cookie_monster_delegate_qt.cpp b/src/core/net/cookie_monster_delegate_qt.cpp index dad9a8db5..e0fa60be7 100644 --- a/src/core/net/cookie_monster_delegate_qt.cpp +++ b/src/core/net/cookie_monster_delegate_qt.cpp @@ -44,7 +44,9 @@ #include "base/task/post_task.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" +#include "mojo/public/cpp/bindings/binding.h" #include "net/cookies/cookie_util.h" +#include "services/network/public/mojom/cookie_manager.mojom.h" #include "api/qwebenginecookiestore.h" #include "api/qwebenginecookiestore_p.h" @@ -52,6 +54,25 @@ namespace QtWebEngineCore { +class CookieChangeListener : public network::mojom::CookieChangeListener +{ +public: + CookieChangeListener(CookieMonsterDelegateQt *delegate) : m_delegate(delegate) { } + ~CookieChangeListener() override = default; + + // network::mojom::CookieChangeListener: + void OnCookieChange(const net::CanonicalCookie &canonical_cookie, + network::mojom::CookieChangeCause cause) override + { + m_delegate->OnCookieChanged(canonical_cookie, net::CookieChangeCause(cause)); + } + +private: + CookieMonsterDelegateQt *m_delegate; + + DISALLOW_COPY_AND_ASSIGN(CookieChangeListener); +}; + static GURL sourceUrlForCookie(const QNetworkCookie &cookie) { QString urlFragment = QStringLiteral("%1%2").arg(cookie.domain()).arg(cookie.path()); @@ -61,6 +82,8 @@ static GURL sourceUrlForCookie(const QNetworkCookie &cookie) CookieMonsterDelegateQt::CookieMonsterDelegateQt() : m_client(0) , m_cookieMonster(nullptr) + , m_listener(new CookieChangeListener(this)) + , m_binding(m_listener.get()) { } @@ -81,20 +104,24 @@ void CookieMonsterDelegateQt::AddStore(net::CookieStore *store) bool CookieMonsterDelegateQt::hasCookieMonster() { - return m_cookieMonster; + return m_cookieMonster || m_mojoCookieManager.is_bound(); } void CookieMonsterDelegateQt::getAllCookies(quint64 callbackId) { - net::CookieMonster::GetCookieListCallback callback = + if (m_mojoCookieManager.is_bound()) { + m_mojoCookieManager->GetAllCookies(base::BindOnce(&CookieMonsterDelegateQt::GetAllCookiesCallbackOnUIThread, this, callbackId)); + } else { + net::CookieMonster::GetCookieListCallback callback = base::BindOnce(&CookieMonsterDelegateQt::GetAllCookiesCallbackOnIOThread, this, callbackId); - - base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&CookieMonsterDelegateQt::GetAllCookiesOnIOThread, this, std::move(callback))); + base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&CookieMonsterDelegateQt::GetAllCookiesOnIOThread, this, std::move(callback))); + } } void CookieMonsterDelegateQt::GetAllCookiesOnIOThread(net::CookieMonster::GetCookieListCallback callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); if (m_cookieMonster) m_cookieMonster->GetAllCookiesAsync(std::move(callback)); } @@ -105,19 +132,30 @@ void CookieMonsterDelegateQt::setCookie(quint64 callbackId, const QNetworkCookie Q_ASSERT(m_client); net::CookieStore::SetCookiesCallback callback; - if (callbackId != CallbackDirectory::NoCallbackId) - callback = base::BindOnce(&CookieMonsterDelegateQt::SetCookieCallbackOnIOThread, this, callbackId); GURL gurl = origin.isEmpty() ? sourceUrlForCookie(cookie) : toGurl(origin); - - base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&CookieMonsterDelegateQt::SetCookieOnIOThread, this, - gurl, cookie.toRawForm().toStdString(), std::move(callback))); + std::string cookie_line = cookie.toRawForm().toStdString(); + + if (m_mojoCookieManager.is_bound()) { + if (callbackId != CallbackDirectory::NoCallbackId) + callback = base::BindOnce(&CookieMonsterDelegateQt::SetCookieCallbackOnUIThread, this, callbackId); + net::CookieOptions options; + options.set_include_httponly(); + auto cookie = net::CanonicalCookie::Create(gurl, cookie_line, base::Time::Now(), options); + m_mojoCookieManager->SetCanonicalCookie(*cookie.get(), gurl.scheme(), options, std::move(callback)); + } else { + if (callbackId != CallbackDirectory::NoCallbackId) + callback = base::BindOnce(&CookieMonsterDelegateQt::SetCookieCallbackOnIOThread, this, callbackId); + base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&CookieMonsterDelegateQt::SetCookieOnIOThread, this, + gurl, std::move(cookie_line), std::move(callback))); + } } void CookieMonsterDelegateQt::SetCookieOnIOThread(const GURL &url, const std::string &cookie_line, net::CookieMonster::SetCookiesCallback callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); net::CookieOptions options; options.set_include_httponly(); @@ -131,14 +169,22 @@ void CookieMonsterDelegateQt::deleteCookie(const QNetworkCookie &cookie, const Q Q_ASSERT(m_client); GURL gurl = origin.isEmpty() ? sourceUrlForCookie(cookie) : toGurl(origin); - - base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&CookieMonsterDelegateQt::DeleteCookieOnIOThread, this, - gurl, cookie.name().toStdString())); + std::string cookie_name = cookie.name().toStdString(); + if (m_mojoCookieManager.is_bound()) { + auto filter = network::mojom::CookieDeletionFilter::New(); + filter->url = gurl; + filter->cookie_name = cookie_name; + m_mojoCookieManager->DeleteCookies(std::move(filter), network::mojom::CookieManager::DeleteCookiesCallback()); + } else { + base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&CookieMonsterDelegateQt::DeleteCookieOnIOThread, this, + gurl, cookie_name)); + } } void CookieMonsterDelegateQt::DeleteCookieOnIOThread(const GURL &url, const std::string &cookie_name) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); if (m_cookieMonster) { net::CookieMonster::GetCookieListCallback callback = base::BindOnce(&CookieMonsterDelegateQt::GetCookiesToDeleteCallback, this, cookie_name); @@ -168,14 +214,23 @@ void CookieMonsterDelegateQt::deleteSessionCookies(quint64 callbackId) Q_ASSERT(hasCookieMonster()); Q_ASSERT(m_client); - net::CookieMonster::DeleteCallback callback = - base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnIOThread, this, callbackId); - base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&CookieMonsterDelegateQt::DeleteSessionCookiesOnIOThread, this, std::move(callback))); + if (m_mojoCookieManager.is_bound()) { + net::CookieMonster::DeleteCallback callback = + base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnUIThread, this, callbackId); + auto filter = network::mojom::CookieDeletionFilter::New(); + filter->session_control = network::mojom::CookieDeletionSessionControl::SESSION_COOKIES; + m_mojoCookieManager->DeleteCookies(std::move(filter), std::move(callback)); + } else { + net::CookieMonster::DeleteCallback callback = + base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnIOThread, this, callbackId); + base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&CookieMonsterDelegateQt::DeleteSessionCookiesOnIOThread, this, std::move(callback))); + } } void CookieMonsterDelegateQt::DeleteSessionCookiesOnIOThread(net::CookieMonster::DeleteCallback callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); if (m_cookieMonster) m_cookieMonster->DeleteSessionCookiesAsync(std::move(callback)); } @@ -185,14 +240,22 @@ void CookieMonsterDelegateQt::deleteAllCookies(quint64 callbackId) Q_ASSERT(hasCookieMonster()); Q_ASSERT(m_client); - net::CookieMonster::DeleteCallback callback = - base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnIOThread, this, callbackId); - base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&CookieMonsterDelegateQt::DeleteAllOnIOThread, this, std::move(callback))); + if (m_mojoCookieManager.is_bound()) { + net::CookieMonster::DeleteCallback callback = + base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnUIThread, this, callbackId); + auto filter = network::mojom::CookieDeletionFilter::New(); + m_mojoCookieManager->DeleteCookies(std::move(filter), std::move(callback)); + } else { + net::CookieMonster::DeleteCallback callback = + base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnIOThread, this, callbackId); + base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&CookieMonsterDelegateQt::DeleteAllOnIOThread, this, std::move(callback))); + } } void CookieMonsterDelegateQt::DeleteAllOnIOThread(net::CookieMonster::DeleteCallback callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); if (m_cookieMonster) m_cookieMonster->DeleteAllAsync(std::move(callback)); } @@ -217,6 +280,26 @@ void CookieMonsterDelegateQt::setCookieMonster(net::CookieMonster *monster) m_client->d_func()->rejectPendingUserCookies(); } +void CookieMonsterDelegateQt::setMojoCookieManager(network::mojom::CookieManagerPtrInfo cookie_manager_info) +{ +// DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + m_mojoCookieManager.Bind(std::move(cookie_manager_info)); + + network::mojom::CookieChangeListenerPtr listener_ptr; + m_binding.Bind(mojo::MakeRequest(&listener_ptr)); + + m_mojoCookieManager->AddGlobalChangeListener(std::move(listener_ptr)); + + if (m_client) + m_client->d_func()->processPendingUserCookies(); +} + +void CookieMonsterDelegateQt::unsetMojoCookieManager() +{ + m_binding.Close(); + m_mojoCookieManager.reset(); +} + void CookieMonsterDelegateQt::setClient(QWebEngineCookieStore *client) { m_client = client; @@ -255,17 +338,29 @@ void CookieMonsterDelegateQt::OnCookieChanged(const net::CanonicalCookie &cookie void CookieMonsterDelegateQt::GetAllCookiesCallbackOnIOThread(qint64 callbackId, const net::CookieList &cookies, const net::CookieStatusList &statusList) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); QByteArray rawCookies; for (auto &&cookie : cookies) rawCookies += toQt(cookie).toRawForm() % QByteArrayLiteral("\n"); base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(&CookieMonsterDelegateQt::GetAllCookiesCallbackOnUIThread, this, callbackId, rawCookies)); + base::BindOnce(&CookieMonsterDelegateQt::GetAllCookiesResultOnUIThread, this, callbackId, rawCookies)); +} + +void CookieMonsterDelegateQt::GetAllCookiesCallbackOnUIThread(qint64 callbackId, const std::vector<net::CanonicalCookie> &cookies) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + QByteArray rawCookies; + for (auto &&cookie : cookies) + rawCookies += toQt(cookie).toRawForm() % QByteArrayLiteral("\n"); + + GetAllCookiesResultOnUIThread(callbackId, rawCookies); } void CookieMonsterDelegateQt::SetCookieCallbackOnIOThread(qint64 callbackId, net::CanonicalCookie::CookieInclusionStatus status) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(&CookieMonsterDelegateQt::SetCookieCallbackOnUIThread, this, callbackId, status)); @@ -273,12 +368,13 @@ void CookieMonsterDelegateQt::SetCookieCallbackOnIOThread(qint64 callbackId, net void CookieMonsterDelegateQt::DeleteCookiesCallbackOnIOThread(qint64 callbackId, uint numCookies) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(&CookieMonsterDelegateQt::DeleteCookiesCallbackOnUIThread, this, callbackId, numCookies)); } -void CookieMonsterDelegateQt::GetAllCookiesCallbackOnUIThread(qint64 callbackId, const QByteArray &cookies) +void CookieMonsterDelegateQt::GetAllCookiesResultOnUIThread(qint64 callbackId, const QByteArray &cookies) { if (m_client) m_client->d_func()->onGetAllCallbackResult(callbackId, cookies); diff --git a/src/core/net/cookie_monster_delegate_qt.h b/src/core/net/cookie_monster_delegate_qt.h index 23b803790..02c84e061 100644 --- a/src/core/net/cookie_monster_delegate_qt.h +++ b/src/core/net/cookie_monster_delegate_qt.h @@ -57,8 +57,21 @@ QT_WARNING_PUSH // For some reason adding -Wno-unused-parameter to QMAKE_CXXFLAGS has no // effect with clang, so use a pragma for these dirty chromium headers QT_WARNING_DISABLE_CLANG("-Wunused-parameter") + +// We need to work around Chromium using 'signals' as a variable name in headers: +#ifdef signals +#define StAsH_signals signals +#undef signals +#endif #include "base/memory/ref_counted.h" +#include "mojo/public/cpp/bindings/binding.h" #include "net/cookies/cookie_monster.h" +#include "services/network/public/mojom/cookie_manager.mojom-forward.h" +#include "services/network/public/mojom/cookie_manager.mojom.h" +#ifdef StAsH_signals +#define signals StAsH_signals +#undef StAsH_signals +#endif QT_WARNING_POP #include <QNetworkCookie> @@ -74,12 +87,17 @@ namespace QtWebEngineCore { // to correctly handle file:// cookies, qrc:// should only be used for testing. static const char *const kCookieableSchemes[] = { "http", "https", "qrc", "ws", "wss" }; +class CookieMonsterDelegateQtPrivate; + class Q_WEBENGINECORE_PRIVATE_EXPORT CookieMonsterDelegateQt : public base::RefCountedThreadSafe<CookieMonsterDelegateQt> { QPointer<QWebEngineCookieStore> m_client; net::CookieMonster *m_cookieMonster; std::vector<std::unique_ptr<net::CookieChangeSubscription>> m_subscriptions; + network::mojom::CookieManagerPtr m_mojoCookieManager; + std::unique_ptr<network::mojom::CookieChangeListener> m_listener; + mojo::Binding<network::mojom::CookieChangeListener> m_binding; public: CookieMonsterDelegateQt(); ~CookieMonsterDelegateQt(); @@ -94,6 +112,8 @@ public: void setCookieMonster(net::CookieMonster *monster); void setClient(QWebEngineCookieStore *client); + void setMojoCookieManager(network::mojom::CookieManagerPtrInfo cookie_manager_info); + void unsetMojoCookieManager(); bool canSetCookie(const QUrl &firstPartyUrl, const QByteArray &cookieLine, const QUrl &url) const; bool canGetCookies(const QUrl &firstPartyUrl, const QUrl &url) const; @@ -116,7 +136,8 @@ private: void SetCookieCallbackOnIOThread(qint64 callbackId, net::CanonicalCookie::CookieInclusionStatus status); void DeleteCookiesCallbackOnIOThread(qint64 callbackId, uint numCookies); - void GetAllCookiesCallbackOnUIThread(qint64 callbackId, const QByteArray &cookies); + void GetAllCookiesCallbackOnUIThread(qint64 callbackId, const std::vector<net::CanonicalCookie> &cookies); + void GetAllCookiesResultOnUIThread(qint64 callbackId, const QByteArray &cookies); void SetCookieCallbackOnUIThread(qint64 callbackId, net::CanonicalCookie::CookieInclusionStatus status); void DeleteCookiesCallbackOnUIThread(qint64 callbackId, uint numCookies); }; diff --git a/src/core/net/custom_url_loader_factory.cpp b/src/core/net/custom_url_loader_factory.cpp new file mode 100644 index 000000000..a9e7e1c34 --- /dev/null +++ b/src/core/net/custom_url_loader_factory.cpp @@ -0,0 +1,401 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "custom_url_loader_factory.h" + +#include "base/strings/stringprintf.h" +#include "base/task/post_task.h" +#include "content/public/browser/browser_task_traits.h" +#include "content/public/browser/browser_thread.h" +#include "mojo/public/cpp/bindings/binding_set.h" +#include "mojo/public/cpp/system/data_pipe.h" +#include "mojo/public/cpp/system/data_pipe_producer.h" +#include "net/base/net_errors.h" +#include "net/http/http_status_code.h" +#include "net/http/http_util.h" +#include "services/network/public/mojom/url_loader.mojom.h" +#include "services/network/public/mojom/url_loader_factory.mojom.h" + +#include "api/qwebengineurlscheme.h" +#include "net/url_request_custom_job_proxy.h" +#include "profile_adapter.h" +#include "type_conversion.h" + +#include <QtCore/qbytearray.h> +#include <QtCore/qfile.h> +#include <QtCore/qfileinfo.h> +#include <QtCore/qiodevice.h> +#include <QtCore/qmimedatabase.h> +#include <QtCore/qmimedata.h> +#include <QtCore/qurl.h> + +namespace QtWebEngineCore { + +namespace { + +class CustomURLLoader : public network::mojom::URLLoader + , private URLRequestCustomJobProxy::Client +{ +public: + static void CreateAndStart(const network::ResourceRequest &request, + network::mojom::URLLoaderRequest loader, + network::mojom::URLLoaderClientPtrInfo client_info, + QPointer<ProfileAdapter> profileAdapter) + { + // CustomURLLoader will handle its own life-cycle, and delete when + // the client lets go. + auto *customUrlLoader = new CustomURLLoader(request, std::move(loader), std::move(client_info), profileAdapter); + customUrlLoader->Start(); + } + + // network::mojom::URLLoader: + void FollowRedirect(const std::vector<std::string> &removed_headers, + const net::HttpRequestHeaders &modified_headers, + const base::Optional<GURL> &new_url) override + { + // We can be asked for follow our own redirect + scoped_refptr<URLRequestCustomJobProxy> proxy = new URLRequestCustomJobProxy(this, m_proxy->m_scheme, m_proxy->m_profileAdapter); + m_proxy->m_client = nullptr; +// m_taskRunner->PostTask(FROM_HERE, base::BindOnce(&URLRequestCustomJobProxy::release, m_proxy)); + base::PostTaskWithTraits(FROM_HERE, { content::BrowserThread::UI }, + base::BindOnce(&URLRequestCustomJobProxy::release, m_proxy)); + m_proxy = std::move(proxy); + if (new_url) + m_request.url = *new_url; + else + m_request.url = m_redirect; + // ### remove and modify headers? + m_redirect = GURL(); + Start(); + } + void SetPriority(net::RequestPriority priority, int32_t intra_priority_value) override { } + void PauseReadingBodyFromNet() override { } + void ResumeReadingBodyFromNet() override { } + void ProceedWithResponse() override { } + +private: + CustomURLLoader(const network::ResourceRequest &request, + network::mojom::URLLoaderRequest loader, + network::mojom::URLLoaderClientPtrInfo client_info, + QPointer<ProfileAdapter> profileAdapter) + // ### We can opt to run the url-loader on the UI thread instead + : m_taskRunner(base::CreateSingleThreadTaskRunner({ content::BrowserThread::IO })) + , m_proxy(new URLRequestCustomJobProxy(this, request.url.scheme(), profileAdapter)) + , m_binding(this, std::move(loader)) + , m_client(std::move(client_info)) + , m_request(request) + { + m_binding.set_connection_error_handler( + base::BindOnce(&CustomURLLoader::OnConnectionError, base::Unretained(this))); + m_device = nullptr; + m_error = 0; + QWebEngineUrlScheme scheme = QWebEngineUrlScheme::schemeByName(QByteArray::fromStdString(request.url.scheme())); + m_corsEnabled = scheme.flags().testFlag(QWebEngineUrlScheme::CorsEnabled); + } + + ~CustomURLLoader() override = default; + + void Start() + { + m_head.request_start = base::TimeTicks::Now(); + + if (!m_pipe.consumer_handle.is_valid()) + return CompleteWithFailure(net::ERR_FAILED); + + std::map<std::string, std::string> headers; + net::HttpRequestHeaders::Iterator it(m_request.headers); + while (it.GetNext()) + headers.emplace(it.name(), it.value()); + if (!m_request.referrer.is_empty()) + headers.emplace("Referer", m_request.referrer.spec()); + +// m_taskRunner->PostTask(FROM_HERE, + base::PostTaskWithTraits(FROM_HERE, { content::BrowserThread::UI }, + base::BindOnce(&URLRequestCustomJobProxy::initialize, m_proxy, + m_request.url, m_request.method, m_request.request_initiator, std::move(headers))); + } + + void CompleteWithFailure(net::Error net_error) + { + m_client->OnComplete(network::URLLoaderCompletionStatus(net_error)); + ClearProxyAndClient(false); + } + + void OnConnectionError() + { + m_binding.Close(); + if (m_client.is_bound()) + ClearProxyAndClient(false); + else + delete this; + } + + void OnTransferComplete(MojoResult result) + { + if (result == MOJO_RESULT_OK) { + network::URLLoaderCompletionStatus status(net::OK); + status.encoded_data_length = m_totalBytesRead + m_head.headers->raw_headers().length(); + status.encoded_body_length = m_totalBytesRead; + status.decoded_body_length = m_totalBytesRead; + m_client->OnComplete(status); + } else { + m_client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED)); + } + ClearProxyAndClient(false /* result == MOJO_RESULT_OK */); + } + + void ClearProxyAndClient(bool wait_for_loader_error = false) + { + m_proxy->m_client = nullptr; + m_client.reset(); + if (m_device && m_device->isOpen()) + m_device->close(); + m_device = nullptr; +// m_taskRunner->PostTask(FROM_HERE, base::BindOnce(&URLRequestCustomJobProxy::release, m_proxy)); + base::PostTaskWithTraits(FROM_HERE, { content::BrowserThread::UI }, + base::BindOnce(&URLRequestCustomJobProxy::release, m_proxy)); + if (!wait_for_loader_error || !m_binding.is_bound()) + delete this; + } + + // URLRequestCustomJobProxy::Client: + void notifyExpectedContentSize(qint64 size) override + { + m_head.content_length = size; + } + void notifyHeadersComplete() override + { + m_taskRunner->PostTask(FROM_HERE, + base::BindOnce(&CustomURLLoader::reportHeadersComplete, base::Unretained(this))); + } + void reportHeadersComplete() + { + DCHECK(!m_error); + m_head.response_start = base::TimeTicks::Now(); + + std::string headers; + if (!m_redirect.is_empty()) { + headers += "HTTP/1.1 303 See Other\n"; + headers += base::StringPrintf("Location: %s\n", m_redirect.spec().c_str()); + } else { + headers += "HTTP/1.1 200 OK\n"; + if (m_mimeType.size() > 0) { + headers += base::StringPrintf("Content-Type: %s", m_mimeType.c_str()); + if (m_charset.size() > 0) + headers += base::StringPrintf("; charset=%s", m_charset.c_str()); + headers += "\n"; + } + } + if (m_corsEnabled) { + std::string origin; + if (m_request.headers.GetHeader("Origin", &origin)) { + headers += base::StringPrintf("Access-Control-Allow-Origin: %s\n", origin.c_str()); + headers += "Access-Control-Allow-Credentials: true\n"; + } + } + m_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>(net::HttpUtil::AssembleRawHeaders(headers)); + m_head.encoded_data_length = m_head.headers->raw_headers().length(); + + if (!m_redirect.is_empty()) { + m_head.content_length = m_head.encoded_body_length = -1; + net::URLRequest::FirstPartyURLPolicy first_party_url_policy = + m_request.update_first_party_url_on_redirect ? net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT + : net::URLRequest::NEVER_CHANGE_FIRST_PARTY_URL; + net::RedirectInfo redirectInfo = net::RedirectInfo::ComputeRedirectInfo( + m_request.method, m_request.url, + m_request.site_for_cookies, m_request.top_frame_origin, + first_party_url_policy, m_request.referrer_policy, + m_request.referrer.spec(), net::HTTP_SEE_OTHER, + m_redirect, base::nullopt, false /*insecure_scheme_was_upgraded*/); + m_client->OnReceiveRedirect(redirectInfo, m_head); + // ### should m_request be updated with RedirectInfo? (see FollowRedirect) + return; + } + DCHECK(m_device); + m_head.mime_type = m_mimeType; + m_head.charset = m_charset; + m_client->OnReceiveResponse(m_head); + m_client->OnStartLoadingResponseBody(std::move(m_pipe.consumer_handle)); + + readAvailableData(); + } + void notifyCanceled() override + { + OnTransferComplete(MOJO_RESULT_CANCELLED); + } + void notifyAborted() override + { + notifyStartFailure(net::ERR_ABORTED); + } + void notifyStartFailure(int error) override + { + m_head.response_start = base::TimeTicks::Now(); + std::string headers; + switch (error) { + case net::ERR_INVALID_URL: + headers = "HTTP/1.1 400 Bad Request\n"; + break; + case net::ERR_FILE_NOT_FOUND: + headers = "HTTP/1.1 404 Not Found\n"; + break; + case net::ERR_ABORTED: + headers = "HTTP/1.1 503 Request Aborted\n"; + break; + case net::ERR_ACCESS_DENIED: + headers = "HTTP/1.1 403 Forbidden\n"; + break; + case net::ERR_FAILED: + headers = "HTTP/1.1 400 Request Failed\n"; + break; + default: + headers = "HTTP/1.1 500 Internal Error\n"; + break; + } + m_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>(net::HttpUtil::AssembleRawHeaders(headers)); + m_head.encoded_data_length = m_head.headers->raw_headers().length(); + m_head.content_length = m_head.encoded_body_length = -1; + m_client->OnReceiveResponse(m_head); + CompleteWithFailure(net::Error(error)); + } + void notifyReadyRead() override + { + m_taskRunner->PostTask(FROM_HERE, + base::BindOnce(&CustomURLLoader::readAvailableData, base::Unretained(this))); + } + void readAvailableData() + { + if (m_error) { + CompleteWithFailure(net::Error(m_error)); + return; + } + if (!m_device) { + CompleteWithFailure(net::ERR_FAILED); + return; + } + char buffer[2048]; + do { + int read_size = m_device->read(buffer, 2048); + if (m_error) { + CompleteWithFailure(net::Error(m_error)); + return; + } + if (read_size > 0) { + uint32_t read_bytes = read_size; + m_pipe.producer_handle->WriteData(buffer, &read_bytes, MOJO_WRITE_DATA_FLAG_NONE); + m_totalBytesRead += read_bytes; + } else if (read_size < 0 && !m_device->atEnd()) { + CompleteWithFailure(net::ERR_FAILED); + return; + } + } while (m_device->bytesAvailable()); + m_client->OnTransferSizeUpdated(m_totalBytesRead); + if (m_device->atEnd()) + OnTransferComplete(MOJO_RESULT_OK); + } + base::TaskRunner *taskRunner() override + { + return m_taskRunner.get(); + } + + scoped_refptr<base::TaskRunner> m_taskRunner; + scoped_refptr<URLRequestCustomJobProxy> m_proxy; + + mojo::Binding<network::mojom::URLLoader> m_binding; + network::mojom::URLLoaderClientPtr m_client; + mojo::DataPipe m_pipe; + + network::ResourceRequest m_request; + network::ResourceResponseHead m_head; + qint64 m_totalBytesRead = 0; + bool m_corsEnabled; + + DISALLOW_COPY_AND_ASSIGN(CustomURLLoader); +}; + +class CustomURLLoaderFactory : public network::mojom::URLLoaderFactory { +public: + CustomURLLoaderFactory(ProfileAdapter *profileAdapter) + : m_taskRunner(base::CreateSequencedTaskRunner({ content::BrowserThread::IO })) + , m_profileAdapter(profileAdapter) + { + } + ~CustomURLLoaderFactory() override = default; + + // network::mojom::URLLoaderFactory: + void CreateLoaderAndStart(network::mojom::URLLoaderRequest loader, + int32_t routing_id, + int32_t request_id, + uint32_t options, + const network::ResourceRequest &request, + network::mojom::URLLoaderClientPtr client, + const net::MutableNetworkTrafficAnnotationTag &traffic_annotation) override + { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + Q_UNUSED(routing_id); + Q_UNUSED(request_id); + Q_UNUSED(options); + Q_UNUSED(traffic_annotation); + + m_taskRunner->PostTask(FROM_HERE, + base::BindOnce(&CustomURLLoader::CreateAndStart, request, + std::move(loader), client.PassInterface(), + m_profileAdapter)); + + } + + void Clone(network::mojom::URLLoaderFactoryRequest request) override + { + m_bindings.AddBinding(this, std::move(request)); + } + + const scoped_refptr<base::SequencedTaskRunner> m_taskRunner; + mojo::BindingSet<network::mojom::URLLoaderFactory> m_bindings; + QPointer<ProfileAdapter> m_profileAdapter; + DISALLOW_COPY_AND_ASSIGN(CustomURLLoaderFactory); +}; + +} // namespace + +std::unique_ptr<network::mojom::URLLoaderFactory> CreateCustomURLLoaderFactory(ProfileAdapter *profileAdapter) +{ + return std::make_unique<CustomURLLoaderFactory>(profileAdapter); +} + +} // namespace QtWebEngineCore + diff --git a/src/core/net/custom_url_loader_factory.h b/src/core/net/custom_url_loader_factory.h new file mode 100644 index 000000000..58adf4b79 --- /dev/null +++ b/src/core/net/custom_url_loader_factory.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef CUSTOM_URL_LOADER_FACTORY_H_ +#define CUSTOM_URL_LOADER_FACTORY_H_ + +#include <memory> + +namespace network { +namespace mojom { +class URLLoaderFactory; +} // namespace mojom +} // namespace network + +namespace QtWebEngineCore { +class ProfileAdapter; + +std::unique_ptr<network::mojom::URLLoaderFactory> CreateCustomURLLoaderFactory(ProfileAdapter *profileAdapter); + +} // namespace QtWebEngineCore + +#endif // CUSTOM_URL_LOADER_FACTORY_H_ diff --git a/src/core/net/proxying_restricted_cookie_manager_qt.cpp b/src/core/net/proxying_restricted_cookie_manager_qt.cpp new file mode 100644 index 000000000..b6abeb567 --- /dev/null +++ b/src/core/net/proxying_restricted_cookie_manager_qt.cpp @@ -0,0 +1,231 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// originally based on android_webview/browser/network_service/aw_proxying_restricted_cookie_manager.cc: +// Copyright 2019 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 "proxying_restricted_cookie_manager_qt.h" + +#include "api/qwebenginecookiestore.h" +#include "api/qwebenginecookiestore_p.h" +#include "profile_adapter.h" +#include "profile_qt.h" +#include "type_conversion.h" + +#include "base/memory/ptr_util.h" +#include "base/task/post_task.h" +#include "content/public/browser/browser_task_traits.h" +#include "content/public/browser/browser_thread.h" +#include "mojo/public/cpp/bindings/strong_binding.h" + +namespace QtWebEngineCore { + +class ProxyingRestrictedCookieManagerListenerQt : public network::mojom::CookieChangeListener { +public: + ProxyingRestrictedCookieManagerListenerQt(const GURL &url, + const GURL &site_for_cookies, + base::WeakPtr<ProxyingRestrictedCookieManagerQt> restricted_cookie_manager, + network::mojom::CookieChangeListenerPtr client_listener) + : url_(url) + , site_for_cookies_(site_for_cookies) + , restricted_cookie_manager_(restricted_cookie_manager) + , client_listener_(std::move(client_listener)) + {} + + void OnCookieChange(const net::CanonicalCookie &cookie, network::mojom::CookieChangeCause cause) override + { + if (restricted_cookie_manager_ && restricted_cookie_manager_->allowCookies(url_, site_for_cookies_)) + client_listener_->OnCookieChange(cookie, cause); + } + +private: + const GURL url_; + const GURL site_for_cookies_; + base::WeakPtr<ProxyingRestrictedCookieManagerQt> restricted_cookie_manager_; + network::mojom::CookieChangeListenerPtr client_listener_; +}; + +// static +void ProxyingRestrictedCookieManagerQt::CreateAndBind(ProfileIODataQt *profileIoData, + network::mojom::RestrictedCookieManagerPtrInfo underlying_rcm, + bool is_service_worker, + int process_id, + int frame_id, + network::mojom::RestrictedCookieManagerRequest request) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&ProxyingRestrictedCookieManagerQt::CreateAndBindOnIoThread, + profileIoData, + std::move(underlying_rcm), + is_service_worker, process_id, frame_id, + std::move(request))); +} + + +// static +void ProxyingRestrictedCookieManagerQt::CreateAndBindOnIoThread(ProfileIODataQt *profileIoData, + network::mojom::RestrictedCookieManagerPtrInfo underlying_rcm, + bool is_service_worker, + int process_id, + int frame_id, + network::mojom::RestrictedCookieManagerRequest request) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + auto wrapper = base::WrapUnique(new ProxyingRestrictedCookieManagerQt( + profileIoData->getWeakPtrOnIOThread(), + network::mojom::RestrictedCookieManagerPtr(std::move(underlying_rcm)), + is_service_worker, process_id, frame_id)); + mojo::MakeStrongBinding(std::move(wrapper), std::move(request)); +} + +ProxyingRestrictedCookieManagerQt::ProxyingRestrictedCookieManagerQt(base::WeakPtr<ProfileIODataQt> profileIoData, + network::mojom::RestrictedCookieManagerPtr underlyingRestrictedCookieManager, + bool is_service_worker, + int32_t process_id, + int32_t frame_id) + : m_profileIoData(std::move(profileIoData)) + , underlying_restricted_cookie_manager_(std::move(underlyingRestrictedCookieManager)) + , is_service_worker_(is_service_worker) + , process_id_(process_id) + , frame_id_(frame_id) + , weak_factory_(this) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); +} + +ProxyingRestrictedCookieManagerQt::~ProxyingRestrictedCookieManagerQt() +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); +} + +void ProxyingRestrictedCookieManagerQt::GetAllForUrl(const GURL &url, + const GURL &site_for_cookies, + network::mojom::CookieManagerGetOptionsPtr options, + GetAllForUrlCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + if (allowCookies(url, site_for_cookies)) { + underlying_restricted_cookie_manager_->GetAllForUrl(url, site_for_cookies, std::move(options), std::move(callback)); + } else { + std::move(callback).Run(std::vector<net::CanonicalCookie>()); + } +} + +void ProxyingRestrictedCookieManagerQt::SetCanonicalCookie(const net::CanonicalCookie &cookie, + const GURL &url, + const GURL &site_for_cookies, + SetCanonicalCookieCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + if (allowCookies(url, site_for_cookies)) { + underlying_restricted_cookie_manager_->SetCanonicalCookie(cookie, url, site_for_cookies, std::move(callback)); + } else { + std::move(callback).Run(false); + } +} + +void ProxyingRestrictedCookieManagerQt::AddChangeListener(const GURL &url, + const GURL &site_for_cookies, + network::mojom::CookieChangeListenerPtr listener, + AddChangeListenerCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + network::mojom::CookieChangeListenerPtr proxy_listener_ptr; + auto proxy_listener = + std::make_unique<ProxyingRestrictedCookieManagerListenerQt>( + url, site_for_cookies, weak_factory_.GetWeakPtr(), + std::move(listener)); + + mojo::MakeStrongBinding(std::move(proxy_listener), + mojo::MakeRequest(&proxy_listener_ptr)); + + underlying_restricted_cookie_manager_->AddChangeListener(url, site_for_cookies, std::move(proxy_listener_ptr), std::move(callback)); +} + +void ProxyingRestrictedCookieManagerQt::SetCookieFromString(const GURL &url, + const GURL &site_for_cookies, + const std::string &cookie, + SetCookieFromStringCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + if (allowCookies(url, site_for_cookies)) { + underlying_restricted_cookie_manager_->SetCookieFromString(url, site_for_cookies, cookie, std::move(callback)); + } else { + std::move(callback).Run(); + } +} + +void ProxyingRestrictedCookieManagerQt::GetCookiesString(const GURL &url, + const GURL &site_for_cookies, + GetCookiesStringCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + if (allowCookies(url, site_for_cookies)) { + underlying_restricted_cookie_manager_->GetCookiesString(url, site_for_cookies, std::move(callback)); + } else { + std::move(callback).Run(""); + } +} + +void ProxyingRestrictedCookieManagerQt::CookiesEnabledFor(const GURL &url, + const GURL &site_for_cookies, + CookiesEnabledForCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + std::move(callback).Run(allowCookies(url, site_for_cookies)); +} + +bool ProxyingRestrictedCookieManagerQt::allowCookies(const GURL &url, const GURL &site_for_cookies) const +{ + if (!m_profileIoData) + return false; + return m_profileIoData->canGetCookies(toQt(site_for_cookies), toQt(url)); +} + +} // namespace QtWebEngineCore diff --git a/src/core/net/proxying_restricted_cookie_manager_qt.h b/src/core/net/proxying_restricted_cookie_manager_qt.h new file mode 100644 index 000000000..7a9f0e3ab --- /dev/null +++ b/src/core/net/proxying_restricted_cookie_manager_qt.h @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PROXYING_RESTRICTED_COOKIE_MANAGER_QT_H +#define PROXYING_RESTRICTED_COOKIE_MANAGER_QT_H + +#include "base/macros.h" +#include "base/memory/weak_ptr.h" +#include "services/network/public/mojom/restricted_cookie_manager.mojom.h" +#include "url/gurl.h" + +namespace QtWebEngineCore { + +class ProfileIODataQt; + +class ProxyingRestrictedCookieManagerQt : public network::mojom::RestrictedCookieManager +{ +public: + // Expects to be called on the UI thread. + static void CreateAndBind(ProfileIODataQt *profileIoData, + network::mojom::RestrictedCookieManagerPtrInfo underlying_rcm, + bool is_service_worker, + int process_id, + int frame_id, + network::mojom::RestrictedCookieManagerRequest request); + + ~ProxyingRestrictedCookieManagerQt() override; + + // network::mojom::RestrictedCookieManager interface: + void GetAllForUrl(const GURL &url, + const GURL &site_for_cookies, + network::mojom::CookieManagerGetOptionsPtr options, + GetAllForUrlCallback callback) override; + void SetCanonicalCookie(const net::CanonicalCookie& cookie, + const GURL &url, + const GURL &site_for_cookies, + SetCanonicalCookieCallback callback) override; + void AddChangeListener(const GURL &url, + const GURL &site_for_cookies, + network::mojom::CookieChangeListenerPtr listener, + AddChangeListenerCallback callback) override; + void SetCookieFromString(const GURL &url, + const GURL &site_for_cookies, + const std::string &cookie, + SetCookieFromStringCallback callback) override; + void GetCookiesString(const GURL &url, + const GURL &site_for_cookies, + GetCookiesStringCallback callback) override; + + void CookiesEnabledFor(const GURL &url, + const GURL &site_for_cookies, + CookiesEnabledForCallback callback) override; + + // Internal: + bool allowCookies(const GURL &url, const GURL &site_for_cookies) const; + +private: + ProxyingRestrictedCookieManagerQt(base::WeakPtr<ProfileIODataQt> profileIoData, + network::mojom::RestrictedCookieManagerPtr underlyingRestrictedCookieManager, + bool is_service_worker, + int32_t process_id, + int32_t frame_id); + + static void CreateAndBindOnIoThread(ProfileIODataQt *profileIoData, + network::mojom::RestrictedCookieManagerPtrInfo underlying_rcm, + bool is_service_worker, + int process_id, + int frame_id, + network::mojom::RestrictedCookieManagerRequest request); + + base::WeakPtr<ProfileIODataQt> m_profileIoData; + + network::mojom::RestrictedCookieManagerPtr underlying_restricted_cookie_manager_; + bool is_service_worker_; + int process_id_; + int frame_id_; + + base::WeakPtrFactory<ProxyingRestrictedCookieManagerQt> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(ProxyingRestrictedCookieManagerQt); +}; + +} // namespace QtWebEngineCore + +#endif // PROXYING_RESTRICTED_COOKIE_MANAGER_QT_H diff --git a/src/core/net/proxying_url_loader_factory_qt.cpp b/src/core/net/proxying_url_loader_factory_qt.cpp new file mode 100644 index 000000000..79dd76ea1 --- /dev/null +++ b/src/core/net/proxying_url_loader_factory_qt.cpp @@ -0,0 +1,556 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "proxying_url_loader_factory_qt.h" + +#include <utility> + +#include "base/bind.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/stringprintf.h" +#include "base/task/post_task.h" +#include "components/safe_browsing/common/safebrowsing_constants.h" +#include "content/browser/web_contents/web_contents_impl.h" +#include "content/public/browser/browser_task_traits.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/global_request_id.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/resource_request_info.h" +#include "content/public/browser/web_contents.h" +#include "content/public/common/content_constants.h" +#include "content/public/common/url_utils.h" +#include "net/base/load_flags.h" +#include "net/http/http_status_code.h" +#include "net/http/http_util.h" + +#include "api/qwebengineurlrequestinfo_p.h" +#include "net/url_request_custom_job_proxy.h" +#include "net/url_request_notification.h" +#include "profile_io_data_qt.h" +#include "type_conversion.h" +#include "web_contents_adapter_client.h" +#include "web_contents_view_qt.h" + +// originally based on aw_proxying_url_loader_factory.cc: +// Copyright 2018 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. + +namespace QtWebEngineCore { + +extern WebContentsAdapterClient::NavigationType pageTransitionToNavigationType(ui::PageTransition transition); + +static QWebEngineUrlRequestInfo::ResourceType toQt(content::ResourceType resourceType) +{ + if (resourceType >= content::ResourceType::kMainFrame && resourceType <= content::ResourceType::kMaxValue) + return static_cast<QWebEngineUrlRequestInfo::ResourceType>(resourceType); + return QWebEngineUrlRequestInfo::ResourceTypeUnknown; +} + +static QWebEngineUrlRequestInfo::NavigationType toQt(WebContentsAdapterClient::NavigationType navigationType) +{ + return static_cast<QWebEngineUrlRequestInfo::NavigationType>(navigationType); +} + +// Handles intercepted, in-progress requests/responses, so that they can be +// controlled and modified accordingly. +class InterceptedRequest : public network::mojom::URLLoader + , public network::mojom::URLLoaderClient +{ +public: + InterceptedRequest(int process_id, uint64_t request_id, int32_t routing_id, uint32_t options, + const network::ResourceRequest &request, + const net::MutableNetworkTrafficAnnotationTag &traffic_annotation, + ProfileIODataQt *profileData, + network::mojom::URLLoaderRequest loader_request, network::mojom::URLLoaderClientPtr client, + network::mojom::URLLoaderFactoryPtr target_factory); + ~InterceptedRequest() override; + + void Restart(); + void InterceptOnUIThread(); + + // network::mojom::URLLoaderClient + void OnReceiveResponse(const network::ResourceResponseHead &head) override; + void OnReceiveRedirect(const net::RedirectInfo &redirect_info, const network::ResourceResponseHead &head) override; + void OnUploadProgress(int64_t current_position, int64_t total_size, OnUploadProgressCallback callback) override; + void OnReceiveCachedMetadata(mojo_base::BigBuffer data) override; + void OnTransferSizeUpdated(int32_t transfer_size_diff) override; + void OnStartLoadingResponseBody(mojo::ScopedDataPipeConsumerHandle body) override; + void OnComplete(const network::URLLoaderCompletionStatus &status) override; + + // network::mojom::URLLoader + void FollowRedirect(const std::vector<std::string> &removed_headers, + const net::HttpRequestHeaders &modified_headers, const base::Optional<GURL> &new_url) override; + void ProceedWithResponse() override; + void SetPriority(net::RequestPriority priority, int32_t intra_priority_value) override; + void PauseReadingBodyFromNet() override; + void ResumeReadingBodyFromNet() override; + + void ContinueAfterIntercept(); + +private: + // This is called when the original URLLoaderClient has a connection error. + void OnURLLoaderClientError(); + + // This is called when the original URLLoader has a connection error. + void OnURLLoaderError(uint32_t custom_reason, const std::string &description); + + // Call OnComplete on |target_client_|. If |wait_for_loader_error| is true + // then this object will wait for |proxied_loader_binding_| to have a + // connection error before destructing. + void CallOnComplete(const network::URLLoaderCompletionStatus &status, bool wait_for_loader_error); + + void SendErrorAndCompleteImmediately(int error_code); + + const int process_id_; + const uint64_t request_id_; + const int32_t routing_id_; + const uint32_t options_; + bool input_stream_previously_failed_ = false; + bool request_was_redirected_ = false; + + // If the |target_loader_| called OnComplete with an error this stores it. + // That way the destructor can send it to OnReceivedError if safe browsing + // error didn't occur. + int error_status_ = net::OK; + GURL m_originalUrl; + + network::ResourceRequest request_; + network::ResourceResponseHead current_response_; + + const net::MutableNetworkTrafficAnnotationTag traffic_annotation_; + + ProfileIODataQt *m_profileData; + mojo::Binding<network::mojom::URLLoader> proxied_loader_binding_; + network::mojom::URLLoaderClientPtr target_client_; + + mojo::Binding<network::mojom::URLLoaderClient> proxied_client_binding_; + network::mojom::URLLoaderPtr target_loader_; + network::mojom::URLLoaderFactoryPtr target_factory_; + + base::WeakPtrFactory<InterceptedRequest> m_weakFactory; + base::WeakPtr<InterceptedRequest> m_weakPtr; + DISALLOW_COPY_AND_ASSIGN(InterceptedRequest); +}; + +InterceptedRequest::InterceptedRequest(int process_id, uint64_t request_id, int32_t routing_id, uint32_t options, + const network::ResourceRequest &request, + const net::MutableNetworkTrafficAnnotationTag &traffic_annotation, + ProfileIODataQt *profileData, + network::mojom::URLLoaderRequest loader_request, + network::mojom::URLLoaderClientPtr client, + network::mojom::URLLoaderFactoryPtr target_factory) + : process_id_(process_id) + , request_id_(request_id) + , routing_id_(routing_id) + , options_(options) + , request_(request) + , traffic_annotation_(traffic_annotation) + , m_profileData(profileData) + , proxied_loader_binding_(this, std::move(loader_request)) + , target_client_(std::move(client)) + , proxied_client_binding_(this) + , target_factory_(std::move(target_factory)) + , m_weakFactory(this) + , m_weakPtr(m_weakFactory.GetWeakPtr()) +{ + // If there is a client error, clean up the request. + target_client_.set_connection_error_handler( + base::BindOnce(&InterceptedRequest::OnURLLoaderClientError, m_weakFactory.GetWeakPtr())); + proxied_loader_binding_.set_connection_error_with_reason_handler( + base::BindOnce(&InterceptedRequest::OnURLLoaderError, m_weakFactory.GetWeakPtr())); +} + +InterceptedRequest::~InterceptedRequest() +{ + m_weakFactory.InvalidateWeakPtrs(); +} + +void InterceptedRequest::Restart() +{ + // FIXME: Support deprecated interceptors here + + // FIXME: unretained post? + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::UI}, + base::BindOnce(&InterceptedRequest::InterceptOnUIThread, base::Unretained(this))); +} + +void InterceptedRequest::InterceptOnUIThread() +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + content::ResourceType resourceType = content::ResourceType(request_.resource_type); + WebContentsAdapterClient::NavigationType navigationType = + pageTransitionToNavigationType(ui::PageTransition(request_.transition_type)); + + m_originalUrl = request_.url; + const QUrl qUrl = toQt(request_.url); + + const QUrl initiator = request_.request_initiator.has_value() ? toQt(request_.request_initiator->GetURL()) : QUrl(); + + // FIXME: request_.first_party_url is currently always empty. + QWebEngineUrlRequestInfoPrivate *infoPrivate = new QWebEngineUrlRequestInfoPrivate(toQt(resourceType), + toQt(navigationType), + qUrl, + toQt(request_.site_for_cookies), + initiator, + QByteArray::fromStdString(request_.method)); + QWebEngineUrlRequestInfo requestInfo(infoPrivate); + + content::WebContents *webContents = nullptr; + if (process_id_) { + content::RenderFrameHost *frameHost = content::RenderFrameHost::FromID(process_id_, request_.render_frame_id); + webContents = content::WebContents::FromRenderFrameHost(frameHost); + } else + webContents = content::WebContents::FromFrameTreeNodeId(request_.render_frame_id); + + if (webContents) { + int result = net::OK; + if (m_profileData) { + QWebEngineUrlRequestInterceptor *interceptor = m_profileData->requestInterceptor(); + if (interceptor && !interceptor->property("deprecated").toBool()) + interceptor->interceptRequest(requestInfo); + } + + WebContentsAdapterClient *client = + WebContentsViewQt::from(static_cast<content::WebContentsImpl*>(webContents)->GetView())->client(); + + if (!requestInfo.changed()) { + client->interceptRequest(requestInfo); + } + + if (requestInfo.changed()) { + result = requestInfo.d_ptr->shouldBlockRequest ? net::ERR_BLOCKED_BY_CLIENT : net::OK; + // We handle the rest of the changes later when we are back in I/O thread + } + + if (result != net::OK) { + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&InterceptedRequest::SendErrorAndCompleteImmediately, m_weakPtr, result)); + return; + } + if (requestInfo.changed()) { + if (requestInfo.requestUrl() != qUrl) { + net::URLRequest::FirstPartyURLPolicy first_party_url_policy = + request_.update_first_party_url_on_redirect ? net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT + : net::URLRequest::NEVER_CHANGE_FIRST_PARTY_URL; + net::RedirectInfo redirectInfo = net::RedirectInfo::ComputeRedirectInfo(request_.method, request_.url, + request_.site_for_cookies, request_.top_frame_origin, + first_party_url_policy, request_.referrer_policy, + request_.referrer.spec(), net::HTTP_TEMPORARY_REDIRECT, + toGurl(requestInfo.requestUrl()), base::nullopt, + false /*insecure_scheme_was_upgraded*/); + + // FIXME: Should probably create a new header. + current_response_.encoded_data_length = 0; + // FIXME: unretained post. + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&network::mojom::URLLoaderClientProxy::OnReceiveRedirect, base::Unretained(&(*target_client_)), redirectInfo, current_response_)); + request_.method = redirectInfo.new_method; + request_.url = redirectInfo.new_url; + request_.site_for_cookies = redirectInfo.new_site_for_cookies; + request_.referrer = GURL(redirectInfo.new_referrer); + request_.referrer_policy = redirectInfo.new_referrer_policy; + if (request_.method == net::HttpRequestHeaders::kGetMethod) + request_.request_body = nullptr; + return; + } + + if (!requestInfo.d_ptr->extraHeaders.isEmpty()) { + auto end = requestInfo.d_ptr->extraHeaders.constEnd(); + for (auto header = requestInfo.d_ptr->extraHeaders.constBegin(); header != end; ++header) { + std::string h = header.key().toStdString(); + if (base::LowerCaseEqualsASCII(h, "referer")) { + request_.referrer = GURL(header.value().toStdString()); + } else { + request_.headers.SetHeader(h, header.value().toStdString()); + } + } + } + } + } + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::IO}, + base::BindOnce(&InterceptedRequest::ContinueAfterIntercept, m_weakPtr)); +} + +void InterceptedRequest::ContinueAfterIntercept() +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + if (!target_loader_ && target_factory_) { + network::mojom::URLLoaderClientPtr proxied_client; + proxied_client_binding_.Bind(mojo::MakeRequest(&proxied_client)); + target_factory_->CreateLoaderAndStart(mojo::MakeRequest(&target_loader_), routing_id_, request_id_, options_, + request_, std::move(proxied_client), traffic_annotation_); + } +} + +// URLLoaderClient methods. + +void InterceptedRequest::OnReceiveResponse(const network::ResourceResponseHead &head) +{ + current_response_ = head; + + target_client_->OnReceiveResponse(head); +} + +void InterceptedRequest::OnReceiveRedirect(const net::RedirectInfo &redirect_info, const network::ResourceResponseHead &head) +{ + // TODO(timvolodine): handle redirect override. + request_was_redirected_ = true; + current_response_ = head; + target_client_->OnReceiveRedirect(redirect_info, head); + request_.url = redirect_info.new_url; + request_.method = redirect_info.new_method; + request_.site_for_cookies = redirect_info.new_site_for_cookies; + request_.referrer = GURL(redirect_info.new_referrer); + request_.referrer_policy = redirect_info.new_referrer_policy; +} + +void InterceptedRequest::OnUploadProgress(int64_t current_position, int64_t total_size, OnUploadProgressCallback callback) +{ + target_client_->OnUploadProgress(current_position, total_size, std::move(callback)); +} + +void InterceptedRequest::OnReceiveCachedMetadata(mojo_base::BigBuffer data) +{ + target_client_->OnReceiveCachedMetadata(std::move(data)); +} + +void InterceptedRequest::OnTransferSizeUpdated(int32_t transfer_size_diff) +{ + target_client_->OnTransferSizeUpdated(transfer_size_diff); +} + +void InterceptedRequest::OnStartLoadingResponseBody(mojo::ScopedDataPipeConsumerHandle body) +{ + target_client_->OnStartLoadingResponseBody(std::move(body)); +} + +void InterceptedRequest::OnComplete(const network::URLLoaderCompletionStatus &status) +{ + // Only wait for the original loader to possibly have a custom error if the + // target loader succeeded. If the target loader failed, then it was a race as + // to whether that error or the safe browsing error would be reported. + CallOnComplete(status, status.error_code == net::OK); +} + +// URLLoader methods. + +void InterceptedRequest::FollowRedirect(const std::vector<std::string> &removed_headers, + const net::HttpRequestHeaders &modified_headers, + const base::Optional<GURL> &new_url) +{ + if (target_loader_) + target_loader_->FollowRedirect(removed_headers, modified_headers, new_url); + + // If |OnURLLoaderClientError| was called then we're just waiting for the + // connection error handler of |proxied_loader_binding_|. Don't restart the + // job since that'll create another URLLoader + if (!target_client_) + return; + + Restart(); +} + +void InterceptedRequest::ProceedWithResponse() +{ + if (target_loader_) + target_loader_->ProceedWithResponse(); +} + +void InterceptedRequest::SetPriority(net::RequestPriority priority, int32_t intra_priority_value) +{ + if (target_loader_) + target_loader_->SetPriority(priority, intra_priority_value); +} + +void InterceptedRequest::PauseReadingBodyFromNet() +{ + if (target_loader_) + target_loader_->PauseReadingBodyFromNet(); +} + +void InterceptedRequest::ResumeReadingBodyFromNet() +{ + if (target_loader_) + target_loader_->ResumeReadingBodyFromNet(); +} + +void InterceptedRequest::OnURLLoaderClientError() +{ + // We set |wait_for_loader_error| to true because if the loader did have a + // custom_reason error then the client would be reset as well and it would be + // a race as to which connection error we saw first. + CallOnComplete(network::URLLoaderCompletionStatus(net::ERR_ABORTED), true /* wait_for_loader_error */); +} + +void InterceptedRequest::OnURLLoaderError(uint32_t custom_reason, const std::string &description) +{ + // If CallOnComplete was already called, then this object is ready to be deleted. + if (!target_client_) + delete this; +} + +void InterceptedRequest::CallOnComplete(const network::URLLoaderCompletionStatus &status, bool wait_for_loader_error) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + // Save an error status so that we call onReceiveError at destruction if there + // was no safe browsing error. + if (status.error_code != net::OK) + error_status_ = status.error_code; + + if (target_client_) + target_client_->OnComplete(status); + + if (proxied_loader_binding_ && wait_for_loader_error) { + // Don't delete |this| yet, in case the |proxied_loader_binding_|'s + // error_handler is called with a reason to indicate an error which we want + // to send to the client bridge. Also reset |target_client_| so we don't + // get its error_handler called and then delete |this|. + target_client_.reset(); + + // Since the original client is gone no need to continue loading the + // request. + proxied_client_binding_.Close(); + target_loader_.reset(); + + // In case there are pending checks as to whether this request should be + // intercepted, we don't want that causing |target_client_| to be used + // later. + m_weakFactory.InvalidateWeakPtrs(); + } else { + delete this; + } +} + +void InterceptedRequest::SendErrorAndCompleteImmediately(int error_code) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + auto status = network::URLLoaderCompletionStatus(error_code); + target_client_->OnComplete(status); + delete this; +} + +ProxyingURLLoaderFactoryQt::ProxyingURLLoaderFactoryQt(int process_id, + content::ResourceContext *resourceContext, + network::mojom::URLLoaderFactoryRequest loader_request, + network::mojom::URLLoaderFactoryPtrInfo target_factory_info) + : m_processId(process_id), m_resourceContext(resourceContext), m_weakFactory(this) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + if (target_factory_info) { + m_targetFactory.Bind(std::move(target_factory_info)); + m_targetFactory.set_connection_error_handler( + base::BindOnce(&ProxyingURLLoaderFactoryQt::OnTargetFactoryError, m_weakFactory.GetWeakPtr())); + } + m_proxyBindings.AddBinding(this, std::move(loader_request)); + m_proxyBindings.set_connection_error_handler( + base::BindRepeating(&ProxyingURLLoaderFactoryQt::OnProxyBindingError, m_weakFactory.GetWeakPtr())); +} + +ProxyingURLLoaderFactoryQt::~ProxyingURLLoaderFactoryQt() +{ + m_weakFactory.InvalidateWeakPtrs(); +} + +// static +void ProxyingURLLoaderFactoryQt::CreateProxy(int process_id, + content::ResourceContext *resourceContext, + network::mojom::URLLoaderFactoryRequest loader_request, + network::mojom::URLLoaderFactoryPtrInfo target_factory_info) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + // Will manage its own lifetime + new ProxyingURLLoaderFactoryQt(process_id, resourceContext, std::move(loader_request), std::move(target_factory_info)); +} + +void ProxyingURLLoaderFactoryQt::CreateLoaderAndStart(network::mojom::URLLoaderRequest loader, int32_t routing_id, + int32_t request_id, uint32_t options, + const network::ResourceRequest &request, + network::mojom::URLLoaderClientPtr client, + const net::MutableNetworkTrafficAnnotationTag &traffic_annotation) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + ProfileIODataQt *profileIOData = ProfileIODataQt::FromResourceContext(m_resourceContext); + + QWebEngineUrlRequestInterceptor *profileInterceptor = profileIOData ? profileIOData->requestInterceptor() : nullptr; + if (!profileIOData || !(profileInterceptor || profileIOData->hasPageInterceptors())) { + m_targetFactory->CreateLoaderAndStart( + std::move(loader), routing_id, request_id, options, request, + std::move(client), traffic_annotation); + return; + } + + network::mojom::URLLoaderFactoryPtr target_factory_clone; + if (m_targetFactory) + m_targetFactory->Clone(mojo::MakeRequest(&target_factory_clone)); + + + // Will manage its own lifetime + InterceptedRequest *req = new InterceptedRequest(m_processId, request_id, routing_id, options, request, + traffic_annotation, profileIOData, + std::move(loader), std::move(client), + std::move(target_factory_clone)); + req->Restart(); +} + +void ProxyingURLLoaderFactoryQt::OnTargetFactoryError() +{ + delete this; +} + +void ProxyingURLLoaderFactoryQt::OnProxyBindingError() +{ + if (m_proxyBindings.empty()) + delete this; +} + +void ProxyingURLLoaderFactoryQt::Clone(network::mojom::URLLoaderFactoryRequest loader_request) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + m_proxyBindings.AddBinding(this, std::move(loader_request)); +} + +} // namespace QtWebEngineCore diff --git a/src/core/net/proxying_url_loader_factory_qt.h b/src/core/net/proxying_url_loader_factory_qt.h new file mode 100644 index 000000000..a2f175885 --- /dev/null +++ b/src/core/net/proxying_url_loader_factory_qt.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PROXYING_URL_LOADER_FACTORY_QT_H_ +#define PROXYING_URL_LOADER_FACTORY_QT_H_ + +#include "base/callback.h" +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/ref_counted_delete_on_sequence.h" +#include "base/memory/weak_ptr.h" +#include "base/optional.h" +#include "mojo/public/cpp/bindings/binding.h" +#include "mojo/public/cpp/bindings/binding_set.h" +#include "net/traffic_annotation/network_traffic_annotation.h" +#include "services/network/public/cpp/resource_request.h" +#include "services/network/public/cpp/resource_response.h" +#include "services/network/public/mojom/url_loader.mojom.h" +#include "services/network/public/mojom/url_loader_factory.mojom.h" +#include "url/gurl.h" + +// based on aw_proxying_url_loader_factory.h: +// Copyright 2018 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. + +namespace content { +class ResourceContext; +} + +namespace QtWebEngineCore { + +class ProxyingURLLoaderFactoryQt : public network::mojom::URLLoaderFactory +{ +public: + ProxyingURLLoaderFactoryQt(int process_id, content::ResourceContext *resourceContext, + network::mojom::URLLoaderFactoryRequest loader_request, + network::mojom::URLLoaderFactoryPtrInfo target_factory_info); + + ~ProxyingURLLoaderFactoryQt() override; + + // static + static void CreateProxy(int process_id, content::ResourceContext *resourceContext, + network::mojom::URLLoaderFactoryRequest loader, + network::mojom::URLLoaderFactoryPtrInfo target_factory_info); + + void CreateLoaderAndStart(network::mojom::URLLoaderRequest loader, int32_t routing_id, int32_t request_id, + uint32_t options, const network::ResourceRequest &request, + network::mojom::URLLoaderClientPtr client, + const net::MutableNetworkTrafficAnnotationTag &traffic_annotation) override; + + void Clone(network::mojom::URLLoaderFactoryRequest loader_request) override; + +private: + void OnTargetFactoryError(); + void OnProxyBindingError(); + + const int m_processId; + mojo::BindingSet<network::mojom::URLLoaderFactory> m_proxyBindings; + network::mojom::URLLoaderFactoryPtr m_targetFactory; + + content::ResourceContext *m_resourceContext; + + base::WeakPtrFactory<ProxyingURLLoaderFactoryQt> m_weakFactory; + + DISALLOW_COPY_AND_ASSIGN(ProxyingURLLoaderFactoryQt); +}; + +} // namespace QtWebEngineCore + +#endif // PROXYING_URL_LOADER_FACTORY_QT_H_ diff --git a/src/core/net/restricted_cookie_manager_qt.h b/src/core/net/restricted_cookie_manager_qt.h index 9154f671d..c135a1795 100644 --- a/src/core/net/restricted_cookie_manager_qt.h +++ b/src/core/net/restricted_cookie_manager_qt.h @@ -62,7 +62,7 @@ public: int32_t frame_id); ~RestrictedCookieManagerQt() override; - // network::mojom::RestrictedCookieManager interface: + // network::RestrictedCookieManager: void GetAllForUrl(const GURL &url, const GURL &site_for_cookies, network::mojom::CookieManagerGetOptionsPtr options, diff --git a/src/core/net/system_network_context_manager.cpp b/src/core/net/system_network_context_manager.cpp new file mode 100644 index 000000000..d82187205 --- /dev/null +++ b/src/core/net/system_network_context_manager.cpp @@ -0,0 +1,351 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// based on chrome/browser/net/system_network_context_manager.cc: +// Copyright 2017 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 "net/system_network_context_manager.h" + +#include <set> +#include <unordered_map> +#include <utility> + +#include "base/bind.h" +#include "base/command_line.h" +#include "base/feature_list.h" +#include "base/logging.h" +#include "base/sequence_checker.h" +#include "base/strings/string_split.h" +#include "base/task/post_task.h" +#include "base/values.h" +#include "build/build_config.h" +#include "chrome/browser/net/chrome_mojo_proxy_resolver_factory.h" +#include "chrome/common/chrome_switches.h" +#include "components/certificate_transparency/ct_known_logs.h" +#include "components/network_session_configurator/common/network_features.h" +#include "content/public/browser/browser_task_traits.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/cors_exempt_headers.h" +#include "content/public/browser/network_service_instance.h" +#include "content/public/common/content_features.h" +#include "content/public/common/content_switches.h" +#include "content/public/common/service_names.mojom.h" +#include "content/public/common/user_agent.h" +#include "mojo/public/cpp/bindings/associated_interface_ptr.h" +#include "net/dns/public/util.h" +#include "net/net_buildflags.h" +#include "net/third_party/uri_template/uri_template.h" +#include "services/network/network_service.h" +#include "services/network/public/cpp/cross_thread_shared_url_loader_factory_info.h" +#include "services/network/public/cpp/features.h" +#include "services/network/public/cpp/shared_url_loader_factory.h" +#include "services/network/public/mojom/host_resolver.mojom.h" +#include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h" +#include "url/gurl.h" + +namespace { + +// The global instance of the SystemNetworkContextmanager. +SystemNetworkContextManager *g_system_network_context_manager = nullptr; + +network::mojom::HttpAuthStaticParamsPtr CreateHttpAuthStaticParams() +{ + network::mojom::HttpAuthStaticParamsPtr auth_static_params = network::mojom::HttpAuthStaticParams::New(); + + auth_static_params->supported_schemes = { "basic", "digest", "ntlm", "negotiate" }; + + return auth_static_params; +} + +network::mojom::HttpAuthDynamicParamsPtr CreateHttpAuthDynamicParams() +{ + network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params = network::mojom::HttpAuthDynamicParams::New(); + + auto *command_line = base::CommandLine::ForCurrentProcess(); + auth_dynamic_params->server_whitelist = command_line->GetSwitchValueASCII(switches::kAuthServerWhitelist); +// auth_dynamic_params->delegate_whitelist = command_line->GetSwitchValueASCII(switches::kAuthNegotiateDelegateWhitelist); +// auth_dynamic_params->enable_negotiate_port = command_line->HasSwitch(switches::kEnableAuthNegotiatePort); + + return auth_dynamic_params; +} + +} // namespace + +// SharedURLLoaderFactory backed by a SystemNetworkContextManager and its +// network context. Transparently handles crashes. +class SystemNetworkContextManager::URLLoaderFactoryForSystem : public network::SharedURLLoaderFactory +{ +public: + explicit URLLoaderFactoryForSystem(SystemNetworkContextManager *manager) : manager_(manager) + { + DETACH_FROM_SEQUENCE(sequence_checker_); + } + + // mojom::URLLoaderFactory implementation: + + void CreateLoaderAndStart(network::mojom::URLLoaderRequest request, int32_t routing_id, int32_t request_id, + uint32_t options, const network::ResourceRequest &url_request, + network::mojom::URLLoaderClientPtr client, + const net::MutableNetworkTrafficAnnotationTag &traffic_annotation) override + { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + if (!manager_) + return; + manager_->GetURLLoaderFactory()->CreateLoaderAndStart(std::move(request), routing_id, request_id, options, + url_request, std::move(client), traffic_annotation); + } + + void Clone(network::mojom::URLLoaderFactoryRequest request) override + { + if (!manager_) + return; + manager_->GetURLLoaderFactory()->Clone(std::move(request)); + } + + // SharedURLLoaderFactory implementation: + std::unique_ptr<network::SharedURLLoaderFactoryInfo> Clone() override + { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + return std::make_unique<network::CrossThreadSharedURLLoaderFactoryInfo>(this); + } + + void Shutdown() { manager_ = nullptr; } + +private: + friend class base::RefCounted<URLLoaderFactoryForSystem>; + ~URLLoaderFactoryForSystem() override {} + + SEQUENCE_CHECKER(sequence_checker_); + SystemNetworkContextManager *manager_; + + DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryForSystem); +}; + +network::mojom::NetworkContext *SystemNetworkContextManager::GetContext() +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) { + // SetUp should already have been called. + DCHECK(io_thread_network_context_); + return io_thread_network_context_.get(); + } + + if (!network_service_network_context_ || network_service_network_context_.encountered_error()) { + // This should call into OnNetworkServiceCreated(), which will re-create + // the network service, if needed. There's a chance that it won't be + // invoked, if the NetworkContext has encountered an error but the + // NetworkService has not yet noticed its pipe was closed. In that case, + // trying to create a new NetworkContext would fail, anyways, and hopefully + // a new NetworkContext will be created on the next GetContext() call. + content::GetNetworkService(); + DCHECK(network_service_network_context_); + } + return network_service_network_context_.get(); +} + +network::mojom::URLLoaderFactory *SystemNetworkContextManager::GetURLLoaderFactory() +{ + // Create the URLLoaderFactory as needed. + if (url_loader_factory_ && !url_loader_factory_.encountered_error()) { + return url_loader_factory_.get(); + } + + network::mojom::URLLoaderFactoryParamsPtr params = network::mojom::URLLoaderFactoryParams::New(); + params->process_id = network::mojom::kBrowserProcessId; + params->is_corb_enabled = false; + GetContext()->CreateURLLoaderFactory(mojo::MakeRequest(&url_loader_factory_), std::move(params)); + return url_loader_factory_.get(); +} + +scoped_refptr<network::SharedURLLoaderFactory> SystemNetworkContextManager::GetSharedURLLoaderFactory() +{ + return shared_url_loader_factory_; +} + +void SystemNetworkContextManager::SetUp( + network::mojom::NetworkContextRequest *network_context_request, + network::mojom::NetworkContextParamsPtr *network_context_params, bool *stub_resolver_enabled, + base::Optional<std::vector<network::mojom::DnsOverHttpsServerPtr>> *dns_over_https_servers, + network::mojom::HttpAuthStaticParamsPtr *http_auth_static_params, + network::mojom::HttpAuthDynamicParamsPtr *http_auth_dynamic_params, bool *is_quic_allowed) +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) { + *network_context_request = mojo::MakeRequest(&io_thread_network_context_); + *network_context_params = CreateNetworkContextParams(); + } + *is_quic_allowed = false; + *http_auth_static_params = CreateHttpAuthStaticParams(); + *http_auth_dynamic_params = CreateHttpAuthDynamicParams(); + // GetStubResolverConfig(local_state_, stub_resolver_enabled, dns_over_https_servers); +} + +// static +SystemNetworkContextManager *SystemNetworkContextManager::CreateInstance() +{ + DCHECK(!g_system_network_context_manager); + g_system_network_context_manager = new SystemNetworkContextManager(); + return g_system_network_context_manager; +} + +// static +SystemNetworkContextManager *SystemNetworkContextManager::GetInstance() +{ + return g_system_network_context_manager; +} + +// static +void SystemNetworkContextManager::DeleteInstance() +{ + DCHECK(g_system_network_context_manager); + delete g_system_network_context_manager; +} + +SystemNetworkContextManager::SystemNetworkContextManager() +{ + shared_url_loader_factory_ = new URLLoaderFactoryForSystem(this); +} + +SystemNetworkContextManager::~SystemNetworkContextManager() +{ + shared_url_loader_factory_->Shutdown(); +} + +void SystemNetworkContextManager::OnNetworkServiceCreated(network::mojom::NetworkService *network_service) +{ + if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) + return; + // Disable QUIC globally + network_service->DisableQuic(); + + network_service->SetUpHttpAuth(CreateHttpAuthStaticParams()); + network_service->ConfigureHttpAuthPrefs(CreateHttpAuthDynamicParams()); + + // The system NetworkContext must be created first, since it sets + // |primary_network_context| to true. + network_service->CreateNetworkContext(MakeRequest(&network_service_network_context_), CreateNetworkContextParams()); + + // Configure the stub resolver. This must be done after the system + // NetworkContext is created, but before anything has the chance to use it. + // bool stub_resolver_enabled; + // base::Optional<std::vector<network::mojom::DnsOverHttpsServerPtr>> dns_over_https_servers; + // GetStubResolverConfig(local_state_, &stub_resolver_enabled, &dns_over_https_servers); + // content::GetNetworkService()->ConfigureStubHostResolver(stub_resolver_enabled, std::move(dns_over_https_servers)); +} + +void SystemNetworkContextManager::AddSSLConfigToNetworkContextParams(network::mojom::NetworkContextParams *network_context_params) +{ + network_context_params->initial_ssl_config = network::mojom::SSLConfig::New(); + network_context_params->initial_ssl_config->rev_checking_enabled = true; + network_context_params->initial_ssl_config->symantec_enforcement_disabled = true; +} + +network::mojom::NetworkContextParamsPtr SystemNetworkContextManager::CreateDefaultNetworkContextParams() +{ + network::mojom::NetworkContextParamsPtr network_context_params = network::mojom::NetworkContextParams::New(); + content::UpdateCorsExemptHeader(network_context_params.get()); + + network_context_params->enable_brotli = true; + + // network_context_params->user_agent = GetUserAgent(); + + // Disable referrers by default. Any consumer that enables referrers should + // respect prefs::kEnableReferrers from the appropriate pref store. + network_context_params->enable_referrers = false; + + // const base::CommandLine& command_line = + // *base::CommandLine::ForCurrentProcess(); + + // // TODO(eroman): Figure out why this doesn't work in single-process mode, + // // or if it does work, now. + // // Should be possible now that a private isolate is used. + // // http://crbug.com/474654 + // if (!command_line.HasSwitch(switches::kWinHttpProxyResolver)) { + // if (command_line.HasSwitch(switches::kSingleProcess)) { + // LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode."; + // } else { + network_context_params->proxy_resolver_factory = ChromeMojoProxyResolverFactory::CreateWithSelfOwnedReceiver(); + // } + // } + + // network_context_params->pac_quick_check_enabled = local_state_->GetBoolean(prefs::kQuickCheckEnabled); + + // Use the SystemNetworkContextManager to populate and update SSL + // configuration. The SystemNetworkContextManager is owned by the + // BrowserProcess itself, so will only be destroyed on shutdown, at which + // point, all NetworkContexts will be destroyed as well. + AddSSLConfigToNetworkContextParams(network_context_params.get()); + + // CT is only enabled on Desktop platforms for now. + network_context_params->enforce_chrome_ct_policy = true; + for (const auto &ct_log : certificate_transparency::GetKnownLogs()) { + // TODO(rsleevi): https://crbug.com/702062 - Remove this duplication. + network::mojom::CTLogInfoPtr log_info = network::mojom::CTLogInfo::New(); + log_info->public_key = std::string(ct_log.log_key, ct_log.log_key_length); + log_info->name = ct_log.log_name; + network_context_params->ct_logs.push_back(std::move(log_info)); + } + + network_context_params->http_09_on_non_default_ports_enabled = false; + + return network_context_params; +} + +network::mojom::NetworkContextParamsPtr SystemNetworkContextManager::CreateNetworkContextParams() +{ + // TODO(mmenke): Set up parameters here (in memory cookie store, etc). + network::mojom::NetworkContextParamsPtr network_context_params = CreateDefaultNetworkContextParams(); + + network_context_params->context_name = std::string("system"); + + network_context_params->enable_referrers = false; + + network_context_params->http_cache_enabled = false; + + // These are needed for PAC scripts that use FTP URLs. +#if !BUILDFLAG(DISABLE_FTP_SUPPORT) + network_context_params->enable_ftp_url_support = true; +#endif + + network_context_params->primary_network_context = true; + + // proxy_config_monitor_.AddToNetworkContextParams(network_context_params.get()); + + return network_context_params; +} diff --git a/src/core/net/system_network_context_manager.h b/src/core/net/system_network_context_manager.h new file mode 100644 index 000000000..f293b8e12 --- /dev/null +++ b/src/core/net/system_network_context_manager.h @@ -0,0 +1,183 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// based on chrome/browser/net/system_network_context_manager.h: +// Copyright 2017 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 SYSTEM_NETWORK_CONTEXT_MANAGER_H_ +#define SYSTEM_NETWORK_CONTEXT_MANAGER_H_ + +#include <memory> +#include <string> +#include <vector> + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/optional.h" +#include "services/network/public/mojom/host_resolver.mojom-forward.h" +#include "services/network/public/mojom/network_context.mojom.h" +#include "services/network/public/mojom/network_service.mojom-forward.h" +#include "services/network/public/mojom/ssl_config.mojom-forward.h" +#include "services/network/public/mojom/url_loader_factory.mojom-forward.h" + +namespace network { +namespace mojom { +class URLLoaderFactory; +} +class SharedURLLoaderFactory; +} // namespace network + +namespace net_log { +class NetExportFileWriter; +} + +// Responsible for creating and managing access to the system NetworkContext. +// Lives on the UI thread. The NetworkContext this owns is intended for requests +// not associated with a profile. It stores no data on disk, and has no HTTP +// cache, but it does have ephemeral cookie and channel ID stores. It also does +// not have access to HTTP proxy auth information the user has entered or that +// comes from extensions, and similarly, has no extension-provided per-profile +// proxy configuration information. +// +// This class is also responsible for configuring global NetworkService state. +// +// The "system" NetworkContext will either share a URLRequestContext with +// IOThread's SystemURLRequestContext and be part of IOThread's NetworkService +// (If the network service is disabled) or be an independent NetworkContext +// using the actual network service. +// +// This class is intended to eventually replace IOThread. Handling the two cases +// differently allows this to be used in production without breaking anything or +// requiring two separate paths, while IOThread consumers slowly transition over +// to being compatible with the network service. +class SystemNetworkContextManager +{ +public: + ~SystemNetworkContextManager(); + + // Creates the global instance of SystemNetworkContextManager. If an + // instance already exists, this will cause a DCHECK failure. + static SystemNetworkContextManager *CreateInstance(); + + // Gets the global SystemNetworkContextManager instance. + static SystemNetworkContextManager *GetInstance(); + + // Destroys the global SystemNetworkContextManager instance. + static void DeleteInstance(); + + // If the network service is disabled, |network_context_request| will be for + // the NetworkContext used by the SystemNetworkContextManager and + // |network_context_params| as needed to set up a system NetworkContext. + // Otherwise, this method can still be used to help set up the IOThread's + // in-process URLRequestContext. + // + // Must be called before the system NetworkContext is first used. + // + // |stub_resolver_enabled|, |dns_over_https_servers|, + // |http_auth_static_params|, |http_auth_dynamic_params|, and + // |is_quic_allowed| are used to pass initial NetworkService state to the + // caller, so the NetworkService can be configured appropriately. Using + // NetworkService's Mojo interface to set those options would lead to races + // with other UI->IO thread network-related tasks, since Mojo doesn't preserve + // execution order relative to PostTasks. + void SetUp(network::mojom::NetworkContextRequest *network_context_request, + network::mojom::NetworkContextParamsPtr *network_context_params, bool *stub_resolver_enabled, + base::Optional<std::vector<network::mojom::DnsOverHttpsServerPtr>> *dns_over_https_servers, + network::mojom::HttpAuthStaticParamsPtr *http_auth_static_params, + network::mojom::HttpAuthDynamicParamsPtr *http_auth_dynamic_params, bool *is_quic_allowed); + + // Returns the System NetworkContext. May only be called after SetUp(). Does + // any initialization of the NetworkService that may be needed when first + // called. + network::mojom::NetworkContext *GetContext(); + + // Returns a URLLoaderFactory owned by the SystemNetworkContextManager that is + // backed by the SystemNetworkContext. Allows sharing of the URLLoaderFactory. + // Prefer this to creating a new one. Call Clone() on the value returned by + // this method to get a URLLoaderFactory that can be used on other threads. + network::mojom::URLLoaderFactory *GetURLLoaderFactory(); + + // Returns a SharedURLLoaderFactory owned by the SystemNetworkContextManager + // that is backed by the SystemNetworkContext. + scoped_refptr<network::SharedURLLoaderFactory> GetSharedURLLoaderFactory(); + + // Called when content creates a NetworkService. Creates the + // SystemNetworkContext, if the network service is enabled. + void OnNetworkServiceCreated(network::mojom::NetworkService *network_service); + + // Populates |initial_ssl_config| and |ssl_config_client_request| members of + // |network_context_params|. As long as the SystemNetworkContextManager + // exists, any NetworkContext created with the params will continue to get + // SSL configuration updates. + void AddSSLConfigToNetworkContextParams(network::mojom::NetworkContextParams *network_context_params); + + // Returns default set of parameters for configuring the network service. + network::mojom::NetworkContextParamsPtr CreateDefaultNetworkContextParams(); + +private: + class URLLoaderFactoryForSystem; + + explicit SystemNetworkContextManager(); + + // Creates parameters for the NetworkContext. May only be called once, since + // it initializes some class members. + network::mojom::NetworkContextParamsPtr CreateNetworkContextParams(); + + // ProxyConfigMonitor proxy_config_monitor_; + + // NetworkContext using the network service, if the network service is + // enabled. nullptr, otherwise. + network::mojom::NetworkContextPtr network_service_network_context_; + + // This is a NetworkContext that wraps the IOThread's SystemURLRequestContext. + // Always initialized in SetUp, but it's only returned by Context() when the + // network service is disabled. + network::mojom::NetworkContextPtr io_thread_network_context_; + + // URLLoaderFactory backed by the NetworkContext returned by GetContext(), so + // consumers don't all need to create their own factory. + scoped_refptr<URLLoaderFactoryForSystem> shared_url_loader_factory_; + network::mojom::URLLoaderFactoryPtr url_loader_factory_; + + DISALLOW_COPY_AND_ASSIGN(SystemNetworkContextManager); +}; + +#endif // SYSTEM_NETWORK_CONTEXT_MANAGER_H_ diff --git a/src/core/profile_adapter.cpp b/src/core/profile_adapter.cpp index d421edf00..3f6d03b2e 100644 --- a/src/core/profile_adapter.cpp +++ b/src/core/profile_adapter.cpp @@ -193,8 +193,7 @@ void ProfileAdapter::setRequestInterceptor(QWebEngineUrlRequestInterceptor *inte Q_ASSERT(!m_profile->m_profileIOData->requestInterceptor()); }); - if (m_profile->m_urlRequestContextGetter.get()) - m_profile->m_profileIOData->updateRequestInterceptor(); + m_profile->m_profileIOData->updateRequestInterceptor(); } void ProfileAdapter::addClient(ProfileAdapterClient *adapterClient) @@ -210,16 +209,14 @@ void ProfileAdapter::removeClient(ProfileAdapterClient *adapterClient) void ProfileAdapter::addPageRequestInterceptor() { ++m_pageRequestInterceptors; - if (m_profile->m_urlRequestContextGetter.get()) - m_profile->m_profileIOData->updateRequestInterceptor(); + m_profile->m_profileIOData->updateRequestInterceptor(); } void ProfileAdapter::removePageRequestInterceptor() { Q_ASSERT(m_pageRequestInterceptors > 0); --m_pageRequestInterceptors; - if (m_profile->m_urlRequestContextGetter.get()) - m_profile->m_profileIOData->updateRequestInterceptor(); + m_profile->m_profileIOData->updateRequestInterceptor(); } diff --git a/src/core/profile_io_data_qt.cpp b/src/core/profile_io_data_qt.cpp index 68be09ad2..2d0bee8d5 100644 --- a/src/core/profile_io_data_qt.cpp +++ b/src/core/profile_io_data_qt.cpp @@ -40,12 +40,14 @@ #include "profile_io_data_qt.h" #include "base/task/post_task.h" +#include "chrome/common/chrome_constants.h" #include "components/certificate_transparency/ct_known_logs.h" #include "components/network_session_configurator/common/network_features.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/browsing_data_remover.h" #include "content/public/browser/cookie_store_factory.h" +#include "content/public/browser/shared_cors_origin_access_list.h" #include "content/public/common/content_features.h" #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" #include "chrome/browser/net/chrome_mojo_proxy_resolver_factory.h" @@ -80,6 +82,8 @@ #include "services/file/user_id_map.h" #include "services/network/proxy_service_mojo.h" #include "services/network/restricted_cookie_manager.h" +#include "services/network/public/cpp/features.h" +#include "services/network/public/cpp/cors/origin_access_list.h" #include "net/client_cert_override.h" #include "net/client_cert_store_data.h" @@ -88,6 +92,7 @@ #include "net/network_delegate_qt.h" #include "net/proxy_config_service_qt.h" #include "net/restricted_cookie_manager_qt.h" +#include "net/system_network_context_manager.h" #include "net/url_request_context_getter_qt.h" #include "profile_qt.h" #include "resource_context_qt.h" @@ -218,6 +223,8 @@ void ProfileIODataQt::shutdownOnUIThread() delete m_clientCertificateStoreData; m_clientCertificateStoreData = nullptr; #endif + if (m_cookieDelegate) + m_cookieDelegate->unsetMojoCookieManager(); bool posted = content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, this); if (!posted) { qWarning() << "Could not delete ProfileIODataQt on io thread !"; @@ -248,9 +255,16 @@ extensions::ExtensionSystemQt* ProfileIODataQt::GetExtensionSystem() base::WeakPtr<ProfileIODataQt> ProfileIODataQt::getWeakPtrOnUIThread() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + DCHECK(m_initialized); return m_weakPtr; } +base::WeakPtr<ProfileIODataQt> ProfileIODataQt::getWeakPtrOnIOThread() +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + return m_weakPtrFactory.GetWeakPtr(); +} + void ProfileIODataQt::initializeOnIOThread() { m_networkDelegate.reset(new NetworkDelegateQt(this)); @@ -407,6 +421,11 @@ void ProfileIODataQt::generateCookieStore() const std::lock_guard<QRecursiveMutex> lock(m_mutex); // FIXME: Add code to remove the old channel-id database. + // TODO(nharper): Remove the following when no longer needed - see + // crbug.com/903642. +// base::PostTaskWithTraits( +// FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, +// base::BindOnce(DeleteChannelIDFiles, path.Append(chrome::kChannelIDFilename))); std::unique_ptr<net::CookieStore> cookieStore; switch (m_persistentCookiesPolicy) { @@ -751,6 +770,7 @@ bool ProfileIODataQt::isInterceptorDeprecated() const QWebEngineUrlRequestInterceptor *ProfileIODataQt::acquireInterceptor() { + Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); m_mutex.lock(); return m_requestInterceptor; } @@ -769,6 +789,7 @@ bool ProfileIODataQt::hasPageInterceptors() void ProfileIODataQt::releaseInterceptor() { + Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); m_mutex.unlock(); } @@ -828,6 +849,56 @@ void ProfileIODataQt::CreateRestrictedCookieManager(network::mojom::RestrictedCo std::move(request)); } +network::mojom::NetworkContextParamsPtr ProfileIODataQt::CreateNetworkContextParams() +{ + network::mojom::NetworkContextParamsPtr network_context_params = + SystemNetworkContextManager::GetInstance()->CreateDefaultNetworkContextParams(); + + network_context_params->context_name = m_profile->profileAdapter()->storageName().toStdString(); + network_context_params->accept_language = m_httpAcceptLanguage.toStdString(); + + network_context_params->enable_referrers = true; + network_context_params->enable_encrypted_cookies = false; // ??? +// network_context_params->proxy_resolver_factory = std::move(m_proxyResolverFactoryInterface); + + network_context_params->http_cache_enabled = m_httpCacheType != ProfileAdapter::NoCache; + network_context_params->http_cache_max_size = m_httpCacheMaxSize; + if (m_httpCacheType == ProfileAdapter::DiskHttpCache) + network_context_params->http_cache_path = toFilePath(m_httpCachePath); + + if (m_persistentCookiesPolicy != ProfileAdapter::NoPersistentCookies) { + base::FilePath cookie_path = toFilePath(m_dataPath); + cookie_path = cookie_path.AppendASCII("Cookies"); + network_context_params->cookie_path = cookie_path; + + network_context_params->restore_old_session_cookies = false; + network_context_params->persist_session_cookies = m_persistentCookiesPolicy == ProfileAdapter::ForcePersistentCookies; + } + if (!m_dataPath.isEmpty()) { + network_context_params->http_server_properties_path = toFilePath(m_dataPath).AppendASCII("Network Persistent State"); + network_context_params->transport_security_persister_path = toFilePath(m_dataPath); + } + +#if !BUILDFLAG(DISABLE_FTP_SUPPORT) + network_context_params->enable_ftp_url_support = true; +#endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) + +// proxy_config_monitor_.AddToNetworkContextParams(network_context_params.get()); + +// network_context_params->enable_certificate_reporting = true; +// network_context_params->enable_expect_ct_reporting = true; + network_context_params->enforce_chrome_ct_policy = false; + network_context_params->primary_network_context = m_useForGlobalCertificateVerification; + + if (base::FeatureList::IsEnabled(network::features::kNetworkService)) { + // Should be initialized with existing per-profile CORS access lists. + network_context_params->cors_origin_access_list = + m_profile->GetSharedCorsOriginAccessList()->GetOriginAccessList().CreateCorsOriginAccessPatternsList(); + } + + return network_context_params; +} + // static ProfileIODataQt *ProfileIODataQt::FromBrowserContext(content::BrowserContext *browser_context) { diff --git a/src/core/profile_io_data_qt.h b/src/core/profile_io_data_qt.h index ec0a3dac9..451929afd 100644 --- a/src/core/profile_io_data_qt.h +++ b/src/core/profile_io_data_qt.h @@ -48,6 +48,7 @@ #include "extensions/buildflags/buildflags.h" #include "mojo/public/cpp/bindings/strong_binding_set.h" #include "services/network/cookie_settings.h" +#include "services/network/public/mojom/network_context.mojom.h" #include "services/network/public/mojom/restricted_cookie_manager.mojom.h" #include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h" @@ -156,6 +157,8 @@ public: int32_t process_id, int32_t routing_id); + network::mojom::NetworkContextParamsPtr CreateNetworkContextParams(); + #if QT_CONFIG(ssl) ClientCertificateStoreData *clientCertificateStoreData(); #endif @@ -163,8 +166,11 @@ public: static ProfileIODataQt *FromBrowserContext(content::BrowserContext *browser_context); static ProfileIODataQt *FromResourceContext(content::ResourceContext *resource_context); + base::WeakPtr<ProfileIODataQt> getWeakPtrOnIOThread(); base::WeakPtr<ProfileIODataQt> getWeakPtrOnUIThread(); + CookieMonsterDelegateQt *cookieDelegate() const { return m_cookieDelegate.get(); } + private: void removeBrowsingDataRemoverObserver(); diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp index a4ceb3a97..6e805fa67 100644 --- a/src/core/web_engine_context.cpp +++ b/src/core/web_engine_context.cpp @@ -73,6 +73,7 @@ #include "content/public/common/content_paths.h" #include "content/public/common/content_switches.h" #include "content/public/common/main_function_params.h" +#include "content/public/common/network_service_util.h" #include "gpu/command_buffer/service/gpu_switches.h" #include "gpu/command_buffer/service/sync_point_manager.h" #include "media/audio/audio_manager.h" @@ -529,8 +530,12 @@ WebEngineContext::WebEngineContext() // The video-capture service is not functioning at this moment (since 69) appendToFeatureList(disableFeatures, features::kMojoVideoCapture.name); - // We do not yet support the network-service, but it is enabled by default since 75. - appendToFeatureList(disableFeatures, network::features::kNetworkService.name); + // We do not yet fully support the network-service, but it has been enabled by default since 75. + bool enableNetworkService = parsedCommandLine->HasSwitch("enable-network-service"); + parsedCommandLine->RemoveSwitch("enable-network-service"); + if (!enableNetworkService) + appendToFeatureList(disableFeatures, network::features::kNetworkService.name); + // BlinkGenPropertyTrees is enabled by default in 75, but causes regressions. appendToFeatureList(disableFeatures, blink::features::kBlinkGenPropertyTrees.name); @@ -677,6 +682,9 @@ WebEngineContext::WebEngineContext() m_serviceManagerEnvironment = std::make_unique<content::ServiceManagerEnvironment>(content::BrowserTaskExecutor::CreateIOThread()); m_startupData = m_serviceManagerEnvironment->CreateBrowserStartupData(); + if (base::FeatureList::IsEnabled(network::features::kNetworkService)) + content::ForceInProcessNetworkService(true); + // Once the MessageLoop has been created, attach a top-level RunLoop. m_runLoop.reset(new base::RunLoop); m_runLoop->BeforeRun(); diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp index c0762aa14..76a061a8f 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp @@ -290,18 +290,21 @@ void tst_QWebEngineUrlRequestInterceptor::requestedUrl() page.setUrl(QUrl("qrc:///resources/__placeholder__")); QVERIFY(spy.wait()); QTRY_COMPARE(spy.count(), 1); + QVERIFY(interceptor.requestInfos.count() >= 1); QCOMPARE(interceptor.requestInfos.at(0).requestUrl, QUrl("qrc:///resources/content.html")); QCOMPARE(page.requestedUrl(), QUrl("qrc:///resources/__placeholder__")); QCOMPARE(page.url(), QUrl("qrc:///resources/content.html")); page.setUrl(QUrl("qrc:/non-existent.html")); QTRY_COMPARE(spy.count(), 2); + QVERIFY(interceptor.requestInfos.count() >= 3); QCOMPARE(interceptor.requestInfos.at(2).requestUrl, QUrl("qrc:/non-existent.html")); QCOMPARE(page.requestedUrl(), QUrl("qrc:///resources/__placeholder__")); QCOMPARE(page.url(), QUrl("qrc:///resources/content.html")); page.setUrl(QUrl("http://abcdef.abcdef")); QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 3, 15000); + QVERIFY(interceptor.requestInfos.count() >= 4); QCOMPARE(interceptor.requestInfos.at(3).requestUrl, QUrl("http://abcdef.abcdef/")); QCOMPARE(page.requestedUrl(), QUrl("qrc:///resources/__placeholder__")); QCOMPARE(page.url(), QUrl("qrc:///resources/content.html")); @@ -359,6 +362,7 @@ void tst_QWebEngineUrlRequestInterceptor::firstPartyUrl() page.setUrl(QUrl("qrc:///resources/firstparty.html")); QVERIFY(spy.wait()); + QVERIFY(interceptor.requestInfos.count() >= 2); QCOMPARE(interceptor.requestInfos.at(0).requestUrl, QUrl("qrc:///resources/firstparty.html")); QCOMPARE(interceptor.requestInfos.at(1).requestUrl, QUrl("qrc:///resources/content.html")); QCOMPARE(interceptor.requestInfos.at(0).firstPartyUrl, QUrl("qrc:///resources/firstparty.html")); @@ -393,16 +397,19 @@ void tst_QWebEngineUrlRequestInterceptor::firstPartyUrlNestedIframes() page.setUrl(requestUrl); QTRY_COMPARE(loadSpy.count(), 1); + QVERIFY(interceptor.requestInfos.count() >= 1); RequestInfo info = interceptor.requestInfos.at(0); QCOMPARE(info.requestUrl, requestUrl); QCOMPARE(info.firstPartyUrl, requestUrl); QCOMPARE(info.resourceType, QWebEngineUrlRequestInfo::ResourceTypeMainFrame); + QVERIFY(interceptor.requestInfos.count() >= 2); info = interceptor.requestInfos.at(1); QCOMPARE(info.requestUrl, QUrl(adjustedUrl + "iframe2.html")); QCOMPARE(info.firstPartyUrl, requestUrl); QCOMPARE(info.resourceType, QWebEngineUrlRequestInfo::ResourceTypeSubFrame); + QVERIFY(interceptor.requestInfos.count() >= 3); info = interceptor.requestInfos.at(2); QCOMPARE(info.requestUrl, QUrl(adjustedUrl + "iframe3.html")); QCOMPARE(info.firstPartyUrl, requestUrl); @@ -456,6 +463,7 @@ void tst_QWebEngineUrlRequestInterceptor::requestInterceptorByResourceType() QTRY_COMPARE(interceptor.getUrlRequestForType(static_cast<QWebEngineUrlRequestInfo::ResourceType>(resourceType)).count(), 1); QList<RequestInfo> infos = interceptor.getUrlRequestForType(static_cast<QWebEngineUrlRequestInfo::ResourceType>(resourceType)); + QVERIFY(infos.count() >= 1); QCOMPARE(infos.at(0).requestUrl, requestUrl); QCOMPARE(infos.at(0).firstPartyUrl, firstPartyUrl); QCOMPARE(infos.at(0).resourceType, resourceType); |