From cdcb0cd74c45dc534e03d150f1a966385264c902 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 4 Mar 2021 21:43:51 +0100 Subject: [PATCH] feat: add process.contextId used by @electron/remote --- docs/api/process.md | 6 ++++++ 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, 23 insertions(+), 1 deletion(-) diff --git a/docs/api/process.md b/docs/api/process.md index ee00672129ff6..a8bb9bec2d151 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,11 @@ 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. +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 c41fa561888d4..3bb3c2d6eff7e 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 a23082ef04ad3..f82f0cbe00587 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 dd02458ff676b..9715bd9c2009a 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -2623,6 +2623,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 3f216a3e03fbb..816b26be2497c 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 6eada5c945feb..8029107413227 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'); + }); + }); });