Manage the life of BrowserContext in Session

This commit is contained in:
Cheng Zhao 2015-09-05 22:39:48 +08:00
parent fafb28e41a
commit 0b1a3f3ef3
5 changed files with 44 additions and 29 deletions

View file

@ -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<v8::Value> 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<v8::Value>::New(isolate, cookies_);
@ -319,8 +319,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
// static
mate::Handle<Session> 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<Session*>(existing));
@ -330,6 +329,14 @@ mate::Handle<Session> Session::CreateFrom(
return handle;
}
// static
mate::Handle<Session> 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<AtomBrowserContext*>(browser_context.get()));
}
void SetWrapSession(const WrapSessionCallback& callback) {
g_wrap_session = callback;
}
@ -348,6 +355,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> 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);
}

View file

@ -37,7 +37,11 @@ class Session: public mate::TrackableObject<Session>,
static mate::Handle<Session> 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<Session> 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<Session>,
v8::Global<v8::Value> cookies_;
AtomBrowserContext* browser_context_; // weak ref
scoped_refptr<AtomBrowserContext> browser_context_;
DISALLOW_COPY_AND_ASSIGN(Session);
};

View file

@ -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<api::Session> 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<v8::Value> WebContents::Session(v8::Isolate* isolate) {
if (session_.IsEmpty()) {
auto handle = Session::CreateFrom(isolate, GetBrowserContext());
session_.Reset(isolate, handle.ToV8());
}
return v8::Local<v8::Value>::New(isolate, session_);
}

View file

@ -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> BrowserContext::Create(
const std::string& partition, bool in_memory) {
return make_scoped_refptr(new atom::AtomBrowserContext(partition, in_memory));
}
} // namespace brightray

2
vendor/brightray vendored

@ -1 +1 @@
Subproject commit b103a37d05335766421f4228fcb392cc57f8ea67
Subproject commit 251521cb8c51670dbd08ab9fd2dd45e249cbb596