OLD | NEW |
1 // Copyright (c) 2015 PDFium Authors. All rights reserved. | 1 // Copyright (c) 2015 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "embedder_test.h" | 5 #include "embedder_test.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 #ifdef _WIN32 | 23 #ifdef _WIN32 |
24 #define snprintf _snprintf | 24 #define snprintf _snprintf |
25 #define PATH_SEPARATOR '\\' | 25 #define PATH_SEPARATOR '\\' |
26 #else | 26 #else |
27 #define PATH_SEPARATOR '/' | 27 #define PATH_SEPARATOR '/' |
28 #endif | 28 #endif |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
| 32 const char* g_exe_path_ = nullptr; |
| 33 |
32 // Reads the entire contents of a file into a newly malloc'd buffer. | 34 // Reads the entire contents of a file into a newly malloc'd buffer. |
33 static char* GetFileContents(const char* filename, size_t* retlen) { | 35 static char* GetFileContents(const char* filename, size_t* retlen) { |
34 FILE* file = fopen(filename, "rb"); | 36 FILE* file = fopen(filename, "rb"); |
35 if (!file) { | 37 if (!file) { |
36 fprintf(stderr, "Failed to open: %s\n", filename); | 38 fprintf(stderr, "Failed to open: %s\n", filename); |
37 return NULL; | 39 return NULL; |
38 } | 40 } |
39 (void) fseek(file, 0, SEEK_END); | 41 (void) fseek(file, 0, SEEK_END); |
40 size_t file_length = ftell(file); | 42 size_t file_length = ftell(file); |
41 if (!file_length) { | 43 if (!file_length) { |
(...skipping 11 matching lines...) Expand all Loading... |
53 free(buffer); | 55 free(buffer); |
54 return NULL; | 56 return NULL; |
55 } | 57 } |
56 *retlen = bytes_read; | 58 *retlen = bytes_read; |
57 return buffer; | 59 return buffer; |
58 } | 60 } |
59 | 61 |
60 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 62 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
61 // Returns the full path for an external V8 data file based on either | 63 // Returns the full path for an external V8 data file based on either |
62 // the currect exectuable path or an explicit override. | 64 // the currect exectuable path or an explicit override. |
63 static std::string GetFullPathForSnapshotFile(const Options& options, | 65 static std::string GetFullPathForSnapshotFile(const std::string& exe_path, |
64 const std::string& filename) { | 66 const std::string& filename) { |
65 std::string result; | 67 std::string result; |
66 if (!options.bin_directory.empty()) { | 68 if (!exe_path.empty()) { |
67 result = options.bin_directory; | 69 size_t last_separator = exe_path.rfind(PATH_SEPARATOR); |
68 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { | |
69 result += PATH_SEPARATOR; | |
70 } | |
71 } else if (!options.exe_path.empty()) { | |
72 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR); | |
73 if (last_separator != std::string::npos) { | 70 if (last_separator != std::string::npos) { |
74 result = options.exe_path.substr(0, last_separator + 1); | 71 result = exe_path.substr(0, last_separator + 1); |
75 } | 72 } |
76 } | 73 } |
77 result += filename; | 74 result += filename; |
78 return result; | 75 return result; |
79 } | 76 } |
80 | 77 |
81 // Reads an extenal V8 data file from the |options|-indicated location, | 78 // Reads an extenal V8 data file from the |options|-indicated location, |
82 // returing true on success and false on error. | 79 // returing true on success and false on error. |
83 static bool GetExternalData(const Options& options, | 80 static bool GetExternalData(const std::string& exe_path, |
84 const std::string& bin_filename, | 81 const std::string& filename, |
85 v8::StartupData* result_data) { | 82 v8::StartupData* result_data) { |
86 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename); | 83 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename); |
87 size_t data_length = 0; | 84 size_t data_length = 0; |
88 char* data_buffer = GetFileContents(full_path.c_str(), &data_length); | 85 char* data_buffer = GetFileContents(full_path.c_str(), &data_length); |
89 if (!data_buffer) { | 86 if (!data_buffer) { |
90 return false; | 87 return false; |
91 } | 88 } |
92 result_data->data = const_cast<const char*>(data_buffer); | 89 result_data->data = const_cast<const char*>(data_buffer); |
93 result_data->raw_size = data_length; | 90 result_data->raw_size = data_length; |
94 return true; | 91 return true; |
95 } | 92 } |
96 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 93 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 return true; | 167 return true; |
171 } | 168 } |
172 | 169 |
173 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 170 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
174 } | 171 } |
175 | 172 |
176 void EmbedderTest::SetUp() { | 173 void EmbedderTest::SetUp() { |
177 v8::V8::InitializeICU(); | 174 v8::V8::InitializeICU(); |
178 | 175 |
179 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 176 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
180 ASSERT_TRUE(GetExternalData(options, "natives_blob.bin", &natives_)); | 177 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_)); |
181 ASSERT_TRUE(GetExternalData(options, "snapshot_blob.bin", &snapshot_)); | 178 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_)); |
182 v8::V8::SetNativesDataBlob(&natives); | 179 v8::V8::SetNativesDataBlob(&natives_); |
183 v8::V8::SetSnapshotDataBlob(&snapshot); | 180 v8::V8::SetSnapshotDataBlob(&snapshot_); |
184 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 181 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
185 | 182 |
186 FPDF_InitLibrary(); | 183 FPDF_InitLibrary(); |
187 | 184 |
188 UNSUPPORT_INFO unsuppored_info; | 185 UNSUPPORT_INFO unsuppored_info; |
189 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); | 186 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
190 unsuppored_info.version = 1; | 187 unsuppored_info.version = 1; |
191 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; | 188 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
192 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 189 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
193 } | 190 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); | 294 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
298 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); | 295 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
299 return bitmap; | 296 return bitmap; |
300 } | 297 } |
301 | 298 |
302 void EmbedderTest::UnloadPage(FPDF_PAGE page, FPDF_FORMHANDLE form) { | 299 void EmbedderTest::UnloadPage(FPDF_PAGE page, FPDF_FORMHANDLE form) { |
303 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 300 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
304 FORM_OnBeforeClosePage(page, form); | 301 FORM_OnBeforeClosePage(page, form); |
305 FPDF_ClosePage(page); | 302 FPDF_ClosePage(page); |
306 } | 303 } |
| 304 |
| 305 // Can't use gtest-provided main since we need to stash the path to the |
| 306 // executable in order to find the external V8 binary data files. |
| 307 int main(int argc, char** argv) { |
| 308 g_exe_path_ = argv[0]; |
| 309 testing::InitGoogleTest(&argc, argv); |
| 310 return RUN_ALL_TESTS(); |
| 311 } |
OLD | NEW |