2014-10-26 11:30:53 +00:00
|
|
|
process = global.process
|
2014-10-27 10:52:55 +00:00
|
|
|
ipc = require 'ipc'
|
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.
|
|
|
|
a = document.createElement 'a'
|
|
|
|
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-03-04 09:29:52 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED', (guestId) =>
|
|
|
|
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
|
|
|
|
|
2015-07-06 08:21:08 +00:00
|
|
|
# Inherit the node-integration option of current window.
|
|
|
|
unless options['node-integration']
|
|
|
|
for arg in process.argv when arg.indexOf('--node-integration=') is 0
|
|
|
|
options['node-integration'] = arg.substr(-4) is 'true'
|
|
|
|
break
|
|
|
|
|
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
|
|
|
|
console.error 'It is not allowed to open new window from this WebContents'
|
|
|
|
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}
|
2014-03-01 12:03:49 +00:00
|
|
|
|
|
|
|
# And the confirm().
|
|
|
|
window.confirm = (message, title='') ->
|
|
|
|
dialog = remote.require 'dialog'
|
|
|
|
buttons = ['OK', 'Cancel']
|
|
|
|
not dialog.showMessageBox remote.getCurrentWindow(), {message, title, buttons}
|
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-05-11 08:03:25 +00:00
|
|
|
# Simple implementation of postMessage.
|
2015-06-18 15:44:45 +00:00
|
|
|
unless process.guestInstanceId?
|
|
|
|
window.opener =
|
|
|
|
postMessage: (message, targetOrigin='*') ->
|
|
|
|
ipc.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', message, targetOrigin
|
2015-02-13 16:45:12 +00:00
|
|
|
|
2015-03-04 15:58:06 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (message, targetOrigin) ->
|
|
|
|
window.postMessage message, targetOrigin
|
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'
|