blob: a49a2708ef367e99d4d0970ac25faa824c4b7643 [file] [log] [blame]
Lei Zhang94293682016-01-27 18:27:56 -08001// 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
dsinclairbec76922016-09-29 16:52:30 -07007#include "fpdfsdk/fpdfxfa/fpdfxfa_doc.h"
thestig9f3dbbc2016-04-13 13:18:21 -07008
dsinclair488b7ad2016-10-04 11:55:50 -07009#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair114e46a2016-09-29 17:18:21 -070010#include "fpdfsdk/cpdfsdk_document.h"
11#include "fpdfsdk/cpdfsdk_environment.h"
12#include "fpdfsdk/cpdfsdk_interform.h"
13#include "fpdfsdk/cpdfsdk_pageview.h"
dsinclairbec76922016-09-29 16:52:30 -070014#include "fpdfsdk/fpdfxfa/fpdfxfa_app.h"
15#include "fpdfsdk/fpdfxfa/fpdfxfa_page.h"
16#include "fpdfsdk/fpdfxfa/fpdfxfa_util.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/fsdk_define.h"
dsinclair64376be2016-03-31 20:03:24 -070018#include "fpdfsdk/javascript/ijs_runtime.h"
Lei Zhang94293682016-01-27 18:27:56 -080019#include "public/fpdf_formfill.h"
dsinclair5b493092016-09-29 20:20:24 -070020#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 Zhang94293682016-01-27 18:27:56 -080026
Lei Zhang94293682016-01-27 18:27:56 -080027#ifndef _WIN32
28extern void SetLastError(int err);
Lei Zhang94293682016-01-27 18:27:56 -080029extern int GetLastError();
30#endif
31
dsinclaircedaa552016-08-24 11:12:19 -070032CPDFXFA_Document::CPDFXFA_Document(std::unique_ptr<CPDF_Document> pPDFDoc,
Lei Zhang94293682016-01-27 18:27:56 -080033 CPDFXFA_App* pProvider)
34 : m_iDocType(DOCTYPE_PDF),
dsinclaircedaa552016-08-24 11:12:19 -070035 m_pPDFDoc(std::move(pPDFDoc)),
Lei Zhang94293682016-01-27 18:27:56 -080036 m_pXFADocView(nullptr),
37 m_pApp(pProvider),
jinming_wanga1cef702016-03-18 16:35:40 +080038 m_nLoadStatus(FXFA_LOADSTATUS_PRELOAD),
dsinclaira440bb32016-09-14 07:01:54 -070039 m_nPageCount(0),
40 m_DocEnv(this) {}
Lei Zhang94293682016-01-27 18:27:56 -080041
42CPDFXFA_Document::~CPDFXFA_Document() {
jinming_wanga1cef702016-03-18 16:35:40 +080043 m_nLoadStatus = FXFA_LOADSTATUS_CLOSING;
44
Jun Fang23bf5602016-02-01 20:29:55 -080045 if (m_pXFADoc) {
dsinclairdf4bc592016-03-31 20:34:43 -070046 CXFA_FFApp* pApp = m_pApp->GetXFAApp();
Jun Fang23bf5602016-02-01 20:29:55 -080047 if (pApp) {
dsinclairdf4bc592016-03-31 20:34:43 -070048 CXFA_FFDocHandler* pDocHandler = pApp->GetDocHandler();
dsinclairf2662c42016-09-14 06:23:48 -070049 if (pDocHandler)
Jun Fang23bf5602016-02-01 20:29:55 -080050 CloseXFADoc(pDocHandler);
Jun Fang23bf5602016-02-01 20:29:55 -080051 }
weili2d5b0202016-08-03 11:06:49 -070052 m_pXFADoc.reset();
Jun Fang23bf5602016-02-01 20:29:55 -080053 }
jinming_wanga1cef702016-03-18 16:35:40 +080054
55 m_nLoadStatus = FXFA_LOADSTATUS_CLOSED;
Lei Zhang94293682016-01-27 18:27:56 -080056}
57
58FX_BOOL CPDFXFA_Document::LoadXFADoc() {
jinming_wanga1cef702016-03-18 16:35:40 +080059 m_nLoadStatus = FXFA_LOADSTATUS_LOADING;
60
Lei Zhang94293682016-01-27 18:27:56 -080061 if (!m_pPDFDoc)
62 return FALSE;
63
64 m_XFAPageList.RemoveAll();
65
dsinclairdf4bc592016-03-31 20:34:43 -070066 CXFA_FFApp* pApp = m_pApp->GetXFAApp();
Lei Zhang94293682016-01-27 18:27:56 -080067 if (!pApp)
68 return FALSE;
69
dsinclaira440bb32016-09-14 07:01:54 -070070 m_pXFADoc.reset(pApp->CreateDoc(&m_DocEnv, m_pPDFDoc.get()));
Lei Zhang94293682016-01-27 18:27:56 -080071 if (!m_pXFADoc) {
72 SetLastError(FPDF_ERR_XFALOAD);
73 return FALSE;
74 }
75
dsinclairdf4bc592016-03-31 20:34:43 -070076 CXFA_FFDocHandler* pDocHandler = pApp->GetDocHandler();
Lei Zhang94293682016-01-27 18:27:56 -080077 if (!pDocHandler) {
78 SetLastError(FPDF_ERR_XFALOAD);
79 return FALSE;
80 }
81
dsinclair221caf62016-04-04 12:08:40 -070082 m_pXFADoc->StartLoad();
83 int iStatus = m_pXFADoc->DoLoad(nullptr);
Lei Zhang94293682016-01-27 18:27:56 -080084 if (iStatus != XFA_PARSESTATUS_Done) {
85 CloseXFADoc(pDocHandler);
86 SetLastError(FPDF_ERR_XFALOAD);
87 return FALSE;
88 }
dsinclair221caf62016-04-04 12:08:40 -070089 m_pXFADoc->StopLoad();
90 m_pXFADoc->GetXFADoc()->InitScriptContext(m_pApp->GetJSERuntime());
Lei Zhang94293682016-01-27 18:27:56 -080091
dsinclair221caf62016-04-04 12:08:40 -070092 if (m_pXFADoc->GetDocType() == XFA_DOCTYPE_Dynamic)
Lei Zhang94293682016-01-27 18:27:56 -080093 m_iDocType = DOCTYPE_DYNAMIC_XFA;
94 else
95 m_iDocType = DOCTYPE_STATIC_XFA;
96
dsinclair221caf62016-04-04 12:08:40 -070097 m_pXFADocView = m_pXFADoc->CreateDocView(XFA_DOCVIEW_View);
Lei Zhang94293682016-01-27 18:27:56 -080098 if (m_pXFADocView->StartLayout() < 0) {
99 CloseXFADoc(pDocHandler);
100 SetLastError(FPDF_ERR_XFALAYOUT);
101 return FALSE;
102 }
103
thestig1cd352e2016-06-07 17:53:06 -0700104 m_pXFADocView->DoLayout(nullptr);
Lei Zhang94293682016-01-27 18:27:56 -0800105 m_pXFADocView->StopLayout();
jinming_wanga1cef702016-03-18 16:35:40 +0800106 m_nLoadStatus = FXFA_LOADSTATUS_LOADED;
107
Lei Zhang94293682016-01-27 18:27:56 -0800108 return TRUE;
109}
110
dsinclair9f206f02016-09-20 12:17:42 -0700111int CPDFXFA_Document::GetPageCount() const {
Lei Zhang94293682016-01-27 18:27:56 -0800112 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 Zhang94293682016-01-27 18:27:56 -0800126}
127
dsinclair9f206f02016-09-20 12:17:42 -0700128CPDFXFA_Page* CPDFXFA_Document::GetXFAPage(int page_index) {
Lei Zhang94293682016-01-27 18:27:56 -0800129 if (page_index < 0)
130 return nullptr;
dsinclair1f5f2ff2016-04-25 14:14:56 -0700131
Lei Zhang94293682016-01-27 18:27:56 -0800132 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)
tsepez44d83f32016-05-16 14:10:22 -0700137 pPage->Retain();
Lei Zhang94293682016-01-27 18:27:56 -0800138 } else {
jinming_wanga1cef702016-03-18 16:35:40 +0800139 m_nPageCount = GetPageCount();
140 m_XFAPageList.SetSize(m_nPageCount);
Lei Zhang94293682016-01-27 18:27:56 -0800141 }
142 if (pPage)
143 return pPage;
dsinclair1f5f2ff2016-04-25 14:14:56 -0700144
Lei Zhang94293682016-01-27 18:27:56 -0800145 pPage = new CPDFXFA_Page(this, page_index);
146 if (!pPage->LoadPage()) {
thestig9f3dbbc2016-04-13 13:18:21 -0700147 pPage->Release();
Lei Zhang94293682016-01-27 18:27:56 -0800148 return nullptr;
149 }
150 m_XFAPageList.SetAt(page_index, pPage);
151 return pPage;
152}
153
dsinclair9f206f02016-09-20 12:17:42 -0700154CPDFXFA_Page* CPDFXFA_Document::GetXFAPage(CXFA_FFPageView* pPage) const {
Lei Zhang94293682016-01-27 18:27:56 -0800155 if (!pPage)
dsinclair1f5f2ff2016-04-25 14:14:56 -0700156 return nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800157
158 if (!m_pXFADoc)
dsinclair1f5f2ff2016-04-25 14:14:56 -0700159 return nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800160
161 if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
dsinclair1f5f2ff2016-04-25 14:14:56 -0700162 return nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800163
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
dsinclair1f5f2ff2016-04-25 14:14:56 -0700173 return nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800174}
175
Tom Sepez744da702016-03-15 12:43:09 -0700176void CPDFXFA_Document::DeletePage(int page_index) {
dsinclair1f5f2ff2016-04-25 14:14:56 -0700177 // 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 Sepez744da702016-03-15 12:43:09 -0700183 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 Zhang94293682016-01-27 18:27:56 -0800190void CPDFXFA_Document::RemovePage(CPDFXFA_Page* page) {
thestig1cd352e2016-06-07 17:53:06 -0700191 m_XFAPageList.SetAt(page->GetPageIndex(), nullptr);
Lei Zhang94293682016-01-27 18:27:56 -0800192}
193
dsinclaira939bfe2016-09-22 13:18:45 -0700194void CPDFXFA_Document::SetSDKDoc(std::unique_ptr<CPDFSDK_Document> pSDKDoc) {
195 m_pSDKDoc.reset(pSDKDoc.release());
Lei Zhang94293682016-01-27 18:27:56 -0800196}
197
dsinclair89f8fa82016-09-14 06:11:08 -0700198void CPDFXFA_Document::ClearChangeMark() {
Lei Zhang94293682016-01-27 18:27:56 -0800199 if (m_pSDKDoc)
200 m_pSDKDoc->ClearChangeMark();
201}