refactor: add ElectronBrowserContext::GetDefaultBrowserContext() (#46065)

* refactor: add ElectronBrowserContext::DestroyAllContexts()

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

* refactor: add ElectronBrowserContext::GetDefaultBrowserContext()

clearer semantics than everyone calling From("", false)
This commit is contained in:
Charles Kerr 2025-03-17 08:43:23 -05:00 committed by GitHub
parent 9513486bf3
commit 4bf99c9bea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 26 additions and 11 deletions

View file

@ -318,6 +318,14 @@ ElectronBrowserContext::browser_context_map() {
return *browser_context_map;
}
// 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,
@ -841,6 +849,12 @@ ElectronBrowserContext* ElectronBrowserContext::From(
return context.get();
}
// static
ElectronBrowserContext* ElectronBrowserContext::GetDefaultBrowserContext(
base::Value::Dict options) {
return ElectronBrowserContext::From("", false, std::move(options));
}
ElectronBrowserContext* ElectronBrowserContext::FromPath(
const base::FilePath& path,
base::Value::Dict options) {