Add app.defaultSession

This commit is contained in:
Cheng Zhao 2015-06-23 23:40:41 +08:00
parent 4359eb4472
commit 33c2768a77
5 changed files with 28 additions and 14 deletions

View 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

View 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);
};

View 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));
}

View 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);
};

View 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_);