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

Unified Diff: samples/pdfium_test.cc

Issue 2330043002: Avoid static initializers and global variables in 'pdfium_test'. (Closed)
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/pdfium_test.cc
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index b78075e788c020992a3e231923811f59b5713ffb..9ef001da56ed838cee702c6e640c0d7c46441962 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -54,14 +54,6 @@ enum OutputFormat {
#endif
};
-// Hold a map of the currently loaded pages in order to avoid them
-// to get loaded twice.
-std::map<int, FPDF_PAGE> g_loadedPages;
-
-// Hold a global pointer of FPDF_FORMHANDLE so that PDFium
-// app hooks can made use of it.
-FPDF_FORMHANDLE g_formHandle;
-
struct Options {
Options()
: show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
@@ -75,6 +67,28 @@ struct Options {
std::string font_directory;
};
+typedef struct {
Tom Sepez 2016/09/12 17:19:41 nit: In C++, we can avoid "typedef" adn just write
tonikitoo 2016/09/12 23:41:55 Done.
+ FPDF_FORMFILLINFO formInfo;
+
+ // Hold a map of the currently loaded pages in order to avoid them
+ // to get loaded twice.
+ std::map<int, FPDF_PAGE> loadedPages;
+
+ // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
+ // make use of it.
+ FPDF_FORMHANDLE formHandle;
+} PDFiumTest_FPDF_FORMFILLINFO;
+
+static PDFiumTest_FPDF_FORMFILLINFO* ToPDFiumTestFormFillInfo(
Tom Sepez 2016/09/12 17:19:40 It might be cleaner if PDFiumTest_FPDF_FORMFILLINF
tonikitoo 2016/09/12 23:41:55 Done.
+ FPDF_FORMFILLINFO* formFillInfo) {
+ return reinterpret_cast<PDFiumTest_FPDF_FORMFILLINFO*>(formFillInfo);
+}
+
+static FPDF_FORMFILLINFO* ToFormFillInfo(
+ PDFiumTest_FPDF_FORMFILLINFO* pdfiumTestFormFillInfo) {
+ return reinterpret_cast<FPDF_FORMFILLINFO*>(pdfiumTestFormFillInfo);
+}
+
static bool CheckDimensions(int stride, int width, int height) {
if (stride < 0 || width < 0 || height < 0)
return false;
@@ -532,28 +546,34 @@ void SendPageEvents(const FPDF_FORMHANDLE& form,
FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
FPDF_DOCUMENT doc,
int index) {
- auto iter = g_loadedPages.find(index);
- if (iter != g_loadedPages.end())
+ PDFiumTest_FPDF_FORMFILLINFO* formFillInfo = ToPDFiumTestFormFillInfo(param);
+ std::map<int, FPDF_PAGE>& loadedPages = formFillInfo->loadedPages;
+
+ auto iter = loadedPages.find(index);
+ if (iter != loadedPages.end())
return iter->second;
FPDF_PAGE page = FPDF_LoadPage(doc, index);
if (!page)
return nullptr;
- FORM_OnAfterLoadPage(page, g_formHandle);
- FORM_DoPageAAction(page, g_formHandle, FPDFPAGE_AACTION_OPEN);
+ FPDF_FORMHANDLE& formHandle = formFillInfo->formHandle;
- g_loadedPages[index] = page;
+ FORM_OnAfterLoadPage(page, formHandle);
+ FORM_DoPageAAction(page, formHandle, FPDFPAGE_AACTION_OPEN);
+
+ loadedPages[index] = page;
return page;
}
bool RenderPage(const std::string& name,
FPDF_DOCUMENT doc,
FPDF_FORMHANDLE& form,
+ FPDF_FORMFILLINFO& formFillInfo,
const int page_index,
const Options& options,
const std::string& events) {
- FPDF_PAGE page = GetPageForIndex(nullptr, doc, page_index);
+ FPDF_PAGE page = GetPageForIndex(&formFillInfo, doc, page_index);
if (!page)
return false;
@@ -619,7 +639,10 @@ bool RenderPage(const std::string& name,
fprintf(stderr, "Page was too large to be rendered.\n");
}
- g_loadedPages.erase(page_index);
+ PDFiumTest_FPDF_FORMFILLINFO* pdfiumTestFormFillInfo =
+ ToPDFiumTestFormFillInfo(&formFillInfo);
+ pdfiumTestFormFillInfo->loadedPages.erase(page_index);
+
FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
FORM_OnBeforeClosePage(page, form);
FPDFText_ClosePage(text_page);
@@ -731,8 +754,16 @@ void RenderPdf(const std::string& name,
(void)FPDF_GetDocPermissions(doc);
- FPDF_FORMHANDLE form = g_formHandle =
- FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
+ std::unique_ptr<PDFiumTest_FPDF_FORMFILLINFO> pdfiumTestFormFillInfo(
+ new PDFiumTest_FPDF_FORMFILLINFO);
+ pdfiumTestFormFillInfo->formInfo = form_callbacks;
+
+ FPDF_FORMFILLINFO* formFillInfo =
+ ToFormFillInfo(pdfiumTestFormFillInfo.get());
+
+ FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, formFillInfo);
+ pdfiumTestFormFillInfo->formHandle = form;
+
#ifdef PDF_ENABLE_XFA
int docType = DOCTYPE_PDF;
if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
@@ -761,7 +792,7 @@ void RenderPdf(const std::string& name,
return;
}
}
- if (RenderPage(name, doc, form, i, options, events))
+ if (RenderPage(name, doc, form, *formFillInfo, i, options, events))
++rendered_pages;
else
++bad_pages;
« 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