Add app.defaultSession
authorCheng Zhao <zcbenz@gmail.com>
Tue, 23 Jun 2015 15:40:41 +0000 (23:40 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 23 Jun 2015 15:40:41 +0000 (23:40 +0800)
atom/browser/api/atom_api_app.cc
atom/browser/api/atom_api_app.h
atom/browser/api/atom_api_session.cc
atom/browser/api/atom_api_session.h
atom/browser/api/atom_api_web_contents.cc

index 7468a4b..2c52a19 100644 (file)
@@ -12,6 +12,7 @@
 #endif
 
 #include "atom/browser/api/atom_api_menu.h"
+#include "atom/browser/api/atom_api_session.h"
 #include "atom/browser/atom_browser_context.h"
 #include "atom/browser/atom_browser_main_parts.h"
 #include "atom/browser/browser.h"
@@ -217,6 +218,16 @@ void App::SetAppUserModelId(const std::string& app_id) {
 #endif
 }
 
+v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
+  if (default_session_.IsEmpty()) {
+    auto browser_context = static_cast<AtomBrowserContext*>(
+        AtomBrowserMainParts::Get()->browser_context());
+    auto handle = Session::Create(isolate, browser_context);
+    default_session_.Reset(isolate, handle.ToV8());
+  }
+  return v8::Local<v8::Value>::New(isolate, default_session_);
+}
+
 mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
     v8::Isolate* isolate) {
   auto browser = base::Unretained(Browser::Get());
@@ -240,7 +251,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
       .SetMethod("getPath", &App::GetPath)
       .SetMethod("resolveProxy", &App::ResolveProxy)
       .SetMethod("setDesktopName", &App::SetDesktopName)
-      .SetMethod("setAppUserModelId", &App::SetAppUserModelId);
+      .SetMethod("setAppUserModelId", &App::SetAppUserModelId)
+      .SetProperty("defaultSession", &App::DefaultSession);
 }
 
 // static
index c2f9212..7808cfe 100644 (file)
@@ -62,6 +62,9 @@ class App : public mate::EventEmitter,
   void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
   void SetDesktopName(const std::string& desktop_name);
   void SetAppUserModelId(const std::string& app_id);
+  v8::Local<v8::Value> DefaultSession(v8::Isolate* isolate);
+
+  v8::Global<v8::Value> default_session_;
 
   DISALLOW_COPY_AND_ASSIGN(App);
 };
index 18806eb..7bddee1 100644 (file)
@@ -5,7 +5,7 @@
 #include "atom/browser/api/atom_api_session.h"
 
 #include "atom/browser/api/atom_api_cookies.h"
-#include "content/public/browser/browser_context.h"
+#include "atom/browser/atom_browser_context.h"
 #include "native_mate/callback.h"
 #include "native_mate/dictionary.h"
 #include "native_mate/object_template_builder.h"
@@ -16,8 +16,8 @@ namespace atom {
 
 namespace api {
 
-Session::Session(content::BrowserContext* browser_context):
-  browser_context_(browser_context) {
+Session::Session(AtomBrowserContext* browser_context)
+    : browser_context_(browser_context) {
 }
 
 Session::~Session() {
@@ -40,7 +40,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
 // static
 mate::Handle<Session> Session::Create(
     v8::Isolate* isolate,
-    content::BrowserContext* browser_context) {
+    AtomBrowserContext* browser_context) {
   return mate::CreateHandle(isolate, new Session(browser_context));
 }
 
index 4bdab07..bd12a38 100644 (file)
@@ -8,21 +8,19 @@
 #include "native_mate/handle.h"
 #include "native_mate/wrappable.h"
 
-namespace content {
-class BrowserContext;
-}
-
 namespace atom {
 
+class AtomBrowserContext;
+
 namespace api {
 
 class Session: public mate::Wrappable {
  public:
   static mate::Handle<Session> Create(v8::Isolate* isolate,
-                                      content::BrowserContext* browser_context);
+                                      AtomBrowserContext* browser_context);
 
  protected:
-  explicit Session(content::BrowserContext* browser_context);
+  explicit Session(AtomBrowserContext* browser_context);
   ~Session();
 
   // mate::Wrappable implementations:
@@ -34,8 +32,7 @@ class Session: public mate::Wrappable {
 
   v8::Global<v8::Value> cookies_;
 
-  // The webContents which owns the Sesssion.
-  content::BrowserContext* browser_context_;
+  AtomBrowserContext* browser_context_;  // weak ref
 
   DISALLOW_COPY_AND_ASSIGN(Session);
 };
index 48a1e6a..c64db28 100644 (file)
@@ -608,7 +608,9 @@ void WebContents::InspectServiceWorker() {
 
 v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
   if (session_.IsEmpty()) {
-    auto handle = Session::Create(isolate, web_contents()->GetBrowserContext());
+    mate::Handle<api::Session> handle = Session::Create(
+        isolate,
+        static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext()));
     session_.Reset(isolate, handle.ToV8());
   }
   return v8::Local<v8::Value>::New(isolate, session_);