From 690c033e86b34f7a9c3d4be15759c63ef9360217 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Tue, 11 Oct 2016 09:13:01 -0700 Subject: Convert CPDFSDK_InterForm to take a CPDFSDK_FormFillEnvironment This CL removes CPDFSDK_Document from CPDFSDK_InterForm and has it work with a CPDFSDK_FormFillEnvironment. Review-Url: https://codereview.chromium.org/2397723005 --- fpdfsdk/cpdfsdk_document.cpp | 2 +- fpdfsdk/cpdfsdk_interform.cpp | 79 ++++++++++++++++++++----------------------- fpdfsdk/cpdfsdk_interform.h | 8 ++--- fpdfsdk/cpdfsdk_widget.cpp | 23 ++++--------- fpdfsdk/javascript/Field.cpp | 13 ++++--- 5 files changed, 57 insertions(+), 68 deletions(-) diff --git a/fpdfsdk/cpdfsdk_document.cpp b/fpdfsdk/cpdfsdk_document.cpp index 677dd835f8..78bcf49185 100644 --- a/fpdfsdk/cpdfsdk_document.cpp +++ b/fpdfsdk/cpdfsdk_document.cpp @@ -162,7 +162,7 @@ UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) { CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() { if (!m_pInterForm) - m_pInterForm = pdfium::MakeUnique(this); + m_pInterForm = pdfium::MakeUnique(GetEnv()); return m_pInterForm.get(); } diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp index 1ed2a6cee5..2cbdae96d4 100644 --- a/fpdfsdk/cpdfsdk_interform.cpp +++ b/fpdfsdk/cpdfsdk_interform.cpp @@ -46,9 +46,10 @@ #include "xfa/fxfa/xfa_ffwidgethandler.h" #endif // PDF_ENABLE_XFA -CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument) - : m_pDocument(pDocument), - m_pInterForm(new CPDF_InterForm(m_pDocument->GetPDFDocument())), +CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv) + : m_pFormFillEnv(pFormFillEnv), + m_pInterForm(new CPDF_InterForm( + m_pFormFillEnv->GetSDKDocument()->GetPDFDocument())), #ifdef PDF_ENABLE_XFA m_bXfaCalculate(TRUE), m_bXfaValidationsEnabled(TRUE), @@ -98,19 +99,19 @@ CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl, return nullptr; CPDF_Dictionary* pControlDict = pControl->GetWidget(); - CPDF_Document* pDocument = m_pDocument->GetPDFDocument(); + CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument(); CPDFSDK_PageView* pPage = nullptr; if (CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) { int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum()); if (nPageIndex >= 0) - pPage = m_pDocument->GetPageView(nPageIndex); + pPage = m_pFormFillEnv->GetSDKDocument()->GetPageView(nPageIndex); } if (!pPage) { int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict); if (nPageIndex >= 0) - pPage = m_pDocument->GetPageView(nPageIndex); + pPage = m_pFormFillEnv->GetSDKDocument()->GetPageView(nPageIndex); } if (!pPage) @@ -221,9 +222,7 @@ void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField, #endif // PDF_ENABLE_XFA void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); - ASSERT(pEnv); - if (!pEnv->IsJSInitiated()) + if (!m_pFormFillEnv->IsJSInitiated()) return; if (m_bBusy) @@ -236,8 +235,8 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { return; } - IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime(); - pRuntime->SetReaderDocument(m_pDocument); + IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime(); + pRuntime->SetReaderDocument(m_pFormFillEnv->GetSDKDocument()); int nSize = m_pInterForm->CountFieldsInCalculationOrder(); for (int i = 0; i < nSize; i++) { @@ -281,15 +280,13 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, FX_BOOL& bFormatted) { CFX_WideString sValue = pFormField->GetValue(); - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); - ASSERT(pEnv); - if (!pEnv->IsJSInitiated()) { + if (!m_pFormFillEnv->IsJSInitiated()) { bFormatted = FALSE; return sValue; } - IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime(); - pRuntime->SetReaderDocument(m_pDocument); + IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime(); + pRuntime->SetReaderDocument(m_pFormFillEnv->GetSDKDocument()); if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX && pFormField->CountSelectedItems() > 0) { @@ -342,14 +339,14 @@ void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) { ASSERT(pFormCtrl); if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl, false)) { - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); UnderlyingPageType* pPage = pWidget->GetUnderlyingPage(); - CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false); - FX_RECT rcBBox = - pEnv->GetInteractiveFormFiller()->GetViewBBox(pPageView, pWidget); + CPDFSDK_PageView* pPageView = + m_pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false); + FX_RECT rcBBox = m_pFormFillEnv->GetInteractiveFormFiller()->GetViewBBox( + pPageView, pWidget); - pEnv->Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right, - rcBBox.bottom); + m_pFormFillEnv->Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right, + rcBBox.bottom); } } } @@ -364,14 +361,14 @@ FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField, if (!action.GetDict()) return TRUE; - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); - CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander(); + CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHander(); PDFSDK_FieldAction fa; - fa.bModifier = pEnv->IsCTRLKeyDown(0); - fa.bShift = pEnv->IsSHIFTKeyDown(0); + fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(0); + fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(0); fa.sValue = csValue; pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke, - m_pDocument, pFormField, fa); + m_pFormFillEnv->GetSDKDocument(), + pFormField, fa); return fa.bRC; } @@ -385,14 +382,14 @@ FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField, if (!action.GetDict()) return TRUE; - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); - CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander(); + CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHander(); PDFSDK_FieldAction fa; - fa.bModifier = pEnv->IsCTRLKeyDown(0); - fa.bShift = pEnv->IsSHIFTKeyDown(0); + fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(0); + fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(0); fa.sValue = csValue; pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate, - m_pDocument, pFormField, fa); + m_pFormFillEnv->GetSDKDocument(), + pFormField, fa); return fa.bRC; } @@ -459,8 +456,6 @@ FX_BOOL CPDFSDK_InterForm::SubmitFields( const std::vector& fields, bool bIncludeOrExclude, bool bUrlEncoded) { - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); - CFX_ByteTextBuf textBuf; ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf); @@ -470,7 +465,7 @@ FX_BOOL CPDFSDK_InterForm::SubmitFields( if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) return FALSE; - pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str()); + m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str()); return TRUE; } @@ -525,7 +520,8 @@ FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf( bool bIncludeOrExclude, CFX_ByteTextBuf& textBuf) { std::unique_ptr pFDF(m_pInterForm->ExportToFDF( - m_pDocument->GetPath().AsStringC(), fields, bIncludeOrExclude, false)); + m_pFormFillEnv->GetSDKDocument()->GetPath().AsStringC(), fields, + bIncludeOrExclude, false)); return pFDF ? pFDF->WriteBuf(textBuf) : FALSE; } @@ -539,11 +535,10 @@ FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, if (sDestination.IsEmpty()) return FALSE; - if (!m_pDocument || !m_pInterForm) + if (!m_pFormFillEnv || !m_pFormFillEnv->GetSDKDocument() || !m_pInterForm) return FALSE; - CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv(); - CFX_WideString wsPDFFilePath = m_pDocument->GetPath(); + CFX_WideString wsPDFFilePath = m_pFormFillEnv->GetSDKDocument()->GetPath(); CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC(), false); if (!pFDFDoc) @@ -561,7 +556,7 @@ FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) return FALSE; - pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str()); + m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str()); if (bUrlEncoded) FX_Free(pBuffer); @@ -570,8 +565,8 @@ FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, } FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) { - CFDF_Document* pFDF = - m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC(), false); + CFDF_Document* pFDF = m_pInterForm->ExportToFDF( + m_pFormFillEnv->GetSDKDocument()->GetPath().AsStringC(), false); if (!pFDF) return FALSE; diff --git a/fpdfsdk/cpdfsdk_interform.h b/fpdfsdk/cpdfsdk_interform.h index 617a1066d7..f6848f3def 100644 --- a/fpdfsdk/cpdfsdk_interform.h +++ b/fpdfsdk/cpdfsdk_interform.h @@ -20,7 +20,7 @@ class CPDF_FormControl; class CPDF_FormField; class CPDF_InterForm; class CPDF_Object; -class CPDFSDK_Document; +class CPDFSDK_FormFillEnvironment; class CPDFSDK_Widget; #ifdef PDF_ENABLE_XFA @@ -30,11 +30,11 @@ class CXFA_FFWidget; class CPDFSDK_InterForm : public IPDF_FormNotify { public: - explicit CPDFSDK_InterForm(CPDFSDK_Document* pDocument); + explicit CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv); ~CPDFSDK_InterForm() override; CPDF_InterForm* GetInterForm() const { return m_pInterForm.get(); } - CPDFSDK_Document* GetDocument() const { return m_pDocument; } + CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { return m_pFormFillEnv; } FX_BOOL HighlightWidgets(); @@ -122,7 +122,7 @@ class CPDFSDK_InterForm : public IPDF_FormNotify { using CPDFSDK_WidgetMap = std::map; - CPDFSDK_Document* m_pDocument; + CPDFSDK_FormFillEnvironment* m_pFormFillEnv; // Not owned. std::unique_ptr m_pInterForm; CPDFSDK_WidgetMap m_Map; #ifdef PDF_ENABLE_XFA diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp index 94555b70c5..0624b0754e 100644 --- a/fpdfsdk/cpdfsdk_widget.cpp +++ b/fpdfsdk/cpdfsdk_widget.cpp @@ -852,10 +852,10 @@ void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice, uint8_t alpha = m_pInterForm->GetHighlightAlpha(); CFX_FloatRect rcDevice; - ASSERT(m_pInterForm->GetDocument()); - CPDFSDK_FormFillEnvironment* pEnv = m_pInterForm->GetDocument()->GetEnv(); + CPDFSDK_FormFillEnvironment* pEnv = m_pInterForm->GetFormFillEnv(); if (!pEnv) return; + CFX_Matrix page2device; pPageView->GetCurrentMatrix(page2device); page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom), @@ -999,10 +999,7 @@ void CPDFSDK_Widget::ResetAppearance_PushButton() { CPDF_IconFit iconFit = pControl->GetIconFit(); - CPDFSDK_Document* pDoc = m_pInterForm->GetDocument(); - CPDFSDK_FormFillEnvironment* pEnv = pDoc->GetEnv(); - - CBA_FontMap font_map(this, pEnv->GetSysHandler()); + CBA_FontMap font_map(this, m_pInterForm->GetFormFillEnv()->GetSysHandler()); font_map.SetAPType("N"); CFX_ByteString csAP = @@ -1377,9 +1374,7 @@ void CPDFSDK_Widget::ResetAppearance_ComboBox(const CFX_WideString* sValue) { std::unique_ptr pEdit(new CFX_Edit); pEdit->EnableRefresh(FALSE); - CPDFSDK_Document* pDoc = m_pInterForm->GetDocument(); - CPDFSDK_FormFillEnvironment* pEnv = pDoc->GetEnv(); - CBA_FontMap font_map(this, pEnv->GetSysHandler()); + CBA_FontMap font_map(this, m_pInterForm->GetFormFillEnv()->GetSysHandler()); pEdit->SetFontMap(&font_map); CFX_FloatRect rcEdit = rcClient; @@ -1443,10 +1438,7 @@ void CPDFSDK_Widget::ResetAppearance_ListBox() { std::unique_ptr pEdit(new CFX_Edit); pEdit->EnableRefresh(FALSE); - CPDFSDK_Document* pDoc = m_pInterForm->GetDocument(); - CPDFSDK_FormFillEnvironment* pEnv = pDoc->GetEnv(); - - CBA_FontMap font_map(this, pEnv->GetSysHandler()); + CBA_FontMap font_map(this, m_pInterForm->GetFormFillEnv()->GetSysHandler()); pEdit->SetFontMap(&font_map); pEdit->SetPlateRect(CFX_FloatRect(rcClient.left, 0.0f, rcClient.right, 0.0f)); @@ -1530,10 +1522,7 @@ void CPDFSDK_Widget::ResetAppearance_TextField(const CFX_WideString* sValue) { std::unique_ptr pEdit(new CFX_Edit); pEdit->EnableRefresh(FALSE); - CPDFSDK_Document* pDoc = m_pInterForm->GetDocument(); - CPDFSDK_FormFillEnvironment* pEnv = pDoc->GetEnv(); - - CBA_FontMap font_map(this, pEnv->GetSysHandler()); + CBA_FontMap font_map(this, m_pInterForm->GetFormFillEnv()->GetSysHandler()); pEdit->SetFontMap(&font_map); CFX_FloatRect rcClient = GetClientRect(); diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp index fa2ac70aa5..14e0692b60 100644 --- a/fpdfsdk/javascript/Field.cpp +++ b/fpdfsdk/javascript/Field.cpp @@ -295,9 +295,14 @@ void Field::UpdateFormField(CPDFSDK_Document* pDocument, std::vector widgets; pInterForm->GetWidgets(pFormField, &widgets); + // TODO(dsinclair): Determine if all widgets share the same + // CPDFSDK_InterForm. If that's the case, we can move the code to + // |GetSDKDocument| out of the loop. for (CPDFSDK_Widget* pWidget : widgets) { - CPDFSDK_Document* pDoc = pWidget->GetInterForm()->GetDocument(); - pDoc->UpdateAllViews(nullptr, pWidget); + pWidget->GetInterForm() + ->GetFormFillEnv() + ->GetSDKDocument() + ->UpdateAllViews(nullptr, pWidget); } } @@ -330,8 +335,8 @@ void Field::UpdateFormControl(CPDFSDK_Document* pDocument, if (bRefresh) { CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm(); - CPDFSDK_Document* pDoc = pInterForm->GetDocument(); - pDoc->UpdateAllViews(nullptr, pWidget); + pInterForm->GetFormFillEnv()->GetSDKDocument()->UpdateAllViews(nullptr, + pWidget); } } -- cgit v1.2.3