Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdfxfa_context.cpp
Go to the documentation of this file.
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/fixed_size_data_vector.h"
21#include "core/fxcrt/stl_util.h"
22#include "core/fxcrt/xml/cfx_xmldocument.h"
23#include "core/fxcrt/xml/cfx_xmlparser.h"
24#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
25#include "fpdfsdk/cpdfsdk_pageview.h"
26#include "fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.h"
27#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
28#include "fxbarcode/BC_Library.h"
29#include "fxjs/cjs_runtime.h"
30#include "fxjs/ijs_runtime.h"
31#include "public/fpdf_formfill.h"
32#include "third_party/base/check.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
52namespace {
53
54bool IsValidAlertButton(int type) {
55 return type == JSPLATFORM_ALERT_BUTTON_OK ||
59}
60
61bool IsValidAlertIcon(int type) {
62 return type == JSPLATFORM_ALERT_ICON_ERROR ||
67}
68
69RetainPtr<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
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
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
125}
126
127CPDFXFA_Context::CPDFXFA_Context(CPDF_Document* pPDFDoc)
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
140CPDFXFA_Context::~CPDFXFA_Context() {
141 m_nLoadStatus = LoadStatus::kClosing;
142 if (m_pFormFillEnv)
143 m_pFormFillEnv->ClearAllFocusedAnnots();
144}
145
146void 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
161bool 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
224int 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
235RetainPtr<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
257RetainPtr<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
264RetainPtr<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
282void 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 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
298bool CPDFXFA_Context::ContainsExtensionForm() const {
299 return m_FormType == FormType::kXFAFull ||
300 m_FormType == FormType::kXFAForeground;
301}
302
303bool CPDFXFA_Context::ContainsExtensionFullForm() const {
304 return m_FormType == FormType::kXFAFull;
305}
306
307bool CPDFXFA_Context::ContainsExtensionForegroundForm() const {
308 return m_FormType == FormType::kXFAForeground;
309}
310
311void CPDFXFA_Context::ClearChangeMark() {
312 if (m_pFormFillEnv)
313 m_pFormFillEnv->ClearChangeMark();
314}
315
316CJS_Runtime* CPDFXFA_Context::GetCJSRuntime() const {
317 if (!m_pFormFillEnv)
318 return nullptr;
319
320 return m_pFormFillEnv->GetIJSRuntime()->AsCJSRuntime();
321}
322
323WideString CPDFXFA_Context::GetAppTitle() const {
324 return L"PDFium";
325}
326
327WideString CPDFXFA_Context::GetAppName() {
328 return m_pFormFillEnv ? m_pFormFillEnv->FFI_GetAppName() : WideString();
329}
330
331WideString CPDFXFA_Context::GetLanguage() {
332 return m_pFormFillEnv ? m_pFormFillEnv->GetLanguage() : WideString();
333}
334
335WideString CPDFXFA_Context::GetPlatform() {
336 return m_pFormFillEnv ? m_pFormFillEnv->GetPlatform() : WideString();
337}
338
339void CPDFXFA_Context::Beep(uint32_t dwType) {
340 if (m_pFormFillEnv)
341 m_pFormFillEnv->JS_appBeep(dwType);
342}
343
344int32_t CPDFXFA_Context::MsgBox(const WideString& wsMessage,
345 const WideString& wsTitle,
346 uint32_t dwIconType,
347 uint32_t dwButtonType) {
348 if (!m_pFormFillEnv || m_nLoadStatus != LoadStatus::kLoaded)
349 return -1;
350
351 int iconType =
352 IsValidAlertIcon(dwIconType) ? dwIconType : JSPLATFORM_ALERT_ICON_DEFAULT;
353 int iButtonType = IsValidAlertButton(dwButtonType)
354 ? dwButtonType
356 return m_pFormFillEnv->JS_appAlert(wsMessage, wsTitle, iButtonType, iconType);
357}
358
359WideString CPDFXFA_Context::Response(const WideString& wsQuestion,
360 const WideString& wsTitle,
361 const WideString& wsDefaultAnswer,
362 bool bMark) {
363 if (!m_pFormFillEnv)
364 return WideString();
365
366 constexpr int kMaxWideChars = 1024;
367 constexpr int kMaxBytes = kMaxWideChars * sizeof(uint16_t);
368 auto buffer = FixedSizeDataVector<uint8_t>::Zeroed(kMaxBytes);
369 pdfium::span<uint8_t> buffer_span = buffer.span();
370 int byte_length = m_pFormFillEnv->JS_appResponse(
371 wsQuestion, wsTitle, wsDefaultAnswer, WideString(), bMark, buffer_span);
372 if (byte_length <= 0)
373 return WideString();
374
375 buffer_span = buffer_span.first(std::min<size_t>(kMaxBytes, byte_length));
376 return WideString::FromUTF16LE(buffer_span);
377}
378
380 const WideString& wsURL) {
381 return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL) : nullptr;
382}
383
384bool CPDFXFA_Context::PostRequestURL(const WideString& wsURL,
385 const WideString& wsData,
386 const WideString& wsContentType,
387 const WideString& wsEncode,
388 const WideString& wsHeader,
389 WideString& wsResponse) {
390 if (!m_pFormFillEnv)
391 return false;
392
393 wsResponse = m_pFormFillEnv->PostRequestURL(wsURL, wsData, wsContentType,
394 wsEncode, wsHeader);
395 return true;
396}
397
398bool CPDFXFA_Context::PutRequestURL(const WideString& wsURL,
399 const WideString& wsData,
400 const WideString& wsEncode) {
401 return m_pFormFillEnv &&
402 m_pFormFillEnv->PutRequestURL(wsURL, wsData, wsEncode);
403}
404
405CFX_Timer::HandlerIface* CPDFXFA_Context::GetTimerHandler() const {
406 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() : nullptr;
407}
408
409cppgc::Heap* CPDFXFA_Context::GetGCHeap() const {
410 return m_pGCHeap.get();
411}
412
413bool CPDFXFA_Context::SaveDatasetsPackage(
414 const RetainPtr<IFX_SeekableStream>& pStream) {
415 return SavePackage(pStream, XFA_HASHCODE_Datasets);
416}
417
418bool CPDFXFA_Context::SaveFormPackage(
419 const RetainPtr<IFX_SeekableStream>& pStream) {
420 return SavePackage(pStream, XFA_HASHCODE_Form);
421}
422
423bool CPDFXFA_Context::SavePackage(const RetainPtr<IFX_SeekableStream>& pStream,
424 XFA_HashCode code) {
425 CXFA_FFDocView* pXFADocView = GetXFADocView();
426 if (!pXFADocView)
427 return false;
428
429 CXFA_FFDoc* ffdoc = pXFADocView->GetDoc();
431 pStream);
432}
433
434void CPDFXFA_Context::SendPostSaveToXFADoc() {
436 return;
437
438 CXFA_FFDocView* pXFADocView = GetXFADocView();
439 if (!pXFADocView)
440 return;
441
442 CXFA_FFWidgetHandler* pWidgetHandler = pXFADocView->GetWidgetHandler();
444 while (CXFA_Node* pNode = it.MoveToNext()) {
446 preParam.m_bTargeted = false;
447 pWidgetHandler->ProcessEvent(pNode, &preParam);
448 }
449 pXFADocView->UpdateDocView();
451}
452
453void CPDFXFA_Context::SendPreSaveToXFADoc(
454 std::vector<RetainPtr<IFX_SeekableStream>>* fileList) {
456 return;
457
458 CXFA_FFDocView* pXFADocView = GetXFADocView();
459 if (!pXFADocView)
460 return;
461
462 CXFA_FFWidgetHandler* pWidgetHandler = pXFADocView->GetWidgetHandler();
464 while (CXFA_Node* pNode = it.MoveToNext()) {
466 preParam.m_bTargeted = false;
467 pWidgetHandler->ProcessEvent(pNode, &preParam);
468 }
469 pXFADocView->UpdateDocView();
470 return;
471}
void BC_Library_Init()
void BC_Library_Destroy()
static void Destroy()
static void Create()
WideString GetPlatform() override
RetainPtr< IFX_SeekableReadStream > DownloadURL(const WideString &wsURL) override
bool PostRequestURL(const WideString &wsURL, const WideString &wsData, const WideString &wsContentType, const WideString &wsEncode, const WideString &wsHeader, WideString &wsResponse) override
RetainPtr< CPDFXFA_Page > GetXFAPage(CXFA_FFPageView *pPage) const
WideString GetAppTitle() const override
WideString Response(const WideString &wsQuestion, const WideString &wsTitle, const WideString &wsDefaultAnswer, bool bMark) override
bool SaveDatasetsPackage(const RetainPtr< IFX_SeekableStream > &pStream)
~CPDFXFA_Context() override
void SetFormFillEnv(CPDFSDK_FormFillEnvironment *pFormFillEnv)
bool PutRequestURL(const WideString &wsURL, const WideString &wsData, const WideString &wsEncode) override
void SendPreSaveToXFADoc(std::vector< RetainPtr< IFX_SeekableStream > > *fileList)
CXFA_FFDocView * GetXFADocView() const
WideString GetAppName() override
bool SaveFormPackage(const RetainPtr< IFX_SeekableStream > &pStream)
CFX_Timer::HandlerIface * GetTimerHandler() const override
void Beep(uint32_t dwType) override
bool ContainsExtensionForegroundForm() const override
CPDFXFA_Context(CPDF_Document *pPDFDoc)
RetainPtr< CPDFXFA_Page > GetOrCreateXFAPage(int page_index)
bool ContainsExtensionForm() const override
WideString GetLanguage() override
RetainPtr< CPDFXFA_Page > GetXFAPage(int page_index)
void DeletePage(int page_index) override
bool ContainsExtensionFullForm() const override
int GetPageCount() const override
cppgc::Heap * GetGCHeap() const override
int32_t MsgBox(const WideString &wsMessage, const WideString &wsTitle, uint32_t dwIconType, uint32_t dwButtonType) override
CPDF_Document * GetPDFDoc() const
const CPDF_Dictionary * GetRoot() const
CXFA_Object * GetXFAObject(XFA_HashCode wsNodeNameHash)
CXFA_EventParam(XFA_EVENTTYPE type)
CXFA_FFWidgetHandler * GetWidgetHandler()
CXFA_FFDoc * GetDoc() const
CXFA_Node * GetRootSubform()
bool SavePackage(CXFA_Node *pNode, const RetainPtr< IFX_SeekableStream > &pFile)
CXFA_Document * GetXFADoc() const
Definition cxfa_ffdoc.h:167
XFA_EventError ProcessEvent(CXFA_Node *pNode, CXFA_EventParam *pParam)
CXFA_ReadyNodeIterator(CXFA_Node *pTravelRoot)
static WideString FromUTF16LE(pdfium::span< const uint8_t > data)
void CPDFXFA_ModuleInit()
void CPDFXFA_ModuleDestroy()
@ XFA_EVENT_PostSave
@ XFA_EVENT_PreSave
CXFA_Node * ToNode(CXFA_Object *pObj)
#define JSPLATFORM_ALERT_BUTTON_DEFAULT
#define JSPLATFORM_ALERT_ICON_ASTERISK
#define JSPLATFORM_ALERT_BUTTON_OKCANCEL
#define JSPLATFORM_ALERT_ICON_ERROR
#define JSPLATFORM_ALERT_BUTTON_YESNO
#define JSPLATFORM_ALERT_ICON_DEFAULT
#define JSPLATFORM_ALERT_ICON_WARNING
#define JSPLATFORM_ALERT_BUTTON_OK
#define JSPLATFORM_ALERT_ICON_QUESTION
#define JSPLATFORM_ALERT_ICON_STATUS
#define JSPLATFORM_ALERT_BUTTON_YESNOCANCEL
FormType
Definition fxfa.h:38
@ kXFAForeground
XFA_HashCode
Definition fxfa_basic.h:12
@ XFA_HASHCODE_Datasets
Definition fxfa_basic.h:19
@ XFA_HASHCODE_Form
Definition fxfa_basic.h:22
Definition heap.h:12