2013-04-21 06:53:26 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/api/atom_browser_bindings.h"
|
2013-04-21 06:53:26 +00:00
|
|
|
|
2013-04-22 14:24:02 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-17 09:14:05 +00:00
|
|
|
#include "atom/browser/api/event.h"
|
2014-04-22 03:06:13 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "base/values.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-04-21 06:53:26 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
AtomBrowserBindings::AtomBrowserBindings() {
|
|
|
|
}
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
void AtomBrowserBindings::OnRendererMessage(int process_id,
|
|
|
|
int routing_id,
|
2013-09-20 14:55:42 +00:00
|
|
|
const string16& channel,
|
2013-04-23 04:18:07 +00:00
|
|
|
const base::ListValue& args) {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-04-22 14:24:02 +00:00
|
|
|
|
2013-12-23 14:42:21 +00:00
|
|
|
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
|
2013-04-22 14:24:02 +00:00
|
|
|
|
2013-04-23 13:52:19 +00:00
|
|
|
// process.emit(channel, 'message', process_id, routing_id);
|
2013-04-22 14:24:02 +00:00
|
|
|
std::vector<v8::Handle<v8::Value>> arguments;
|
2013-04-23 04:18:07 +00:00
|
|
|
arguments.reserve(3 + args.GetSize());
|
2014-04-22 03:06:13 +00:00
|
|
|
arguments.push_back(mate::ConvertToV8(node_isolate, channel));
|
2013-04-23 12:57:14 +00:00
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(0, &value))
|
2013-12-11 07:48:19 +00:00
|
|
|
arguments.push_back(converter->ToV8Value(value, global_env->context()));
|
2013-04-23 04:18:07 +00:00
|
|
|
arguments.push_back(v8::Integer::New(process_id));
|
2013-04-22 14:24:02 +00:00
|
|
|
arguments.push_back(v8::Integer::New(routing_id));
|
|
|
|
|
2013-04-23 12:57:14 +00:00
|
|
|
for (size_t i = 1; i < args.GetSize(); i++) {
|
2013-04-22 14:24:02 +00:00
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(i, &value))
|
2013-12-11 07:48:19 +00:00
|
|
|
arguments.push_back(converter->ToV8Value(value, global_env->context()));
|
2013-04-22 14:24:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
node::MakeCallback(global_env->process_object(),
|
|
|
|
"emit",
|
|
|
|
arguments.size(),
|
|
|
|
&arguments[0]);
|
2013-04-22 13:32:48 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 13:52:19 +00:00
|
|
|
void AtomBrowserBindings::OnRendererMessageSync(
|
|
|
|
int process_id,
|
|
|
|
int routing_id,
|
2013-09-20 14:55:42 +00:00
|
|
|
const string16& channel,
|
2013-04-23 13:52:19 +00:00
|
|
|
const base::ListValue& args,
|
2014-04-04 14:04:42 +00:00
|
|
|
content::WebContents* sender,
|
2013-09-22 01:52:58 +00:00
|
|
|
IPC::Message* message) {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-04-23 13:52:19 +00:00
|
|
|
|
2013-12-23 14:42:21 +00:00
|
|
|
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
|
2013-04-23 13:52:19 +00:00
|
|
|
|
2013-09-22 01:52:58 +00:00
|
|
|
// Create the event object.
|
2014-04-17 09:14:05 +00:00
|
|
|
mate::Handle<mate::Event> event = mate::Event::Create(node_isolate);
|
|
|
|
event->SetSenderAndMessage(sender, message);
|
2013-04-23 13:52:19 +00:00
|
|
|
|
|
|
|
// process.emit(channel, 'sync-message', event, process_id, routing_id);
|
|
|
|
std::vector<v8::Handle<v8::Value>> arguments;
|
|
|
|
arguments.reserve(3 + args.GetSize());
|
2014-04-22 03:06:13 +00:00
|
|
|
arguments.push_back(mate::ConvertToV8(node_isolate, channel));
|
2013-04-23 13:52:19 +00:00
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(0, &value))
|
2013-12-11 07:48:19 +00:00
|
|
|
arguments.push_back(converter->ToV8Value(value, global_env->context()));
|
2014-04-17 09:14:05 +00:00
|
|
|
arguments.push_back(event.ToV8());
|
2013-04-23 13:52:19 +00:00
|
|
|
arguments.push_back(v8::Integer::New(process_id));
|
|
|
|
arguments.push_back(v8::Integer::New(routing_id));
|
|
|
|
|
|
|
|
for (size_t i = 1; i < args.GetSize(); i++) {
|
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(i, &value))
|
2013-12-11 07:48:19 +00:00
|
|
|
arguments.push_back(converter->ToV8Value(value, global_env->context()));
|
2013-04-23 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
node::MakeCallback(global_env->process_object(),
|
|
|
|
"emit",
|
|
|
|
arguments.size(),
|
|
|
|
&arguments[0]);
|
2013-04-23 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2013-04-21 06:53:26 +00:00
|
|
|
} // namespace atom
|