Merge pull request #8766 from electron/browser-require-in-renderer-process
Don't require browser process API in renderer process
This commit is contained in:
commit
dba154b062
3 changed files with 61 additions and 25 deletions
|
@ -4,7 +4,9 @@ const common = require('../../../common/api/exports/electron')
|
|||
common.defineProperties(exports)
|
||||
|
||||
Object.defineProperties(exports, {
|
||||
// Browser side modules, please sort with alphabet order.
|
||||
// Browser side modules, please sort alphabetically.
|
||||
// Any modules added here must also be added to the browserModules array
|
||||
// in remote.js
|
||||
app: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
|
@ -35,18 +37,18 @@ Object.defineProperties(exports, {
|
|||
return require('../dialog')
|
||||
}
|
||||
},
|
||||
ipcMain: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require('../ipc-main')
|
||||
}
|
||||
},
|
||||
globalShortcut: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require('../global-shortcut')
|
||||
}
|
||||
},
|
||||
ipcMain: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require('../ipc-main')
|
||||
}
|
||||
},
|
||||
Menu: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
|
@ -59,6 +61,12 @@ Object.defineProperties(exports, {
|
|||
return require('../menu-item')
|
||||
}
|
||||
},
|
||||
net: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require('../net')
|
||||
}
|
||||
},
|
||||
powerMonitor: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
|
@ -107,12 +115,6 @@ Object.defineProperties(exports, {
|
|||
return require('../web-contents')
|
||||
}
|
||||
},
|
||||
net: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require('../net')
|
||||
}
|
||||
},
|
||||
|
||||
// The internal modules, invisible unless you know their names.
|
||||
NavigationController: {
|
||||
|
|
|
@ -288,18 +288,6 @@ ipcRenderer.on('ELECTRON_RENDERER_RELEASE_CALLBACK', function (event, id) {
|
|||
callbacksRegistry.remove(id)
|
||||
})
|
||||
|
||||
// List all built-in modules in browser process.
|
||||
const browserModules = require('../../browser/api/exports/electron')
|
||||
|
||||
// And add a helper receiver for each one.
|
||||
for (let name of Object.getOwnPropertyNames(browserModules)) {
|
||||
Object.defineProperty(exports, name, {
|
||||
get: function () {
|
||||
return exports.getBuiltin(name)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Get remote module.
|
||||
exports.require = function (module) {
|
||||
return metaToValue(ipcRenderer.sendSync('ELECTRON_BROWSER_REQUIRE', module))
|
||||
|
@ -344,3 +332,40 @@ exports.getGuestWebContents = function (guestInstanceId) {
|
|||
const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', guestInstanceId)
|
||||
return metaToValue(meta)
|
||||
}
|
||||
|
||||
const addBuiltinProperty = (name) => {
|
||||
Object.defineProperty(exports, name, {
|
||||
get: function () {
|
||||
return exports.getBuiltin(name)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Add each browser module name as a property
|
||||
// This list should match the exports in browser/api/exports/electron.js
|
||||
const browserModules = [
|
||||
'app',
|
||||
'autoUpdater',
|
||||
'BrowserWindow',
|
||||
'contentTracing',
|
||||
'dialog',
|
||||
'globalShortcut',
|
||||
'ipcMain',
|
||||
'Menu',
|
||||
'MenuItem',
|
||||
'net',
|
||||
'powerMonitor',
|
||||
'powerSaveBlocker',
|
||||
'protocol',
|
||||
'screen',
|
||||
'session',
|
||||
'systemPreferences',
|
||||
'Tray',
|
||||
'webContents'
|
||||
]
|
||||
browserModules.forEach(addBuiltinProperty)
|
||||
|
||||
// Add each common module name as a property
|
||||
const commonModules = {}
|
||||
require('../../common/api/exports/electron').defineProperties(commonModules)
|
||||
Object.getOwnPropertyNames(commonModules).forEach(addBuiltinProperty)
|
||||
|
|
|
@ -154,6 +154,15 @@ describe('ipc module', function () {
|
|||
})
|
||||
})
|
||||
|
||||
describe('remote modules', function () {
|
||||
it('includes browser process modules as properties', function () {
|
||||
assert.equal(typeof remote.app.getPath, 'function')
|
||||
assert.equal(typeof remote.webContents.getFocusedWebContents, 'function')
|
||||
assert.equal(typeof remote.clipboard.readText, 'function')
|
||||
assert.equal(typeof remote.shell.openExternal, 'function')
|
||||
})
|
||||
})
|
||||
|
||||
describe('remote object in renderer', function () {
|
||||
it('can change its properties', function () {
|
||||
var property = remote.require(path.join(fixtures, 'module', 'property.js'))
|
||||
|
|
Loading…
Reference in a new issue