From 485fa5bea9313b45c014b65a1fb8291aa448782e Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Wed, 17 Mar 2021 19:23:03 +0100 Subject: [PATCH] feat: add process.contextId used by @electron/remote (#28007) --- docs/api/process.md | 8 ++++++++ lib/renderer/init.ts | 4 ++++ lib/sandboxed_renderer/init.ts | 4 ++++ spec-main/api-browser-window-spec.ts | 1 + spec-main/fixtures/module/preload-sandbox.js | 3 ++- spec/api-process-spec.js | 6 ++++++ 6 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/api/process.md b/docs/api/process.md index ee00672129ff..e4b5c26d550d 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -35,6 +35,7 @@ In sandboxed renderers the `process` object contains only a subset of the APIs: * `versions` * `mas` * `windowsStore` +* `contextId` ## Events @@ -133,6 +134,13 @@ A `String` representing Electron's version string. A `Boolean`. If the app is running as a Windows Store app (appx), this property is `true`, for otherwise it is `undefined`. +### `process.contextId` _Readonly_ + +A `String` (optional) representing a globally unique ID of the current JavaScript context. +Each frame has its own JavaScript context. When contextIsolation is enabled, the isolated +world also has a separate JavaScript context. +This property is only available in the renderer process. + ## Methods The `process` object has the following methods: diff --git a/lib/renderer/init.ts b/lib/renderer/init.ts index c41fa561888d..3bb3c2d6eff7 100644 --- a/lib/renderer/init.ts +++ b/lib/renderer/init.ts @@ -39,6 +39,10 @@ require('@electron/internal/common/init'); // The global variable will be used by ipc for event dispatching const v8Util = process._linkedBinding('electron_common_v8_util'); +// Expose process.contextId +const contextId = v8Util.getHiddenValue(global, 'contextId'); +Object.defineProperty(process, 'contextId', { enumerable: true, value: contextId }); + const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal'); const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default; diff --git a/lib/sandboxed_renderer/init.ts b/lib/sandboxed_renderer/init.ts index a23082ef04ad..f82f0cbe0058 100644 --- a/lib/sandboxed_renderer/init.ts +++ b/lib/sandboxed_renderer/init.ts @@ -89,6 +89,10 @@ Object.defineProperty(preloadProcess, 'noDeprecation', { } }); +// Expose process.contextId +const contextId = v8Util.getHiddenValue(global, 'contextId'); +Object.defineProperty(preloadProcess, 'contextId', { enumerable: true, value: contextId }); + process.on('loaded', () => (preloadProcess as events.EventEmitter).emit('loaded')); process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit')); (process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start')); diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index bc673855bbfa..957607b85552 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -2490,6 +2490,7 @@ describe('BrowserWindow module', () => { expect(test.type).to.equal('renderer'); expect(test.version).to.equal(process.version); expect(test.versions).to.deep.equal(process.versions); + expect(test.contextId).to.be.a('string'); if (process.platform === 'linux' && test.osSandbox) { expect(test.creationTime).to.be.null('creation time'); diff --git a/spec-main/fixtures/module/preload-sandbox.js b/spec-main/fixtures/module/preload-sandbox.js index 3f216a3e03fb..816b26be2497 100644 --- a/spec-main/fixtures/module/preload-sandbox.js +++ b/spec-main/fixtures/module/preload-sandbox.js @@ -42,7 +42,8 @@ sandboxed: process.sandboxed, type: process.type, version: process.version, - versions: process.versions + versions: process.versions, + contextId: process.contextId }; } } else if (location.href !== 'about:blank') { diff --git a/spec/api-process-spec.js b/spec/api-process-spec.js index 6eada5c945fe..802910741322 100644 --- a/spec/api-process-spec.js +++ b/spec/api-process-spec.js @@ -115,4 +115,10 @@ describe('process module', () => { expect(success).to.be.false(); }); }); + + describe('process.contextId', () => { + it('is a string', () => { + expect(process.contextId).to.be.a('string'); + }); + }); });