From a545cd37902d490297310b4c04a6bd2094999a10 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 27 Jul 2021 07:48:12 +0200 Subject: [PATCH] fix: type internal APIs that can return null properly (#29852) --- lib/browser/devtools.ts | 4 ++-- lib/browser/guest-view-manager.ts | 2 +- lib/browser/guest-window-manager.ts | 4 ++-- lib/browser/guest-window-proxy.ts | 6 +++--- spec-main/ambient.d.ts | 4 ++-- typings/internal-electron.d.ts | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/browser/devtools.ts b/lib/browser/devtools.ts index fbfc965b483..89422f80a2d 100644 --- a/lib/browser/devtools.ts +++ b/lib/browser/devtools.ts @@ -67,7 +67,7 @@ ipcMainInternal.handle(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, function (event, ite const template = isEditMenu ? getEditMenuItems() : convertToMenuTemplate(items, resolve); const menu = Menu.buildFromTemplate(template); - const window = event.sender.getOwnerBrowserWindow(); + const window = event.sender.getOwnerBrowserWindow()!; menu.popup({ window, callback: () => resolve() }); }); @@ -94,7 +94,7 @@ ipcMainUtils.handleSync(IPC_MESSAGES.INSPECTOR_CONFIRM, async function (event, m buttons: ['OK', 'Cancel'], cancelId: 1 }; - const window = event.sender.getOwnerBrowserWindow(); + const window = event.sender.getOwnerBrowserWindow()!; const { response } = await dialog.showMessageBox(window, options); return response === 0; }); diff --git a/lib/browser/guest-view-manager.ts b/lib/browser/guest-view-manager.ts index 4b43fb7e25d..858490da61b 100644 --- a/lib/browser/guest-view-manager.ts +++ b/lib/browser/guest-view-manager.ts @@ -193,7 +193,7 @@ const attachGuest = function (event: Electron.IpcMainInvokeEvent, ]); // Inherit certain option values from embedder - const lastWebPreferences = embedder.getLastWebPreferences(); + const lastWebPreferences = embedder.getLastWebPreferences()!; for (const [name, value] of inheritedWebPreferences) { if (lastWebPreferences[name as keyof Electron.WebPreferences] === value) { (webPreferences as any)[name] = value; diff --git a/lib/browser/guest-window-manager.ts b/lib/browser/guest-window-manager.ts index 03fd2761992..ae5a0f95515 100644 --- a/lib/browser/guest-window-manager.ts +++ b/lib/browser/guest-window-manager.ts @@ -144,7 +144,7 @@ function emitDeprecatedNewWindowEvent ({ event, embedder, guest, windowOpenArgs, postData?: PostData, }): boolean { const { url, frameName } = windowOpenArgs; - const isWebViewWithPopupsDisabled = embedder.getType() === 'webview' && embedder.getLastWebPreferences().disablePopups; + const isWebViewWithPopupsDisabled = embedder.getType() === 'webview' && embedder.getLastWebPreferences()!.disablePopups; const postBody = postData ? { data: postData, ...parseContentTypeFormat(postData) @@ -229,7 +229,7 @@ export function makeWebPreferences ({ embedder, secureOverrideWebPreferences = { // have unvetted prefs, use parsedWebPreferences. secureOverrideWebPreferences?: BrowserWindowConstructorOptions['webPreferences'], }) { - const parentWebPreferences = embedder.getLastWebPreferences(); + const parentWebPreferences = embedder.getLastWebPreferences()!; const securityWebPreferencesFromParent = (Object.keys(securityWebPreferences).reduce((map, key) => { if (securityWebPreferences[key] === parentWebPreferences[key as keyof Electron.WebPreferences]) { (map as any)[key] = parentWebPreferences[key as keyof Electron.WebPreferences]; diff --git a/lib/browser/guest-window-proxy.ts b/lib/browser/guest-window-proxy.ts index 1d0fe47f8be..f2eacad53b4 100644 --- a/lib/browser/guest-window-proxy.ts +++ b/lib/browser/guest-window-proxy.ts @@ -28,7 +28,7 @@ const getGuestWindow = function (guestContents: WebContents) { }; const isChildWindow = function (sender: WebContents, target: WebContents) { - return target.getLastWebPreferences().openerId === sender.id; + return target.getLastWebPreferences()!.openerId === sender.id; }; const isRelatedWindow = function (sender: WebContents, target: WebContents) { @@ -43,7 +43,7 @@ const isScriptableWindow = function (sender: WebContents, target: WebContents) { }; const isNodeIntegrationEnabled = function (sender: WebContents) { - return sender.getLastWebPreferences().nodeIntegration === true; + return sender.getLastWebPreferences()!.nodeIntegration === true; }; // Checks whether |sender| can access the |target|: @@ -65,7 +65,7 @@ ipcMainInternal.on( features: string ) => { // This should only be allowed for senders that have nativeWindowOpen: false - const lastWebPreferences = event.sender.getLastWebPreferences(); + const lastWebPreferences = event.sender.getLastWebPreferences()!; if (lastWebPreferences.nativeWindowOpen || lastWebPreferences.sandbox) { event.returnValue = null; throw new Error( diff --git a/spec-main/ambient.d.ts b/spec-main/ambient.d.ts index eb6e1b1a556..36d9261b98b 100644 --- a/spec-main/ambient.d.ts +++ b/spec-main/ambient.d.ts @@ -2,8 +2,8 @@ declare let standardScheme: string; declare namespace Electron { interface WebContents { - getOwnerBrowserWindow(): BrowserWindow; - getWebPreferences(): any; + getOwnerBrowserWindow(): BrowserWindow | null; + getWebPreferences(): WebPreferences | null; } interface Session { diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index 8a5ff5619a6..efd12313e8d 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -56,8 +56,8 @@ declare namespace Electron { interface WebContents { _loadURL(url: string, options: ElectronInternal.LoadURLOptions): void; - getOwnerBrowserWindow(): Electron.BrowserWindow; - getLastWebPreferences(): Electron.WebPreferences; + getOwnerBrowserWindow(): Electron.BrowserWindow | null; + getLastWebPreferences(): Electron.WebPreferences | null; _getPreloadPaths(): string[]; equal(other: WebContents): boolean; browserWindowOptions: BrowserWindowConstructorOptions;