Move more functions to outer scope
authorKevin Sawicki <kevinsawicki@gmail.com>
Thu, 12 Jan 2017 01:04:20 +0000 (17:04 -0800)
committerKevin Sawicki <kevinsawicki@gmail.com>
Mon, 16 Jan 2017 20:38:16 +0000 (12:38 -0800)
lib/renderer/window-setup.js

index 3aa451d..f7328c9 100644 (file)
@@ -15,53 +15,62 @@ const resolveURL = function (url) {
 
 const windowProxies = {}
 
-module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
-  const getOrCreateProxy = (guestId) => {
-    let proxy = windowProxies[guestId]
-    if (proxy == null) {
-      proxy = new BrowserWindowProxy(guestId)
-      windowProxies[guestId] = proxy
-    }
-    return proxy
+const getOrCreateProxy = (ipcRenderer, guestId) => {
+  let proxy = windowProxies[guestId]
+  if (proxy == null) {
+    proxy = new BrowserWindowProxy(ipcRenderer, guestId)
+    windowProxies[guestId] = proxy
   }
+  return proxy
+}
 
-  const removeProxy = (guestId) => {
-    delete windowProxies[guestId]
-  }
+const removeProxy = (guestId) => {
+  delete windowProxies[guestId]
+}
 
-  function BrowserWindowProxy (guestId) {
-    this.closed = false
+function BrowserWindowProxy (ipcRenderer, guestId) {
+  this.closed = false
 
-    ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
-      removeProxy(this.guestId)
-      this.closed = true
-    })
+  ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
+    removeProxy(this.guestId)
+    this.closed = true
+  })
 
-    this.close = () => {
-      ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId)
-    }
+  this.close = () => {
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId)
+  }
 
-    this.focus = () => {
-      ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus')
-    }
+  this.focus = () => {
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus')
+  }
 
-    this.blur = () => {
-      ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur')
-    }
+  this.blur = () => {
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur')
+  }
 
-    this.print = () => {
-      ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print')
-    }
+  this.print = () => {
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print')
+  }
 
-    this.postMessage = (message, targetOrigin) => {
-      ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, targetOrigin, window.location.origin)
-    }
+  this.postMessage = (message, targetOrigin) => {
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, targetOrigin, window.location.origin)
+  }
 
-    this.eval = (...args) => {
-      ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args)
-    }
+  this.eval = (...args) => {
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args)
   }
+}
 
+// Forward history operations to browser.
+const sendHistoryOperation = function (ipcRenderer, ...args) {
+  ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
+}
+
+const getHistoryOperation = function (ipcRenderer, ...args) {
+  return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
+}
+
+module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
   if (guestInstanceId == null) {
     // Override default window.close.
     window.close = function () {
@@ -76,7 +85,7 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
     }
     const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, features)
     if (guestId != null) {
-      return getOrCreateProxy(guestId)
+      return getOrCreateProxy(ipcRenderer, guestId)
     } else {
       return null
     }
@@ -96,7 +105,7 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
   }
 
   if (openerId != null) {
-    window.opener = getOrCreateProxy(openerId)
+    window.opener = getOrCreateProxy(ipcRenderer, openerId)
   }
 
   ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
@@ -106,34 +115,25 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
     event.initEvent('message', false, false)
     event.data = message
     event.origin = sourceOrigin
-    event.source = BrowserWindowProxy.getOrCreate(sourceId)
+    event.source = getOrCreateProxy(ipcRenderer, sourceId)
     window.dispatchEvent(event)
   })
 
-  // Forward history operations to browser.
-  const sendHistoryOperation = function (...args) {
-    ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
-  }
-
-  const getHistoryOperation = function (...args) {
-    return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
-  }
-
   window.history.back = function () {
-    sendHistoryOperation('goBack')
+    sendHistoryOperation(ipcRenderer, 'goBack')
   }
 
   window.history.forward = function () {
-    sendHistoryOperation('goForward')
+    sendHistoryOperation(ipcRenderer, 'goForward')
   }
 
   window.history.go = function (offset) {
-    sendHistoryOperation('goToOffset', offset)
+    sendHistoryOperation(ipcRenderer, 'goToOffset', offset)
   }
 
   defineProperty(window.history, 'length', {
     get: function () {
-      return getHistoryOperation('length')
+      return getHistoryOperation(ipcRenderer, 'length')
     }
   })