From e80e3a53e978a2170c7f68627989ab9f3250e696 Mon Sep 17 00:00:00 2001 From: Anrock Date: Mon, 8 Oct 2018 15:08:59 +0300 Subject: [PATCH] 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)