From fa5c8fc943be3ef993cab5712e8746f5e987426c Mon Sep 17 00:00:00 2001 From: Robo Date: Thu, 27 Aug 2015 03:10:02 +0530 Subject: [PATCH 1/2] handle partition config in browser context --- brightray/browser/browser_context.cc | 9 ++++-- brightray/browser/browser_context.h | 4 ++- brightray/browser/browser_main_parts.cc | 2 +- .../browser/url_request_context_getter.cc | 28 ++++++++++++++----- .../browser/url_request_context_getter.h | 2 ++ 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index c490de4fe2..34701a4d9c 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -69,13 +69,17 @@ class BrowserContext::ResourceContext : public content::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_)) { PathService::Get(DIR_APP_DATA, &path_); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); 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")); base::PrefServiceFactory prefs_factory; prefs_factory.SetUserPrefsFile(prefs_path, @@ -108,6 +112,7 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext( this, net_log, GetPath(), + in_memory_, BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO), BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE), protocol_handlers, @@ -130,7 +135,7 @@ scoped_ptr BrowserContext::CreateZoomLevelDelegate( } bool BrowserContext::IsOffTheRecord() const { - return false; + return in_memory_; } net::URLRequestContextGetter* BrowserContext::GetRequestContext() { diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index 6b0ca300b2..a59d21c49a 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -23,7 +23,8 @@ class BrowserContext : public content::BrowserContext, BrowserContext(); ~BrowserContext(); - virtual void Initialize(); + virtual void Initialize(const base::FilePath& partition_path, + bool in_memory = false); // content::BrowserContext: scoped_ptr CreateZoomLevelDelegate( @@ -71,6 +72,7 @@ class BrowserContext : public content::BrowserContext, void RegisterInternalPrefs(PrefRegistrySimple* pref_registry); base::FilePath path_; + bool in_memory_; scoped_ptr resource_context_; scoped_refptr url_request_getter_; scoped_ptr prefs_; diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index fe6a42b936..537d1c1f29 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -121,7 +121,7 @@ void BrowserMainParts::PreMainMessageLoopStart() { void BrowserMainParts::PreMainMessageLoopRun() { browser_context_.reset(CreateBrowserContext()); - browser_context_->Initialize(); + browser_context_->Initialize(base::FilePath()); web_ui_controller_factory_.reset( new WebUIControllerFactory(browser_context_.get())); diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 2896c49ae4..e390f1c2af 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -121,6 +121,7 @@ URLRequestContextGetter::URLRequestContextGetter( Delegate* delegate, NetLog* net_log, const base::FilePath& base_path, + bool in_memory, base::MessageLoop* io_loop, base::MessageLoop* file_loop, content::ProtocolHandlerMap* protocol_handlers, @@ -128,6 +129,7 @@ URLRequestContextGetter::URLRequestContextGetter( : delegate_(delegate), net_log_(net_log), base_path_(base_path), + in_memory_(in_memory), io_loop_(io_loop), file_loop_(file_loop), url_sec_mgr_(net::URLSecurityManager::Create(NULL, NULL)), @@ -154,8 +156,8 @@ net::HostResolver* URLRequestContextGetter::host_resolver() { net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - auto& command_line = *base::CommandLine::ForCurrentProcess(); if (!url_request_context_.get()) { + auto& command_line = *base::CommandLine::ForCurrentProcess(); url_request_context_.reset(new net::URLRequestContext); // --log-net-log @@ -166,11 +168,18 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { url_request_context_->set_network_delegate(network_delegate_.get()); storage_.reset(new net::URLRequestContextStorage(url_request_context_.get())); - auto cookie_config = content::CookieStoreConfig( - base_path_.Append(FILE_PATH_LITERAL("Cookies")), - content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES, - NULL, NULL); - storage_->set_cookie_store(content::CreateCookieStore(cookie_config)); + + scoped_refptr cookie_store = nullptr; + if (in_memory_) { + cookie_store = content::CreateCookieStore(content::CookieStoreConfig()); + } 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( new net::ChannelIDService(new net::DefaultChannelIDStore(NULL), base::WorkerPool::GetTaskRunner(true)))); @@ -265,7 +274,12 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { storage_->set_host_resolver(host_resolver.Pass()); 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_job_factory(delegate_->CreateURLRequestJobFactory( diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index 376f69c9a2..ab773b29a4 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -49,6 +49,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { Delegate* delegate, NetLog* net_log, const base::FilePath& base_path, + bool in_memory, base::MessageLoop* io_loop, base::MessageLoop* file_loop, content::ProtocolHandlerMap* protocol_handlers, @@ -66,6 +67,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { NetLog* net_log_; base::FilePath base_path_; + bool in_memory_; base::MessageLoop* io_loop_; base::MessageLoop* file_loop_; From 0e956c497dcd26290216f07e03c178b5f07eb210 Mon Sep 17 00:00:00 2001 From: Robo Date: Fri, 28 Aug 2015 01:24:05 +0530 Subject: [PATCH 2/2] use browser context from webcontents for registering datasource --- brightray/browser/browser_context.cc | 4 ++-- brightray/browser/browser_context.h | 2 +- brightray/browser/browser_main_parts.cc | 6 ++---- brightray/browser/browser_main_parts.h | 2 -- brightray/browser/devtools_ui.cc | 5 ++--- brightray/browser/devtools_ui.h | 4 +++- .../browser/web_ui_controller_factory.cc | 21 ++++++++++--------- brightray/browser/web_ui_controller_factory.h | 11 +++++----- 8 files changed, 27 insertions(+), 28 deletions(-) diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 34701a4d9c..756f27e0f9 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -69,7 +69,7 @@ class BrowserContext::ResourceContext : public content::ResourceContext { BrowserContext::BrowserContext() : resource_context_(new ResourceContext) { } -void BrowserContext::Initialize(const base::FilePath& partition_path, bool in_memory) { +void BrowserContext::Initialize(const std::string& partition_path, bool in_memory) { if (!PathService::Get(DIR_USER_DATA, &path_)) { PathService::Get(DIR_APP_DATA, &path_); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); @@ -77,7 +77,7 @@ void BrowserContext::Initialize(const base::FilePath& partition_path, bool in_me } if (!partition_path.empty()) - path_ = path_.Append(partition_path); + path_ = path_.Append(base::FilePath::FromUTF8Unsafe(partition_path)); in_memory_ = in_memory; auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences")); diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index a59d21c49a..ebf16a32b7 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -23,7 +23,7 @@ class BrowserContext : public content::BrowserContext, BrowserContext(); ~BrowserContext(); - virtual void Initialize(const base::FilePath& partition_path, + virtual void Initialize(const std::string& partition_path, bool in_memory = false); // content::BrowserContext: diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index 537d1c1f29..fd283f72ed 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -121,12 +121,10 @@ void BrowserMainParts::PreMainMessageLoopStart() { void BrowserMainParts::PreMainMessageLoopRun() { browser_context_.reset(CreateBrowserContext()); - browser_context_->Initialize(base::FilePath()); + browser_context_->Initialize(std::string()); - web_ui_controller_factory_.reset( - new WebUIControllerFactory(browser_context_.get())); content::WebUIControllerFactory::RegisterFactory( - web_ui_controller_factory_.get()); + WebUIControllerFactory::GetInstance()); // --remote-debugging-port auto command_line = base::CommandLine::ForCurrentProcess(); diff --git a/brightray/browser/browser_main_parts.h b/brightray/browser/browser_main_parts.h index d5ae8f1b7f..8822cb7c09 100644 --- a/brightray/browser/browser_main_parts.h +++ b/brightray/browser/browser_main_parts.h @@ -28,7 +28,6 @@ class WMState; namespace brightray { class BrowserContext; -class WebUIControllerFactory; class BrowserMainParts : public content::BrowserMainParts { public: @@ -57,7 +56,6 @@ class BrowserMainParts : public content::BrowserMainParts { #endif scoped_ptr browser_context_; - scoped_ptr web_ui_controller_factory_; scoped_ptr devtools_http_handler_; #if defined(TOOLKIT_VIEWS) diff --git a/brightray/browser/devtools_ui.cc b/brightray/browser/devtools_ui.cc index c5df7e0276..8f8ded3b76 100644 --- a/brightray/browser/devtools_ui.cc +++ b/brightray/browser/devtools_ui.cc @@ -6,8 +6,6 @@ #include -#include "browser/browser_context.h" - #include "base/memory/ref_counted_memory.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" @@ -93,7 +91,8 @@ class BundledDataSource : public content::URLDataSource { } // namespace -DevToolsUI::DevToolsUI(BrowserContext* browser_context, content::WebUI* web_ui) +DevToolsUI::DevToolsUI(content::BrowserContext* browser_context, + content::WebUI* web_ui) : WebUIController(web_ui) { web_ui->SetBindings(0); content::URLDataSource::Add(browser_context, new BundledDataSource()); diff --git a/brightray/browser/devtools_ui.h b/brightray/browser/devtools_ui.h index 1f5ff4f721..176de9b71b 100644 --- a/brightray/browser/devtools_ui.h +++ b/brightray/browser/devtools_ui.h @@ -6,6 +6,7 @@ #define BRIGHTRAY_BROWSER_DEVTOOLS_UI_H_ #include "base/compiler_specific.h" +#include "content/public/browser/browser_context.h" #include "content/public/browser/web_ui_controller.h" namespace brightray { @@ -14,7 +15,8 @@ class BrowserContext; class DevToolsUI : public content::WebUIController { public: - explicit DevToolsUI(BrowserContext* browser_context, content::WebUI* web_ui); + explicit DevToolsUI(content::BrowserContext* browser_context, + content::WebUI* web_ui); private: DISALLOW_COPY_AND_ASSIGN(DevToolsUI); diff --git a/brightray/browser/web_ui_controller_factory.cc b/brightray/browser/web_ui_controller_factory.cc index 9a9694de08..cacc2f8dc2 100644 --- a/brightray/browser/web_ui_controller_factory.cc +++ b/brightray/browser/web_ui_controller_factory.cc @@ -4,9 +4,8 @@ #include "browser/web_ui_controller_factory.h" -#include "browser/browser_context.h" #include "browser/devtools_ui.h" - +#include "base/memory/singleton.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_ui.h" #include "content/public/common/url_constants.h" @@ -19,9 +18,12 @@ const char kChromeUIDevToolsBundledHost[] = "devtools"; } -WebUIControllerFactory::WebUIControllerFactory(BrowserContext* browser_context) - : browser_context_(browser_context) { - DCHECK(browser_context_); +// static +WebUIControllerFactory* WebUIControllerFactory::GetInstance() { + return Singleton::get(); +} + +WebUIControllerFactory::WebUIControllerFactory() { } WebUIControllerFactory::~WebUIControllerFactory() { @@ -48,11 +50,10 @@ bool WebUIControllerFactory::UseWebUIBindingsForURL( content::WebUIController* WebUIControllerFactory::CreateWebUIControllerForURL( content::WebUI* web_ui, const GURL& url) const { - DCHECK(browser_context_); - - if (url.host() == kChromeUIDevToolsBundledHost) - return new DevToolsUI(browser_context_, web_ui); - + if (url.host() == kChromeUIDevToolsBundledHost) { + auto browser_context = web_ui->GetWebContents()->GetBrowserContext(); + return new DevToolsUI(browser_context, web_ui); + } return NULL; } diff --git a/brightray/browser/web_ui_controller_factory.h b/brightray/browser/web_ui_controller_factory.h index 6f860841af..3367255bba 100644 --- a/brightray/browser/web_ui_controller_factory.h +++ b/brightray/browser/web_ui_controller_factory.h @@ -9,13 +9,17 @@ #include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui_controller_factory.h" +template struct DefaultSingletonTraits; + namespace brightray { class BrowserContext; class WebUIControllerFactory : public content::WebUIControllerFactory { public: - explicit WebUIControllerFactory(BrowserContext* browser_context); + static WebUIControllerFactory* GetInstance(); + + WebUIControllerFactory(); virtual ~WebUIControllerFactory(); content::WebUI::TypeID GetWebUIType( @@ -28,11 +32,8 @@ class WebUIControllerFactory : public content::WebUIControllerFactory { content::WebUI* web_ui, const GURL& url) const override; - static WebUIControllerFactory* GetInstance(); - private: - // Weak reference to the browser context. - BrowserContext* browser_context_; + friend struct DefaultSingletonTraits; DISALLOW_COPY_AND_ASSIGN(WebUIControllerFactory); };