From 42862347218b61b01c4891036d84ecda0b6a86d6 Mon Sep 17 00:00:00 2001 From: Alice Zhao <66543449+alicelovescake@users.noreply.github.com> Date: Mon, 29 Jul 2024 04:00:51 -0700 Subject: [PATCH] fix: redirect webview navigation methods (#42981) --- lib/browser/guest-view-manager.ts | 10 +++++++++- lib/common/web-view-methods.ts | 22 +++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/browser/guest-view-manager.ts b/lib/browser/guest-view-manager.ts index 6b82328472fb..e2b1a39e619a 100644 --- a/lib/browser/guest-view-manager.ts +++ b/lib/browser/guest-view-manager.ts @@ -2,7 +2,7 @@ import { webContents } from 'electron/main'; import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'; import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils'; import { parseWebViewWebPreferences } from '@electron/internal/browser/parse-features-string'; -import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods'; +import { syncMethods, asyncMethods, properties, navigationHistorySyncMethods } from '@electron/internal/common/web-view-methods'; import { webViewEvents } from '@electron/internal/browser/web-view-events'; import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages'; @@ -311,6 +311,14 @@ handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, function (event, guestIn if (!syncMethods.has(method)) { throw new Error(`Invalid method: ${method}`); } + // Redirect history methods to updated navigationHistory property on webContents. See issue #42879. + if (navigationHistorySyncMethods.has(method)) { + let navigationMethod = method; + if (method === 'clearHistory') { + navigationMethod = 'clear'; + } + return (guest as any).navigationHistory[navigationMethod](...args); + } return (guest as any)[method](...args); }); diff --git a/lib/common/web-view-methods.ts b/lib/common/web-view-methods.ts index 81fc88f1f8e8..f0f4fb4bad87 100644 --- a/lib/common/web-view-methods.ts +++ b/lib/common/web-view-methods.ts @@ -1,3 +1,14 @@ +export const navigationHistorySyncMethods = new Set([ + 'canGoBack', + 'canGoForward', + 'canGoToOffset', + 'clearHistory', + 'goBack', + 'goForward', + 'goToIndex', + 'goToOffset' +]); + // Public-facing API methods. export const syncMethods = new Set([ 'getURL', @@ -8,14 +19,6 @@ export const syncMethods = new Set([ 'stop', 'reload', 'reloadIgnoringCache', - 'canGoBack', - 'canGoForward', - 'canGoToOffset', - 'clearHistory', - 'goBack', - 'goForward', - 'goToIndex', - 'goToOffset', 'isCrashed', 'setUserAgent', 'getUserAgent', @@ -51,7 +54,8 @@ export const syncMethods = new Set([ 'getZoomFactor', 'getZoomLevel', 'setZoomFactor', - 'setZoomLevel' + 'setZoomLevel', + ...navigationHistorySyncMethods ]); export const properties = new Set([