From 5bef0bb6d32719085402b1ecf64b9b21c6d48f35 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Thu, 4 Aug 2022 22:40:32 -0500 Subject: [PATCH 01/10] Updates to allow for using a custom v8 snapshot file name --- patches/chromium/.patches | 1 + ...using_a_custom_v8_snapshot_file_name.patch | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch diff --git a/patches/chromium/.patches b/patches/chromium/.patches index b2a8de2dd1ba0..e3bc27285f8eb 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -115,3 +115,4 @@ disable_freezing_flags_after_init_in_node.patch short-circuit_permissions_checks_in_mediastreamdevicescontroller.patch chore_add_electron_deps_to_gitignores.patch chore_allow_chromium_to_handle_synthetic_mouse_events_for_touch.patch +updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch diff --git a/patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch new file mode 100644 index 0000000000000..317d50c1e22af --- /dev/null +++ b/patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch @@ -0,0 +1,92 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Ryan Manuel +Date: Thu, 4 Aug 2022 22:37:01 -0500 +Subject: Updates to allow for using a custom v8 snapshot file name + + +diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc +index f19e32fd33ff0ce8f44ecec76e4583273f43b231..e24a23704d93b5209323677c50a66e7f38ebb29b 100644 +--- a/content/app/content_main_runner_impl.cc ++++ b/content/app/content_main_runner_impl.cc +@@ -232,6 +232,11 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { + + void LoadV8SnapshotFile(const base::CommandLine& command_line) { + const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line); ++ const std::string& custom_v8_snapshot_file_name = command_line.GetSwitchValueASCII(switches::kCustomV8SnapshotFileName); ++ if (!custom_v8_snapshot_file_name.empty()) { ++ gin::V8Initializer::LoadV8SnapshotFromFileName(&*custom_v8_snapshot_file_name.begin(), snapshot_type); ++ return; ++ } + #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) + base::FileDescriptorStore& file_descriptor_store = + base::FileDescriptorStore::GetInstance(); +diff --git a/content/public/common/content_switches.cc b/content/public/common/content_switches.cc +index a60146c49d70419227ecc3b09365f472ee5a56ed..06d86901658b575d43b353c88a42e47d617693a2 100644 +--- a/content/public/common/content_switches.cc ++++ b/content/public/common/content_switches.cc +@@ -617,6 +617,8 @@ const char kProcessPerTab[] = "process-per-tab"; + // renderer or plugin host. If it's empty, it's the browser. + const char kProcessType[] = "type"; + ++const char kCustomV8SnapshotFileName[] = "custom-v8-snapshot-file-name"; ++ + // Uses a specified proxy server, overrides system settings. This switch only + // affects HTTP and HTTPS requests. ARC-apps use only HTTP proxy server with the + // highest priority. +diff --git a/content/public/common/content_switches.h b/content/public/common/content_switches.h +index ccc81e32a1a6a3cf90f2c716848d47857045a0c7..6ede3fcc07f83b499578c633408519fc57c979ef 100644 +--- a/content/public/common/content_switches.h ++++ b/content/public/common/content_switches.h +@@ -182,6 +182,7 @@ extern const char kPpapiStartupDialog[]; + CONTENT_EXPORT extern const char kProcessPerSite[]; + CONTENT_EXPORT extern const char kProcessPerTab[]; + CONTENT_EXPORT extern const char kProcessType[]; ++CONTENT_EXPORT extern const char kCustomV8SnapshotFileName[]; + CONTENT_EXPORT extern const char kProxyServer[]; + CONTENT_EXPORT extern const char kPullToRefresh[]; + CONTENT_EXPORT extern const char kQuotaChangeEventInterval[]; +diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc +index 1b41c6ad5681ee85250b27cb9d221995ca1e4b7f..988167eb608c6bfbcda71702f31d6cc3e77e0adb 100644 +--- a/gin/v8_initializer.cc ++++ b/gin/v8_initializer.cc +@@ -511,8 +511,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out, + + #if defined(V8_USE_EXTERNAL_STARTUP_DATA) + +-// static +-void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { ++void V8Initializer::LoadV8SnapshotFromFileName(const char* file_name, V8SnapshotFileType snapshot_file_type) { + if (g_mapped_snapshot) { + // TODO(crbug.com/802962): Confirm not loading different type of snapshot + // files in a process. +@@ -521,10 +520,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { + + base::MemoryMappedFile::Region file_region; + base::File file = +- OpenV8File(GetSnapshotFileName(snapshot_file_type), &file_region); ++ OpenV8File(file_name, &file_region); + LoadV8SnapshotFromFile(std::move(file), &file_region, snapshot_file_type); + } + ++// static ++void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { ++ const char* file_name = GetSnapshotFileName(snapshot_file_type); ++ ++ LoadV8SnapshotFromFileName(file_name, snapshot_file_type); ++} ++ + // static + void V8Initializer::LoadV8SnapshotFromFile( + base::File snapshot_file, +diff --git a/gin/v8_initializer.h b/gin/v8_initializer.h +index 13a120c7fe8e69a44793473f3124c33d572a07a3..70f566c8186e5c0cfbff5d8c67357955fa8226e3 100644 +--- a/gin/v8_initializer.h ++++ b/gin/v8_initializer.h +@@ -42,6 +42,7 @@ class GIN_EXPORT V8Initializer { + int* snapshot_size_out); + + #if defined(V8_USE_EXTERNAL_STARTUP_DATA) ++ static void LoadV8SnapshotFromFileName(const char* file_name, V8SnapshotFileType snapshot_file_type); + // Load V8 snapshot from default resources, if they are available. + static void LoadV8Snapshot( + V8SnapshotFileType snapshot_file_type = V8SnapshotFileType::kDefault); From 067fa3c4335febe33b86df6d7836628855d7fab5 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Mon, 8 Aug 2022 15:48:59 -0500 Subject: [PATCH 02/10] Allow using a custom v8 snapshot file name --- patches/chromium/.patches | 2 +- ...atch => allow_using_a_custom_v8_snapshot_file_name.patch} | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) rename patches/chromium/{updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch => allow_using_a_custom_v8_snapshot_file_name.patch} (94%) diff --git a/patches/chromium/.patches b/patches/chromium/.patches index e3bc27285f8eb..08e0e54776c92 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -115,4 +115,4 @@ disable_freezing_flags_after_init_in_node.patch short-circuit_permissions_checks_in_mediastreamdevicescontroller.patch chore_add_electron_deps_to_gitignores.patch chore_allow_chromium_to_handle_synthetic_mouse_events_for_touch.patch -updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch +allow_using_a_custom_v8_snapshot_file_name.patch diff --git a/patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch similarity index 94% rename from patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch rename to patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index 317d50c1e22af..60277e9d952d0 100644 --- a/patches/chromium/updates_to_allow_for_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -1,8 +1,11 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Thu, 4 Aug 2022 22:37:01 -0500 -Subject: Updates to allow for using a custom v8 snapshot file name +Subject: Allow using a custom v8 snapshot file name +By default, chromium sets up one v8 snapshot to be used in all v8 contexts. This patch allows consumers +to have multiple v8 snapshots loaded in different contexts by passing `custom-v8-snapshot-file-name` +via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc index f19e32fd33ff0ce8f44ecec76e4583273f43b231..e24a23704d93b5209323677c50a66e7f38ebb29b 100644 From fa7e3d3b5f8502665df9d2c780199cabf594f68f Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Fri, 19 Aug 2022 16:47:18 -0500 Subject: [PATCH 03/10] Fix up patch due to merge --- ...llow_using_a_custom_v8_snapshot_file_name.patch | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index 60277e9d952d0..779176f992c94 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -8,7 +8,7 @@ to have multiple v8 snapshots loaded in different contexts by passing `custom-v8 via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc -index f19e32fd33ff0ce8f44ecec76e4583273f43b231..e24a23704d93b5209323677c50a66e7f38ebb29b 100644 +index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..542ac3d594aaafaddaa9df4b63a82884113cb8a1 100644 --- a/content/app/content_main_runner_impl.cc +++ b/content/app/content_main_runner_impl.cc @@ -232,6 +232,11 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { @@ -24,10 +24,10 @@ index f19e32fd33ff0ce8f44ecec76e4583273f43b231..e24a23704d93b5209323677c50a66e7f base::FileDescriptorStore& file_descriptor_store = base::FileDescriptorStore::GetInstance(); diff --git a/content/public/common/content_switches.cc b/content/public/common/content_switches.cc -index a60146c49d70419227ecc3b09365f472ee5a56ed..06d86901658b575d43b353c88a42e47d617693a2 100644 +index 402bd199572ba9d066c10470459c906535d9635b..74252a0dad3af3a3362200e79567802e51664829 100644 --- a/content/public/common/content_switches.cc +++ b/content/public/common/content_switches.cc -@@ -617,6 +617,8 @@ const char kProcessPerTab[] = "process-per-tab"; +@@ -618,6 +618,8 @@ const char kProcessPerTab[] = "process-per-tab"; // renderer or plugin host. If it's empty, it's the browser. const char kProcessType[] = "type"; @@ -37,7 +37,7 @@ index a60146c49d70419227ecc3b09365f472ee5a56ed..06d86901658b575d43b353c88a42e47d // affects HTTP and HTTPS requests. ARC-apps use only HTTP proxy server with the // highest priority. diff --git a/content/public/common/content_switches.h b/content/public/common/content_switches.h -index ccc81e32a1a6a3cf90f2c716848d47857045a0c7..6ede3fcc07f83b499578c633408519fc57c979ef 100644 +index f33a35c1e86be9a50fd060bb8fb59aa44bd76da1..f1148f8da2fa67d23f0a2e3d3c5983cb5eb61e8a 100644 --- a/content/public/common/content_switches.h +++ b/content/public/common/content_switches.h @@ -182,6 +182,7 @@ extern const char kPpapiStartupDialog[]; @@ -49,10 +49,10 @@ index ccc81e32a1a6a3cf90f2c716848d47857045a0c7..6ede3fcc07f83b499578c633408519fc CONTENT_EXPORT extern const char kPullToRefresh[]; CONTENT_EXPORT extern const char kQuotaChangeEventInterval[]; diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc -index 1b41c6ad5681ee85250b27cb9d221995ca1e4b7f..988167eb608c6bfbcda71702f31d6cc3e77e0adb 100644 +index 952b3ba6247935053d9a080bced30e3b3c640dd4..d097b845e9fc356ce3ba7c518aa0b79cf52b9c5b 100644 --- a/gin/v8_initializer.cc +++ b/gin/v8_initializer.cc -@@ -511,8 +511,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out, +@@ -490,8 +490,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out, #if defined(V8_USE_EXTERNAL_STARTUP_DATA) @@ -62,7 +62,7 @@ index 1b41c6ad5681ee85250b27cb9d221995ca1e4b7f..988167eb608c6bfbcda71702f31d6cc3 if (g_mapped_snapshot) { // TODO(crbug.com/802962): Confirm not loading different type of snapshot // files in a process. -@@ -521,10 +520,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { +@@ -500,10 +499,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { base::MemoryMappedFile::Region file_region; base::File file = From d512baa903f40e52ec57233b598b6b1c71654ebb Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Sun, 21 Aug 2022 22:54:09 -0500 Subject: [PATCH 04/10] Use fuse to set up custom v8 snapshot file in browser process --- build/fuses/fuses.json5 | 3 ++- ...using_a_custom_v8_snapshot_file_name.patch | 8 ++++--- shell/app/electron_main_linux.cc | 20 ++++++++++++++---- shell/app/electron_main_mac.cc | 21 +++++++++++++++---- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/build/fuses/fuses.json5 b/build/fuses/fuses.json5 index f4984aa2a17b4..fd27020c8ff74 100644 --- a/build/fuses/fuses.json5 +++ b/build/fuses/fuses.json5 @@ -7,5 +7,6 @@ "node_options": "1", "node_cli_inspect": "1", "embedded_asar_integrity_validation": "0", - "only_load_app_from_asar": "0" + "only_load_app_from_asar": "0", + "load_v8_snapshot_from_custom_path": "0" } diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index 779176f992c94..e3346d8f74bd8 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -8,15 +8,17 @@ to have multiple v8 snapshots loaded in different contexts by passing `custom-v8 via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc -index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..542ac3d594aaafaddaa9df4b63a82884113cb8a1 100644 +index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..b08d658b093b6b70cd4abadee138bc1f76416f27 100644 --- a/content/app/content_main_runner_impl.cc +++ b/content/app/content_main_runner_impl.cc -@@ -232,6 +232,11 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { +@@ -232,6 +232,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { void LoadV8SnapshotFile(const base::CommandLine& command_line) { const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line); ++ const std::string& process_type = ++ command_line.GetSwitchValueASCII(switches::kProcessType); + const std::string& custom_v8_snapshot_file_name = command_line.GetSwitchValueASCII(switches::kCustomV8SnapshotFileName); -+ if (!custom_v8_snapshot_file_name.empty()) { ++ if (process_type.empty() && !custom_v8_snapshot_file_name.empty()) { + gin::V8Initializer::LoadV8SnapshotFromFileName(&*custom_v8_snapshot_file_name.begin(), snapshot_type); + return; + } diff --git a/shell/app/electron_main_linux.cc b/shell/app/electron_main_linux.cc index d06bded3f8391..a331139451d7e 100644 --- a/shell/app/electron_main_linux.cc +++ b/shell/app/electron_main_linux.cc @@ -19,6 +19,18 @@ #include "shell/common/electron_constants.h" int main(int argc, char* argv[]) { + bool use_custom_v8_snapshot_file_name = + electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); + int new_argc = argc + (use_custom_v8_snapshot_file_name ? 1 : 0); + char* new_argv[new_argc]; + for (int i = 0; i < argc; i++) { + new_argv[i] = argv[i]; + } + if (use_custom_v8_snapshot_file_name) { + new_argv[new_argc - 1] = const_cast( + "--custom-v8-snapshot-file-name=custom_v8_context_snapshot.bin"); + } + FixStdioStreams(); #if BUILDFLAG(ENABLE_RUN_AS_NODE) @@ -27,15 +39,15 @@ int main(int argc, char* argv[]) { indicator[0] != '\0') { base::i18n::InitializeICU(); base::AtExitManager atexit_manager; - return electron::NodeMain(argc, argv); + return electron::NodeMain(new_argc, new_argv); } #endif electron::ElectronMainDelegate delegate; content::ContentMainParams params(&delegate); - electron::ElectronCommandLine::Init(argc, argv); - params.argc = argc; - params.argv = const_cast(argv); + electron::ElectronCommandLine::Init(new_argc, new_argv); + params.argc = new_argc; + params.argv = const_cast(new_argv); base::CommandLine::Init(params.argc, params.argv); // TODO(https://crbug.com/1176772): Remove when Chrome Linux is fully migrated // to Crashpad. diff --git a/shell/app/electron_main_mac.cc b/shell/app/electron_main_mac.cc index c817df186e205..100777833d72c 100644 --- a/shell/app/electron_main_mac.cc +++ b/shell/app/electron_main_mac.cc @@ -28,13 +28,26 @@ namespace { } // namespace int main(int argc, char* argv[]) { + bool use_custom_v8_snapshot_file_name = + electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); + int new_argc = argc + (use_custom_v8_snapshot_file_name ? 1 : 0); + char* new_argv[new_argc]; + for (int i = 0; i < argc; i++) { + new_argv[i] = argv[i]; + } + if (use_custom_v8_snapshot_file_name) { + new_argv[new_argc - 1] = const_cast( + "--custom-v8-snapshot-file-name=custom_v8_context_snapshot.bin"); + } + partition_alloc::EarlyMallocZoneRegistration(); FixStdioStreams(); + fprintf(stderr, "main 1\n"); #if BUILDFLAG(ENABLE_RUN_AS_NODE) if (electron::fuses::IsRunAsNodeEnabled() && IsEnvSet("ELECTRON_RUN_AS_NODE")) { - return ElectronInitializeICUandStartNode(argc, argv); + return ElectronInitializeICUandStartNode(new_argc, new_argv); } #endif @@ -53,8 +66,8 @@ int main(int argc, char* argv[]) { abort(); } sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt = - sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc, - argv); + sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), + new_argc, new_argv); if (seatbelt.sandbox_required) { if (!seatbelt.server) { fprintf(stderr, "Failed to create seatbelt sandbox server.\n"); @@ -67,5 +80,5 @@ int main(int argc, char* argv[]) { } #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD) - return ElectronMain(argc, argv); + return ElectronMain(new_argc, new_argv); } From 8f6e9cdd08c8fd73376c6edc45c51e3332e80fab Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Sat, 27 Aug 2022 07:49:11 -0500 Subject: [PATCH 05/10] Refactor to use delegate instead of command line parameter --- ...using_a_custom_v8_snapshot_file_name.patch | 90 ++++++++++++------- shell/app/electron_main_delegate.cc | 15 ++++ shell/app/electron_main_delegate.h | 2 + shell/app/electron_main_linux.cc | 20 +---- shell/app/electron_main_mac.cc | 21 +---- 5 files changed, 84 insertions(+), 64 deletions(-) diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index e3346d8f74bd8..ce0b92c68f2c6 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -8,48 +8,76 @@ to have multiple v8 snapshots loaded in different contexts by passing `custom-v8 via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc -index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..b08d658b093b6b70cd4abadee138bc1f76416f27 100644 +index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..c58d76fc7e20893100af147fd231cd006e78bf5a 100644 --- a/content/app/content_main_runner_impl.cc +++ b/content/app/content_main_runner_impl.cc -@@ -232,6 +232,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { +@@ -230,8 +230,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { - void LoadV8SnapshotFile(const base::CommandLine& command_line) { + #endif + +-void LoadV8SnapshotFile(const base::CommandLine& command_line) { ++void LoadV8SnapshotFile(const raw_ptr delegate, const base::CommandLine& command_line) { const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line); -+ const std::string& process_type = -+ command_line.GetSwitchValueASCII(switches::kProcessType); -+ const std::string& custom_v8_snapshot_file_name = command_line.GetSwitchValueASCII(switches::kCustomV8SnapshotFileName); -+ if (process_type.empty() && !custom_v8_snapshot_file_name.empty()) { -+ gin::V8Initializer::LoadV8SnapshotFromFileName(&*custom_v8_snapshot_file_name.begin(), snapshot_type); ++ const char *custom_v8_snapshot_file_name = delegate->GetCustomV8SnapshotFilename(); ++ if (custom_v8_snapshot_file_name != nullptr) { ++ gin::V8Initializer::LoadV8SnapshotFromFileName(custom_v8_snapshot_file_name, snapshot_type); + return; + } #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) base::FileDescriptorStore& file_descriptor_store = base::FileDescriptorStore::GetInstance(); -diff --git a/content/public/common/content_switches.cc b/content/public/common/content_switches.cc -index 402bd199572ba9d066c10470459c906535d9635b..74252a0dad3af3a3362200e79567802e51664829 100644 ---- a/content/public/common/content_switches.cc -+++ b/content/public/common/content_switches.cc -@@ -618,6 +618,8 @@ const char kProcessPerTab[] = "process-per-tab"; - // renderer or plugin host. If it's empty, it's the browser. - const char kProcessType[] = "type"; +@@ -260,11 +265,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line, + + #endif // V8_USE_EXTERNAL_STARTUP_DATA + +-void LoadV8SnapshotIfNeeded(const base::CommandLine& command_line, ++void LoadV8SnapshotIfNeeded(const raw_ptr delegate, ++ const base::CommandLine& command_line, + const std::string& process_type) { + #if defined(V8_USE_EXTERNAL_STARTUP_DATA) + if (ShouldLoadV8Snapshot(command_line, process_type)) +- LoadV8SnapshotFile(command_line); ++ LoadV8SnapshotFile(delegate, command_line); + #endif // V8_USE_EXTERNAL_STARTUP_DATA + } + +@@ -918,7 +924,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) { + return TerminateForFatalInitializationError(); + #endif // BUILDFLAG(IS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) + +- LoadV8SnapshotIfNeeded(command_line, process_type); ++ LoadV8SnapshotIfNeeded(delegate_, command_line, process_type); + + blink::TrialTokenValidator::SetOriginTrialPolicyGetter( + base::BindRepeating([]() -> blink::OriginTrialPolicy* { +diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc +index 148b1ebe67eee851b5a9a3e054e09fee5a185354..10a1d5ce17a16e0686ffad184d24bf72a1f68ba5 100644 +--- a/content/public/app/content_main_delegate.cc ++++ b/content/public/app/content_main_delegate.cc +@@ -64,6 +64,10 @@ absl::optional ContentMainDelegate::PostEarlyInitialization( + return absl::nullopt; + } + ++char* ContentMainDelegate::GetCustomV8SnapshotFilename() { ++ return nullptr; ++} ++ + ContentClient* ContentMainDelegate::CreateContentClient() { + return new ContentClient(); + } +diff --git a/content/public/app/content_main_delegate.h b/content/public/app/content_main_delegate.h +index f40146c4ed20e63dd09450e43c26736171f02ed4..f0cecef37eb71c675630d84e783bccd14969b722 100644 +--- a/content/public/app/content_main_delegate.h ++++ b/content/public/app/content_main_delegate.h +@@ -154,6 +154,8 @@ class CONTENT_EXPORT ContentMainDelegate { + virtual bool ShouldHandleConsoleControlEvents(); + #endif -+const char kCustomV8SnapshotFileName[] = "custom-v8-snapshot-file-name"; ++ virtual char* GetCustomV8SnapshotFilename(); + - // Uses a specified proxy server, overrides system settings. This switch only - // affects HTTP and HTTPS requests. ARC-apps use only HTTP proxy server with the - // highest priority. -diff --git a/content/public/common/content_switches.h b/content/public/common/content_switches.h -index f33a35c1e86be9a50fd060bb8fb59aa44bd76da1..f1148f8da2fa67d23f0a2e3d3c5983cb5eb61e8a 100644 ---- a/content/public/common/content_switches.h -+++ b/content/public/common/content_switches.h -@@ -182,6 +182,7 @@ extern const char kPpapiStartupDialog[]; - CONTENT_EXPORT extern const char kProcessPerSite[]; - CONTENT_EXPORT extern const char kProcessPerTab[]; - CONTENT_EXPORT extern const char kProcessType[]; -+CONTENT_EXPORT extern const char kCustomV8SnapshotFileName[]; - CONTENT_EXPORT extern const char kProxyServer[]; - CONTENT_EXPORT extern const char kPullToRefresh[]; - CONTENT_EXPORT extern const char kQuotaChangeEventInterval[]; + protected: + friend class ContentClientCreator; + friend class ContentClientInitializer; diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc index 952b3ba6247935053d9a080bced30e3b3c640dd4..d097b845e9fc356ce3ba7c518aa0b79cf52b9c5b 100644 --- a/gin/v8_initializer.cc diff --git a/shell/app/electron_main_delegate.cc b/shell/app/electron_main_delegate.cc index 537f74a14d6d9..f8ecefed369c8 100644 --- a/shell/app/electron_main_delegate.cc +++ b/shell/app/electron_main_delegate.cc @@ -23,6 +23,7 @@ #include "components/content_settings/core/common/content_settings_pattern.h" #include "content/public/common/content_switches.h" #include "electron/buildflags/buildflags.h" +#include "electron/fuses.h" #include "extensions/common/constants.h" #include "ipc/ipc_buildflags.h" #include "sandbox/policy/switches.h" @@ -422,6 +423,20 @@ absl::optional ElectronMainDelegate::PreBrowserMain() { return absl::nullopt; } +char* ElectronMainDelegate::GetCustomV8SnapshotFilename() { + const base::CommandLine* command_line = + base::CommandLine::ForCurrentProcess(); + std::string process_type = + command_line->GetSwitchValueASCII(::switches::kProcessType); + bool use_custom_v8_snapshot_file_name = + process_type.empty() && + electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); + if (use_custom_v8_snapshot_file_name) { + return const_cast("custom_v8_context_snapshot.bin"); + } + return ContentMainDelegate::GetCustomV8SnapshotFilename(); +} + content::ContentBrowserClient* ElectronMainDelegate::CreateContentBrowserClient() { browser_client_ = std::make_unique(); diff --git a/shell/app/electron_main_delegate.h b/shell/app/electron_main_delegate.h index e8e57fad85db6..b53fcad1ffff6 100644 --- a/shell/app/electron_main_delegate.h +++ b/shell/app/electron_main_delegate.h @@ -30,6 +30,8 @@ class ElectronMainDelegate : public content::ContentMainDelegate { ElectronMainDelegate(const ElectronMainDelegate&) = delete; ElectronMainDelegate& operator=(const ElectronMainDelegate&) = delete; + char* GetCustomV8SnapshotFilename() override; + protected: // content::ContentMainDelegate: absl::optional BasicStartupComplete() override; diff --git a/shell/app/electron_main_linux.cc b/shell/app/electron_main_linux.cc index a331139451d7e..d06bded3f8391 100644 --- a/shell/app/electron_main_linux.cc +++ b/shell/app/electron_main_linux.cc @@ -19,18 +19,6 @@ #include "shell/common/electron_constants.h" int main(int argc, char* argv[]) { - bool use_custom_v8_snapshot_file_name = - electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); - int new_argc = argc + (use_custom_v8_snapshot_file_name ? 1 : 0); - char* new_argv[new_argc]; - for (int i = 0; i < argc; i++) { - new_argv[i] = argv[i]; - } - if (use_custom_v8_snapshot_file_name) { - new_argv[new_argc - 1] = const_cast( - "--custom-v8-snapshot-file-name=custom_v8_context_snapshot.bin"); - } - FixStdioStreams(); #if BUILDFLAG(ENABLE_RUN_AS_NODE) @@ -39,15 +27,15 @@ int main(int argc, char* argv[]) { indicator[0] != '\0') { base::i18n::InitializeICU(); base::AtExitManager atexit_manager; - return electron::NodeMain(new_argc, new_argv); + return electron::NodeMain(argc, argv); } #endif electron::ElectronMainDelegate delegate; content::ContentMainParams params(&delegate); - electron::ElectronCommandLine::Init(new_argc, new_argv); - params.argc = new_argc; - params.argv = const_cast(new_argv); + electron::ElectronCommandLine::Init(argc, argv); + params.argc = argc; + params.argv = const_cast(argv); base::CommandLine::Init(params.argc, params.argv); // TODO(https://crbug.com/1176772): Remove when Chrome Linux is fully migrated // to Crashpad. diff --git a/shell/app/electron_main_mac.cc b/shell/app/electron_main_mac.cc index 100777833d72c..c817df186e205 100644 --- a/shell/app/electron_main_mac.cc +++ b/shell/app/electron_main_mac.cc @@ -28,26 +28,13 @@ namespace { } // namespace int main(int argc, char* argv[]) { - bool use_custom_v8_snapshot_file_name = - electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); - int new_argc = argc + (use_custom_v8_snapshot_file_name ? 1 : 0); - char* new_argv[new_argc]; - for (int i = 0; i < argc; i++) { - new_argv[i] = argv[i]; - } - if (use_custom_v8_snapshot_file_name) { - new_argv[new_argc - 1] = const_cast( - "--custom-v8-snapshot-file-name=custom_v8_context_snapshot.bin"); - } - partition_alloc::EarlyMallocZoneRegistration(); FixStdioStreams(); - fprintf(stderr, "main 1\n"); #if BUILDFLAG(ENABLE_RUN_AS_NODE) if (electron::fuses::IsRunAsNodeEnabled() && IsEnvSet("ELECTRON_RUN_AS_NODE")) { - return ElectronInitializeICUandStartNode(new_argc, new_argv); + return ElectronInitializeICUandStartNode(argc, argv); } #endif @@ -66,8 +53,8 @@ int main(int argc, char* argv[]) { abort(); } sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt = - sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), - new_argc, new_argv); + sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc, + argv); if (seatbelt.sandbox_required) { if (!seatbelt.server) { fprintf(stderr, "Failed to create seatbelt sandbox server.\n"); @@ -80,5 +67,5 @@ int main(int argc, char* argv[]) { } #endif // defined(HELPER_EXECUTABLE) && !defined(MAS_BUILD) - return ElectronMain(new_argc, new_argv); + return ElectronMain(argc, argv); } From 55a95b0c4b8ca3cf0662d77d6fac83e9be8d992e Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Mon, 29 Aug 2022 12:15:15 -0500 Subject: [PATCH 06/10] Refactoring --- ...using_a_custom_v8_snapshot_file_name.patch | 70 ++++++++++++++----- shell/app/electron_main_delegate.cc | 4 +- shell/app/electron_main_delegate.h | 2 +- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index ce0b92c68f2c6..6132df6f73687 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -8,25 +8,33 @@ to have multiple v8 snapshots loaded in different contexts by passing `custom-v8 via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc -index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..c58d76fc7e20893100af147fd231cd006e78bf5a 100644 +index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..ac615ba77696f9cb747418ec9a887b3f1ad32360 100644 --- a/content/app/content_main_runner_impl.cc +++ b/content/app/content_main_runner_impl.cc -@@ -230,8 +230,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { +@@ -37,6 +37,7 @@ + #include "base/process/memory.h" + #include "base/process/process.h" + #include "base/process/process_handle.h" ++#include "base/strings/string_piece.h" + #include "base/strings/string_number_conversions.h" + #include "base/strings/string_util.h" + #include "base/task/thread_pool/thread_pool_instance.h" +@@ -230,8 +231,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { #endif -void LoadV8SnapshotFile(const base::CommandLine& command_line) { +void LoadV8SnapshotFile(const raw_ptr delegate, const base::CommandLine& command_line) { const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line); -+ const char *custom_v8_snapshot_file_name = delegate->GetCustomV8SnapshotFilename(); -+ if (custom_v8_snapshot_file_name != nullptr) { ++ base::StringPiece custom_v8_snapshot_file_name = delegate->GetCustomV8SnapshotFilename(); ++ if (!custom_v8_snapshot_file_name.empty()) { + gin::V8Initializer::LoadV8SnapshotFromFileName(custom_v8_snapshot_file_name, snapshot_type); + return; + } #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) base::FileDescriptorStore& file_descriptor_store = base::FileDescriptorStore::GetInstance(); -@@ -260,11 +265,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line, +@@ -260,11 +266,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line, #endif // V8_USE_EXTERNAL_STARTUP_DATA @@ -41,7 +49,7 @@ index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..c58d76fc7e20893100af147fd231cd00 #endif // V8_USE_EXTERNAL_STARTUP_DATA } -@@ -918,7 +924,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) { +@@ -918,7 +925,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) { return TerminateForFatalInitializationError(); #endif // BUILDFLAG(IS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) @@ -51,35 +59,51 @@ index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..c58d76fc7e20893100af147fd231cd00 blink::TrialTokenValidator::SetOriginTrialPolicyGetter( base::BindRepeating([]() -> blink::OriginTrialPolicy* { diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc -index 148b1ebe67eee851b5a9a3e054e09fee5a185354..10a1d5ce17a16e0686ffad184d24bf72a1f68ba5 100644 +index 148b1ebe67eee851b5a9a3e054e09fee5a185354..236f81bbb55d2b314608ad5f418513be2bc09e4b 100644 --- a/content/public/app/content_main_delegate.cc +++ b/content/public/app/content_main_delegate.cc -@@ -64,6 +64,10 @@ absl::optional ContentMainDelegate::PostEarlyInitialization( +@@ -5,6 +5,7 @@ + #include "content/public/app/content_main_delegate.h" + + #include "base/check.h" ++#include "base/strings/string_piece.h" + #include "build/build_config.h" + #include "content/public/browser/content_browser_client.h" + #include "content/public/common/content_client.h" +@@ -64,6 +65,10 @@ absl::optional ContentMainDelegate::PostEarlyInitialization( return absl::nullopt; } -+char* ContentMainDelegate::GetCustomV8SnapshotFilename() { -+ return nullptr; ++base::StringPiece ContentMainDelegate::GetCustomV8SnapshotFilename() { ++ return base::StringPiece(); +} + ContentClient* ContentMainDelegate::CreateContentClient() { return new ContentClient(); } diff --git a/content/public/app/content_main_delegate.h b/content/public/app/content_main_delegate.h -index f40146c4ed20e63dd09450e43c26736171f02ed4..f0cecef37eb71c675630d84e783bccd14969b722 100644 +index f40146c4ed20e63dd09450e43c26736171f02ed4..5a89cbf6a3965b8deb69c1c151168116a90031cb 100644 --- a/content/public/app/content_main_delegate.h +++ b/content/public/app/content_main_delegate.h -@@ -154,6 +154,8 @@ class CONTENT_EXPORT ContentMainDelegate { +@@ -9,6 +9,7 @@ + #include + #include + ++#include "base/strings/string_piece.h" + #include "build/build_config.h" + #include "content/common/content_export.h" + #include "content/public/common/main_function_params.h" +@@ -154,6 +155,8 @@ class CONTENT_EXPORT ContentMainDelegate { virtual bool ShouldHandleConsoleControlEvents(); #endif -+ virtual char* GetCustomV8SnapshotFilename(); ++ virtual base::StringPiece GetCustomV8SnapshotFilename(); + protected: friend class ContentClientCreator; friend class ContentClientInitializer; diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc -index 952b3ba6247935053d9a080bced30e3b3c640dd4..d097b845e9fc356ce3ba7c518aa0b79cf52b9c5b 100644 +index 952b3ba6247935053d9a080bced30e3b3c640dd4..fa391aa2239c8183a75162d2c6a7d72713a9c79e 100644 --- a/gin/v8_initializer.cc +++ b/gin/v8_initializer.cc @@ -490,8 +490,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out, @@ -88,7 +112,7 @@ index 952b3ba6247935053d9a080bced30e3b3c640dd4..d097b845e9fc356ce3ba7c518aa0b79c -// static -void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { -+void V8Initializer::LoadV8SnapshotFromFileName(const char* file_name, V8SnapshotFileType snapshot_file_type) { ++void V8Initializer::LoadV8SnapshotFromFileName(base::StringPiece file_name, V8SnapshotFileType snapshot_file_type) { if (g_mapped_snapshot) { // TODO(crbug.com/802962): Confirm not loading different type of snapshot // files in a process. @@ -97,7 +121,7 @@ index 952b3ba6247935053d9a080bced30e3b3c640dd4..d097b845e9fc356ce3ba7c518aa0b79c base::MemoryMappedFile::Region file_region; base::File file = - OpenV8File(GetSnapshotFileName(snapshot_file_type), &file_region); -+ OpenV8File(file_name, &file_region); ++ OpenV8File(file_name.data(), &file_region); LoadV8SnapshotFromFile(std::move(file), &file_region, snapshot_file_type); } @@ -112,14 +136,22 @@ index 952b3ba6247935053d9a080bced30e3b3c640dd4..d097b845e9fc356ce3ba7c518aa0b79c void V8Initializer::LoadV8SnapshotFromFile( base::File snapshot_file, diff --git a/gin/v8_initializer.h b/gin/v8_initializer.h -index 13a120c7fe8e69a44793473f3124c33d572a07a3..70f566c8186e5c0cfbff5d8c67357955fa8226e3 100644 +index 13a120c7fe8e69a44793473f3124c33d572a07a3..acb294780873c1d84546eb2b9acc00f86838361d 100644 --- a/gin/v8_initializer.h +++ b/gin/v8_initializer.h -@@ -42,6 +42,7 @@ class GIN_EXPORT V8Initializer { +@@ -9,6 +9,7 @@ + + #include "base/files/file.h" + #include "base/files/memory_mapped_file.h" ++#include "base/strings/string_piece.h" + #include "build/build_config.h" + #include "gin/array_buffer.h" + #include "gin/gin_export.h" +@@ -42,6 +43,7 @@ class GIN_EXPORT V8Initializer { int* snapshot_size_out); #if defined(V8_USE_EXTERNAL_STARTUP_DATA) -+ static void LoadV8SnapshotFromFileName(const char* file_name, V8SnapshotFileType snapshot_file_type); ++ static void LoadV8SnapshotFromFileName(base::StringPiece file_name, V8SnapshotFileType snapshot_file_type); // Load V8 snapshot from default resources, if they are available. static void LoadV8Snapshot( V8SnapshotFileType snapshot_file_type = V8SnapshotFileType::kDefault); diff --git a/shell/app/electron_main_delegate.cc b/shell/app/electron_main_delegate.cc index f8ecefed369c8..69cc99eb8f5a2 100644 --- a/shell/app/electron_main_delegate.cc +++ b/shell/app/electron_main_delegate.cc @@ -423,7 +423,7 @@ absl::optional ElectronMainDelegate::PreBrowserMain() { return absl::nullopt; } -char* ElectronMainDelegate::GetCustomV8SnapshotFilename() { +base::StringPiece ElectronMainDelegate::GetCustomV8SnapshotFilename() { const base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); std::string process_type = @@ -432,7 +432,7 @@ char* ElectronMainDelegate::GetCustomV8SnapshotFilename() { process_type.empty() && electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); if (use_custom_v8_snapshot_file_name) { - return const_cast("custom_v8_context_snapshot.bin"); + return "custom_v8_context_snapshot.bin"; } return ContentMainDelegate::GetCustomV8SnapshotFilename(); } diff --git a/shell/app/electron_main_delegate.h b/shell/app/electron_main_delegate.h index b53fcad1ffff6..2e980e8fc590f 100644 --- a/shell/app/electron_main_delegate.h +++ b/shell/app/electron_main_delegate.h @@ -30,7 +30,7 @@ class ElectronMainDelegate : public content::ContentMainDelegate { ElectronMainDelegate(const ElectronMainDelegate&) = delete; ElectronMainDelegate& operator=(const ElectronMainDelegate&) = delete; - char* GetCustomV8SnapshotFilename() override; + base::StringPiece GetCustomV8SnapshotFilename() override; protected: // content::ContentMainDelegate: From e09c894f4c4550a1092d32e38996922569470606 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Thu, 8 Sep 2022 11:53:01 -0500 Subject: [PATCH 07/10] Update due to merge --- ..._using_a_custom_v8_snapshot_file_name.patch | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index 6132df6f73687..5b73e1dd6eda5 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -8,7 +8,7 @@ to have multiple v8 snapshots loaded in different contexts by passing `custom-v8 via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc -index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..ac615ba77696f9cb747418ec9a887b3f1ad32360 100644 +index 6242f0a0070b2b862f9fa5ed70825a16d6e270ef..58a0606060df1d0484d7ba32dafcf0a21c5f1621 100644 --- a/content/app/content_main_runner_impl.cc +++ b/content/app/content_main_runner_impl.cc @@ -37,6 +37,7 @@ @@ -19,7 +19,7 @@ index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..ac615ba77696f9cb747418ec9a887b3f #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "base/task/thread_pool/thread_pool_instance.h" -@@ -230,8 +231,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { +@@ -232,8 +233,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) { #endif @@ -34,7 +34,7 @@ index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..ac615ba77696f9cb747418ec9a887b3f #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) base::FileDescriptorStore& file_descriptor_store = base::FileDescriptorStore::GetInstance(); -@@ -260,11 +266,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line, +@@ -262,11 +268,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line, #endif // V8_USE_EXTERNAL_STARTUP_DATA @@ -49,7 +49,7 @@ index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..ac615ba77696f9cb747418ec9a887b3f #endif // V8_USE_EXTERNAL_STARTUP_DATA } -@@ -918,7 +925,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) { +@@ -925,7 +932,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) { return TerminateForFatalInitializationError(); #endif // BUILDFLAG(IS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) @@ -59,7 +59,7 @@ index ef51df5e85584329c9c02aa2b6b88ba9eadd1a31..ac615ba77696f9cb747418ec9a887b3f blink::TrialTokenValidator::SetOriginTrialPolicyGetter( base::BindRepeating([]() -> blink::OriginTrialPolicy* { diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc -index 148b1ebe67eee851b5a9a3e054e09fee5a185354..236f81bbb55d2b314608ad5f418513be2bc09e4b 100644 +index 5450eb6ba565164953b778f861d8fc75a06b6115..ffe1cbfb3a532d129dc6c265faf9da5e6770e20e 100644 --- a/content/public/app/content_main_delegate.cc +++ b/content/public/app/content_main_delegate.cc @@ -5,6 +5,7 @@ @@ -70,7 +70,7 @@ index 148b1ebe67eee851b5a9a3e054e09fee5a185354..236f81bbb55d2b314608ad5f418513be #include "build/build_config.h" #include "content/public/browser/content_browser_client.h" #include "content/public/common/content_client.h" -@@ -64,6 +65,10 @@ absl::optional ContentMainDelegate::PostEarlyInitialization( +@@ -83,6 +84,10 @@ absl::optional ContentMainDelegate::PostEarlyInitialization( return absl::nullopt; } @@ -103,10 +103,10 @@ index f40146c4ed20e63dd09450e43c26736171f02ed4..5a89cbf6a3965b8deb69c1c151168116 friend class ContentClientCreator; friend class ContentClientInitializer; diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc -index 952b3ba6247935053d9a080bced30e3b3c640dd4..fa391aa2239c8183a75162d2c6a7d72713a9c79e 100644 +index 5530d975303cc96701e4b70ffbcaf6e7c02bb016..edd9959cc0fd23711e19de4aee104199a8a3599e 100644 --- a/gin/v8_initializer.cc +++ b/gin/v8_initializer.cc -@@ -490,8 +490,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out, +@@ -496,8 +496,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out, #if defined(V8_USE_EXTERNAL_STARTUP_DATA) @@ -116,7 +116,7 @@ index 952b3ba6247935053d9a080bced30e3b3c640dd4..fa391aa2239c8183a75162d2c6a7d727 if (g_mapped_snapshot) { // TODO(crbug.com/802962): Confirm not loading different type of snapshot // files in a process. -@@ -500,10 +499,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { +@@ -506,10 +505,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { base::MemoryMappedFile::Region file_region; base::File file = From 28d73aaf9c57826da206758c01667e3e5a6a37eb Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Wed, 14 Sep 2022 11:34:24 -0500 Subject: [PATCH 08/10] PR comments --- build/fuses/fuses.json5 | 2 +- docs/tutorial/fuses.md | 7 +++++++ ...ow_using_a_custom_v8_snapshot_file_name.patch | 16 ++++++++-------- shell/app/electron_main_delegate.cc | 12 ++++++------ shell/app/electron_main_delegate.h | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/build/fuses/fuses.json5 b/build/fuses/fuses.json5 index fd27020c8ff74..e8df5ffd7ab98 100644 --- a/build/fuses/fuses.json5 +++ b/build/fuses/fuses.json5 @@ -8,5 +8,5 @@ "node_cli_inspect": "1", "embedded_asar_integrity_validation": "0", "only_load_app_from_asar": "0", - "load_v8_snapshot_from_custom_path": "0" + "load_browser_process_specific_v8_snapshot": "0" } diff --git a/docs/tutorial/fuses.md b/docs/tutorial/fuses.md index bb0c61b475136..d933a841c46c5 100644 --- a/docs/tutorial/fuses.md +++ b/docs/tutorial/fuses.md @@ -54,6 +54,13 @@ For more information on how to use asar integrity validation please read the [As The onlyLoadAppFromAsar fuse changes the search system that Electron uses to locate your app code. By default Electron will search in the following order `app.asar` -> `app` -> `default_app.asar`. When this fuse is enabled the search order becomes a single entry `app.asar` thus ensuring that when combined with the `embeddedAsarIntegrityValidation` fuse it is impossible to load non-validated code. +### `loadBrowserProcessSpecificV8Snapshot` + +**Default:** Disabled +**@electron/fuses:** `FuseV1Options.LoadBrowserProcessSpecificV8Snapshot` + +The loadBrowserProcessSpecificV8Snapshot fuse changes which V8 snapshot file is used for the browser process. By default Electron's processes will all use the same V8 snapshot file. When this fuse is enabled the browser process uses the file called `browser_v8_context_snapshot.bin` for its V8 snapshot. The other processes will use the V8 snapshot file that they normally do. + ## How do I flip the fuses? ### The easy way diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch index 5b73e1dd6eda5..88df6d5a8de9e 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch @@ -8,7 +8,7 @@ to have multiple v8 snapshots loaded in different contexts by passing `custom-v8 via the command line. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc -index 6242f0a0070b2b862f9fa5ed70825a16d6e270ef..58a0606060df1d0484d7ba32dafcf0a21c5f1621 100644 +index 6242f0a0070b2b862f9fa5ed70825a16d6e270ef..af355d4b0adb509f6b04d8bc2eddbb3a6b3c9a08 100644 --- a/content/app/content_main_runner_impl.cc +++ b/content/app/content_main_runner_impl.cc @@ -37,6 +37,7 @@ @@ -26,9 +26,9 @@ index 6242f0a0070b2b862f9fa5ed70825a16d6e270ef..58a0606060df1d0484d7ba32dafcf0a2 -void LoadV8SnapshotFile(const base::CommandLine& command_line) { +void LoadV8SnapshotFile(const raw_ptr delegate, const base::CommandLine& command_line) { const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line); -+ base::StringPiece custom_v8_snapshot_file_name = delegate->GetCustomV8SnapshotFilename(); -+ if (!custom_v8_snapshot_file_name.empty()) { -+ gin::V8Initializer::LoadV8SnapshotFromFileName(custom_v8_snapshot_file_name, snapshot_type); ++ base::StringPiece browser_v8_snapshot_file_name = delegate->GetBrowserV8SnapshotFilename(); ++ if (!browser_v8_snapshot_file_name.empty()) { ++ gin::V8Initializer::LoadV8SnapshotFromFileName(browser_v8_snapshot_file_name, snapshot_type); + return; + } #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) @@ -59,7 +59,7 @@ index 6242f0a0070b2b862f9fa5ed70825a16d6e270ef..58a0606060df1d0484d7ba32dafcf0a2 blink::TrialTokenValidator::SetOriginTrialPolicyGetter( base::BindRepeating([]() -> blink::OriginTrialPolicy* { diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc -index 5450eb6ba565164953b778f861d8fc75a06b6115..ffe1cbfb3a532d129dc6c265faf9da5e6770e20e 100644 +index 5450eb6ba565164953b778f861d8fc75a06b6115..3f15d5a83d6e8011da09da178a0a9dfd2dd95d30 100644 --- a/content/public/app/content_main_delegate.cc +++ b/content/public/app/content_main_delegate.cc @@ -5,6 +5,7 @@ @@ -74,7 +74,7 @@ index 5450eb6ba565164953b778f861d8fc75a06b6115..ffe1cbfb3a532d129dc6c265faf9da5e return absl::nullopt; } -+base::StringPiece ContentMainDelegate::GetCustomV8SnapshotFilename() { ++base::StringPiece ContentMainDelegate::GetBrowserV8SnapshotFilename() { + return base::StringPiece(); +} + @@ -82,7 +82,7 @@ index 5450eb6ba565164953b778f861d8fc75a06b6115..ffe1cbfb3a532d129dc6c265faf9da5e return new ContentClient(); } diff --git a/content/public/app/content_main_delegate.h b/content/public/app/content_main_delegate.h -index f40146c4ed20e63dd09450e43c26736171f02ed4..5a89cbf6a3965b8deb69c1c151168116a90031cb 100644 +index f40146c4ed20e63dd09450e43c26736171f02ed4..5e3246a1346bd0210e7b83842a17dcc1986c8647 100644 --- a/content/public/app/content_main_delegate.h +++ b/content/public/app/content_main_delegate.h @@ -9,6 +9,7 @@ @@ -97,7 +97,7 @@ index f40146c4ed20e63dd09450e43c26736171f02ed4..5a89cbf6a3965b8deb69c1c151168116 virtual bool ShouldHandleConsoleControlEvents(); #endif -+ virtual base::StringPiece GetCustomV8SnapshotFilename(); ++ virtual base::StringPiece GetBrowserV8SnapshotFilename(); + protected: friend class ContentClientCreator; diff --git a/shell/app/electron_main_delegate.cc b/shell/app/electron_main_delegate.cc index 69cc99eb8f5a2..73d063026ad3e 100644 --- a/shell/app/electron_main_delegate.cc +++ b/shell/app/electron_main_delegate.cc @@ -423,18 +423,18 @@ absl::optional ElectronMainDelegate::PreBrowserMain() { return absl::nullopt; } -base::StringPiece ElectronMainDelegate::GetCustomV8SnapshotFilename() { +base::StringPiece ElectronMainDelegate::GetBrowserV8SnapshotFilename() { const base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); std::string process_type = command_line->GetSwitchValueASCII(::switches::kProcessType); - bool use_custom_v8_snapshot_file_name = + bool load_browser_process_specific_v8_snapshot = process_type.empty() && - electron::fuses::IsLoadV8SnapshotFromCustomPathEnabled(); - if (use_custom_v8_snapshot_file_name) { - return "custom_v8_context_snapshot.bin"; + electron::fuses::IsLoadBrowserProcessSpecificV8SnapshotEnabled(); + if (load_browser_process_specific_v8_snapshot) { + return "browser_v8_context_snapshot.bin"; } - return ContentMainDelegate::GetCustomV8SnapshotFilename(); + return ContentMainDelegate::GetBrowserV8SnapshotFilename(); } content::ContentBrowserClient* diff --git a/shell/app/electron_main_delegate.h b/shell/app/electron_main_delegate.h index 2e980e8fc590f..0a8363fb7a90b 100644 --- a/shell/app/electron_main_delegate.h +++ b/shell/app/electron_main_delegate.h @@ -30,7 +30,7 @@ class ElectronMainDelegate : public content::ContentMainDelegate { ElectronMainDelegate(const ElectronMainDelegate&) = delete; ElectronMainDelegate& operator=(const ElectronMainDelegate&) = delete; - base::StringPiece GetCustomV8SnapshotFilename() override; + base::StringPiece GetBrowserV8SnapshotFilename() override; protected: // content::ContentMainDelegate: From 4e7ef9d88b87caf6a340f6e12ef718de1192a6d4 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Wed, 14 Sep 2022 17:01:57 -0500 Subject: [PATCH 09/10] Rename patch --- patches/chromium/.patches | 2 +- ....patch => allow_using_a_browser_v8_snapshot_file_name.patch} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename patches/chromium/{allow_using_a_custom_v8_snapshot_file_name.patch => allow_using_a_browser_v8_snapshot_file_name.patch} (99%) diff --git a/patches/chromium/.patches b/patches/chromium/.patches index 22447f8339744..acd9567cbb85c 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -121,4 +121,4 @@ fix_revert_emulationhandler_update_functions_to_early_return.patch fix_crash_loading_non-standard_schemes_in_iframes.patch disable_optimization_guide_for_preconnect_feature.patch fix_return_v8_value_from_localframe_requestexecutescript.patch -allow_using_a_custom_v8_snapshot_file_name.patch +allow_using_a_browser_v8_snapshot_file_name.patch diff --git a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch b/patches/chromium/allow_using_a_browser_v8_snapshot_file_name.patch similarity index 99% rename from patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch rename to patches/chromium/allow_using_a_browser_v8_snapshot_file_name.patch index 88df6d5a8de9e..c4fed7428da96 100644 --- a/patches/chromium/allow_using_a_custom_v8_snapshot_file_name.patch +++ b/patches/chromium/allow_using_a_browser_v8_snapshot_file_name.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Thu, 4 Aug 2022 22:37:01 -0500 -Subject: Allow using a custom v8 snapshot file name +Subject: Allow using a browser v8 snapshot file name By default, chromium sets up one v8 snapshot to be used in all v8 contexts. This patch allows consumers to have multiple v8 snapshots loaded in different contexts by passing `custom-v8-snapshot-file-name` From 3f5e3247a757b0c93994ce2a4ecc557562364312 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Wed, 14 Sep 2022 17:05:59 -0500 Subject: [PATCH 10/10] Rename patch --- patches/chromium/.patches | 2 +- ...patch => create_browser_v8_snapshot_file_name_fuse.patch} | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) rename patches/chromium/{allow_using_a_browser_v8_snapshot_file_name.patch => create_browser_v8_snapshot_file_name_fuse.patch} (97%) diff --git a/patches/chromium/.patches b/patches/chromium/.patches index acd9567cbb85c..efa176d161397 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -121,4 +121,4 @@ fix_revert_emulationhandler_update_functions_to_early_return.patch fix_crash_loading_non-standard_schemes_in_iframes.patch disable_optimization_guide_for_preconnect_feature.patch fix_return_v8_value_from_localframe_requestexecutescript.patch -allow_using_a_browser_v8_snapshot_file_name.patch +create_browser_v8_snapshot_file_name_fuse.patch diff --git a/patches/chromium/allow_using_a_browser_v8_snapshot_file_name.patch b/patches/chromium/create_browser_v8_snapshot_file_name_fuse.patch similarity index 97% rename from patches/chromium/allow_using_a_browser_v8_snapshot_file_name.patch rename to patches/chromium/create_browser_v8_snapshot_file_name_fuse.patch index c4fed7428da96..6b074078b5735 100644 --- a/patches/chromium/allow_using_a_browser_v8_snapshot_file_name.patch +++ b/patches/chromium/create_browser_v8_snapshot_file_name_fuse.patch @@ -1,11 +1,10 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Thu, 4 Aug 2022 22:37:01 -0500 -Subject: Allow using a browser v8 snapshot file name +Subject: Create browser v8 snapshot file name fuse By default, chromium sets up one v8 snapshot to be used in all v8 contexts. This patch allows consumers -to have multiple v8 snapshots loaded in different contexts by passing `custom-v8-snapshot-file-name` -via the command line. +to have a dedicated browser process v8 snapshot defined by the file `browser_v8_context_snapshot.bin`. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc index 6242f0a0070b2b862f9fa5ed70825a16d6e270ef..af355d4b0adb509f6b04d8bc2eddbb3a6b3c9a08 100644