2016-03-25 19:57:17 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
const ipcRenderer = require('electron').ipcRenderer
|
|
|
|
const remote = require('electron').remote
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-07 09:26:36 +00:00
|
|
|
// Cache browser window visibility
|
2016-03-25 19:57:17 +00:00
|
|
|
var _isVisible = true
|
|
|
|
var _isMinimized = false
|
|
|
|
;(function () {
|
|
|
|
var currentWindow
|
|
|
|
currentWindow = remote.getCurrentWindow()
|
|
|
|
_isVisible = currentWindow.isVisible()
|
|
|
|
_isMinimized = currentWindow.isMinimized()
|
|
|
|
})()
|
2016-03-07 09:26:36 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Helper function to resolve relative url.
|
2016-03-25 19:57:17 +00:00
|
|
|
var a = window.top.document.createElement('a')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
var resolveURL = function (url) {
|
|
|
|
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-03-25 19:57:17 +00:00
|
|
|
var BrowserWindowProxy = (function () {
|
|
|
|
BrowserWindowProxy.proxies = {}
|
|
|
|
|
|
|
|
BrowserWindowProxy.getOrCreate = function (guestId) {
|
|
|
|
var base
|
|
|
|
return (base = this.proxies)[guestId] != null ? base[guestId] : base[guestId] = new BrowserWindowProxy(guestId)
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserWindowProxy.remove = function (guestId) {
|
|
|
|
return delete this.proxies[guestId]
|
|
|
|
}
|
|
|
|
|
|
|
|
function BrowserWindowProxy (guestId1) {
|
|
|
|
this.guestId = guestId1
|
|
|
|
this.closed = false
|
|
|
|
ipcRenderer.once('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_' + this.guestId, () => {
|
|
|
|
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 () {
|
|
|
|
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype.focus = function () {
|
|
|
|
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus')
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
BrowserWindowProxy.prototype.blur = function () {
|
|
|
|
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur')
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-13 00:05:01 +00:00
|
|
|
Object.defineProperty(BrowserWindowProxy.prototype, 'location', {
|
2016-03-25 19:57:17 +00:00
|
|
|
get: function () {
|
|
|
|
return ipcRenderer.sendSync('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'getURL')
|
2016-03-13 00:05:01 +00:00
|
|
|
},
|
2016-03-25 19:57:17 +00:00
|
|
|
set: function (url) {
|
|
|
|
return ipcRenderer.sendSync('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', 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-03-30 21:07:27 +00:00
|
|
|
return ipcRenderer.send('ATOM_SHELL_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) {
|
|
|
|
return ipcRenderer.send.apply(ipcRenderer, ['ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript'].concat(args))
|
|
|
|
}
|
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 () {
|
|
|
|
return remote.getCurrentWindow().close()
|
|
|
|
}
|
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) {
|
|
|
|
var feature, guestId, i, j, len, len1, name, options, ref1, ref2, value
|
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
|
|
|
|
|
|
|
// TODO remove hyphenated options in both of the following arrays for 1.0
|
2016-03-25 19:57:17 +00:00
|
|
|
const ints = ['x', 'y', 'width', 'height', 'min-width', 'minWidth', 'max-width', 'maxWidth', 'min-height', 'minHeight', 'max-height', 'maxHeight', 'zoom-factor', 'zoomFactor']
|
|
|
|
const webPreferences = ['zoom-factor', 'zoomFactor', 'node-integration', 'nodeIntegration', 'preload']
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Make sure to get rid of excessive whitespace in the property name
|
2016-03-25 19:57:17 +00:00
|
|
|
ref1 = features.split(/,\s*/)
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = ref1.length; i < len; i++) {
|
2016-03-25 19:57:17 +00:00
|
|
|
feature = ref1[i]
|
|
|
|
ref2 = feature.split(/\s*=/)
|
|
|
|
name = ref2[0]
|
|
|
|
value = ref2[1]
|
|
|
|
value = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value
|
2016-03-16 17:59:47 +00:00
|
|
|
if (webPreferences.includes(name)) {
|
|
|
|
if (options.webPreferences == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
options.webPreferences = {}
|
2016-03-16 17:59:47 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
options.webPreferences[name] = value
|
2016-03-16 17:59:47 +00:00
|
|
|
} else {
|
2016-03-25 19:57:17 +00:00
|
|
|
options[name] = value
|
2016-03-16 17:59:47 +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-03-25 19:57:17 +00:00
|
|
|
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-03-25 19:57:17 +00:00
|
|
|
guestId = ipcRenderer.sendSync('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, options)
|
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-01-14 18:35:29 +00:00
|
|
|
// Use the dialog API to implement alert().
|
2016-01-12 02:40:23 +00:00
|
|
|
window.alert = function(message, title) {
|
2016-03-25 19:57:17 +00:00
|
|
|
var buttons
|
|
|
|
if (arguments.length === 0) {
|
|
|
|
message = ''
|
2016-03-20 07:09:30 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
if (title == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
title = ''
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
buttons = ['OK']
|
|
|
|
message = String(message)
|
2016-01-12 02:40:23 +00:00
|
|
|
remote.dialog.showMessageBox(remote.getCurrentWindow(), {
|
|
|
|
message: message,
|
|
|
|
title: title,
|
|
|
|
buttons: buttons
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
// Alert should always return undefined.
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// And the confirm().
|
2016-03-25 19:57:17 +00:00
|
|
|
window.confirm = function (message, title) {
|
|
|
|
var buttons, cancelId
|
2016-01-12 02:40:23 +00:00
|
|
|
if (title == null) {
|
2016-03-25 19:57:17 +00:00
|
|
|
title = ''
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
buttons = ['OK', 'Cancel']
|
|
|
|
cancelId = 1
|
2016-01-12 02:40:23 +00:00
|
|
|
return !remote.dialog.showMessageBox(remote.getCurrentWindow(), {
|
|
|
|
message: message,
|
|
|
|
title: title,
|
|
|
|
buttons: buttons,
|
|
|
|
cancelId: cancelId
|
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-03-08 17:36:41 +00:00
|
|
|
ipcRenderer.on('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, isVisible, isMinimized) {
|
2016-03-29 00:35:49 +00:00
|
|
|
var hasChanged = _isVisible !== isVisible || _isMinimized !== isMinimized
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-03-08 17:36:41 +00:00
|
|
|
if (hasChanged) {
|
2016-03-25 19:57:17 +00:00
|
|
|
_isVisible = isVisible
|
|
|
|
_isMinimized = isMinimized
|
2016-03-07 09:26:36 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
document.dispatchEvent(new Event('visibilitychange'))
|
2016-03-08 17:36:41 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-03-07 09:26:36 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
ipcRenderer.on('ATOM_SHELL_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)
|
|
|
|
return window.dispatchEvent(event)
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Forward history operations to browser.
|
2016-03-25 19:57:17 +00:00
|
|
|
var sendHistoryOperation = function (...args) {
|
|
|
|
return ipcRenderer.send.apply(ipcRenderer, ['ATOM_SHELL_NAVIGATION_CONTROLLER'].concat(args))
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
var getHistoryOperation = function (...args) {
|
|
|
|
return ipcRenderer.sendSync.apply(ipcRenderer, ['ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER'].concat(args))
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.history.back = function () {
|
|
|
|
return sendHistoryOperation('goBack')
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.history.forward = function () {
|
|
|
|
return sendHistoryOperation('goForward')
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
window.history.go = function (offset) {
|
|
|
|
return sendHistoryOperation('goToOffset', offset)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
Object.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-01-14 18:35:29 +00:00
|
|
|
// Make document.hidden and document.visibilityState return the correct value.
|
2016-01-12 02:40:23 +00:00
|
|
|
Object.defineProperty(document, 'hidden', {
|
2016-03-08 17:36:41 +00:00
|
|
|
get: function () {
|
2016-03-25 19:57:17 +00:00
|
|
|
return _isMinimized || !_isVisible
|
2016-03-08 17:36:41 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
Object.defineProperty(document, 'visibilityState', {
|
2016-03-25 19:57:17 +00:00
|
|
|
get: function () {
|
2016-03-08 17:36:41 +00:00
|
|
|
if (_isVisible && !_isMinimized) {
|
2016-03-25 19:57:17 +00:00
|
|
|
return 'visible'
|
2016-03-07 09:26:36 +00:00
|
|
|
} else {
|
2016-03-25 19:57:17 +00:00
|
|
|
return 'hidden'
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|