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"
|
2021-11-24 08:45:59 +00:00
|
|
|
#include "base/task/single_thread_task_runner.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"
|
2021-08-05 17:50:11 +00:00
|
|
|
#include "electron/fuses.h"
|
2021-06-02 19:17:08 +00:00
|
|
|
#include "shell/browser/api/electron_api_app.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"
|
2023-02-13 21:39:18 +00:00
|
|
|
#include "shell/common/gin_helper/event.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#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
|
2022-08-29 13:45:48 +00:00
|
|
|
#include "third_party/electron_node/src/debug_utils.h"
|
2013-04-13 10:39:09 +00:00
|
|
|
|
2022-11-14 20:46:52 +00:00
|
|
|
#if !IS_MAS_BUILD()
|
2020-07-30 02:04:24 +00:00
|
|
|
#include "shell/common/crash_keys.h"
|
|
|
|
#endif
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
#define ELECTRON_BROWSER_BINDINGS(V) \
|
2020-02-14 11:25:39 +00:00
|
|
|
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) \
|
2023-05-24 19:32:31 +00:00
|
|
|
V(electron_browser_desktop_capturer) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_dialog) \
|
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) \
|
2022-06-21 07:34:56 +00:00
|
|
|
V(electron_browser_native_theme) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_net) \
|
2022-06-21 07:34:56 +00:00
|
|
|
V(electron_browser_notification) \
|
2020-02-14 11:25:39 +00:00
|
|
|
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) \
|
2022-07-12 15:38:49 +00:00
|
|
|
V(electron_browser_push_notifications) \
|
2021-08-05 22:12:54 +00:00
|
|
|
V(electron_browser_safe_storage) \
|
2020-02-14 11:25:39 +00:00
|
|
|
V(electron_browser_session) \
|
2022-06-21 07:34:56 +00:00
|
|
|
V(electron_browser_screen) \
|
2020-02-14 11:25:39 +00:00
|
|
|
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) \
|
2022-10-20 05:49:49 +00:00
|
|
|
V(electron_browser_utility_process) \
|
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) \
|
2023-02-08 01:22:18 +00:00
|
|
|
V(electron_browser_window)
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
#define ELECTRON_COMMON_BINDINGS(V) \
|
2023-02-08 01:22:18 +00:00
|
|
|
V(electron_common_asar) \
|
|
|
|
V(electron_common_clipboard) \
|
|
|
|
V(electron_common_command_line) \
|
|
|
|
V(electron_common_crashpad_support) \
|
|
|
|
V(electron_common_environment) \
|
|
|
|
V(electron_common_features) \
|
|
|
|
V(electron_common_native_image) \
|
|
|
|
V(electron_common_shell) \
|
|
|
|
V(electron_common_v8_util)
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
#define ELECTRON_RENDERER_BINDINGS(V) \
|
2023-02-08 01:22:18 +00:00
|
|
|
V(electron_renderer_context_bridge) \
|
|
|
|
V(electron_renderer_crash_reporter) \
|
|
|
|
V(electron_renderer_ipc) \
|
|
|
|
V(electron_renderer_web_frame)
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
#define ELECTRON_UTILITY_BINDINGS(V) V(electron_utility_parent_port)
|
2018-01-06 15:58:24 +00:00
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
#define ELECTRON_VIEWS_BINDINGS(V) V(electron_browser_image_view)
|
2018-05-24 06:35:50 +00:00
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
#define ELECTRON_TESTING_BINDINGS(V) V(electron_common_testing)
|
2021-06-17 21:17:25 +00:00
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
// This is used to load built-in bindings. Instead of using
|
2018-01-06 15:58:24 +00:00
|
|
|
// __attribute__((constructor)), we call the _register_<modname>
|
2023-02-09 01:31:38 +00:00
|
|
|
// function for each built-in bindings explicitly. This is only
|
|
|
|
// forward declaration. The definitions are in each binding's
|
|
|
|
// implementation when calling the NODE_LINKED_BINDING_CONTEXT_AWARE.
|
2018-01-06 15:58:24 +00:00
|
|
|
#define V(modname) void _register_##modname();
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_BROWSER_BINDINGS(V)
|
|
|
|
ELECTRON_COMMON_BINDINGS(V)
|
|
|
|
ELECTRON_RENDERER_BINDINGS(V)
|
|
|
|
ELECTRON_UTILITY_BINDINGS(V)
|
2020-03-28 06:44:57 +00:00
|
|
|
#if BUILDFLAG(ENABLE_VIEWS_API)
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_VIEWS_BINDINGS(V)
|
2018-05-24 06:35:50 +00:00
|
|
|
#endif
|
2021-06-17 21:17:25 +00:00
|
|
|
#if DCHECK_IS_ON()
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_TESTING_BINDINGS(V)
|
2021-06-17 21:17:25 +00:00
|
|
|
#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));
|
2022-08-29 13:45:48 +00:00
|
|
|
node::CheckedUvLoopClose(loop);
|
2018-01-04 19:16:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 07:38:31 +00:00
|
|
|
bool g_is_initialized = false;
|
|
|
|
|
2020-07-30 02:04:24 +00:00
|
|
|
void V8FatalErrorCallback(const char* location, const char* message) {
|
|
|
|
LOG(ERROR) << "Fatal error in V8: " << location << " " << message;
|
|
|
|
|
2022-11-14 20:46:52 +00:00
|
|
|
#if !IS_MAS_BUILD()
|
2020-07-30 02:04:24 +00:00
|
|
|
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,
|
2022-12-14 18:05:34 +00:00
|
|
|
v8::Local<v8::String> source) {
|
2020-10-08 19:26:01 +00:00
|
|
|
// If we're running with contextIsolation enabled in the renderer process,
|
|
|
|
// fall back to Blink's logic.
|
2022-12-14 18:05:34 +00:00
|
|
|
if (node::Environment::GetCurrent(context) == nullptr) {
|
2023-04-20 00:27:02 +00:00
|
|
|
if (!electron::IsRendererProcess())
|
2020-10-08 19:26:01 +00:00
|
|
|
return false;
|
|
|
|
return blink::V8Initializer::WasmCodeGenerationCheckCallbackInMainThread(
|
2022-12-14 18:05:34 +00:00
|
|
|
context, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node::AllowWasmCodeGenerationCallback(context, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::ModifyCodeGenerationFromStringsResult ModifyCodeGenerationFromStrings(
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Value> source,
|
|
|
|
bool is_code_like) {
|
|
|
|
if (node::Environment::GetCurrent(context) == nullptr) {
|
2023-04-04 08:31:49 +00:00
|
|
|
// No node environment means we're in the renderer process, either in a
|
|
|
|
// sandboxed renderer or in an unsandboxed renderer with context isolation
|
|
|
|
// enabled.
|
2023-04-20 00:27:02 +00:00
|
|
|
if (!electron::IsRendererProcess()) {
|
2022-12-14 18:05:34 +00:00
|
|
|
NOTREACHED();
|
|
|
|
return {false, {}};
|
|
|
|
}
|
|
|
|
return blink::V8Initializer::CodeGenerationCheckCallbackInMainThread(
|
|
|
|
context, source, is_code_like);
|
2020-10-08 19:26:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 08:31:49 +00:00
|
|
|
// If we get here then we have a node environment, so either a) we're in the
|
2023-04-20 00:27:02 +00:00
|
|
|
// non-rendrer process, or b) we're in the renderer process in a context that
|
|
|
|
// has both node and blink, i.e. contextIsolation disabled.
|
2023-04-04 08:31:49 +00:00
|
|
|
|
|
|
|
// If we're in the renderer with contextIsolation disabled, ask blink first
|
|
|
|
// (for CSP), and iff that allows codegen, delegate to node.
|
2023-04-20 00:27:02 +00:00
|
|
|
if (electron::IsRendererProcess()) {
|
|
|
|
v8::ModifyCodeGenerationFromStringsResult result =
|
|
|
|
blink::V8Initializer::CodeGenerationCheckCallbackInMainThread(
|
|
|
|
context, source, is_code_like);
|
|
|
|
if (!result.codegen_allowed)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in the main process or utility process, delegate to node.
|
2022-12-14 18:05:34 +00:00
|
|
|
return node::ModifyCodeGenerationFromStrings(context, source, is_code_like);
|
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) {
|
2023-01-11 16:59:32 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope(
|
|
|
|
isolate, env->context()->GetMicrotaskQueue(),
|
|
|
|
v8::MicrotasksScope::kDoNotRunMicrotasks);
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:50:11 +00:00
|
|
|
const std::unordered_set<base::StringPiece, base::StringPieceHash>
|
|
|
|
GetAllowedDebugOptions() {
|
|
|
|
if (electron::fuses::IsNodeCliInspectEnabled()) {
|
|
|
|
// Only allow DebugOptions in non-ELECTRON_RUN_AS_NODE mode
|
|
|
|
return {
|
|
|
|
"--inspect", "--inspect-brk",
|
|
|
|
"--inspect-port", "--debug",
|
|
|
|
"--debug-brk", "--debug-port",
|
|
|
|
"--inspect-brk-node", "--inspect-publish-uid",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// If node CLI inspect support is disabled, allow no debug options.
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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")) {
|
2021-08-05 17:50:11 +00:00
|
|
|
if (electron::fuses::IsNodeOptionsEnabled()) {
|
|
|
|
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());
|
|
|
|
}
|
2019-11-02 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 17:50:11 +00:00
|
|
|
// overwrite new NODE_OPTIONS without unsupported variables
|
|
|
|
env->SetVar("NODE_OPTIONS", options);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "NODE_OPTIONS have been disabled in this app";
|
|
|
|
env->SetVar("NODE_OPTIONS", "");
|
|
|
|
}
|
2019-11-02 01:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_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();
|
|
|
|
}
|
2022-03-29 10:03:07 +00:00
|
|
|
|
|
|
|
// Interrupt embed polling when a handle is started.
|
|
|
|
uv_loop_configure(uv_loop_, UV_LOOP_INTERRUPT_ON_IO_CHANGE);
|
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
|
|
|
}
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
void NodeBindings::RegisterBuiltinBindings() {
|
2018-01-06 15:58:24 +00:00
|
|
|
#define V(modname) _register_##modname();
|
2023-06-06 08:19:13 +00:00
|
|
|
if (IsBrowserProcess()) {
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_BROWSER_BINDINGS(V)
|
2020-03-28 06:44:57 +00:00
|
|
|
#if BUILDFLAG(ENABLE_VIEWS_API)
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_VIEWS_BINDINGS(V)
|
2018-06-13 16:15:34 +00:00
|
|
|
#endif
|
2023-02-08 01:22:18 +00:00
|
|
|
}
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_COMMON_BINDINGS(V)
|
2023-06-06 08:19:13 +00:00
|
|
|
if (IsRendererProcess()) {
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_RENDERER_BINDINGS(V)
|
2023-02-08 01:22:18 +00:00
|
|
|
}
|
2023-06-06 08:19:13 +00:00
|
|
|
if (IsUtilityProcess()) {
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_UTILITY_BINDINGS(V)
|
2023-02-08 01:22:18 +00:00
|
|
|
}
|
2021-06-17 21:17:25 +00:00
|
|
|
#if DCHECK_IS_ON()
|
2023-02-09 01:31:38 +00:00
|
|
|
ELECTRON_TESTING_BINDINGS(V)
|
2021-06-17 21:17:25 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2022-11-10 21:31:20 +00:00
|
|
|
// Initialize Node.js cli options to pass to Node.js
|
|
|
|
// See https://nodejs.org/api/cli.html#cli_options
|
|
|
|
void NodeBindings::SetNodeCliFlags() {
|
|
|
|
const std::unordered_set<base::StringPiece, base::StringPieceHash> allowed =
|
|
|
|
GetAllowedDebugOptions();
|
|
|
|
|
|
|
|
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 BUILDFLAG(IS_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to disable Node.js' fetch implementation to prevent
|
|
|
|
// conflict with Blink's in renderer and worker processes.
|
|
|
|
if (browser_env_ == BrowserEnvironment::kRenderer ||
|
|
|
|
browser_env_ == BrowserEnvironment::kWorker) {
|
|
|
|
args.push_back("--no-experimental-fetch");
|
|
|
|
}
|
|
|
|
|
|
|
|
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, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 09:03:46 +00:00
|
|
|
void NodeBindings::Initialize(v8::Local<v8::Context> context) {
|
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.
|
2013-07-01 13:44:25 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_LINUX)
|
2015-06-19 14:56:10 +00:00
|
|
|
// 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
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
// Explicitly register electron's builtin bindings.
|
|
|
|
RegisterBuiltinBindings();
|
2018-01-06 15:58:24 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
// Parse and set Node.js cli flags.
|
|
|
|
SetNodeCliFlags();
|
|
|
|
|
2021-06-04 00:23:06 +00:00
|
|
|
auto 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;
|
2022-10-20 05:49:49 +00:00
|
|
|
uint64_t process_flags = node::ProcessFlags::kNoFlags;
|
|
|
|
// We do not want the child processes spawned from the utility process
|
|
|
|
// to inherit the custom stdio handles created for the parent.
|
|
|
|
if (browser_env_ != BrowserEnvironment::kUtility)
|
|
|
|
process_flags |= node::ProcessFlags::kEnableStdioInheritance;
|
2022-03-23 22:59:54 +00:00
|
|
|
if (!fuses::IsNodeOptionsEnabled())
|
|
|
|
process_flags |= node::ProcessFlags::kDisableNodeOptionsEnv;
|
2021-03-22 09:42:06 +00:00
|
|
|
|
2022-03-23 22:59:54 +00:00
|
|
|
int exit_code = node::InitializeNodeWithArgs(
|
|
|
|
&argv, &exec_argv, &errors,
|
|
|
|
static_cast<node::ProcessFlags::Flags>(process_flags));
|
2021-03-22 09:42:06 +00:00
|
|
|
|
2021-06-17 06:50:56 +00:00
|
|
|
for (const std::string& error : errors)
|
2021-03-22 09:42:06 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", argv[0].c_str(), error.c_str());
|
|
|
|
|
2021-06-17 06:50:56 +00:00
|
|
|
if (exit_code != 0)
|
2021-03-22 09:42:06 +00:00
|
|
|
exit(exit_code);
|
2015-11-06 10:51:34 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2015-11-06 10:51:34 +00:00
|
|
|
// 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
|
|
|
|
2023-02-22 09:03:46 +00:00
|
|
|
gin_helper::internal::Event::GetConstructor(context);
|
2023-02-13 21:39:18 +00:00
|
|
|
|
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,
|
2022-10-20 05:49:49 +00:00
|
|
|
node::MultiIsolatePlatform* platform,
|
|
|
|
std::vector<std::string> args,
|
|
|
|
std::vector<std::string> exec_args) {
|
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;
|
2022-10-20 05:49:49 +00:00
|
|
|
case BrowserEnvironment::kUtility:
|
|
|
|
process_type = "utility";
|
|
|
|
break;
|
2017-03-08 08:33:44 +00:00
|
|
|
}
|
2019-07-16 01:58:39 +00:00
|
|
|
|
2021-09-09 21:49:01 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
gin_helper::Dictionary global(isolate, context->Global());
|
2019-07-16 01:58:39 +00:00
|
|
|
|
2021-09-09 21:49:01 +00:00
|
|
|
if (browser_env_ == BrowserEnvironment::kBrowser) {
|
|
|
|
const std::vector<std::string> search_paths = {"app.asar", "app",
|
|
|
|
"default_app.asar"};
|
|
|
|
const std::vector<std::string> app_asar_search_paths = {"app.asar"};
|
|
|
|
context->Global()->SetPrivate(
|
|
|
|
context,
|
|
|
|
v8::Private::ForApi(
|
|
|
|
isolate,
|
|
|
|
gin::ConvertToV8(isolate, "appSearchPaths").As<v8::String>()),
|
|
|
|
gin::ConvertToV8(isolate,
|
|
|
|
electron::fuses::IsOnlyLoadAppFromAsarEnabled()
|
|
|
|
? app_asar_search_paths
|
|
|
|
: search_paths));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-03-21 07:42:22 +00:00
|
|
|
if (!isolate_data_)
|
|
|
|
isolate_data_ = node::CreateIsolateData(isolate, uv_loop_, platform);
|
2020-09-04 21:53:49 +00:00
|
|
|
|
|
|
|
node::Environment* env;
|
2021-08-31 14:36:04 +00:00
|
|
|
uint64_t flags = node::EnvironmentFlags::kDefaultFlags |
|
2021-10-06 00:20:05 +00:00
|
|
|
node::EnvironmentFlags::kHideConsoleWindows |
|
|
|
|
node::EnvironmentFlags::kNoGlobalSearchPaths;
|
2021-08-31 14:36:04 +00:00
|
|
|
|
2022-10-20 05:49:49 +00:00
|
|
|
if (browser_env_ == BrowserEnvironment::kRenderer ||
|
|
|
|
browser_env_ == BrowserEnvironment::kWorker) {
|
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.
|
2022-11-10 21:31:20 +00:00
|
|
|
// We also avoid overriding globals like setImmediate, clearImmediate
|
|
|
|
// queueMicrotask etc during the bootstrap phase of Node.js
|
|
|
|
// for processes that already have these defined by DOM.
|
|
|
|
// Check //third_party/electron_node/lib/internal/bootstrap/node.js
|
|
|
|
// for the list of overrides on globalThis.
|
2021-08-31 14:36:04 +00:00
|
|
|
flags |= node::EnvironmentFlags::kNoRegisterESMLoader |
|
2022-11-10 21:31:20 +00:00
|
|
|
node::EnvironmentFlags::kNoBrowserGlobals |
|
2022-05-09 22:55:49 +00:00
|
|
|
node::EnvironmentFlags::kNoCreateInspector;
|
2022-03-22 07:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!electron::fuses::IsNodeCliInspectEnabled()) {
|
|
|
|
// If --inspect and friends are disabled we also shouldn't listen for
|
|
|
|
// SIGUSR1
|
|
|
|
flags |= node::EnvironmentFlags::kNoStartDebugSignalHandler;
|
2020-09-04 21:53:49 +00:00
|
|
|
}
|
2019-07-16 01:58:39 +00:00
|
|
|
|
2022-10-04 06:35:20 +00:00
|
|
|
{
|
|
|
|
v8::TryCatch try_catch(isolate);
|
|
|
|
env = node::CreateEnvironment(
|
|
|
|
isolate_data_, context, args, exec_args,
|
|
|
|
static_cast<node::EnvironmentFlags::Flags>(flags));
|
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
std::string err_msg =
|
|
|
|
"Failed to initialize node environment in process: " + process_type;
|
|
|
|
v8::Local<v8::Message> message = try_catch.Message();
|
|
|
|
std::string msg;
|
|
|
|
if (!message.IsEmpty() &&
|
|
|
|
gin::ConvertFromV8(isolate, message->Get(), &msg))
|
|
|
|
err_msg += " , with error: " + msg;
|
|
|
|
LOG(ERROR) << err_msg;
|
|
|
|
}
|
2022-02-17 05:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(env);
|
|
|
|
|
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.
|
2022-10-20 05:49:49 +00:00
|
|
|
// For utility process we expect the process to behave as standard
|
|
|
|
// Node.js runtime and abort the process with appropriate exit
|
|
|
|
// code depending on a handler being set for `uncaughtException` event.
|
|
|
|
if (browser_env_ != BrowserEnvironment::kUtility) {
|
|
|
|
is.should_abort_on_uncaught_exception_callback = [](v8::Isolate*) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
2020-10-06 18:16:49 +00:00
|
|
|
|
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;
|
2023-04-18 20:23:11 +00:00
|
|
|
is.flags |= node::IsolateSettingsFlags::
|
|
|
|
ALLOW_MODIFY_CODE_GENERATION_FROM_STRINGS_CALLBACK;
|
2022-12-14 18:05:34 +00:00
|
|
|
is.modify_code_generation_from_strings_callback =
|
|
|
|
ModifyCodeGenerationFromStrings;
|
2020-10-08 19:26:01 +00:00
|
|
|
|
2022-10-20 05:49:49 +00:00
|
|
|
if (browser_env_ == BrowserEnvironment::kBrowser ||
|
|
|
|
browser_env_ == BrowserEnvironment::kUtility) {
|
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 {
|
2021-06-21 05:06:17 +00:00
|
|
|
// Blink expects the microtasks policy to be kScoped, but Node.js expects it
|
|
|
|
// to be kExplicit. In the renderer, there can be many contexts within the
|
|
|
|
// same isolate, so we don't want to change the existing policy here, which
|
|
|
|
// could be either kExplicit or kScoped depending on whether we're executing
|
|
|
|
// from within a Node.js or a Blink entrypoint. Instead, the policy is
|
|
|
|
// toggled to kExplicit when entering Node.js through UvRunOnce.
|
|
|
|
is.policy = context->GetIsolate()->GetMicrotasksPolicy();
|
2020-07-20 19:41:52 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-10-20 05:49:49 +00:00
|
|
|
node::Environment* NodeBindings::CreateEnvironment(
|
|
|
|
v8::Handle<v8::Context> context,
|
|
|
|
node::MultiIsolatePlatform* platform) {
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
auto& electron_args = ElectronCommandLine::argv();
|
|
|
|
std::vector<std::string> args(electron_args.size());
|
|
|
|
std::transform(electron_args.cbegin(), electron_args.cend(), args.begin(),
|
|
|
|
[](auto& a) { return base::WideToUTF8(a); });
|
|
|
|
#else
|
|
|
|
auto args = ElectronCommandLine::argv();
|
|
|
|
#endif
|
|
|
|
return CreateEnvironment(context, platform, args, {});
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:00:19 +00:00
|
|
|
void NodeBindings::LoadEnvironment(node::Environment* env) {
|
2021-06-17 06:50:56 +00:00
|
|
|
node::LoadEnvironment(env, node::StartExecutionCallback{});
|
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
|
|
|
}
|
|
|
|
|
2022-03-30 03:09:42 +00:00
|
|
|
void NodeBindings::PrepareEmbedThread() {
|
|
|
|
// IOCP does not change for the process until the loop is recreated,
|
|
|
|
// we ensure that there is only a single polling thread satisfying
|
|
|
|
// the concurrency limit set from CreateIoCompletionPort call by
|
|
|
|
// uv_loop_init for the lifetime of this process.
|
|
|
|
// More background can be found at:
|
|
|
|
// https://github.com/microsoft/vscode/issues/142786#issuecomment-1061673400
|
|
|
|
if (initialized_)
|
|
|
|
return;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:09:42 +00:00
|
|
|
void NodeBindings::StartPolling() {
|
|
|
|
// Avoid calling UvRunOnce if the loop is already active,
|
|
|
|
// otherwise it can lead to situations were the number of active
|
|
|
|
// threads processing on IOCP is greater than the concurrency limit.
|
|
|
|
if (initialized_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
initialized_ = true;
|
|
|
|
|
2013-07-22 06:58:25 +00:00
|
|
|
// The MessageLoop should have been created, remember the one in main thread.
|
2023-02-03 11:43:42 +00:00
|
|
|
task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
|
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
|
|
|
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
|
|
|
|
2021-05-03 21:30:12 +00:00
|
|
|
// 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.
|
2023-01-11 16:59:32 +00:00
|
|
|
v8::MicrotaskQueue* microtask_queue = env->context()->GetMicrotaskQueue();
|
|
|
|
auto old_policy = microtask_queue->microtasks_policy();
|
|
|
|
DCHECK_EQ(microtask_queue->GetMicrotasksScopeDepth(), 0);
|
|
|
|
microtask_queue->set_microtasks_policy(v8::MicrotasksPolicy::kExplicit);
|
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");
|
|
|
|
|
2023-01-11 16:59:32 +00:00
|
|
|
microtask_queue->set_microtasks_policy(old_policy);
|
2021-05-03 21:30:12 +00:00
|
|
|
|
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
|