Skip to content

Commit

Permalink
feat: add <webview>.getWebContents() filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jan 7, 2019
1 parent 5862784 commit b56a1f7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/api/app.md
Expand Up @@ -469,6 +469,18 @@ Emitted when `remote.getCurrentWebContents()` is called in the renderer process
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

### Event: 'remote-get-guest-web-contents'

Returns:

* `event` Event
* `webContents` [WebContents](web-contents.md)
* `guestWebContents` [WebContents](web-contents.md)

Emitted when `<webview>.getWebContents()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

## Methods

The `app` object has the following methods:
Expand Down
11 changes: 11 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -725,6 +725,17 @@ Emitted when `remote.getCurrentWebContents()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

#### Event: 'remote-get-guest-web-contents'

Returns:

* `event` Event
* `guestWebContents` [WebContents](web-contents.md)

Emitted when `<webview>.getWebContents()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`.

### Instance Methods

#### `contents.loadURL(url[, options])`
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/api/web-contents.js
Expand Up @@ -383,7 +383,8 @@ WebContents.prototype._init = function () {
'remote-get-global',
'remote-get-builtin',
'remote-get-current-window',
'remote-get-current-web-contents'
'remote-get-current-web-contents',
'remote-get-guest-web-contents'
]

for (const eventName of forwardedEvents) {
Expand Down
14 changes: 13 additions & 1 deletion lib/browser/rpc-server.js
Expand Up @@ -446,7 +446,19 @@ handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {

handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender)
return valueToMeta(event.sender, contextId, guest)

const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-get-guest-web-contents', customEvent, guest)

if (customEvent.defaultPrevented) {
if (typeof customEvent.returnValue === 'undefined') {
throw new Error('Blocked <webview>.getWebContents()')
}
} else {
customEvent.returnValue = guest
}

return valueToMeta(event.sender, contextId, customEvent.returnValue)
})

// Implements window.close()
Expand Down
3 changes: 2 additions & 1 deletion spec/static/main.js
Expand Up @@ -251,7 +251,8 @@ for (const eventName of [
for (const eventName of [
'desktop-capturer-get-sources',
'remote-get-current-window',
'remote-get-current-web-contents'
'remote-get-current-web-contents',
'remote-get-guest-web-contents'
]) {
ipcMain.on(`handle-next-${eventName}`, function (event, returnValue) {
event.sender.once(eventName, (event) => {
Expand Down
16 changes: 16 additions & 0 deletions spec/webview-spec.js
Expand Up @@ -1195,6 +1195,22 @@ describe('<webview> tag', function () {
assert(webviewContents)
expect(webviewContents.getURL()).to.equal(src)
})

it('can return custom value', async () => {
const src = 'about:blank'
await loadWebView(webview, { src })

ipcRenderer.send('handle-next-remote-get-guest-web-contents', 'Hello World!')
expect(webview.getWebContents()).to.be.equal('Hello World!')
})

it('throws when no returnValue set', async () => {
const src = 'about:blank'
await loadWebView(webview, { src })

ipcRenderer.send('handle-next-remote-get-guest-web-contents')
expect(() => webview.getWebContents()).to.throw('Blocked <webview>.getWebContents()')
})
})

// FIXME(deepak1556): Ch69 follow up.
Expand Down

0 comments on commit b56a1f7

Please sign in to comment.