Skip to content

Commit

Permalink
feat: add support for running in sandboxed renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Burda committed Mar 22, 2021
1 parent 9af5b2e commit bcbfd6e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/common/get-electron-binding.ts
@@ -1,3 +1,9 @@
export const getElectronBinding: typeof process.electronBinding = process.electronBinding
? (name: string) => process.electronBinding(name as any)
: (name: string) => (process as any)._linkedBinding('electron_common_' + name)
export const getElectronBinding: typeof process.electronBinding = (name: string) => {
if ((process as any)._linkedBinding) {
return (process as any)._linkedBinding('electron_common_' + name)
} else if (process.electronBinding) {
return process.electronBinding(name as any)
} else {
return null
}
}
4 changes: 2 additions & 2 deletions src/common/module-names.ts
Expand Up @@ -43,10 +43,10 @@ export const browserModuleNames = [

const features = getElectronBinding('features');

if (features.isDesktopCapturerEnabled()) {
if (!features || features.isDesktopCapturerEnabled()) {
browserModuleNames.push('desktopCapturer');
}

if (features.isViewApiEnabled()) {
if (!features || features.isViewApiEnabled()) {
browserModuleNames.push('ImageView');
}
1 change: 1 addition & 0 deletions src/internal-ambient.d.ts
Expand Up @@ -23,5 +23,6 @@ declare namespace NodeJS {
electronBinding(name: 'event'): EventBinding;
electronBinding(name: 'v8_util'): V8UtilBinding;
electronBinding(name: 'features'): FeaturesBinding;
readonly contextId: string;
}
}
13 changes: 10 additions & 3 deletions src/renderer/remote.ts
Expand Up @@ -6,8 +6,6 @@ import { browserModuleNames } from '../common/module-names'
import { getElectronBinding } from '../common/get-electron-binding'
import { IPC_MESSAGES } from '../common/ipc-messages';

const v8Util = getElectronBinding('v8_util')

const callbacksRegistry = new CallbacksRegistry()
const remoteObjectCache = new Map()
const finalizationRegistry = new FinalizationRegistry((id: number) => {
Expand Down Expand Up @@ -35,8 +33,17 @@ function setCachedRemoteObject (id: number, value: any) {
return value
}

function getContextId() {
const v8Util = getElectronBinding('v8_util')
if (v8Util) {
return v8Util.getHiddenValue<string>(global, 'contextId')
} else {
throw new Error('Electron v13.0.0-beta.6 required to support sandboxed renderers')
}
}

// An unique ID that can represent current context.
const contextId = v8Util.getHiddenValue<string>(global, 'contextId')
const contextId = process.contextId || getContextId()

// Notify the main process when current context is going to be released.
// Note that when the renderer process is destroyed, the message may not be
Expand Down

0 comments on commit bcbfd6e

Please sign in to comment.