create_browser_v8_snapshot_file_name_fuse.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 054f38d683280638c7ac618d2ff8f7aef1a0def0..6e9d8d9a043cf8b67e26f70b3a904abcfc3c1a61 100644
  9. --- a/content/app/content_main_runner_impl.cc
  10. +++ b/content/app/content_main_runner_impl.cc
  11. @@ -271,8 +271,13 @@ void AsanProcessInfoCB(const char*, bool*) {
  12. }
  13. #endif // defined(ADDRESS_SANITIZER)
  14. -void LoadV8SnapshotFile(const base::CommandLine& command_line) {
  15. +void LoadV8SnapshotFile(const raw_ptr<ContentMainDelegate> delegate, const base::CommandLine& command_line) {
  16. const gin::V8SnapshotFileType snapshot_type = GetSnapshotType(command_line);
  17. + std::string_view browser_v8_snapshot_file_name = delegate->GetBrowserV8SnapshotFilename();
  18. + if (!browser_v8_snapshot_file_name.empty()) {
  19. + gin::V8Initializer::LoadV8SnapshotFromFileName(browser_v8_snapshot_file_name, snapshot_type);
  20. + return;
  21. + }
  22. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
  23. base::FileDescriptorStore& file_descriptor_store =
  24. base::FileDescriptorStore::GetInstance();
  25. @@ -301,11 +306,12 @@ bool ShouldLoadV8Snapshot(const base::CommandLine& command_line,
  26. #endif // V8_USE_EXTERNAL_STARTUP_DATA
  27. -void LoadV8SnapshotIfNeeded(const base::CommandLine& command_line,
  28. +void LoadV8SnapshotIfNeeded(const raw_ptr<ContentMainDelegate> delegate,
  29. + const base::CommandLine& command_line,
  30. const std::string& process_type) {
  31. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  32. if (ShouldLoadV8Snapshot(command_line, process_type))
  33. - LoadV8SnapshotFile(command_line);
  34. + LoadV8SnapshotFile(delegate, command_line);
  35. #endif // V8_USE_EXTERNAL_STARTUP_DATA
  36. }
  37. @@ -978,7 +984,7 @@ int ContentMainRunnerImpl::Initialize(ContentMainParams params) {
  38. return TerminateForFatalInitializationError();
  39. #endif // BUILDFLAG(IS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
  40. - LoadV8SnapshotIfNeeded(command_line, process_type);
  41. + LoadV8SnapshotIfNeeded(delegate_, command_line, process_type);
  42. blink::TrialTokenValidator::SetOriginTrialPolicyGetter(
  43. base::BindRepeating([]() -> blink::OriginTrialPolicy* {
  44. diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc
  45. index 8194fc8b036482eedb162ff92bb82165cdf3c8d0..f038620516d7783170bc82b3b14dde4e01f3975d 100644
  46. --- a/content/public/app/content_main_delegate.cc
  47. +++ b/content/public/app/content_main_delegate.cc
  48. @@ -4,6 +4,8 @@
  49. #include "content/public/app/content_main_delegate.h"
  50. +#include <string_view>
  51. +
  52. #include "base/check.h"
  53. #include "base/notreached.h"
  54. #include "build/build_config.h"
  55. @@ -72,6 +74,10 @@ std::optional<int> ContentMainDelegate::PostEarlyInitialization(
  56. return std::nullopt;
  57. }
  58. +std::string_view ContentMainDelegate::GetBrowserV8SnapshotFilename() {
  59. + return std::string_view();
  60. +}
  61. +
  62. ContentClient* ContentMainDelegate::CreateContentClient() {
  63. return new ContentClient();
  64. }
  65. diff --git a/content/public/app/content_main_delegate.h b/content/public/app/content_main_delegate.h
  66. index 801bfd401ea4a8e72417d88efaa718cc6fb60883..663fec68d0c2855cdf83bb259b85c22910a67464 100644
  67. --- a/content/public/app/content_main_delegate.h
  68. +++ b/content/public/app/content_main_delegate.h
  69. @@ -8,6 +8,7 @@
  70. #include <memory>
  71. #include <optional>
  72. #include <string>
  73. +#include <string_view>
  74. #include <vector>
  75. #include "base/notreached.h"
  76. @@ -174,6 +175,8 @@ class CONTENT_EXPORT ContentMainDelegate {
  77. virtual bool ShouldHandleConsoleControlEvents();
  78. #endif
  79. + virtual std::string_view GetBrowserV8SnapshotFilename();
  80. +
  81. protected:
  82. friend class ContentClientCreator;
  83. friend class ContentClientInitializer;
  84. diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc
  85. index 578e200a88d89b356e991b3317ff1e71f25ff75e..ae49b7b5e830a7127812219df1c8888b7ba4b348 100644
  86. --- a/gin/v8_initializer.cc
  87. +++ b/gin/v8_initializer.cc
  88. @@ -672,8 +672,7 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** snapshot_data_out,
  89. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  90. -// static
  91. -void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
  92. +void V8Initializer::LoadV8SnapshotFromFileName(std::string_view file_name, V8SnapshotFileType snapshot_file_type) {
  93. if (g_mapped_snapshot) {
  94. // TODO(crbug.com/40558459): Confirm not loading different type of snapshot
  95. // files in a process.
  96. @@ -682,10 +681,17 @@ void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
  97. base::MemoryMappedFile::Region file_region;
  98. base::File file =
  99. - OpenV8File(GetSnapshotFileName(snapshot_file_type), &file_region);
  100. + OpenV8File(file_name.data(), &file_region);
  101. LoadV8SnapshotFromFile(std::move(file), &file_region, snapshot_file_type);
  102. }
  103. +// static
  104. +void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) {
  105. + const char* file_name = GetSnapshotFileName(snapshot_file_type);
  106. +
  107. + LoadV8SnapshotFromFileName(file_name, snapshot_file_type);
  108. +}
  109. +
  110. // static
  111. void V8Initializer::LoadV8SnapshotFromFile(
  112. base::File snapshot_file,
  113. diff --git a/gin/v8_initializer.h b/gin/v8_initializer.h
  114. index ec64afd9dd91b292604ca834a91b9cfbd52eb853..6f7382cd600cd34916d9382878aee4b469dae5d0 100644
  115. --- a/gin/v8_initializer.h
  116. +++ b/gin/v8_initializer.h
  117. @@ -7,6 +7,8 @@
  118. #include <stdint.h>
  119. +#include <string_view>
  120. +
  121. #include "base/files/file.h"
  122. #include "base/files/memory_mapped_file.h"
  123. #include "build/build_config.h"
  124. @@ -43,6 +45,7 @@ class GIN_EXPORT V8Initializer {
  125. int* snapshot_size_out);
  126. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  127. + static void LoadV8SnapshotFromFileName(std::string_view file_name, V8SnapshotFileType snapshot_file_type);
  128. // Load V8 snapshot from default resources, if they are available.
  129. static void LoadV8Snapshot(
  130. V8SnapshotFileType snapshot_file_type = V8SnapshotFileType::kDefault);