From 33c2768a77df3e123a820c254d5d9f46ed9ecb2f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 23 Jun 2015 23:40:41 +0800 Subject: [PATCH] Add app.defaultSession --- atom/browser/api/atom_api_app.cc | 14 +++++++++++++- atom/browser/api/atom_api_app.h | 3 +++ atom/browser/api/atom_api_session.cc | 8 ++++---- atom/browser/api/atom_api_session.h | 13 +++++-------- atom/browser/api/atom_api_web_contents.cc | 4 +++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 7468a4b8b837..2c52a19d800c 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -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 App::DefaultSession(v8::Isolate* isolate) { + if (default_session_.IsEmpty()) { + auto browser_context = static_cast( + AtomBrowserMainParts::Get()->browser_context()); + auto handle = Session::Create(isolate, browser_context); + default_session_.Reset(isolate, handle.ToV8()); + } + return v8::Local::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 diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index c2f9212b7ca7..7808cfe5fa21 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -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 DefaultSession(v8::Isolate* isolate); + + v8::Global default_session_; DISALLOW_COPY_AND_ASSIGN(App); }; diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 18806ebf41f3..7bddee1ccc97 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -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::Create( v8::Isolate* isolate, - content::BrowserContext* browser_context) { + AtomBrowserContext* browser_context) { return mate::CreateHandle(isolate, new Session(browser_context)); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 4bdab0757709..bd12a3861568 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -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 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 cookies_; - // The webContents which owns the Sesssion. - content::BrowserContext* browser_context_; + AtomBrowserContext* browser_context_; // weak ref 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 48a1e6a020de..c64db2810ec5 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -608,7 +608,9 @@ void WebContents::InspectServiceWorker() { v8::Local WebContents::Session(v8::Isolate* isolate) { if (session_.IsEmpty()) { - auto handle = Session::Create(isolate, web_contents()->GetBrowserContext()); + mate::Handle handle = Session::Create( + isolate, + static_cast(web_contents()->GetBrowserContext())); session_.Reset(isolate, handle.ToV8()); } return v8::Local::New(isolate, session_);