2013-04-22 08:27:34 +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 "renderer/api/atom_renderer_bindings.h"
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-04-22 08:27:34 +00:00
|
|
|
#include "base/logging.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "base/values.h"
|
|
|
|
#include "content/public/renderer/render_view.h"
|
|
|
|
#include "content/public/renderer/v8_value_converter.h"
|
2013-04-22 08:27:34 +00:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
2013-04-22 13:32:48 +00:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
|
|
|
|
#include "vendor/node/src/node.h"
|
|
|
|
|
|
|
|
using content::RenderView;
|
|
|
|
using content::V8ValueConverter;
|
|
|
|
using WebKit::WebFrame;
|
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
|
|
|
|
|
|
|
|
AtomRendererBindings::AtomRendererBindings(RenderView* render_view)
|
|
|
|
: render_view_(render_view) {
|
2013-04-22 08:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomRendererBindings::~AtomRendererBindings() {
|
|
|
|
}
|
|
|
|
|
2013-04-22 13:32:48 +00:00
|
|
|
void AtomRendererBindings::BindToFrame(WebFrame* frame) {
|
2013-04-22 08:27:34 +00:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
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-04-23 12:57:14 +00:00
|
|
|
void AtomRendererBindings::OnBrowserMessage(const std::string& channel,
|
|
|
|
const base::ListValue& args) {
|
2013-04-23 04:18:07 +00:00
|
|
|
if (!render_view_->GetWebView())
|
2013-04-22 13:32:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
render_view_->GetWebView()->mainFrame()->mainWorldScriptContext();
|
|
|
|
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-04-22 13:32:48 +00:00
|
|
|
scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
std::vector<v8::Handle<v8::Value>> arguments;
|
|
|
|
arguments.reserve(1 + args.GetSize());
|
|
|
|
arguments.push_back(v8::String::New(channel.c_str(), channel.size()));
|
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
|