5#include "testing/v8_initializer.h"
12#include "public/fpdfview.h"
13#include "testing/utils/file_util.h"
14#include "testing/utils/path_service.h"
15#include "third_party/base/numerics/safe_conversions.h"
16#include "v8/include/libplatform/libplatform.h"
17#include "v8/include/v8-initialization.h"
18#include "v8/include/v8-snapshot.h"
21#include "v8/include/cppgc/platform.h"
26#ifdef V8_USE_EXTERNAL_STARTUP_DATA
29std::string GetFullPathForSnapshotFile(
const std::string& exe_path,
30 const std::string& bin_dir,
31 const std::string& filename) {
33 if (!bin_dir.empty()) {
35 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
36 result += PATH_SEPARATOR;
38 }
else if (!exe_path.empty()) {
39 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
40 if (last_separator != std::string::npos) {
41 result = exe_path.substr(0, last_separator + 1);
48bool GetExternalData(
const std::string& exe_path,
49 const std::string& bin_dir,
50 const std::string& filename,
51 v8::StartupData* result_data) {
52 std::string full_path =
53 GetFullPathForSnapshotFile(exe_path, bin_dir, filename);
54 std::vector<uint8_t> data_buffer = GetFileContents(full_path.c_str());
55 if (data_buffer.empty()) {
60 void* copy = malloc(data_buffer.size());
61 memcpy(copy, data_buffer.data(), data_buffer.size());
62 result_data->data =
static_cast<
char*>(copy);
63 result_data->raw_size = pdfium::base::checked_cast<
int>(data_buffer.size());
68std::unique_ptr<v8::Platform> InitializeV8Common(
const std::string& exe_path,
69 const std::string& js_flags) {
70 v8::V8::InitializeICUDefaultLocation(exe_path.c_str());
72 std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
73 v8::V8::InitializePlatform(platform.get());
75 cppgc::InitializeProcess(platform->GetPageAllocator());
78 const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags();
79 v8::V8::SetFlagsFromString(recommended_v8_flags);
81 if (!js_flags.empty())
82 v8::V8::SetFlagsFromString(js_flags.c_str());
86 static const char kAdditionalV8Flags[] =
"--predictable --expose-gc";
87 v8::V8::SetFlagsFromString(kAdditionalV8Flags);
95#ifdef V8_USE_EXTERNAL_STARTUP_DATA
96std::unique_ptr<v8::Platform> InitializeV8ForPDFiumWithStartupData(
97 const std::string& exe_path,
98 const std::string& js_flags,
99 const std::string& bin_dir,
100 v8::StartupData* snapshot_blob) {
101 std::unique_ptr<v8::Platform> platform =
102 InitializeV8Common(exe_path, js_flags);
104 if (!GetExternalData(exe_path, bin_dir,
"snapshot_blob.bin", snapshot_blob))
106 v8::V8::SetSnapshotDataBlob(snapshot_blob);
112 const std::string& exe_path,
113 const std::string& js_flags) {
114 return InitializeV8Common(exe_path, js_flags);
120 cppgc::ShutdownProcess();
123 v8::V8::DisposePlatform();
std::unique_ptr< v8::Platform > InitializeV8ForPDFium(const std::string &exe_path, const std::string &js_flags)
void ShutdownV8ForPDFium()