2013-04-22 08:27:34 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-22 08:27:34 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/renderer/api/atom_renderer_bindings.h"
|
2013-04-22 08:27:34 +00:00
|
|
|
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-21 15:49:53 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "base/values.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "content/public/renderer/render_view.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebFrame.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebView.h"
|
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2013-04-22 08:27:34 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2013-04-22 13:32:48 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> GetProcessObject(v8::Handle<v8::Context> context) {
|
|
|
|
v8::Handle<v8::Object> process =
|
|
|
|
context->Global()->Get(v8::String::New("process"))->ToObject();
|
|
|
|
DCHECK(!process.IsEmpty());
|
|
|
|
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-12-23 14:08:45 +00:00
|
|
|
AtomRendererBindings::AtomRendererBindings() {
|
2013-04-22 08:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomRendererBindings::~AtomRendererBindings() {
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:08:45 +00:00
|
|
|
void AtomRendererBindings::BindToFrame(WebKit::WebFrame* frame) {
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-04-22 08:27:34 +00:00
|
|
|
|
|
|
|
v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
|
|
|
|
if (context.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
v8::Context::Scope scope(context);
|
2013-04-22 13:32:48 +00:00
|
|
|
AtomBindings::BindTo(GetProcessObject(context));
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:08:45 +00:00
|
|
|
void AtomRendererBindings::OnBrowserMessage(content::RenderView* render_view,
|
|
|
|
const string16& channel,
|
2013-04-23 12:57:14 +00:00
|
|
|
const base::ListValue& args) {
|
2013-12-23 14:08:45 +00:00
|
|
|
if (!render_view->GetWebView())
|
2013-04-22 13:32:48 +00:00
|
|
|
return;
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
v8::Local<v8::Context> context =
|
2013-12-23 14:08:45 +00:00
|
|
|
render_view->GetWebView()->mainFrame()->mainWorldScriptContext();
|
2013-04-23 04:18:07 +00:00
|
|
|
if (context.IsEmpty())
|
|
|
|
return;
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
v8::Handle<v8::Object> process = GetProcessObject(context);
|
2013-12-23 14:42:21 +00:00
|
|
|
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
std::vector<v8::Handle<v8::Value>> arguments;
|
|
|
|
arguments.reserve(1 + args.GetSize());
|
2014-04-21 15:49:53 +00:00
|
|
|
arguments.push_back(mate::ConvertToV8(node_isolate, channel));
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
for (size_t i = 0; i < args.GetSize(); i++) {
|
|
|
|
const base::Value* value;
|
|
|
|
if (args.Get(i, &value))
|
|
|
|
arguments.push_back(converter->ToV8Value(value, context));
|
|
|
|
}
|
2013-04-22 08:27:34 +00:00
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
node::MakeCallback(process, "emit", arguments.size(), &arguments[0]);
|
2013-04-22 08:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|