Move session-created event to app

This follows the convention of other *-created events.
This commit is contained in:
Cheng Zhao 2016-07-12 20:06:08 +09:00
parent 45500701f1
commit d739d8772c
2 changed files with 7 additions and 10 deletions

View file

@ -1,13 +1,12 @@
const {EventEmitter} = require('events') const {EventEmitter} = require('events')
const electron = require('electron') const {app} = require('electron')
const bindings = process.atomBinding('session') const bindings = process.atomBinding('session')
const PERSIST_PREFIX = 'persist:' const PERSIST_PREFIX = 'persist:'
const Session = new EventEmitter()
// Wrapper of binding.fromPartition that checks for ready event. // Wrapper of binding.fromPartition that checks for ready event.
const fromPartition = function (partition, persist) { const fromPartition = function (partition, persist) {
if (!electron.app.isReady()) { if (!app.isReady()) {
throw new Error('session module can only be used when app is ready') throw new Error('session module can only be used when app is ready')
} }
@ -15,7 +14,7 @@ const fromPartition = function (partition, persist) {
} }
// Returns the Session from |partition| string. // Returns the Session from |partition| string.
Session.fromPartition = function (partition = '') { exports.fromPartition = function (partition = '') {
if (partition === '') return exports.defaultSession if (partition === '') return exports.defaultSession
if (partition.startsWith(PERSIST_PREFIX)) { if (partition.startsWith(PERSIST_PREFIX)) {
@ -26,7 +25,7 @@ Session.fromPartition = function (partition = '') {
} }
// Returns the default session. // Returns the default session.
Object.defineProperty(Session, 'defaultSession', { Object.defineProperty(exports, 'defaultSession', {
enumerable: true, enumerable: true,
get: function () { get: function () {
return fromPartition('', false) return fromPartition('', false)
@ -36,9 +35,7 @@ Object.defineProperty(Session, 'defaultSession', {
const wrapSession = function (session) { const wrapSession = function (session) {
// Session is an EventEmitter. // Session is an EventEmitter.
Object.setPrototypeOf(session, EventEmitter.prototype) Object.setPrototypeOf(session, EventEmitter.prototype)
Session.emit('session-created', session) app.emit('session-created', session)
} }
bindings._setWrapSession(wrapSession) bindings._setWrapSession(wrapSession)
module.exports = Session

View file

@ -1,4 +1,4 @@
const {app, ipcMain, session, webContents, BrowserWindow} = require('electron') const {app, ipcMain, webContents, BrowserWindow} = require('electron')
const {getAllWebContents} = process.atomBinding('web_contents') const {getAllWebContents} = process.atomBinding('web_contents')
const renderProcessPreferences = process.atomBinding('render_process_preferences').forAllWebContents() const renderProcessPreferences = process.atomBinding('render_process_preferences').forAllWebContents()
@ -333,7 +333,7 @@ app.once('ready', function () {
} }
}) })
} }
session.on('session-created', function (ses) { app.on('session-created', function (ses) {
ses.protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler, function (error) { ses.protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler, function (error) {
if (error) { if (error) {
console.error(`Unable to register chrome-extension protocol: ${error}`) console.error(`Unable to register chrome-extension protocol: ${error}`)