2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-13 10:39:09 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/node_bindings.h"
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-04-13 10:39:09 +00:00
|
|
|
#include "base/command_line.h"
|
2013-12-11 15:59:03 +00:00
|
|
|
#include "base/base_paths.h"
|
2014-04-21 15:49:53 +00:00
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/message_loop/message_loop.h"
|
2013-12-11 15:59:03 +00:00
|
|
|
#include "base/path_service.h"
|
2013-07-22 06:58:25 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2014-08-10 11:14:20 +00:00
|
|
|
#include "native_mate/locker.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
|
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2013-12-11 15:59:03 +00:00
|
|
|
// Forward declaration of internal node functions.
|
2013-12-11 07:48:19 +00:00
|
|
|
namespace node {
|
2013-12-15 06:20:28 +00:00
|
|
|
void Init(int*, const char**, int*, const char***);
|
|
|
|
void Load(Environment* env);
|
|
|
|
void SetupProcessObject(Environment*, int, const char* const*, int,
|
|
|
|
const char* const*);
|
2013-12-11 07:48:19 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 10:12:46 +00:00
|
|
|
// Force all builtin modules to be referenced so they can actually run their
|
|
|
|
// DSO constructors, see http://git.io/DRIqCg.
|
|
|
|
#define REFERENCE_MODULE(name) \
|
2014-06-30 06:16:16 +00:00
|
|
|
extern "C" void _register_ ## name(void); \
|
2014-06-29 10:12:46 +00:00
|
|
|
void (*fp_register_ ## name)(void) = _register_ ## name
|
|
|
|
// Node's builtin modules.
|
|
|
|
REFERENCE_MODULE(cares_wrap);
|
|
|
|
REFERENCE_MODULE(fs_event_wrap);
|
|
|
|
REFERENCE_MODULE(buffer);
|
|
|
|
REFERENCE_MODULE(contextify);
|
|
|
|
REFERENCE_MODULE(crypto);
|
|
|
|
REFERENCE_MODULE(fs);
|
|
|
|
REFERENCE_MODULE(http_parser);
|
|
|
|
REFERENCE_MODULE(os);
|
|
|
|
REFERENCE_MODULE(v8);
|
|
|
|
REFERENCE_MODULE(zlib);
|
|
|
|
REFERENCE_MODULE(pipe_wrap);
|
|
|
|
REFERENCE_MODULE(process_wrap);
|
|
|
|
REFERENCE_MODULE(signal_wrap);
|
|
|
|
REFERENCE_MODULE(smalloc);
|
|
|
|
REFERENCE_MODULE(spawn_sync);
|
|
|
|
REFERENCE_MODULE(tcp_wrap);
|
|
|
|
REFERENCE_MODULE(timer_wrap);
|
|
|
|
REFERENCE_MODULE(tls_wrap);
|
|
|
|
REFERENCE_MODULE(tty_wrap);
|
|
|
|
REFERENCE_MODULE(udp_wrap);
|
|
|
|
REFERENCE_MODULE(uv);
|
|
|
|
// Atom Shell's builtin modules.
|
|
|
|
REFERENCE_MODULE(atom_browser_app);
|
|
|
|
REFERENCE_MODULE(atom_browser_auto_updater);
|
2014-07-31 07:08:54 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_content_tracing);
|
2014-06-29 10:12:46 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_dialog);
|
|
|
|
REFERENCE_MODULE(atom_browser_menu);
|
|
|
|
REFERENCE_MODULE(atom_browser_power_monitor);
|
|
|
|
REFERENCE_MODULE(atom_browser_protocol);
|
2014-07-31 04:22:29 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_global_shortcut);
|
2014-06-29 10:12:46 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_tray);
|
2014-10-23 06:04:13 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_web_contents);
|
2014-06-29 10:12:46 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_window);
|
2014-09-24 03:10:07 +00:00
|
|
|
REFERENCE_MODULE(atom_common_asar);
|
2014-06-29 10:12:46 +00:00
|
|
|
REFERENCE_MODULE(atom_common_clipboard);
|
|
|
|
REFERENCE_MODULE(atom_common_crash_reporter);
|
|
|
|
REFERENCE_MODULE(atom_common_id_weak_map);
|
|
|
|
REFERENCE_MODULE(atom_common_screen);
|
|
|
|
REFERENCE_MODULE(atom_common_shell);
|
|
|
|
REFERENCE_MODULE(atom_common_v8_util);
|
|
|
|
REFERENCE_MODULE(atom_renderer_ipc);
|
2014-10-24 10:24:12 +00:00
|
|
|
REFERENCE_MODULE(atom_renderer_web_frame);
|
2014-06-29 10:12:46 +00:00
|
|
|
#undef REFERENCE_MODULE
|
|
|
|
|
2013-04-13 10:39:09 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
// Empty callback for async handle.
|
2014-06-28 14:33:00 +00:00
|
|
|
void UvNoOp(uv_async_t* handle) {
|
2013-07-22 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 21:09:50 +00:00
|
|
|
// Moved from node.cc.
|
|
|
|
void HandleCloseCb(uv_handle_t* handle) {
|
|
|
|
node::Environment* env = reinterpret_cast<node::Environment*>(handle->data);
|
|
|
|
env->FinishHandleCleanup(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleCleanup(node::Environment* env,
|
|
|
|
uv_handle_t* handle,
|
|
|
|
void* arg) {
|
|
|
|
handle->data = env;
|
|
|
|
uv_close(handle, HandleCloseCb);
|
|
|
|
}
|
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
// Convert the given vector to an array of C-strings. The strings in the
|
|
|
|
// returned vector are only guaranteed valid so long as the vector of strings
|
|
|
|
// is not modified.
|
|
|
|
scoped_ptr<const char*[]> StringVectorToArgArray(
|
|
|
|
const std::vector<std::string>& vector) {
|
|
|
|
scoped_ptr<const char*[]> array(new const char*[vector.size()]);
|
|
|
|
for (size_t i = 0; i < vector.size(); ++i)
|
|
|
|
array[i] = vector[i].c_str();
|
|
|
|
return array.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
std::vector<std::string> String16VectorToStringVector(
|
2014-07-11 04:19:01 +00:00
|
|
|
const std::vector<base::string16>& vector) {
|
2013-12-15 06:20:28 +00:00
|
|
|
std::vector<std::string> utf8_vector;
|
|
|
|
utf8_vector.reserve(vector.size());
|
|
|
|
for (size_t i = 0; i < vector.size(); ++i)
|
2014-07-11 04:19:01 +00:00
|
|
|
utf8_vector.push_back(base::UTF16ToUTF8(vector[i]));
|
2013-12-15 06:20:28 +00:00
|
|
|
return utf8_vector;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
node::Environment* global_env = NULL;
|
|
|
|
|
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()),
|
2014-01-08 03:55:54 +00:00
|
|
|
embed_closed_(false),
|
2014-01-09 13:35:29 +00:00
|
|
|
uv_env_(NULL),
|
2014-01-08 03:55:54 +00:00
|
|
|
weak_factory_(this) {
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeBindings::~NodeBindings() {
|
2013-11-11 08:57:40 +00:00
|
|
|
// Quit the embed thread.
|
2013-07-22 06:58:25 +00:00
|
|
|
embed_closed_ = true;
|
2013-11-11 08:57:40 +00:00
|
|
|
uv_sem_post(&embed_sem_);
|
2013-07-23 08:55:09 +00:00
|
|
|
WakeupEmbedThread();
|
2013-11-11 08:57:40 +00:00
|
|
|
|
|
|
|
// Wait for everything to be done.
|
2013-07-22 06:58:25 +00:00
|
|
|
uv_thread_join(&embed_thread_);
|
2013-11-11 08:57:40 +00:00
|
|
|
|
|
|
|
// Clear uv.
|
2013-07-22 06:58:25 +00:00
|
|
|
uv_sem_destroy(&embed_sem_);
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::Initialize() {
|
2013-12-15 06:20:28 +00:00
|
|
|
// Open node's error reporting system for browser process.
|
|
|
|
node::g_standalone_mode = is_browser_;
|
|
|
|
node::g_upstream_node_mode = false;
|
2013-07-01 13:44:25 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
// Init node.
|
|
|
|
// (we assume it would not node::Init would not modify the parameters under
|
|
|
|
// embedded mode).
|
|
|
|
node::Init(NULL, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
node::Environment* NodeBindings::CreateEnvironment(
|
|
|
|
v8::Handle<v8::Context> context) {
|
|
|
|
std::vector<std::string> args =
|
2013-07-01 13:44:25 +00:00
|
|
|
#if defined(OS_WIN)
|
2013-12-15 06:20:28 +00:00
|
|
|
String16VectorToStringVector(CommandLine::ForCurrentProcess()->argv());
|
2013-07-01 13:44:25 +00:00
|
|
|
#else
|
2013-12-15 06:20:28 +00:00
|
|
|
CommandLine::ForCurrentProcess()->argv();
|
2013-07-01 13:44:25 +00:00
|
|
|
#endif
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2013-12-11 15:59:03 +00:00
|
|
|
// Feed node the path to initialization script.
|
2013-12-17 06:01:40 +00:00
|
|
|
base::FilePath exec_path(CommandLine::ForCurrentProcess()->argv()[0]);
|
2013-12-11 15:59:03 +00:00
|
|
|
PathService::Get(base::FILE_EXE, &exec_path);
|
|
|
|
base::FilePath resources_path =
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
is_browser_ ? exec_path.DirName().DirName().Append("Resources") :
|
|
|
|
exec_path.DirName().DirName().DirName().DirName().DirName()
|
|
|
|
.Append("Resources");
|
|
|
|
#else
|
2013-12-17 06:01:40 +00:00
|
|
|
exec_path.DirName().AppendASCII("resources");
|
2013-12-11 15:59:03 +00:00
|
|
|
#endif
|
|
|
|
base::FilePath script_path =
|
2014-03-16 05:24:25 +00:00
|
|
|
resources_path.AppendASCII("atom")
|
|
|
|
.AppendASCII(is_browser_ ? "browser" : "renderer")
|
2013-12-15 06:20:28 +00:00
|
|
|
.AppendASCII("lib")
|
|
|
|
.AppendASCII("init.js");
|
2013-12-11 15:59:03 +00:00
|
|
|
std::string script_path_str = script_path.AsUTF8Unsafe();
|
|
|
|
args.insert(args.begin() + 1, script_path_str.c_str());
|
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
// Convert string vector to const char* array.
|
|
|
|
scoped_ptr<const char*[]> c_argv = StringVectorToArgArray(args);
|
2013-04-13 15:38:13 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
// Construct the parameters that passed to node::CreateEnvironment:
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2014-12-07 21:09:50 +00:00
|
|
|
uv_loop_t* loop = uv_default_loop();
|
2013-12-11 07:48:19 +00:00
|
|
|
int argc = args.size();
|
2013-12-15 06:20:28 +00:00
|
|
|
const char** argv = c_argv.get();
|
|
|
|
int exec_argc = 0;
|
|
|
|
const char** exec_argv = NULL;
|
|
|
|
|
|
|
|
using namespace v8; // NOLINT
|
|
|
|
using namespace node; // NOLINT
|
|
|
|
|
|
|
|
// Following code are stripped from node::CreateEnvironment in node.cc:
|
|
|
|
HandleScope handle_scope(isolate);
|
2014-12-07 21:09:50 +00:00
|
|
|
|
2013-12-17 13:55:56 +00:00
|
|
|
Context::Scope context_scope(context);
|
2014-12-07 21:09:50 +00:00
|
|
|
Environment* env = Environment::New(context, loop);
|
2013-12-15 06:20:28 +00:00
|
|
|
|
2014-12-07 21:09:50 +00:00
|
|
|
isolate->SetAutorunMicrotasks(false);
|
2013-04-13 13:10:41 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
uv_check_init(env->event_loop(), env->immediate_check_handle());
|
|
|
|
uv_unref(
|
|
|
|
reinterpret_cast<uv_handle_t*>(env->immediate_check_handle()));
|
2014-12-07 21:09:50 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
uv_idle_init(env->event_loop(), env->immediate_idle_handle());
|
2013-04-20 03:13:06 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
uv_prepare_init(env->event_loop(), env->idle_prepare_handle());
|
|
|
|
uv_check_init(env->event_loop(), env->idle_check_handle());
|
|
|
|
uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_prepare_handle()));
|
|
|
|
uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_check_handle()));
|
2013-04-20 03:13:06 +00:00
|
|
|
|
2014-12-07 21:09:50 +00:00
|
|
|
// Register handle cleanups
|
|
|
|
env->RegisterHandleCleanup(
|
|
|
|
reinterpret_cast<uv_handle_t*>(env->immediate_check_handle()),
|
|
|
|
HandleCleanup,
|
|
|
|
nullptr);
|
|
|
|
env->RegisterHandleCleanup(
|
|
|
|
reinterpret_cast<uv_handle_t*>(env->immediate_idle_handle()),
|
|
|
|
HandleCleanup,
|
|
|
|
nullptr);
|
|
|
|
env->RegisterHandleCleanup(
|
|
|
|
reinterpret_cast<uv_handle_t*>(env->idle_prepare_handle()),
|
|
|
|
HandleCleanup,
|
|
|
|
nullptr);
|
|
|
|
env->RegisterHandleCleanup(
|
|
|
|
reinterpret_cast<uv_handle_t*>(env->idle_check_handle()),
|
|
|
|
HandleCleanup,
|
|
|
|
nullptr);
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
|
2013-12-15 06:20:28 +00:00
|
|
|
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
|
2013-04-20 03:13:06 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
Local<Object> process_object = process_template->GetFunction()->NewInstance();
|
|
|
|
env->set_process_object(process_object);
|
2013-04-20 03:13:06 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
SetupProcessObject(env, argc, argv, exec_argc, exec_argv);
|
2014-12-07 21:09:50 +00:00
|
|
|
LoadEnvironment(env);
|
2013-04-20 03:13:06 +00:00
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
return env;
|
2013-04-20 03:13:06 +00:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
// By default the global env would be used unless user specified another one
|
|
|
|
// (this happens for renderer process, which wraps the uv loop with web page
|
|
|
|
// context).
|
|
|
|
node::Environment* env = uv_env() ? uv_env() : global_env;
|
2014-01-27 07:14:11 +00:00
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
// Use Locker in browser process.
|
2014-08-10 11:14:20 +00:00
|
|
|
mate::Locker locker(env->isolate());
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::HandleScope handle_scope(env->isolate());
|
2014-01-09 13:35:29 +00:00
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
// Enter node context while dealing with uv events.
|
2014-01-09 13:35:29 +00:00
|
|
|
v8::Context::Scope context_scope(env->context());
|
2013-07-22 06:58:25 +00:00
|
|
|
|
|
|
|
// 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,
|
2014-01-08 03:55:54 +00:00
|
|
|
weak_factory_.GetWeakPtr()));
|
2013-07-22 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2014-01-08 02:30:30 +00:00
|
|
|
while (true) {
|
2013-07-22 06:58:25 +00:00
|
|
|
// Wait for the main loop to deal with events.
|
|
|
|
uv_sem_wait(&self->embed_sem_);
|
2014-01-08 02:30:30 +00:00
|
|
|
if (self->embed_closed_)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Wait for something to happen in uv loop.
|
|
|
|
// Note that the PollEvents() is implemented by derived classes, so when
|
|
|
|
// this class is being destructed the PollEvents() would not be available
|
|
|
|
// anymore. Because of it we must make sure we only invoke PollEvents()
|
|
|
|
// when this class is alive.
|
2013-07-22 06:58:25 +00:00
|
|
|
self->PollEvents();
|
2014-01-08 02:30:30 +00:00
|
|
|
if (self->embed_closed_)
|
|
|
|
break;
|
2013-07-22 06:58:25 +00:00
|
|
|
|
|
|
|
// Deal with event in main thread.
|
|
|
|
self->WakeupMainThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 13:10:41 +00:00
|
|
|
} // namespace atom
|