From 1a8dc779514ffb3a5e8dcefc45899510f0b23e0c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 5 Sep 2015 20:52:50 +0800 Subject: [PATCH] Move management of browser context to BrowserContext --- brightray/browser/browser_context.cc | 24 ++++++++++++----- brightray/browser/browser_context.h | 34 ++++++++++++++++++++++--- brightray/browser/browser_main_parts.cc | 3 +-- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 207925c0aeb4..ec9f681e4288 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -44,6 +44,20 @@ std::string MakePartitionName(const std::string& input) { } // namespace +// static +BrowserContext::BrowserContextMap BrowserContext::browser_context_map_; + +// static +BrowserContext* BrowserContext::From(const std::string& partition, + bool in_memory) { + PartitionKey key(partition, in_memory); + if (!ContainsKey(browser_context_map_, key)) { + auto browser_context = BrowserContext::Create(partition, in_memory); + browser_context_map_[key] = make_scoped_refptr(browser_context); + } + return browser_context_map_[key].get(); +} + class BrowserContext::ResourceContext : public content::ResourceContext { public: ResourceContext() : getter_(nullptr) {} @@ -77,20 +91,16 @@ class BrowserContext::ResourceContext : public content::ResourceContext { URLRequestContextGetter* getter_; }; -BrowserContext::BrowserContext() - : in_memory_(false), +BrowserContext::BrowserContext(const std::string& partition, bool in_memory) + : in_memory_(in_memory), resource_context_(new ResourceContext) { -} - -void BrowserContext::Initialize(const std::string& partition, bool in_memory) { if (!PathService::Get(DIR_USER_DATA, &path_)) { PathService::Get(DIR_APP_DATA, &path_); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); PathService::Override(DIR_USER_DATA, path_); } - in_memory_ = in_memory; - if (!in_memory && !partition.empty()) + if (!in_memory_ && !partition.empty()) path_ = path_.Append(FILE_PATH_LITERAL("Partitions")) .Append(base::FilePath::FromUTF8Unsafe(MakePartitionName(partition))); diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index 68ed6aafd25b..b7cf340a1aa7 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -5,6 +5,8 @@ #ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_ #define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_ +#include + #include "browser/permission_manager.h" #include "browser/url_request_context_getter.h" @@ -22,10 +24,11 @@ class BrowserContext : public base::RefCounted, public content::BrowserContext, public brightray::URLRequestContextGetter::Delegate { public: - BrowserContext(); - ~BrowserContext(); + // Get or Create the BrowserContext according to its |partition| and |in_memory|. + static BrowserContext* From(const std::string& partition, bool in_memory); - void Initialize(const std::string& partition, bool in_memory = false); + // Create a new BrowserContext, embedders should implement it on their own. + static BrowserContext* Create(const std::string& partition, bool in_memory); // content::BrowserContext: scoped_ptr CreateZoomLevelDelegate( @@ -59,6 +62,9 @@ class BrowserContext : public base::RefCounted, PrefService* prefs() { return prefs_.get(); } protected: + BrowserContext(const std::string& partition, bool in_memory); + ~BrowserContext() override; + // Subclasses should override this to register custom preferences. virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {} @@ -73,6 +79,28 @@ class BrowserContext : public base::RefCounted, void RegisterInternalPrefs(PrefRegistrySimple* pref_registry); + // partition_id => browser_context + struct PartitionKey { + std::string partition; + bool in_memory; + + PartitionKey(const std::string& partition, bool in_memory) + : partition(partition), in_memory(in_memory) {} + + bool operator<(const PartitionKey& other) const { + if (partition != other.partition) + return in_memory < other.in_memory; + return partition < other.partition; + } + + bool operator==(const PartitionKey& other) const { + return (partition == other.partition) && (in_memory == other.in_memory); + } + }; + using BrowserContextMap = + std::map>; + static BrowserContextMap browser_context_map_; + base::FilePath path_; bool in_memory_; scoped_ptr resource_context_; diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index 1e35eca97ddf..01dae70fe691 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -121,7 +121,6 @@ void BrowserMainParts::PreMainMessageLoopStart() { void BrowserMainParts::PreMainMessageLoopRun() { browser_context_ = CreateBrowserContext(); - browser_context_->Initialize(std::string()); content::WebUIControllerFactory::RegisterFactory( WebUIControllerFactory::GetInstance()); @@ -146,7 +145,7 @@ int BrowserMainParts::PreCreateThreads() { } BrowserContext* BrowserMainParts::CreateBrowserContext() { - return new BrowserContext; + return BrowserContext::From("", false); } } // namespace brightray