electron/shell/common/node_bindings.cc

630 lines
21 KiB
C++
Raw Normal View History

// 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.
#include "shell/common/node_bindings.h"
2013-04-13 10:39:09 +00:00
#include <algorithm>
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/base_paths.h"
2016-08-26 22:30:02 +00:00
#include "base/command_line.h"
#include "base/environment.h"
#include "base/path_service.h"
2016-11-30 07:30:03 +00:00
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
2016-11-30 07:30:03 +00:00
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_paths.h"
#include "electron/buildflags/buildflags.h"
#include "shell/browser/api/electron_api_app.h"
#include "shell/common/api/electron_bindings.h"
#include "shell/common/electron_command_line.h"
#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"
#include "shell/common/gin_helper/locker.h"
#include "shell/common/gin_helper/microtasks_scope.h"
#include "shell/common/mac/main_application_bundle.h"
#include "shell/common/node_includes.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h" // nogncheck
2013-04-13 10:39:09 +00:00
#if !defined(MAS_BUILD)
#include "shell/common/crash_keys.h"
#endif
#define ELECTRON_BUILTIN_MODULES(V) \
V(electron_browser_app) \
V(electron_browser_auto_updater) \
V(electron_browser_browser_view) \
V(electron_browser_content_tracing) \
V(electron_browser_crash_reporter) \
V(electron_browser_dialog) \
V(electron_browser_event) \
V(electron_browser_event_emitter) \
V(electron_browser_global_shortcut) \
V(electron_browser_in_app_purchase) \
V(electron_browser_menu) \
V(electron_browser_message_port) \
V(electron_browser_net) \
V(electron_browser_power_monitor) \
V(electron_browser_power_save_blocker) \
V(electron_browser_protocol) \
V(electron_browser_printing) \
V(electron_browser_session) \
V(electron_browser_system_preferences) \
V(electron_browser_base_window) \
V(electron_browser_tray) \
V(electron_browser_view) \
V(electron_browser_web_contents) \
V(electron_browser_web_contents_view) \
V(electron_browser_web_frame_main) \
V(electron_browser_web_view_manager) \
V(electron_browser_window) \
V(electron_common_asar) \
V(electron_common_clipboard) \
V(electron_common_command_line) \
V(electron_common_environment) \
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) \
V(electron_renderer_crash_reporter) \
V(electron_renderer_ipc) \
V(electron_renderer_web_frame)
#define ELECTRON_VIEWS_MODULES(V) V(electron_browser_image_view)
2018-05-24 06:35:50 +00:00
#define ELECTRON_DESKTOP_CAPTURER_MODULE(V) V(electron_browser_desktop_capturer)
// 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
// implementation when calling the NODE_LINKED_MODULE_CONTEXT_AWARE.
#define V(modname) void _register_##modname();
ELECTRON_BUILTIN_MODULES(V)
#if BUILDFLAG(ENABLE_VIEWS_API)
ELECTRON_VIEWS_MODULES(V)
2018-05-24 06:35:50 +00:00
#endif
#if BUILDFLAG(ENABLE_DESKTOP_CAPTURER)
ELECTRON_DESKTOP_CAPTURER_MODULE(V)
#endif
#undef V
namespace {
void stop_and_close_uv_loop(uv_loop_t* loop) {
uv_stop(loop);
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));
}
bool g_is_initialized = false;
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;
}
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));
}
return node::AllowWasmCodeGenerationCallback(context,
v8::String::Empty(isolate));
}
void ErrorMessageListener(v8::Local<v8::Message> message,
v8::Local<v8::Value> data) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
chore: bump chromium to 92.0.4505.0 (master) (#29058) * chore: bump chromium in DEPS to 92.0.4500.2 * resolve conflicts * update patches * chore: cherry-pick 82434206f306 from chromium (#29060) * fix patch * chore: bump chromium in DEPS to 92.0.4501.0 * chore: bump chromium in DEPS to 92.0.4502.0 * chore: bump chromium in DEPS to 92.0.4503.0 * chore: update patches * 2869869: [Code Health] Refactor ListValue::Insert in gpu compositor https://chromium-review.googlesource.com/c/chromium/src/+/2869869 * 2877924: Separate InkDropHost from InkDropHostView https://chromium-review.googlesource.com/c/chromium/src/+/2877924 * chore: bump chromium in DEPS to 92.0.4504.0 * update patches * Fixup for Separate InkDropHost from InkDropHostView https://chromium-review.googlesource.com/c/chromium/src/+/2877924 * 2873469: Compute hashes of .pak files during the build, and check it at runtime. https://chromium-review.googlesource.com/c/chromium/src/+/2873469 * 2874397: Remove flag to disable microtasks scope consistency checks https://chromium-review.googlesource.com/c/v8/v8/+/2874397 * 2881471: Remove unneeded trace_event.h includes in headers. https://chromium-review.googlesource.com/c/chromium/src/+/2881471 * 2844717: [Keyboard Tooltip] Rename RWHV*::SetTooltipText to UpdateTooltipUnderCursor https://chromium-review.googlesource.com/c/chromium/src/+/2844717 * chore: bump chromium in DEPS to 92.0.4505.0 * chore: update patches * 2883887: Retire ScopedObserver in /chrome/browser/predictors. https://chromium-review.googlesource.com/c/chromium/src/+/2883887 * 2883694: Retire ScopedObserver in /chrome/browser. https://chromium-review.googlesource.com/c/chromium/src/+/2883694 * fixup after merge * fixup: Remove flag to disable microtasks scope consistency checks * Temporarily disable setcallhandler-test.js nan test This test should be renabled once https://github.com/electron/electron/pull/29028 lands * Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope * chore: bump chromium in DEPS to 92.0.4506.0 * update patches * Revert "update patches" This reverts commit 333ec0d4c205bd3cbee28d2bc3d068871dbb900a. * Revert "chore: bump chromium in DEPS to 92.0.4506.0" This reverts commit 2bd52f8cd89b173c8b15a61d74fa7539cdbf574b. * Fixup: Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope * Fixup: Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope Co-authored-by: Jeremy Rose <nornagon@nornagon.net> Co-authored-by: Jeremy Rose <jeremya@chromium.org> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
2021-05-14 01:21:36 +00:00
gin_helper::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
node::Environment* env = node::Environment::GetCurrent(isolate);
if (env) {
// 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.
env->async_hooks()->clear_async_id_stack();
}
}
// 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::WideToUTF8(arg);
#else
const auto& option = arg;
#endif
const auto stripped = base::StringPiece(option).substr(0, option.find('='));
// Only allow in no-op (--) option or DebugOptions
if (allowed.count(stripped) != 0 || stripped == "--")
args.push_back(option);
}
std::vector<std::string> errors;
const int exit_code = ProcessGlobalArgs(&args, nullptr, &errors,
node::kDisallowedInEnvironment);
const std::string err_str = "Error parsing Node.js cli flags ";
if (exit_code != 0) {
LOG(ERROR) << err_str;
} else if (!errors.empty()) {
LOG(ERROR) << err_str << base::JoinString(errors, " ");
}
} // namespace
// Initialize NODE_OPTIONS to pass to Node.js
// See https://nodejs.org/api/cli.html#cli_node_options_options
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
const std::set<std::string> allowed_in_packaged = {"--max-http-header-size",
"--http-parser"};
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 = electron::api::App::IsPackaged();
for (const auto& part : parts) {
// Strip off values passed to individual NODE_OPTIONs
std::string option = part.substr(0, part.find('='));
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);
}
}
} // namespace
namespace electron {
2013-04-13 10:39:09 +00:00
namespace {
base::FilePath GetResourcesPath() {
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687) * chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a * viz: Do not apply white level scaling for RGBA fp16 HDR video https://chromium-review.googlesource.com/c/chromium/src/+/2296006 * Move WebPreferences to WebContents https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * Fix missing WeakPtr check in PreconnectManager https://chromium-review.googlesource.com/c/chromium/src/+/2309029 * Fixup swiftshader roll revision * Update patch indices * Move WebDeviceEmulationParams into common. https://chromium-review.googlesource.com/c/chromium/src/+/2303356 * Move EnableDisableDeviceEmulation to blink mojom messages https://chromium-review.googlesource.com/c/chromium/src/+/2303367 * PDF Viewer: Remove flag for two-up view https://chromium-review.googlesource.com/c/chromium/src/+/2311130 * Add mojom definition for DeviceEmulationParams. https://chromium-review.googlesource.com/c/chromium/src/+/2303491 * Remove ServiceWorkerContextWatcher from PaymentAppInstaller https://chromium-review.googlesource.com/c/chromium/src/+/2291186 * Loader: Move transferrable_url_loader.mojom into blink's mojom directory https://chromium-review.googlesource.com/c/chromium/src/+/2306123 * chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46 * mac: Work around Xcode 12b3 SDK bug https://chromium-review.googlesource.com/c/chromium/src/+/2315078 * Reland Update core items for macOS Big Sur. https://chromium-review.googlesource.com/c/chromium/src/+/2315162 * Update Swiftshader revision * mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host. https://chromium-review.googlesource.com/c/v8/v8/+/2310575 * Update patch indices * Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested() https://chromium-review.googlesource.com/c/chromium/src/+/2247838 * chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71 * chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5 * chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce * chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798 * chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5 * chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c * chore: update patches * chore: base::DeleteFile with two params is removed Should use base::DeleteFile and base::DeletePathRecursively when appropriate Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376 * chore: add patch for NodePlatform::PostJob impl * chore: update patches * chore: extension file access is now instrumented Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995 * chore: implement SetWindowFrameInScreen in OSR RWHV Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409 * chore: NotifyUserActivation requires a type now This is just for a histogram thing and therefore it does not matter what we pass in Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303 * chore: update patches * chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c * chore: update patches * chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0 * chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9 * chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50 * chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702 * chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e * update patches * change OS_MACOSX -> OS_MAC Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907 * patch: add header for ToExecutionContext in WebMessagePortConverter * chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b * update patches * NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711 * chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23 * update patches * add dragdrop header for autofill popup * int -> x11::Time * patch out accessibility private API use Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812 * remove usage of XEvent Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767 * trigger recalculation of WebPreferences before renderer initialization Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a * chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5 * chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6 * chore: update patches * fixup! trigger recalculation of WebPreferences before renderer initialization * views: Make MenuButton and RadioButton default constructible https://chromium-review.googlesource.com/c/chromium/src/+/2339586 * chore: fix code style Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com> Co-authored-by: Andy Locascio <andy@slack-corp.com> Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 18:33:58 +00:00
#if defined(OS_MAC)
return MainApplicationBundlePath().Append("Contents").Append("Resources");
#else
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
return exec_path.DirName().Append(FILE_PATH_LITERAL("resources"));
2015-02-13 05:32:58 +00:00
#endif
}
} // namespace
2017-03-08 08:33:44 +00:00
NodeBindings::NodeBindings(BrowserEnvironment browser_env)
: browser_env_(browser_env) {
if (browser_env == BrowserEnvironment::kWorker) {
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() {
// Quit the embed thread.
embed_closed_ = true;
uv_sem_post(&embed_sem_);
WakeupEmbedThread();
// Wait for everything to be done.
uv_thread_join(&embed_thread_);
// Clear uv.
uv_sem_destroy(&embed_sem_);
dummy_uv_handle_.reset();
// Clean up worker loop
if (in_worker_loop())
stop_and_close_uv_loop(uv_loop_);
2013-04-13 10:39:09 +00:00
}
void NodeBindings::RegisterBuiltinModules() {
#define V(modname) _register_##modname();
ELECTRON_BUILTIN_MODULES(V)
#if BUILDFLAG(ENABLE_VIEWS_API)
ELECTRON_VIEWS_MODULES(V)
2018-05-24 06:35:50 +00:00
#endif
#if BUILDFLAG(ENABLE_DESKTOP_CAPTURER)
ELECTRON_DESKTOP_CAPTURER_MODULE(V)
#endif
#undef V
}
bool NodeBindings::IsInitialized() {
return g_is_initialized;
}
2013-04-13 10:39:09 +00:00
void NodeBindings::Initialize() {
TRACE_EVENT0("electron", "NodeBindings::Initialize");
// Open node's error reporting system for browser process.
node::g_upstream_node_mode = false;
2015-06-19 14:56:10 +00:00
#if defined(OS_LINUX)
// Get real command line in renderer process forked by zygote.
if (browser_env_ != BrowserEnvironment::kBrowser)
ElectronCommandLine::InitializeFromCommandLine();
2015-06-19 14:56:10 +00:00
#endif
// Explicitly register electron's builtin modules.
RegisterBuiltinModules();
// Parse and set Node.js cli flags.
SetNodeCliFlags();
auto env = base::Environment::Create();
SetNodeOptions(env.get());
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);
}
#if defined(OS_WIN)
// uv_init overrides error mode to suppress the default crash dialog, bring
// it back if user wants to show it.
if (browser_env_ == BrowserEnvironment::kBrowser ||
env->HasVar("ELECTRON_DEFAULT_ERROR_MODE"))
SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX);
#endif
g_is_initialized = true;
}
node::Environment* NodeBindings::CreateEnvironment(
2018-01-31 07:09:13 +00:00
v8::Handle<v8::Context> context,
node::MultiIsolatePlatform* platform) {
#if defined(OS_WIN)
auto& atom_args = ElectronCommandLine::argv();
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
auto args = ElectronCommandLine::argv();
#endif
2013-04-13 10:39:09 +00:00
// Feed node the path to initialization script.
std::string process_type;
2017-03-08 08:33:44 +00:00
switch (browser_env_) {
case BrowserEnvironment::kBrowser:
process_type = "browser";
2017-03-08 08:33:44 +00:00
break;
case BrowserEnvironment::kRenderer:
process_type = "renderer";
2017-03-08 08:33:44 +00:00
break;
case BrowserEnvironment::kWorker:
process_type = "worker";
2017-03-08 08:33:44 +00:00
break;
}
gin_helper::Dictionary global(context->GetIsolate(), context->Global());
// Do not set DOM globals for renderer process.
// We must set this before the node bootstrapper which is run inside
// CreateEnvironment
if (browser_env_ != BrowserEnvironment::kBrowser)
global.Set("_noBrowserGlobals", true);
std::vector<std::string> exec_args;
base::FilePath resources_path = GetResourcesPath();
std::string init_script = "electron/js2c/" + process_type + "_init";
args.insert(args.begin() + 1, init_script);
isolate_data_ =
node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform);
node::Environment* env;
if (browser_env_ != BrowserEnvironment::kBrowser) {
// 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.
uint64_t flags = node::EnvironmentFlags::kDefaultFlags |
node::EnvironmentFlags::kNoRegisterESMLoader |
node::EnvironmentFlags::kNoInitializeInspector;
v8::TryCatch try_catch(context->GetIsolate());
env = node::CreateEnvironment(
isolate_data_, context, args, exec_args,
static_cast<node::EnvironmentFlags::Flags>(flags));
DCHECK(env);
// This will only be caught when something has gone terrible wrong as all
// electron scripts are wrapped in a try {} catch {} by webpack
if (try_catch.HasCaught()) {
LOG(ERROR) << "Failed to initialize node environment in process: "
<< process_type;
}
} else {
env = node::CreateEnvironment(isolate_data_, context, args, exec_args);
DCHECK(env);
}
// Clean up the global _noBrowserGlobals that we unironically injected into
// the global scope
if (browser_env_ != BrowserEnvironment::kBrowser) {
// We need to bootstrap the env in non-browser processes so that
// _noBrowserGlobals is read correctly before we remove it
global.Delete("_noBrowserGlobals");
}
2015-02-13 05:32:58 +00:00
node::IsolateSettings is;
// Use a custom fatal error callback to allow us to add
// crash message and location to CrashReports.
is.fatal_error_callback = V8FatalErrorCallback;
// 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;
};
// Use a custom callback here to allow us to leverage Blink's logic in the
// renderer process.
is.allow_wasm_code_generation_callback = AllowWasmCodeGenerationCallback;
if (browser_env_ == BrowserEnvironment::kBrowser) {
// Node.js requires that microtask checkpoints be explicitly invoked.
is.policy = v8::MicrotasksPolicy::kExplicit;
} else {
// Match Blink's behavior by allowing microtasks invocation to be controlled
// by MicrotasksScope objects.
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;
// 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);
// 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.
is.flags |=
node::IsolateSettingsFlags::SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK;
// 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
}
node::SetIsolateUpForNode(context->GetIsolate(), is);
gin_helper::Dictionary process(context->GetIsolate(), env->process_object());
process.SetReadOnly("type", process_type);
2015-02-13 05:32:58 +00:00
process.Set("resourcesPath", resources_path);
// 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);
process.Set("helperExecPath", helper_exec_path);
2015-02-13 05:32:58 +00:00
return env;
}
void NodeBindings::LoadEnvironment(node::Environment* env) {
node::LoadEnvironment(env);
gin_helper::EmitEvent(env->isolate(), env->process_object(), "loaded");
}
void NodeBindings::PrepareMessageLoop() {
#if !defined(OS_WIN)
int handle = uv_backend_fd(uv_loop_);
// If the backend fd hasn't changed, don't proceed.
if (handle == handle_)
return;
handle_ = handle;
2021-03-22 20:11:03 +00:00
#endif
// Add dummy handle for libuv, otherwise libuv would quit when there is
// nothing to do.
uv_async_init(uv_loop_, dummy_uv_handle_.get(), nullptr);
// 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();
// 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();
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;
// Use Locker in browser process.
gin_helper::Locker locker(env->isolate());
v8::HandleScope handle_scope(env->isolate());
// Enter node context while dealing with uv events.
v8::Context::Scope context_scope(env->context());
// Node.js expects `kExplicit` microtasks policy and will run microtasks
// checkpoints after every call into JavaScript. Since we use a different
// policy in the renderer - switch to `kExplicit` and then drop back to the
// previous policy value.
auto old_policy = env->isolate()->GetMicrotasksPolicy();
DCHECK_EQ(v8::MicrotasksScope::GetCurrentDepth(env->isolate()), 0);
env->isolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
if (browser_env_ != BrowserEnvironment::kBrowser)
2016-12-12 21:49:58 +00:00
TRACE_EVENT_BEGIN0("devtools.timeline", "FunctionCall");
// Deal with uv events.
int r = uv_run(uv_loop_, UV_RUN_NOWAIT);
if (browser_env_ != BrowserEnvironment::kBrowser)
TRACE_EVENT_END0("devtools.timeline", "FunctionCall");
env->isolate()->SetMicrotasksPolicy(old_policy);
if (r == 0)
2016-11-30 07:30:03 +00:00
base::RunLoop().QuitWhenIdle(); // Quit from uv.
// 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::BindOnce(&NodeBindings::UvRunOnce,
weak_factory_.GetWeakPtr()));
}
void NodeBindings::WakeupEmbedThread() {
uv_async_send(dummy_uv_handle_.get());
}
// static
2018-04-18 01:55:30 +00:00
void NodeBindings::EmbedThreadRunner(void* arg) {
auto* self = static_cast<NodeBindings*>(arg);
while (true) {
// Wait for the main loop to deal with events.
uv_sem_wait(&self->embed_sem_);
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.
self->PollEvents();
if (self->embed_closed_)
break;
// Deal with event in main thread.
self->WakeupMainThread();
}
}
} // namespace electron