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>
|
|
|
|
|
2015-08-07 11:34:00 +00:00
|
|
|
#include "atom/common/api/event_emitter_caller.h"
|
|
|
|
#include "atom/common/api/locker.h"
|
2015-06-19 15:11:53 +00:00
|
|
|
#include "atom/common/atom_command_line.h"
|
2015-02-13 05:32:58 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2013-12-11 15:59:03 +00:00
|
|
|
#include "base/base_paths.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "base/command_line.h"
|
2015-11-06 10:51:34 +00:00
|
|
|
#include "base/environment.h"
|
2014-04-21 15:49:53 +00:00
|
|
|
#include "base/files/file_path.h"
|
2013-12-11 15:59:03 +00:00
|
|
|
#include "base/path_service.h"
|
2016-11-30 07:30:03 +00:00
|
|
|
#include "base/run_loop.h"
|
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2016-12-07 11:22:08 +00:00
|
|
|
#include "base/trace_event/trace_event.h"
|
2013-07-22 06:58:25 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-04-14 06:05:30 +00:00
|
|
|
#include "content/public/common/content_paths.h"
|
2015-02-13 05:32:58 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2016-07-21 07:44:30 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
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
|
2015-04-08 17:16:10 +00:00
|
|
|
// Electron's builtin modules.
|
2014-06-29 10:12:46 +00:00
|
|
|
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);
|
2016-01-23 04:02:21 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_debugger);
|
2015-10-02 09:50:10 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_desktop_capturer);
|
2015-09-20 10:56:10 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_download_item);
|
2014-06-29 10:12:46 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_menu);
|
2016-09-15 13:59:40 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_net);
|
2014-06-29 10:12:46 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_power_monitor);
|
2015-06-21 12:57:42 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_power_save_blocker);
|
2016-02-24 05:25:30 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_protocol);
|
2014-07-31 04:22:29 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_global_shortcut);
|
2016-05-26 23:51:02 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_render_process_preferences);
|
2015-09-01 10:02:14 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_session);
|
2016-04-24 12:13:46 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_system_preferences);
|
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);
|
2015-02-04 23:39:41 +00:00
|
|
|
REFERENCE_MODULE(atom_browser_web_view_manager);
|
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);
|
2015-02-11 08:03:19 +00:00
|
|
|
REFERENCE_MODULE(atom_common_native_image);
|
2014-06-29 10:12:46 +00: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 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 {
|
|
|
|
|
2015-03-29 11:53:47 +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.
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<const char*[]> StringVectorToArgArray(
|
2015-03-29 11:53:47 +00:00
|
|
|
const std::vector<std::string>& vector) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<const char*[]> array(new const char*[vector.size()]);
|
2015-03-29 11:53:47 +00:00
|
|
|
for (size_t i = 0; i < vector.size(); ++i) {
|
|
|
|
array[i] = vector[i].c_str();
|
|
|
|
}
|
2016-03-08 14:28:53 +00:00
|
|
|
return array;
|
2015-03-29 11:53:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 11:59:16 +00:00
|
|
|
base::FilePath GetResourcesPath(bool is_browser) {
|
|
|
|
auto command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
base::FilePath exec_path(command_line->GetProgram());
|
2015-02-13 05:32:58 +00:00
|
|
|
PathService::Get(base::FILE_EXE, &exec_path);
|
2015-03-29 11:59:16 +00:00
|
|
|
|
2015-02-13 05:32:58 +00: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 06:58:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-03-08 08:33:44 +00:00
|
|
|
NodeBindings::NodeBindings(BrowserEnvironment browser_env)
|
|
|
|
: browser_env_(browser_env),
|
2017-03-10 06:22:25 +00:00
|
|
|
uv_loop_(browser_env == WORKER ? uv_loop_new() : uv_default_loop()),
|
2014-01-08 03:55:54 +00:00
|
|
|
embed_closed_(false),
|
2015-01-24 05:02:31 +00:00
|
|
|
uv_env_(nullptr),
|
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_);
|
2017-02-28 01:58:52 +00:00
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(&dummy_uv_handle_), nullptr);
|
2017-03-10 08:07:51 +00:00
|
|
|
|
|
|
|
// Destroy loop.
|
|
|
|
if (uv_loop_ != uv_default_loop())
|
|
|
|
uv_loop_delete(uv_loop_);
|
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.
|
2017-03-08 08:33:44 +00:00
|
|
|
node::g_standalone_mode = browser_env_ == BROWSER;
|
2013-12-15 06:20:28 +00:00
|
|
|
node::g_upstream_node_mode = false;
|
2013-07-01 13:44:25 +00:00
|
|
|
|
2015-06-19 14:56:10 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Get real command line in renderer process forked by zygote.
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ != BROWSER)
|
2015-06-19 14:56:10 +00:00
|
|
|
AtomCommandLine::InitializeFromCommandLine();
|
|
|
|
#endif
|
|
|
|
|
2013-12-15 06:20:28 +00:00
|
|
|
// Init node.
|
2015-04-20 06:35:57 +00:00
|
|
|
// (we assume node::Init would not modify the parameters under embedded mode).
|
2015-01-24 05:02:31 +00:00
|
|
|
node::Init(nullptr, nullptr, nullptr, nullptr);
|
2015-11-06 10:51:34 +00: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.
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ == BROWSER || env->HasVar("ELECTRON_DEFAULT_ERROR_MODE"))
|
2016-09-16 21:01:33 +00:00
|
|
|
SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX);
|
2015-11-06 10:51:34 +00:00
|
|
|
#endif
|
2013-12-15 06:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node::Environment* NodeBindings::CreateEnvironment(
|
2017-01-19 00:41:36 +00:00
|
|
|
v8::Handle<v8::Context> context) {
|
2015-03-17 12:55:53 +00:00
|
|
|
auto args = AtomCommandLine::argv();
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2013-12-11 15:59:03 +00:00
|
|
|
// Feed node the path to initialization script.
|
2017-03-08 08:33:44 +00:00
|
|
|
base::FilePath::StringType process_type;
|
|
|
|
switch (browser_env_) {
|
|
|
|
case BROWSER:
|
|
|
|
process_type = FILE_PATH_LITERAL("browser");
|
|
|
|
break;
|
|
|
|
case RENDERER:
|
|
|
|
process_type = FILE_PATH_LITERAL("renderer");
|
|
|
|
break;
|
|
|
|
case WORKER:
|
|
|
|
process_type = FILE_PATH_LITERAL("worker");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
base::FilePath resources_path = GetResourcesPath(browser_env_ == BROWSER);
|
2013-12-11 15:59:03 +00:00
|
|
|
base::FilePath script_path =
|
2016-03-31 16:26:11 +00:00
|
|
|
resources_path.Append(FILE_PATH_LITERAL("electron.asar"))
|
2015-02-27 20:21:19 +00:00
|
|
|
.Append(process_type)
|
2015-01-24 05:12:45 +00:00
|
|
|
.Append(FILE_PATH_LITERAL("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());
|
|
|
|
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<const char*[]> c_argv = StringVectorToArgArray(args);
|
2015-03-29 11:59:16 +00:00
|
|
|
node::Environment* env = node::CreateEnvironment(
|
2017-03-10 06:22:25 +00:00
|
|
|
new node::IsolateData(context->GetIsolate(), uv_loop_), context,
|
2015-03-29 11:53:47 +00:00
|
|
|
args.size(), c_argv.get(), 0, nullptr);
|
2015-02-13 05:32:58 +00:00
|
|
|
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ == BROWSER) {
|
2017-01-25 17:24:24 +00:00
|
|
|
// SetAutorunMicrotasks is no longer called in node::CreateEnvironment
|
|
|
|
// so instead call it here to match expected node behavior
|
|
|
|
context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
|
|
|
|
} else {
|
|
|
|
// Node uses the deprecated SetAutorunMicrotasks(false) mode, we should
|
|
|
|
// switch to use the scoped policy to match blink's behavior.
|
2016-08-22 10:34:59 +00:00
|
|
|
context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
|
|
|
|
}
|
2016-08-03 08:57:10 +00:00
|
|
|
|
2015-02-13 05:32:58 +00:00
|
|
|
mate::Dictionary process(context->GetIsolate(), env->process_object());
|
|
|
|
process.Set("type", process_type);
|
|
|
|
process.Set("resourcesPath", resources_path);
|
2016-04-03 06:05:47 +00:00
|
|
|
// Do not set DOM globals for renderer process.
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ != BROWSER)
|
2016-04-03 06:05:47 +00:00
|
|
|
process.Set("_noBrowserGlobals", resources_path);
|
2015-04-14 06:05:30 +00: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);
|
2016-08-26 19:06:15 +00:00
|
|
|
|
2016-08-26 19:30:27 +00:00
|
|
|
// Set process._debugWaitConnect if --debug-brk was specified to stop
|
|
|
|
// the debugger on the first line
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ == BROWSER &&
|
2016-08-26 19:30:27 +00:00
|
|
|
base::CommandLine::ForCurrentProcess()->HasSwitch("debug-brk"))
|
2016-08-26 19:06:15 +00:00
|
|
|
process.Set("_debugWaitConnect", true);
|
|
|
|
|
2015-02-13 05:32:58 +00:00
|
|
|
return env;
|
2013-04-20 03:13:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 22:00:19 +00:00
|
|
|
void NodeBindings::LoadEnvironment(node::Environment* env) {
|
|
|
|
node::LoadEnvironment(env);
|
2015-06-23 12:14:03 +00:00
|
|
|
mate::EmitEvent(env->isolate(), env->process_object(), "loaded");
|
2015-01-21 22:00:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
void NodeBindings::PrepareMessageLoop() {
|
|
|
|
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
|
|
|
// nothing to do.
|
2016-08-03 11:48:41 +00:00
|
|
|
uv_async_init(uv_loop_, &dummy_uv_handle_, nullptr);
|
2013-07-22 06:58:25 +00:00
|
|
|
|
|
|
|
// 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() {
|
|
|
|
// The MessageLoop should have been created, remember the one in main thread.
|
2016-11-30 07:30:03 +00:00
|
|
|
task_runner_ = base::ThreadTaskRunnerHandle::Get();
|
2013-07-22 06:58:25 +00:00
|
|
|
|
|
|
|
// Run uv loop for once to give the uv__io_poll a chance to add all events.
|
|
|
|
UvRunOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::UvRunOnce() {
|
2016-03-27 10:21:12 +00:00
|
|
|
node::Environment* env = uv_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
|
|
|
|
2015-08-07 07:10:14 +00:00
|
|
|
// Perform microtask checkpoint after running JavaScript.
|
2016-06-24 05:45:31 +00:00
|
|
|
v8::MicrotasksScope script_scope(env->isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2015-08-07 07:10:14 +00:00
|
|
|
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ != BROWSER)
|
2016-12-12 21:49:58 +00:00
|
|
|
TRACE_EVENT_BEGIN0("devtools.timeline", "FunctionCall");
|
2016-12-07 11:22:08 +00:00
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// Deal with uv events.
|
2015-01-13 00:17:44 +00:00
|
|
|
int r = uv_run(uv_loop_, UV_RUN_NOWAIT);
|
2016-12-07 11:22:08 +00:00
|
|
|
|
2017-03-08 08:33:44 +00:00
|
|
|
if (browser_env_ != BROWSER)
|
2016-12-07 11:22:08 +00:00
|
|
|
TRACE_EVENT_END0("devtools.timeline", "FunctionCall");
|
|
|
|
|
2016-08-03 11:52:17 +00:00
|
|
|
if (r == 0)
|
2016-11-30 07:30:03 +00:00
|
|
|
base::RunLoop().QuitWhenIdle(); // Quit from uv.
|
2013-07-22 06:58:25 +00:00
|
|
|
|
|
|
|
// Tell the worker thread to continue polling.
|
|
|
|
uv_sem_post(&embed_sem_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeBindings::WakeupMainThread() {
|
2016-11-30 07:30:03 +00:00
|
|
|
DCHECK(task_runner_);
|
|
|
|
task_runner_->PostTask(FROM_HERE, base::Bind(&NodeBindings::UvRunOnce,
|
|
|
|
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
|