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

* refactor: add ElectronBrowserContext::BrowserContexts()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

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

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: move PartitionKey, BrowserContextMap private

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: add ElectronBrowserContext::IsValidContext()

decouple ElectronExtensionsBrowserClient from the internals of ElectronBrowserContext

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-03-20 15:42:12 -05:00 committed by GitHub
parent b3526da28e
commit cee2c2ceeb
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/barrier_closure.h"
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/containers/to_vector.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/path_service.h" #include "base/path_service.h"
@ -308,19 +309,50 @@ bool DoesDeviceMatch(const base::Value& device,
return false; 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 } // namespace
// static // static
ElectronBrowserContext::BrowserContextMap& std::vector<ElectronBrowserContext*> ElectronBrowserContext::BrowserContexts() {
ElectronBrowserContext::browser_context_map() { return base::ToVector(ContextMap(),
static base::NoDestructor<ElectronBrowserContext::BrowserContextMap> [](auto& iter) { return iter.second.get(); });
browser_context_map; }
return *browser_context_map;
bool ElectronBrowserContext::IsValidContext(const void* context) {
return std::ranges::any_of(ContextMap(), [context](const auto& iter) {
return iter.second.get() == context;
});
} }
// static // static
void ElectronBrowserContext::DestroyAllContexts() { void ElectronBrowserContext::DestroyAllContexts() {
auto& map = browser_context_map(); auto& map = ContextMap();
// Avoid UAF by destroying the default context last. See ba629e3 for info. // Avoid UAF by destroying the default context last. See ba629e3 for info.
const auto extracted = map.extract(PartitionKey{"", false}); const auto extracted = map.extract(PartitionKey{"", false});
map.clear(); map.clear();
@ -841,7 +873,7 @@ ElectronBrowserContext* ElectronBrowserContext::From(
const std::string& partition, const std::string& partition,
bool in_memory, bool in_memory,
base::Value::Dict options) { base::Value::Dict options) {
auto& context = browser_context_map()[PartitionKey(partition, in_memory)]; auto& context = ContextMap()[PartitionKey(partition, in_memory)];
if (!context) { if (!context) {
context.reset(new ElectronBrowserContext{std::cref(partition), in_memory, context.reset(new ElectronBrowserContext{std::cref(partition), in_memory,
std::move(options)}); std::move(options)});
@ -858,7 +890,7 @@ ElectronBrowserContext* ElectronBrowserContext::GetDefaultBrowserContext(
ElectronBrowserContext* ElectronBrowserContext::FromPath( ElectronBrowserContext* ElectronBrowserContext::FromPath(
const base::FilePath& path, const base::FilePath& path,
base::Value::Dict options) { base::Value::Dict options) {
auto& context = browser_context_map()[PartitionKey(path)]; auto& context = ContextMap()[PartitionKey(path)];
if (!context) { if (!context) {
context.reset( context.reset(
new ElectronBrowserContext{std::cref(path), false, std::move(options)}); new ElectronBrowserContext{std::cref(path), false, std::move(options)});

View file

@ -61,28 +61,9 @@ class ElectronBrowserContext : public content::BrowserContext {
ElectronBrowserContext(const ElectronBrowserContext&) = delete; ElectronBrowserContext(const ElectronBrowserContext&) = delete;
ElectronBrowserContext& operator=(const ElectronBrowserContext&) = delete; ElectronBrowserContext& operator=(const ElectronBrowserContext&) = delete;
// partition_id => browser_context [[nodiscard]] static std::vector<ElectronBrowserContext*> BrowserContexts();
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) [[nodiscard]] static bool IsValidContext(const void* context);
: 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_;
};
using BrowserContextMap =
std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>;
// Get or create the default BrowserContext. // Get or create the default BrowserContext.
static ElectronBrowserContext* GetDefaultBrowserContext( static ElectronBrowserContext* GetDefaultBrowserContext(
@ -101,8 +82,6 @@ class ElectronBrowserContext : public content::BrowserContext {
static ElectronBrowserContext* FromPath(const base::FilePath& path, static ElectronBrowserContext* FromPath(const base::FilePath& path,
base::Value::Dict options = {}); base::Value::Dict options = {});
static BrowserContextMap& browser_context_map();
static void DestroyAllContexts(); static void DestroyAllContexts();
void SetUserAgent(const std::string& user_agent); void SetUserAgent(const std::string& user_agent);

View file

@ -554,11 +554,9 @@ void ElectronBrowserMainParts::PostMainMessageLoopRun() {
// Shutdown the DownloadManager before destroying Node to prevent // Shutdown the DownloadManager before destroying Node to prevent
// DownloadItem callbacks from crashing. // DownloadItem callbacks from crashing.
for (auto& iter : ElectronBrowserContext::browser_context_map()) { for (auto* browser_context : ElectronBrowserContext::BrowserContexts()) {
auto* download_manager = iter.second.get()->GetDownloadManager(); if (auto* download_manager = browser_context->GetDownloadManager())
if (download_manager) {
download_manager->Shutdown(); download_manager->Shutdown();
}
} }
// Shutdown utility process created with Electron API before // Shutdown utility process created with Electron API before

View file

@ -84,12 +84,7 @@ bool ElectronExtensionsBrowserClient::AreExtensionsDisabled(
} }
bool ElectronExtensionsBrowserClient::IsValidContext(void* context) { bool ElectronExtensionsBrowserClient::IsValidContext(void* context) {
auto& context_map = ElectronBrowserContext::browser_context_map(); return ElectronBrowserContext::IsValidContext(context);
for (auto const& entry : context_map) {
if (entry.second && entry.second.get() == context)
return true;
}
return false;
} }
bool ElectronExtensionsBrowserClient::IsSameContext(BrowserContext* first, bool ElectronExtensionsBrowserClient::IsSameContext(BrowserContext* first,
@ -340,13 +335,10 @@ void ElectronExtensionsBrowserClient::BroadcastEventToRenderers(
return; return;
} }
for (auto const& [key, browser_context] : for (auto* browser_context : ElectronBrowserContext::BrowserContexts()) {
ElectronBrowserContext::browser_context_map()) { extensions::EventRouter::Get(browser_context)
if (browser_context) { ->BroadcastEvent(std::make_unique<extensions::Event>(
extensions::EventRouter::Get(browser_context.get()) histogram_value, event_name, args.Clone()));
->BroadcastEvent(std::make_unique<extensions::Event>(
histogram_value, event_name, args.Clone()));
}
} }
} }