refactor: cleanup web-frame-init.js (#14516)

* refactor: add error-utils.js

* fix exception handling for asyncWebFrameMethods

* remove dead code

* handle exceptions

* rename rehydratedError to deserializedError

* Revert "handle exceptions"

This reverts commit 396b179948b137f9e525e9ebba4f7c6e9bf19429.
This commit is contained in:
Milan Burda 2018-09-11 11:56:00 +02:00 committed by Samuel Attard
parent 38419e3a6a
commit 0821edc843
4 changed files with 53 additions and 65 deletions

View file

@ -6,6 +6,8 @@ const path = require('path')
const url = require('url')
const {app, ipcMain, session, NavigationController, deprecate} = electron
const errorUtils = require('../../common/error-utils')
// session is not used here, the purpose is to make sure session is initalized
// before the webContents module.
// eslint-disable-next-line
@ -111,17 +113,6 @@ const webFrameMethods = [
'setLayoutZoomLevelLimits',
'setVisualZoomLevelLimits'
]
const webFrameMethodsWithResult = []
const errorConstructors = {
Error,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError
}
const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
return new Promise((resolve, reject) => {
@ -131,40 +122,18 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
if (typeof callback === 'function') callback(result)
resolve(result)
} else {
if (error.__ELECTRON_SERIALIZED_ERROR__ && errorConstructors[error.name]) {
const rehydratedError = new errorConstructors[error.name](error.message)
rehydratedError.stack = error.stack
reject(rehydratedError)
} else {
reject(error)
}
reject(errorUtils.deserialize(error))
}
})
})
}
const syncWebFrameMethods = function (requestId, method, callback, ...args) {
this.send('ELECTRON_INTERNAL_RENDERER_SYNC_WEB_FRAME_METHOD', requestId, method, args)
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_SYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, result) {
if (callback) callback(result)
})
}
for (const method of webFrameMethods) {
WebContents.prototype[method] = function (...args) {
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
}
}
for (const method of webFrameMethodsWithResult) {
WebContents.prototype[method] = function (...args) {
const callback = args[args.length - 1]
const actualArgs = args.slice(0, args.length - 2)
syncWebFrameMethods.call(this, getNextId(), method, callback, ...actualArgs)
}
}
// Make sure WebContents::executeJavaScript would run the code only when the
// WebContents has been loaded.
WebContents.prototype.executeJavaScript = function (code, hasUserGesture, callback) {