Skip to content

Commit

Permalink
refactor: use helpers when using the remote module in sandboxed rende…
Browse files Browse the repository at this point in the history
…rers (#15960)
  • Loading branch information
miniak authored and codebytere committed Dec 5, 2018
1 parent d243a45 commit ab2a061
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/common/api/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

Expand Down
4 changes: 2 additions & 2 deletions lib/renderer/api/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { deprecate } = require('electron')

deprecate.warn(`electron.screen`, `electron.remote.screen`)

const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('screen').screen
const { getRemote } = require('@electron/internal/renderer/remote')
module.exports = getRemote('screen')
5 changes: 3 additions & 2 deletions lib/renderer/extensions/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
// Does not implement predefined messages:
// 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 fs = require('fs')
const path = require('path')
const fs = nativeRequire('fs')
const path = nativeRequire('path')

let metadata

Expand Down
8 changes: 4 additions & 4 deletions lib/renderer/extensions/storage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const fs = require('fs')
const path = require('path')
const { remote } = require('electron')
const { app } = remote
const { getRemote, nativeRequire } = require('@electron/internal/renderer/remote')
const fs = nativeRequire('fs')
const path = nativeRequire('path')
const app = getRemote('app')

const getChromeStoragePath = (storageType, extensionId) => {
return path.join(
Expand Down
14 changes: 8 additions & 6 deletions lib/renderer/inspector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { getRemote, nativeRequire } = require('@electron/internal/renderer/remote')

window.onload = function () {
// Use menu API to show context menu.
window.InspectorFrontendHost.showContextMenuAtPoint = createMenu
Expand All @@ -18,7 +20,7 @@ function completeURL (project, path) {
}

window.confirm = function (message, title) {
const { dialog } = require('electron').remote
const dialog = getRemote('dialog')
if (title == null) {
title = ''
}
Expand Down Expand Up @@ -62,8 +64,7 @@ const convertToMenuTemplate = function (items) {
}

const createMenu = function (x, y, items) {
const { remote } = require('electron')
const { Menu } = remote
const Menu = getRemote('Menu')

let template = convertToMenuTemplate(items)
if (useEditMenuItems(x, y, template)) {
Expand All @@ -73,7 +74,8 @@ const createMenu = function (x, y, items) {

// The menu is expected to show asynchronously.
setTimeout(function () {
menu.popup({ window: remote.getCurrentWindow() })
const getCurrentWindow = getRemote('getCurrentWindow')
menu.popup({ window: getCurrentWindow() })
})
}

Expand Down Expand Up @@ -116,15 +118,15 @@ const getEditMenuItems = function () {
}

const showFileChooserDialog = function (callback) {
const { dialog } = require('electron').remote
const dialog = getRemote('dialog')
const files = dialog.showOpenDialog({})
if (files != null) {
callback(pathToHtml5FileObject(files[0]))
}
}

const pathToHtml5FileObject = function (path) {
const fs = require('fs')
const fs = nativeRequire('fs')
const blob = new Blob([fs.readFileSync(path)])
blob.name = path
return blob
Expand Down
17 changes: 16 additions & 1 deletion lib/renderer/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@

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) {
throw new Error(`${usage} requires remote, which is not enabled`)
}
return remote
}

exports.nativeRequire = function (name) {
if (process.sandboxed) {
return exports.getRemoteFor(name).require(name)
} else {
return require(name)
}
}
6 changes: 3 additions & 3 deletions lib/renderer/web-view/web-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ const registerWebViewElement = function () {

// WebContents associated with this webview.
proto.getWebContents = function () {
const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
const remote = getRemoteForUsage('getWebContents()')
const { getRemote } = require('@electron/internal/renderer/remote')
const getGuestWebContents = getRemote('getGuestWebContents')
const internal = v8Util.getHiddenValue(this, 'internal')
if (!internal.guestInstanceId) {
internal.createGuestSync()
}
return remote.getGuestWebContents(internal.guestInstanceId)
return getGuestWebContents(internal.guestInstanceId)
}

// Focusing the webview should move page focus to the underlying iframe.
Expand Down
4 changes: 2 additions & 2 deletions lib/sandboxed_renderer/api/exports/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { deprecate } = require('electron')

deprecate.warn(`require('child_process')`, `remote.require('child_process')`)

const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('child_process').require('child_process')
const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteFor('child_process').require('child_process')
4 changes: 2 additions & 2 deletions lib/sandboxed_renderer/api/exports/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { deprecate } = require('electron')

deprecate.warn(`require('fs')`, `remote.require('fs')`)

const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('fs').require('fs')
const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteFor('fs').require('fs')
4 changes: 2 additions & 2 deletions lib/sandboxed_renderer/api/exports/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { deprecate } = require('electron')

deprecate.warn(`require('os')`, `remote.require('os')`)

const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('os').require('os')
const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteFor('os').require('os')
4 changes: 2 additions & 2 deletions lib/sandboxed_renderer/api/exports/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { deprecate } = require('electron')

deprecate.warn(`require('path')`, `remote.require('path')`)

const { getRemoteForUsage } = require('@electron/internal/renderer/remote')
module.exports = getRemoteForUsage('path').require('path')
const { getRemoteFor } = require('@electron/internal/renderer/remote')
module.exports = getRemoteFor('path').require('path')

0 comments on commit ab2a061

Please sign in to comment.