From 1e1dc22e167a9b7313aaa914fb400a19edbbeb7f Mon Sep 17 00:00:00 2001 From: Robo Date: Mon, 15 Jul 2024 17:46:24 +0900 Subject: [PATCH] fix: crash when resolving proxy due to network service restart (#42878) --- shell/browser/browser_process_impl.cc | 2 +- shell/browser/electron_browser_context.cc | 3 +-- shell/browser/net/resolve_proxy_helper.cc | 24 +++++++++++++++++------ shell/browser/net/resolve_proxy_helper.h | 7 ++++--- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/shell/browser/browser_process_impl.cc b/shell/browser/browser_process_impl.cc index f43d20475f6f..727bacccccfa 100644 --- a/shell/browser/browser_process_impl.cc +++ b/shell/browser/browser_process_impl.cc @@ -329,7 +329,7 @@ const std::string& BrowserProcessImpl::GetSystemLocale() const { electron::ResolveProxyHelper* BrowserProcessImpl::GetResolveProxyHelper() { if (!resolve_proxy_helper_) { resolve_proxy_helper_ = base::MakeRefCounted( - system_network_context_manager()->GetContext()); + nullptr /* browser_context */); } return resolve_proxy_helper_.get(); } diff --git a/shell/browser/electron_browser_context.cc b/shell/browser/electron_browser_context.cc index f373ad6b5498..779a76573d17 100644 --- a/shell/browser/electron_browser_context.cc +++ b/shell/browser/electron_browser_context.cc @@ -609,8 +609,7 @@ ElectronBrowserContext::GetFileSystemAccessPermissionContext() { ResolveProxyHelper* ElectronBrowserContext::GetResolveProxyHelper() { if (!resolve_proxy_helper_) { - resolve_proxy_helper_ = base::MakeRefCounted( - GetDefaultStoragePartition()->GetNetworkContext()); + resolve_proxy_helper_ = base::MakeRefCounted(this); } return resolve_proxy_helper_.get(); } diff --git a/shell/browser/net/resolve_proxy_helper.cc b/shell/browser/net/resolve_proxy_helper.cc index 3f54212b5458..85f425edbf9d 100644 --- a/shell/browser/net/resolve_proxy_helper.cc +++ b/shell/browser/net/resolve_proxy_helper.cc @@ -8,17 +8,20 @@ #include "base/functional/bind.h" #include "content/public/browser/browser_thread.h" +#include "content/public/browser/storage_partition.h" #include "mojo/public/cpp/bindings/pending_remote.h" #include "net/base/network_anonymization_key.h" #include "net/proxy_resolution/proxy_info.h" +#include "services/network/public/mojom/network_context.mojom.h" +#include "shell/browser/electron_browser_context.h" +#include "shell/browser/net/system_network_context_manager.h" using content::BrowserThread; namespace electron { -ResolveProxyHelper::ResolveProxyHelper( - network::mojom::NetworkContext* network_context) - : network_context_(network_context) {} +ResolveProxyHelper::ResolveProxyHelper(ElectronBrowserContext* browser_context) + : browser_context_(browser_context) {} ResolveProxyHelper::~ResolveProxyHelper() { DCHECK_CURRENTLY_ON(BrowserThread::UI); @@ -52,9 +55,18 @@ void ResolveProxyHelper::StartPendingRequest() { receiver_.set_disconnect_handler( base::BindOnce(&ResolveProxyHelper::OnProxyLookupComplete, base::Unretained(this), net::ERR_ABORTED, std::nullopt)); - network_context_->LookUpProxyForURL(pending_requests_.front().url, - net::NetworkAnonymizationKey(), - std::move(proxy_lookup_client)); + network::mojom::NetworkContext* network_context = nullptr; + if (browser_context_) { + network_context = + browser_context_->GetDefaultStoragePartition()->GetNetworkContext(); + } else { + DCHECK(SystemNetworkContextManager::GetInstance()); + network_context = SystemNetworkContextManager::GetInstance()->GetContext(); + } + CHECK(network_context); + network_context->LookUpProxyForURL(pending_requests_.front().url, + net::NetworkAnonymizationKey(), + std::move(proxy_lookup_client)); } void ResolveProxyHelper::OnProxyLookupComplete( diff --git a/shell/browser/net/resolve_proxy_helper.h b/shell/browser/net/resolve_proxy_helper.h index d12b64acc7d4..20c998be26cf 100644 --- a/shell/browser/net/resolve_proxy_helper.h +++ b/shell/browser/net/resolve_proxy_helper.h @@ -12,19 +12,20 @@ #include "base/memory/raw_ptr.h" #include "base/memory/ref_counted.h" #include "mojo/public/cpp/bindings/receiver.h" -#include "services/network/public/mojom/network_context.mojom.h" #include "services/network/public/mojom/proxy_lookup_client.mojom.h" #include "url/gurl.h" namespace electron { +class ElectronBrowserContext; + class ResolveProxyHelper : public base::RefCountedThreadSafe, network::mojom::ProxyLookupClient { public: using ResolveProxyCallback = base::OnceCallback; - explicit ResolveProxyHelper(network::mojom::NetworkContext* network_context); + explicit ResolveProxyHelper(ElectronBrowserContext* browser_context); void ResolveProxy(const GURL& url, ResolveProxyCallback callback); @@ -70,7 +71,7 @@ class ResolveProxyHelper mojo::Receiver receiver_{this}; // Weak Ref - raw_ptr network_context_ = nullptr; + raw_ptr browser_context_; }; } // namespace electron