2021-05-15 07:42:07 +00:00
|
|
|
import type * as guestViewInternalModule from '@electron/internal/renderer/web-view/guest-view-internal';
|
2023-01-25 15:08:46 +00:00
|
|
|
import { WEB_VIEW_ATTRIBUTES } from '@electron/internal/renderer/web-view/web-view-constants';
|
2020-03-03 22:25:14 +00:00
|
|
|
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods';
|
2020-11-03 23:02:23 +00:00
|
|
|
import type { WebViewAttribute, PartitionAttribute } from '@electron/internal/renderer/web-view/web-view-attributes';
|
2021-05-07 07:07:47 +00:00
|
|
|
import { setupWebViewAttributes } from '@electron/internal/renderer/web-view/web-view-attributes';
|
2018-07-10 08:15:40 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// ID generator.
|
2016-11-03 17:39:40 +00:00
|
|
|
let nextId = 0;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-11-03 17:39:40 +00:00
|
|
|
const getNextId = function () {
|
2016-03-25 19:57:17 +00:00
|
|
|
return ++nextId;
|
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2021-05-15 07:42:07 +00:00
|
|
|
export interface WebViewImplHooks {
|
|
|
|
readonly guestViewInternal: typeof guestViewInternalModule;
|
|
|
|
readonly allowGuestViewElementDefinition: NodeJS.InternalWebFrame['allowGuestViewElementDefinition'];
|
|
|
|
readonly setIsWebView: (iframe: HTMLIFrameElement) => void;
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Represents the internal state of the WebView node.
|
2019-03-07 23:26:23 +00:00
|
|
|
export class WebViewImpl {
|
|
|
|
public beforeFirstNavigation = true
|
|
|
|
public elementAttached = false
|
|
|
|
public guestInstanceId?: number
|
|
|
|
public hasFocus = false
|
|
|
|
public internalInstanceId?: number;
|
|
|
|
public viewInstanceId: number
|
|
|
|
|
|
|
|
// on* Event handlers.
|
|
|
|
public on: Record<string, any> = {}
|
|
|
|
public internalElement: HTMLIFrameElement
|
|
|
|
|
2021-05-07 07:07:47 +00:00
|
|
|
public attributes: Map<string, WebViewAttribute>;
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2021-05-15 07:42:07 +00:00
|
|
|
constructor (public webviewNode: HTMLElement, private hooks: WebViewImplHooks) {
|
2018-08-28 18:35:44 +00:00
|
|
|
// Create internal iframe element.
|
2018-08-16 22:57:40 +00:00
|
|
|
this.internalElement = this.createInternalElement();
|
2018-09-13 16:10:51 +00:00
|
|
|
const shadowRoot = this.webviewNode.attachShadow({ mode: 'open' });
|
2021-01-25 01:29:25 +00:00
|
|
|
const style = shadowRoot.ownerDocument.createElement('style');
|
|
|
|
style.textContent = ':host { display: flex; }';
|
|
|
|
shadowRoot.appendChild(style);
|
2021-05-07 07:07:47 +00:00
|
|
|
this.attributes = setupWebViewAttributes(this);
|
2016-03-25 19:57:17 +00:00
|
|
|
this.viewInstanceId = getNextId();
|
2018-08-16 22:57:40 +00:00
|
|
|
shadowRoot.appendChild(this.internalElement);
|
2018-08-28 18:35:44 +00:00
|
|
|
|
|
|
|
// Provide access to contentWindow.
|
|
|
|
Object.defineProperty(this.webviewNode, 'contentWindow', {
|
|
|
|
get: () => {
|
|
|
|
return this.internalElement.contentWindow;
|
|
|
|
},
|
|
|
|
enumerable: true
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
createInternalElement () {
|
|
|
|
const iframeElement = document.createElement('iframe');
|
2018-08-31 20:53:13 +00:00
|
|
|
iframeElement.style.flex = '1 1 auto';
|
2018-10-17 00:33:31 +00:00
|
|
|
iframeElement.style.width = '100%';
|
2018-08-16 22:57:40 +00:00
|
|
|
iframeElement.style.border = '0';
|
2021-05-08 00:56:22 +00:00
|
|
|
// used by RendererClientBase::IsWebViewFrame
|
2021-05-15 07:42:07 +00:00
|
|
|
this.hooks.setIsWebView(iframeElement);
|
2018-08-16 22:57:40 +00:00
|
|
|
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 15:12:18 +00:00
|
|
|
this.guestInstanceId = undefined;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-09-08 17:01:01 +00:00
|
|
|
|
|
|
|
this.beforeFirstNavigation = true;
|
2023-01-25 15:08:46 +00:00
|
|
|
(this.attributes.get(WEB_VIEW_ATTRIBUTES.PARTITION) as PartitionAttribute).validPartitionId = true;
|
2016-11-03 00:28:37 +00:00
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
// Since attachment swaps a local frame for a remote frame, we need our
|
|
|
|
// internal iframe element to be local again before we can reattach.
|
|
|
|
const newFrame = this.createInternalElement();
|
|
|
|
const oldFrame = this.internalElement;
|
|
|
|
this.internalElement = newFrame;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
if (oldFrame && oldFrame.parentNode) {
|
|
|
|
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.
|
2019-03-07 23:26:23 +00:00
|
|
|
handleWebviewAttributeMutation (attributeName: string, oldValue: any, newValue: any) {
|
2020-11-03 23:02:23 +00:00
|
|
|
if (!this.attributes.has(attributeName) || this.attributes.get(attributeName)!.ignoreMutation) {
|
2016-03-25 19:57:17 +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-11-03 23:02:23 +00:00
|
|
|
this.attributes.get(attributeName)!.handleMutation(oldValue, newValue);
|
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 () {
|
2021-08-02 15:35:57 +00:00
|
|
|
this.internalInstanceId = getNextId();
|
|
|
|
this.hooks.guestViewInternal.createGuest(this.internalElement, this.internalInstanceId, this.buildParams())
|
|
|
|
.then(guestInstanceId => {
|
|
|
|
this.attachGuestInstance(guestInstanceId);
|
|
|
|
});
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-11-04 02:15:20 +00:00
|
|
|
dispatchEvent (eventName: string, props: Record<string, any> = {}) {
|
2021-05-15 07:42:07 +00:00
|
|
|
const event = new Event(eventName);
|
|
|
|
Object.assign(event, props);
|
|
|
|
this.webviewNode.dispatchEvent(event);
|
|
|
|
|
|
|
|
if (eventName === 'load-commit') {
|
|
|
|
this.onLoadCommit(props);
|
|
|
|
} else if (eventName === '-focus-change') {
|
|
|
|
this.onFocusChange();
|
|
|
|
}
|
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.
|
2019-03-07 23:26:23 +00:00
|
|
|
setupEventProperty (eventName: string) {
|
2016-11-03 17:39:40 +00:00
|
|
|
const propertyName = `on${eventName.toLowerCase()}`;
|
2016-01-12 02:40:23 +00:00
|
|
|
return Object.defineProperty(this.webviewNode, propertyName, {
|
2016-03-10 19:54:17 +00:00
|
|
|
get: () => {
|
2016-03-25 19:57:17 +00:00
|
|
|
return this.on[propertyName];
|
2016-03-10 19:54:17 +00:00
|
|
|
},
|
|
|
|
set: (value) => {
|
|
|
|
if (this.on[propertyName]) {
|
2016-03-25 19:57:17 +00:00
|
|
|
this.webviewNode.removeEventListener(eventName, this.on[propertyName]);
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
this.on[propertyName] = value;
|
2016-03-10 19:54:17 +00:00
|
|
|
if (value) {
|
2016-03-25 19:57:17 +00:00
|
|
|
return this.webviewNode.addEventListener(eventName, value);
|
2016-03-10 19:54:17 +00:00
|
|
|
}
|
|
|
|
},
|
2016-01-12 02:40:23 +00:00
|
|
|
enumerable: true
|
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.
|
2020-11-04 02:15:20 +00:00
|
|
|
onLoadCommit (props: Record<string, any>) {
|
2023-01-25 15:08:46 +00:00
|
|
|
const oldValue = this.webviewNode.getAttribute(WEB_VIEW_ATTRIBUTES.SRC);
|
2020-11-04 02:15:20 +00:00
|
|
|
const newValue = props.url;
|
|
|
|
if (props.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.
|
2023-01-25 15:08:46 +00:00
|
|
|
this.attributes.get(WEB_VIEW_ATTRIBUTES.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
|
|
|
|
2018-08-28 18:35:44 +00:00
|
|
|
// Emits focus/blur events.
|
|
|
|
onFocusChange () {
|
2021-05-05 18:31:44 +00:00
|
|
|
const hasFocus = this.webviewNode.ownerDocument.activeElement === this.webviewNode;
|
2018-08-28 18:35:44 +00:00
|
|
|
if (hasFocus !== this.hasFocus) {
|
|
|
|
this.hasFocus = hasFocus;
|
2020-11-04 02:15:20 +00:00
|
|
|
this.dispatchEvent(hasFocus ? 'focus' : 'blur');
|
2018-08-28 18:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
onAttach (storagePartitionId: number) {
|
2023-01-25 15:08:46 +00:00
|
|
|
return this.attributes.get(WEB_VIEW_ATTRIBUTES.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 () {
|
2019-03-07 23:26:23 +00:00
|
|
|
const params: Record<string, any> = {
|
2022-01-12 07:41:20 +00:00
|
|
|
instanceId: this.viewInstanceId
|
2016-03-25 19:57:17 +00:00
|
|
|
};
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2020-11-03 23:02:23 +00:00
|
|
|
for (const [attributeName, attribute] of this.attributes) {
|
|
|
|
params[attributeName] = attribute.getValue();
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
attachGuestInstance (guestInstanceId: number) {
|
2021-08-02 15:35:57 +00:00
|
|
|
if (guestInstanceId === -1) {
|
2022-07-21 09:29:31 +00:00
|
|
|
this.dispatchEvent('destroyed');
|
2021-08-02 15:35:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
if (!this.elementAttached) {
|
|
|
|
// The element could be detached before we got response from browser.
|
2021-08-02 15:35:57 +00:00
|
|
|
// Destroy the backing webContents to avoid any zombie nodes in the frame tree.
|
|
|
|
this.hooks.guestViewInternal.detachGuest(guestInstanceId);
|
2018-08-16 22:57:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-07 23:26:23 +00:00
|
|
|
|
2021-08-02 15:35:57 +00:00
|
|
|
this.guestInstanceId = guestInstanceId;
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-03-07 23:26:23 +00:00
|
|
|
// I wish eslint wasn't so stupid, but it is
|
|
|
|
// eslint-disable-next-line
|
2021-05-15 07:42:07 +00:00
|
|
|
export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElement, hooks: WebViewImplHooks) => {
|
2019-01-12 01:00:43 +00:00
|
|
|
// Focusing the webview should move page focus to the underlying iframe.
|
|
|
|
WebViewElement.prototype.focus = function () {
|
|
|
|
this.contentWindow.focus();
|
2016-03-25 19:57:17 +00:00
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Forward proto.foo* method calls to WebViewImpl.foo*.
|
2018-12-04 15:12:21 +00:00
|
|
|
for (const method of syncMethods) {
|
2021-05-15 07:42:07 +00:00
|
|
|
(WebViewElement.prototype as Record<string, any>)[method] = function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
|
|
|
return hooks.guestViewInternal.invokeSync(this.getWebContentsId(), method, args);
|
2019-01-17 20:08:54 +00:00
|
|
|
};
|
2021-05-15 07:42:07 +00:00
|
|
|
}
|
2019-01-17 20:08:54 +00:00
|
|
|
|
2019-06-17 09:10:02 +00:00
|
|
|
for (const method of asyncMethods) {
|
2021-05-15 07:42:07 +00:00
|
|
|
(WebViewElement.prototype as Record<string, any>)[method] = function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
|
|
|
return hooks.guestViewInternal.invoke(this.getWebContentsId(), method, args);
|
|
|
|
};
|
2019-01-17 20:08:54 +00:00
|
|
|
}
|
2019-11-12 20:56:17 +00:00
|
|
|
|
2020-03-03 22:25:14 +00:00
|
|
|
const createPropertyGetter = function (property: string) {
|
|
|
|
return function (this: ElectronInternal.WebViewElement) {
|
2021-05-15 07:42:07 +00:00
|
|
|
return hooks.guestViewInternal.propertyGet(this.getWebContentsId(), property);
|
2020-03-03 22:25:14 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createPropertySetter = function (property: string) {
|
|
|
|
return function (this: ElectronInternal.WebViewElement, arg: any) {
|
2021-05-15 07:42:07 +00:00
|
|
|
return hooks.guestViewInternal.propertySet(this.getWebContentsId(), property, arg);
|
2020-03-03 22:25:14 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const property of properties) {
|
|
|
|
Object.defineProperty(WebViewElement.prototype, property, {
|
2020-10-08 01:01:23 +00:00
|
|
|
get: createPropertyGetter(property),
|
2020-03-03 22:25:14 +00:00
|
|
|
set: createPropertySetter(property)
|
|
|
|
});
|
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
};
|