Repository: electron/electron
Pull request: fix: crash when resolving proxy due to network service restart

Backport of #42878

See that PR for details.


Notes: fix crash when resolving proxy with `session.resolveProxy` api

Changed files:
filename: shell/browser/browser_process_impl.cc
patch: @@ -329,7 +329,7 @@ const std::string& BrowserProcessImpl::GetSystemLocale() const {
 electron::ResolveProxyHelper* BrowserProcessImpl::GetResolveProxyHelper() {
   if (!resolve_proxy_helper_) {
     resolve_proxy_helper_ = base::MakeRefCounted<electron::ResolveProxyHelper>(
-        system_network_context_manager()->GetContext());
+        nullptr /* browser_context */);
   }
   return resolve_proxy_helper_.get();
 }

filename: shell/browser/electron_browser_context.cc
patch: @@ -547,8 +547,7 @@ ElectronBrowserContext::GetFileSystemAccessPermissionContext() {
 
 ResolveProxyHelper* ElectronBrowserContext::GetResolveProxyHelper() {
   if (!resolve_proxy_helper_) {
-    resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(
-        GetDefaultStoragePartition()->GetNetworkContext());
+    resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);
   }
   return resolve_proxy_helper_.get();
 }

filename: shell/browser/net/resolve_proxy_helper.cc
patch: @@ -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(

filename: shell/browser/net/resolve_proxy_helper.h
patch: @@ -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<ResolveProxyHelper>,
       network::mojom::ProxyLookupClient {
  public:
   using ResolveProxyCallback = base::OnceCallback<void(std::string)>;
 
-  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<network::mojom::ProxyLookupClient> receiver_{this};
 
   // Weak Ref
-  raw_ptr<network::mojom::NetworkContext> network_context_ = nullptr;
+  raw_ptr<ElectronBrowserContext> browser_context_;
 };
 
 }  // namespace electron