create_browser_v8_snapshot_file_name_fuse.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Ryan Manuel <[email protected]>
  3. Date: Thu, 4 Aug 2022 22:37:01 -0500
  4. Subject: Create browser v8 snapshot file name fuse
  5. By default, chromium sets up one v8 snapshot to be used in all v8 contexts. This patch allows consumers
  6. to have a dedicated browser process v8 snapshot defined by the file `browser_v8_context_snapshot.bin`.
  7. diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc
  8. index 496cf165cd37711fcb27fe74e4d3b00709941d19..003a7fd18179ed1df339eeb178b9a2e10a31520f 100644
  9. --- a/content/app/content_main_runner_impl.cc
  10. +++ b/content/app/content_main_runner_impl.cc
  11. @@ -39,6 +39,7 @@
  12. #include "base/process/memory.h"
  13. #include "base/process/process.h"
  14. #include "base/process/process_handle.h"
  15. +#include "base/strings/string_piece.h"
  16. #include "base/strings/string_number_conversions.h"
  17. #include "base/strings/string_util.h"
  18. #include "base/task/single_thread_task_runner.h"
  19. @@ -253,8 +254,13 @@ std::string GetSnapshotDataDescriptor(const base::CommandLine& command_line) {
  20. #endif
  21. -void LoadV8SnapshotFile(const base::CommandLine& command_line) {
  22. +void LoadV8SnapshotFile(const raw_ptr<ContentMainDelegate> delegate, const base::CommandLine& command_line) {
  23. const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line);
  24. + base::StringPiece browser_v8_snapshot_file_name = delegate->GetBrowserV8SnapshotFilename();
  25. + if (!browser_v8_snapshot_file_name.empty()) {
  26. + gin::V8Initializer::LoadV8SnapshotFromFileName(browser_v8_snapshot_file_name, snapshot_type);
  27. + return;
  28. + }
  29. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
  30. base::FileDescriptorStore& file_descriptor_store =
  31. base::FileDescriptorStore::GetInstance();
  32. @@ -283,11 +289,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line,
  33. #endif // V8_USE_EXTERNAL_STARTUP_DATA
  34. -void LoadV8SnapshotIfNeeded(const base::CommandLine& command_line,
  35. +void LoadV8SnapshotIfNeeded(const raw_ptr<ContentMainDelegate> delegate,
  36. + const base::CommandLine& command_line,
  37. const std::string& process_type) {
  38. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  39. if (ShouldLoadV8Snapshot(command_line, process_type))
  40. - LoadV8SnapshotFile(command_line);
  41. + LoadV8SnapshotFile(delegate, command_line);
  42. #endif // V8_USE_EXTERNAL_STARTUP_DATA
  43. }
  44. @@ -967,7 +974,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) {
  45. return TerminateForFatalInitializationError();
  46. #endif // BUILDFLAG(IS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
  47. - LoadV8SnapshotIfNeeded(command_line, process_type);
  48. + LoadV8SnapshotIfNeeded(delegate_, command_line, process_type);
  49. blink::TrialTokenValidator::SetOriginTrialPolicyGetter(
  50. base::BindRepeating([]() -> blink::OriginTrialPolicy* {
  51. diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc
  52. index a687861c04b323102a8d2bfe22b24a964793cd9b..4a7a469111eaec3e1e76ee852bd5afbbc0da2956 100644
  53. --- a/content/public/app/content_main_delegate.cc
  54. +++ b/content/public/app/content_main_delegate.cc
  55. @@ -5,6 +5,7 @@
  56. #include "content/public/app/content_main_delegate.h"
  57. #include "base/check.h"
  58. +#include "base/strings/string_piece.h"
  59. #include "build/build_config.h"
  60. #include "content/public/browser/content_browser_client.h"
  61. #include "content/public/common/content_client.h"
  62. @@ -87,6 +88,10 @@ absl::optional<int> ContentMainDelegate::PostEarlyInitialization(
  63. return absl::nullopt;
  64. }
  65. +base::StringPiece ContentMainDelegate::GetBrowserV8SnapshotFilename() {
  66. + return base::StringPiece();
  67. +}
  68. +
  69. ContentClient* ContentMainDelegate::CreateContentClient() {
  70. return new ContentClient();
  71. }
  72. diff --git a/content/public/app/content_main_delegate.h b/content/public/app/content_main_delegate.h
  73. index c891c5649a6ae76c9f0f988359649ece0e8ac1d9..54e30c4dd82042c283e36cae767dcdd716f94292 100644
  74. --- a/content/public/app/content_main_delegate.h
  75. +++ b/content/public/app/content_main_delegate.h
  76. @@ -9,6 +9,7 @@
  77. #include <string>
  78. #include <vector>
  79. +#include "base/strings/string_piece.h"
  80. #include "build/build_config.h"
  81. #include "content/common/content_export.h"
  82. #include "content/public/common/main_function_params.h"
  83. @@ -168,6 +169,8 @@ class CONTENT_EXPORT ContentMainDelegate {
  84. virtual bool ShouldHandleConsoleControlEvents();
  85. #endif
  86. + virtual base::StringPiece GetBrowserV8SnapshotFilename();
  87. +
  88. protected:
  89. friend class ContentClientCreator;
  90. friend class ContentClientInitializer;
  91. diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc
  92. index 7dca3d0f45b63a270e6705180ffac4a05430f5ec..4bb4848178554033afd8cf6591d14775c33b2fde 100644
  93. --- a/gin/v8_initializer.cc
  94. +++ b/gin/v8_initializer.cc
  95. @@ -555,8 +555,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out,
  96. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  97. -// static
  98. -void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
  99. +void V8Initializer::LoadV8SnapshotFromFileName(base::StringPiece file_name, V8SnapshotFileType snapshot_file_type) {
  100. if (g_mapped_snapshot) {
  101. // TODO(crbug.com/802962): Confirm not loading different type of snapshot
  102. // files in a process.
  103. @@ -565,10 +564,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
  104. base::MemoryMappedFile::Region file_region;
  105. base::File file =
  106. - OpenV8File(GetSnapshotFileName(snapshot_file_type), &file_region);
  107. + OpenV8File(file_name.data(), &file_region);
  108. LoadV8SnapshotFromFile(std::move(file), &file_region, snapshot_file_type);
  109. }
  110. +// static
  111. +void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
  112. + const char* file_name = GetSnapshotFileName(snapshot_file_type);
  113. +
  114. + LoadV8SnapshotFromFileName(file_name, snapshot_file_type);
  115. +}
  116. +
  117. // static
  118. void V8Initializer::LoadV8SnapshotFromFile(
  119. base::File snapshot_file,
  120. diff --git a/gin/v8_initializer.h b/gin/v8_initializer.h
  121. index dd9899c3e0dbadfc0074ffce87d51193e1c963cb..25c747ee645d845a0abfaf63cc66595319df4b51 100644
  122. --- a/gin/v8_initializer.h
  123. +++ b/gin/v8_initializer.h
  124. @@ -9,6 +9,7 @@
  125. #include "base/files/file.h"
  126. #include "base/files/memory_mapped_file.h"
  127. +#include "base/strings/string_piece.h"
  128. #include "build/build_config.h"
  129. #include "gin/array_buffer.h"
  130. #include "gin/gin_export.h"
  131. @@ -42,6 +43,7 @@ class GIN_EXPORT V8Initializer {
  132. int* snapshot_size_out);
  133. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  134. + static void LoadV8SnapshotFromFileName(base::StringPiece file_name, V8SnapshotFileType snapshot_file_type);
  135. // Load V8 snapshot from default resources, if they are available.
  136. static void LoadV8Snapshot(
  137. V8SnapshotFileType snapshot_file_type = V8SnapshotFileType::kDefault);