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.
|
|
|
|
|
|
|
|
#include "browser/api/atom_browser_bindings.h"
|
|
|
|
|
2013-04-22 14:24:02 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-04-21 06:53:26 +00:00
|
|
|
#include "base/logging.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "base/values.h"
|
2013-04-22 14:24:02 +00:00
|
|
|
#include "common/v8_value_converter_impl.h"
|
2013-04-21 06:53:26 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "vendor/node/src/node.h"
|
|
|
|
#include "vendor/node/src/node_internals.h"
|
|
|
|
|
2013-04-22 14:24:02 +00:00
|
|
|
using content::V8ValueConverter;
|
2013-04-21 06:53:26 +00:00
|
|
|
using node::node_isolate;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
AtomBrowserBindings::AtomBrowserBindings() {
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomBrowserBindings::~AtomBrowserBindings() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomBrowserBindings::AfterLoad() {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> global = node::g_context->Global();
|
|
|
|
v8::Handle<v8::Object> atom =
|
|
|
|
global->Get(v8::String::New("__atom"))->ToObject();
|
|
|
|
DCHECK(!atom.IsEmpty());
|
|
|
|
|
|
|
|
browser_main_parts_ = v8::Persistent<v8::Object>::New(
|
|
|
|
node_isolate,
|
|
|
|
atom->Get(v8::String::New("browserMainParts"))->ToObject());
|
|
|
|
DCHECK(!browser_main_parts_.IsEmpty());
|
|
|
|
}
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
void AtomBrowserBindings::OnRendererMessage(int process_id,
|
|
|
|
int routing_id,
|
|
|
|
const std::string& channel,
|
|
|
|
const base::ListValue& args) {
|
2013-04-22 14:24:02 +00:00
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
|
|
|
|
|
|
|
|
scoped_ptr<V8ValueConverter> converter(new V8ValueConverterImpl());
|
|
|
|
|
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());
|
|
|
|
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
|
2013-04-23 12:57:14 +00:00
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(0, &value))
|
|
|
|
arguments.push_back(converter->ToV8Value(value, 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))
|
|
|
|
arguments.push_back(converter->ToV8Value(value, context));
|
|
|
|
}
|
|
|
|
|
|
|
|
node::MakeCallback(node::process, "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,
|
|
|
|
const std::string& channel,
|
|
|
|
const base::ListValue& args,
|
|
|
|
base::DictionaryValue* result) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
|
|
|
|
|
|
|
|
scoped_ptr<V8ValueConverter> converter(new V8ValueConverterImpl());
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> event = v8::Object::New();
|
|
|
|
|
|
|
|
// process.emit(channel, 'sync-message', event, process_id, routing_id);
|
|
|
|
std::vector<v8::Handle<v8::Value>> arguments;
|
|
|
|
arguments.reserve(3 + args.GetSize());
|
|
|
|
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
|
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(0, &value))
|
|
|
|
arguments.push_back(converter->ToV8Value(value, context));
|
|
|
|
arguments.push_back(event);
|
|
|
|
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))
|
|
|
|
arguments.push_back(converter->ToV8Value(value, context));
|
|
|
|
}
|
|
|
|
|
|
|
|
node::MakeCallback(node::process, "emit", arguments.size(), &arguments[0]);
|
|
|
|
|
|
|
|
scoped_ptr<base::Value> base_event(converter->FromV8Value(event, context));
|
|
|
|
DCHECK(base_event && base_event->IsType(base::Value::TYPE_DICTIONARY));
|
|
|
|
|
|
|
|
result->Swap(static_cast<base::DictionaryValue*>(base_event.get()));
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:53:26 +00:00
|
|
|
} // namespace atom
|