From e80e3a53e978a2170c7f68627989ab9f3250e696 Mon Sep 17 00:00:00 2001 From: Anrock Date: Mon, 8 Oct 2018 15:08:59 +0300 Subject: [PATCH 1/3] feat: introduce LocationProxy for BrowserWindowProxy --- lib/renderer/window-setup.js | 60 +++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/lib/renderer/window-setup.js b/lib/renderer/window-setup.js index f4ab44adb5bc..486da6ca6a69 100644 --- a/lib/renderer/window-setup.js +++ b/lib/renderer/window-setup.js @@ -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') @@ -54,18 +54,56 @@ 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 - - defineProperty(this, 'location', { - get: function () { - return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL') - }, - set: function (url) { - url = resolveURL(url) - return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'loadURL', url) - } - }) + this.location = new LocationProxy(ipcRenderer, guestId) ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => { removeProxy(guestId) From fc4e10b6c067985b3a0cf9b6e4214dbff4287ca0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 4 Dec 2018 16:22:03 +0900 Subject: [PATCH 2/3] fix: set setter of window.location --- lib/renderer/window-setup.js | 12 +++++++++++- spec/chromium-spec.js | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/renderer/window-setup.js b/lib/renderer/window-setup.js index 486da6ca6a69..ec7580916ea2 100644 --- a/lib/renderer/window-setup.js +++ b/lib/renderer/window-setup.js @@ -103,7 +103,17 @@ function LocationProxy (ipcRenderer, guestId) { function BrowserWindowProxy (ipcRenderer, guestId) { this.closed = false - this.location = new LocationProxy(ipcRenderer, guestId) + + const location = new LocationProxy(ipcRenderer, guestId) + defineProperty(this, 'location', { + get: function () { + return location + }, + set: function (url) { + url = resolveURL(url) + return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'loadURL', url) + } + }) ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => { removeProxy(guestId) diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index 439475506a3a..79063476e87c 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -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() }) From ca7dec2082a29801e88c67541d1b8c92a339b56f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 4 Dec 2018 17:00:13 +0900 Subject: [PATCH 3/3] fix: default prop of location should be empty str --- lib/renderer/window-setup.js | 3 ++- spec/api-browser-window-spec.js | 2 +- spec/chromium-spec.js | 8 ++++---- spec/fixtures/pages/window-opener-location.html | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/renderer/window-setup.js b/lib/renderer/window-setup.js index ec7580916ea2..3da16bad7d40 100644 --- a/lib/renderer/window-setup.js +++ b/lib/renderer/window-setup.js @@ -70,7 +70,8 @@ function LocationProxy (ipcRenderer, guestId) { return { get: function () { const guestURL = getGuestURL() - return guestURL ? guestURL[property] : '' + const value = guestURL ? guestURL[property] : '' + return value === undefined ? '' : value }, set: function (newVal) { const guestURL = getGuestURL() diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 84d4abc85825..e816a0c9e70a 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -3480,7 +3480,7 @@ describe('BrowserWindow module', () => { iw.loadURL('about:blank') iw.webContents.executeJavaScript(` const opened = window.open() - openedLocation = opened.location + openedLocation = opened.location.href opened.close() window.postMessage({openedLocation}, '*') `) diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index 79063476e87c..939b9c0fa8f4 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -545,14 +545,14 @@ describe('chromium feature', () => { webContents.once('did-finish-load', () => { const { location } = b b.close() - assert.strictEqual(location, 'about:blank') + assert.strictEqual(location.href, 'about:blank') let c = null app.once('browser-window-created', (event, { webContents }) => { webContents.once('did-finish-load', () => { const { location } = c c.close() - assert.strictEqual(location, 'about:blank') + assert.strictEqual(location.href, 'about:blank') done() }) }) @@ -645,7 +645,7 @@ describe('chromium feature', () => { it('does nothing when origin of current window does not match opener', (done) => { listener = (event) => { - assert.strictEqual(event.data, null) + assert.strictEqual(event.data, '') done() } 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) => { webview = new WebView() webview.addEventListener('console-message', (e) => { - assert.strictEqual(e.message, 'null') + assert.strictEqual(e.message, '') done() }) webview.setAttribute('allowpopups', 'on') diff --git a/spec/fixtures/pages/window-opener-location.html b/spec/fixtures/pages/window-opener-location.html index c1594e667de3..d386f5cc5d9e 100644 --- a/spec/fixtures/pages/window-opener-location.html +++ b/spec/fixtures/pages/window-opener-location.html @@ -1,7 +1,7 @@