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

View file

@ -7,10 +7,14 @@ module.exports.wrap = (webContents) ->
# webContents is an EventEmitter. # webContents is an EventEmitter.
webContents.__proto__ = EventEmitter.prototype webContents.__proto__ = EventEmitter.prototype
# Add the send method. # WebContents::send(channel, args..)
webContents.send = (args...) -> webContents.send = (args...) ->
@_send 'ATOM_INTERNAL_MESSAGE', [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. # Dispatch IPC messages to the ipc module.
webContents.on 'ipc-message', (event, channel, args...) => webContents.on 'ipc-message', (event, channel, args...) =>
Object.defineProperty event, 'sender', value: webContents 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 throw new Error("Invalid key #{id} for ObjectsStore") unless @has id
@objects[id] @objects[id]
@forRenderView: (processId, routingId) -> @forRenderView: (key) ->
key = "#{processId}_#{routingId}"
@stores[key] = new ObjectsStore unless @stores[key]? @stores[key] = new ObjectsStore unless @stores[key]?
@stores[key] @stores[key]
@releaseForRenderView: (processId, routingId) -> @releaseForRenderView: (key) ->
key = "#{processId}_#{routingId}"
delete @stores[key] delete @stores[key]
class ObjectsRegistry extends EventEmitter 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 # Register a new object, the object would be kept referenced until you release
# it explicitly. # it explicitly.
add: (processId, routingId, obj) -> add: (key, obj) ->
# Some native objects may already been added to objectsWeakMap, be care not # Some native objects may already been added to objectsWeakMap, be care not
# to add it twice. # to add it twice.
@objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId' @objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
@ -63,7 +61,7 @@ class ObjectsRegistry extends EventEmitter
# with the storeId. # with the storeId.
# We use a difference key because the same object can be referenced for # We use a difference key because the same object can be referenced for
# multiple times by the same renderer view. # multiple times by the same renderer view.
store = ObjectsStore.forRenderView processId, routingId store = ObjectsStore.forRenderView key
storeId = store.add obj storeId = store.add obj
[id, storeId] [id, storeId]
@ -73,12 +71,12 @@ class ObjectsRegistry extends EventEmitter
@objectsWeakMap.get id @objectsWeakMap.get id
# Remove an object according to its storeId. # Remove an object according to its storeId.
remove: (processId, routingId, storeId) -> remove: (key, storeId) ->
ObjectsStore.forRenderView(processId, routingId).remove storeId ObjectsStore.forRenderView(key).remove storeId
# Clear all references to objects from renderer view. # Clear all references to objects from renderer view.
clear: (processId, routingId) -> clear: (key) ->
@emit "clear-#{processId}-#{routingId}" @emit "clear-#{key}"
ObjectsStore.releaseForRenderView processId, routingId ObjectsStore.releaseForRenderView key
module.exports = new ObjectsRegistry 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 # 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 # passed to renderer we would assume the renderer keeps a reference of
# it. # it.
processId = sender.getProcessId() [meta.id, meta.storeId] = objectsRegistry.add sender.getId(), value
routingId = sender.getRoutingId()
[meta.id, meta.storeId] = objectsRegistry.add processId, routingId, value
meta.members = [] meta.members = []
meta.members.push {name: prop, type: typeof field} for prop, field of value 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. # Convert array of meta data from renderer into array of real values.
unwrapArgs = (sender, args) -> unwrapArgs = (sender, args) ->
processId = sender.getProcessId()
routingId = sender.getRoutingId()
metaToValue = (meta) -> metaToValue = (meta) ->
switch meta.type switch meta.type
when 'value' then meta.value when 'value' then meta.value
@ -57,7 +53,7 @@ unwrapArgs = (sender, args) ->
-> returnValue -> returnValue
when 'function' when 'function'
rendererReleased = false rendererReleased = false
objectsRegistry.once "clear-#{processId}-#{routingId}", -> objectsRegistry.once "clear-#{sender.getId()}", ->
rendererReleased = true rendererReleased = true
ret = -> ret = ->
@ -83,8 +79,8 @@ callFunction = (event, func, caller, args) ->
event.returnValue = valueToMeta event.sender, ret event.returnValue = valueToMeta event.sender, ret
# Send by BrowserWindow when its render view is deleted. # Send by BrowserWindow when its render view is deleted.
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (processId, routingId) -> process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
objectsRegistry.clear processId, routingId objectsRegistry.clear id
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) -> ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
try try
@ -101,10 +97,8 @@ ipc.on 'ATOM_BROWSER_GLOBAL', (event, name) ->
ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event) -> ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event) ->
try try
BrowserWindow = require 'browser-window' BrowserWindow = require 'browser-window'
processId = event.sender.getProcessId() window = BrowserWindow.fromWebContents event.sender
routingId = event.sender.getRoutingId() window = BrowserWindow.fromDevToolsWebContents event.sender unless window?
window = BrowserWindow.fromProcessIdAndRoutingId processId, routingId
window = BrowserWindow.fromDevTools processId, routingId unless window?
event.returnValue = valueToMeta event.sender, window event.returnValue = valueToMeta event.sender, window
catch e catch e
event.returnValue = errorToMeta e event.returnValue = errorToMeta e
@ -162,6 +156,4 @@ ipc.on 'ATOM_BROWSER_MEMBER_GET', (event, id, name) ->
event.returnValue = errorToMeta e event.returnValue = errorToMeta e
ipc.on 'ATOM_BROWSER_DEREFERENCE', (event, storeId) -> ipc.on 'ATOM_BROWSER_DEREFERENCE', (event, storeId) ->
processId = event.sender.getProcessId() objectsRegistry.remove event.sender.getId(), storeId
routingId = event.sender.getRoutingId()
objectsRegistry.remove processId, routingId, storeId