Skip to content

Commit

Permalink
feat: add <webview>.getWebContentsId() (electron#17407)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored and kiku-jw committed May 16, 2019
1 parent 6acc31d commit d6e6635
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
4 changes: 4 additions & 0 deletions docs/api/webview-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ this `webview`.
It depends on the [`remote`](remote.md) module,
it is therefore not available when this module is disabled.

### `<webview>.getWebContentsId()`

Returns `Number` - The WebContents ID of this `webview`.

## DOM events

The following DOM events are available to the `webview` tag:
Expand Down
8 changes: 1 addition & 7 deletions lib/browser/guest-view-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,21 @@ const supportedWebViewEvents = [
'update-target-url'
]

let nextGuestInstanceId = 0
const guestInstances = {}
const embedderElementsMap = {}

// Generate guestInstanceId.
const getNextGuestInstanceId = function () {
return ++nextGuestInstanceId
}

// Create a new guest instance.
const createGuest = function (embedder, params) {
if (webViewManager == null) {
webViewManager = process.electronBinding('web_view_manager')
}

const guestInstanceId = getNextGuestInstanceId(embedder)
const guest = webContents.create({
isGuest: true,
partition: params.partition,
embedder: embedder
})
const guestInstanceId = guest.id
guestInstances[guestInstanceId] = {
guest: guest,
embedder: embedder
Expand Down
28 changes: 14 additions & 14 deletions lib/renderer/web-view/web-view-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/pages/webview-did-attach-event.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var {ipcRenderer} = require('electron')
var wv = document.querySelector('webview')
wv.addEventListener('dom-ready', () => {
ipcRenderer.send('webview-dom-ready', wv.getWebContents().id)
ipcRenderer.send('webview-dom-ready', wv.getWebContentsId())
})
</script>
</body>
Expand Down
13 changes: 11 additions & 2 deletions spec/webview-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ describe('<webview> tag', function () {
assert.ok(webview.partition)

const listener = function (webContents, permission, callback) {
if (webContents.id === webview.getWebContents().id) {
if (webContents.id === webview.getWebContentsId()) {
// requestMIDIAccess with sysex requests both midi and midiSysex so
// grant the first midi one and then reject the midiSysex one
if (requestedPermission === 'midiSysex' && permission === 'midi') {
Expand Down Expand Up @@ -1210,7 +1210,7 @@ describe('<webview> tag', function () {
webview.partition = 'permissionTest'
webview.setAttribute('nodeintegration', 'on')
session.fromPartition(webview.partition).setPermissionRequestHandler((webContents, permission, callback) => {
if (webContents.id === webview.getWebContents().id) {
if (webContents.id === webview.getWebContentsId()) {
assert.strictEqual(permission, 'notifications')
setTimeout(() => { callback(true) }, 10)
}
Expand All @@ -1219,6 +1219,15 @@ describe('<webview> tag', function () {
})
})

describe('<webview>.getWebContentsId', () => {
it('can return the WebContents ID', async () => {
const src = 'about:blank'
await loadWebView(webview, { src })

expect(webview.getWebContentsId()).to.be.equal(webview.getWebContents().id)
})
})

describe('<webview>.getWebContents', () => {
it('can return the webcontents associated', async () => {
const src = 'about:blank'
Expand Down
1 change: 1 addition & 0 deletions typings/internal-electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ declare namespace ElectronInternal {

// Created in web-view-impl
public getWebContents(): Electron.WebContents;
public getWebContentsId(): number;
}
}

Expand Down

0 comments on commit d6e6635

Please sign in to comment.