refactor: use helpers when using the remote module in sandboxed renderers (#15960)

This commit is contained in:
Milan Burda 2018-12-05 19:07:32 +01:00 committed by Shelley Vohr
parent d243a45173
commit ab2a061b59
11 changed files with 46 additions and 28 deletions

View file

@ -2,8 +2,8 @@
if (process.platform === 'linux' && process.type === 'renderer') { if (process.platform === 'linux' && process.type === 'renderer') {
// On Linux we could not access clipboard in renderer process. // On Linux we could not access clipboard in renderer process.
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemote } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('clipboard').clipboard module.exports = getRemote('clipboard')
} else { } else {
const clipboard = process.atomBinding('clipboard') const clipboard = process.atomBinding('clipboard')

View file

@ -4,5 +4,5 @@ const { deprecate } = require('electron')
deprecate.warn(`electron.screen`, `electron.remote.screen`) deprecate.warn(`electron.screen`, `electron.remote.screen`)
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemote } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('screen').screen module.exports = getRemote('screen')

View file

@ -6,9 +6,10 @@
// Does not implement predefined messages: // Does not implement predefined messages:
// https://developer.chrome.com/extensions/i18n#overview-predefined // https://developer.chrome.com/extensions/i18n#overview-predefined
const { nativeRequire } = require('@electron/internal/renderer/remote')
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal') const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
const fs = require('fs') const fs = nativeRequire('fs')
const path = require('path') const path = nativeRequire('path')
let metadata let metadata

View file

@ -1,9 +1,9 @@
'use strict' 'use strict'
const fs = require('fs') const { getRemote, nativeRequire } = require('@electron/internal/renderer/remote')
const path = require('path') const fs = nativeRequire('fs')
const { remote } = require('electron') const path = nativeRequire('path')
const { app } = remote const app = getRemote('app')
const getChromeStoragePath = (storageType, extensionId) => { const getChromeStoragePath = (storageType, extensionId) => {
return path.join( return path.join(

View file

@ -1,5 +1,7 @@
'use strict' 'use strict'
const { getRemote, nativeRequire } = require('@electron/internal/renderer/remote')
window.onload = function () { window.onload = function () {
// Use menu API to show context menu. // Use menu API to show context menu.
window.InspectorFrontendHost.showContextMenuAtPoint = createMenu window.InspectorFrontendHost.showContextMenuAtPoint = createMenu
@ -18,7 +20,7 @@ function completeURL (project, path) {
} }
window.confirm = function (message, title) { window.confirm = function (message, title) {
const { dialog } = require('electron').remote const dialog = getRemote('dialog')
if (title == null) { if (title == null) {
title = '' title = ''
} }
@ -62,8 +64,7 @@ const convertToMenuTemplate = function (items) {
} }
const createMenu = function (x, y, items) { const createMenu = function (x, y, items) {
const { remote } = require('electron') const Menu = getRemote('Menu')
const { Menu } = remote
let template = convertToMenuTemplate(items) let template = convertToMenuTemplate(items)
if (useEditMenuItems(x, y, template)) { if (useEditMenuItems(x, y, template)) {
@ -73,7 +74,8 @@ const createMenu = function (x, y, items) {
// The menu is expected to show asynchronously. // The menu is expected to show asynchronously.
setTimeout(function () { setTimeout(function () {
menu.popup({ window: remote.getCurrentWindow() }) const getCurrentWindow = getRemote('getCurrentWindow')
menu.popup({ window: getCurrentWindow() })
}) })
} }
@ -116,7 +118,7 @@ const getEditMenuItems = function () {
} }
const showFileChooserDialog = function (callback) { const showFileChooserDialog = function (callback) {
const { dialog } = require('electron').remote const dialog = getRemote('dialog')
const files = dialog.showOpenDialog({}) const files = dialog.showOpenDialog({})
if (files != null) { if (files != null) {
callback(pathToHtml5FileObject(files[0])) callback(pathToHtml5FileObject(files[0]))
@ -124,7 +126,7 @@ const showFileChooserDialog = function (callback) {
} }
const pathToHtml5FileObject = function (path) { const pathToHtml5FileObject = function (path) {
const fs = require('fs') const fs = nativeRequire('fs')
const blob = new Blob([fs.readFileSync(path)]) const blob = new Blob([fs.readFileSync(path)])
blob.name = path blob.name = path
return blob return blob

View file

@ -2,9 +2,24 @@
const { remote } = require('electron') const { remote } = require('electron')
exports.getRemoteForUsage = function (usage) { exports.getRemote = function (name) {
if (!remote) {
throw new Error(`${name} requires remote, which is not enabled`)
}
return remote[name]
}
exports.getRemoteFor = function (usage) {
if (!remote) { if (!remote) {
throw new Error(`${usage} requires remote, which is not enabled`) throw new Error(`${usage} requires remote, which is not enabled`)
} }
return remote return remote
} }
exports.nativeRequire = function (name) {
if (process.sandboxed) {
return exports.getRemoteFor(name).require(name)
} else {
return require(name)
}
}

View file

@ -274,13 +274,13 @@ const registerWebViewElement = function () {
// WebContents associated with this webview. // WebContents associated with this webview.
proto.getWebContents = function () { proto.getWebContents = function () {
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemote } = require('@electron/internal/renderer/remote')
const remote = getRemoteForUsage('getWebContents()') const getGuestWebContents = getRemote('getGuestWebContents')
const internal = v8Util.getHiddenValue(this, 'internal') const internal = v8Util.getHiddenValue(this, 'internal')
if (!internal.guestInstanceId) { if (!internal.guestInstanceId) {
internal.createGuestSync() internal.createGuestSync()
} }
return remote.getGuestWebContents(internal.guestInstanceId) return getGuestWebContents(internal.guestInstanceId)
} }
// Focusing the webview should move page focus to the underlying iframe. // Focusing the webview should move page focus to the underlying iframe.

View file

@ -4,5 +4,5 @@ const { deprecate } = require('electron')
deprecate.warn(`require('child_process')`, `remote.require('child_process')`) deprecate.warn(`require('child_process')`, `remote.require('child_process')`)
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('child_process').require('child_process') module.exports = getRemoteFor('child_process').require('child_process')

View file

@ -4,5 +4,5 @@ const { deprecate } = require('electron')
deprecate.warn(`require('fs')`, `remote.require('fs')`) deprecate.warn(`require('fs')`, `remote.require('fs')`)
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('fs').require('fs') module.exports = getRemoteFor('fs').require('fs')

View file

@ -4,5 +4,5 @@ const { deprecate } = require('electron')
deprecate.warn(`require('os')`, `remote.require('os')`) deprecate.warn(`require('os')`, `remote.require('os')`)
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('os').require('os') module.exports = getRemoteFor('os').require('os')

View file

@ -4,5 +4,5 @@ const { deprecate } = require('electron')
deprecate.warn(`require('path')`, `remote.require('path')`) deprecate.warn(`require('path')`, `remote.require('path')`)
const { getRemoteForUsage } = require('@electron/internal/renderer/remote') const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('path').require('path') module.exports = getRemoteFor('path').require('path')