Only store weak ref to BrowserContext

This commit is contained in:
Cheng Zhao 2015-09-05 22:34:42 +08:00
parent 1a8dc77951
commit d2ea3b496d
4 changed files with 26 additions and 27 deletions

View file

@ -44,20 +44,6 @@ std::string MakePartitionName(const std::string& input) {
} // namespace } // 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 { class BrowserContext::ResourceContext : public content::ResourceContext {
public: public:
ResourceContext() : getter_(nullptr) {} ResourceContext() : getter_(nullptr) {}
@ -91,9 +77,25 @@ class BrowserContext::ResourceContext : public content::ResourceContext {
URLRequestContextGetter* getter_; URLRequestContextGetter* getter_;
}; };
// static
BrowserContext::BrowserContextMap BrowserContext::browser_context_map_;
// static
scoped_refptr<BrowserContext> 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) BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
: in_memory_(in_memory), : in_memory_(in_memory),
resource_context_(new ResourceContext) { resource_context_(new ResourceContext),
weak_factory_(this) {
if (!PathService::Get(DIR_USER_DATA, &path_)) { if (!PathService::Get(DIR_USER_DATA, &path_)) {
PathService::Get(DIR_APP_DATA, &path_); PathService::Get(DIR_APP_DATA, &path_);
path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));

View file

@ -11,6 +11,7 @@
#include "browser/url_request_context_getter.h" #include "browser/url_request_context_getter.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
class PrefRegistrySimple; class PrefRegistrySimple;
@ -25,10 +26,12 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
public brightray::URLRequestContextGetter::Delegate { public brightray::URLRequestContextGetter::Delegate {
public: public:
// Get or Create the BrowserContext according to its |partition| and |in_memory|. // 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<BrowserContext> From(
const std::string& partition, bool in_memory);
// Create a new BrowserContext, embedders should implement it on their own. // Create a new BrowserContext, embedders should implement it on their own.
static BrowserContext* Create(const std::string& partition, bool in_memory); static scoped_refptr<BrowserContext> Create(
const std::string& partition, bool in_memory);
// content::BrowserContext: // content::BrowserContext:
scoped_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate( scoped_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
@ -98,7 +101,7 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
} }
}; };
using BrowserContextMap = using BrowserContextMap =
std::map<PartitionKey, scoped_refptr<brightray::BrowserContext>>; std::map<PartitionKey, base::WeakPtr<brightray::BrowserContext>>;
static BrowserContextMap browser_context_map_; static BrowserContextMap browser_context_map_;
base::FilePath path_; base::FilePath path_;
@ -108,6 +111,8 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
scoped_ptr<PrefService> prefs_; scoped_ptr<PrefService> prefs_;
scoped_ptr<PermissionManager> permission_manager_; scoped_ptr<PermissionManager> permission_manager_;
base::WeakPtrFactory<BrowserContext> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BrowserContext); DISALLOW_COPY_AND_ASSIGN(BrowserContext);
}; };

View file

@ -120,7 +120,7 @@ void BrowserMainParts::PreMainMessageLoopStart() {
} }
void BrowserMainParts::PreMainMessageLoopRun() { void BrowserMainParts::PreMainMessageLoopRun() {
browser_context_ = CreateBrowserContext(); browser_context_ = BrowserContext::From("", false);
content::WebUIControllerFactory::RegisterFactory( content::WebUIControllerFactory::RegisterFactory(
WebUIControllerFactory::GetInstance()); WebUIControllerFactory::GetInstance());
@ -144,8 +144,4 @@ int BrowserMainParts::PreCreateThreads() {
return 0; return 0;
} }
BrowserContext* BrowserMainParts::CreateBrowserContext() {
return BrowserContext::From("", false);
}
} // namespace brightray } // namespace brightray

View file

@ -46,10 +46,6 @@ class BrowserMainParts : public content::BrowserMainParts {
void PostMainMessageLoopRun() override; void PostMainMessageLoopRun() override;
int PreCreateThreads() 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: private:
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
void IncreaseFileDescriptorLimit(); void IncreaseFileDescriptorLimit();