Merge pull request #1059 from atom/ipc-preload
Don't rely on global "process" object for message dispatching
This commit is contained in:
commit
5bfe644c3e
6 changed files with 53 additions and 28 deletions
|
@ -9,8 +9,8 @@ module.exports.wrap = (webContents) ->
|
||||||
webContents.__proto__ = EventEmitter.prototype
|
webContents.__proto__ = EventEmitter.prototype
|
||||||
|
|
||||||
# WebContents::send(channel, args..)
|
# WebContents::send(channel, args..)
|
||||||
webContents.send = (args...) ->
|
webContents.send = (channel, args...) ->
|
||||||
@_send 'ATOM_INTERNAL_MESSAGE', [args...]
|
@_send channel, [args...]
|
||||||
|
|
||||||
# Make sure webContents.executeJavaScript would run the code only when the
|
# Make sure webContents.executeJavaScript would run the code only when the
|
||||||
# web contents has been loaded.
|
# web contents has been loaded.
|
||||||
|
|
|
@ -1,26 +1,20 @@
|
||||||
EventEmitter = require('events').EventEmitter
|
binding = process.atomBinding 'ipc'
|
||||||
process = global.process
|
v8Util = process.atomBinding 'v8_util'
|
||||||
ipc = process.atomBinding('ipc')
|
|
||||||
|
|
||||||
class Ipc extends EventEmitter
|
# Created by init.coffee.
|
||||||
constructor: ->
|
ipc = v8Util.getHiddenValue global, 'ipc'
|
||||||
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
|
|
||||||
@emit args...
|
|
||||||
|
|
||||||
window.addEventListener 'unload', (event) ->
|
ipc.send = (args...) ->
|
||||||
process.removeAllListeners 'ATOM_INTERNAL_MESSAGE'
|
binding.send 'ipc-message', [args...]
|
||||||
|
|
||||||
send: (args...) ->
|
ipc.sendSync = (args...) ->
|
||||||
ipc.send 'ipc-message', [args...]
|
JSON.parse binding.sendSync('ipc-message-sync', [args...])
|
||||||
|
|
||||||
sendSync: (args...) ->
|
ipc.sendToHost = (args...) ->
|
||||||
JSON.parse ipc.sendSync('ipc-message-sync', [args...])
|
binding.send 'ipc-message-host', [args...]
|
||||||
|
|
||||||
sendToHost: (args...) ->
|
# Deprecated.
|
||||||
ipc.send 'ipc-message-host', [args...]
|
ipc.sendChannel = ipc.send
|
||||||
|
ipc.sendChannelSync = ipc.sendSync
|
||||||
|
|
||||||
# Discarded
|
module.exports = ipc
|
||||||
sendChannel: -> @send.apply this, arguments
|
|
||||||
sendChannelSync: -> @sendSync.apply this, arguments
|
|
||||||
|
|
||||||
module.exports = new Ipc
|
|
||||||
|
|
|
@ -29,10 +29,15 @@ namespace atom {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
v8::Handle<v8::Object> GetProcessObject(v8::Isolate* isolate,
|
bool GetIPCObject(v8::Isolate* isolate,
|
||||||
v8::Handle<v8::Context> context) {
|
v8::Handle<v8::Context> context,
|
||||||
v8::Handle<v8::String> key = mate::StringToV8(isolate, "process");
|
v8::Handle<v8::Object>* ipc) {
|
||||||
return context->Global()->Get(key)->ToObject();
|
v8::Handle<v8::String> key = mate::StringToV8(isolate, "ipc");
|
||||||
|
v8::Handle<v8::Value> value = context->Global()->GetHiddenValue(key);
|
||||||
|
if (value.IsEmpty() || !value->IsObject())
|
||||||
|
return false;
|
||||||
|
*ipc = value->ToObject();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<v8::Handle<v8::Value>> ListValueToVector(
|
std::vector<v8::Handle<v8::Value>> ListValueToVector(
|
||||||
|
@ -118,8 +123,9 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
|
||||||
isolate, args);
|
isolate, args);
|
||||||
arguments.insert(arguments.begin(), mate::ConvertToV8(isolate, channel));
|
arguments.insert(arguments.begin(), mate::ConvertToV8(isolate, channel));
|
||||||
|
|
||||||
v8::Handle<v8::Object> process = GetProcessObject(isolate, context);
|
v8::Handle<v8::Object> ipc;
|
||||||
node::MakeCallback(isolate, process, "emit", arguments.size(), &arguments[0]);
|
if (GetIPCObject(isolate, context, &ipc))
|
||||||
|
node::MakeCallback(isolate, ipc, "emit", arguments.size(), &arguments[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
process = global.process
|
process = global.process
|
||||||
|
events = require 'events'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
url = require 'url'
|
url = require 'url'
|
||||||
Module = require 'module'
|
Module = require 'module'
|
||||||
|
@ -21,6 +22,10 @@ globalPaths.push path.join(process.resourcesPath, 'app')
|
||||||
# Import common settings.
|
# Import common settings.
|
||||||
require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init')
|
require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init')
|
||||||
|
|
||||||
|
# The global variable will be used by ipc for event dispatching
|
||||||
|
v8Util = process.atomBinding 'v8_util'
|
||||||
|
v8Util.setHiddenValue global, 'ipc', new events.EventEmitter
|
||||||
|
|
||||||
# Process command line arguments.
|
# Process command line arguments.
|
||||||
nodeIntegration = 'false'
|
nodeIntegration = 'false'
|
||||||
for arg in process.argv
|
for arg in process.argv
|
||||||
|
|
4
spec/fixtures/module/preload-ipc.js
vendored
Normal file
4
spec/fixtures/module/preload-ipc.js
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
var ipc = require('ipc');
|
||||||
|
ipc.on('ping', function(message) {
|
||||||
|
ipc.sendToHost('pong', message);
|
||||||
|
});
|
|
@ -58,6 +58,22 @@ describe '<webview> tag', ->
|
||||||
webview.src = "file://#{fixtures}/pages/e.html"
|
webview.src = "file://#{fixtures}/pages/e.html"
|
||||||
document.body.appendChild webview
|
document.body.appendChild webview
|
||||||
|
|
||||||
|
it 'receives ipc message in preload script', (done) ->
|
||||||
|
message = 'boom!'
|
||||||
|
listener = (e) ->
|
||||||
|
assert.equal e.channel, 'pong'
|
||||||
|
assert.deepEqual e.args, [message]
|
||||||
|
webview.removeEventListener 'ipc-message', listener
|
||||||
|
done()
|
||||||
|
listener2 = (e) ->
|
||||||
|
webview.send 'ping', message
|
||||||
|
webview.removeEventListener 'did-finish-load', listener2
|
||||||
|
webview.addEventListener 'ipc-message', listener
|
||||||
|
webview.addEventListener 'did-finish-load', listener2
|
||||||
|
webview.setAttribute 'preload', "#{fixtures}/module/preload-ipc.js"
|
||||||
|
webview.src = "file://#{fixtures}/pages/e.html"
|
||||||
|
document.body.appendChild webview
|
||||||
|
|
||||||
describe 'httpreferrer attribute', ->
|
describe 'httpreferrer attribute', ->
|
||||||
it 'sets the referrer url', (done) ->
|
it 'sets the referrer url', (done) ->
|
||||||
referrer = 'http://github.com/'
|
referrer = 'http://github.com/'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue