Browse Source

refactor: add ElectronBrowserContext::DestroyAllContexts()

Simpler semantics than previous implementation; also hides the
"default context must be destroyed last" implementation detail.

Co-authored-by: Charles Kerr <[email protected]>
trop[bot] 1 month ago
parent
commit
449e67513a

+ 8 - 0
shell/browser/electron_browser_context.cc

@@ -359,6 +359,14 @@ void ElectronBrowserContext::DestroyAllContexts() {
   map.clear();
 }
 
+// static
+void ElectronBrowserContext::DestroyAllContexts() {
+  auto& map = browser_context_map();
+  // Avoid UAF by destroying the default context last. See ba629e3 for info.
+  const auto extracted = map.extract(PartitionKey{"", false});
+  map.clear();
+}
+
 ElectronBrowserContext::ElectronBrowserContext(
     const PartitionOrPath partition_location,
     bool in_memory,

+ 2 - 0
shell/browser/electron_browser_context.h

@@ -78,6 +78,8 @@ class ElectronBrowserContext : public content::BrowserContext {
 
   static void DestroyAllContexts();
 
+  static void DestroyAllContexts();
+
   void SetUserAgent(const std::string& user_agent);
   std::string GetUserAgent() const;
   bool can_use_http_cache() const { return use_cache_; }