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
7 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
8
9 #include <algorithm>
10 #include <utility>
11
12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fpdfapi/parser/cpdf_seekablemultistream.h"
16 #include "core/fxcrt/autonuller.h"
17 #include "core/fxcrt/fx_memory_wrappers.h"
18 #include "core/fxcrt/xml/cfx_xmldocument.h"
19 #include "core/fxcrt/xml/cfx_xmlparser.h"
20 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
21 #include "fpdfsdk/cpdfsdk_pageview.h"
22 #include "fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.h"
23 #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
24 #include "fxbarcode/BC_Library.h"
25 #include "fxjs/cjs_runtime.h"
26 #include "fxjs/ijs_runtime.h"
27 #include "public/fpdf_formfill.h"
28 #include "third_party/base/notreached.h"
29 #include "third_party/base/stl_util.h"
30 #include "v8/include/cppgc/allocation.h"
31 #include "xfa/fgas/font/cfgas_gemodule.h"
32 #include "xfa/fxfa/cxfa_eventparam.h"
33 #include "xfa/fxfa/cxfa_ffapp.h"
34 #include "xfa/fxfa/cxfa_ffdoc.h"
35 #include "xfa/fxfa/cxfa_ffdocview.h"
36 #include "xfa/fxfa/cxfa_ffpageview.h"
37 #include "xfa/fxfa/cxfa_ffwidgethandler.h"
38 #include "xfa/fxfa/cxfa_fontmgr.h"
39 #include "xfa/fxfa/cxfa_readynodeiterator.h"
40
41 namespace {
42
IsValidAlertButton(int type)43 bool IsValidAlertButton(int type) {
44 return type == JSPLATFORM_ALERT_BUTTON_OK ||
45 type == JSPLATFORM_ALERT_BUTTON_OKCANCEL ||
46 type == JSPLATFORM_ALERT_BUTTON_YESNO ||
47 type == JSPLATFORM_ALERT_BUTTON_YESNOCANCEL;
48 }
49
IsValidAlertIcon(int type)50 bool IsValidAlertIcon(int type) {
51 return type == JSPLATFORM_ALERT_ICON_ERROR ||
52 type == JSPLATFORM_ALERT_ICON_WARNING ||
53 type == JSPLATFORM_ALERT_ICON_QUESTION ||
54 type == JSPLATFORM_ALERT_ICON_STATUS ||
55 type == JSPLATFORM_ALERT_ICON_ASTERISK;
56 }
57
CreateXFAMultiStream(const CPDF_Document * pPDFDoc)58 RetainPtr<CPDF_SeekableMultiStream> CreateXFAMultiStream(
59 const CPDF_Document* pPDFDoc) {
60 const CPDF_Dictionary* pRoot = pPDFDoc->GetRoot();
61 if (!pRoot)
62 return nullptr;
63
64 const CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
65 if (!pAcroForm)
66 return nullptr;
67
68 const CPDF_Object* pElementXFA = pAcroForm->GetDirectObjectFor("XFA");
69 if (!pElementXFA)
70 return nullptr;
71
72 std::vector<const CPDF_Stream*> xfaStreams;
73 if (pElementXFA->IsArray()) {
74 const CPDF_Array* pXFAArray = pElementXFA->AsArray();
75 for (size_t i = 0; i < pXFAArray->size() / 2; i++) {
76 if (const CPDF_Stream* pStream = pXFAArray->GetStreamAt(i * 2 + 1))
77 xfaStreams.push_back(pStream);
78 }
79 } else if (pElementXFA->IsStream()) {
80 xfaStreams.push_back(pElementXFA->AsStream());
81 }
82 if (xfaStreams.empty())
83 return nullptr;
84
85 return pdfium::MakeRetain<CPDF_SeekableMultiStream>(xfaStreams);
86 }
87
88 } // namespace
89
CPDFXFA_ModuleInit()90 void CPDFXFA_ModuleInit() {
91 CFGAS_GEModule::Create();
92 BC_Library_Init();
93 }
94
CPDFXFA_ModuleDestroy()95 void CPDFXFA_ModuleDestroy() {
96 BC_Library_Destroy();
97 CFGAS_GEModule::Destroy();
98 }
99
CPDFXFA_Context(CPDF_Document * pPDFDoc)100 CPDFXFA_Context::CPDFXFA_Context(CPDF_Document* pPDFDoc)
101 : m_pPDFDoc(pPDFDoc),
102 m_pDocEnv(std::make_unique<CPDFXFA_DocEnvironment>(this)),
103 m_pGCHeap(FXGC_CreateHeap()) {
104 ASSERT(m_pPDFDoc);
105
106 // There might not be a heap when JS not initialized.
107 if (m_pGCHeap) {
108 m_pXFAApp = cppgc::MakeGarbageCollected<CXFA_FFApp>(
109 m_pGCHeap->GetAllocationHandle(), this);
110 }
111 }
112
~CPDFXFA_Context()113 CPDFXFA_Context::~CPDFXFA_Context() {
114 m_nLoadStatus = FXFA_LOADSTATUS_CLOSING;
115 if (m_pFormFillEnv)
116 m_pFormFillEnv->ClearAllFocusedAnnots();
117 }
118
SetFormFillEnv(CPDFSDK_FormFillEnvironment * pFormFillEnv)119 void CPDFXFA_Context::SetFormFillEnv(
120 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
121 // The layout data can have pointers back into the script context. That
122 // context will be different if the form fill environment closes, so, force
123 // the layout data to clear.
124 if (m_pXFADoc && m_pXFADoc->GetXFADoc()) {
125 m_pXFADoc->GetXFADoc()->ClearLayoutData();
126 m_pXFADocView.Clear();
127 m_pXFADoc.Clear();
128 m_pXFAApp.Clear();
129 FXGC_ForceGarbageCollection(m_pGCHeap.get());
130 }
131 m_pFormFillEnv.Reset(pFormFillEnv);
132 }
133
LoadXFADoc()134 bool CPDFXFA_Context::LoadXFADoc() {
135 m_nLoadStatus = FXFA_LOADSTATUS_LOADING;
136 m_XFAPageList.clear();
137
138 CJS_Runtime* actual_runtime = GetCJSRuntime(); // Null if a stub.
139 if (!actual_runtime) {
140 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
141 return false;
142 }
143
144 auto stream = CreateXFAMultiStream(m_pPDFDoc.Get());
145 if (!stream) {
146 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
147 return false;
148 }
149
150 CFX_XMLParser parser(stream);
151 m_pXML = parser.Parse();
152 if (!m_pXML) {
153 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
154 return false;
155 }
156
157 AutoNuller<cppgc::Persistent<CXFA_FFDoc>> doc_nuller(&m_pXFADoc);
158 m_pXFADoc = cppgc::MakeGarbageCollected<CXFA_FFDoc>(
159 m_pGCHeap->GetAllocationHandle(), m_pXFAApp, m_pDocEnv.get(),
160 m_pPDFDoc.Get(), m_pGCHeap.get());
161
162 if (!m_pXFADoc->OpenDoc(m_pXML.get())) {
163 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
164 return false;
165 }
166
167 if (!m_pXFAApp->LoadFWLTheme(m_pXFADoc)) {
168 FXSYS_SetLastError(FPDF_ERR_XFALAYOUT);
169 return false;
170 }
171
172 m_pXFADoc->GetXFADoc()->InitScriptContext(actual_runtime);
173 if (m_pXFADoc->GetFormType() == FormType::kXFAFull)
174 m_FormType = FormType::kXFAFull;
175 else
176 m_FormType = FormType::kXFAForeground;
177
178 AutoNuller<cppgc::Persistent<CXFA_FFDocView>> view_nuller(&m_pXFADocView);
179 m_pXFADocView = m_pXFADoc->CreateDocView();
180
181 if (m_pXFADocView->StartLayout() < 0) {
182 m_pXFADoc->GetXFADoc()->ClearLayoutData();
183 FXGC_ForceGarbageCollection(m_pGCHeap.get());
184 FXSYS_SetLastError(FPDF_ERR_XFALAYOUT);
185 return false;
186 }
187
188 m_pXFADocView->DoLayout();
189 m_pXFADocView->StopLayout();
190
191 view_nuller.AbandonNullification();
192 doc_nuller.AbandonNullification();
193 m_nLoadStatus = FXFA_LOADSTATUS_LOADED;
194 return true;
195 }
196
GetPageCount() const197 int CPDFXFA_Context::GetPageCount() const {
198 switch (m_FormType) {
199 case FormType::kNone:
200 case FormType::kAcroForm:
201 case FormType::kXFAForeground:
202 return m_pPDFDoc->GetPageCount();
203 case FormType::kXFAFull:
204 return m_pXFADoc ? m_pXFADocView->CountPageViews() : 0;
205 }
206 NOTREACHED();
207 return 0;
208 }
209
GetXFAPage(int page_index)210 RetainPtr<CPDFXFA_Page> CPDFXFA_Context::GetXFAPage(int page_index) {
211 if (page_index < 0)
212 return nullptr;
213
214 if (pdfium::IndexInBounds(m_XFAPageList, page_index)) {
215 if (m_XFAPageList[page_index])
216 return m_XFAPageList[page_index];
217 } else {
218 m_nPageCount = GetPageCount();
219 m_XFAPageList.resize(m_nPageCount);
220 }
221
222 auto pPage = pdfium::MakeRetain<CPDFXFA_Page>(GetPDFDoc(), page_index);
223 if (!pPage->LoadPage())
224 return nullptr;
225
226 if (pdfium::IndexInBounds(m_XFAPageList, page_index))
227 m_XFAPageList[page_index] = pPage;
228
229 return pPage;
230 }
231
GetXFAPage(CXFA_FFPageView * pPage) const232 RetainPtr<CPDFXFA_Page> CPDFXFA_Context::GetXFAPage(
233 CXFA_FFPageView* pPage) const {
234 if (!pPage)
235 return nullptr;
236
237 if (!m_pXFADoc)
238 return nullptr;
239
240 if (m_FormType != FormType::kXFAFull)
241 return nullptr;
242
243 for (auto& pTempPage : m_XFAPageList) {
244 if (pTempPage && pTempPage->GetXFAPageView() == pPage)
245 return pTempPage;
246 }
247 return nullptr;
248 }
249
GetPDFDoc() const250 CPDF_Document* CPDFXFA_Context::GetPDFDoc() const {
251 return m_pPDFDoc.Get();
252 }
253
DeletePage(int page_index)254 void CPDFXFA_Context::DeletePage(int page_index) {
255 // Delete from the document first because, if GetPage was never called for
256 // this |page_index| then |m_XFAPageList| may have size < |page_index| even
257 // if it's a valid page in the document.
258 m_pPDFDoc->DeletePage(page_index);
259
260 if (pdfium::IndexInBounds(m_XFAPageList, page_index))
261 m_XFAPageList[page_index].Reset();
262 }
263
GetUserPermissions() const264 uint32_t CPDFXFA_Context::GetUserPermissions() const {
265 // See https://bugs.chromium.org/p/pdfium/issues/detail?id=499
266 return 0xFFFFFFFF;
267 }
268
ContainsExtensionForm() const269 bool CPDFXFA_Context::ContainsExtensionForm() const {
270 return m_FormType == FormType::kXFAFull ||
271 m_FormType == FormType::kXFAForeground;
272 }
273
ContainsExtensionFullForm() const274 bool CPDFXFA_Context::ContainsExtensionFullForm() const {
275 return m_FormType == FormType::kXFAFull;
276 }
277
ContainsExtensionForegroundForm() const278 bool CPDFXFA_Context::ContainsExtensionForegroundForm() const {
279 return m_FormType == FormType::kXFAForeground;
280 }
281
ClearChangeMark()282 void CPDFXFA_Context::ClearChangeMark() {
283 if (m_pFormFillEnv)
284 m_pFormFillEnv->ClearChangeMark();
285 }
286
GetCJSRuntime() const287 CJS_Runtime* CPDFXFA_Context::GetCJSRuntime() const {
288 if (!m_pFormFillEnv)
289 return nullptr;
290
291 return m_pFormFillEnv->GetIJSRuntime()->AsCJSRuntime();
292 }
293
GetAppTitle() const294 WideString CPDFXFA_Context::GetAppTitle() const {
295 return L"PDFium";
296 }
297
GetAppName()298 WideString CPDFXFA_Context::GetAppName() {
299 return m_pFormFillEnv ? m_pFormFillEnv->FFI_GetAppName() : WideString();
300 }
301
GetLanguage()302 WideString CPDFXFA_Context::GetLanguage() {
303 return m_pFormFillEnv ? m_pFormFillEnv->GetLanguage() : WideString();
304 }
305
GetPlatform()306 WideString CPDFXFA_Context::GetPlatform() {
307 return m_pFormFillEnv ? m_pFormFillEnv->GetPlatform() : WideString();
308 }
309
Beep(uint32_t dwType)310 void CPDFXFA_Context::Beep(uint32_t dwType) {
311 if (m_pFormFillEnv)
312 m_pFormFillEnv->JS_appBeep(dwType);
313 }
314
MsgBox(const WideString & wsMessage,const WideString & wsTitle,uint32_t dwIconType,uint32_t dwButtonType)315 int32_t CPDFXFA_Context::MsgBox(const WideString& wsMessage,
316 const WideString& wsTitle,
317 uint32_t dwIconType,
318 uint32_t dwButtonType) {
319 if (!m_pFormFillEnv || m_nLoadStatus != FXFA_LOADSTATUS_LOADED)
320 return -1;
321
322 int iconType =
323 IsValidAlertIcon(dwIconType) ? dwIconType : JSPLATFORM_ALERT_ICON_DEFAULT;
324 int iButtonType = IsValidAlertButton(dwButtonType)
325 ? dwButtonType
326 : JSPLATFORM_ALERT_BUTTON_DEFAULT;
327 return m_pFormFillEnv->JS_appAlert(wsMessage, wsTitle, iButtonType, iconType);
328 }
329
Response(const WideString & wsQuestion,const WideString & wsTitle,const WideString & wsDefaultAnswer,bool bMark)330 WideString CPDFXFA_Context::Response(const WideString& wsQuestion,
331 const WideString& wsTitle,
332 const WideString& wsDefaultAnswer,
333 bool bMark) {
334 if (!m_pFormFillEnv)
335 return WideString();
336
337 int nLength = 2048;
338 std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nLength);
339 nLength = m_pFormFillEnv->JS_appResponse(wsQuestion, wsTitle, wsDefaultAnswer,
340 WideString(), bMark, pBuff.data(),
341 nLength);
342 if (nLength <= 0)
343 return WideString();
344
345 nLength = std::min(2046, nLength);
346 pBuff[nLength] = 0;
347 pBuff[nLength + 1] = 0;
348 return WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
349 nLength / sizeof(uint16_t));
350 }
351
DownloadURL(const WideString & wsURL)352 RetainPtr<IFX_SeekableReadStream> CPDFXFA_Context::DownloadURL(
353 const WideString& wsURL) {
354 return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL) : nullptr;
355 }
356
PostRequestURL(const WideString & wsURL,const WideString & wsData,const WideString & wsContentType,const WideString & wsEncode,const WideString & wsHeader,WideString & wsResponse)357 bool CPDFXFA_Context::PostRequestURL(const WideString& wsURL,
358 const WideString& wsData,
359 const WideString& wsContentType,
360 const WideString& wsEncode,
361 const WideString& wsHeader,
362 WideString& wsResponse) {
363 if (!m_pFormFillEnv)
364 return false;
365
366 wsResponse = m_pFormFillEnv->PostRequestURL(wsURL, wsData, wsContentType,
367 wsEncode, wsHeader);
368 return true;
369 }
370
PutRequestURL(const WideString & wsURL,const WideString & wsData,const WideString & wsEncode)371 bool CPDFXFA_Context::PutRequestURL(const WideString& wsURL,
372 const WideString& wsData,
373 const WideString& wsEncode) {
374 return m_pFormFillEnv &&
375 m_pFormFillEnv->PutRequestURL(wsURL, wsData, wsEncode);
376 }
377
GetTimerHandler() const378 CFX_Timer::HandlerIface* CPDFXFA_Context::GetTimerHandler() const {
379 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() : nullptr;
380 }
381
GetGCHeap() const382 cppgc::Heap* CPDFXFA_Context::GetGCHeap() const {
383 return m_pGCHeap.get();
384 }
385
SaveDatasetsPackage(const RetainPtr<IFX_SeekableStream> & pStream)386 bool CPDFXFA_Context::SaveDatasetsPackage(
387 const RetainPtr<IFX_SeekableStream>& pStream) {
388 return SavePackage(pStream, XFA_HASHCODE_Datasets);
389 }
390
SaveFormPackage(const RetainPtr<IFX_SeekableStream> & pStream)391 bool CPDFXFA_Context::SaveFormPackage(
392 const RetainPtr<IFX_SeekableStream>& pStream) {
393 return SavePackage(pStream, XFA_HASHCODE_Form);
394 }
395
SavePackage(const RetainPtr<IFX_SeekableStream> & pStream,XFA_HashCode code)396 bool CPDFXFA_Context::SavePackage(const RetainPtr<IFX_SeekableStream>& pStream,
397 XFA_HashCode code) {
398 CXFA_FFDocView* pXFADocView = GetXFADocView();
399 if (!pXFADocView)
400 return false;
401
402 CXFA_FFDoc* ffdoc = pXFADocView->GetDoc();
403 return ffdoc->SavePackage(ToNode(ffdoc->GetXFADoc()->GetXFAObject(code)),
404 pStream);
405 }
406
SendPostSaveToXFADoc()407 void CPDFXFA_Context::SendPostSaveToXFADoc() {
408 if (!ContainsExtensionForm())
409 return;
410
411 CXFA_FFDocView* pXFADocView = GetXFADocView();
412 if (!pXFADocView)
413 return;
414
415 CXFA_FFWidgetHandler* pWidgetHandler = pXFADocView->GetWidgetHandler();
416 CXFA_ReadyNodeIterator it(pXFADocView->GetRootSubform());
417 while (CXFA_Node* pNode = it.MoveToNext()) {
418 CXFA_EventParam preParam;
419 preParam.m_eType = XFA_EVENT_PostSave;
420 pWidgetHandler->ProcessEvent(pNode, &preParam);
421 }
422 pXFADocView->UpdateDocView();
423 ClearChangeMark();
424 }
425
SendPreSaveToXFADoc(std::vector<RetainPtr<IFX_SeekableStream>> * fileList)426 void CPDFXFA_Context::SendPreSaveToXFADoc(
427 std::vector<RetainPtr<IFX_SeekableStream>>* fileList) {
428 if (!ContainsExtensionForm())
429 return;
430
431 CXFA_FFDocView* pXFADocView = GetXFADocView();
432 if (!pXFADocView)
433 return;
434
435 CXFA_FFWidgetHandler* pWidgetHandler = pXFADocView->GetWidgetHandler();
436 CXFA_ReadyNodeIterator it(pXFADocView->GetRootSubform());
437 while (CXFA_Node* pNode = it.MoveToNext()) {
438 CXFA_EventParam preParam;
439 preParam.m_eType = XFA_EVENT_PreSave;
440 pWidgetHandler->ProcessEvent(pNode, &preParam);
441 }
442 pXFADocView->UpdateDocView();
443 return;
444 }
445