Set prototype of constructor directly

This commit is contained in:
Cheng Zhao 2016-08-02 20:38:35 +09:00
parent 8c3232dc56
commit 844f32aa36
19 changed files with 84 additions and 158 deletions

View file

@ -7,9 +7,6 @@ const {app, ipcMain, session, Menu, NavigationController} = require('electron')
// before the webContents module.
session
const binding = process.atomBinding('web_contents')
const debuggerBinding = process.atomBinding('debugger')
let nextId = 0
const getNextId = function () {
return ++nextId
@ -81,6 +78,11 @@ const defaultPrintingSetting = {
shouldPrintSelectionOnly: false
}
const binding = process.atomBinding('web_contents')
const {WebContents} = binding
Object.setPrototypeOf(WebContents.prototype, EventEmitter.prototype)
// Following methods are mapped to webFrame.
const webFrameMethods = [
'insertText',
@ -95,13 +97,10 @@ const webFrameMethodsWithResult = [
]
// Add JavaScript wrappers for WebContents class.
const wrapWebContents = function (webContents) {
// webContents is an EventEmitter.
Object.setPrototypeOf(webContents.__proto__, EventEmitter.prototype)
WebContents.prototype._init = function () {
// Every remote callback from renderer process would add a listenter to the
// render-view-deleted event, so ignore the listenters warning.
webContents.setMaxListeners(0)
this.setMaxListeners(0)
// WebContents::send(channel, args..)
// WebContents::sendToAll(channel, args..)
@ -109,17 +108,17 @@ const wrapWebContents = function (webContents) {
if (channel == null) {
throw new Error('Missing required channel argument')
}
return webContents._send(allFrames, channel, args)
return this._send(allFrames, channel, args)
}
webContents.send = sendWrapper.bind(null, false)
webContents.sendToAll = sendWrapper.bind(null, true)
this.send = sendWrapper.bind(null, false)
this.sendToAll = sendWrapper.bind(null, true)
// The navigation controller.
const controller = new NavigationController(webContents)
const controller = new NavigationController(this)
for (const name in NavigationController.prototype) {
const method = NavigationController.prototype[name]
if (method instanceof Function) {
webContents[name] = function () {
this[name] = function () {
return method.apply(controller, arguments)
}
}
@ -141,13 +140,13 @@ const wrapWebContents = function (webContents) {
// Mapping webFrame methods.
for (const method of webFrameMethods) {
webContents[method] = function (...args) {
this[method] = function (...args) {
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
}
}
for (const method of webFrameMethodsWithResult) {
webContents[method] = function (...args) {
this[method] = function (...args) {
const callback = args[args.length - 1]
const actualArgs = args.slice(0, args.length - 2)
syncWebFrameMethods.call(this, getNextId(), method, callback, ...actualArgs)
@ -156,7 +155,7 @@ const wrapWebContents = function (webContents) {
// Make sure webContents.executeJavaScript would run the code only when the
// webContents has been loaded.
webContents.executeJavaScript = function (code, hasUserGesture, callback) {
this.executeJavaScript = function (code, hasUserGesture, callback) {
const requestId = getNextId()
if (typeof hasUserGesture === 'function') {
callback = hasUserGesture
@ -172,10 +171,10 @@ const wrapWebContents = function (webContents) {
}
// Dispatch IPC messages to the ipc module.
webContents.on('ipc-message', function (event, [channel, ...args]) {
this.on('ipc-message', function (event, [channel, ...args]) {
ipcMain.emit(channel, event, ...args)
})
webContents.on('ipc-message-sync', function (event, [channel, ...args]) {
this.on('ipc-message-sync', function (event, [channel, ...args]) {
Object.defineProperty(event, 'returnValue', {
set: function (value) {
return event.sendReply(JSON.stringify(value))
@ -186,24 +185,24 @@ const wrapWebContents = function (webContents) {
})
// Handle context menu action request from pepper plugin.
webContents.on('pepper-context-menu', function (event, params) {
this.on('pepper-context-menu', function (event, params) {
const menu = Menu.buildFromTemplate(params.menu)
menu.popup(params.x, params.y)
})
// The devtools requests the webContents to reload.
webContents.on('devtools-reload-page', function () {
webContents.reload()
this.on('devtools-reload-page', function () {
this.reload()
})
// Delays the page-title-updated event to next tick.
webContents.on('-page-title-updated', function (...args) {
this.on('-page-title-updated', function (...args) {
setImmediate(() => {
this.emit.apply(this, ['page-title-updated'].concat(args))
})
})
webContents.printToPDF = function (options, callback) {
this.printToPDF = function (options, callback) {
const printingSetting = Object.assign({}, defaultPrintingSetting)
if (options.landscape) {
printingSetting.landscape = options.landscape
@ -244,19 +243,14 @@ const wrapWebContents = function (webContents) {
this._printToPDF(printingSetting, callback)
}
app.emit('web-contents-created', {}, webContents)
app.emit('web-contents-created', {}, this)
}
binding._setWrapWebContents(wrapWebContents)
const {Debugger} = process.atomBinding('debugger')
// Add JavaScript wrappers for Debugger class.
const wrapDebugger = function (webContentsDebugger) {
// debugger is an EventEmitter.
Object.setPrototypeOf(webContentsDebugger.__proto__, EventEmitter.prototype)
}
debuggerBinding._setWrapDebugger(wrapDebugger)
Object.setPrototypeOf(Debugger.prototype, EventEmitter.prototype)
// Public APIs.
module.exports = {
create (options = {}) {
return binding.create(options)