5#include "testing/v8_initializer.h"
12#include "core/fxcrt/fx_memcpy_wrappers.h"
13#include "core/fxcrt/numerics/safe_conversions.h"
14#include "public/fpdfview.h"
15#include "testing/utils/file_util.h"
16#include "testing/utils/path_service.h"
17#include "v8/include/libplatform/libplatform.h"
18#include "v8/include/v8-initialization.h"
19#include "v8/include/v8-snapshot.h"
22#include "v8/include/cppgc/platform.h"
27#ifdef V8_USE_EXTERNAL_STARTUP_DATA
30std::string GetFullPathForSnapshotFile(
const std::string& exe_path,
31 const std::string& bin_dir,
32 const std::string& filename) {
34 if (!bin_dir.empty()) {
36 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
37 result += PATH_SEPARATOR;
39 }
else if (!exe_path.empty()) {
40 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
41 if (last_separator != std::string::npos) {
42 result = exe_path.substr(0, last_separator + 1);
49bool GetExternalData(
const std::string& exe_path,
50 const std::string& bin_dir,
51 const std::string& filename,
52 v8::StartupData* result_data) {
53 std::string full_path =
54 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
55 std::vector<uint8_t> data_buffer = GetFileContents(full_path.c_str());
56 if (data_buffer.empty()) {
61 void* copy = malloc(data_buffer.size());
62 FXSYS_memcpy(copy, data_buffer.data(), data_buffer.size());
63 result_data->data =
static_cast<
char*>(copy);
64 result_data->raw_size = pdfium::checked_cast<
int>(data_buffer.size());
69std::unique_ptr<v8::Platform> InitializeV8Common(
const std::string& exe_path,
70 const std::string& js_flags) {
71 v8::V8::InitializeICUDefaultLocation(exe_path.c_str());
73 std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
74 v8::V8::InitializePlatform(platform.get());
76 cppgc::InitializeProcess(platform->GetPageAllocator());
79 const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags();
80 v8::V8::SetFlagsFromString(recommended_v8_flags);
82 if (!js_flags.empty())
83 v8::V8::SetFlagsFromString(js_flags.c_str());
87 static const char kAdditionalV8Flags[] =
"--predictable --expose-gc";
88 v8::V8::SetFlagsFromString(kAdditionalV8Flags);
96#ifdef V8_USE_EXTERNAL_STARTUP_DATA
97std::unique_ptr<v8::Platform> InitializeV8ForPDFiumWithStartupData(
98 const std::string& exe_path,
99 const std::string& js_flags,
100 const std::string& bin_dir,
101 v8::StartupData* snapshot_blob) {
102 std::unique_ptr<v8::Platform> platform =
103 InitializeV8Common(exe_path, js_flags);
105 if (!GetExternalData(exe_path, bin_dir,
"snapshot_blob.bin", snapshot_blob))
107 v8::V8::SetSnapshotDataBlob(snapshot_blob);
113 const std::string& exe_path,
114 const std::string& js_flags) {
115 return InitializeV8Common(exe_path, js_flags);
121 cppgc::ShutdownProcess();
124 v8::V8::DisposePlatform();
std::unique_ptr< v8::Platform > InitializeV8ForPDFium(const std::string &exe_path, const std::string &js_flags)
void ShutdownV8ForPDFium()