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