2020-11-09 18:15:30 +01:00
|
|
|
import { BrowserWindow, BrowserWindowConstructorOptions, LoadURLOptions } from 'electron';
|
|
|
|
import { promisify } from 'util';
|
|
|
|
import { ISessionHelper } from '../session/i-session-helper';
|
|
|
|
import { AppWindow } from './app-window';
|
|
|
|
import { IUrlAppWindow } from './i-url-app-window';
|
|
|
|
import { WindowClosedError } from './window-closed-error';
|
|
|
|
|
|
|
|
export abstract class UrlAppWindow extends AppWindow implements IUrlAppWindow {
|
|
|
|
protected loadOptions: LoadURLOptions;
|
|
|
|
|
|
|
|
protected constructor(
|
|
|
|
sessionHelper: ISessionHelper,
|
|
|
|
uri: string,
|
|
|
|
options: BrowserWindowConstructorOptions = {},
|
|
|
|
loadOptions: LoadURLOptions = {}
|
|
|
|
) {
|
|
|
|
const securedOptions: BrowserWindowConstructorOptions = {
|
|
|
|
...options,
|
|
|
|
...{
|
|
|
|
webPreferences: {
|
|
|
|
enableRemoteModule: false,
|
|
|
|
nodeIntegration: false,
|
|
|
|
contextIsolation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
super(sessionHelper, uri, securedOptions);
|
|
|
|
this.loadOptions = loadOptions;
|
|
|
|
}
|
|
|
|
|
2021-01-04 23:35:01 +01:00
|
|
|
public downloadUrlSafe(url: string, savePath: string, options?: LoadURLOptions): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
if (!this._window) {
|
|
|
|
throw new WindowClosedError();
|
|
|
|
}
|
|
|
|
this._window.webContents.session.once('will-download', (event, item) => {
|
|
|
|
item.setSavePath(savePath);
|
|
|
|
item.once('done', (doneEvent, state) => {
|
|
|
|
switch (state) {
|
|
|
|
case 'completed':
|
|
|
|
resolve();
|
|
|
|
break;
|
|
|
|
case 'cancelled':
|
|
|
|
case 'interrupted':
|
|
|
|
default:
|
|
|
|
reject(new Error(state));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
item.on('updated', () => {
|
|
|
|
item.resume();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
void this.loadUrlSafe(url, options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:15:30 +01:00
|
|
|
public async loadUrlSafe(url: string, options?: LoadURLOptions): Promise<void> {
|
|
|
|
if (!this._window) {
|
|
|
|
throw new WindowClosedError();
|
|
|
|
}
|
|
|
|
const waitInterval = 1000;
|
|
|
|
let failedLoad = true;
|
|
|
|
do {
|
2020-12-28 19:58:20 +01:00
|
|
|
await new Promise<void>((resolve) => {
|
2020-11-09 18:15:30 +01:00
|
|
|
if (!this._window) {
|
|
|
|
throw new WindowClosedError();
|
|
|
|
}
|
|
|
|
this._window.webContents.once('did-navigate', (event, navigationUrl, httpResponseCode) => {
|
|
|
|
failedLoad = HttpCode.BAD_REQUEST <= httpResponseCode;
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
void this._window.loadURL(url, options);
|
|
|
|
});
|
|
|
|
if (failedLoad) {
|
|
|
|
await promisify(setTimeout)(waitInterval);
|
|
|
|
}
|
|
|
|
} while (failedLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected load(window: BrowserWindow): Promise<void> {
|
|
|
|
return window.loadURL(this.uri, this.loadOptions);
|
|
|
|
}
|
|
|
|
}
|