2016-03-25 19:57:17 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
const {ipcRenderer} = require('electron')
|
2016-10-25 01:21:42 +00:00
|
|
|
const parseFeaturesString = require('../common/parse-features-string')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
const {defineProperty} = Object
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Helper function to resolve relative url.
|
2016-12-02 01:34:14 +00:00
|
|
|
const a = window.top.document.createElement('a')
|
|
|
|
const resolveURL = function (url) {
|
2016-03-25 19:57:17 +00:00
|
|
|
a.href = url
|
|
|
|
return a.href
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Window object returned by "window.open".
|
2016-12-02 01:34:14 +00:00
|
|
|
const BrowserWindowProxy = (function () {
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.proxies = {}
|
|
|
|
|
|
|
|
BrowserWindowProxy.getOrCreate = function (guestId) {
|
2016-12-02 01:34:14 +00:00
|
|
|
let proxy = this.proxies[guestId]
|
|
|
|
if (proxy == null) {
|
|
|
|
proxy = new BrowserWindowProxy(guestId)
|
|
|
|
this.proxies[guestId] = proxy
|
|
|
|
}
|
|
|
|
return proxy
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserWindowProxy.remove = function (guestId) {
|
2016-11-25 18:05:12 +00:00
|
|
|
delete this.proxies[guestId]
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function BrowserWindowProxy (guestId1) {
|
2016-12-02 01:34:14 +00:00
|
|
|
defineProperty(this, 'guestId', {
|
2016-06-09 20:41:42 +00:00
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
writeable: false,
|
|
|
|
value: guestId1
|
|
|
|
})
|
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
this.closed = false
|
2016-04-06 23:21:26 +00:00
|
|
|
ipcRenderer.once('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_' + this.guestId, () => {
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.remove(this.guestId)
|
|
|
|
this.closed = true
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype.close = function () {
|
2016-11-25 18:03:47 +00:00
|
|
|
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype.focus = function () {
|
2016-11-25 18:03:47 +00:00
|
|
|
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus')
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype.blur = function () {
|
2016-11-25 18:03:47 +00:00
|
|
|
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur')
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-05-26 16:35:26 +00:00
|
|
|
BrowserWindowProxy.prototype.print = function () {
|
2016-11-25 18:03:47 +00:00
|
|
|
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print')
|
2016-05-26 16:35:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
defineProperty(BrowserWindowProxy.prototype, 'location', {
|
2016-03-25 19:57:17 +00:00
|
|
|
get: function () {
|
2016-11-25 18:03:47 +00:00
|
|
|
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'getURL')
|
2016-03-13 00:05:01 +00:00
|
|
|
},
|
2016-03-25 19:57:17 +00:00
|
|
|
set: function (url) {
|
2016-08-16 11:24:49 +00:00
|
|
|
url = resolveURL(url)
|
2016-11-25 18:03:47 +00:00
|
|
|
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'loadURL', url)
|
2016-03-13 00:05:01 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-03-13 00:05:01 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype.postMessage = function (message, targetOrigin) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (targetOrigin == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
targetOrigin = '*'
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-11-25 18:05:12 +00:00
|
|
|
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', this.guestId, message, targetOrigin, window.location.origin)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype['eval'] = function (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript', ...args)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
return BrowserWindowProxy
|
|
|
|
})()
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
if (process.guestInstanceId == null) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Override default window.close.
|
2016-03-25 19:57:17 +00:00
|
|
|
window.close = function () {
|
2016-12-02 01:34:14 +00:00
|
|
|
ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Make the browser window or guest view emit "new-window" event.
|
2016-03-25 19:57:17 +00:00
|
|
|
window.open = function (url, frameName, features) {
|
2016-12-02 01:34:14 +00:00
|
|
|
let guestId, j, len1, name, options, additionalFeatures
|
2016-01-12 02:40:23 +00:00
|
|
|
if (frameName == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
frameName = ''
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (features == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
features = ''
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
options = {}
|
2016-03-16 18:03:50 +00:00
|
|
|
|
2016-04-28 16:39:37 +00:00
|
|
|
const ints = ['x', 'y', 'width', 'height', 'minWidth', 'maxWidth', 'minHeight', 'maxHeight', 'zoomFactor']
|
|
|
|
const webPreferences = ['zoomFactor', 'nodeIntegration', 'preload']
|
2016-03-25 03:17:33 +00:00
|
|
|
const disposition = 'new-window'
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-09-15 13:37:35 +00:00
|
|
|
// Used to store additional features
|
|
|
|
additionalFeatures = []
|
|
|
|
|
2016-10-25 01:21:42 +00:00
|
|
|
// Parse the features
|
|
|
|
parseFeaturesString(features, function (key, value) {
|
2016-09-15 13:37:35 +00:00
|
|
|
if (value === undefined) {
|
2016-10-25 01:21:42 +00:00
|
|
|
additionalFeatures.push(key)
|
2016-03-16 17:59:47 +00:00
|
|
|
} else {
|
2016-10-25 01:21:42 +00:00
|
|
|
if (webPreferences.includes(key)) {
|
2016-09-15 13:37:35 +00:00
|
|
|
if (options.webPreferences == null) {
|
|
|
|
options.webPreferences = {}
|
|
|
|
}
|
2016-10-25 01:21:42 +00:00
|
|
|
options.webPreferences[key] = value
|
2016-09-15 13:37:35 +00:00
|
|
|
} else {
|
2016-10-25 01:21:42 +00:00
|
|
|
options[key] = value
|
2016-09-15 13:37:35 +00:00
|
|
|
}
|
2016-03-16 17:59:47 +00:00
|
|
|
}
|
2016-10-25 01:21:42 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options.left) {
|
|
|
|
if (options.x == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options.x = options.left
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.top) {
|
|
|
|
if (options.y == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options.y = options.top
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.title == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options.title = frameName
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.width == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options.width = 800
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.height == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options.height = 600
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Resolve relative urls.
|
2016-09-09 00:11:10 +00:00
|
|
|
if (url == null || url === '') {
|
2016-09-08 23:10:21 +00:00
|
|
|
url = 'about:blank'
|
|
|
|
} else {
|
|
|
|
url = resolveURL(url)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
for (j = 0, len1 = ints.length; j < len1; j++) {
|
2016-03-25 19:57:17 +00:00
|
|
|
name = ints[j]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options[name] != null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options[name] = parseInt(options[name], 10)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 13:37:35 +00:00
|
|
|
guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, disposition, options, additionalFeatures)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (guestId) {
|
2016-03-25 19:57:17 +00:00
|
|
|
return BrowserWindowProxy.getOrCreate(guestId)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-25 19:57:17 +00:00
|
|
|
return null
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
window.alert = function (message, title) {
|
|
|
|
ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', message, title)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.confirm = function (message, title) {
|
2016-12-02 01:34:14 +00:00
|
|
|
return ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CONFIRM', message, title)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// But we do not support prompt().
|
2016-03-25 19:57:17 +00:00
|
|
|
window.prompt = function () {
|
|
|
|
throw new Error('prompt() is and will not be supported.')
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
if (process.openerId != null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
window.opener = BrowserWindowProxy.getOrCreate(process.openerId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 23:21:26 +00:00
|
|
|
ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Manually dispatch event instead of using postMessage because we also need to
|
|
|
|
// set event.source.
|
2016-03-25 19:57:17 +00:00
|
|
|
event = document.createEvent('Event')
|
|
|
|
event.initEvent('message', false, false)
|
|
|
|
event.data = message
|
|
|
|
event.origin = sourceOrigin
|
|
|
|
event.source = BrowserWindowProxy.getOrCreate(sourceId)
|
2016-05-19 22:28:08 +00:00
|
|
|
window.dispatchEvent(event)
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Forward history operations to browser.
|
2016-12-02 01:34:14 +00:00
|
|
|
const sendHistoryOperation = function (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
const getHistoryOperation = function (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.history.back = function () {
|
2016-11-25 18:05:12 +00:00
|
|
|
sendHistoryOperation('goBack')
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.history.forward = function () {
|
2016-11-25 18:05:12 +00:00
|
|
|
sendHistoryOperation('goForward')
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.history.go = function (offset) {
|
2016-11-25 18:05:12 +00:00
|
|
|
sendHistoryOperation('goToOffset', offset)
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
defineProperty(window.history, 'length', {
|
2016-03-25 19:57:17 +00:00
|
|
|
get: function () {
|
|
|
|
return getHistoryOperation('length')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-04-13 13:56:11 +00:00
|
|
|
// The initial visibilityState.
|
2016-04-14 01:50:07 +00:00
|
|
|
let cachedVisibilityState = process.argv.includes('--hidden-page') ? 'hidden' : 'visible'
|
2016-04-13 13:56:11 +00:00
|
|
|
|
2016-04-13 12:36:43 +00:00
|
|
|
// Subscribe to visibilityState changes.
|
|
|
|
ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, visibilityState) {
|
2016-04-13 14:10:31 +00:00
|
|
|
if (cachedVisibilityState !== visibilityState) {
|
2016-04-13 12:36:43 +00:00
|
|
|
cachedVisibilityState = visibilityState
|
|
|
|
document.dispatchEvent(new Event('visibilitychange'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Make document.hidden and document.visibilityState return the correct value.
|
2016-12-02 01:34:14 +00:00
|
|
|
defineProperty(document, 'hidden', {
|
2016-03-08 17:36:41 +00:00
|
|
|
get: function () {
|
2016-04-13 14:10:31 +00:00
|
|
|
return cachedVisibilityState !== 'visible'
|
2016-03-08 17:36:41 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
defineProperty(document, 'visibilityState', {
|
2016-03-25 19:57:17 +00:00
|
|
|
get: function () {
|
2016-04-13 12:36:43 +00:00
|
|
|
return cachedVisibilityState
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|