electron/lib/renderer/web-view/web-view-impl.ts

278 lines
9.9 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import * as electron from 'electron';
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';
import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal';
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants';
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods';
import { deserialize } from '@electron/internal/common/type-utils';
const { webFrame } = electron;
const v8Util = process._linkedBinding('electron_common_v8_util');
2016-01-14 18:35:29 +00:00
// ID generator.
2020-03-20 20:28:31 +00:00
let nextId = 0;
2016-01-12 02:40:23 +00:00
2016-11-03 17:39:40 +00:00
const getNextId = function () {
2020-03-20 20:28:31 +00:00
return ++nextId;
};
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Represents the internal state of the WebView node.
export class WebViewImpl {
public beforeFirstNavigation = true
public elementAttached = false
public guestInstanceId?: number
public hasFocus = false
public internalInstanceId?: number;
public resizeObserver?: ResizeObserver;
public userAgentOverride?: string;
public viewInstanceId: number
// on* Event handlers.
public on: Record<string, any> = {}
public internalElement: HTMLIFrameElement
// Replaced in web-view-attributes
public attributes: Record<string, any> = {}
public setupWebViewAttributes (): void {}
constructor (public webviewNode: HTMLElement) {
// Create internal iframe element.
2020-03-20 20:28:31 +00:00
this.internalElement = this.createInternalElement();
const shadowRoot = this.webviewNode.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = '<!DOCTYPE html><style type="text/css">:host { display: flex; }</style>';
this.setupWebViewAttributes();
this.viewInstanceId = getNextId();
shadowRoot.appendChild(this.internalElement);
// Provide access to contentWindow.
Object.defineProperty(this.webviewNode, 'contentWindow', {
get: () => {
2020-03-20 20:28:31 +00:00
return this.internalElement.contentWindow;
},
enumerable: true
2020-03-20 20:28:31 +00:00
});
2016-01-12 02:40:23 +00:00
}
createInternalElement () {
2020-03-20 20:28:31 +00:00
const iframeElement = document.createElement('iframe');
iframeElement.style.flex = '1 1 auto';
iframeElement.style.width = '100%';
iframeElement.style.border = '0';
v8Util.setHiddenValue(iframeElement, 'internal', this);
return iframeElement;
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Resets some state upon reattaching <webview> element to the DOM.
2016-11-03 17:55:12 +00:00
reset () {
2016-01-14 19:10:12 +00:00
// If guestInstanceId is defined then the <webview> has navigated and has
// already picked up a partition ID. Thus, we need to reset the initialization
// state. However, it may be the case that beforeFirstNavigation is false BUT
// guestInstanceId has yet to be initialized. This means that we have not
// heard back from createGuest yet. We will not reset the flag in this case so
// that we don't end up allocating a second guest.
2016-01-12 02:40:23 +00:00
if (this.guestInstanceId) {
2020-03-20 20:28:31 +00:00
this.guestInstanceId = undefined;
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
this.beforeFirstNavigation = true;
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION].validPartitionId = true;
// Since attachment swaps a local frame for a remote frame, we need our
// internal iframe element to be local again before we can reattach.
2020-03-20 20:28:31 +00:00
const newFrame = this.createInternalElement();
const oldFrame = this.internalElement;
this.internalElement = newFrame;
2016-01-12 02:40:23 +00:00
if (oldFrame && oldFrame.parentNode) {
2020-03-20 20:28:31 +00:00
oldFrame.parentNode.replaceChild(newFrame, oldFrame);
}
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 19:10:12 +00:00
// This observer monitors mutations to attributes of the <webview> and
// updates the BrowserPlugin properties accordingly. In turn, updating
// a BrowserPlugin property will update the corresponding BrowserPlugin
// attribute, if necessary. See BrowserPlugin::UpdateDOMAttribute for more
// details.
handleWebviewAttributeMutation (attributeName: string, oldValue: any, newValue: any) {
2016-01-12 02:40:23 +00:00
if (!this.attributes[attributeName] || this.attributes[attributeName].ignoreMutation) {
2020-03-20 20:28:31 +00:00
return;
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
// Let the changed attribute handle its own mutation
2020-03-20 20:28:31 +00:00
this.attributes[attributeName].handleMutation(oldValue, newValue);
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
onElementResize () {
2020-03-20 20:28:31 +00:00
const resizeEvent = new Event('resize') as ElectronInternal.WebFrameResizeEvent;
resizeEvent.newWidth = this.webviewNode.clientWidth;
resizeEvent.newHeight = this.webviewNode.clientHeight;
this.dispatchEvent(resizeEvent);
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-11-03 17:55:12 +00:00
createGuest () {
guestViewInternal.createGuest(this.buildParams()).then(guestInstanceId => {
2020-03-20 20:28:31 +00:00
this.attachGuestInstance(guestInstanceId);
});
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
dispatchEvent (webViewEvent: Electron.Event) {
2020-03-20 20:28:31 +00:00
this.webviewNode.dispatchEvent(webViewEvent);
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Adds an 'on<event>' property on the webview, which can be used to set/unset
// an event handler.
setupEventProperty (eventName: string) {
2020-03-20 20:28:31 +00:00
const propertyName = `on${eventName.toLowerCase()}`;
2016-01-12 02:40:23 +00:00
return Object.defineProperty(this.webviewNode, propertyName, {
get: () => {
2020-03-20 20:28:31 +00:00
return this.on[propertyName];
},
set: (value) => {
if (this.on[propertyName]) {
2020-03-20 20:28:31 +00:00
this.webviewNode.removeEventListener(eventName, this.on[propertyName]);
}
2020-03-20 20:28:31 +00:00
this.on[propertyName] = value;
if (value) {
2020-03-20 20:28:31 +00:00
return this.webviewNode.addEventListener(eventName, value);
}
},
2016-01-12 02:40:23 +00:00
enumerable: true
2020-03-20 20:28:31 +00:00
});
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Updates state upon loadcommit.
onLoadCommit (webViewEvent: ElectronInternal.WebViewEvent) {
2020-03-20 20:28:31 +00:00
const oldValue = this.webviewNode.getAttribute(WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC);
const newValue = webViewEvent.url;
2016-01-12 02:40:23 +00:00
if (webViewEvent.isMainFrame && (oldValue !== newValue)) {
2016-01-14 19:10:12 +00:00
// Touching the src attribute triggers a navigation. To avoid
// triggering a page reload on every guest-initiated navigation,
// we do not handle this mutation.
2020-03-20 20:28:31 +00:00
this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_SRC].setValueIgnoreMutation(newValue);
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
// Emits focus/blur events.
onFocusChange () {
2020-03-20 20:28:31 +00:00
const hasFocus = document.activeElement === this.webviewNode;
if (hasFocus !== this.hasFocus) {
2020-03-20 20:28:31 +00:00
this.hasFocus = hasFocus;
this.dispatchEvent(new Event(hasFocus ? 'focus' : 'blur'));
}
}
onAttach (storagePartitionId: number) {
2020-03-20 20:28:31 +00:00
return this.attributes[WEB_VIEW_CONSTANTS.ATTRIBUTE_PARTITION].setValue(storagePartitionId);
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-11-03 17:55:12 +00:00
buildParams () {
const params: Record<string, any> = {
2016-01-12 02:40:23 +00:00
instanceId: this.viewInstanceId,
userAgentOverride: this.userAgentOverride
2020-03-20 20:28:31 +00:00
};
2016-11-03 17:39:40 +00:00
for (const attributeName in this.attributes) {
if (Object.prototype.hasOwnProperty.call(this.attributes, attributeName)) {
2020-03-20 20:28:31 +00:00
params[attributeName] = this.attributes[attributeName].getValue();
2016-11-03 17:39:40 +00:00
}
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
return params;
2016-03-25 19:57:17 +00:00
}
attachGuestInstance (guestInstanceId: number) {
if (!this.elementAttached) {
// The element could be detached before we got response from browser.
2020-03-20 20:28:31 +00:00
return;
}
2020-03-20 20:28:31 +00:00
this.internalInstanceId = getNextId();
this.guestInstanceId = guestInstanceId;
guestViewInternal.attachGuest(
this.internalInstanceId,
this.guestInstanceId,
this.buildParams(),
this.internalElement.contentWindow!
2020-03-20 20:28:31 +00:00
);
// TODO(zcbenz): Should we deprecate the "resize" event? Wait, it is not
// even documented.
2020-03-20 20:28:31 +00:00
this.resizeObserver = new ResizeObserver(this.onElementResize.bind(this));
this.resizeObserver.observe(this.internalElement);
2016-03-25 19:57:17 +00:00
}
}
2016-01-12 02:40:23 +00:00
export const setupAttributes = () => {
2020-03-20 20:28:31 +00:00
require('@electron/internal/renderer/web-view/web-view-attributes');
};
// I wish eslint wasn't so stupid, but it is
// eslint-disable-next-line
export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElement) => {
WebViewElement.prototype.getWebContentsId = function () {
2020-03-20 20:28:31 +00:00
const internal = v8Util.getHiddenValue<WebViewImpl>(this, 'internal');
if (!internal.guestInstanceId) {
2020-03-20 20:28:31 +00:00
throw new Error('The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.');
}
2020-03-20 20:28:31 +00:00
return internal.guestInstanceId;
};
// Focusing the webview should move page focus to the underlying iframe.
WebViewElement.prototype.focus = function () {
2020-03-20 20:28:31 +00:00
this.contentWindow.focus();
};
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Forward proto.foo* method calls to WebViewImpl.foo*.
const createBlockHandler = function (method: string) {
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
2020-03-20 20:28:31 +00:00
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args);
};
};
for (const method of syncMethods) {
2020-03-20 20:28:31 +00:00
(WebViewElement.prototype as Record<string, any>)[method] = createBlockHandler(method);
2016-03-25 19:57:17 +00:00
}
2016-11-03 18:09:53 +00:00
const createNonBlockHandler = function (method: string) {
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
2020-03-20 20:28:31 +00:00
return ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args);
};
};
for (const method of asyncMethods) {
2020-03-20 20:28:31 +00:00
(WebViewElement.prototype as Record<string, any>)[method] = createNonBlockHandler(method);
}
WebViewElement.prototype.capturePage = async function (...args) {
2020-03-20 20:28:31 +00:00
return deserialize(await ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', this.getWebContentsId(), args));
};
const createPropertyGetter = function (property: string) {
return function (this: ElectronInternal.WebViewElement) {
2020-03-20 20:28:31 +00:00
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', this.getWebContentsId(), property);
};
};
const createPropertySetter = function (property: string) {
return function (this: ElectronInternal.WebViewElement, arg: any) {
2020-03-20 20:28:31 +00:00
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', this.getWebContentsId(), property, arg);
};
};
for (const property of properties) {
Object.defineProperty(WebViewElement.prototype, property, {
get: createPropertyGetter(property),
set: createPropertySetter(property)
2020-03-20 20:28:31 +00:00
});
}
2020-03-20 20:28:31 +00:00
};
2016-01-12 02:40:23 +00:00
export const webViewImplModule = {
setupAttributes,
setupMethods,
guestViewInternal,
webFrame,
WebViewImpl
2020-03-20 20:28:31 +00:00
};