mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
Use require('ipc').send to communicate between browser and renderer.
This commit is contained in:
66
browser/api/atom_api_browser_ipc.cc
Normal file
66
browser/api/atom_api_browser_ipc.cc
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "browser/api/atom_api_browser_ipc.h"
|
||||
|
||||
#include "base/values.h"
|
||||
#include "common/api/api_messages.h"
|
||||
#include "common/v8_value_converter_impl.h"
|
||||
#include "content/public/browser/render_view_host.h"
|
||||
#include "vendor/node/src/node.h"
|
||||
#include "vendor/node/src/node_internals.h"
|
||||
|
||||
using content::RenderViewHost;
|
||||
using content::V8ValueConverter;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
// static
|
||||
v8::Handle<v8::Value> BrowserIPC::Send(const v8::Arguments &args) {
|
||||
v8::HandleScope scope;
|
||||
|
||||
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString())
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
int process_id = args[0]->IntegerValue();
|
||||
int routing_id = args[1]->IntegerValue();
|
||||
std::string channel(*v8::String::Utf8Value(args[2]));
|
||||
|
||||
RenderViewHost* render_view_host(RenderViewHost::FromID(
|
||||
process_id, routing_id));
|
||||
if (!render_view_host)
|
||||
return node::ThrowError("Invalid render view host");
|
||||
|
||||
// Convert Arguments to Array, so we can use V8ValueConverter to convert it
|
||||
// to ListValue.
|
||||
v8::Local<v8::Array> v8_args = v8::Array::New(args.Length() - 3);
|
||||
for (int i = 0; i < args.Length() - 3; ++i)
|
||||
v8_args->Set(i, args[i + 3]);
|
||||
|
||||
scoped_ptr<V8ValueConverter> converter(new V8ValueConverterImpl());
|
||||
scoped_ptr<base::Value> arguments(
|
||||
converter->FromV8Value(v8_args, v8::Context::GetCurrent()));
|
||||
|
||||
DCHECK(arguments && arguments->IsType(base::Value::TYPE_LIST));
|
||||
|
||||
render_view_host->Send(new AtomViewMsg_Message(
|
||||
routing_id,
|
||||
channel,
|
||||
*static_cast<base::ListValue*>(arguments.get())));
|
||||
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
// static
|
||||
void BrowserIPC::Initialize(v8::Handle<v8::Object> target) {
|
||||
node::SetMethod(target, "send", Send);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_ipc, atom::api::BrowserIPC::Initialize)
|
||||
29
browser/api/atom_api_browser_ipc.h
Normal file
29
browser/api/atom_api_browser_ipc.h
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_API_ATOM_API_BROWSER_IPC_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_BROWSER_IPC_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class BrowserIPC {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static v8::Handle<v8::Value> Send(const v8::Arguments &args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(BrowserIPC);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_API_ATOM_API_BROWSER_IPC_H_
|
||||
@@ -40,8 +40,10 @@ void AtomBrowserBindings::AfterLoad() {
|
||||
DCHECK(!browser_main_parts_.IsEmpty());
|
||||
}
|
||||
|
||||
void AtomBrowserBindings::OnRendererMessage(
|
||||
int routing_id, const base::ListValue& args) {
|
||||
void AtomBrowserBindings::OnRendererMessage(int process_id,
|
||||
int routing_id,
|
||||
const std::string& channel,
|
||||
const base::ListValue& args) {
|
||||
v8::HandleScope scope;
|
||||
|
||||
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
|
||||
@@ -49,8 +51,9 @@ void AtomBrowserBindings::OnRendererMessage(
|
||||
scoped_ptr<V8ValueConverter> converter(new V8ValueConverterImpl());
|
||||
|
||||
std::vector<v8::Handle<v8::Value>> arguments;
|
||||
arguments.reserve(2 + args.GetSize());
|
||||
arguments.push_back(v8::String::New("message"));
|
||||
arguments.reserve(3 + args.GetSize());
|
||||
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
|
||||
arguments.push_back(v8::Integer::New(process_id));
|
||||
arguments.push_back(v8::Integer::New(routing_id));
|
||||
|
||||
for (size_t i = 0; i < args.GetSize(); i++) {
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#ifndef ATOM_BROWSER_API_ATOM_BROWSER_BINDINGS_
|
||||
#define ATOM_BROWSER_API_ATOM_BROWSER_BINDINGS_
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include "common/api/atom_bindings.h"
|
||||
|
||||
namespace base {
|
||||
@@ -22,7 +24,10 @@ class AtomBrowserBindings : public AtomBindings {
|
||||
virtual void AfterLoad();
|
||||
|
||||
// Called when received a message from renderer.
|
||||
void OnRendererMessage(int routing_id, const base::ListValue& args);
|
||||
void OnRendererMessage(int process_id,
|
||||
int routing_id,
|
||||
const std::string& channel,
|
||||
const base::ListValue& args);
|
||||
|
||||
// The require('atom').browserMainParts object.
|
||||
v8::Handle<v8::Object> browser_main_parts() {
|
||||
|
||||
12
browser/api/lib/ipc.coffee
Normal file
12
browser/api/lib/ipc.coffee
Normal file
@@ -0,0 +1,12 @@
|
||||
EventEmitter = require('events').EventEmitter
|
||||
send = process.atom_binding('ipc').send
|
||||
|
||||
class Ipc extends EventEmitter
|
||||
constructor: ->
|
||||
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
|
||||
@emit('message', args...)
|
||||
|
||||
send: (process_id, routing_id, args...) ->
|
||||
send(process_id, routing_id, 'ATOM_INTERNAL_MESSAGE', args...)
|
||||
|
||||
module.exports = new Ipc
|
||||
@@ -1,10 +1,12 @@
|
||||
var atom = require('atom');
|
||||
var ipc = require('ipc');
|
||||
var Window = require('window');
|
||||
|
||||
var mainWindow = null;
|
||||
|
||||
process.on('message', function() {
|
||||
console.log.apply(this, arguments);
|
||||
ipc.on('message', function(process_id, routing_id) {
|
||||
console.log('message from', process_id, routing_id);
|
||||
ipc.send.apply(ipc, arguments);
|
||||
});
|
||||
|
||||
atom.browserMainParts.preMainMessageLoopRun = function() {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "content/public/browser/notification_details.h"
|
||||
#include "content/public/browser/notification_source.h"
|
||||
#include "content/public/browser/notification_types.h"
|
||||
#include "content/public/browser/render_process_host.h"
|
||||
#include "common/api/api_messages.h"
|
||||
#include "common/options_switches.h"
|
||||
#include "ipc/ipc_message_macros.h"
|
||||
@@ -159,9 +160,13 @@ void NativeWindow::Observe(int type,
|
||||
}
|
||||
}
|
||||
|
||||
void NativeWindow::OnRendererMessage(const base::ListValue& args) {
|
||||
void NativeWindow::OnRendererMessage(const std::string& channel,
|
||||
const base::ListValue& args) {
|
||||
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
|
||||
GetWebContents()->GetRoutingID(), args);
|
||||
GetWebContents()->GetRenderProcessHost()->GetID(),
|
||||
GetWebContents()->GetRoutingID(),
|
||||
channel,
|
||||
args);
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
|
||||
@@ -120,7 +120,8 @@ class NativeWindow : public content::WebContentsDelegate,
|
||||
const content::NotificationDetails& details) OVERRIDE;
|
||||
|
||||
private:
|
||||
void OnRendererMessage(const base::ListValue& args);
|
||||
void OnRendererMessage(const std::string& channel,
|
||||
const base::ListValue& args);
|
||||
|
||||
// Notification manager.
|
||||
content::NotificationRegistrar registrar_;
|
||||
|
||||
Reference in New Issue
Block a user