chore: remove node patches by using the preload feature (#41080)

This commit is contained in:
Cheng Zhao 2024-01-25 00:54:32 +09:00 committed by GitHub
parent 031d636823
commit d13a93fb61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 403 additions and 135 deletions

View file

@ -21,7 +21,6 @@
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "content/public/common/content_switches.h"
#include "electron/electron_version.h"
#include "electron/fuses.h"
#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"
@ -260,7 +259,8 @@ int NodeMain(int argc, char* argv[]) {
env = node::CreateEnvironment(
isolate_data, isolate->GetCurrentContext(), result->args(),
result->exec_args(),
static_cast<node::EnvironmentFlags::Flags>(env_flags));
static_cast<node::EnvironmentFlags::Flags>(env_flags), {}, {},
&OnNodePreload);
CHECK_NE(nullptr, env);
node::SetIsolateUpForNode(isolate);
@ -282,11 +282,6 @@ int NodeMain(int argc, char* argv[]) {
#endif
process.Set("crashReporter", reporter);
gin_helper::Dictionary versions;
if (process.Get("versions", &versions)) {
versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING);
}
}
v8::HandleScope scope(isolate);

View file

@ -10,7 +10,6 @@
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
namespace {
@ -191,19 +190,6 @@ class Archive : public node::ObjectWrap {
std::shared_ptr<asar::Archive> archive_;
};
static void InitAsarSupport(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto* isolate = args.GetIsolate();
auto require = args[0];
// Evaluate asar_bundle.js.
std::vector<v8::Local<v8::String>> asar_bundle_params = {
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
std::vector<v8::Local<v8::Value>> asar_bundle_args = {require};
electron::util::CompileAndCall(isolate->GetCurrentContext(),
"electron/js2c/asar_bundle",
&asar_bundle_params, &asar_bundle_args);
}
static void SplitPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto* isolate = args.GetIsolate();
@ -239,7 +225,6 @@ void Initialize(v8::Local<v8::Object> exports,
exports->Set(context, node::FIXED_ONE_BYTE_STRING(isolate, "Archive"), cons)
.Check();
NODE_SET_METHOD(exports, "splitPath", &SplitPath);
NODE_SET_METHOD(exports, "initAsarSupport", &InitAsarSupport);
}
} // namespace

View file

@ -15,8 +15,6 @@
#include "base/process/process_handle.h"
#include "base/process/process_metrics_iocounters.h"
#include "base/system/sys_info.h"
#include "chrome/common/chrome_version.h"
#include "electron/electron_version.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/global_memory_dump.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/memory_instrumentation.h"
#include "shell/browser/browser.h"
@ -84,12 +82,6 @@ void ElectronBindings::BindTo(v8::Isolate* isolate,
dict.SetMethod("activateUvLoop",
base::BindRepeating(&ElectronBindings::ActivateUVLoop,
base::Unretained(this)));
gin_helper::Dictionary versions;
if (dict.Get("versions", &versions)) {
versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING);
versions.SetReadOnly("chrome", CHROME_VERSION_STRING);
}
}
void ElectronBindings::EnvironmentDestroyed(node::Environment* env) {

View file

@ -20,9 +20,11 @@
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "chrome/common/chrome_version.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_paths.h"
#include "electron/buildflags/buildflags.h"
#include "electron/electron_version.h"
#include "electron/fuses.h"
#include "shell/browser/api/electron_api_app.h"
#include "shell/common/api/electron_bindings.h"
@ -34,6 +36,7 @@
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/gin_helper/microtasks_scope.h"
#include "shell/common/mac/main_application_bundle.h"
#include "shell/common/node_util.h"
#include "shell/common/world_ids.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h" // nogncheck
@ -610,7 +613,6 @@ std::shared_ptr<node::Environment> NodeBindings::CreateEnvironment(
electron::fuses::IsOnlyLoadAppFromAsarEnabled()));
}
base::FilePath resources_path = GetResourcesPath();
std::string init_script = "electron/js2c/" + process_type + "_init";
args.insert(args.begin() + 1, init_script);
@ -649,7 +651,8 @@ std::shared_ptr<node::Environment> NodeBindings::CreateEnvironment(
v8::TryCatch try_catch(isolate);
env = node::CreateEnvironment(
static_cast<node::IsolateData*>(isolate_data), context, args, exec_args,
static_cast<node::EnvironmentFlags::Flags>(env_flags));
static_cast<node::EnvironmentFlags::Flags>(env_flags), {}, {},
&OnNodePreload);
if (try_catch.HasCaught()) {
std::string err_msg =
@ -735,11 +738,6 @@ std::shared_ptr<node::Environment> NodeBindings::CreateEnvironment(
gin_helper::Dictionary process(context->GetIsolate(), env->process_object());
process.SetReadOnly("type", process_type);
process.Set("resourcesPath", resources_path);
// The path to helper app.
base::FilePath helper_exec_path;
base::PathService::Get(content::CHILD_PROCESS_EXE, &helper_exec_path);
process.Set("helperExecPath", helper_exec_path);
if (browser_env_ == BrowserEnvironment::kBrowser ||
browser_env_ == BrowserEnvironment::kRenderer) {
@ -931,4 +929,29 @@ void NodeBindings::EmbedThreadRunner(void* arg) {
}
}
void OnNodePreload(node::Environment* env,
v8::Local<v8::Value> process,
v8::Local<v8::Value> require) {
// Set custom process properties.
gin_helper::Dictionary dict(env->isolate(), process.As<v8::Object>());
dict.SetReadOnly("resourcesPath", GetResourcesPath());
base::FilePath helper_exec_path; // path to the helper app.
base::PathService::Get(content::CHILD_PROCESS_EXE, &helper_exec_path);
dict.SetReadOnly("helperExecPath", helper_exec_path);
gin_helper::Dictionary versions;
if (dict.Get("versions", &versions)) {
versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING);
versions.SetReadOnly("chrome", CHROME_VERSION_STRING);
}
// Execute lib/node/init.ts.
std::vector<v8::Local<v8::String>> bundle_params = {
node::FIXED_ONE_BYTE_STRING(env->isolate(), "process"),
node::FIXED_ONE_BYTE_STRING(env->isolate(), "require"),
};
std::vector<v8::Local<v8::Value>> bundle_args = {process, require};
electron::util::CompileAndCall(env->context(), "electron/js2c/node_init",
&bundle_params, &bundle_args);
}
} // namespace electron

View file

@ -219,6 +219,12 @@ class NodeBindings {
base::WeakPtrFactory<NodeBindings> weak_factory_{this};
};
// A thread-safe function responsible for loading preload script which runs for
// all node environments (including child processes and workers).
void OnNodePreload(node::Environment* env,
v8::Local<v8::Value> process,
v8::Local<v8::Value> require);
} // namespace electron
#endif // ELECTRON_SHELL_COMMON_NODE_BINDINGS_H_

View file

@ -5,6 +5,7 @@
#include "shell/common/node_util.h"
#include "base/logging.h"
#include "gin/converter.h"
#include "shell/common/node_includes.h"
namespace electron::util {
@ -31,7 +32,14 @@ v8::MaybeLocal<v8::Value> CompileAndCall(
// 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 CompileAndCall electron script: " << id;
std::string msg = "no error message";
if (!try_catch.Message().IsEmpty()) {
gin::ConvertFromV8(isolate, try_catch.Message()->Get(), &msg);
} else if (try_catch.HasTerminated()) {
msg = "script execution has been terminated";
}
LOG(ERROR) << "Failed to CompileAndCall electron script (" << id
<< "): " << msg;
}
return ret;
}