refactor: Add ElectronBrowserContext::BrowserContexts() (35-x-y backport) (#46161)

refactor: Add `ElectronBrowserContext::BrowserContexts()`

* 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-21 10:50:16 -05:00 committed by GitHub
parent e3d95bb8a4
commit 998de7aa6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 58 additions and 51 deletions

View file

@ -61,28 +61,9 @@ class ElectronBrowserContext : public content::BrowserContext {
ElectronBrowserContext(const ElectronBrowserContext&) = delete;
ElectronBrowserContext& operator=(const ElectronBrowserContext&) = delete;
// partition_id => browser_context
struct PartitionKey {
PartitionKey(const std::string_view partition, bool in_memory)
: type_{Type::Partition}, location_{partition}, in_memory_{in_memory} {}
[[nodiscard]] static std::vector<ElectronBrowserContext*> BrowserContexts();
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_;
};
using BrowserContextMap =
std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>;
[[nodiscard]] static bool IsValidContext(const void* context);
// Get or create the BrowserContext according to its |partition| and
// |in_memory|. The |options| will be passed to constructor when there is no
@ -97,7 +78,7 @@ class ElectronBrowserContext : public content::BrowserContext {
static ElectronBrowserContext* FromPath(const base::FilePath& path,
base::Value::Dict options = {});
static BrowserContextMap& browser_context_map();
static void DestroyAllContexts();
void SetUserAgent(const std::string& user_agent);
std::string GetUserAgent() const;