Move the code to native
authorCheng Zhao <zcbenz@gmail.com>
Mon, 1 Aug 2016 11:11:17 +0000 (20:11 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 1 Aug 2016 11:11:17 +0000 (20:11 +0900)
atom/browser/api/atom_api_session.cc
lib/browser/api/session.js

index 02fafdd..9117ff5 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "atom/browser/api/atom_api_session.h"
 
+#include <map>
 #include <string>
 #include <vector>
 
@@ -173,6 +174,9 @@ const char kPersistPrefix[] = "persist:";
 using WrapSessionCallback = base::Callback<void(v8::Local<v8::Value>)>;
 WrapSessionCallback g_wrap_session;
 
+// Referenced session objects.
+std::map<uint32_t, v8::Global<v8::Object>> g_sessions;
+
 class ResolveProxyHelper {
  public:
   ResolveProxyHelper(AtomBrowserContext* browser_context,
@@ -346,6 +350,7 @@ Session::Session(v8::Isolate* isolate, AtomBrowserContext* browser_context)
 Session::~Session() {
   content::BrowserContext::GetDownloadManager(browser_context())->
       RemoveObserver(this);
+  g_sessions.erase(weak_map_id());
 }
 
 void Session::OnDownloadCreated(content::DownloadManager* manager,
@@ -537,6 +542,12 @@ mate::Handle<Session> Session::CreateFrom(
   auto handle = mate::CreateHandle(
       isolate, new Session(isolate, browser_context));
   g_wrap_session.Run(handle.ToV8());
+
+  // The Sessions should never be garbage collected, since the common pattern is
+  // to use partition strings, instead of using the Session object directly.
+  g_sessions[handle->weak_map_id()] =
+      v8::Global<v8::Object>(isolate, handle.ToV8());
+
   return handle;
 }
 
index ab2d277..d82e6d0 100644 (file)
@@ -14,15 +14,9 @@ Object.defineProperties(exports, {
   }
 })
 
-const sessions = []
-
 // Wraps native Session class.
 _setWrapSession(function (session) {
   // Session is an EventEmitter.
   Object.setPrototypeOf(session, EventEmitter.prototype)
   app.emit('session-created', session)
-
-  // The Sessions should never be garbage collected, since the common pattern is
-  // to use partition strings, instead of using the Session object directly.
-  sessions.push(session)
 })