2014-10-31 21:17:05 +03:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 13:49:37 +04:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-13 14:39:09 +04:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 04:30:26 +04:00
|
|
|
#include "atom/common/node_bindings.h"
|
2013-04-13 14:39:09 +04:00
|
|
|
|
2014-03-16 05:13:06 +04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-08-07 14:34:00 +03:00
|
|
|
#include "atom/common/api/event_emitter_caller.h"
|
|
|
|
#include "atom/common/api/locker.h"
|
2015-06-19 18:11:53 +03:00
|
|
|
#include "atom/common/atom_command_line.h"
|
2015-02-13 08:32:58 +03:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2015-09-07 11:29:54 +03:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-04-13 14:39:09 +04:00
|
|
|
#include "base/command_line.h"
|
2013-12-11 19:59:03 +04:00
|
|
|
#include "base/base_paths.h"
|
2015-11-06 13:51:34 +03:00
|
|
|
#include "base/environment.h"
|
2014-04-21 19:49:53 +04:00
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/message_loop/message_loop.h"
|
2013-12-11 19:59:03 +04:00
|
|
|
#include "base/path_service.h"
|
2013-07-22 10:58:25 +04:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-04-14 09:05:30 +03:00
|
|
|
#include "content/public/common/content_paths.h"
|
2015-02-13 08:32:58 +03:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-08-07 10:10:14 +03:00
|
|
|
#include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
|
2013-04-13 14:39:09 +04:00
|
|
|
|
2013-07-22 10:58:25 +04:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2014-06-29 14:12:46 +04: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 10:16:16 +04:00
|
|
|
extern "C" void _register_ ## name(void); \
|
2014-06-29 14:12:46 +04:00
|
|
|
void (*fp_register_ ## name)(void) = _register_ ## name
|
2015-04-08 20:16:10 +03:00
|
|
|
// Electron's builtin modules.
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_app);
|
|
|
|
REFERENCE_MODULE(atom_browser_auto_updater);
|
2014-07-31 11:08:54 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_content_tracing);
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_dialog);
|
2016-01-23 07:02:21 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_debugger);
|
2015-10-02 12:50:10 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_desktop_capturer);
|
2015-09-20 13:56:10 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_download_item);
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_menu);
|
|
|
|
REFERENCE_MODULE(atom_browser_power_monitor);
|
2015-06-21 15:57:42 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_power_save_blocker);
|
2016-02-24 08:25:30 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_protocol);
|
2014-07-31 08:22:29 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_global_shortcut);
|
2015-09-01 13:02:14 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_session);
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_tray);
|
2014-10-23 10:04:13 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_web_contents);
|
2015-02-05 02:39:41 +03:00
|
|
|
REFERENCE_MODULE(atom_browser_web_view_manager);
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_browser_window);
|
2014-09-24 07:10:07 +04:00
|
|
|
REFERENCE_MODULE(atom_common_asar);
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_common_clipboard);
|
|
|
|
REFERENCE_MODULE(atom_common_crash_reporter);
|
2015-10-29 14:22:12 +03:00
|
|
|
REFERENCE_MODULE(atom_common_id_weak_map);
|
2015-02-11 11:03:19 +03:00
|
|
|
REFERENCE_MODULE(atom_common_native_image);
|
2014-06-29 14:12:46 +04:00
|
|
|
REFERENCE_MODULE(atom_common_screen);
|
|
|
|
REFERENCE_MODULE(atom_common_shell);
|
|
|
|
REFERENCE_MODULE(atom_common_v8_util);
|
|
|
|
REFERENCE_MODULE(atom_renderer_ipc);
|
2014-10-24 14:24:12 +04:00
|
|
|
REFERENCE_MODULE(atom_renderer_web_frame);
|
2014-06-29 14:12:46 +04:00
|
|
|
#undef REFERENCE_MODULE
|
|
|
|
|
2015-04-10 10:35:25 +03:00
|
|
|
// The "v8::Function::kLineOffsetNotFound" is exported in node.dll, but the
|
|
|
|
// linker can not find it, could be a bug of VS.
|
|
|
|
#if defined(OS_WIN) && !defined(DEBUG)
|
|
|
|
namespace v8 {
|
|
|
|
const int Function::kLineOffsetNotFound = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-13 14:39:09 +04:00
|
|
|
namespace atom {
|
|
|
|
|
2013-07-22 10:58:25 +04:00
|
|
|
namespace {
|
|
|
|
|
2013-12-15 10:20:28 +04:00
|
|
|
// Empty callback for async handle.
|
2014-06-28 18:33:00 +04:00
|
|
|
void UvNoOp(uv_async_t* handle) {
|
2013-07-22 10:58:25 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 14:53:47 +03: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();
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:59:16 +03:00
|
|
|
base::FilePath GetResourcesPath(bool is_browser) {
|
|
|
|
auto command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
base::FilePath exec_path(command_line->GetProgram());
|
2015-02-13 08:32:58 +03:00
|
|
|
PathService::Get(base::FILE_EXE, &exec_path);
|
2015-03-29 14:59:16 +03:00
|
|
|
|
2015-02-13 08:32:58 +03:00
|
|
|
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
|
|
|
|
exec_path.DirName().Append(FILE_PATH_LITERAL("resources"));
|
|
|
|
#endif
|
|
|
|
return resources_path;
|
|
|
|
}
|
|
|
|
|
2013-07-22 10:58:25 +04:00
|
|
|
} // namespace
|
|
|
|
|
2015-01-24 08:02:31 +03:00
|
|
|
node::Environment* global_env = nullptr;
|
2013-12-11 11:48:19 +04:00
|
|
|
|
2013-04-13 17:10:41 +04:00
|
|
|
NodeBindings::NodeBindings(bool is_browser)
|
2013-07-22 10:58:25 +04:00
|
|
|
: is_browser_(is_browser),
|
2015-01-24 08:02:31 +03:00
|
|
|
message_loop_(nullptr),
|
2013-07-22 10:58:25 +04:00
|
|
|
uv_loop_(uv_default_loop()),
|
2014-01-08 07:55:54 +04:00
|
|
|
embed_closed_(false),
|
2015-01-24 08:02:31 +03:00
|
|
|
uv_env_(nullptr),
|
2014-01-08 07:55:54 +04:00
|
|
|
weak_factory_(this) {
|
2013-04-13 14:39:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeBindings::~NodeBindings() {
|
2013-11-11 12:57:40 +04:00
|
|
|
// Quit the embed thread.
|
2013-07-22 10:58:25 +04:00
|
|
|
embed_closed_ = true;
|
2013-11-11 12:57:40 +04:00
|
|
|
uv_sem_post(&embed_sem_);
|
2013-07-23 12:55:09 +04:00
|
|
|
WakeupEmbedThread();
|
2013-11-11 12:57:40 +04:00
|
|
|
|
|
|
|
// Wait for everything to be done.
|
2013-07-22 10:58:25 +04:00
|
|
|
uv_thread_join(&embed_thread_);
|
2013-11-11 12:57:40 +04:00
|
|
|
|
|
|
|
// Clear uv.
|
2013-07-22 10:58:25 +04:00
|
|
|
uv_sem_destroy(&embed_sem_);
|
2013-04-13 14:39:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::Initialize() {
|
2013-12-15 10:20:28 +04: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 17:44:25 +04:00
|
|
|
|
2015-06-19 17:56:10 +03:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Get real command line in renderer process forked by zygote.
|
|
|
|
if (!is_browser_)
|
|
|
|
AtomCommandLine::InitializeFromCommandLine();
|
|
|
|
#endif
|
|
|
|
|
2013-12-15 10:20:28 +04:00
|
|
|
// Init node.
|
2015-04-20 09:35:57 +03:00
|
|
|
// (we assume node::Init would not modify the parameters under embedded mode).
|
2015-01-24 08:02:31 +03:00
|
|
|
node::Init(nullptr, nullptr, nullptr, nullptr);
|
2015-11-06 13:51:34 +03:00
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// uv_init overrides error mode to suppress the default crash dialog, bring
|
|
|
|
// it back if user wants to show it.
|
|
|
|
scoped_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
if (env->HasVar("ELECTRON_DEFAULT_ERROR_MODE"))
|
|
|
|
SetErrorMode(0);
|
|
|
|
#endif
|
2013-12-15 10:20:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
node::Environment* NodeBindings::CreateEnvironment(
|
|
|
|
v8::Handle<v8::Context> context) {
|
2015-03-17 15:55:53 +03:00
|
|
|
auto args = AtomCommandLine::argv();
|
2013-04-13 14:39:09 +04:00
|
|
|
|
2013-12-11 19:59:03 +04:00
|
|
|
// Feed node the path to initialization script.
|
2015-02-27 23:21:19 +03:00
|
|
|
base::FilePath::StringType process_type = is_browser_ ?
|
|
|
|
FILE_PATH_LITERAL("browser") : FILE_PATH_LITERAL("renderer");
|
2015-03-29 14:59:16 +03:00
|
|
|
base::FilePath resources_path = GetResourcesPath(is_browser_);
|
2013-12-11 19:59:03 +04:00
|
|
|
base::FilePath script_path =
|
2015-02-13 08:05:51 +03:00
|
|
|
resources_path.Append(FILE_PATH_LITERAL("atom.asar"))
|
2015-02-27 23:21:19 +03:00
|
|
|
.Append(process_type)
|
2015-01-24 08:12:45 +03:00
|
|
|
.Append(FILE_PATH_LITERAL("lib"))
|
|
|
|
.Append(FILE_PATH_LITERAL("init.js"));
|
2013-12-11 19:59:03 +04:00
|
|
|
std::string script_path_str = script_path.AsUTF8Unsafe();
|
|
|
|
args.insert(args.begin() + 1, script_path_str.c_str());
|
|
|
|
|
2015-03-29 14:53:47 +03:00
|
|
|
scoped_ptr<const char*[]> c_argv = StringVectorToArgArray(args);
|
2015-03-29 14:59:16 +03:00
|
|
|
node::Environment* env = node::CreateEnvironment(
|
2015-02-13 08:32:58 +03:00
|
|
|
context->GetIsolate(), uv_default_loop(), context,
|
2015-03-29 14:53:47 +03:00
|
|
|
args.size(), c_argv.get(), 0, nullptr);
|
2015-02-13 08:32:58 +03:00
|
|
|
|
|
|
|
mate::Dictionary process(context->GetIsolate(), env->process_object());
|
|
|
|
process.Set("type", process_type);
|
|
|
|
process.Set("resourcesPath", resources_path);
|
2015-04-14 09:05:30 +03:00
|
|
|
// The path to helper app.
|
|
|
|
base::FilePath helper_exec_path;
|
|
|
|
PathService::Get(content::CHILD_PROCESS_EXE, &helper_exec_path);
|
|
|
|
process.Set("helperExecPath", helper_exec_path);
|
2015-02-13 08:32:58 +03:00
|
|
|
return env;
|
2013-04-20 07:13:06 +04:00
|
|
|
}
|
|
|
|
|
2015-01-22 01:00:19 +03:00
|
|
|
void NodeBindings::LoadEnvironment(node::Environment* env) {
|
|
|
|
node::LoadEnvironment(env);
|
2015-06-23 15:14:03 +03:00
|
|
|
mate::EmitEvent(env->isolate(), env->process_object(), "loaded");
|
2015-01-22 01:00:19 +03:00
|
|
|
}
|
|
|
|
|
2013-07-22 10:58:25 +04: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 18:33:00 +04: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 11:14:11 +04:00
|
|
|
|
2014-06-28 18:33:00 +04:00
|
|
|
// Use Locker in browser process.
|
2014-08-10 15:14:20 +04:00
|
|
|
mate::Locker locker(env->isolate());
|
2014-06-28 18:33:00 +04:00
|
|
|
v8::HandleScope handle_scope(env->isolate());
|
2014-01-09 17:35:29 +04:00
|
|
|
|
2014-06-28 18:33:00 +04:00
|
|
|
// Enter node context while dealing with uv events.
|
2014-01-09 17:35:29 +04:00
|
|
|
v8::Context::Scope context_scope(env->context());
|
2013-07-22 10:58:25 +04:00
|
|
|
|
2015-08-07 10:10:14 +03:00
|
|
|
// Perform microtask checkpoint after running JavaScript.
|
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
2015-12-07 14:56:23 +03:00
|
|
|
is_browser_ ? nullptr : new blink::WebScopedRunV8Script);
|
2015-08-07 10:10:14 +03:00
|
|
|
|
2013-07-22 10:58:25 +04:00
|
|
|
// Deal with uv events.
|
2015-01-13 03:17:44 +03:00
|
|
|
int r = uv_run(uv_loop_, UV_RUN_NOWAIT);
|
2013-07-22 10:58:25 +04:00
|
|
|
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 07:55:54 +04:00
|
|
|
weak_factory_.GetWeakPtr()));
|
2013-07-22 10:58:25 +04:00
|
|
|
}
|
|
|
|
|
2013-07-23 09:08:40 +04:00
|
|
|
void NodeBindings::WakeupEmbedThread() {
|
|
|
|
uv_async_send(&dummy_uv_handle_);
|
|
|
|
}
|
|
|
|
|
2013-07-22 10:58:25 +04:00
|
|
|
// static
|
|
|
|
void NodeBindings::EmbedThreadRunner(void *arg) {
|
|
|
|
NodeBindings* self = static_cast<NodeBindings*>(arg);
|
|
|
|
|
2014-01-08 06:30:30 +04:00
|
|
|
while (true) {
|
2013-07-22 10:58:25 +04:00
|
|
|
// Wait for the main loop to deal with events.
|
|
|
|
uv_sem_wait(&self->embed_sem_);
|
2014-01-08 06:30:30 +04: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 10:58:25 +04:00
|
|
|
self->PollEvents();
|
2014-01-08 06:30:30 +04:00
|
|
|
if (self->embed_closed_)
|
|
|
|
break;
|
2013-07-22 10:58:25 +04:00
|
|
|
|
|
|
|
// Deal with event in main thread.
|
|
|
|
self->WakeupMainThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 17:10:41 +04:00
|
|
|
} // namespace atom
|