Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Introduce LocationProxy for BrowserWindowProxy #15019

Merged
merged 3 commits into from Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 50 additions & 2 deletions lib/renderer/window-setup.js
Expand Up @@ -23,7 +23,7 @@
// - document.hidden
// - document.visibilityState

const { defineProperty } = Object
const { defineProperty, defineProperties } = Object

// Helper function to resolve relative url.
const a = window.top.document.createElement('a')
Expand Down Expand Up @@ -54,12 +54,60 @@ const removeProxy = (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()
return guestURL ? guestURL[property] : ''
},
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) {
this.closed = false

const location = new LocationProxy(ipcRenderer, guestId)
defineProperty(this, 'location', {
get: function () {
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
return location
},
set: function (url) {
url = resolveURL(url)
Expand Down
2 changes: 1 addition & 1 deletion spec/chromium-spec.js
Expand Up @@ -515,7 +515,7 @@ describe('chromium feature', () => {
}
app.once('browser-window-created', (event, window) => {
window.webContents.once('did-finish-load', () => {
assert.strictEqual(b.location, targetURL)
assert.strictEqual(b.location.href, targetURL)
b.close()
done()
})
Expand Down