handle partition config in browser context

This commit is contained in:
Robo 2015-08-27 03:10:02 +05:30
parent 89e66f15c3
commit fa5c8fc943
5 changed files with 34 additions and 11 deletions

View file

@ -69,13 +69,17 @@ class BrowserContext::ResourceContext : public content::ResourceContext {
BrowserContext::BrowserContext() : resource_context_(new ResourceContext) { BrowserContext::BrowserContext() : resource_context_(new ResourceContext) {
} }
void BrowserContext::Initialize() { void BrowserContext::Initialize(const base::FilePath& partition_path, 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_);
} }
if (!partition_path.empty())
path_ = path_.Append(partition_path);
in_memory_ = in_memory;
auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences")); auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
base::PrefServiceFactory prefs_factory; base::PrefServiceFactory prefs_factory;
prefs_factory.SetUserPrefsFile(prefs_path, prefs_factory.SetUserPrefsFile(prefs_path,
@ -108,6 +112,7 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
this, this,
net_log, net_log,
GetPath(), GetPath(),
in_memory_,
BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO), BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE), BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
protocol_handlers, protocol_handlers,
@ -130,7 +135,7 @@ scoped_ptr<content::ZoomLevelDelegate> BrowserContext::CreateZoomLevelDelegate(
} }
bool BrowserContext::IsOffTheRecord() const { bool BrowserContext::IsOffTheRecord() const {
return false; return in_memory_;
} }
net::URLRequestContextGetter* BrowserContext::GetRequestContext() { net::URLRequestContextGetter* BrowserContext::GetRequestContext() {

View file

@ -23,7 +23,8 @@ class BrowserContext : public content::BrowserContext,
BrowserContext(); BrowserContext();
~BrowserContext(); ~BrowserContext();
virtual void Initialize(); virtual void Initialize(const base::FilePath& partition_path,
bool in_memory = false);
// content::BrowserContext: // content::BrowserContext:
scoped_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate( scoped_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
@ -71,6 +72,7 @@ class BrowserContext : public content::BrowserContext,
void RegisterInternalPrefs(PrefRegistrySimple* pref_registry); void RegisterInternalPrefs(PrefRegistrySimple* pref_registry);
base::FilePath path_; base::FilePath path_;
bool in_memory_;
scoped_ptr<ResourceContext> resource_context_; scoped_ptr<ResourceContext> resource_context_;
scoped_refptr<URLRequestContextGetter> url_request_getter_; scoped_refptr<URLRequestContextGetter> url_request_getter_;
scoped_ptr<PrefService> prefs_; scoped_ptr<PrefService> prefs_;

View file

@ -121,7 +121,7 @@ void BrowserMainParts::PreMainMessageLoopStart() {
void BrowserMainParts::PreMainMessageLoopRun() { void BrowserMainParts::PreMainMessageLoopRun() {
browser_context_.reset(CreateBrowserContext()); browser_context_.reset(CreateBrowserContext());
browser_context_->Initialize(); browser_context_->Initialize(base::FilePath());
web_ui_controller_factory_.reset( web_ui_controller_factory_.reset(
new WebUIControllerFactory(browser_context_.get())); new WebUIControllerFactory(browser_context_.get()));

View file

@ -121,6 +121,7 @@ URLRequestContextGetter::URLRequestContextGetter(
Delegate* delegate, Delegate* delegate,
NetLog* net_log, NetLog* net_log,
const base::FilePath& base_path, const base::FilePath& base_path,
bool in_memory,
base::MessageLoop* io_loop, base::MessageLoop* io_loop,
base::MessageLoop* file_loop, base::MessageLoop* file_loop,
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
@ -128,6 +129,7 @@ URLRequestContextGetter::URLRequestContextGetter(
: delegate_(delegate), : delegate_(delegate),
net_log_(net_log), net_log_(net_log),
base_path_(base_path), base_path_(base_path),
in_memory_(in_memory),
io_loop_(io_loop), io_loop_(io_loop),
file_loop_(file_loop), file_loop_(file_loop),
url_sec_mgr_(net::URLSecurityManager::Create(NULL, NULL)), url_sec_mgr_(net::URLSecurityManager::Create(NULL, NULL)),
@ -154,8 +156,8 @@ net::HostResolver* URLRequestContextGetter::host_resolver() {
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
auto& command_line = *base::CommandLine::ForCurrentProcess();
if (!url_request_context_.get()) { if (!url_request_context_.get()) {
auto& command_line = *base::CommandLine::ForCurrentProcess();
url_request_context_.reset(new net::URLRequestContext); url_request_context_.reset(new net::URLRequestContext);
// --log-net-log // --log-net-log
@ -166,11 +168,18 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
url_request_context_->set_network_delegate(network_delegate_.get()); url_request_context_->set_network_delegate(network_delegate_.get());
storage_.reset(new net::URLRequestContextStorage(url_request_context_.get())); storage_.reset(new net::URLRequestContextStorage(url_request_context_.get()));
auto cookie_config = content::CookieStoreConfig(
base_path_.Append(FILE_PATH_LITERAL("Cookies")), scoped_refptr<net::CookieStore> cookie_store = nullptr;
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES, if (in_memory_) {
NULL, NULL); cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
storage_->set_cookie_store(content::CreateCookieStore(cookie_config)); } else {
auto cookie_config = content::CookieStoreConfig(
base_path_.Append(FILE_PATH_LITERAL("Cookies")),
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
NULL, NULL);
cookie_store = content::CreateCookieStore(cookie_config);
}
storage_->set_cookie_store(cookie_store.get());
storage_->set_channel_id_service(make_scoped_ptr( storage_->set_channel_id_service(make_scoped_ptr(
new net::ChannelIDService(new net::DefaultChannelIDStore(NULL), new net::ChannelIDService(new net::DefaultChannelIDStore(NULL),
base::WorkerPool::GetTaskRunner(true)))); base::WorkerPool::GetTaskRunner(true))));
@ -265,7 +274,12 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
storage_->set_host_resolver(host_resolver.Pass()); storage_->set_host_resolver(host_resolver.Pass());
network_session_params.host_resolver = url_request_context_->host_resolver(); network_session_params.host_resolver = url_request_context_->host_resolver();
net::HttpCache::BackendFactory* backend = delegate_->CreateHttpCacheBackendFactory(base_path_); net::HttpCache::BackendFactory* backend = nullptr;
if (in_memory_) {
backend = net::HttpCache::DefaultBackend::InMemory(0);
} else {
backend = delegate_->CreateHttpCacheBackendFactory(base_path_);
}
storage_->set_http_transaction_factory(new net::HttpCache(network_session_params, backend)); storage_->set_http_transaction_factory(new net::HttpCache(network_session_params, backend));
storage_->set_job_factory(delegate_->CreateURLRequestJobFactory( storage_->set_job_factory(delegate_->CreateURLRequestJobFactory(

View file

@ -49,6 +49,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
Delegate* delegate, Delegate* delegate,
NetLog* net_log, NetLog* net_log,
const base::FilePath& base_path, const base::FilePath& base_path,
bool in_memory,
base::MessageLoop* io_loop, base::MessageLoop* io_loop,
base::MessageLoop* file_loop, base::MessageLoop* file_loop,
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
@ -66,6 +67,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
NetLog* net_log_; NetLog* net_log_;
base::FilePath base_path_; base::FilePath base_path_;
bool in_memory_;
base::MessageLoop* io_loop_; base::MessageLoop* io_loop_;
base::MessageLoop* file_loop_; base::MessageLoop* file_loop_;