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

Side by Side Diff: samples/pdfium_test.cc

Issue 2375263002: Fix leaks in pdfium_test. (Closed)
Patch Set: simplify skia Created 4 years, 2 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO { 70 struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
71 // Hold a map of the currently loaded pages in order to avoid them 71 // Hold a map of the currently loaded pages in order to avoid them
72 // to get loaded twice. 72 // to get loaded twice.
73 std::map<int, FPDF_PAGE> loadedPages; 73 std::map<int, FPDF_PAGE> loadedPages;
74 74
75 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can 75 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
76 // make use of it. 76 // make use of it.
77 FPDF_FORMHANDLE formHandle; 77 FPDF_FORMHANDLE formHandle;
78 }; 78 };
79 79
80 struct AvailDeleter {
81 inline void operator()(FPDF_AVAIL avail) const { FPDFAvail_Destroy(avail); }
82 };
83
80 static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo( 84 static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
81 FPDF_FORMFILLINFO* formFillInfo) { 85 FPDF_FORMFILLINFO* formFillInfo) {
82 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(formFillInfo); 86 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(formFillInfo);
83 } 87 }
84 88
89 static void CloseDocAndForm(FPDF_DOCUMENT doc, FPDF_FORMHANDLE form) {
90 #ifdef PDF_ENABLE_XFA
91 // Note: The shut down order here is the reverse of the non-XFA branch order.
92 // Need to work out if this is required, and if it is, the lifetimes of
93 // objects owned by |doc| that |form| reference.
94 FPDF_CloseDocument(doc);
95 FPDFDOC_ExitFormFillEnvironment(form);
96 #else // PDF_ENABLE_XFA
97 FPDFDOC_ExitFormFillEnvironment(form);
98 FPDF_CloseDocument(doc);
99 #endif // PDF_ENABLE_XFA
100 }
101
85 static bool CheckDimensions(int stride, int width, int height) { 102 static bool CheckDimensions(int stride, int width, int height) {
86 if (stride < 0 || width < 0 || height < 0) 103 if (stride < 0 || width < 0 || height < 0)
87 return false; 104 return false;
88 if (height > 0 && width > INT_MAX / height) 105 if (height > 0 && width > INT_MAX / height)
89 return false; 106 return false;
90 return true; 107 return true;
91 } 108 }
92 109
93 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, 110 static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
94 int stride, int width, int height) { 111 int stride, int width, int height) {
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 Rectangle(dc, 0, 0, width + 1, height + 1); 266 Rectangle(dc, 0, 0, width + 1, height + 1);
250 267
251 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, 268 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
252 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 269 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
253 270
254 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); 271 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
255 } 272 }
256 #endif 273 #endif
257 274
258 #ifdef PDF_ENABLE_SKIA 275 #ifdef PDF_ENABLE_SKIA
259 void WriteSkp(const char* pdf_name, int num, const void* recorder) { 276 void WriteSkp(const char* pdf_name, int num, SkPictureRecorder* recorder) {
260 char filename[256]; 277 char filename[256];
261 int chars_formatted = 278 int chars_formatted =
262 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num); 279 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
263 280
264 if (chars_formatted < 0 || 281 if (chars_formatted < 0 ||
265 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { 282 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
266 fprintf(stderr, "Filename %s is too long\n", filename); 283 fprintf(stderr, "Filename %s is too long\n", filename);
267 return; 284 return;
268 } 285 }
269 286
270 SkPictureRecorder* r = (SkPictureRecorder*)recorder; 287 sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture());
271 sk_sp<SkPicture> picture(r->finishRecordingAsPicture());
272 SkFILEWStream wStream(filename); 288 SkFILEWStream wStream(filename);
273 picture->serialize(&wStream); 289 picture->serialize(&wStream);
274 } 290 }
275 #endif 291 #endif
276 292
277 // These example JS platform callback handlers are entirely optional, 293 // These example JS platform callback handlers are entirely optional,
278 // and exist here to show the flow of information from a document back 294 // and exist here to show the flow of information from a document back
279 // to the embedder. 295 // to the embedder.
280 int ExampleAppAlert(IPDF_JSPLATFORM*, 296 int ExampleAppAlert(IPDF_JSPLATFORM*,
281 FPDF_WIDESTRING msg, 297 FPDF_WIDESTRING msg,
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 WritePng(name.c_str(), page_index, buffer, stride, width, height); 627 WritePng(name.c_str(), page_index, buffer, stride, width, height);
612 break; 628 break;
613 629
614 case OUTPUT_PPM: 630 case OUTPUT_PPM:
615 WritePpm(name.c_str(), page_index, buffer, stride, width, height); 631 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
616 break; 632 break;
617 633
618 #ifdef PDF_ENABLE_SKIA 634 #ifdef PDF_ENABLE_SKIA
619 case OUTPUT_SKP: { 635 case OUTPUT_SKP: {
620 std::unique_ptr<SkPictureRecorder> recorder( 636 std::unique_ptr<SkPictureRecorder> recorder(
621 (SkPictureRecorder*)FPDF_RenderPageSkp(page, width, height)); 637 reinterpret_cast<SkPictureRecorder*>(
638 FPDF_RenderPageSkp(page, width, height)));
622 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0); 639 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
623 WriteSkp(name.c_str(), page_index, recorder.get()); 640 WriteSkp(name.c_str(), page_index, recorder.get());
624 } break; 641 } break;
625 #endif 642 #endif
626 default: 643 default:
627 break; 644 break;
628 } 645 }
629 646
630 FPDFBitmap_Destroy(bitmap); 647 FPDFBitmap_Destroy(bitmap);
631 } else { 648 } else {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 694
678 FX_DOWNLOADHINTS hints; 695 FX_DOWNLOADHINTS hints;
679 memset(&hints, '\0', sizeof(hints)); 696 memset(&hints, '\0', sizeof(hints));
680 hints.version = 1; 697 hints.version = 1;
681 hints.AddSegment = Add_Segment; 698 hints.AddSegment = Add_Segment;
682 699
683 FPDF_DOCUMENT doc; 700 FPDF_DOCUMENT doc;
684 int nRet = PDF_DATA_NOTAVAIL; 701 int nRet = PDF_DATA_NOTAVAIL;
685 bool bIsLinearized = false; 702 bool bIsLinearized = false;
686 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); 703 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
704 std::unique_ptr<void, AvailDeleter> scoped_pdf_avail_deleter(pdf_avail);
687 705
688 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) { 706 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
689 doc = FPDFAvail_GetDocument(pdf_avail, nullptr); 707 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
690 if (doc) { 708 if (doc) {
691 while (nRet == PDF_DATA_NOTAVAIL) 709 while (nRet == PDF_DATA_NOTAVAIL)
692 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints); 710 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
693 711
694 if (nRet == PDF_DATA_ERROR) { 712 if (nRet == PDF_DATA_ERROR) {
695 fprintf(stderr, "Unknown error in checking if doc was available.\n"); 713 fprintf(stderr, "Unknown error in checking if doc was available.\n");
714 FPDF_CloseDocument(doc);
696 return; 715 return;
697 } 716 }
698 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints); 717 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
699 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { 718 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
700 fprintf(stderr, 719 fprintf(stderr,
701 "Error %d was returned in checking if form was available.\n", 720 "Error %d was returned in checking if form was available.\n",
702 nRet); 721 nRet);
722 FPDF_CloseDocument(doc);
703 return; 723 return;
704 } 724 }
705 bIsLinearized = true; 725 bIsLinearized = true;
706 } 726 }
707 } else { 727 } else {
708 doc = FPDF_LoadCustomDocument(&file_access, nullptr); 728 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
709 } 729 }
710 730
711 if (!doc) { 731 if (!doc) {
712 unsigned long err = FPDF_GetLastError(); 732 unsigned long err = FPDF_GetLastError();
(...skipping 18 matching lines...) Expand all
731 fprintf(stderr, "Unsupported security scheme"); 751 fprintf(stderr, "Unsupported security scheme");
732 break; 752 break;
733 case FPDF_ERR_PAGE: 753 case FPDF_ERR_PAGE:
734 fprintf(stderr, "Page not found or content error"); 754 fprintf(stderr, "Page not found or content error");
735 break; 755 break;
736 default: 756 default:
737 fprintf(stderr, "Unknown error %ld", err); 757 fprintf(stderr, "Unknown error %ld", err);
738 } 758 }
739 fprintf(stderr, ".\n"); 759 fprintf(stderr, ".\n");
740 760
741 FPDFAvail_Destroy(pdf_avail);
742 return; 761 return;
743 } 762 }
744 763
745 (void)FPDF_GetDocPermissions(doc); 764 (void)FPDF_GetDocPermissions(doc);
746 765
747 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 766 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
748 form_callbacks.formHandle = form; 767 form_callbacks.formHandle = form;
749 768
750 #ifdef PDF_ENABLE_XFA 769 #ifdef PDF_ENABLE_XFA
751 int docType = DOCTYPE_PDF; 770 int doc_type = DOCTYPE_PDF;
752 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && 771 if (FPDF_HasXFAField(doc, &doc_type) && doc_type != DOCTYPE_PDF &&
753 !FPDF_LoadXFA(doc)) { 772 !FPDF_LoadXFA(doc)) {
754 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); 773 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
755 } 774 }
756 #endif // PDF_ENABLE_XFA 775 #endif // PDF_ENABLE_XFA
757 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 776 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
758 FPDF_SetFormFieldHighlightAlpha(form, 100); 777 FPDF_SetFormFieldHighlightAlpha(form, 100);
759 778
760 FORM_DoDocumentJSAction(form); 779 FORM_DoDocumentJSAction(form);
761 FORM_DoDocumentOpenAction(form); 780 FORM_DoDocumentOpenAction(form);
762 781
763 int page_count = FPDF_GetPageCount(doc); 782 int page_count = FPDF_GetPageCount(doc);
764 int rendered_pages = 0; 783 int rendered_pages = 0;
765 int bad_pages = 0; 784 int bad_pages = 0;
766 for (int i = 0; i < page_count; ++i) { 785 for (int i = 0; i < page_count; ++i) {
767 if (bIsLinearized) { 786 if (bIsLinearized) {
768 nRet = PDF_DATA_NOTAVAIL; 787 nRet = PDF_DATA_NOTAVAIL;
769 while (nRet == PDF_DATA_NOTAVAIL) 788 while (nRet == PDF_DATA_NOTAVAIL)
770 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); 789 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
771 790
772 if (nRet == PDF_DATA_ERROR) { 791 if (nRet == PDF_DATA_ERROR) {
773 fprintf(stderr, "Unknown error in checking if page %d is available.\n", 792 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
774 i); 793 i);
794 CloseDocAndForm(doc, form);
775 return; 795 return;
776 } 796 }
777 } 797 }
778 if (RenderPage(name, doc, form, form_callbacks, i, options, events)) 798 if (RenderPage(name, doc, form, form_callbacks, i, options, events))
779 ++rendered_pages; 799 ++rendered_pages;
780 else 800 else
781 ++bad_pages; 801 ++bad_pages;
782 } 802 }
783 803
784 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 804 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
785 805
786 #ifdef PDF_ENABLE_XFA 806 CloseDocAndForm(doc, form);
787 // Note: The shut down order here is the reverse of the non-XFA branch order.
788 // Need to work out if this is required, and if it is, the lifetimes of
789 // objects owned by |doc| that |form| reference.
790 FPDF_CloseDocument(doc);
791 FPDFDOC_ExitFormFillEnvironment(form);
792 #else // PDF_ENABLE_XFA
793 FPDFDOC_ExitFormFillEnvironment(form);
794 FPDF_CloseDocument(doc);
795 #endif // PDF_ENABLE_XFA
796
797 FPDFAvail_Destroy(pdf_avail);
798 807
799 fprintf(stderr, "Rendered %d pages.\n", rendered_pages); 808 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
800 if (bad_pages) 809 if (bad_pages)
801 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); 810 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
802 } 811 }
803 812
804 static void ShowConfig() { 813 static void ShowConfig() {
805 std::string config; 814 std::string config;
806 std::string maybe_comma; 815 std::string maybe_comma;
807 #if PDF_ENABLE_V8 816 #if PDF_ENABLE_V8
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 delete platform; 933 delete platform;
925 934
926 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 935 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
927 free(const_cast<char*>(natives.data)); 936 free(const_cast<char*>(natives.data));
928 free(const_cast<char*>(snapshot.data)); 937 free(const_cast<char*>(snapshot.data));
929 #endif // V8_USE_EXTERNAL_STARTUP_DATA 938 #endif // V8_USE_EXTERNAL_STARTUP_DATA
930 #endif // PDF_ENABLE_V8 939 #endif // PDF_ENABLE_V8
931 940
932 return 0; 941 return 0;
933 } 942 }
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