Avoid using processId and routingId directly.

This commit is contained in:
Cheng Zhao 2014-04-25 16:45:14 +08:00
parent 23ccad4915
commit 1bba15cb7f
4 changed files with 26 additions and 39 deletions

View file

@ -40,7 +40,7 @@ BrowserWindow::_init = ->
# Tell the rpc server that a render view has been deleted and we need to
# release all objects owned by it.
@webContents.on 'render-view-deleted', (event, processId, routingId) ->
process.emit 'ATOM_BROWSER_RELEASE_RENDER_VIEW', processId, routingId
process.emit 'ATOM_BROWSER_RELEASE_RENDER_VIEW', "#{processId}-#{routingId}"
BrowserWindow::toggleDevTools = ->
if @isDevToolsOpened() then @closeDevTools() else @openDevTools()
@ -71,17 +71,13 @@ BrowserWindow.getFocusedWindow = ->
windows = BrowserWindow.getAllWindows()
return window for window in windows when window.isFocused()
BrowserWindow.fromProcessIdAndRoutingId = (processId, routingId) ->
BrowserWindow.fromWebContents = (webContents) ->
windows = BrowserWindow.getAllWindows()
return window for window in windows when window.getProcessId() == processId and
window.getRoutingId() == routingId
return window for window in windows when webContents.equal window.webContents
BrowserWindow.fromDevTools = (processId, routingId) ->
BrowserWindow.fromDevToolsWebContents = (webContents) ->
windows = BrowserWindow.getAllWindows()
for window in windows when window.isDevToolsOpened()
devtools = window.getDevTools()
return window if devtools.processId == processId and
devtools.routingId == routingId
return window for window in windows when webContents.equal window.devToolsWebContents
# Helpers.
BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
@ -98,9 +94,6 @@ BrowserWindow::stop = -> @webContents.stop()
BrowserWindow::getRoutingId = -> @webContents.getRoutingId()
BrowserWindow::getProcessId = -> @webContents.getProcessId()
BrowserWindow::isCrashed = -> @webContents.isCrashed()
BrowserWindow::getDevTools = ->
processId: @devToolsWebContents.getProcessId()
routingId: @devToolsWebContents.getRoutingId()
BrowserWindow::executeJavaScriptInDevTools = (code) ->
@devToolsWebContents.executeJavaScript code

View file

@ -7,10 +7,14 @@ module.exports.wrap = (webContents) ->
# webContents is an EventEmitter.
webContents.__proto__ = EventEmitter.prototype
# Add the send method.
# WebContents::send(channel, args..)
webContents.send = (args...) ->
@_send 'ATOM_INTERNAL_MESSAGE', [args...]
# The processId and routingId and identify a webContents.
webContents.getId = -> "#{@getProcessId()}-#{@getRoutingId()}"
webContents.equal = (other) -> @getId() is other.getId()
# Dispatch IPC messages to the ipc module.
webContents.on 'ipc-message', (event, channel, args...) =>
Object.defineProperty event, 'sender', value: webContents

View file

@ -29,13 +29,11 @@ class ObjectsStore
throw new Error("Invalid key #{id} for ObjectsStore") unless @has id
@objects[id]
@forRenderView: (processId, routingId) ->
key = "#{processId}_#{routingId}"
@forRenderView: (key) ->
@stores[key] = new ObjectsStore unless @stores[key]?
@stores[key]
@releaseForRenderView: (processId, routingId) ->
key = "#{processId}_#{routingId}"
@releaseForRenderView: (key) ->
delete @stores[key]
class ObjectsRegistry extends EventEmitter
@ -52,7 +50,7 @@ class ObjectsRegistry extends EventEmitter
# Register a new object, the object would be kept referenced until you release
# it explicitly.
add: (processId, routingId, obj) ->
add: (key, obj) ->
# Some native objects may already been added to objectsWeakMap, be care not
# to add it twice.
@objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
@ -63,7 +61,7 @@ class ObjectsRegistry extends EventEmitter
# with the storeId.
# We use a difference key because the same object can be referenced for
# multiple times by the same renderer view.
store = ObjectsStore.forRenderView processId, routingId
store = ObjectsStore.forRenderView key
storeId = store.add obj
[id, storeId]
@ -73,12 +71,12 @@ class ObjectsRegistry extends EventEmitter
@objectsWeakMap.get id
# Remove an object according to its storeId.
remove: (processId, routingId, storeId) ->
ObjectsStore.forRenderView(processId, routingId).remove storeId
remove: (key, storeId) ->
ObjectsStore.forRenderView(key).remove storeId
# Clear all references to objects from renderer view.
clear: (processId, routingId) ->
@emit "clear-#{processId}-#{routingId}"
ObjectsStore.releaseForRenderView processId, routingId
clear: (key) ->
@emit "clear-#{key}"
ObjectsStore.releaseForRenderView key
module.exports = new ObjectsRegistry

View file

@ -22,9 +22,7 @@ valueToMeta = (sender, value) ->
# Reference the original value if it's an object, because when it's
# passed to renderer we would assume the renderer keeps a reference of
# it.
processId = sender.getProcessId()
routingId = sender.getRoutingId()
[meta.id, meta.storeId] = objectsRegistry.add processId, routingId, value
[meta.id, meta.storeId] = objectsRegistry.add sender.getId(), value
meta.members = []
meta.members.push {name: prop, type: typeof field} for prop, field of value
@ -40,8 +38,6 @@ errorToMeta = (error) ->
# Convert array of meta data from renderer into array of real values.
unwrapArgs = (sender, args) ->
processId = sender.getProcessId()
routingId = sender.getRoutingId()
metaToValue = (meta) ->
switch meta.type
when 'value' then meta.value
@ -57,7 +53,7 @@ unwrapArgs = (sender, args) ->
-> returnValue
when 'function'
rendererReleased = false
objectsRegistry.once "clear-#{processId}-#{routingId}", ->
objectsRegistry.once "clear-#{sender.getId()}", ->
rendererReleased = true
ret = ->
@ -83,8 +79,8 @@ callFunction = (event, func, caller, args) ->
event.returnValue = valueToMeta event.sender, ret
# Send by BrowserWindow when its render view is deleted.
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (processId, routingId) ->
objectsRegistry.clear processId, routingId
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
objectsRegistry.clear id
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
try
@ -101,10 +97,8 @@ ipc.on 'ATOM_BROWSER_GLOBAL', (event, name) ->
ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event) ->
try
BrowserWindow = require 'browser-window'
processId = event.sender.getProcessId()
routingId = event.sender.getRoutingId()
window = BrowserWindow.fromProcessIdAndRoutingId processId, routingId
window = BrowserWindow.fromDevTools processId, routingId unless window?
window = BrowserWindow.fromWebContents event.sender
window = BrowserWindow.fromDevToolsWebContents event.sender unless window?
event.returnValue = valueToMeta event.sender, window
catch e
event.returnValue = errorToMeta e
@ -162,6 +156,4 @@ ipc.on 'ATOM_BROWSER_MEMBER_GET', (event, id, name) ->
event.returnValue = errorToMeta e
ipc.on 'ATOM_BROWSER_DEREFERENCE', (event, storeId) ->
processId = event.sender.getProcessId()
routingId = event.sender.getRoutingId()
objectsRegistry.remove processId, routingId, storeId
objectsRegistry.remove event.sender.getId(), storeId