Browse Source

Access URL through webContents directly

Kevin Sawicki 8 years ago
parent
commit
64077277b1
2 changed files with 21 additions and 6 deletions
  1. 15 0
      lib/browser/guest-window-manager.js
  2. 6 6
      lib/renderer/override.js

+ 15 - 0
lib/browser/guest-window-manager.js

@@ -182,3 +182,18 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event,
     console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
   }
 })
+
+ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', function (event, guestId, method, ...args) {
+  const guestContents = webContents.fromId(guestId)
+  if (guestContents == null) {
+    event.returnValue = null
+    return
+  }
+
+  if (canAccessWindow(event.sender, guestContents)) {
+    event.returnValue = guestContents[method].apply(guestContents, args)
+  } else {
+    console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
+    event.returnValue = null
+  }
+})

+ 6 - 6
lib/renderer/override.js

@@ -40,28 +40,28 @@ var BrowserWindowProxy = (function () {
   }
 
   BrowserWindowProxy.prototype.close = function () {
-    return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId)
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId)
   }
 
   BrowserWindowProxy.prototype.focus = function () {
-    return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus')
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus')
   }
 
   BrowserWindowProxy.prototype.blur = function () {
-    return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur')
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur')
   }
 
   BrowserWindowProxy.prototype.print = function () {
-    return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print')
+    ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print')
   }
 
   Object.defineProperty(BrowserWindowProxy.prototype, 'location', {
     get: function () {
-      return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'getURL')
+      return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'getURL')
     },
     set: function (url) {
       url = resolveURL(url)
-      return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'loadURL', url)
+      return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'loadURL', url)
     }
   })