Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | bec7692 | 2016-09-29 16:52:30 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/fpdfxfa/fpdfxfa_doc.h" |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 8 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame^] | 9 | #include "core/fpdfapi/parser/cpdf_document.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 10 | #include "fpdfsdk/cpdfsdk_document.h" |
| 11 | #include "fpdfsdk/cpdfsdk_environment.h" |
| 12 | #include "fpdfsdk/cpdfsdk_interform.h" |
| 13 | #include "fpdfsdk/cpdfsdk_pageview.h" |
dsinclair | bec7692 | 2016-09-29 16:52:30 -0700 | [diff] [blame] | 14 | #include "fpdfsdk/fpdfxfa/fpdfxfa_app.h" |
| 15 | #include "fpdfsdk/fpdfxfa/fpdfxfa_page.h" |
| 16 | #include "fpdfsdk/fpdfxfa/fpdfxfa_util.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 17 | #include "fpdfsdk/fsdk_define.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 18 | #include "fpdfsdk/javascript/ijs_runtime.h" |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 19 | #include "public/fpdf_formfill.h" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 20 | #include "xfa/fxfa/cxfa_eventparam.h" |
| 21 | #include "xfa/fxfa/xfa_ffapp.h" |
| 22 | #include "xfa/fxfa/xfa_ffdoc.h" |
| 23 | #include "xfa/fxfa/xfa_ffdocview.h" |
| 24 | #include "xfa/fxfa/xfa_ffpageview.h" |
| 25 | #include "xfa/fxfa/xfa_ffwidgethandler.h" |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 26 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 27 | #ifndef _WIN32 |
| 28 | extern void SetLastError(int err); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 29 | extern int GetLastError(); |
| 30 | #endif |
| 31 | |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 32 | CPDFXFA_Document::CPDFXFA_Document(std::unique_ptr<CPDF_Document> pPDFDoc, |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 33 | CPDFXFA_App* pProvider) |
| 34 | : m_iDocType(DOCTYPE_PDF), |
dsinclair | cedaa55 | 2016-08-24 11:12:19 -0700 | [diff] [blame] | 35 | m_pPDFDoc(std::move(pPDFDoc)), |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 36 | m_pXFADocView(nullptr), |
| 37 | m_pApp(pProvider), |
jinming_wang | a1cef70 | 2016-03-18 16:35:40 +0800 | [diff] [blame] | 38 | m_nLoadStatus(FXFA_LOADSTATUS_PRELOAD), |
dsinclair | a440bb3 | 2016-09-14 07:01:54 -0700 | [diff] [blame] | 39 | m_nPageCount(0), |
| 40 | m_DocEnv(this) {} |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 41 | |
| 42 | CPDFXFA_Document::~CPDFXFA_Document() { |
jinming_wang | a1cef70 | 2016-03-18 16:35:40 +0800 | [diff] [blame] | 43 | m_nLoadStatus = FXFA_LOADSTATUS_CLOSING; |
| 44 | |
Jun Fang | 23bf560 | 2016-02-01 20:29:55 -0800 | [diff] [blame] | 45 | if (m_pXFADoc) { |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 46 | CXFA_FFApp* pApp = m_pApp->GetXFAApp(); |
Jun Fang | 23bf560 | 2016-02-01 20:29:55 -0800 | [diff] [blame] | 47 | if (pApp) { |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 48 | CXFA_FFDocHandler* pDocHandler = pApp->GetDocHandler(); |
dsinclair | f2662c4 | 2016-09-14 06:23:48 -0700 | [diff] [blame] | 49 | if (pDocHandler) |
Jun Fang | 23bf560 | 2016-02-01 20:29:55 -0800 | [diff] [blame] | 50 | CloseXFADoc(pDocHandler); |
Jun Fang | 23bf560 | 2016-02-01 20:29:55 -0800 | [diff] [blame] | 51 | } |
weili | 2d5b020 | 2016-08-03 11:06:49 -0700 | [diff] [blame] | 52 | m_pXFADoc.reset(); |
Jun Fang | 23bf560 | 2016-02-01 20:29:55 -0800 | [diff] [blame] | 53 | } |
jinming_wang | a1cef70 | 2016-03-18 16:35:40 +0800 | [diff] [blame] | 54 | |
| 55 | m_nLoadStatus = FXFA_LOADSTATUS_CLOSED; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | FX_BOOL CPDFXFA_Document::LoadXFADoc() { |
jinming_wang | a1cef70 | 2016-03-18 16:35:40 +0800 | [diff] [blame] | 59 | m_nLoadStatus = FXFA_LOADSTATUS_LOADING; |
| 60 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 61 | if (!m_pPDFDoc) |
| 62 | return FALSE; |
| 63 | |
| 64 | m_XFAPageList.RemoveAll(); |
| 65 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 66 | CXFA_FFApp* pApp = m_pApp->GetXFAApp(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 67 | if (!pApp) |
| 68 | return FALSE; |
| 69 | |
dsinclair | a440bb3 | 2016-09-14 07:01:54 -0700 | [diff] [blame] | 70 | m_pXFADoc.reset(pApp->CreateDoc(&m_DocEnv, m_pPDFDoc.get())); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 71 | if (!m_pXFADoc) { |
| 72 | SetLastError(FPDF_ERR_XFALOAD); |
| 73 | return FALSE; |
| 74 | } |
| 75 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 76 | CXFA_FFDocHandler* pDocHandler = pApp->GetDocHandler(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 77 | if (!pDocHandler) { |
| 78 | SetLastError(FPDF_ERR_XFALOAD); |
| 79 | return FALSE; |
| 80 | } |
| 81 | |
dsinclair | 221caf6 | 2016-04-04 12:08:40 -0700 | [diff] [blame] | 82 | m_pXFADoc->StartLoad(); |
| 83 | int iStatus = m_pXFADoc->DoLoad(nullptr); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 84 | if (iStatus != XFA_PARSESTATUS_Done) { |
| 85 | CloseXFADoc(pDocHandler); |
| 86 | SetLastError(FPDF_ERR_XFALOAD); |
| 87 | return FALSE; |
| 88 | } |
dsinclair | 221caf6 | 2016-04-04 12:08:40 -0700 | [diff] [blame] | 89 | m_pXFADoc->StopLoad(); |
| 90 | m_pXFADoc->GetXFADoc()->InitScriptContext(m_pApp->GetJSERuntime()); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 91 | |
dsinclair | 221caf6 | 2016-04-04 12:08:40 -0700 | [diff] [blame] | 92 | if (m_pXFADoc->GetDocType() == XFA_DOCTYPE_Dynamic) |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 93 | m_iDocType = DOCTYPE_DYNAMIC_XFA; |
| 94 | else |
| 95 | m_iDocType = DOCTYPE_STATIC_XFA; |
| 96 | |
dsinclair | 221caf6 | 2016-04-04 12:08:40 -0700 | [diff] [blame] | 97 | m_pXFADocView = m_pXFADoc->CreateDocView(XFA_DOCVIEW_View); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 98 | if (m_pXFADocView->StartLayout() < 0) { |
| 99 | CloseXFADoc(pDocHandler); |
| 100 | SetLastError(FPDF_ERR_XFALAYOUT); |
| 101 | return FALSE; |
| 102 | } |
| 103 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 104 | m_pXFADocView->DoLayout(nullptr); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 105 | m_pXFADocView->StopLayout(); |
jinming_wang | a1cef70 | 2016-03-18 16:35:40 +0800 | [diff] [blame] | 106 | m_nLoadStatus = FXFA_LOADSTATUS_LOADED; |
| 107 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 108 | return TRUE; |
| 109 | } |
| 110 | |
dsinclair | 9f206f0 | 2016-09-20 12:17:42 -0700 | [diff] [blame] | 111 | int CPDFXFA_Document::GetPageCount() const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 112 | if (!m_pPDFDoc && !m_pXFADoc) |
| 113 | return 0; |
| 114 | |
| 115 | switch (m_iDocType) { |
| 116 | case DOCTYPE_PDF: |
| 117 | case DOCTYPE_STATIC_XFA: |
| 118 | if (m_pPDFDoc) |
| 119 | return m_pPDFDoc->GetPageCount(); |
| 120 | case DOCTYPE_DYNAMIC_XFA: |
| 121 | if (m_pXFADoc) |
| 122 | return m_pXFADocView->CountPageViews(); |
| 123 | default: |
| 124 | return 0; |
| 125 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 126 | } |
| 127 | |
dsinclair | 9f206f0 | 2016-09-20 12:17:42 -0700 | [diff] [blame] | 128 | CPDFXFA_Page* CPDFXFA_Document::GetXFAPage(int page_index) { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 129 | if (page_index < 0) |
| 130 | return nullptr; |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 131 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 132 | CPDFXFA_Page* pPage = nullptr; |
| 133 | int nCount = m_XFAPageList.GetSize(); |
| 134 | if (nCount > 0 && page_index < nCount) { |
| 135 | pPage = m_XFAPageList.GetAt(page_index); |
| 136 | if (pPage) |
tsepez | 44d83f3 | 2016-05-16 14:10:22 -0700 | [diff] [blame] | 137 | pPage->Retain(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 138 | } else { |
jinming_wang | a1cef70 | 2016-03-18 16:35:40 +0800 | [diff] [blame] | 139 | m_nPageCount = GetPageCount(); |
| 140 | m_XFAPageList.SetSize(m_nPageCount); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 141 | } |
| 142 | if (pPage) |
| 143 | return pPage; |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 144 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 145 | pPage = new CPDFXFA_Page(this, page_index); |
| 146 | if (!pPage->LoadPage()) { |
thestig | 9f3dbbc | 2016-04-13 13:18:21 -0700 | [diff] [blame] | 147 | pPage->Release(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 148 | return nullptr; |
| 149 | } |
| 150 | m_XFAPageList.SetAt(page_index, pPage); |
| 151 | return pPage; |
| 152 | } |
| 153 | |
dsinclair | 9f206f0 | 2016-09-20 12:17:42 -0700 | [diff] [blame] | 154 | CPDFXFA_Page* CPDFXFA_Document::GetXFAPage(CXFA_FFPageView* pPage) const { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 155 | if (!pPage) |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 156 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 157 | |
| 158 | if (!m_pXFADoc) |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 159 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 160 | |
| 161 | if (m_iDocType != DOCTYPE_DYNAMIC_XFA) |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 162 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 163 | |
| 164 | int nSize = m_XFAPageList.GetSize(); |
| 165 | for (int i = 0; i < nSize; i++) { |
| 166 | CPDFXFA_Page* pTempPage = m_XFAPageList.GetAt(i); |
| 167 | if (!pTempPage) |
| 168 | continue; |
| 169 | if (pTempPage->GetXFAPageView() && pTempPage->GetXFAPageView() == pPage) |
| 170 | return pTempPage; |
| 171 | } |
| 172 | |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 173 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Tom Sepez | 744da70 | 2016-03-15 12:43:09 -0700 | [diff] [blame] | 176 | void CPDFXFA_Document::DeletePage(int page_index) { |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 177 | // Delete from the document first because, if GetPage was never called for |
| 178 | // this |page_index| then |m_XFAPageList| may have size < |page_index| even |
| 179 | // if it's a valid page in the document. |
| 180 | if (m_pPDFDoc) |
| 181 | m_pPDFDoc->DeletePage(page_index); |
| 182 | |
Tom Sepez | 744da70 | 2016-03-15 12:43:09 -0700 | [diff] [blame] | 183 | if (page_index < 0 || page_index >= m_XFAPageList.GetSize()) |
| 184 | return; |
| 185 | |
| 186 | if (CPDFXFA_Page* pPage = m_XFAPageList.GetAt(page_index)) |
| 187 | pPage->Release(); |
| 188 | } |
| 189 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 190 | void CPDFXFA_Document::RemovePage(CPDFXFA_Page* page) { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 191 | m_XFAPageList.SetAt(page->GetPageIndex(), nullptr); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 192 | } |
| 193 | |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 194 | void CPDFXFA_Document::SetSDKDoc(std::unique_ptr<CPDFSDK_Document> pSDKDoc) { |
| 195 | m_pSDKDoc.reset(pSDKDoc.release()); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 196 | } |
| 197 | |
dsinclair | 89f8fa8 | 2016-09-14 06:11:08 -0700 | [diff] [blame] | 198 | void CPDFXFA_Document::ClearChangeMark() { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 199 | if (m_pSDKDoc) |
| 200 | m_pSDKDoc->ClearChangeMark(); |
| 201 | } |