Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
v8_initializer.cpp
Go to the documentation of this file.
1// Copyright 2019 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "testing/v8_initializer.h"
6
7#include <stdlib.h>
8
9#include <cstring>
10#include <vector>
11
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"
19
20#ifdef PDF_ENABLE_XFA
21#include "v8/include/cppgc/platform.h"
22#endif
23
24namespace {
25
26#ifdef V8_USE_EXTERNAL_STARTUP_DATA
27// Returns the full path for an external V8 data file based on either
28// the currect exectuable path or an explicit override.
29std::string GetFullPathForSnapshotFile(const std::string& exe_path,
30 const std::string& bin_dir,
31 const std::string& filename) {
32 std::string result;
33 if (!bin_dir.empty()) {
34 result = bin_dir;
35 if (*bin_dir.rbegin() != PATH_SEPARATOR) {
36 result += PATH_SEPARATOR;
37 }
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);
42 }
43 }
44 result += filename;
45 return result;
46}
47
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()) {
56 return false;
57 }
58
59 // `result_data` takes ownership.
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());
64 return true;
65}
66#endif // V8_USE_EXTERNAL_STARTUP_DATA
67
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());
71
72 std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
73 v8::V8::InitializePlatform(platform.get());
74#ifdef PDF_ENABLE_XFA
75 cppgc::InitializeProcess(platform->GetPageAllocator());
76#endif
77
78 const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags();
79 v8::V8::SetFlagsFromString(recommended_v8_flags);
80
81 if (!js_flags.empty())
82 v8::V8::SetFlagsFromString(js_flags.c_str());
83
84 // By enabling predictable mode, V8 won't post any background tasks.
85 // By enabling GC, it makes it easier to chase use-after-free.
86 static const char kAdditionalV8Flags[] = "--predictable --expose-gc";
87 v8::V8::SetFlagsFromString(kAdditionalV8Flags);
88
89 v8::V8::Initialize();
90 return platform;
91}
92
93} // namespace
94
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);
103 if (snapshot_blob) {
104 if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob))
105 return nullptr;
106 v8::V8::SetSnapshotDataBlob(snapshot_blob);
107 }
108 return platform;
109}
110#else // V8_USE_EXTERNAL_STARTUP_DATA
112 const std::string& exe_path,
113 const std::string& js_flags) {
114 return InitializeV8Common(exe_path, js_flags);
115}
116#endif // V8_USE_EXTERNAL_STARTUP_DATA
117
119#ifdef PDF_ENABLE_XFA
120 cppgc::ShutdownProcess();
121#endif
122 v8::V8::Dispose();
123 v8::V8::DisposePlatform();
124}
std::unique_ptr< v8::Platform > InitializeV8ForPDFium(const std::string &exe_path, const std::string &js_flags)
void ShutdownV8ForPDFium()