7#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
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"
44#ifdef PDF_ENABLE_XFA_BMP
45#include "core/fxcodec/bmp/bmp_progressive_decoder.h"
48#ifdef PDF_ENABLE_XFA_GIF
49#include "core/fxcodec/gif/gif_progressive_decoder.h"
54bool IsValidAlertButton(
int type) {
61bool IsValidAlertIcon(
int type) {
69RetainPtr<CPDF_SeekableMultiStream> CreateXFAMultiStream(
75 RetainPtr<
const CPDF_Dictionary> pAcroForm = pRoot->GetDictFor(
"AcroForm");
80 pAcroForm->GetDirectObjectFor(
"XFA");
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);
90 xfa_streams.push_back(
std::move(pStream));
92 }
else if (pElementXFA->IsStream()) {
93 xfa_streams.push_back(ToStream(pElementXFA));
95 if (xfa_streams.empty())
98 return pdfium::MakeRetain<CPDF_SeekableMultiStream>(
std::move(xfa_streams));
106#ifdef PDF_ENABLE_XFA_BMP
107 fxcodec::BmpProgressiveDecoder::InitializeGlobals();
109#ifdef PDF_ENABLE_XFA_GIF
110 fxcodec::GifProgressiveDecoder::InitializeGlobals();
117#ifdef PDF_ENABLE_XFA_GIF
118 fxcodec::GifProgressiveDecoder::DestroyGlobals();
120#ifdef PDF_ENABLE_XFA_BMP
121 fxcodec::BmpProgressiveDecoder::DestroyGlobals();
135 m_pXFAApp = cppgc::MakeGarbageCollected<CXFA_FFApp>(
136 m_pGCHeap->GetAllocationHandle(),
this);
143 m_pFormFillEnv->ClearAllFocusedAnnots();
147 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
151 if (m_pXFADoc && m_pXFADoc->GetXFADoc()) {
152 m_pXFADoc->GetXFADoc()->ClearLayoutData();
153 m_pXFADocView.Clear();
156 FXGC_ForceGarbageCollection(m_pGCHeap.get());
158 m_pFormFillEnv.Reset(pFormFillEnv);
163 m_XFAPageList.clear();
165 CJS_Runtime* actual_runtime = GetCJSRuntime();
166 if (!actual_runtime) {
167 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
171 auto stream = CreateXFAMultiStream(m_pPDFDoc);
173 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
177 CFX_XMLParser parser(stream);
178 m_pXML = parser.Parse();
180 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
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,
189 if (!m_pXFADoc->OpenDoc(m_pXML.get())) {
190 FXSYS_SetLastError(FPDF_ERR_XFALOAD);
194 if (!m_pXFAApp->LoadFWLTheme(m_pXFADoc)) {
195 FXSYS_SetLastError(FPDF_ERR_XFALAYOUT);
199 m_pXFADoc->GetXFADoc()->InitScriptContext(actual_runtime);
200 if (m_pXFADoc->GetFormType() == FormType::kXFAFull)
205 AutoNuller<cppgc::Persistent<CXFA_FFDocView>> view_nuller(&m_pXFADocView);
206 m_pXFADocView = m_pXFADoc->CreateDocView();
208 if (m_pXFADocView->StartLayout() < 0) {
209 m_pXFADoc->GetXFADoc()->ClearLayoutData();
210 FXGC_ForceGarbageCollection(m_pGCHeap.get());
211 FXSYS_SetLastError(FPDF_ERR_XFALAYOUT);
215 m_pXFADocView->DoLayout();
216 m_pXFADocView->StopLayout();
218 view_nuller.AbandonNullification();
219 doc_nuller.AbandonNullification();
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;
239 if (fxcrt::IndexInBounds(m_XFAPageList, page_index)) {
240 if (m_XFAPageList[page_index])
241 return m_XFAPageList[page_index];
244 m_XFAPageList.resize(m_nPageCount);
247 auto pPage = pdfium::MakeRetain<CPDFXFA_Page>(
GetPDFDoc(), page_index);
248 if (!pPage->LoadPage())
251 if (fxcrt::IndexInBounds(m_XFAPageList, page_index))
252 m_XFAPageList[page_index] = pPage;
258 if (!fxcrt::IndexInBounds(m_XFAPageList, page_index))
261 return m_XFAPageList[page_index];
265 CXFA_FFPageView* pPage)
const {
275 for (
auto& pTempPage : m_XFAPageList) {
276 if (pTempPage && pTempPage->GetXFAPageView() == pPage)
286 m_pPDFDoc->DeletePage(page_index);
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);
313 m_pFormFillEnv->ClearChangeMark();
316CJS_Runtime* CPDFXFA_Context::GetCJSRuntime()
const {
320 return m_pFormFillEnv->GetIJSRuntime()->AsCJSRuntime();
328 return m_pFormFillEnv ? m_pFormFillEnv->FFI_GetAppName() : WideString();
332 return m_pFormFillEnv ? m_pFormFillEnv->GetLanguage() : WideString();
336 return m_pFormFillEnv ? m_pFormFillEnv->GetPlatform() : WideString();
339void CPDFXFA_Context::
Beep(uint32_t dwType) {
341 m_pFormFillEnv->JS_appBeep(dwType);
344int32_t CPDFXFA_Context::
MsgBox(
const WideString& wsMessage,
345 const WideString& wsTitle,
347 uint32_t dwButtonType) {
348 if (!m_pFormFillEnv || m_nLoadStatus != LoadStatus::kLoaded)
353 int iButtonType = IsValidAlertButton(dwButtonType)
356 return m_pFormFillEnv->JS_appAlert(wsMessage, wsTitle, iButtonType, iconType);
359WideString CPDFXFA_Context::
Response(
const WideString& wsQuestion,
360 const WideString& wsTitle,
361 const WideString& wsDefaultAnswer,
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)
375 buffer_span = buffer_span.first(std::min<size_t>(kMaxBytes, byte_length));
380 const WideString& wsURL) {
381 return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL) :
nullptr;
385 const WideString& wsData,
386 const WideString& wsContentType,
387 const WideString& wsEncode,
388 const WideString& wsHeader,
389 WideString& wsResponse) {
393 wsResponse = m_pFormFillEnv->PostRequestURL(wsURL, wsData, wsContentType,
399 const WideString& wsData,
400 const WideString& wsEncode) {
401 return m_pFormFillEnv &&
402 m_pFormFillEnv->PutRequestURL(wsURL, wsData, wsEncode);
406 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() :
nullptr;
410 return m_pGCHeap.get();
454 std::vector<RetainPtr<IFX_SeekableStream>>* fileList) {
void BC_Library_Destroy()
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
void SendPostSaveToXFADoc()
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
CXFA_ReadyNodeIterator(CXFA_Node *pTravelRoot)
static void InitializeGlobals()
static void DestroyGlobals()
static WideString FromUTF16LE(pdfium::span< const uint8_t > data)
void CPDFXFA_ModuleInit()
void CPDFXFA_ModuleDestroy()
CXFA_Node * ToNode(CXFA_Object *pObj)