feat: add more info in setWindowOpenHandler details (#28518)

* fix: invoke the window open handler for _blank links

* feat: add disposition to setWindowOpenHandler details

* fix: pass postData to new-window event

* postData can be heterogeneous

* fix type of postBody

* fix type of UploadFile and UploadRawData to be discriminated unions

* exclude the empty string from additionalFeatures

* add a test

* add postBody and referrer to setWindowOpenHandler args

* appease typescript

* Update api-browser-window-spec.ts

* update snapshots
This commit is contained in:
Jeremy Rose 2021-04-13 12:35:27 -07:00 committed by GitHub
parent b6315612dd
commit dba4df9326
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 152 additions and 65 deletions

View file

@ -3,7 +3,7 @@ import type { BrowserWindowConstructorOptions, LoadURLOptions } from 'electron/m
import * as url from 'url';
import * as path from 'path';
import { openGuestWindow, makeWebPreferences } from '@electron/internal/browser/guest-window-manager';
import { openGuestWindow, makeWebPreferences, parseContentTypeFormat } from '@electron/internal/browser/guest-window-manager';
import { NavigationController } from '@electron/internal/browser/navigation-controller';
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
@ -411,11 +411,11 @@ WebContents.prototype.setWindowOpenHandler = function (handler: (details: Electr
this._windowOpenHandler = handler;
};
WebContents.prototype._callWindowOpenHandler = function (event: Electron.Event, url: string, frameName: string, rawFeatures: string): BrowserWindowConstructorOptions | null {
WebContents.prototype._callWindowOpenHandler = function (event: Electron.Event, details: Electron.HandlerDetails): BrowserWindowConstructorOptions | null {
if (!this._windowOpenHandler) {
return null;
}
const response = this._windowOpenHandler({ url, frameName, features: rawFeatures });
const response = this._windowOpenHandler(details);
if (typeof response !== 'object') {
event.preventDefault();
@ -567,9 +567,21 @@ WebContents.prototype._init = function () {
if (this.getType() !== 'remote') {
// Make new windows requested by links behave like "window.open".
this.on('-new-window' as any, (event: ElectronInternal.Event, url: string, frameName: string, disposition: string,
this.on('-new-window' as any, (event: ElectronInternal.Event, url: string, frameName: string, disposition: Electron.HandlerDetails['disposition'],
rawFeatures: string, referrer: Electron.Referrer, postData: PostData) => {
const options = this._callWindowOpenHandler(event, url, frameName, rawFeatures);
const postBody = postData ? {
data: postData,
...parseContentTypeFormat(postData)
} : undefined;
const details: Electron.HandlerDetails = {
url,
frameName,
features: rawFeatures,
referrer,
postBody,
disposition
};
const options = this._callWindowOpenHandler(event, details);
if (!event.defaultPrevented) {
openGuestWindow({
event,
@ -578,18 +590,26 @@ WebContents.prototype._init = function () {
referrer,
postData,
overrideBrowserWindowOptions: options || {},
windowOpenArgs: {
url,
frameName,
features: rawFeatures
}
windowOpenArgs: details
});
}
});
let windowOpenOverriddenOptions: BrowserWindowConstructorOptions | null = null;
this.on('-will-add-new-contents' as any, (event: ElectronInternal.Event, url: string, frameName: string, rawFeatures: string) => {
windowOpenOverriddenOptions = this._callWindowOpenHandler(event, url, frameName, rawFeatures);
this.on('-will-add-new-contents' as any, (event: ElectronInternal.Event, url: string, frameName: string, rawFeatures: string, disposition: Electron.HandlerDetails['disposition'], referrer: Electron.Referrer, postData: PostData) => {
const postBody = postData ? {
data: postData,
...parseContentTypeFormat(postData)
} : undefined;
const details: Electron.HandlerDetails = {
url,
frameName,
features: rawFeatures,
disposition,
referrer,
postBody
};
windowOpenOverriddenOptions = this._callWindowOpenHandler(event, details);
if (!event.defaultPrevented) {
const secureOverrideWebPreferences = windowOpenOverriddenOptions ? {
// Allow setting of backgroundColor as a webPreference even though