]> khamidou — GitHub mirrors - electron.git/commitdiff
Add remote.getCurrentWindow() API in renderer.
authorCheng Zhao <zcbenz@gmail.com>
Sat, 27 Apr 2013 11:06:41 +0000 (19:06 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sat, 27 Apr 2013 11:06:41 +0000 (19:06 +0800)
browser/atom/objects_registry.coffee
browser/atom/rpc_server.coffee
renderer/api/lib/remote.coffee

index 89eba868cd915c12571ffbff57930518ef7fe786..680e5a7541c0f8538febfb6686ab4bc61990b2af 100644 (file)
@@ -40,6 +40,8 @@ objectsWeakMap.add = (obj) ->
     enumerable: true, writable: false, value: id
   id
 
+windowsWeakMap = new IDWeakMap
+
 process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
   # 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
@@ -47,6 +49,9 @@ process.on 'ATOM_BROWSER_INTERNAL_NEW', (obj) ->
   # renderer.
   objectsWeakMap.add obj
 
+  # Also remember all windows.
+  windowsWeakMap.add obj if obj.constructor.name is 'Window'
+
 exports.add = (process_id, routing_id, obj) ->
   # Some native objects may already been added to objectsWeakMap, be care not
   # to add it twice.
@@ -61,5 +66,9 @@ exports.add = (process_id, routing_id, obj) ->
 exports.get = (id) ->
   objectsWeakMap.get id
 
+exports.getAllWindows = () ->
+  keys = windowsWeakMap.keys()
+  windowsWeakMap.get key for key in keys
+
 exports.remove = (process_id, routing_id, storeId) ->
   ObjectsStore.forRenderView(process_id, routing_id).remove storeId
index 6c0b955880abc19491121dd7061f7108a5768fb3..d4da3e9132d98405d00260c456f37619e63463ce 100644 (file)
@@ -34,6 +34,16 @@ ipc.on 'ATOM_INTERNAL_REQUIRE', (event, process_id, routing_id, module) ->
   catch e
     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) ->
   try
     constructor = objectsRegistry.get id
index e2ef151dc1d6e2438890b07d2054abdefc4b8435..bee625dca3c5cfd6271abcff00ea132cdf535209 100644 (file)
@@ -62,3 +62,8 @@ exports.require = (module) ->
 exports.getObject = (id) ->
   meta = ipc.sendChannelSync 'ATOM_INTERNAL_REFERENCE', id
   metaToValue meta
+
+# Get current window object.
+exports.getCurrentWindow = ->
+  meta = ipc.sendChannelSync 'ATOM_INTERNAL_CURRENT_WINDOW'
+  metaToValue meta