From 06a41cedabd2c90c0aa2b0406465276d32041285 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 12 Jul 2016 20:21:59 +0900 Subject: [PATCH] Move check of isReady to native code --- atom/browser/api/atom_api_session.cc | 13 ++++++++++++- lib/browser/api/session.js | 18 ++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 87d42486d5..7ce902484e 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -11,6 +11,7 @@ #include "atom/browser/api/atom_api_download_item.h" #include "atom/browser/api/atom_api_protocol.h" #include "atom/browser/api/atom_api_web_request.h" +#include "atom/browser/browser.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_permission_manager.h" @@ -576,11 +577,21 @@ void SetWrapSession(const WrapSessionCallback& callback) { namespace { +v8::Local FromPartition( + const std::string& partition, bool in_memory, mate::Arguments* args) { + if (!atom::Browser::Get()->is_ready()) { + args->ThrowError("Session can only be received when app is ready"); + return v8::Null(args->isolate()); + } + return atom::api::Session::FromPartition( + args->isolate(), partition, in_memory).ToV8(); +} + void Initialize(v8::Local exports, v8::Local unused, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); mate::Dictionary dict(isolate, exports); - dict.SetMethod("fromPartition", &atom::api::Session::FromPartition); + dict.SetMethod("fromPartition", &FromPartition); dict.SetMethod("_setWrapSession", &atom::api::SetWrapSession); } diff --git a/lib/browser/api/session.js b/lib/browser/api/session.js index 313cf4d96d..2fa0baca98 100644 --- a/lib/browser/api/session.js +++ b/lib/browser/api/session.js @@ -1,18 +1,9 @@ const {EventEmitter} = require('events') const {app} = require('electron') -const bindings = process.atomBinding('session') +const {fromPartition, _setWrapSession} = process.atomBinding('session') const PERSIST_PREFIX = 'persist:' -// Wrapper of binding.fromPartition that checks for ready event. -const fromPartition = function (partition, persist) { - if (!app.isReady()) { - throw new Error('session module can only be used when app is ready') - } - - return bindings.fromPartition(partition, persist) -} - // Returns the Session from |partition| string. exports.fromPartition = function (partition = '') { if (partition === '') return exports.defaultSession @@ -32,10 +23,9 @@ Object.defineProperty(exports, 'defaultSession', { } }) -const wrapSession = function (session) { +// Wraps native Session class. +_setWrapSession(function (session) { // Session is an EventEmitter. Object.setPrototypeOf(session, EventEmitter.prototype) app.emit('session-created', session) -} - -bindings._setWrapSession(wrapSession) +})