2013-04-13 10:39:09 +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 "common/node_bindings.h"
|
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/logging.h"
|
2013-07-22 06:58:25 +00:00
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2013-04-20 03:13:06 +00:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
2013-04-13 10:39:09 +00:00
|
|
|
#include "vendor/node/src/node.h"
|
|
|
|
#include "vendor/node/src/node_internals.h"
|
2013-04-20 03:13:06 +00:00
|
|
|
#include "vendor/node/src/node_javascript.h"
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2013-07-22 07:25:39 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2013-04-13 10:39:09 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void UvNoOp(uv_async_t* handle, int status) {
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-04-13 13:10:41 +00:00
|
|
|
NodeBindings::NodeBindings(bool is_browser)
|
2013-07-22 06:58:25 +00:00
|
|
|
: is_browser_(is_browser),
|
|
|
|
message_loop_(NULL),
|
|
|
|
uv_loop_(uv_default_loop()),
|
|
|
|
embed_closed_(false) {
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeBindings::~NodeBindings() {
|
2013-07-22 06:58:25 +00:00
|
|
|
// Clear uv.
|
|
|
|
embed_closed_ = true;
|
2013-07-23 08:55:09 +00:00
|
|
|
WakeupEmbedThread();
|
2013-07-22 06:58:25 +00:00
|
|
|
uv_thread_join(&embed_thread_);
|
|
|
|
uv_sem_destroy(&embed_sem_);
|
2013-07-29 12:50:03 +00:00
|
|
|
uv_timer_stop(&idle_timer_);
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::Initialize() {
|
|
|
|
CommandLine::StringVector str_argv = CommandLine::ForCurrentProcess()->argv();
|
|
|
|
|
2013-07-01 13:44:25 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
std::vector<std::string> utf8_str_argv;
|
|
|
|
utf8_str_argv.reserve(str_argv.size());
|
|
|
|
#endif
|
|
|
|
|
2013-04-13 10:39:09 +00:00
|
|
|
// Convert string vector to char* array.
|
|
|
|
std::vector<char*> argv(str_argv.size(), NULL);
|
2013-07-01 13:44:25 +00:00
|
|
|
for (size_t i = 0; i < str_argv.size(); ++i) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
utf8_str_argv.push_back(UTF16ToUTF8(str_argv[i]));
|
|
|
|
argv[i] = const_cast<char*>(utf8_str_argv[i].c_str());
|
|
|
|
#else
|
2013-04-13 10:39:09 +00:00
|
|
|
argv[i] = const_cast<char*>(str_argv[i].c_str());
|
2013-07-01 13:44:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2013-07-29 12:50:03 +00:00
|
|
|
// Init idle GC.
|
|
|
|
uv_timer_init(uv_default_loop(), &idle_timer_);
|
|
|
|
uv_timer_start(&idle_timer_, IdleCallback, 5000, 5000);
|
|
|
|
|
2013-04-13 15:38:13 +00:00
|
|
|
// Open node's error reporting system for browser process.
|
|
|
|
node::g_standalone_mode = is_browser_;
|
|
|
|
|
2013-04-13 10:39:09 +00:00
|
|
|
// Init node.
|
|
|
|
node::Init(argv.size(), &argv[0]);
|
|
|
|
v8::V8::Initialize();
|
|
|
|
|
|
|
|
// Load node.js.
|
|
|
|
v8::HandleScope scope;
|
2013-06-01 14:33:40 +00:00
|
|
|
node::g_context = v8::Persistent<v8::Context>::New(
|
|
|
|
node::node_isolate, v8::Context::New(node::node_isolate));
|
2013-04-13 10:39:09 +00:00
|
|
|
{
|
|
|
|
v8::Context::Scope context_scope(node::g_context);
|
|
|
|
v8::Handle<v8::Object> process = node::SetupProcessObject(
|
|
|
|
argv.size(), &argv[0]);
|
2013-04-13 13:10:41 +00:00
|
|
|
|
|
|
|
// Tell node.js we are in browser or renderer.
|
|
|
|
v8::Handle<v8::String> type =
|
|
|
|
is_browser_ ? v8::String::New("browser") : v8::String::New("renderer");
|
|
|
|
process->Set(v8::String::New("__atom_type"), type);
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-13 13:10:41 +00:00
|
|
|
|
2013-04-14 07:36:48 +00:00
|
|
|
void NodeBindings::Load() {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Context::Scope context_scope(node::g_context);
|
|
|
|
node::Load(node::process);
|
|
|
|
}
|
|
|
|
|
2013-04-20 03:13:06 +00:00
|
|
|
void NodeBindings::BindTo(WebKit::WebFrame* frame) {
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
|
|
|
|
if (context.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
v8::Context::Scope scope(context);
|
|
|
|
|
|
|
|
// Erase security token.
|
|
|
|
context->SetSecurityToken(node::g_context->GetSecurityToken());
|
|
|
|
|
|
|
|
// Evaluate cefode.js.
|
|
|
|
v8::Handle<v8::Script> script = node::CompileCefodeMainSource();
|
|
|
|
v8::Local<v8::Value> result = script->Run();
|
|
|
|
|
|
|
|
// Run the script of cefode.js.
|
|
|
|
std::string script_path(GURL(frame->document().url()).path());
|
|
|
|
v8::Handle<v8::Value> args[2] = {
|
|
|
|
v8::Local<v8::Value>::New(node::process),
|
|
|
|
v8::String::New(script_path.c_str(), script_path.size())
|
|
|
|
};
|
|
|
|
v8::Local<v8::Function>::Cast(result)->Call(context->Global(), 2, args);
|
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
void NodeBindings::PrepareMessageLoop() {
|
|
|
|
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
|
|
|
|
|
|
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
|
|
|
// nothing to do.
|
|
|
|
uv_async_init(uv_loop_, &dummy_uv_handle_, UvNoOp);
|
|
|
|
|
|
|
|
// Start worker that will interrupt main loop when having uv events.
|
|
|
|
uv_sem_init(&embed_sem_, 0);
|
|
|
|
uv_thread_create(&embed_thread_, EmbedThreadRunner, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::RunMessageLoop() {
|
|
|
|
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
|
|
|
|
|
|
// The MessageLoop should have been created, remember the one in main thread.
|
|
|
|
message_loop_ = base::MessageLoop::current();
|
|
|
|
|
|
|
|
// Run uv loop for once to give the uv__io_poll a chance to add all events.
|
|
|
|
UvRunOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::UvRunOnce() {
|
|
|
|
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
|
|
|
|
|
|
// Enter node context while dealing with uv events.
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Context::Scope context_scope(node::g_context);
|
|
|
|
|
|
|
|
// Deal with uv events.
|
|
|
|
int r = uv_run(uv_loop_, (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT));
|
|
|
|
if (r == 0 || uv_loop_->stop_flag != 0)
|
|
|
|
message_loop_->QuitWhenIdle(); // Quit from uv.
|
|
|
|
|
|
|
|
// Tell the worker thread to continue polling.
|
|
|
|
uv_sem_post(&embed_sem_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::WakeupMainThread() {
|
|
|
|
DCHECK(message_loop_);
|
|
|
|
message_loop_->PostTask(FROM_HERE, base::Bind(&NodeBindings::UvRunOnce,
|
|
|
|
base::Unretained(this)));
|
|
|
|
}
|
|
|
|
|
2013-07-23 05:08:40 +00:00
|
|
|
void NodeBindings::WakeupEmbedThread() {
|
|
|
|
uv_async_send(&dummy_uv_handle_);
|
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// static
|
|
|
|
void NodeBindings::EmbedThreadRunner(void *arg) {
|
|
|
|
NodeBindings* self = static_cast<NodeBindings*>(arg);
|
|
|
|
|
|
|
|
while (!self->embed_closed_) {
|
|
|
|
// Wait for the main loop to deal with events.
|
|
|
|
uv_sem_wait(&self->embed_sem_);
|
|
|
|
|
|
|
|
self->PollEvents();
|
|
|
|
|
|
|
|
// Deal with event in main thread.
|
|
|
|
self->WakeupMainThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 12:50:03 +00:00
|
|
|
// static
|
|
|
|
void NodeBindings::IdleCallback(uv_timer_t*, int) {
|
|
|
|
v8::V8::IdleNotification();
|
|
|
|
}
|
|
|
|
|
2013-04-13 13:10:41 +00:00
|
|
|
} // namespace atom
|