Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "embedder_test.h" |
| 6 | |
| 7 | #include <limits.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <list> |
| 13 | #include <string> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "../fpdfsdk/include/fpdf_ext.h" |
| 18 | #include "../fpdfsdk/include/fpdftext.h" |
| 19 | #include "../fpdfsdk/include/fpdfview.h" |
| 20 | #include "../core/include/fxcrt/fx_system.h" |
| 21 | #include "v8/include/v8.h" |
| 22 | |
| 23 | #ifdef _WIN32 |
| 24 | #define snprintf _snprintf |
| 25 | #define PATH_SEPARATOR '\\' |
| 26 | #else |
| 27 | #define PATH_SEPARATOR '/' |
| 28 | #endif |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // Reads the entire contents of a file into a newly malloc'd buffer. |
| 33 | static char* GetFileContents(const char* filename, size_t* retlen) { |
| 34 | FILE* file = fopen(filename, "rb"); |
| 35 | if (!file) { |
| 36 | fprintf(stderr, "Failed to open: %s\n", filename); |
| 37 | return NULL; |
| 38 | } |
| 39 | (void) fseek(file, 0, SEEK_END); |
| 40 | size_t file_length = ftell(file); |
| 41 | if (!file_length) { |
| 42 | return NULL; |
| 43 | } |
| 44 | (void) fseek(file, 0, SEEK_SET); |
| 45 | char* buffer = (char*) malloc(file_length); |
| 46 | if (!buffer) { |
| 47 | return NULL; |
| 48 | } |
| 49 | size_t bytes_read = fread(buffer, 1, file_length, file); |
| 50 | (void) fclose(file); |
| 51 | if (bytes_read != file_length) { |
| 52 | fprintf(stderr, "Failed to read: %s\n", filename); |
| 53 | free(buffer); |
| 54 | return NULL; |
| 55 | } |
| 56 | *retlen = bytes_read; |
| 57 | return buffer; |
| 58 | } |
| 59 | |
| 60 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 61 | // Returns the full path for an external V8 data file based on either |
| 62 | // the currect exectuable path or an explicit override. |
| 63 | static std::string GetFullPathForSnapshotFile(const Options& options, |
| 64 | const std::string& filename) { |
| 65 | std::string result; |
| 66 | if (!options.bin_directory.empty()) { |
| 67 | result = options.bin_directory; |
| 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) { |
| 74 | result = options.exe_path.substr(0, last_separator + 1); |
| 75 | } |
| 76 | } |
| 77 | result += filename; |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | // Reads an extenal V8 data file from the |options|-indicated location, |
| 82 | // returing true on success and false on error. |
| 83 | static bool GetExternalData(const Options& options, |
| 84 | const std::string& bin_filename, |
| 85 | v8::StartupData* result_data) { |
| 86 | std::string full_path = GetFullPathForSnapshotFile(options, bin_filename); |
| 87 | size_t data_length = 0; |
| 88 | char* data_buffer = GetFileContents(full_path.c_str(), &data_length); |
| 89 | if (!data_buffer) { |
| 90 | return false; |
| 91 | } |
| 92 | result_data->data = const_cast<const char*>(data_buffer); |
| 93 | result_data->raw_size = data_length; |
| 94 | return true; |
| 95 | } |
| 96 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 100 | int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) { |
| 101 | printf("Form_Alert called.\n"); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
| 106 | std::string feature = "Unknown"; |
| 107 | switch (type) { |
| 108 | case FPDF_UNSP_DOC_XFAFORM: |
| 109 | feature = "XFA"; |
| 110 | break; |
| 111 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 112 | feature = "Portfolios_Packages"; |
| 113 | break; |
| 114 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 115 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 116 | feature = "Attachment"; |
| 117 | break; |
| 118 | case FPDF_UNSP_DOC_SECURITY: |
| 119 | feature = "Rights_Management"; |
| 120 | break; |
| 121 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 122 | feature = "Shared_Review"; |
| 123 | break; |
| 124 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 125 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 126 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 127 | feature = "Shared_Form"; |
| 128 | break; |
| 129 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 130 | feature = "3D"; |
| 131 | break; |
| 132 | case FPDF_UNSP_ANNOT_MOVIE: |
| 133 | feature = "Movie"; |
| 134 | break; |
| 135 | case FPDF_UNSP_ANNOT_SOUND: |
| 136 | feature = "Sound"; |
| 137 | break; |
| 138 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 139 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 140 | feature = "Screen"; |
| 141 | break; |
| 142 | case FPDF_UNSP_ANNOT_SIG: |
| 143 | feature = "Digital_Signature"; |
| 144 | break; |
| 145 | } |
| 146 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 147 | } |
| 148 | |
| 149 | class TestLoader { |
| 150 | public: |
| 151 | TestLoader(const char* pBuf, size_t len); |
| 152 | |
| 153 | const char* m_pBuf; |
| 154 | size_t m_Len; |
| 155 | }; |
| 156 | |
| 157 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 158 | : m_pBuf(pBuf), m_Len(len) { |
| 159 | } |
| 160 | |
| 161 | int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, |
| 162 | unsigned long size) { |
| 163 | TestLoader* pLoader = (TestLoader*) param; |
| 164 | if (pos + size < pos || pos + size > pLoader->m_Len) return 0; |
| 165 | memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 174 | } |
| 175 | |
| 176 | void EmbedderTest::SetUp() { |
| 177 | v8::V8::InitializeICU(); |
| 178 | |
| 179 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 180 | ASSERT_TRUE(GetExternalData(options, "natives_blob.bin", &natives_)); |
| 181 | ASSERT_TRUE(GetExternalData(options, "snapshot_blob.bin", &snapshot_)); |
| 182 | v8::V8::SetNativesDataBlob(&natives); |
| 183 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 184 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 185 | |
| 186 | FPDF_InitLibrary(); |
| 187 | |
| 188 | UNSUPPORT_INFO unsuppored_info; |
| 189 | memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 190 | unsuppored_info.version = 1; |
| 191 | unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 192 | FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 193 | } |
| 194 | |
| 195 | void EmbedderTest::TearDown() { |
| 196 | FPDF_CloseDocument(document_); |
| 197 | FPDFAvail_Destroy(avail_); |
| 198 | FPDF_DestroyLibrary(); |
| 199 | if (loader_) { |
| 200 | delete loader_; |
| 201 | } |
| 202 | if (file_contents_) { |
| 203 | free(file_contents_); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | bool EmbedderTest::OpenDocument(const std::string& filename) { |
| 208 | file_contents_ = GetFileContents(filename.c_str(), &file_length_); |
| 209 | if (!file_contents_) { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | loader_ = new TestLoader(file_contents_, file_length_); |
| 214 | file_access_.m_FileLen = static_cast<unsigned long>(file_length_); |
| 215 | file_access_.m_GetBlock = Get_Block; |
| 216 | file_access_.m_Param = loader_; |
| 217 | |
| 218 | file_avail_.version = 1; |
| 219 | file_avail_.IsDataAvail = Is_Data_Avail; |
| 220 | |
| 221 | hints_.version = 1; |
| 222 | hints_.AddSegment = Add_Segment; |
| 223 | |
| 224 | avail_ = FPDFAvail_Create(&file_avail_, &file_access_); |
| 225 | (void) FPDFAvail_IsDocAvail(avail_, &hints_); |
| 226 | |
| 227 | if (!FPDFAvail_IsLinearized(avail_)) { |
| 228 | document_ = FPDF_LoadCustomDocument(&file_access_, NULL); |
| 229 | } else { |
| 230 | document_ = FPDFAvail_GetDocument(avail_, NULL); |
| 231 | } |
| 232 | if (!document_) { |
| 233 | return false; |
| 234 | } |
| 235 | (void) FPDF_LoadXFA(document_); |
| 236 | (void) FPDF_GetDocPermissions(document_); |
| 237 | (void) FPDFAvail_IsFormAvail(avail_, &hints_); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | FPDF_FORMHANDLE EmbedderTest::SetFormFillEnvironment() { |
| 242 | IPDF_JSPLATFORM platform_callbacks; |
| 243 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 244 | platform_callbacks.version = 1; |
| 245 | platform_callbacks.app_alert = Form_Alert; |
| 246 | |
| 247 | FPDF_FORMFILLINFO form_callbacks; |
| 248 | memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 249 | form_callbacks.version = 1; |
| 250 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 251 | |
| 252 | FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(document_, |
| 253 | &form_callbacks); |
| 254 | FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 255 | FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 256 | return form; |
| 257 | } |
| 258 | |
| 259 | void EmbedderTest::ClearFormFillEnvironment(FPDF_FORMHANDLE form) { |
| 260 | FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 261 | FPDFDOC_ExitFormFillEnvironment(form); |
| 262 | } |
| 263 | |
| 264 | void EmbedderTest::DoOpenActions(FPDF_FORMHANDLE form) { |
| 265 | FORM_DoDocumentJSAction(form); |
| 266 | FORM_DoDocumentOpenAction(form); |
| 267 | } |
| 268 | |
| 269 | int EmbedderTest::GetFirstPageNum() { |
| 270 | int first_page = FPDFAvail_GetFirstPageNum(document_); |
| 271 | (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_); |
| 272 | return first_page; |
| 273 | } |
| 274 | |
| 275 | int EmbedderTest::GetPageCount() { |
| 276 | int page_count = FPDF_GetPageCount(document_); |
| 277 | for (int i = 0; i < page_count; ++i) { |
| 278 | (void) FPDFAvail_IsPageAvail(avail_, i, &hints_); |
| 279 | } |
| 280 | return page_count; |
| 281 | } |
| 282 | |
| 283 | FPDF_PAGE EmbedderTest::LoadPage(int page_number, |
| 284 | FPDF_FORMHANDLE form) { |
| 285 | FPDF_PAGE page = FPDF_LoadPage(document_, page_number); |
| 286 | if (!page) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | FORM_OnAfterLoadPage(page, form); |
| 290 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 291 | return page; |
| 292 | } |
| 293 | |
| 294 | FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page, |
| 295 | FPDF_FORMHANDLE form) { |
| 296 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 297 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 298 | FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
| 299 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 300 | FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
| 301 | FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 302 | return bitmap; |
| 303 | } |
| 304 | |
| 305 | void EmbedderTest::UnloadPage(FPDF_PAGE page, FPDF_FORMHANDLE form) { |
| 306 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 307 | FORM_OnBeforeClosePage(page, form); |
| 308 | FPDF_ClosePage(page); |
| 309 | } |