2015-11-10 07:29:43 +00:00
|
|
|
ipc = require 'ipc-renderer'
|
2014-10-26 11:30:53 +00:00
|
|
|
remote = require 'remote'
|
2014-03-01 12:00:39 +00:00
|
|
|
|
2015-07-10 03:36:25 +00:00
|
|
|
# Helper function to resolve relative url.
|
2015-07-10 05:54:30 +00:00
|
|
|
a = window.top.document.createElement 'a'
|
2015-07-10 03:36:25 +00:00
|
|
|
resolveUrl = (url) ->
|
|
|
|
a.href = url
|
|
|
|
a.href
|
|
|
|
|
2014-10-27 13:56:04 +00:00
|
|
|
# Window object returned by "window.open".
|
2015-03-04 16:46:45 +00:00
|
|
|
class BrowserWindowProxy
|
2014-10-28 05:23:25 +00:00
|
|
|
constructor: (@guestId) ->
|
2015-08-05 08:03:08 +00:00
|
|
|
@closed = false
|
2015-11-10 07:29:43 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED', (event, guestId) =>
|
2015-03-04 09:29:52 +00:00
|
|
|
if guestId is @guestId
|
|
|
|
@closed = true
|
2014-10-27 13:56:04 +00:00
|
|
|
|
|
|
|
close: ->
|
2014-10-28 05:23:25 +00:00
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', @guestId
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2014-10-27 15:03:05 +00:00
|
|
|
focus: ->
|
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', @guestId, 'focus'
|
|
|
|
|
|
|
|
blur: ->
|
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', @guestId, 'blur'
|
|
|
|
|
2015-03-04 15:58:06 +00:00
|
|
|
postMessage: (message, targetOrigin='*') ->
|
2015-03-04 09:29:52 +00:00
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', @guestId, message, targetOrigin
|
2015-02-13 16:45:12 +00:00
|
|
|
|
2014-10-27 15:07:41 +00:00
|
|
|
eval: (args...) ->
|
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', @guestId, 'executeJavaScript', args...
|
|
|
|
|
2014-10-27 11:14:17 +00:00
|
|
|
unless process.guestInstanceId?
|
2014-10-27 10:52:55 +00:00
|
|
|
# Override default window.close.
|
2014-10-26 11:30:53 +00:00
|
|
|
window.close = ->
|
|
|
|
remote.getCurrentWindow().close()
|
2014-03-01 12:00:39 +00:00
|
|
|
|
2014-10-27 10:52:55 +00:00
|
|
|
# Make the browser window or guest view emit "new-window" event.
|
2014-10-27 11:14:17 +00:00
|
|
|
window.open = (url, frameName='', features='') ->
|
2014-10-27 10:52:55 +00:00
|
|
|
options = {}
|
2015-02-18 12:36:05 +00:00
|
|
|
ints = [ 'x', 'y', 'width', 'height', 'min-width', 'max-width', 'min-height', 'max-height', 'zoom-factor' ]
|
|
|
|
# Make sure to get rid of excessive whitespace in the property name
|
|
|
|
for feature in features.split /,\s*/
|
|
|
|
[name, value] = feature.split /\s*=/
|
2014-10-27 10:52:55 +00:00
|
|
|
options[name] =
|
2015-02-18 12:36:05 +00:00
|
|
|
if value is 'yes' or value is '1'
|
2014-10-27 10:52:55 +00:00
|
|
|
true
|
2015-02-18 12:36:05 +00:00
|
|
|
else if value is 'no' or value is '0'
|
2014-10-27 10:52:55 +00:00
|
|
|
false
|
|
|
|
else
|
|
|
|
value
|
2015-02-18 12:36:05 +00:00
|
|
|
options.x ?= options.left if options.left
|
|
|
|
options.y ?= options.top if options.top
|
2015-06-29 17:32:27 +00:00
|
|
|
options.title ?= frameName
|
2014-10-27 10:52:55 +00:00
|
|
|
options.width ?= 800
|
|
|
|
options.height ?= 600
|
2014-03-01 12:00:39 +00:00
|
|
|
|
2015-07-10 03:36:25 +00:00
|
|
|
# Resolve relative urls.
|
|
|
|
url = resolveUrl url
|
|
|
|
|
2015-02-18 12:36:05 +00:00
|
|
|
(options[name] = parseInt(options[name], 10) if options[name]?) for name in ints
|
|
|
|
|
2014-10-28 05:23:25 +00:00
|
|
|
guestId = ipc.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, options
|
2015-01-29 05:07:57 +00:00
|
|
|
if guestId
|
2015-03-04 16:46:45 +00:00
|
|
|
new BrowserWindowProxy(guestId)
|
2015-01-29 05:07:57 +00:00
|
|
|
else
|
|
|
|
null
|
2014-03-01 12:01:04 +00:00
|
|
|
|
|
|
|
# Use the dialog API to implement alert().
|
|
|
|
window.alert = (message, title='') ->
|
|
|
|
dialog = remote.require 'dialog'
|
|
|
|
buttons = ['OK']
|
2015-05-29 07:55:11 +00:00
|
|
|
message = message.toString()
|
2014-03-01 12:01:04 +00:00
|
|
|
dialog.showMessageBox remote.getCurrentWindow(), {message, title, buttons}
|
2015-08-22 01:20:52 +00:00
|
|
|
# Alert should always return undefined.
|
|
|
|
return
|
2014-03-01 12:03:49 +00:00
|
|
|
|
|
|
|
# And the confirm().
|
|
|
|
window.confirm = (message, title='') ->
|
|
|
|
dialog = remote.require 'dialog'
|
|
|
|
buttons = ['OK', 'Cancel']
|
2015-08-22 01:20:52 +00:00
|
|
|
cancelId = 1
|
|
|
|
not dialog.showMessageBox remote.getCurrentWindow(), {message, title, buttons, cancelId}
|
2014-03-01 12:05:52 +00:00
|
|
|
|
|
|
|
# But we do not support prompt().
|
|
|
|
window.prompt = ->
|
2015-04-14 07:55:41 +00:00
|
|
|
throw new Error('prompt() is and will not be supported.')
|
2015-02-13 16:45:12 +00:00
|
|
|
|
2015-09-01 03:29:39 +00:00
|
|
|
# Implement window.postMessage if current window is a guest window.
|
|
|
|
guestId = ipc.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID'
|
|
|
|
if guestId?
|
2015-06-18 15:44:45 +00:00
|
|
|
window.opener =
|
|
|
|
postMessage: (message, targetOrigin='*') ->
|
2015-09-01 03:29:39 +00:00
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin
|
|
|
|
|
2015-11-10 07:29:43 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, guestId, message, sourceOrigin) ->
|
2015-09-01 03:29:39 +00:00
|
|
|
# Manually dispatch event instead of using postMessage because we also need to
|
|
|
|
# set event.source.
|
|
|
|
event = document.createEvent 'Event'
|
|
|
|
event.initEvent 'message', false, false
|
|
|
|
event.data = message
|
|
|
|
event.origin = sourceOrigin
|
|
|
|
event.source = new BrowserWindowProxy(guestId)
|
|
|
|
window.dispatchEvent event
|
2015-05-11 08:03:25 +00:00
|
|
|
|
|
|
|
# Forward history operations to browser.
|
|
|
|
sendHistoryOperation = (args...) ->
|
|
|
|
ipc.send 'ATOM_SHELL_NAVIGATION_CONTROLLER', args...
|
2015-05-19 17:11:03 +00:00
|
|
|
|
|
|
|
getHistoryOperation = (args...) ->
|
|
|
|
ipc.sendSync 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', args...
|
|
|
|
|
2015-05-11 08:03:25 +00:00
|
|
|
window.history.back = -> sendHistoryOperation 'goBack'
|
|
|
|
window.history.forward = -> sendHistoryOperation 'goForward'
|
2015-05-11 08:44:01 +00:00
|
|
|
window.history.go = (offset) -> sendHistoryOperation 'goToOffset', offset
|
2015-05-19 17:11:03 +00:00
|
|
|
Object.defineProperty window.history, 'length',
|
|
|
|
get: ->
|
|
|
|
getHistoryOperation 'length'
|
2015-10-01 10:39:35 +00:00
|
|
|
|
|
|
|
# Make document.hidden return the correct value.
|
|
|
|
Object.defineProperty document, 'hidden',
|
|
|
|
get: -> !remote.getCurrentWindow().isVisible()
|