diff --git a/libcef/browser/browser_context.cc b/libcef/browser/browser_context.cc index 121d25e36..cc98a4ebc 100644 --- a/libcef/browser/browser_context.cc +++ b/libcef/browser/browser_context.cc @@ -696,4 +696,10 @@ void CefBrowserContext::ClearSchemeHandlerFactories() { base::Bind(&CefResourceContext::ClearSchemeHandlerFactories, base::Unretained(resource_context_.get()))); } -} \ No newline at end of file +} + +network::mojom::NetworkContext* CefBrowserContext::GetNetworkContext() { + CEF_REQUIRE_UIT(); + DCHECK(net_service::IsEnabled()); + return GetDefaultStoragePartition(this)->GetNetworkContext(); +} diff --git a/libcef/browser/browser_context.h b/libcef/browser/browser_context.h index 28b80d899..3db566410 100644 --- a/libcef/browser/browser_context.h +++ b/libcef/browser/browser_context.h @@ -230,6 +230,8 @@ class CefBrowserContext : public ChromeProfileStub, CefRefPtr factory); void ClearSchemeHandlerFactories(); + network::mojom::NetworkContext* GetNetworkContext(); + void set_should_persist_session_cookies(bool value) { should_persist_session_cookies_ = value; } diff --git a/libcef/browser/request_context_impl.cc b/libcef/browser/request_context_impl.cc index b791ed85e..3d6510acf 100644 --- a/libcef/browser/request_context_impl.cc +++ b/libcef/browser/request_context_impl.cc @@ -22,8 +22,10 @@ #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/plugin_service.h" #include "content/public/browser/ssl_host_state_delegate.h" +#include "mojo/public/cpp/bindings/binding.h" #include "net/http/http_cache.h" #include "net/http/http_transaction_factory.h" +#include "services/network/public/cpp/resolve_host_client_base.h" using content::BrowserThread; @@ -58,8 +60,8 @@ const char* GetTypeString(base::Value::Type type) { } // Helper for HostResolver::Resolve. -struct ResolveHostHelper { - explicit ResolveHostHelper(CefRefPtr callback) +struct ResolveHostHelperOld { + explicit ResolveHostHelperOld(CefRefPtr callback) : callback_(callback) {} void OnResolveCompleted(int result) { @@ -83,6 +85,61 @@ struct ResolveHostHelper { std::unique_ptr request_; }; +class ResolveHostHelper : public network::ResolveHostClientBase { + public: + explicit ResolveHostHelper(CefRefPtr callback) + : callback_(callback), binding_(this) {} + + void Start(CefBrowserContext* browser_context, const CefString& origin) { + CEF_REQUIRE_UIT(); + + network::mojom::HostResolverPtrInfo host_resolver_info; + browser_context->GetNetworkContext()->CreateHostResolver( + base::nullopt, mojo::MakeRequest(&host_resolver_info)); + + network::mojom::ResolveHostClientPtr client_ptr; + binding_.Bind(mojo::MakeRequest(&client_ptr)); + binding_.set_connection_error_handler( + base::BindOnce(&ResolveHostHelper::OnComplete, base::Unretained(this), + net::ERR_FAILED, base::nullopt)); + host_resolver_ = + network::mojom::HostResolverPtr(std::move(host_resolver_info)); + host_resolver_->ResolveHost( + net::HostPortPair::FromURL(GURL(origin.ToString())), nullptr, + std::move(client_ptr)); + } + + private: + void OnComplete( + int result, + const base::Optional& resolved_addresses) override { + CEF_REQUIRE_UIT(); + + host_resolver_.reset(); + binding_.Close(); + + std::vector resolved_ips; + + if (result == net::OK) { + DCHECK(resolved_addresses && !resolved_addresses->empty()); + for (const auto& value : resolved_addresses.value()) { + resolved_ips.push_back(value.ToStringWithoutPort()); + } + } + + callback_->OnResolveCompleted(static_cast(result), + resolved_ips); + delete this; + } + + CefRefPtr callback_; + + network::mojom::HostResolverPtr host_resolver_; + mojo::Binding binding_; + + DISALLOW_COPY_AND_ASSIGN(ResolveHostHelper); +}; + } // namespace // CefBrowserContext @@ -491,19 +548,33 @@ void CefRequestContextImpl::ClearCertificateExceptions( void CefRequestContextImpl::CloseAllConnections( CefRefPtr callback) { - GetRequestContextImpl( - base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefRequestContextImpl::CloseAllConnectionsInternal, this, - callback)); + if (net_service::IsEnabled()) { + GetBrowserContext( + base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI}), + base::Bind(&CefRequestContextImpl::CloseAllConnectionsInternal, this, + callback)); + } else { + GetRequestContextImpl( + base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), + base::Bind(&CefRequestContextImpl::CloseAllConnectionsInternalOld, this, + callback)); + } } void CefRequestContextImpl::ResolveHost( const CefString& origin, CefRefPtr callback) { - GetRequestContextImpl( - base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefRequestContextImpl::ResolveHostInternal, this, origin, - callback)); + if (net_service::IsEnabled()) { + GetBrowserContext( + base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI}), + base::Bind(&CefRequestContextImpl::ResolveHostInternal, this, origin, + callback)); + } else { + GetRequestContextImpl( + base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), + base::Bind(&CefRequestContextImpl::ResolveHostInternalOld, this, origin, + callback)); + } } void CefRequestContextImpl::LoadExtension( @@ -777,6 +848,15 @@ void CefRequestContextImpl::ClearCertificateExceptionsInternal( } void CefRequestContextImpl::CloseAllConnectionsInternal( + CefRefPtr callback, + CefBrowserContext* browser_context) { + CEF_REQUIRE_UIT(); + + browser_context->GetNetworkContext()->CloseAllConnections( + base::Bind(&CefCompletionCallback::OnComplete, callback)); +} + +void CefRequestContextImpl::CloseAllConnectionsInternalOld( CefRefPtr callback, scoped_refptr request_context) { CEF_REQUIRE_IOT(); @@ -799,6 +879,17 @@ void CefRequestContextImpl::CloseAllConnectionsInternal( } void CefRequestContextImpl::ResolveHostInternal( + const CefString& origin, + CefRefPtr callback, + CefBrowserContext* browser_context) { + CEF_REQUIRE_UIT(); + + // |helper| will be deleted in ResolveHostHelper::OnComplete(). + ResolveHostHelper* helper = new ResolveHostHelper(callback); + helper->Start(browser_context, origin); +} + +void CefRequestContextImpl::ResolveHostInternalOld( const CefString& origin, CefRefPtr callback, scoped_refptr request_context) { @@ -806,8 +897,8 @@ void CefRequestContextImpl::ResolveHostInternal( int retval = ERR_FAILED; - // |helper| will be deleted in ResolveHostHelper::OnResolveCompleted(). - ResolveHostHelper* helper = new ResolveHostHelper(callback); + // |helper| will be deleted in ResolveHostHelperOld::OnResolveCompleted(). + ResolveHostHelperOld* helper = new ResolveHostHelperOld(callback); net::HostResolver* host_resolver = request_context->GetHostResolver(); if (host_resolver) { @@ -815,7 +906,7 @@ void CefRequestContextImpl::ResolveHostInternal( net::HostPortPair::FromURL(GURL(origin.ToString())), net::NetLogWithSource(), {}); retval = helper->request_->Start(base::Bind( - &ResolveHostHelper::OnResolveCompleted, base::Unretained(helper))); + &ResolveHostHelperOld::OnResolveCompleted, base::Unretained(helper))); if (retval == net::ERR_IO_PENDING) { // The result will be delivered asynchronously via the callback. return; diff --git a/libcef/browser/request_context_impl.h b/libcef/browser/request_context_impl.h index 8122221f0..4e945a934 100644 --- a/libcef/browser/request_context_impl.h +++ b/libcef/browser/request_context_impl.h @@ -157,10 +157,15 @@ class CefRequestContextImpl : public CefRequestContext { void ClearCertificateExceptionsInternal( CefRefPtr callback, CefBrowserContext* browser_context); - void CloseAllConnectionsInternal( + void CloseAllConnectionsInternal(CefRefPtr callback, + CefBrowserContext* browser_context); + void CloseAllConnectionsInternalOld( CefRefPtr callback, scoped_refptr request_context); - void ResolveHostInternal( + void ResolveHostInternal(const CefString& origin, + CefRefPtr callback, + CefBrowserContext* browser_context); + void ResolveHostInternalOld( const CefString& origin, CefRefPtr callback, scoped_refptr request_context);