Skip to content

Commit a10782c

Browse files
committed
Don't rely on the global process object for message dispatching
1 parent c14c6a3 commit a10782c

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

atom/renderer/api/lib/ipc.coffee

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
EventEmitter = require('events').EventEmitter
2-
process = global.process
3-
ipc = process.atomBinding('ipc')
1+
binding = process.atomBinding 'ipc'
2+
v8Util = process.atomBinding 'v8_util'
43

5-
class Ipc extends EventEmitter
6-
constructor: ->
7-
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
8-
@emit args...
4+
# Created by init.coffee.
5+
ipc = v8Util.getHiddenValue global, 'ipc'
96

10-
window.addEventListener 'unload', (event) ->
11-
process.removeAllListeners 'ATOM_INTERNAL_MESSAGE'
7+
ipc.on 'ATOM_INTERNAL_MESSAGE', (args...) ->
8+
@emit args...
129

13-
send: (args...) ->
14-
ipc.send 'ipc-message', [args...]
10+
ipc.send = (args...) ->
11+
binding.send 'ipc-message', [args...]
1512

16-
sendSync: (args...) ->
17-
JSON.parse ipc.sendSync('ipc-message-sync', [args...])
13+
ipc.sendSync = (args...) ->
14+
JSON.parse binding.sendSync('ipc-message-sync', [args...])
1815

19-
sendToHost: (args...) ->
20-
ipc.send 'ipc-message-host', [args...]
16+
ipc.sendToHost = (args...) ->
17+
binding.send 'ipc-message-host', [args...]
2118

22-
# Discarded
23-
sendChannel: -> @send.apply this, arguments
24-
sendChannelSync: -> @sendSync.apply this, arguments
19+
# Deprecated.
20+
ipc.sendChannel = ipc.send
21+
ipc.sendChannelSync = ipc.sendSync
2522

26-
module.exports = new Ipc
23+
module.exports = ipc

atom/renderer/atom_render_view_observer.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ namespace atom {
2929

3030
namespace {
3131

32-
v8::Handle<v8::Object> GetProcessObject(v8::Isolate* isolate,
33-
v8::Handle<v8::Context> context) {
34-
v8::Handle<v8::String> key = mate::StringToV8(isolate, "process");
35-
return context->Global()->Get(key)->ToObject();
32+
v8::Handle<v8::Object> GetIPCObject(v8::Isolate* isolate,
33+
v8::Handle<v8::Context> context) {
34+
v8::Handle<v8::String> key = mate::StringToV8(isolate, "ipc");
35+
return context->Global()->GetHiddenValue(key)->ToObject();
3636
}
3737

3838
std::vector<v8::Handle<v8::Value>> ListValueToVector(
@@ -118,8 +118,8 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
118118
isolate, args);
119119
arguments.insert(arguments.begin(), mate::ConvertToV8(isolate, channel));
120120

121-
v8::Handle<v8::Object> process = GetProcessObject(isolate, context);
122-
node::MakeCallback(isolate, process, "emit", arguments.size(), &arguments[0]);
121+
v8::Handle<v8::Object> ipc = GetIPCObject(isolate, context);
122+
node::MakeCallback(isolate, ipc, "emit", arguments.size(), &arguments[0]);
123123
}
124124

125125
} // namespace atom

atom/renderer/lib/init.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
process = global.process
2+
events = require 'events'
23
path = require 'path'
34
url = require 'url'
45
Module = require 'module'
@@ -21,6 +22,10 @@ globalPaths.push path.join(process.resourcesPath, 'app')
2122
# Import common settings.
2223
require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init')
2324

25+
# The global variable will be used by ipc for event dispatching
26+
v8Util = process.atomBinding 'v8_util'
27+
v8Util.setHiddenValue global, 'ipc', new events.EventEmitter
28+
2429
# Process command line arguments.
2530
nodeIntegration = 'false'
2631
for arg in process.argv

0 commit comments

Comments
 (0)