From 0b1a3f3ef3b02bd76f411eef775913242d2ff3fb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 5 Sep 2015 22:39:48 +0800 Subject: [PATCH] Manage the life of BrowserContext in Session --- atom/browser/api/atom_api_session.cc | 20 ++++++++---- atom/browser/api/atom_api_session.h | 8 +++-- atom/browser/api/atom_api_web_contents.cc | 37 ++++++++++++----------- atom/browser/atom_browser_context.cc | 6 ++-- vendor/brightray | 2 +- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 3ca15ab871fa..db9d89c3dd60 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -241,7 +241,7 @@ Session::Session(AtomBrowserContext* browser_context) Session::~Session() { auto download_manager = - content::BrowserContext::GetDownloadManager(browser_context_); + content::BrowserContext::GetDownloadManager(browser_context()); download_manager->RemoveObserver(this); } @@ -258,7 +258,7 @@ void Session::OnDownloadCreated(content::DownloadManager* manager, } void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { - new ResolveProxyHelper(browser_context_, url, callback); + new ResolveProxyHelper(browser_context(), url, callback); } void Session::ClearCache(const net::CompletionCallback& callback) { @@ -279,7 +279,7 @@ void Session::ClearStorageData(mate::Arguments* args) { } auto storage_partition = - content::BrowserContext::GetStoragePartition(browser_context_, nullptr); + content::BrowserContext::GetStoragePartition(browser_context(), nullptr); storage_partition->ClearData( options.storage_types, options.quota_types, options.origin, content::StoragePartition::OriginMatcherFunction(), @@ -300,7 +300,7 @@ void Session::SetDownloadPath(const base::FilePath& path) { v8::Local Session::Cookies(v8::Isolate* isolate) { if (cookies_.IsEmpty()) { - auto handle = atom::api::Cookies::Create(isolate, browser_context_); + auto handle = atom::api::Cookies::Create(isolate, browser_context()); cookies_.Reset(isolate, handle.ToV8()); } return v8::Local::New(isolate, cookies_); @@ -319,8 +319,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( // static mate::Handle Session::CreateFrom( - v8::Isolate* isolate, - AtomBrowserContext* browser_context) { + v8::Isolate* isolate, AtomBrowserContext* browser_context) { auto existing = TrackableObject::FromWrappedClass(isolate, browser_context); if (existing) return mate::CreateHandle(isolate, static_cast(existing)); @@ -330,6 +329,14 @@ mate::Handle Session::CreateFrom( return handle; } +// static +mate::Handle Session::FromPartition( + v8::Isolate* isolate, const std::string& partition, bool in_memory) { + auto browser_context = brightray::BrowserContext::From(partition, in_memory); + return CreateFrom(isolate, + static_cast(browser_context.get())); +} + void SetWrapSession(const WrapSessionCallback& callback) { g_wrap_session = callback; } @@ -348,6 +355,7 @@ void Initialize(v8::Local exports, v8::Local unused, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); mate::Dictionary dict(isolate, exports); + dict.SetMethod("fromPartition", &atom::api::Session::FromPartition); dict.SetMethod("_setWrapSession", &atom::api::SetWrapSession); dict.SetMethod("_clearWrapSession", &atom::api::ClearWrapSession); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index c06348974ff4..d9799861401b 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -37,7 +37,11 @@ class Session: public mate::TrackableObject, static mate::Handle CreateFrom( v8::Isolate* isolate, AtomBrowserContext* browser_context); - AtomBrowserContext* browser_context() const { return browser_context_; } + // Gets the Session of |partition| and |in_memory|. + static mate::Handle FromPartition( + v8::Isolate* isolate, const std::string& partition, bool in_memory); + + AtomBrowserContext* browser_context() const { return browser_context_.get(); } protected: explicit Session(AtomBrowserContext* browser_context); @@ -61,7 +65,7 @@ class Session: public mate::TrackableObject, v8::Global cookies_; - AtomBrowserContext* browser_context_; // weak ref + scoped_refptr browser_context_; DISALLOW_COPY_AND_ASSIGN(Session); }; diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index e91656570112..77fbf9f1de8a 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -156,30 +156,36 @@ WebContents::WebContents(content::WebContents* web_contents) WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options) { + // Whether it is a guest WebContents. bool is_guest = false; options.Get("isGuest", &is_guest); - type_ = is_guest ? WEB_VIEW : BROWSER_WINDOW; - // TODO(zcbenz): Use host's BrowserContext when no partition is specified. - content::BrowserContext* browser_context = - AtomBrowserMainParts::Get()->browser_context(); + // Obtain the session. + std::string partition; + mate::Handle session; + if (options.Get("session", &session)) { + } else if (options.Get("partition", &partition)) { + bool in_memory = true; + options.Get("inMemory", &in_memory); + session = Session::FromPartition(isolate, partition, in_memory); + } else { + // Use the default session if not specified. + session = Session::FromPartition(isolate, "", false); + } + session_.Reset(isolate, session.ToV8()); + content::WebContents* web_contents; if (is_guest) { - std::string partition; - bool in_memory = false; - options.Get("partition", &partition); - options.Get("inMemory", &in_memory); - if (!partition.empty()) - browser_context = brightray::BrowserContext::From(partition, in_memory); content::SiteInstance* site_instance = content::SiteInstance::CreateForURL( - browser_context, GURL("chrome-guest://fake-host")); - content::WebContents::CreateParams params(browser_context, site_instance); + session->browser_context(), GURL("chrome-guest://fake-host")); + content::WebContents::CreateParams params( + session->browser_context(), site_instance); guest_delegate_.reset(new WebViewGuestDelegate); params.guest_delegate = guest_delegate_.get(); web_contents = content::WebContents::Create(params); } else { - content::WebContents::CreateParams params(browser_context); + content::WebContents::CreateParams params(session->browser_context()); web_contents = content::WebContents::Create(params); } @@ -496,6 +502,7 @@ void WebContents::NavigationEntryCommitted( } void WebContents::Destroy() { + session_.Reset(); if (type_ == WEB_VIEW && managed_web_contents()) { // When force destroying the "destroyed" event is not emitted. WebContentsDestroyed(); @@ -658,10 +665,6 @@ void WebContents::InspectServiceWorker() { } v8::Local WebContents::Session(v8::Isolate* isolate) { - if (session_.IsEmpty()) { - auto handle = Session::CreateFrom(isolate, GetBrowserContext()); - session_.Reset(isolate, handle.ToV8()); - } return v8::Local::New(isolate, session_); } diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 81973857b124..d1ef09e70ff6 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -168,9 +168,9 @@ void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) { namespace brightray { // static -BrowserContext* BrowserContext::Create(const std::string& partition, - bool in_memory) { - return new atom::AtomBrowserContext(partition, in_memory); +scoped_refptr BrowserContext::Create( + const std::string& partition, bool in_memory) { + return make_scoped_refptr(new atom::AtomBrowserContext(partition, in_memory)); } } // namespace brightray diff --git a/vendor/brightray b/vendor/brightray index b103a37d0533..251521cb8c51 160000 --- a/vendor/brightray +++ b/vendor/brightray @@ -1 +1 @@ -Subproject commit b103a37d05335766421f4228fcb392cc57f8ea67 +Subproject commit 251521cb8c51670dbd08ab9fd2dd45e249cbb596