refactor: remove duplicate contextIsolation from getWebPreference() (#31730)

This commit is contained in:
Milan Burda 2021-11-08 17:57:56 +01:00 committed by GitHub
parent 4af21a1df8
commit fe7f296339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 7 additions and 16 deletions

View file

@ -1,10 +1,7 @@
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
const binding = process._linkedBinding('electron_renderer_context_bridge'); const binding = process._linkedBinding('electron_renderer_context_bridge');
const contextIsolationEnabled = mainFrame.getWebPreference('contextIsolation');
const checkContextIsolationEnabled = () => { const checkContextIsolationEnabled = () => {
if (!contextIsolationEnabled) throw new Error('contextBridge API can only be used when contextIsolation is enabled'); if (!process.contextIsolated) throw new Error('contextBridge API can only be used when contextIsolation is enabled');
}; };
const contextBridge: Electron.ContextBridge = { const contextBridge: Electron.ContextBridge = {
@ -17,7 +14,7 @@ const contextBridge: Electron.ContextBridge = {
export default contextBridge; export default contextBridge;
export const internalContextBridge = { export const internalContextBridge = {
contextIsolationEnabled, contextIsolationEnabled: process.contextIsolated,
overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => { overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => {
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false); return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false);
}, },

View file

@ -71,7 +71,6 @@ webFrameInit();
const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line'); const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame'); const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
const contextIsolation = mainFrame.getWebPreference('contextIsolation');
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration'); const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
const webviewTag = mainFrame.getWebPreference('webviewTag'); const webviewTag = mainFrame.getWebPreference('webviewTag');
const isHiddenPage = mainFrame.getWebPreference('hiddenPage'); const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
@ -109,7 +108,7 @@ switch (window.location.protocol) {
// Load webview tag implementation. // Load webview tag implementation.
if (process.isMainFrame) { if (process.isMainFrame) {
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule; const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule;
webViewInit(contextIsolation, webviewTag, isWebView); webViewInit(webviewTag, isWebView);
} }
if (nodeIntegration) { if (nodeIntegration) {
@ -166,7 +165,7 @@ if (nodeIntegration) {
} else { } else {
// Delete Node's symbols after the Environment has been loaded in a // Delete Node's symbols after the Environment has been loaded in a
// non context-isolated environment // non context-isolated environment
if (!contextIsolation) { if (!process.contextIsolated) {
process.once('loaded', function () { process.once('loaded', function () {
delete (global as any).process; delete (global as any).process;
delete (global as any).Buffer; delete (global as any).Buffer;

View file

@ -20,11 +20,11 @@ function handleFocusBlur () {
}); });
} }
export function webViewInit (contextIsolation: boolean, webviewTag: boolean, isWebView: boolean) { export function webViewInit (webviewTag: boolean, isWebView: boolean) {
// Don't allow recursive `<webview>`. // Don't allow recursive `<webview>`.
if (webviewTag && !isWebView) { if (webviewTag && !isWebView) {
const guestViewInternal = require('@electron/internal/renderer/web-view/guest-view-internal') as typeof guestViewInternalModule; const guestViewInternal = require('@electron/internal/renderer/web-view/guest-view-internal') as typeof guestViewInternalModule;
if (contextIsolation) { if (process.contextIsolated) {
v8Util.setHiddenValue(window, 'guestViewInternal', guestViewInternal); v8Util.setHiddenValue(window, 'guestViewInternal', guestViewInternal);
} else { } else {
const { setupWebView } = require('@electron/internal/renderer/web-view/web-view-element') as typeof webViewElementModule; const { setupWebView } = require('@electron/internal/renderer/web-view/web-view-element') as typeof webViewElementModule;

View file

@ -127,7 +127,6 @@ if (hasSwitch('unsafely-expose-electron-internals-for-testing')) {
preloadProcess._linkedBinding = process._linkedBinding; preloadProcess._linkedBinding = process._linkedBinding;
} }
const contextIsolation = mainFrame.getWebPreference('contextIsolation');
const webviewTag = mainFrame.getWebPreference('webviewTag'); const webviewTag = mainFrame.getWebPreference('webviewTag');
const isHiddenPage = mainFrame.getWebPreference('hiddenPage'); const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
const usesNativeWindowOpen = true; const usesNativeWindowOpen = true;
@ -156,7 +155,7 @@ switch (window.location.protocol) {
// Load webview tag implementation. // Load webview tag implementation.
if (process.isMainFrame) { if (process.isMainFrame) {
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule; const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init') as typeof webViewInitModule;
webViewInit(contextIsolation, webviewTag, isWebView); webViewInit(webviewTag, isWebView);
} }
// Wrap the script into a function executed in global scope. It won't have // Wrap the script into a function executed in global scope. It won't have

View file

@ -499,8 +499,6 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
} else if (pref_name == options::kOpenerID) { } else if (pref_name == options::kOpenerID) {
// NOTE: openerId is internal-only. // NOTE: openerId is internal-only.
return gin::ConvertToV8(isolate, prefs.opener_id); return gin::ConvertToV8(isolate, prefs.opener_id);
} else if (pref_name == options::kContextIsolation) {
return gin::ConvertToV8(isolate, prefs.context_isolation);
} else if (pref_name == "isWebView") { } else if (pref_name == "isWebView") {
return gin::ConvertToV8(isolate, prefs.is_webview); return gin::ConvertToV8(isolate, prefs.is_webview);
} else if (pref_name == options::kHiddenPage) { } else if (pref_name == options::kHiddenPage) {

View file

@ -6,7 +6,6 @@ setImmediate(function () {
ipcRenderer.send('answer', { ipcRenderer.send('answer', {
nativeWindowOpen: webFrame.getWebPreference('nativeWindowOpen'), nativeWindowOpen: webFrame.getWebPreference('nativeWindowOpen'),
nodeIntegration: webFrame.getWebPreference('nodeIntegration'), nodeIntegration: webFrame.getWebPreference('nodeIntegration'),
sandbox: webFrame.getWebPreference('sandbox'),
typeofProcess: typeof global.process, typeofProcess: typeof global.process,
windowOpenerIsNull windowOpenerIsNull
}); });

View file

@ -106,7 +106,6 @@ declare namespace NodeJS {
} }
interface InternalWebPreferences { interface InternalWebPreferences {
contextIsolation: boolean;
isWebView: boolean; isWebView: boolean;
hiddenPage: boolean; hiddenPage: boolean;
nativeWindowOpen: boolean; nativeWindowOpen: boolean;