Add remote.getCurrentWindow() API in renderer.

This commit is contained in:
Cheng Zhao 2013-04-27 19:06:41 +08:00
parent 51db9494bd
commit 99f6a5678a
3 changed files with 24 additions and 0 deletions

View file

@ -40,6 +40,8 @@ objectsWeakMap.add = (obj) ->
enumerable: true, writable: false, value: id enumerable: true, writable: false, value: id
id id
windowsWeakMap = new IDWeakMap
process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) -> process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
# It's possible that user created a object in browser side and then want to # It's possible that user created a object in browser side and then want to
# get it in renderer via remote.getObject. So we must add every native object # get it in renderer via remote.getObject. So we must add every native object
@ -47,6 +49,9 @@ process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
# renderer. # renderer.
objectsWeakMap.add obj objectsWeakMap.add obj
# Also remember all windows.
windowsWeakMap.add obj if obj.constructor.name is 'Window'
exports.add = (process_id, routing_id, obj) -> exports.add = (process_id, routing_id, 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.
@ -61,5 +66,9 @@ exports.add = (process_id, routing_id, obj) ->
exports.get = (id) -> exports.get = (id) ->
objectsWeakMap.get id objectsWeakMap.get id
exports.getAllWindows = () ->
keys = windowsWeakMap.keys()
windowsWeakMap.get key for key in keys
exports.remove = (process_id, routing_id, storeId) -> exports.remove = (process_id, routing_id, storeId) ->
ObjectsStore.forRenderView(process_id, routing_id).remove storeId ObjectsStore.forRenderView(process_id, routing_id).remove storeId

View file

@ -34,6 +34,16 @@ ipc.on 'ATOM_INTERNAL_REQUIRE', (event, process_id, routing_id, module) ->
catch e catch e
event.result = type: 'error', value: e.message event.result = type: 'error', value: e.message
ipc.on 'ATOM_INTERNAL_CURRENT_WINDOW', (event, process_id, routing_id) ->
try
windows = objectsRegistry.getAllWindows()
for window in windows
break if window.getProcessID() == process_id and
window.getRoutingID() == routing_id
event.result = new Meta(process_id, routing_id, window)
catch e
event.result = type: 'error', value: e.message
ipc.on 'ATOM_INTERNAL_CONSTRUCTOR', (event, process_id, routing_id, id, args) -> ipc.on 'ATOM_INTERNAL_CONSTRUCTOR', (event, process_id, routing_id, id, args) ->
try try
constructor = objectsRegistry.get id constructor = objectsRegistry.get id

View file

@ -62,3 +62,8 @@ exports.require = (module) ->
exports.getObject = (id) -> exports.getObject = (id) ->
meta = ipc.sendChannelSync 'ATOM_INTERNAL_REFERENCE', id meta = ipc.sendChannelSync 'ATOM_INTERNAL_REFERENCE', id
metaToValue meta metaToValue meta
# Get current window object.
exports.getCurrentWindow = ->
meta = ipc.sendChannelSync 'ATOM_INTERNAL_CURRENT_WINDOW'
metaToValue meta