Browse Source

feat: add process.contextId used by @electron/remote (#28007)

Milan Burda 4 years ago
parent
commit
485fa5bea9

+ 8 - 0
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:

+ 4 - 0
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<string>(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;
 

+ 4 - 0
lib/sandboxed_renderer/init.ts

@@ -89,6 +89,10 @@ Object.defineProperty(preloadProcess, 'noDeprecation', {
   }
 });
 
+// Expose process.contextId
+const contextId = v8Util.getHiddenValue<string>(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'));

+ 1 - 0
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');

+ 2 - 1
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') {

+ 6 - 0
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');
+    });
+  });
 });