refactor: remove more dead code post render process reuse (#28983)

* Overrides for window.history.*
* Node environment cleanup / creation logic
* Options and switches that are now static values
This commit is contained in:
Samuel Attard 2021-05-04 11:30:29 -07:00 committed by GitHub
parent 38c877e9d3
commit e5e8ab4eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 10 additions and 92 deletions

View file

@ -100,22 +100,6 @@ ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD, async function (event
};
});
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_BACK, function (event) {
event.sender.goBack();
});
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_FORWARD, function (event) {
event.sender.goForward();
});
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_TO_OFFSET, function (event, offset) {
event.sender.goToOffset(offset);
});
ipcMainInternal.on(IPC_MESSAGES.NAVIGATION_CONTROLLER_LENGTH, function (event) {
event.returnValue = event.sender.length();
});
ipcMainInternal.on(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, function (event, preloadPath: string, error: Error) {
event.sender.emit('preload-error', event, preloadPath, error);
});

View file

@ -29,11 +29,6 @@ export const enum IPC_MESSAGES {
RENDERER_WEB_FRAME_METHOD = 'RENDERER_WEB_FRAME_METHOD',
NAVIGATION_CONTROLLER_GO_BACK = 'NAVIGATION_CONTROLLER_GO_BACK',
NAVIGATION_CONTROLLER_GO_FORWARD = 'NAVIGATION_CONTROLLER_GO_FORWARD',
NAVIGATION_CONTROLLER_GO_TO_OFFSET = 'NAVIGATION_CONTROLLER_GO_TO_OFFSET',
NAVIGATION_CONTROLLER_LENGTH = 'NAVIGATION_CONTROLLER_LENGTH',
INSPECTOR_CONFIRM = 'INSPECTOR_CONFIRM',
INSPECTOR_CONTEXT_MENU = 'INSPECTOR_CONTEXT_MENU',
INSPECTOR_SELECT_FILE = 'INSPECTOR_SELECT_FILE',

View file

@ -70,7 +70,6 @@ const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
const webviewTag = mainFrame.getWebPreference('webviewTag');
const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
const usesNativeWindowOpen = mainFrame.getWebPreference('nativeWindowOpen');
const rendererProcessReuseEnabled = mainFrame.getWebPreference('disableElectronSiteInstanceOverrides');
const preloadScript = mainFrame.getWebPreference('preload');
const preloadScripts = mainFrame.getWebPreference('preloadScripts');
const guestInstanceId = mainFrame.getWebPreference('guestInstanceId') || null;
@ -97,7 +96,7 @@ switch (window.location.protocol) {
default: {
// Override default web functions.
const { windowSetup } = require('@electron/internal/renderer/window-setup');
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen, rendererProcessReuseEnabled);
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen);
}
}

View file

@ -243,8 +243,7 @@ class BrowserWindowProxy {
}
export const windowSetup = (
guestInstanceId: number, openerId: number, isHiddenPage: boolean, usesNativeWindowOpen: boolean, rendererProcessReuseEnabled: boolean
) => {
guestInstanceId: number, openerId: number, isHiddenPage: boolean, usesNativeWindowOpen: boolean) => {
if (!process.sandboxed && guestInstanceId == null) {
// Override default window.close.
window.close = function () {
@ -319,30 +318,6 @@ export const windowSetup = (
});
}
if (!process.sandboxed && !rendererProcessReuseEnabled) {
window.history.back = function () {
ipcRendererInternal.send(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_BACK);
};
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['history', 'back'], window.history.back);
window.history.forward = function () {
ipcRendererInternal.send(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_FORWARD);
};
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['history', 'forward'], window.history.forward);
window.history.go = function (offset: number) {
ipcRendererInternal.send(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_TO_OFFSET, +offset);
};
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['history', 'go'], window.history.go);
const getHistoryLength = () => ipcRendererInternal.sendSync(IPC_MESSAGES.NAVIGATION_CONTROLLER_LENGTH);
Object.defineProperty(window.history, 'length', {
get: getHistoryLength,
set () {}
});
if (contextIsolationEnabled) internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['history', 'length'], getHistoryLength);
}
if (guestInstanceId != null) {
// Webview `document.visibilityState` tracks window visibility (and ignores
// the actual <webview> element visibility) for backwards compatibility.

View file

@ -124,7 +124,6 @@ if (hasSwitch('unsafely-expose-electron-internals-for-testing')) {
const contextIsolation = mainFrame.getWebPreference('contextIsolation');
const webviewTag = mainFrame.getWebPreference('webviewTag');
const isHiddenPage = mainFrame.getWebPreference('hiddenPage');
const rendererProcessReuseEnabled = mainFrame.getWebPreference('disableElectronSiteInstanceOverrides');
const usesNativeWindowOpen = true;
const guestInstanceId = mainFrame.getWebPreference('guestInstanceId') || null;
const openerId = mainFrame.getWebPreference('openerId') || null;
@ -144,7 +143,7 @@ switch (window.location.protocol) {
default: {
// Override default web functions.
const { windowSetup } = require('@electron/internal/renderer/window-setup');
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen, rendererProcessReuseEnabled);
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen);
}
}

View file

@ -445,7 +445,6 @@ void ElectronBrowserClient::OverrideWebkitPrefs(
SessionPreferences::GetValidPreloads(web_contents->GetBrowserContext());
if (!preloads.empty())
prefs->preloads = preloads;
prefs->disable_electron_site_instance_overrides = true;
SetFontDefaults(prefs);

View file

@ -181,9 +181,6 @@ const char kWebGL[] = "webgl";
// navigation.
const char kNavigateOnDragDrop[] = "navigateOnDragDrop";
const char kDisableElectronSiteInstanceOverrides[] =
"disableElectronSiteInstanceOverrides";
const char kEnableNodeLeakageInRenderers[] = "enableNodeLeakageInRenderers";
const char kHiddenPage[] = "hiddenPage";
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)

View file

@ -90,8 +90,6 @@ extern const char kNavigateOnDragDrop[];
extern const char kEnableWebSQL[];
extern const char kEnablePreferredSizeMode[];
extern const char kDisableElectronSiteInstanceOverrides[];
extern const char kEnableNodeLeakageInRenderers[];
extern const char kHiddenPage[];
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)

View file

@ -491,9 +491,6 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
if (pref_name == options::kPreloadScripts) {
return gin::ConvertToV8(isolate, prefs.preloads);
} else if (pref_name == options::kDisableElectronSiteInstanceOverrides) {
return gin::ConvertToV8(isolate,
prefs.disable_electron_site_instance_overrides);
} else if (pref_name == options::kBackgroundColor) {
return gin::ConvertToV8(isolate, prefs.background_color);
} else if (pref_name == options::kOpenerID) {
@ -517,9 +514,6 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
return gin::ConvertToV8(isolate, prefs.node_integration);
} else if (pref_name == options::kNodeIntegrationInWorker) {
return gin::ConvertToV8(isolate, prefs.node_integration_in_worker);
} else if (pref_name == options::kEnableNodeLeakageInRenderers) {
// NOTE: enableNodeLeakageInRenderers is internal-only.
return gin::ConvertToV8(isolate, prefs.node_leakage_in_renderers);
} else if (pref_name == options::kNodeIntegrationInSubFrames) {
return gin::ConvertToV8(isolate, true);
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)

View file

@ -73,15 +73,10 @@ void ElectronRenderFrameObserver::DidInstallConditionalFeatures(
// DidCreateScriptContext();
bool is_main_world = IsMainWorld(world_id);
bool is_main_frame = render_frame_->IsMainFrame();
bool reuse_renderer_processes_enabled =
prefs.disable_electron_site_instance_overrides;
bool is_not_opened = !render_frame_->GetWebFrame()->Opener() ||
prefs.node_leakage_in_renderers;
bool allow_node_in_sub_frames = prefs.node_integration_in_sub_frames;
bool should_create_isolated_context =
use_context_isolation && is_main_world &&
(is_main_frame || allow_node_in_sub_frames) &&
(is_not_opened || reuse_renderer_processes_enabled);
(is_main_frame || allow_node_in_sub_frames);
if (should_create_isolated_context) {
CreateIsolatedWorldContext();

View file

@ -95,18 +95,7 @@ void ElectronRendererClient::DidCreateScriptContext(
// Only load node if we are a main frame or a devtools extension
// unless node support has been explicitly enabled for sub frames
auto prefs = render_frame->GetBlinkPreferences();
bool reuse_renderer_processes_enabled =
prefs.disable_electron_site_instance_overrides;
// Consider the window not "opened" if it does not have an Opener, or if a
// user has manually opted in to leaking node in the renderer
bool is_not_opened =
!render_frame->GetWebFrame()->Opener() || prefs.node_leakage_in_renderers;
// Consider this the main frame if it is both a Main Frame and it wasn't
// opened. We allow an opened main frame to have node if renderer process
// reuse is enabled as that will correctly free node environments prevent a
// leak in child windows.
bool is_main_frame = render_frame->IsMainFrame() &&
(is_not_opened || reuse_renderer_processes_enabled);
bool is_main_frame = render_frame->IsMainFrame();
bool is_devtools = IsDevToolsExtension(render_frame);
bool allow_node_in_subframes = prefs.node_integration_in_sub_frames;
bool should_load_node =
@ -121,7 +110,7 @@ void ElectronRendererClient::DidCreateScriptContext(
node_integration_initialized_ = true;
node_bindings_->Initialize();
node_bindings_->PrepareMessageLoop();
} else if (reuse_renderer_processes_enabled) {
} else {
node_bindings_->PrepareMessageLoop();
}
@ -138,9 +127,7 @@ void ElectronRendererClient::DidCreateScriptContext(
// If we have disabled the site instance overrides we should prevent loading
// any non-context aware native module
if (reuse_renderer_processes_enabled)
env->set_force_context_aware(true);
env->set_warn_context_aware(true);
env->set_force_context_aware(true);
environments_.insert(env);
@ -183,12 +170,9 @@ void ElectronRendererClient::WillReleaseScriptContext(
// We also do this if we have disable electron site instance overrides to
// avoid memory leaks
auto prefs = render_frame->GetBlinkPreferences();
if (prefs.node_integration_in_sub_frames ||
prefs.disable_electron_site_instance_overrides) {
node::FreeEnvironment(env);
if (env == node_bindings_->uv_env())
node::FreeIsolateData(node_bindings_->isolate_data());
}
node::FreeEnvironment(env);
if (env == node_bindings_->uv_env())
node::FreeIsolateData(node_bindings_->isolate_data());
// ElectronBindings is tracking node environments.
electron_bindings_->EnvironmentDestroyed(env);

View file

@ -103,7 +103,6 @@ declare namespace NodeJS {
interface InternalWebPreferences {
contextIsolation: boolean;
disableElectronSiteInstanceOverrides: boolean;
guestInstanceId: number;
hiddenPage: boolean;
nativeWindowOpen: boolean;