Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: samples/pdfium_test.cc

Issue 2330043002: Avoid static initializers and global variables in 'pdfium_test'. (Closed)
Patch Set: addressed tsepez' feedback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium 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 <limits.h> 5 #include <limits.h>
6 #include <stdio.h> 6 #include <stdio.h>
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 OUTPUT_PNG, 47 OUTPUT_PNG,
48 #ifdef _WIN32 48 #ifdef _WIN32
49 OUTPUT_BMP, 49 OUTPUT_BMP,
50 OUTPUT_EMF, 50 OUTPUT_EMF,
51 #endif 51 #endif
52 #ifdef PDF_ENABLE_SKIA 52 #ifdef PDF_ENABLE_SKIA
53 OUTPUT_SKP, 53 OUTPUT_SKP,
54 #endif 54 #endif
55 }; 55 };
56 56
57 // Hold a map of the currently loaded pages in order to avoid them
58 // to get loaded twice.
59 std::map<int, FPDF_PAGE> g_loadedPages;
60
61 // Hold a global pointer of FPDF_FORMHANDLE so that PDFium
62 // app hooks can made use of it.
63 FPDF_FORMHANDLE g_formHandle;
64
65 struct Options { 57 struct Options {
66 Options() 58 Options()
67 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {} 59 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
68 60
69 bool show_config; 61 bool show_config;
70 bool send_events; 62 bool send_events;
71 OutputFormat output_format; 63 OutputFormat output_format;
72 std::string scale_factor_as_string; 64 std::string scale_factor_as_string;
73 std::string exe_path; 65 std::string exe_path;
74 std::string bin_directory; 66 std::string bin_directory;
75 std::string font_directory; 67 std::string font_directory;
76 }; 68 };
77 69
70 struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
71 // Hold a map of the currently loaded pages in order to avoid them
72 // to get loaded twice.
73 std::map<int, FPDF_PAGE> loadedPages;
74
75 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
76 // make use of it.
77 FPDF_FORMHANDLE formHandle;
78 };
79
80 static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
81 FPDF_FORMFILLINFO* formFillInfo) {
82 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(formFillInfo);
83 }
84
78 static bool CheckDimensions(int stride, int width, int height) { 85 static bool CheckDimensions(int stride, int width, int height) {
79 if (stride < 0 || width < 0 || height < 0) 86 if (stride < 0 || width < 0 || height < 0)
80 return false; 87 return false;
81 if (height > 0 && width > INT_MAX / height) 88 if (height > 0 && width > INT_MAX / height)
82 return false; 89 return false;
83 return true; 90 return true;
84 } 91 }
85 92
86 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, 93 static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
87 int stride, int width, int height) { 94 int stride, int width, int height) {
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 } 532 }
526 } else { 533 } else {
527 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); 534 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
528 } 535 }
529 } 536 }
530 } 537 }
531 538
532 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param, 539 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
533 FPDF_DOCUMENT doc, 540 FPDF_DOCUMENT doc,
534 int index) { 541 int index) {
535 auto iter = g_loadedPages.find(index); 542 FPDF_FORMFILLINFO_PDFiumTest* formFillInfo = ToPDFiumTestFormFillInfo(param);
536 if (iter != g_loadedPages.end()) 543 auto& loadedPages = formFillInfo->loadedPages;
544
545 auto iter = loadedPages.find(index);
546 if (iter != loadedPages.end())
537 return iter->second; 547 return iter->second;
538 548
539 FPDF_PAGE page = FPDF_LoadPage(doc, index); 549 FPDF_PAGE page = FPDF_LoadPage(doc, index);
540 if (!page) 550 if (!page)
541 return nullptr; 551 return nullptr;
542 552
543 FORM_OnAfterLoadPage(page, g_formHandle); 553 FPDF_FORMHANDLE& formHandle = formFillInfo->formHandle;
544 FORM_DoPageAAction(page, g_formHandle, FPDFPAGE_AACTION_OPEN);
545 554
546 g_loadedPages[index] = page; 555 FORM_OnAfterLoadPage(page, formHandle);
556 FORM_DoPageAAction(page, formHandle, FPDFPAGE_AACTION_OPEN);
557
558 loadedPages[index] = page;
547 return page; 559 return page;
548 } 560 }
549 561
550 bool RenderPage(const std::string& name, 562 bool RenderPage(const std::string& name,
551 FPDF_DOCUMENT doc, 563 FPDF_DOCUMENT doc,
552 FPDF_FORMHANDLE& form, 564 FPDF_FORMHANDLE& form,
565 FPDF_FORMFILLINFO_PDFiumTest& formFillInfo,
553 const int page_index, 566 const int page_index,
554 const Options& options, 567 const Options& options,
555 const std::string& events) { 568 const std::string& events) {
556 FPDF_PAGE page = GetPageForIndex(nullptr, doc, page_index); 569 FPDF_PAGE page = GetPageForIndex(&formFillInfo, doc, page_index);
557 if (!page) 570 if (!page)
558 return false; 571 return false;
559 572
560 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); 573 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
561 574
562 if (options.send_events) 575 if (options.send_events)
563 SendPageEvents(form, page, events); 576 SendPageEvents(form, page, events);
564 577
565 double scale = 1.0; 578 double scale = 1.0;
566 if (!options.scale_factor_as_string.empty()) 579 if (!options.scale_factor_as_string.empty())
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 #endif 625 #endif
613 default: 626 default:
614 break; 627 break;
615 } 628 }
616 629
617 FPDFBitmap_Destroy(bitmap); 630 FPDFBitmap_Destroy(bitmap);
618 } else { 631 } else {
619 fprintf(stderr, "Page was too large to be rendered.\n"); 632 fprintf(stderr, "Page was too large to be rendered.\n");
620 } 633 }
621 634
622 g_loadedPages.erase(page_index); 635 formFillInfo.loadedPages.erase(page_index);
636
623 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 637 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
624 FORM_OnBeforeClosePage(page, form); 638 FORM_OnBeforeClosePage(page, form);
625 FPDFText_ClosePage(text_page); 639 FPDFText_ClosePage(text_page);
626 FPDF_ClosePage(page); 640 FPDF_ClosePage(page);
627 return !!bitmap; 641 return !!bitmap;
628 } 642 }
629 643
630 void RenderPdf(const std::string& name, 644 void RenderPdf(const std::string& name,
631 const char* pBuf, 645 const char* pBuf,
632 size_t len, 646 size_t len,
633 const Options& options, 647 const Options& options,
634 const std::string& events) { 648 const std::string& events) {
635 IPDF_JSPLATFORM platform_callbacks; 649 IPDF_JSPLATFORM platform_callbacks;
636 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 650 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
637 platform_callbacks.version = 3; 651 platform_callbacks.version = 3;
638 platform_callbacks.app_alert = ExampleAppAlert; 652 platform_callbacks.app_alert = ExampleAppAlert;
639 platform_callbacks.app_response = ExampleAppResponse; 653 platform_callbacks.app_response = ExampleAppResponse;
640 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 654 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
641 platform_callbacks.Doc_mail = ExampleDocMail; 655 platform_callbacks.Doc_mail = ExampleDocMail;
642 656
643 FPDF_FORMFILLINFO form_callbacks; 657 FPDF_FORMFILLINFO_PDFiumTest form_callbacks;
644 memset(&form_callbacks, '\0', sizeof(form_callbacks)); 658 memset(&form_callbacks, '\0', sizeof(platform_callbacks));
dsinclair 2016/09/21 17:25:13 Shouldn't this be form_callbacks?
tonikitoo 2016/09/21 18:21:35 You are right. After FPDF_FORMFILLINFO_PDFiumTest
645 #ifdef PDF_ENABLE_XFA 659 #ifdef PDF_ENABLE_XFA
646 form_callbacks.version = 2; 660 form_callbacks.version = 2;
647 #else // PDF_ENABLE_XFA 661 #else // PDF_ENABLE_XFA
648 form_callbacks.version = 1; 662 form_callbacks.version = 1;
649 #endif // PDF_ENABLE_XFA 663 #endif // PDF_ENABLE_XFA
650 form_callbacks.FFI_GetPage = GetPageForIndex; 664 form_callbacks.FFI_GetPage = GetPageForIndex;
651 form_callbacks.m_pJsPlatform = &platform_callbacks; 665 form_callbacks.m_pJsPlatform = &platform_callbacks;
652 666
653 TestLoader loader(pBuf, len); 667 TestLoader loader(pBuf, len);
654 FPDF_FILEACCESS file_access; 668 FPDF_FILEACCESS file_access;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 fprintf(stderr, "Unknown error %ld", err); 738 fprintf(stderr, "Unknown error %ld", err);
725 } 739 }
726 fprintf(stderr, ".\n"); 740 fprintf(stderr, ".\n");
727 741
728 FPDFAvail_Destroy(pdf_avail); 742 FPDFAvail_Destroy(pdf_avail);
729 return; 743 return;
730 } 744 }
731 745
732 (void)FPDF_GetDocPermissions(doc); 746 (void)FPDF_GetDocPermissions(doc);
733 747
734 FPDF_FORMHANDLE form = g_formHandle = 748 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
735 FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 749 form_callbacks.formHandle = form;
750
736 #ifdef PDF_ENABLE_XFA 751 #ifdef PDF_ENABLE_XFA
737 int docType = DOCTYPE_PDF; 752 int docType = DOCTYPE_PDF;
738 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && 753 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
739 !FPDF_LoadXFA(doc)) { 754 !FPDF_LoadXFA(doc)) {
740 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); 755 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
741 } 756 }
742 #endif // PDF_ENABLE_XFA 757 #endif // PDF_ENABLE_XFA
743 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 758 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
744 FPDF_SetFormFieldHighlightAlpha(form, 100); 759 FPDF_SetFormFieldHighlightAlpha(form, 100);
745 760
746 FORM_DoDocumentJSAction(form); 761 FORM_DoDocumentJSAction(form);
747 FORM_DoDocumentOpenAction(form); 762 FORM_DoDocumentOpenAction(form);
748 763
749 int page_count = FPDF_GetPageCount(doc); 764 int page_count = FPDF_GetPageCount(doc);
750 int rendered_pages = 0; 765 int rendered_pages = 0;
751 int bad_pages = 0; 766 int bad_pages = 0;
752 for (int i = 0; i < page_count; ++i) { 767 for (int i = 0; i < page_count; ++i) {
753 if (bIsLinearized) { 768 if (bIsLinearized) {
754 nRet = PDF_DATA_NOTAVAIL; 769 nRet = PDF_DATA_NOTAVAIL;
755 while (nRet == PDF_DATA_NOTAVAIL) 770 while (nRet == PDF_DATA_NOTAVAIL)
756 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); 771 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
757 772
758 if (nRet == PDF_DATA_ERROR) { 773 if (nRet == PDF_DATA_ERROR) {
759 fprintf(stderr, "Unknown error in checking if page %d is available.\n", 774 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
760 i); 775 i);
761 return; 776 return;
762 } 777 }
763 } 778 }
764 if (RenderPage(name, doc, form, i, options, events)) 779 if (RenderPage(name, doc, form, form_callbacks, i, options, events))
765 ++rendered_pages; 780 ++rendered_pages;
766 else 781 else
767 ++bad_pages; 782 ++bad_pages;
768 } 783 }
769 784
770 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 785 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
771 786
772 #ifdef PDF_ENABLE_XFA 787 #ifdef PDF_ENABLE_XFA
773 // Note: The shut down order here is the reverse of the non-XFA branch order. 788 // Note: The shut down order here is the reverse of the non-XFA branch order.
774 // Need to work out if this is required, and if it is, the lifetimes of 789 // Need to work out if this is required, and if it is, the lifetimes of
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 delete platform; 925 delete platform;
911 926
912 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 927 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
913 free(const_cast<char*>(natives.data)); 928 free(const_cast<char*>(natives.data));
914 free(const_cast<char*>(snapshot.data)); 929 free(const_cast<char*>(snapshot.data));
915 #endif // V8_USE_EXTERNAL_STARTUP_DATA 930 #endif // V8_USE_EXTERNAL_STARTUP_DATA
916 #endif // PDF_ENABLE_V8 931 #endif // PDF_ENABLE_V8
917 932
918 return 0; 933 return 0;
919 } 934 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698