From d2ea3b496dec0a37ef69c6bba9374a040c58e1d7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 5 Sep 2015 22:34:42 +0800 Subject: [PATCH] Only store weak ref to BrowserContext --- brightray/browser/browser_context.cc | 32 +++++++++++++------------ brightray/browser/browser_context.h | 11 ++++++--- brightray/browser/browser_main_parts.cc | 6 +---- brightray/browser/browser_main_parts.h | 4 ---- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index ec9f681e42..f5d6effdeb 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -44,20 +44,6 @@ 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) {} @@ -91,9 +77,25 @@ class BrowserContext::ResourceContext : public content::ResourceContext { URLRequestContextGetter* getter_; }; +// static +BrowserContext::BrowserContextMap BrowserContext::browser_context_map_; + +// static +scoped_refptr BrowserContext::From( + const std::string& partition, bool in_memory) { + PartitionKey key(partition, in_memory); + if (browser_context_map_[key].get()) + return make_scoped_refptr(browser_context_map_[key].get()); + + auto browser_context = BrowserContext::Create(partition, in_memory); + browser_context_map_[key] = browser_context->weak_factory_.GetWeakPtr(); + return browser_context; +} + BrowserContext::BrowserContext(const std::string& partition, bool in_memory) : in_memory_(in_memory), - resource_context_(new ResourceContext) { + resource_context_(new ResourceContext), + weak_factory_(this) { if (!PathService::Get(DIR_USER_DATA, &path_)) { PathService::Get(DIR_APP_DATA, &path_); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index b7cf340a1a..0ae89f3e48 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -11,6 +11,7 @@ #include "browser/url_request_context_getter.h" #include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" #include "content/public/browser/browser_context.h" class PrefRegistrySimple; @@ -25,10 +26,12 @@ class BrowserContext : public base::RefCounted, public brightray::URLRequestContextGetter::Delegate { public: // Get or Create the BrowserContext according to its |partition| and |in_memory|. - static BrowserContext* From(const std::string& partition, bool in_memory); + static scoped_refptr From( + const std::string& partition, bool in_memory); // Create a new BrowserContext, embedders should implement it on their own. - static BrowserContext* Create(const std::string& partition, bool in_memory); + static scoped_refptr Create( + const std::string& partition, bool in_memory); // content::BrowserContext: scoped_ptr CreateZoomLevelDelegate( @@ -98,7 +101,7 @@ class BrowserContext : public base::RefCounted, } }; using BrowserContextMap = - std::map>; + std::map>; static BrowserContextMap browser_context_map_; base::FilePath path_; @@ -108,6 +111,8 @@ class BrowserContext : public base::RefCounted, scoped_ptr prefs_; scoped_ptr permission_manager_; + base::WeakPtrFactory weak_factory_; + DISALLOW_COPY_AND_ASSIGN(BrowserContext); }; diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index 01dae70fe6..b69c90f88a 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -120,7 +120,7 @@ void BrowserMainParts::PreMainMessageLoopStart() { } void BrowserMainParts::PreMainMessageLoopRun() { - browser_context_ = CreateBrowserContext(); + browser_context_ = BrowserContext::From("", false); content::WebUIControllerFactory::RegisterFactory( WebUIControllerFactory::GetInstance()); @@ -144,8 +144,4 @@ int BrowserMainParts::PreCreateThreads() { return 0; } -BrowserContext* BrowserMainParts::CreateBrowserContext() { - return BrowserContext::From("", false); -} - } // namespace brightray diff --git a/brightray/browser/browser_main_parts.h b/brightray/browser/browser_main_parts.h index 0e151d8dd4..5ed3e1eb1f 100644 --- a/brightray/browser/browser_main_parts.h +++ b/brightray/browser/browser_main_parts.h @@ -46,10 +46,6 @@ class BrowserMainParts : public content::BrowserMainParts { void PostMainMessageLoopRun() override; int PreCreateThreads() override; - // Subclasses should override this to provide their own BrowserContxt - // implementation. The caller takes ownership of the returned object. - virtual BrowserContext* CreateBrowserContext(); - private: #if defined(OS_MACOSX) void IncreaseFileDescriptorLimit();