refactor: Add ElectronBrowserContext::BrowserContexts() (#46089)

* refactor: add ElectronBrowserContext::BrowserContexts()

* refactor: use ElectronBrowserContext::BrowserContexts() in ElectronBrowserMainParts::PostMainMessageLoopRun()

* refactor: use ElectronBrowserContext::BrowserContexts() in ElectronExtensionsBrowserClient::IsValidContext()

* refactor: use ElectronBrowserContext::BrowserContexts() in ElectronExtensionsBrowserClient::BroadcastEventToRenderers()

* refactor: move PartitionKey, BrowserContextMap private

* refactor: add ElectronBrowserContext::IsValidContext()

decouple ElectronExtensionsBrowserClient from the internals of ElectronBrowserContext
This commit is contained in:
Charles Kerr 2025-03-20 11:17:26 -05:00 committed by GitHub
parent 273baf4ec2
commit 46967ca9c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 48 deletions

View file

@ -12,6 +12,7 @@
#include "base/barrier_closure.h"
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/containers/to_vector.h"
#include "base/files/file_path.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
@ -308,19 +309,50 @@ bool DoesDeviceMatch(const base::Value& device,
return false;
}
// partition_id => browser_context
struct PartitionKey {
PartitionKey(const std::string_view partition, bool in_memory)
: type_{Type::Partition}, location_{partition}, in_memory_{in_memory} {}
explicit PartitionKey(const base::FilePath& file_path)
: type_{Type::Path},
location_{file_path.AsUTF8Unsafe()},
in_memory_{false} {}
friend auto operator<=>(const PartitionKey&, const PartitionKey&) = default;
private:
enum class Type { Partition, Path };
Type type_;
std::string location_;
bool in_memory_;
};
[[nodiscard]] auto& ContextMap() {
static base::NoDestructor<
std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>>
map;
return *map;
}
} // namespace
// static
ElectronBrowserContext::BrowserContextMap&
ElectronBrowserContext::browser_context_map() {
static base::NoDestructor<ElectronBrowserContext::BrowserContextMap>
browser_context_map;
return *browser_context_map;
std::vector<ElectronBrowserContext*> ElectronBrowserContext::BrowserContexts() {
return base::ToVector(ContextMap(),
[](auto& iter) { return iter.second.get(); });
}
bool ElectronBrowserContext::IsValidContext(const void* context) {
return std::ranges::any_of(ContextMap(), [context](const auto& iter) {
return iter.second.get() == context;
});
}
// static
void ElectronBrowserContext::DestroyAllContexts() {
auto& map = browser_context_map();
auto& map = ContextMap();
// Avoid UAF by destroying the default context last. See ba629e3 for info.
const auto extracted = map.extract(PartitionKey{"", false});
map.clear();
@ -841,7 +873,7 @@ ElectronBrowserContext* ElectronBrowserContext::From(
const std::string& partition,
bool in_memory,
base::Value::Dict options) {
auto& context = browser_context_map()[PartitionKey(partition, in_memory)];
auto& context = ContextMap()[PartitionKey(partition, in_memory)];
if (!context) {
context.reset(new ElectronBrowserContext{std::cref(partition), in_memory,
std::move(options)});
@ -858,7 +890,7 @@ ElectronBrowserContext* ElectronBrowserContext::GetDefaultBrowserContext(
ElectronBrowserContext* ElectronBrowserContext::FromPath(
const base::FilePath& path,
base::Value::Dict options) {
auto& context = browser_context_map()[PartitionKey(path)];
auto& context = ContextMap()[PartitionKey(path)];
if (!context) {
context.reset(
new ElectronBrowserContext{std::cref(path), false, std::move(options)});