Merge pull request #15019 from Anrock/child-window-location
fix: Introduce LocationProxy for BrowserWindowProxy
This commit is contained in:
commit
5bc86eaf9c
4 changed files with 58 additions and 9 deletions
|
@ -23,7 +23,7 @@
|
||||||
// - document.hidden
|
// - document.hidden
|
||||||
// - document.visibilityState
|
// - document.visibilityState
|
||||||
|
|
||||||
const { defineProperty } = Object
|
const { defineProperty, defineProperties } = Object
|
||||||
|
|
||||||
// Helper function to resolve relative url.
|
// Helper function to resolve relative url.
|
||||||
const a = window.top.document.createElement('a')
|
const a = window.top.document.createElement('a')
|
||||||
|
@ -54,12 +54,61 @@ const removeProxy = (guestId) => {
|
||||||
delete windowProxies[guestId]
|
delete windowProxies[guestId]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function LocationProxy (ipcRenderer, guestId) {
|
||||||
|
const getGuestURL = function () {
|
||||||
|
const urlString = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
|
||||||
|
try {
|
||||||
|
return new URL(urlString)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('LocationProxy: failed to parse string', urlString, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const propertyProxyFor = function (property) {
|
||||||
|
return {
|
||||||
|
get: function () {
|
||||||
|
const guestURL = getGuestURL()
|
||||||
|
const value = guestURL ? guestURL[property] : ''
|
||||||
|
return value === undefined ? '' : value
|
||||||
|
},
|
||||||
|
set: function (newVal) {
|
||||||
|
const guestURL = getGuestURL()
|
||||||
|
if (guestURL) {
|
||||||
|
guestURL[property] = newVal
|
||||||
|
return ipcRenderer.sendSync(
|
||||||
|
'ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC',
|
||||||
|
guestId, 'loadURL', guestURL.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
defineProperties(this, {
|
||||||
|
hash: propertyProxyFor('hash'),
|
||||||
|
href: propertyProxyFor('href'),
|
||||||
|
host: propertyProxyFor('host'),
|
||||||
|
hostname: propertyProxyFor('hostname'),
|
||||||
|
origin: propertyProxyFor('origin'),
|
||||||
|
pathname: propertyProxyFor('pathname'),
|
||||||
|
port: propertyProxyFor('port'),
|
||||||
|
protocol: propertyProxyFor('protocol'),
|
||||||
|
search: propertyProxyFor('search')
|
||||||
|
})
|
||||||
|
|
||||||
|
this.toString = function () {
|
||||||
|
return this.href
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function BrowserWindowProxy (ipcRenderer, guestId) {
|
function BrowserWindowProxy (ipcRenderer, guestId) {
|
||||||
this.closed = false
|
this.closed = false
|
||||||
|
|
||||||
|
const location = new LocationProxy(ipcRenderer, guestId)
|
||||||
defineProperty(this, 'location', {
|
defineProperty(this, 'location', {
|
||||||
get: function () {
|
get: function () {
|
||||||
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
|
return location
|
||||||
},
|
},
|
||||||
set: function (url) {
|
set: function (url) {
|
||||||
url = resolveURL(url)
|
url = resolveURL(url)
|
||||||
|
|
|
@ -3480,7 +3480,7 @@ describe('BrowserWindow module', () => {
|
||||||
iw.loadURL('about:blank')
|
iw.loadURL('about:blank')
|
||||||
iw.webContents.executeJavaScript(`
|
iw.webContents.executeJavaScript(`
|
||||||
const opened = window.open()
|
const opened = window.open()
|
||||||
openedLocation = opened.location
|
openedLocation = opened.location.href
|
||||||
opened.close()
|
opened.close()
|
||||||
window.postMessage({openedLocation}, '*')
|
window.postMessage({openedLocation}, '*')
|
||||||
`)
|
`)
|
||||||
|
|
|
@ -515,7 +515,7 @@ describe('chromium feature', () => {
|
||||||
}
|
}
|
||||||
app.once('browser-window-created', (event, window) => {
|
app.once('browser-window-created', (event, window) => {
|
||||||
window.webContents.once('did-finish-load', () => {
|
window.webContents.once('did-finish-load', () => {
|
||||||
assert.strictEqual(b.location, targetURL)
|
assert.strictEqual(b.location.href, targetURL)
|
||||||
b.close()
|
b.close()
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
@ -545,14 +545,14 @@ describe('chromium feature', () => {
|
||||||
webContents.once('did-finish-load', () => {
|
webContents.once('did-finish-load', () => {
|
||||||
const { location } = b
|
const { location } = b
|
||||||
b.close()
|
b.close()
|
||||||
assert.strictEqual(location, 'about:blank')
|
assert.strictEqual(location.href, 'about:blank')
|
||||||
|
|
||||||
let c = null
|
let c = null
|
||||||
app.once('browser-window-created', (event, { webContents }) => {
|
app.once('browser-window-created', (event, { webContents }) => {
|
||||||
webContents.once('did-finish-load', () => {
|
webContents.once('did-finish-load', () => {
|
||||||
const { location } = c
|
const { location } = c
|
||||||
c.close()
|
c.close()
|
||||||
assert.strictEqual(location, 'about:blank')
|
assert.strictEqual(location.href, 'about:blank')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -645,7 +645,7 @@ describe('chromium feature', () => {
|
||||||
|
|
||||||
it('does nothing when origin of current window does not match opener', (done) => {
|
it('does nothing when origin of current window does not match opener', (done) => {
|
||||||
listener = (event) => {
|
listener = (event) => {
|
||||||
assert.strictEqual(event.data, null)
|
assert.strictEqual(event.data, '')
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
window.addEventListener('message', listener)
|
window.addEventListener('message', listener)
|
||||||
|
@ -694,7 +694,7 @@ describe('chromium feature', () => {
|
||||||
it('does nothing when origin of webview src URL does not match opener', (done) => {
|
it('does nothing when origin of webview src URL does not match opener', (done) => {
|
||||||
webview = new WebView()
|
webview = new WebView()
|
||||||
webview.addEventListener('console-message', (e) => {
|
webview.addEventListener('console-message', (e) => {
|
||||||
assert.strictEqual(e.message, 'null')
|
assert.strictEqual(e.message, '')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
webview.setAttribute('allowpopups', 'on')
|
webview.setAttribute('allowpopups', 'on')
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
window.opener.postMessage(window.opener.location, '*')
|
window.opener.postMessage(window.opener.location.href, '*')
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue