2020-03-20 20:28:31 +00:00
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
|
|
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
2020-04-22 19:42:51 +00:00
|
|
|
import { internalContextBridge } from '@electron/internal/renderer/api/context-bridge';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-04-22 19:42:51 +00:00
|
|
|
const { contextIsolationEnabled } = internalContextBridge;
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
// This file implements the following APIs over the ctx bridge:
|
2019-02-14 13:52:38 +00:00
|
|
|
// - window.open()
|
|
|
|
// - window.opener.blur()
|
|
|
|
// - window.opener.close()
|
|
|
|
// - window.opener.eval()
|
|
|
|
// - window.opener.focus()
|
|
|
|
// - window.opener.location
|
|
|
|
// - window.opener.print()
|
2020-04-27 19:46:04 +00:00
|
|
|
// - window.opener.closed
|
2019-02-14 13:52:38 +00:00
|
|
|
// - window.opener.postMessage()
|
2020-04-22 19:42:51 +00:00
|
|
|
// - window.history.back()
|
|
|
|
// - window.history.forward()
|
|
|
|
// - window.history.go()
|
|
|
|
// - window.history.length
|
2019-02-14 13:52:38 +00:00
|
|
|
// - window.prompt()
|
|
|
|
// - document.hidden
|
|
|
|
// - document.visibilityState
|
|
|
|
|
|
|
|
// Helper function to resolve relative url.
|
2020-03-20 20:28:31 +00:00
|
|
|
const resolveURL = (url: string, base: string) => new URL(url, base).href;
|
2019-02-14 13:52:38 +00:00
|
|
|
|
|
|
|
// Use this method to ensure values expected as strings in the main process
|
|
|
|
// are convertible to strings in the renderer process. This ensures exceptions
|
|
|
|
// converting values to strings are thrown in this process.
|
|
|
|
const toString = (value: any) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
return value != null ? `${value}` : value;
|
|
|
|
};
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const windowProxies = new Map<number, BrowserWindowProxy>();
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
const getOrCreateProxy = (guestId: number): SafelyBoundBrowserWindowProxy => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let proxy = windowProxies.get(guestId);
|
2019-02-14 13:52:38 +00:00
|
|
|
if (proxy == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
proxy = new BrowserWindowProxy(guestId);
|
|
|
|
windowProxies.set(guestId, proxy);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2020-04-27 19:46:04 +00:00
|
|
|
return proxy.getSafe();
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-02-14 13:52:38 +00:00
|
|
|
|
|
|
|
const removeProxy = (guestId: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
windowProxies.delete(guestId);
|
|
|
|
};
|
2019-02-14 13:52:38 +00:00
|
|
|
|
|
|
|
type LocationProperties = 'hash' | 'href' | 'host' | 'hostname' | 'origin' | 'pathname' | 'port' | 'protocol' | 'search'
|
|
|
|
|
|
|
|
class LocationProxy {
|
|
|
|
@LocationProxy.ProxyProperty public hash!: string;
|
|
|
|
@LocationProxy.ProxyProperty public href!: string;
|
|
|
|
@LocationProxy.ProxyProperty public host!: string;
|
|
|
|
@LocationProxy.ProxyProperty public hostname!: string;
|
|
|
|
@LocationProxy.ProxyProperty public origin!: string;
|
|
|
|
@LocationProxy.ProxyProperty public pathname!: string;
|
|
|
|
@LocationProxy.ProxyProperty public port!: string;
|
|
|
|
@LocationProxy.ProxyProperty public protocol!: string;
|
|
|
|
@LocationProxy.ProxyProperty public search!: URLSearchParams;
|
|
|
|
|
|
|
|
private guestId: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Beware: This decorator will have the _prototype_ as the `target`. It defines properties
|
|
|
|
* commonly found in URL on the LocationProxy.
|
|
|
|
*/
|
|
|
|
private static ProxyProperty<T> (target: LocationProxy, propertyKey: LocationProperties) {
|
|
|
|
Object.defineProperty(target, propertyKey, {
|
2020-04-27 19:46:04 +00:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2019-07-29 18:20:19 +00:00
|
|
|
get: function (this: LocationProxy): T | string {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestURL = this.getGuestURL();
|
|
|
|
const value = guestURL ? guestURL[propertyKey] : '';
|
|
|
|
return value === undefined ? '' : value;
|
2019-02-14 13:52:38 +00:00
|
|
|
},
|
2019-07-29 18:20:19 +00:00
|
|
|
set: function (this: LocationProxy, newVal: T) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const guestURL = this.getGuestURL();
|
2019-02-14 13:52:38 +00:00
|
|
|
if (guestURL) {
|
|
|
|
// TypeScript doesn't want us to assign to read-only variables.
|
2020-10-13 17:25:21 +00:00
|
|
|
// It's right, that's bad, but we're doing it anyway.
|
2020-03-20 20:28:31 +00:00
|
|
|
(guestURL as any)[propertyKey] = newVal;
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._invokeWebContentsMethod('loadURL', guestURL.toString());
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public getSafe = () => {
|
|
|
|
const that = this;
|
|
|
|
return {
|
|
|
|
get href () { return that.href; },
|
|
|
|
set href (newValue) { that.href = newValue; },
|
|
|
|
get hash () { return that.hash; },
|
|
|
|
set hash (newValue) { that.hash = newValue; },
|
|
|
|
get host () { return that.host; },
|
|
|
|
set host (newValue) { that.host = newValue; },
|
|
|
|
get hostname () { return that.hostname; },
|
|
|
|
set hostname (newValue) { that.hostname = newValue; },
|
|
|
|
get origin () { return that.origin; },
|
|
|
|
set origin (newValue) { that.origin = newValue; },
|
|
|
|
get pathname () { return that.pathname; },
|
|
|
|
set pathname (newValue) { that.pathname = newValue; },
|
|
|
|
get port () { return that.port; },
|
|
|
|
set port (newValue) { that.port = newValue; },
|
|
|
|
get protocol () { return that.protocol; },
|
|
|
|
set protocol (newValue) { that.protocol = newValue; },
|
|
|
|
get search () { return that.search; },
|
|
|
|
set search (newValue) { that.search = newValue; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-30 01:10:21 +00:00
|
|
|
constructor (guestId: number) {
|
2019-02-14 13:52:38 +00:00
|
|
|
// eslint will consider the constructor "useless"
|
|
|
|
// unless we assign them in the body. It's fine, that's what
|
|
|
|
// TS would do anyway.
|
2020-03-20 20:28:31 +00:00
|
|
|
this.guestId = guestId;
|
|
|
|
this.getGuestURL = this.getGuestURL.bind(this);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public toString (): string {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this.href;
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getGuestURL (): URL | null {
|
2020-05-27 20:50:54 +00:00
|
|
|
const maybeURL = this._invokeWebContentsMethodSync('getURL') as string;
|
|
|
|
|
2020-10-13 17:25:21 +00:00
|
|
|
// When there's no previous frame the url will be blank, so account for that here
|
2020-05-27 20:50:54 +00:00
|
|
|
// to prevent url parsing errors on an empty string.
|
|
|
|
const urlString = maybeURL !== '' ? maybeURL : 'about:blank';
|
2019-02-14 13:52:38 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
return new URL(urlString);
|
2019-02-14 13:52:38 +00:00
|
|
|
} catch (e) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('LocationProxy: failed to parse string', urlString, e);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return null;
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2019-05-18 18:52:29 +00:00
|
|
|
|
2019-07-29 18:20:19 +00:00
|
|
|
private _invokeWebContentsMethod (method: string, ...args: any[]) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD, this.guestId, method, ...args);
|
2019-07-29 18:20:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 18:52:29 +00:00
|
|
|
private _invokeWebContentsMethodSync (method: string, ...args: any[]) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD, this.guestId, method, ...args);
|
2019-05-18 18:52:29 +00:00
|
|
|
}
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
interface SafelyBoundBrowserWindowProxy {
|
|
|
|
location: WindowProxy['location'];
|
|
|
|
blur: WindowProxy['blur'];
|
|
|
|
close: WindowProxy['close'];
|
|
|
|
eval: typeof eval; // eslint-disable-line no-eval
|
|
|
|
focus: WindowProxy['focus'];
|
|
|
|
print: WindowProxy['print'];
|
|
|
|
postMessage: WindowProxy['postMessage'];
|
|
|
|
closed: boolean;
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:52:38 +00:00
|
|
|
class BrowserWindowProxy {
|
|
|
|
public closed: boolean = false
|
|
|
|
|
|
|
|
private _location: LocationProxy
|
|
|
|
private guestId: number
|
|
|
|
|
|
|
|
// TypeScript doesn't allow getters/accessors with different types,
|
|
|
|
// so for now, we'll have to make do with an "any" in the mix.
|
|
|
|
// https://github.com/Microsoft/TypeScript/issues/2521
|
|
|
|
public get location (): LocationProxy | any {
|
2020-04-27 19:46:04 +00:00
|
|
|
return this._location.getSafe();
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2019-02-14 13:52:38 +00:00
|
|
|
public set location (url: string | any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
url = resolveURL(url, this.location.href);
|
|
|
|
this._invokeWebContentsMethod('loadURL', url);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 01:10:21 +00:00
|
|
|
constructor (guestId: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.guestId = guestId;
|
|
|
|
this._location = new LocationProxy(guestId);
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2021-02-10 01:12:26 +00:00
|
|
|
ipcRendererInternal.once(`${IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_CLOSED}_${guestId}`, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
removeProxy(guestId);
|
|
|
|
this.closed = true;
|
|
|
|
});
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public getSafe = (): SafelyBoundBrowserWindowProxy => {
|
|
|
|
const that = this;
|
|
|
|
return {
|
|
|
|
postMessage: this.postMessage,
|
|
|
|
blur: this.blur,
|
|
|
|
close: this.close,
|
|
|
|
focus: this.focus,
|
|
|
|
print: this.print,
|
|
|
|
eval: this.eval,
|
|
|
|
get location () {
|
|
|
|
return that.location;
|
|
|
|
},
|
|
|
|
set location (url: string | any) {
|
|
|
|
that.location = url;
|
|
|
|
},
|
|
|
|
get closed () {
|
|
|
|
return that.closed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public close = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._invokeWindowMethod('destroy');
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public focus = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._invokeWindowMethod('focus');
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public blur = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._invokeWindowMethod('blur');
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public print = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._invokeWebContentsMethod('print');
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public postMessage = (message: any, targetOrigin: string) => {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE, this.guestId, message, toString(targetOrigin), window.location.origin);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
public eval = (code: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._invokeWebContentsMethod('executeJavaScript', code);
|
2019-05-18 18:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _invokeWindowMethod (method: string, ...args: any[]) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_METHOD, this.guestId, method, ...args);
|
2019-05-18 18:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _invokeWebContentsMethod (method: string, ...args: any[]) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererInternal.invoke(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD, this.guestId, method, ...args);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const windowSetup = (
|
2020-02-24 20:14:11 +00:00
|
|
|
guestInstanceId: number, openerId: number, isHiddenPage: boolean, usesNativeWindowOpen: boolean, rendererProcessReuseEnabled: boolean
|
2019-02-14 13:52:38 +00:00
|
|
|
) => {
|
2020-04-27 19:46:04 +00:00
|
|
|
if (!process.sandboxed && guestInstanceId == null) {
|
2019-02-14 13:52:38 +00:00
|
|
|
// Override default window.close.
|
|
|
|
window.close = function () {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.BROWSER_WINDOW_CLOSE);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-04-27 19:46:04 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['close'], window.close);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!usesNativeWindowOpen) {
|
2020-04-22 19:42:51 +00:00
|
|
|
// TODO(MarshallOfSound): Make compatible with ctx isolation without hole-punch
|
2019-02-14 13:52:38 +00:00
|
|
|
// Make the browser window or guest view emit "new-window" event.
|
2020-10-08 01:01:23 +00:00
|
|
|
window.open = function (url?: string, frameName?: string, features?: string) {
|
2019-02-14 13:52:38 +00:00
|
|
|
if (url != null && url !== '') {
|
2020-03-20 20:28:31 +00:00
|
|
|
url = resolveURL(url, location.href);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2020-10-13 21:11:06 +00:00
|
|
|
const guestId = ipcRendererInternal.sendSync(IPC_MESSAGES.GUEST_WINDOW_MANAGER_WINDOW_OPEN, url, toString(frameName), toString(features));
|
2019-02-14 13:52:38 +00:00
|
|
|
if (guestId != null) {
|
2020-10-08 01:01:23 +00:00
|
|
|
return getOrCreateProxy(guestId) as any as WindowProxy;
|
2019-02-14 13:52:38 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
return null;
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-04-27 19:46:04 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['open'], window.open);
|
2020-01-06 21:23:03 +00:00
|
|
|
}
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-01-06 21:23:03 +00:00
|
|
|
if (openerId != null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
window.opener = getOrCreateProxy(openerId);
|
2020-04-27 19:46:04 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueWithDynamicPropsFromIsolatedWorld(['opener'], window.opener);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// But we do not support prompt().
|
2020-04-27 19:46:04 +00:00
|
|
|
window.prompt = function () {
|
|
|
|
throw new Error('prompt() is and will not be supported.');
|
|
|
|
};
|
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['prompt'], window.prompt);
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-01-06 21:23:03 +00:00
|
|
|
if (!usesNativeWindowOpen || openerId != null) {
|
2021-02-10 01:12:26 +00:00
|
|
|
ipcRendererInternal.on(IPC_MESSAGES.GUEST_WINDOW_POSTMESSAGE, function (
|
2020-01-06 21:23:03 +00:00
|
|
|
_event, sourceId: number, message: any, sourceOrigin: string
|
|
|
|
) {
|
|
|
|
// Manually dispatch event instead of using postMessage because we also need to
|
|
|
|
// set event.source.
|
|
|
|
//
|
|
|
|
// Why any? We can't construct a MessageEvent and we can't
|
|
|
|
// use `as MessageEvent` because you're not supposed to override
|
|
|
|
// data, origin, and source
|
2020-03-20 20:28:31 +00:00
|
|
|
const event: any = document.createEvent('Event');
|
|
|
|
event.initEvent('message', false, false);
|
2020-01-06 21:23:03 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
event.data = message;
|
|
|
|
event.origin = sourceOrigin;
|
|
|
|
event.source = getOrCreateProxy(sourceId);
|
2020-01-06 21:23:03 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
window.dispatchEvent(event as MessageEvent);
|
|
|
|
});
|
2020-01-06 21:23:03 +00:00
|
|
|
}
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
if (!process.sandboxed && !rendererProcessReuseEnabled) {
|
2020-01-06 21:23:03 +00:00
|
|
|
window.history.back = function () {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_BACK);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-04-27 19:46:04 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['history', 'back'], window.history.back);
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-01-06 21:23:03 +00:00
|
|
|
window.history.forward = function () {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_FORWARD);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-04-27 19:46:04 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['history', 'forward'], window.history.forward);
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-01-06 21:23:03 +00:00
|
|
|
window.history.go = function (offset: number) {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.send(IPC_MESSAGES.NAVIGATION_CONTROLLER_GO_TO_OFFSET, +offset);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-04-27 19:46:04 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalValueFromIsolatedWorld(['history', 'go'], window.history.go);
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-10-13 21:11:06 +00:00
|
|
|
const getHistoryLength = () => ipcRendererInternal.sendSync(IPC_MESSAGES.NAVIGATION_CONTROLLER_LENGTH);
|
2020-01-06 21:23:03 +00:00
|
|
|
Object.defineProperty(window.history, 'length', {
|
2020-04-22 19:42:51 +00:00
|
|
|
get: getHistoryLength,
|
2020-01-06 21:23:03 +00:00
|
|
|
set () {}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-04-22 19:42:51 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['history', 'length'], getHistoryLength);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
if (guestInstanceId != null) {
|
2019-02-14 13:52:38 +00:00
|
|
|
// Webview `document.visibilityState` tracks window visibility (and ignores
|
|
|
|
// the actual <webview> element visibility) for backwards compatibility.
|
|
|
|
// See discussion in #9178.
|
|
|
|
//
|
|
|
|
// Note that this results in duplicate visibilitychange events (since
|
|
|
|
// Chromium also fires them) and potentially incorrect visibility change.
|
|
|
|
// We should reconsider this decision for Electron 2.0.
|
2020-03-20 20:28:31 +00:00
|
|
|
let cachedVisibilityState = isHiddenPage ? 'hidden' : 'visible';
|
2019-02-14 13:52:38 +00:00
|
|
|
|
|
|
|
// Subscribe to visibilityState changes.
|
2021-02-10 01:12:26 +00:00
|
|
|
ipcRendererInternal.on(IPC_MESSAGES.GUEST_INSTANCE_VISIBILITY_CHANGE, function (_event, visibilityState: VisibilityState) {
|
2019-02-14 13:52:38 +00:00
|
|
|
if (cachedVisibilityState !== visibilityState) {
|
2020-03-20 20:28:31 +00:00
|
|
|
cachedVisibilityState = visibilityState;
|
|
|
|
document.dispatchEvent(new Event('visibilitychange'));
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-02-14 13:52:38 +00:00
|
|
|
|
|
|
|
// Make document.hidden and document.visibilityState return the correct value.
|
2020-04-22 19:42:51 +00:00
|
|
|
const getDocumentHidden = () => cachedVisibilityState !== 'visible';
|
2019-07-08 23:43:49 +00:00
|
|
|
Object.defineProperty(document, 'hidden', {
|
2020-04-22 19:42:51 +00:00
|
|
|
get: getDocumentHidden
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-04-22 19:42:51 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['document', 'hidden'], getDocumentHidden);
|
2019-02-14 13:52:38 +00:00
|
|
|
|
2020-04-22 19:42:51 +00:00
|
|
|
const getDocumentVisibilityState = () => cachedVisibilityState;
|
2019-07-08 23:43:49 +00:00
|
|
|
Object.defineProperty(document, 'visibilityState', {
|
2020-04-22 19:42:51 +00:00
|
|
|
get: getDocumentVisibilityState
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-04-22 19:42:51 +00:00
|
|
|
if (contextIsolationEnabled) internalContextBridge.overrideGlobalPropertyFromIsolatedWorld(['document', 'visibilityState'], getDocumentVisibilityState);
|
2019-02-14 13:52:38 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|