feat: add <webview>.getWebContentsId() (#17407)

This commit is contained in:
Milan Burda 2019-03-26 02:57:27 +01:00 committed by Cheng Zhao
parent 9e26dfaa06
commit 546466b209
6 changed files with 32 additions and 24 deletions

View file

@ -219,6 +219,14 @@ export const setupAttributes = () => {
// 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 () {
const internal = v8Util.getHiddenValue<WebViewImpl>(this, 'internal')
if (!internal.guestInstanceId) {
throw new Error('The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.')
}
return internal.guestInstanceId
}
// WebContents associated with this webview.
WebViewElement.prototype.getWebContents = function () {
if (!remote) {
@ -237,18 +245,10 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
this.contentWindow.focus()
}
const getGuestInstanceId = function (self: any) {
const internal = v8Util.getHiddenValue<WebViewImpl>(self, 'internal')
if (!internal.guestInstanceId) {
throw new Error('The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.')
}
return internal.guestInstanceId
}
// Forward proto.foo* method calls to WebViewImpl.foo*.
const createBlockHandler = function (method: string) {
return function (this: any, ...args: Array<any>) {
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', getGuestInstanceId(this), method, args)
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
}
}
@ -257,8 +257,8 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
}
const createNonBlockHandler = function (method: string) {
return function (this: any, ...args: Array<any>) {
ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', getGuestInstanceId(this), method, args)
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
}
}
@ -267,8 +267,8 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
}
const createPromiseHandler = function (method: string) {
return function (this: any, ...args: Array<any>) {
return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', getGuestInstanceId(this), method, args)
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
}
}