Move management of browser context to BrowserContext

This commit is contained in:
Cheng Zhao 2015-09-05 20:52:50 +08:00
parent dc1e50c62e
commit 1a8dc77951
3 changed files with 49 additions and 12 deletions

View file

@ -44,6 +44,20 @@ 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) {}
@ -77,20 +91,16 @@ class BrowserContext::ResourceContext : public content::ResourceContext {
URLRequestContextGetter* getter_; URLRequestContextGetter* getter_;
}; };
BrowserContext::BrowserContext() BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
: in_memory_(false), : in_memory_(in_memory),
resource_context_(new ResourceContext) { resource_context_(new ResourceContext) {
}
void BrowserContext::Initialize(const std::string& partition, bool in_memory) {
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()));
PathService::Override(DIR_USER_DATA, path_); 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")) path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
.Append(base::FilePath::FromUTF8Unsafe(MakePartitionName(partition))); .Append(base::FilePath::FromUTF8Unsafe(MakePartitionName(partition)));

View file

@ -5,6 +5,8 @@
#ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_ #ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
#define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_ #define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
#include <map>
#include "browser/permission_manager.h" #include "browser/permission_manager.h"
#include "browser/url_request_context_getter.h" #include "browser/url_request_context_getter.h"
@ -22,10 +24,11 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
public content::BrowserContext, public content::BrowserContext,
public brightray::URLRequestContextGetter::Delegate { public brightray::URLRequestContextGetter::Delegate {
public: public:
BrowserContext(); // Get or Create the BrowserContext according to its |partition| and |in_memory|.
~BrowserContext(); 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: // content::BrowserContext:
scoped_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate( scoped_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
@ -59,6 +62,9 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
PrefService* prefs() { return prefs_.get(); } PrefService* prefs() { return prefs_.get(); }
protected: protected:
BrowserContext(const std::string& partition, bool in_memory);
~BrowserContext() override;
// Subclasses should override this to register custom preferences. // Subclasses should override this to register custom preferences.
virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {} virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {}
@ -73,6 +79,28 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
void RegisterInternalPrefs(PrefRegistrySimple* pref_registry); 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<PartitionKey, scoped_refptr<brightray::BrowserContext>>;
static BrowserContextMap browser_context_map_;
base::FilePath path_; base::FilePath path_;
bool in_memory_; bool in_memory_;
scoped_ptr<ResourceContext> resource_context_; scoped_ptr<ResourceContext> resource_context_;

View file

@ -121,7 +121,6 @@ void BrowserMainParts::PreMainMessageLoopStart() {
void BrowserMainParts::PreMainMessageLoopRun() { void BrowserMainParts::PreMainMessageLoopRun() {
browser_context_ = CreateBrowserContext(); browser_context_ = CreateBrowserContext();
browser_context_->Initialize(std::string());
content::WebUIControllerFactory::RegisterFactory( content::WebUIControllerFactory::RegisterFactory(
WebUIControllerFactory::GetInstance()); WebUIControllerFactory::GetInstance());
@ -146,7 +145,7 @@ int BrowserMainParts::PreCreateThreads() {
} }
BrowserContext* BrowserMainParts::CreateBrowserContext() { BrowserContext* BrowserMainParts::CreateBrowserContext() {
return new BrowserContext; return BrowserContext::From("", false);
} }
} // namespace brightray } // namespace brightray