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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_bindings.h"
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2018-01-22 22:49:30 +00:00
|
|
|
#include <algorithm>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2019-11-02 01:06:15 +00:00
|
|
|
#include <set>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
2020-02-07 02:59:38 +00:00
|
|
|
#include <unordered_set>
|
2018-10-18 23:57:28 +00:00
|
|
|
#include <utility>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <vector>
|
|
|
|
|
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"
|
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"
|
2018-10-18 23:57:28 +00:00
|
|
|
#include "base/strings/string_split.h"
|
2018-01-22 22:49:30 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2016-11-30 07:30:03 +00:00
|
|
|
#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"
|
2018-10-01 20:00:53 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2020-07-30 02:04:24 +00:00
|
|
|
#include "shell/common/api/electron_bindings.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/common/electron_command_line.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
|
|
#include "shell/common/gin_helper/event_emitter_caller.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_helper/locker.h"
|
2020-06-17 17:08:10 +00:00
|
|
|
#include "shell/common/gin_helper/microtasks_scope.h"
|
2019-07-09 09:56:40 +00:00
|
|
|
#include "shell/common/mac/main_application_bundle.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2020-10-08 19:26:01 +00:00
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h" // nogncheck
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2020-07-30 02:04:24 +00:00
|
|
|
#if !defined(MAS_BUILD)
|
|
|
|
#include "shell/common/crash_keys.h"
|
|
|
|
#endif
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
#define ELECTRON_BUILTIN_MODULES(V) \
|
|
|
|
V(electron_browser_app) \
|
|
|
|
V(electron_browser_auto_updater) \
|
|
|
|
V(electron_browser_browser_view) \
|
|
|
|
V(electron_browser_content_tracing) \
|
2020-05-07 20:31:26 +00:00
|
|
|
V(electron_browser_crash_reporter) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_dialog) \
|
|
|
|
V(electron_browser_event) \
|
2020-03-18 19:57:08 +00:00
|
|
|
V(electron_browser_event_emitter) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_global_shortcut) \
|
|
|
|
V(electron_browser_in_app_purchase) \
|
|
|
|
V(electron_browser_menu) \
|
2020-03-12 01:07:54 +00:00
|
|
|
V(electron_browser_message_port) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_net) \
|
|
|
|
V(electron_browser_power_monitor) \
|
|
|
|
V(electron_browser_power_save_blocker) \
|
|
|
|
V(electron_browser_protocol) \
|
2020-11-17 22:14:09 +00:00
|
|
|
V(electron_browser_printing) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_session) \
|
|
|
|
V(electron_browser_system_preferences) \
|
2020-06-29 07:06:20 +00:00
|
|
|
V(electron_browser_base_window) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_tray) \
|
2020-05-07 20:31:26 +00:00
|
|
|
V(electron_browser_view) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_web_contents) \
|
|
|
|
V(electron_browser_web_contents_view) \
|
2020-10-09 16:50:46 +00:00
|
|
|
V(electron_browser_web_frame_main) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_web_view_manager) \
|
|
|
|
V(electron_browser_window) \
|
|
|
|
V(electron_common_asar) \
|
|
|
|
V(electron_common_clipboard) \
|
|
|
|
V(electron_common_command_line) \
|
2020-10-06 00:58:31 +00:00
|
|
|
V(electron_common_environment) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_common_features) \
|
|
|
|
V(electron_common_native_image) \
|
|
|
|
V(electron_common_native_theme) \
|
|
|
|
V(electron_common_notification) \
|
|
|
|
V(electron_common_screen) \
|
|
|
|
V(electron_common_shell) \
|
|
|
|
V(electron_common_v8_util) \
|
|
|
|
V(electron_renderer_context_bridge) \
|
2020-05-07 20:31:26 +00:00
|
|
|
V(electron_renderer_crash_reporter) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_renderer_ipc) \
|
|
|
|
V(electron_renderer_web_frame)
|
2018-01-06 15:58:24 +00:00
|
|
|
|
2020-03-28 06:44:57 +00:00
|
|
|
#define ELECTRON_VIEWS_MODULES(V) V(electron_browser_image_view)
|
2018-05-24 06:35:50 +00:00
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
#define ELECTRON_DESKTOP_CAPTURER_MODULE(V) V(electron_browser_desktop_capturer)
|
2018-06-13 16:15:34 +00:00
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
// This is used to load built-in modules. Instead of using
|
|
|
|
// __attribute__((constructor)), we call the _register_<modname>
|
|
|
|
// function for each built-in modules explicitly. This is only
|
|
|
|
// forward declaration. The definitions are in each module's
|
2019-03-08 18:29:52 +00:00
|
|
|
// implementation when calling the NODE_LINKED_MODULE_CONTEXT_AWARE.
|
2018-01-06 15:58:24 +00:00
|
|
|
#define V(modname) void _register_##modname();
|
|
|
|
ELECTRON_BUILTIN_MODULES(V)
|
2020-03-28 06:44:57 +00:00
|
|
|
#if BUILDFLAG(ENABLE_VIEWS_API)
|
|
|
|
ELECTRON_VIEWS_MODULES(V)
|
2018-05-24 06:35:50 +00:00
|
|
|
#endif
|
2018-10-01 20:00:53 +00:00
|
|
|
#if BUILDFLAG(ENABLE_DESKTOP_CAPTURER)
|
2018-06-13 16:15:34 +00:00
|
|
|
ELECTRON_DESKTOP_CAPTURER_MODULE(V)
|
|
|
|
#endif
|
2018-01-06 15:58:24 +00:00
|
|
|
#undef V
|
2014-06-29 10:12:46 +00:00
|
|
|
|
2018-01-04 19:16:06 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void stop_and_close_uv_loop(uv_loop_t* loop) {
|
|
|
|
uv_stop(loop);
|
|
|
|
|
2020-09-14 00:53:50 +00:00
|
|
|
auto const ensure_closing = [](uv_handle_t* handle, void*) {
|
|
|
|
// We should be using the UvHandle wrapper everywhere, in which case
|
|
|
|
// all handles should already be in a closing state...
|
|
|
|
DCHECK(uv_is_closing(handle));
|
|
|
|
// ...but if a raw handle got through, through, do the right thing anyway
|
|
|
|
if (!uv_is_closing(handle)) {
|
|
|
|
uv_close(handle, nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
uv_walk(loop, ensure_closing, nullptr);
|
|
|
|
|
|
|
|
// All remaining handles are in a closing state now.
|
|
|
|
// Pump the event loop so that they can finish closing.
|
|
|
|
for (;;)
|
|
|
|
if (uv_run(loop, UV_RUN_DEFAULT) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
DCHECK_EQ(0, uv_loop_alive(loop));
|
2018-01-04 19:16:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 07:38:31 +00:00
|
|
|
bool g_is_initialized = false;
|
|
|
|
|
2019-11-02 01:06:15 +00:00
|
|
|
bool IsPackagedApp() {
|
|
|
|
base::FilePath exe_path;
|
|
|
|
base::PathService::Get(base::FILE_EXE, &exe_path);
|
|
|
|
base::FilePath::StringType base_name =
|
|
|
|
base::ToLowerASCII(exe_path.BaseName().value());
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
return base_name != FILE_PATH_LITERAL("electron.exe");
|
|
|
|
#else
|
|
|
|
return base_name != FILE_PATH_LITERAL("electron");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-30 02:04:24 +00:00
|
|
|
void V8FatalErrorCallback(const char* location, const char* message) {
|
|
|
|
LOG(ERROR) << "Fatal error in V8: " << location << " " << message;
|
|
|
|
|
|
|
|
#if !defined(MAS_BUILD)
|
|
|
|
electron::crash_keys::SetCrashKey("electron.v8-fatal.message", message);
|
|
|
|
electron::crash_keys::SetCrashKey("electron.v8-fatal.location", location);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
volatile int* zero = nullptr;
|
|
|
|
*zero = 0;
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:26:01 +00:00
|
|
|
bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::String>) {
|
|
|
|
// If we're running with contextIsolation enabled in the renderer process,
|
|
|
|
// fall back to Blink's logic.
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
if (node::Environment::GetCurrent(isolate) == nullptr) {
|
|
|
|
if (gin_helper::Locker::IsBrowserProcess())
|
|
|
|
return false;
|
|
|
|
return blink::V8Initializer::WasmCodeGenerationCheckCallbackInMainThread(
|
|
|
|
context, v8::String::Empty(isolate));
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:28:28 +00:00
|
|
|
return node::AllowWasmCodeGenerationCallback(context,
|
|
|
|
v8::String::Empty(isolate));
|
2020-10-08 19:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 17:17:58 +00:00
|
|
|
void ErrorMessageListener(v8::Local<v8::Message> message,
|
|
|
|
v8::Local<v8::Value> data) {
|
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
node::Environment* env = node::Environment::GetCurrent(isolate);
|
|
|
|
|
|
|
|
if (env) {
|
2020-12-02 05:34:08 +00:00
|
|
|
// Emit the after() hooks now that the exception has been handled.
|
|
|
|
// Analogous to node/lib/internal/process/execution.js#L176-L180
|
|
|
|
if (env->async_hooks()->fields()[node::AsyncHooks::kAfter]) {
|
|
|
|
while (env->async_hooks()->fields()[node::AsyncHooks::kStackLength]) {
|
|
|
|
node::AsyncWrap::EmitAfter(env, env->execution_async_id());
|
|
|
|
env->async_hooks()->pop_async_context(env->execution_async_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the async id stack is properly cleared so the async
|
|
|
|
// hook stack does not become corrupted.
|
2020-11-10 17:17:58 +00:00
|
|
|
env->async_hooks()->clear_async_id_stack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
// Initialize Node.js cli options to pass to Node.js
|
|
|
|
// See https://nodejs.org/api/cli.html#cli_options
|
|
|
|
void SetNodeCliFlags() {
|
|
|
|
// Only allow DebugOptions in non-ELECTRON_RUN_AS_NODE mode
|
|
|
|
const std::unordered_set<base::StringPiece, base::StringPieceHash> allowed = {
|
|
|
|
"--inspect", "--inspect-brk",
|
|
|
|
"--inspect-port", "--debug",
|
|
|
|
"--debug-brk", "--debug-port",
|
|
|
|
"--inspect-brk-node", "--inspect-publish-uid",
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto argv = base::CommandLine::ForCurrentProcess()->argv();
|
|
|
|
std::vector<std::string> args;
|
|
|
|
|
|
|
|
// TODO(codebytere): We need to set the first entry in args to the
|
|
|
|
// process name owing to src/node_options-inl.h#L286-L290 but this is
|
|
|
|
// redundant and so should be refactored upstream.
|
|
|
|
args.reserve(argv.size() + 1);
|
|
|
|
args.emplace_back("electron");
|
|
|
|
|
|
|
|
for (const auto& arg : argv) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
const auto& option = base::UTF16ToUTF8(arg);
|
|
|
|
#else
|
|
|
|
const auto& option = arg;
|
|
|
|
#endif
|
|
|
|
const auto stripped = base::StringPiece(option).substr(0, option.find('='));
|
2020-02-09 18:42:02 +00:00
|
|
|
|
|
|
|
// Only allow in no-op (--) option or DebugOptions
|
|
|
|
if (allowed.count(stripped) != 0 || stripped == "--")
|
2020-02-07 02:59:38 +00:00
|
|
|
args.push_back(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> errors;
|
|
|
|
const int exit_code = ProcessGlobalArgs(&args, nullptr, &errors,
|
|
|
|
node::kDisallowedInEnvironment);
|
|
|
|
|
2020-02-09 18:42:02 +00:00
|
|
|
const std::string err_str = "Error parsing Node.js cli flags ";
|
2020-02-07 02:59:38 +00:00
|
|
|
if (exit_code != 0) {
|
2020-02-09 18:42:02 +00:00
|
|
|
LOG(ERROR) << err_str;
|
|
|
|
} else if (!errors.empty()) {
|
|
|
|
LOG(ERROR) << err_str << base::JoinString(errors, " ");
|
2020-02-07 02:59:38 +00:00
|
|
|
}
|
2020-02-09 18:42:02 +00:00
|
|
|
} // namespace
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2019-11-02 01:06:15 +00:00
|
|
|
// Initialize NODE_OPTIONS to pass to Node.js
|
2020-02-07 02:59:38 +00:00
|
|
|
// See https://nodejs.org/api/cli.html#cli_node_options_options
|
2019-11-02 01:06:15 +00:00
|
|
|
void SetNodeOptions(base::Environment* env) {
|
|
|
|
// Options that are unilaterally disallowed
|
|
|
|
const std::set<std::string> disallowed = {
|
|
|
|
"--openssl-config", "--use-bundled-ca", "--use-openssl-ca",
|
|
|
|
"--force-fips", "--enable-fips"};
|
|
|
|
|
|
|
|
// Subset of options allowed in packaged apps
|
2020-01-09 16:09:14 +00:00
|
|
|
const std::set<std::string> allowed_in_packaged = {"--max-http-header-size",
|
|
|
|
"--http-parser"};
|
2019-11-02 01:06:15 +00:00
|
|
|
|
|
|
|
if (env->HasVar("NODE_OPTIONS")) {
|
|
|
|
std::string options;
|
|
|
|
env->GetVar("NODE_OPTIONS", &options);
|
|
|
|
std::vector<std::string> parts = base::SplitString(
|
|
|
|
options, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
|
|
|
|
|
|
|
|
bool is_packaged_app = IsPackagedApp();
|
|
|
|
|
|
|
|
for (const auto& part : parts) {
|
|
|
|
// Strip off values passed to individual NODE_OPTIONs
|
2020-02-07 02:59:38 +00:00
|
|
|
std::string option = part.substr(0, part.find('='));
|
2019-11-02 01:06:15 +00:00
|
|
|
|
|
|
|
if (is_packaged_app &&
|
|
|
|
allowed_in_packaged.find(option) == allowed_in_packaged.end()) {
|
|
|
|
// Explicitly disallow majority of NODE_OPTIONS in packaged apps
|
|
|
|
LOG(ERROR) << "Most NODE_OPTIONs are not supported in packaged apps."
|
|
|
|
<< " See documentation for more details.";
|
|
|
|
options.erase(options.find(option), part.length());
|
|
|
|
} else if (disallowed.find(option) != disallowed.end()) {
|
|
|
|
// Remove NODE_OPTIONS specifically disallowed for use in Node.js
|
|
|
|
// through Electron owing to constraints like BoringSSL.
|
|
|
|
LOG(ERROR) << "The NODE_OPTION " << option
|
|
|
|
<< " is not supported in Electron";
|
|
|
|
options.erase(options.find(option), part.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite new NODE_OPTIONS without unsupported variables
|
|
|
|
env->SetVar("NODE_OPTIONS", options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:16:06 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-07-09 09:56:40 +00:00
|
|
|
base::FilePath GetResourcesPath() {
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2019-07-09 09:56:40 +00:00
|
|
|
return MainApplicationBundlePath().Append("Contents").Append("Resources");
|
|
|
|
#else
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
2015-03-29 11:59:16 +00:00
|
|
|
base::FilePath exec_path(command_line->GetProgram());
|
2018-09-15 00:09:42 +00:00
|
|
|
base::PathService::Get(base::FILE_EXE, &exec_path);
|
2015-03-29 11:59:16 +00:00
|
|
|
|
2019-07-09 09:56:40 +00:00
|
|
|
return exec_path.DirName().Append(FILE_PATH_LITERAL("resources"));
|
2015-02-13 05:32:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-03-08 08:33:44 +00:00
|
|
|
NodeBindings::NodeBindings(BrowserEnvironment browser_env)
|
2021-01-26 18:16:21 +00:00
|
|
|
: browser_env_(browser_env) {
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env == BrowserEnvironment::kWorker) {
|
2018-01-04 19:16:06 +00:00
|
|
|
uv_loop_init(&worker_loop_);
|
|
|
|
uv_loop_ = &worker_loop_;
|
|
|
|
} else {
|
|
|
|
uv_loop_ = uv_default_loop();
|
|
|
|
}
|
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_);
|
2020-07-08 18:00:43 +00:00
|
|
|
|
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_);
|
2020-09-14 00:53:50 +00:00
|
|
|
dummy_uv_handle_.reset();
|
2017-03-10 08:07:51 +00:00
|
|
|
|
2018-01-04 19:16:06 +00:00
|
|
|
// Clean up worker loop
|
2020-07-08 18:00:43 +00:00
|
|
|
if (in_worker_loop())
|
2018-01-04 19:16:06 +00:00
|
|
|
stop_and_close_uv_loop(uv_loop_);
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
void NodeBindings::RegisterBuiltinModules() {
|
|
|
|
#define V(modname) _register_##modname();
|
|
|
|
ELECTRON_BUILTIN_MODULES(V)
|
2020-03-28 06:44:57 +00:00
|
|
|
#if BUILDFLAG(ENABLE_VIEWS_API)
|
|
|
|
ELECTRON_VIEWS_MODULES(V)
|
2018-05-24 06:35:50 +00:00
|
|
|
#endif
|
2018-10-01 20:00:53 +00:00
|
|
|
#if BUILDFLAG(ENABLE_DESKTOP_CAPTURER)
|
2018-06-13 16:15:34 +00:00
|
|
|
ELECTRON_DESKTOP_CAPTURER_MODULE(V)
|
|
|
|
#endif
|
2018-01-06 15:58:24 +00:00
|
|
|
#undef V
|
|
|
|
}
|
|
|
|
|
2018-06-13 07:38:31 +00:00
|
|
|
bool NodeBindings::IsInitialized() {
|
|
|
|
return g_is_initialized;
|
|
|
|
}
|
|
|
|
|
2013-04-13 10:39:09 +00:00
|
|
|
void NodeBindings::Initialize() {
|
2019-04-29 22:28:30 +00:00
|
|
|
TRACE_EVENT0("electron", "NodeBindings::Initialize");
|
2013-12-15 06:20:28 +00:00
|
|
|
// Open node's error reporting system for browser process.
|
|
|
|
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.
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ != BrowserEnvironment::kBrowser)
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronCommandLine::InitializeFromCommandLine();
|
2015-06-19 14:56:10 +00:00
|
|
|
#endif
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
// Explicitly register electron's builtin modules.
|
|
|
|
RegisterBuiltinModules();
|
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
// Parse and set Node.js cli flags.
|
|
|
|
SetNodeCliFlags();
|
|
|
|
|
2018-10-18 23:57:28 +00:00
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
2019-11-02 01:06:15 +00:00
|
|
|
SetNodeOptions(env.get());
|
2018-10-18 23:57:28 +00:00
|
|
|
|
2021-03-22 09:42:06 +00:00
|
|
|
std::vector<std::string> argv = {"electron"};
|
|
|
|
std::vector<std::string> exec_argv;
|
|
|
|
std::vector<std::string> errors;
|
|
|
|
|
|
|
|
int exit_code = node::InitializeNodeWithArgs(&argv, &exec_argv, &errors);
|
|
|
|
|
|
|
|
for (const std::string& error : errors) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0].c_str(), error.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exit_code != 0) {
|
|
|
|
exit(exit_code);
|
|
|
|
}
|
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.
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ == BrowserEnvironment::kBrowser ||
|
2019-05-03 18:11:41 +00:00
|
|
|
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
|
2018-06-13 07:38:31 +00:00
|
|
|
|
|
|
|
g_is_initialized = true;
|
2013-12-15 06:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node::Environment* NodeBindings::CreateEnvironment(
|
2018-01-31 07:09:13 +00:00
|
|
|
v8::Handle<v8::Context> context,
|
2020-02-25 16:46:08 +00:00
|
|
|
node::MultiIsolatePlatform* platform) {
|
2018-01-22 22:49:30 +00:00
|
|
|
#if defined(OS_WIN)
|
2020-02-04 20:19:40 +00:00
|
|
|
auto& atom_args = ElectronCommandLine::argv();
|
2018-01-22 22:49:30 +00:00
|
|
|
std::vector<std::string> args(atom_args.size());
|
|
|
|
std::transform(atom_args.cbegin(), atom_args.cend(), args.begin(),
|
|
|
|
[](auto& a) { return base::WideToUTF8(a); });
|
|
|
|
#else
|
2020-02-04 20:19:40 +00:00
|
|
|
auto args = ElectronCommandLine::argv();
|
2018-01-22 22:49:30 +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.
|
2019-06-04 00:03:59 +00:00
|
|
|
std::string process_type;
|
2017-03-08 08:33:44 +00:00
|
|
|
switch (browser_env_) {
|
2020-10-27 17:51:45 +00:00
|
|
|
case BrowserEnvironment::kBrowser:
|
2019-06-04 00:03:59 +00:00
|
|
|
process_type = "browser";
|
2017-03-08 08:33:44 +00:00
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
case BrowserEnvironment::kRenderer:
|
2019-06-04 00:03:59 +00:00
|
|
|
process_type = "renderer";
|
2017-03-08 08:33:44 +00:00
|
|
|
break;
|
2020-10-27 17:51:45 +00:00
|
|
|
case BrowserEnvironment::kWorker:
|
2019-06-04 00:03:59 +00:00
|
|
|
process_type = "worker";
|
2017-03-08 08:33:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-07-16 01:58:39 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
gin_helper::Dictionary global(context->GetIsolate(), context->Global());
|
2019-07-16 01:58:39 +00:00
|
|
|
// Do not set DOM globals for renderer process.
|
|
|
|
// We must set this before the node bootstrapper which is run inside
|
|
|
|
// CreateEnvironment
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ != BrowserEnvironment::kBrowser)
|
2019-07-16 01:58:39 +00:00
|
|
|
global.Set("_noBrowserGlobals", true);
|
|
|
|
|
2020-09-17 22:08:57 +00:00
|
|
|
std::vector<std::string> exec_args;
|
2019-07-09 09:56:40 +00:00
|
|
|
base::FilePath resources_path = GetResourcesPath();
|
2019-06-04 00:03:59 +00:00
|
|
|
std::string init_script = "electron/js2c/" + process_type + "_init";
|
|
|
|
|
|
|
|
args.insert(args.begin() + 1, init_script);
|
2013-12-11 15:59:03 +00:00
|
|
|
|
2019-11-02 22:14:11 +00:00
|
|
|
isolate_data_ =
|
|
|
|
node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform);
|
2020-09-04 21:53:49 +00:00
|
|
|
|
|
|
|
node::Environment* env;
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ != BrowserEnvironment::kBrowser) {
|
2020-09-17 22:08:57 +00:00
|
|
|
// Only one ESM loader can be registered per isolate -
|
|
|
|
// in renderer processes this should be blink. We need to tell Node.js
|
|
|
|
// not to register its handler (overriding blinks) in non-browser processes.
|
2020-09-24 03:35:56 +00:00
|
|
|
uint64_t flags = node::EnvironmentFlags::kDefaultFlags |
|
|
|
|
node::EnvironmentFlags::kNoRegisterESMLoader |
|
|
|
|
node::EnvironmentFlags::kNoInitializeInspector;
|
2020-09-04 21:53:49 +00:00
|
|
|
v8::TryCatch try_catch(context->GetIsolate());
|
2020-09-17 22:08:57 +00:00
|
|
|
env = node::CreateEnvironment(isolate_data_, context, args, exec_args,
|
|
|
|
(node::EnvironmentFlags::Flags)flags);
|
2020-09-04 21:53:49 +00:00
|
|
|
DCHECK(env);
|
2020-09-17 22:08:57 +00:00
|
|
|
|
2020-09-04 21:53:49 +00:00
|
|
|
// This will only be caught when something has gone terrible wrong as all
|
2020-10-20 19:10:15 +00:00
|
|
|
// electron scripts are wrapped in a try {} catch {} by webpack
|
2020-09-04 21:53:49 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
LOG(ERROR) << "Failed to initialize node environment in process: "
|
|
|
|
<< process_type;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-24 03:35:56 +00:00
|
|
|
env = node::CreateEnvironment(isolate_data_, context, args, exec_args);
|
2020-09-04 21:53:49 +00:00
|
|
|
DCHECK(env);
|
|
|
|
}
|
2019-07-16 01:58:39 +00:00
|
|
|
|
|
|
|
// Clean up the global _noBrowserGlobals that we unironically injected into
|
|
|
|
// the global scope
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ != BrowserEnvironment::kBrowser) {
|
2019-07-18 23:54:23 +00:00
|
|
|
// We need to bootstrap the env in non-browser processes so that
|
|
|
|
// _noBrowserGlobals is read correctly before we remove it
|
2019-07-16 01:58:39 +00:00
|
|
|
global.Delete("_noBrowserGlobals");
|
2019-07-18 23:54:23 +00:00
|
|
|
}
|
2015-02-13 05:32:58 +00:00
|
|
|
|
2020-07-20 19:41:52 +00:00
|
|
|
node::IsolateSettings is;
|
|
|
|
|
2020-07-30 02:04:24 +00:00
|
|
|
// Use a custom fatal error callback to allow us to add
|
|
|
|
// crash message and location to CrashReports.
|
|
|
|
is.fatal_error_callback = V8FatalErrorCallback;
|
|
|
|
|
2020-10-06 18:16:49 +00:00
|
|
|
// We don't want to abort either in the renderer or browser processes.
|
|
|
|
// We already listen for uncaught exceptions and handle them there.
|
|
|
|
is.should_abort_on_uncaught_exception_callback = [](v8::Isolate*) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-10-08 19:26:01 +00:00
|
|
|
// Use a custom callback here to allow us to leverage Blink's logic in the
|
|
|
|
// renderer process.
|
|
|
|
is.allow_wasm_code_generation_callback = AllowWasmCodeGenerationCallback;
|
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ == BrowserEnvironment::kBrowser) {
|
2020-07-20 19:41:52 +00:00
|
|
|
// Node.js requires that microtask checkpoints be explicitly invoked.
|
|
|
|
is.policy = v8::MicrotasksPolicy::kExplicit;
|
2017-01-25 17:24:24 +00:00
|
|
|
} else {
|
2020-04-27 08:03:40 +00:00
|
|
|
// Match Blink's behavior by allowing microtasks invocation to be controlled
|
|
|
|
// by MicrotasksScope objects.
|
2020-07-20 19:41:52 +00:00
|
|
|
is.policy = v8::MicrotasksPolicy::kScoped;
|
|
|
|
|
|
|
|
// We do not want to use Node.js' message listener as it interferes with
|
|
|
|
// Blink's.
|
|
|
|
is.flags &= ~node::IsolateSettingsFlags::MESSAGE_LISTENER_WITH_ERROR_LEVEL;
|
|
|
|
|
2020-11-10 17:17:58 +00:00
|
|
|
// Isolate message listeners are additive (you can add multiple), so instead
|
|
|
|
// we add an extra one here to ensure that the async hook stack is properly
|
|
|
|
// cleared when errors are thrown.
|
|
|
|
context->GetIsolate()->AddMessageListenerWithErrorLevel(
|
|
|
|
ErrorMessageListener, v8::Isolate::kMessageError);
|
|
|
|
|
2020-07-20 19:41:52 +00:00
|
|
|
// We do not want to use the promise rejection callback that Node.js uses,
|
|
|
|
// because it does not send PromiseRejectionEvents to the global script
|
|
|
|
// context. We need to use the one Blink already provides.
|
2020-09-17 22:08:57 +00:00
|
|
|
is.flags |=
|
|
|
|
node::IsolateSettingsFlags::SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK;
|
2020-12-09 19:08:13 +00:00
|
|
|
|
|
|
|
// We do not want to use the stack trace callback that Node.js uses,
|
|
|
|
// because it relies on Node.js being aware of the current Context and
|
|
|
|
// that's not always the case. We need to use the one Blink already
|
|
|
|
// provides.
|
|
|
|
is.flags |=
|
|
|
|
node::IsolateSettingsFlags::SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK;
|
2016-08-22 10:34:59 +00:00
|
|
|
}
|
2016-08-03 08:57:10 +00:00
|
|
|
|
2020-07-20 19:41:52 +00:00
|
|
|
node::SetIsolateUpForNode(context->GetIsolate(), is);
|
2020-04-27 08:03:40 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
gin_helper::Dictionary process(context->GetIsolate(), env->process_object());
|
2018-11-08 14:28:06 +00:00
|
|
|
process.SetReadOnly("type", process_type);
|
2015-02-13 05:32:58 +00:00
|
|
|
process.Set("resourcesPath", resources_path);
|
2015-04-14 06:05:30 +00:00
|
|
|
// The path to helper app.
|
|
|
|
base::FilePath helper_exec_path;
|
2018-09-15 00:09:42 +00:00
|
|
|
base::PathService::Get(content::CHILD_PROCESS_EXE, &helper_exec_path);
|
2015-04-14 06:05:30 +00:00
|
|
|
process.Set("helperExecPath", helper_exec_path);
|
2016-08-26 19:06:15 +00:00
|
|
|
|
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);
|
2019-09-06 05:52:54 +00:00
|
|
|
gin_helper::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() {
|
2021-02-17 03:42:28 +00:00
|
|
|
#if !defined(OS_WIN)
|
|
|
|
int handle = uv_backend_fd(uv_loop_);
|
|
|
|
#else
|
|
|
|
HANDLE handle = uv_loop_->iocp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If the backend fd hasn't changed, don't proceed.
|
|
|
|
if (handle == handle_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
handle_ = handle;
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
|
|
|
// nothing to do.
|
2020-09-14 00:53:50 +00:00
|
|
|
uv_async_init(uv_loop_, dummy_uv_handle_.get(), 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
|
|
|
|
2017-03-28 08:19:14 +00:00
|
|
|
// When doing navigation without restarting renderer process, it may happen
|
|
|
|
// that the node environment is destroyed but the message loop is still there.
|
|
|
|
// In this case we should not run uv loop.
|
|
|
|
if (!env)
|
|
|
|
return;
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
// Use Locker in browser process.
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::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.
|
2020-06-17 17:08:10 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope(env->isolate());
|
2015-08-07 07:10:14 +00:00
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ != BrowserEnvironment::kBrowser)
|
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
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
if (browser_env_ != BrowserEnvironment::kBrowser)
|
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_);
|
2018-04-20 10:55:05 +00:00
|
|
|
task_runner_->PostTask(FROM_HERE, base::BindOnce(&NodeBindings::UvRunOnce,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
2013-07-22 06:58:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 05:08:40 +00:00
|
|
|
void NodeBindings::WakeupEmbedThread() {
|
2020-09-14 00:53:50 +00:00
|
|
|
uv_async_send(dummy_uv_handle_.get());
|
2013-07-23 05:08:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// static
|
2018-04-18 01:55:30 +00:00
|
|
|
void NodeBindings::EmbedThreadRunner(void* arg) {
|
2020-10-26 18:56:31 +00:00
|
|
|
auto* self = static_cast<NodeBindings*>(arg);
|
2013-07-22 06:58:25 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|