Move check of isReady to native code

This commit is contained in:
Cheng Zhao 2016-07-12 20:21:59 +09:00
Родитель d739d8772c
Коммит 06a41cedab
2 изменённых файлов: 16 добавлений и 15 удалений

Просмотреть файл

@ -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<v8::Value> 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<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> 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);
}

Просмотреть файл

@ -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)
})