Handle IPC messages in webContents instead of BrowserWindow.

This commit is contained in:
Cheng Zhao 2014-04-25 16:13:16 +08:00
parent 1815f8b40d
commit c0875864dc
21 changed files with 138 additions and 421 deletions

View file

@ -1,10 +1,32 @@
EventEmitter = require('events').EventEmitter
IDWeakMap = require 'id-weak-map'
app = require 'app'
ipc = require 'ipc'
BrowserWindow = process.atomBinding('window').BrowserWindow
BrowserWindow::__proto__ = EventEmitter.prototype
wrapWebContents = (webContents) ->
return null unless webContents.isAlive()
# webContents is an EventEmitter.
webContents.__proto__ = EventEmitter.prototype
# Wrap around the send method.
webContents.send = (args...) ->
@_send 'ATOM_INTERNAL_MESSAGE', [args...]
# Dispatch the ipc messages.
webContents.on 'ipc-message', (event, channel, args...) =>
Object.defineProperty event, 'sender', value: webContents
ipc.emit channel, event, args...
webContents.on 'ipc-message-sync', (event, channel, args...) =>
set = (value) -> event.sendReply JSON.stringify(value)
Object.defineProperty event, 'returnValue', {set}
Object.defineProperty event, 'sender', value: webContents
ipc.emit channel, event, args...
webContents
# Store all created windows in the weak map.
BrowserWindow.windows = new IDWeakMap
@ -45,15 +67,10 @@ BrowserWindow::toggleDevTools = ->
if @isDevToolsOpened() then @closeDevTools() else @openDevTools()
BrowserWindow::getWebContents = ->
webContents = @_getWebContents()
webContents.__proto__ = EventEmitter.prototype
webContents
wrapWebContents @_getWebContents()
BrowserWindow::getDevToolsWebContents = ->
webContents = @_getDevToolsWebContents()
webContents.__proto__ = EventEmitter.prototype
webContents = null unless webContents.isAlive()
webContents
wrapWebContents @_getDevToolsWebContents()
BrowserWindow::restart = ->
@loadUrl(@getUrl())

View file

@ -1,32 +1,3 @@
EventEmitter = require('events').EventEmitter
send = process.atomBinding('ipc').send
sendWrap = (channel, processId, routingId, args...) ->
BrowserWindow = require 'browser-window'
if processId?.constructor is BrowserWindow
window = processId
args = [routingId, args...]
processId = window.getProcessId()
routingId = window.getRoutingId()
send channel, processId, routingId, [args...]
class Ipc extends EventEmitter
constructor: ->
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
@emit(args...)
process.on 'ATOM_INTERNAL_MESSAGE_SYNC', (channel, event, args...) =>
set = (value) -> event.sendReply JSON.stringify(value)
Object.defineProperty event, 'returnValue', {set}
Object.defineProperty event, 'result', {set}
@emit(channel, event, args...)
send: (processId, routingId, args...) ->
@sendChannel(processId, routingId, 'message', args...)
sendChannel: (args...) ->
sendWrap('ATOM_INTERNAL_MESSAGE', args...)
module.exports = new Ipc
module.exports = new EventEmitter