]> khamidou — GitHub mirrors - electron.git/commitdiff
Manage the life of BrowserContext in Session
authorCheng Zhao <zcbenz@gmail.com>
Sat, 5 Sep 2015 14:39:48 +0000 (22:39 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 6 Sep 2015 02:54:34 +0000 (10:54 +0800)
atom/browser/api/atom_api_session.cc
atom/browser/api/atom_api_session.h
atom/browser/api/atom_api_web_contents.cc
atom/browser/atom_browser_context.cc
vendor/brightray

index 3ca15ab871fa2d6abcf17bd79adbc1fdcb124a72..db9d89c3dd606da34aed8a672399e3cf177dec31 100644 (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);
 }
index c06348974ff4c304c0fbf7c8239d652ba64d1b8b..d9799861401b4c38d4d02ae47fa6e0e64382c29f 100644 (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);
 };
index e91656570112abe4cc470bd6848783617dbfbadb..77fbf9f1de8a89ececcfa4c16dd520690e2982da 100644 (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_);
 }
 
index 81973857b1249abbde893a7bfcf40050d06b0af3..d1ef09e70ff66b70bbe27943423db2273573990b 100644 (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
index b103a37d05335766421f4228fcb392cc57f8ea67..251521cb8c51670dbd08ab9fd2dd45e249cbb596 160000 (submodule)
@@ -1 +1 @@
-Subproject commit b103a37d05335766421f4228fcb392cc57f8ea67
+Subproject commit 251521cb8c51670dbd08ab9fd2dd45e249cbb596