blob: 4c8591e2eb9f40ef133b97804c564bbf105bc7e3 [file] [log] [blame]
dsinclaira440bb32016-09-14 07:01:54 -07001// Copyright 2016 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/cpdfxfa_docenvironment.h"
dsinclaira440bb32016-09-14 07:01:54 -07008
tsepez0e606b52016-11-18 16:22:41 -08009#include <memory>
10
dsinclair488b7ad2016-10-04 11:55:50 -070011#include "core/fpdfapi/parser/cpdf_array.h"
12#include "core/fpdfapi/parser/cpdf_stream_acc.h"
13#include "core/fpdfapi/parser/cpdf_string.h"
tsepez833619b2016-12-07 09:21:17 -080014#include "core/fxcrt/cfx_retain_ptr.h"
dsinclair735606d2016-10-05 15:47:02 -070015#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/cpdfsdk_interform.h"
17#include "fpdfsdk/cpdfsdk_pageview.h"
dsinclair521b7502016-11-02 13:02:28 -070018#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070019#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
dsinclaira440bb32016-09-14 07:01:54 -070020#include "fpdfsdk/javascript/ijs_runtime.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040021#include "xfa/fxfa/cxfa_ffdocview.h"
22#include "xfa/fxfa/cxfa_ffwidget.h"
23#include "xfa/fxfa/cxfa_ffwidgethandler.h"
24#include "xfa/fxfa/cxfa_widgetacciterator.h"
dsinclaira440bb32016-09-14 07:01:54 -070025
26#define IDS_XFA_Validate_Input \
27 "At least one required field was empty. Please fill in the required " \
28 "fields\r\n(highlighted) before continuing."
29
30// submit
31#define FXFA_CONFIG 0x00000001
32#define FXFA_TEMPLATE 0x00000010
33#define FXFA_LOCALESET 0x00000100
34#define FXFA_DATASETS 0x00001000
35#define FXFA_XMPMETA 0x00010000
36#define FXFA_XFDF 0x00100000
37#define FXFA_FORM 0x01000000
38#define FXFA_PDF 0x10000000
39#define FXFA_XFA_ALL 0x01111111
40
dsinclair521b7502016-11-02 13:02:28 -070041CPDFXFA_DocEnvironment::CPDFXFA_DocEnvironment(CPDFXFA_Context* pContext)
Tom Sepezd6ae2af2017-02-16 11:49:55 -080042 : m_pContext(pContext), m_pJSEventContext(nullptr) {
dsinclair521b7502016-11-02 13:02:28 -070043 ASSERT(m_pContext);
dsinclaira440bb32016-09-14 07:01:54 -070044}
45
46CPDFXFA_DocEnvironment::~CPDFXFA_DocEnvironment() {
Tom Sepezd6ae2af2017-02-16 11:49:55 -080047 if (m_pJSEventContext && m_pContext->GetFormFillEnv()) {
48 m_pContext->GetFormFillEnv()->GetJSRuntime()->ReleaseEventContext(
49 m_pJSEventContext);
50 }
dsinclaira440bb32016-09-14 07:01:54 -070051}
52
53void CPDFXFA_DocEnvironment::SetChangeMark(CXFA_FFDoc* hDoc) {
dsinclair521b7502016-11-02 13:02:28 -070054 if (hDoc == m_pContext->GetXFADoc() && m_pContext->GetFormFillEnv())
55 m_pContext->GetFormFillEnv()->SetChangeMark();
dsinclaira440bb32016-09-14 07:01:54 -070056}
57
58void CPDFXFA_DocEnvironment::InvalidateRect(CXFA_FFPageView* pPageView,
Dan Sinclaire3978d02017-03-23 09:12:10 -040059 const CFX_RectF& rt) {
dsinclair521b7502016-11-02 13:02:28 -070060 if (!m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
dsinclaira440bb32016-09-14 07:01:54 -070061 return;
62
Dan Sinclaircdba7472017-03-23 09:17:10 -040063 if (m_pContext->GetDocType() != XFA_DocType::Dynamic)
dsinclaira440bb32016-09-14 07:01:54 -070064 return;
65
dsinclair521b7502016-11-02 13:02:28 -070066 CPDFXFA_Page* pPage = m_pContext->GetXFAPage(pPageView);
dsinclaira440bb32016-09-14 07:01:54 -070067 if (!pPage)
68 return;
69
dsinclair521b7502016-11-02 13:02:28 -070070 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -070071 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -070072 return;
73
Dan Sinclair6eec1c42017-02-21 17:20:43 -050074 pFormFillEnv->Invalidate(static_cast<FPDF_PAGE>(pPage),
75 CFX_FloatRect::FromCFXRectF(rt).ToFxRect());
dsinclaira440bb32016-09-14 07:01:54 -070076}
77
78void CPDFXFA_DocEnvironment::DisplayCaret(CXFA_FFWidget* hWidget,
tsepez4cf55152016-11-02 14:37:54 -070079 bool bVisible,
dsinclaira440bb32016-09-14 07:01:54 -070080 const CFX_RectF* pRtAnchor) {
dsinclair521b7502016-11-02 13:02:28 -070081 if (!hWidget || !pRtAnchor || !m_pContext->GetXFADoc() ||
82 !m_pContext->GetFormFillEnv() || !m_pContext->GetXFADocView())
dsinclaira440bb32016-09-14 07:01:54 -070083 return;
84
Dan Sinclaircdba7472017-03-23 09:17:10 -040085 if (m_pContext->GetDocType() != XFA_DocType::Dynamic)
dsinclaira440bb32016-09-14 07:01:54 -070086 return;
87
88 CXFA_FFWidgetHandler* pWidgetHandler =
dsinclair521b7502016-11-02 13:02:28 -070089 m_pContext->GetXFADocView()->GetWidgetHandler();
dsinclaira440bb32016-09-14 07:01:54 -070090 if (!pWidgetHandler)
91 return;
92
93 CXFA_FFPageView* pPageView = hWidget->GetPageView();
94 if (!pPageView)
95 return;
96
dsinclair521b7502016-11-02 13:02:28 -070097 CPDFXFA_Page* pPage = m_pContext->GetXFAPage(pPageView);
dsinclaira440bb32016-09-14 07:01:54 -070098 if (!pPage)
99 return;
100
dsinclair521b7502016-11-02 13:02:28 -0700101 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700102 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -0700103 return;
104
105 CFX_FloatRect rcCaret = CFX_FloatRect::FromCFXRectF(*pRtAnchor);
dsinclair655fcca2016-10-11 13:53:37 -0700106 pFormFillEnv->DisplayCaret((FPDF_PAGE)pPage, bVisible, rcCaret.left,
107 rcCaret.top, rcCaret.right, rcCaret.bottom);
dsinclaira440bb32016-09-14 07:01:54 -0700108}
109
tsepez4cf55152016-11-02 14:37:54 -0700110bool CPDFXFA_DocEnvironment::GetPopupPos(CXFA_FFWidget* hWidget,
Dan Sinclair05df0752017-03-14 14:43:42 -0400111 float fMinPopup,
112 float fMaxPopup,
tsepez4cf55152016-11-02 14:37:54 -0700113 const CFX_RectF& rtAnchor,
114 CFX_RectF& rtPopup) {
dsinclaira440bb32016-09-14 07:01:54 -0700115 if (!hWidget)
tsepez4cf55152016-11-02 14:37:54 -0700116 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700117
118 CXFA_FFPageView* pXFAPageView = hWidget->GetPageView();
119 if (!pXFAPageView)
tsepez4cf55152016-11-02 14:37:54 -0700120 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700121
dsinclair521b7502016-11-02 13:02:28 -0700122 CPDFXFA_Page* pPage = m_pContext->GetXFAPage(pXFAPageView);
dsinclaira440bb32016-09-14 07:01:54 -0700123 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -0700124 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700125
126 CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc();
127 int nRotate = pWidgetAcc->GetRotate();
dsinclair521b7502016-11-02 13:02:28 -0700128 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700129 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700130 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700131
132 FS_RECTF pageViewRect = {0.0f, 0.0f, 0.0f, 0.0f};
dsinclair655fcca2016-10-11 13:53:37 -0700133 pFormFillEnv->GetPageViewRect(pPage, pageViewRect);
dsinclaira440bb32016-09-14 07:01:54 -0700134
135 int t1;
136 int t2;
137 CFX_FloatRect rcAnchor = CFX_FloatRect::FromCFXRectF(rtAnchor);
138 switch (nRotate) {
139 case 90: {
140 t1 = (int)(pageViewRect.right - rcAnchor.right);
141 t2 = (int)(rcAnchor.left - pageViewRect.left);
142 if (rcAnchor.bottom < pageViewRect.bottom)
143 rtPopup.left += rcAnchor.bottom - pageViewRect.bottom;
144 break;
145 }
146 case 180: {
147 t2 = (int)(pageViewRect.top - rcAnchor.top);
148 t1 = (int)(rcAnchor.bottom - pageViewRect.bottom);
149 if (rcAnchor.left < pageViewRect.left)
150 rtPopup.left += rcAnchor.left - pageViewRect.left;
151 break;
152 }
153 case 270: {
154 t1 = (int)(rcAnchor.left - pageViewRect.left);
155 t2 = (int)(pageViewRect.right - rcAnchor.right);
156 if (rcAnchor.top > pageViewRect.top)
157 rtPopup.left -= rcAnchor.top - pageViewRect.top;
158 break;
159 }
160 case 0:
161 default: {
162 t1 = (int)(pageViewRect.top - rcAnchor.top);
163 t2 = (int)(rcAnchor.bottom - pageViewRect.bottom);
164 if (rcAnchor.right > pageViewRect.right)
165 rtPopup.left -= rcAnchor.right - pageViewRect.right;
166 break;
167 }
168 }
169
170 int t;
171 uint32_t dwPos;
172 if (t1 <= 0 && t2 <= 0)
tsepez4cf55152016-11-02 14:37:54 -0700173 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700174 if (t1 <= 0) {
175 t = t2;
176 dwPos = 1;
177 } else if (t2 <= 0) {
178 t = t1;
179 dwPos = 0;
180 } else if (t1 > t2) {
181 t = t1;
182 dwPos = 0;
183 } else {
184 t = t2;
185 dwPos = 1;
186 }
187
Dan Sinclair05df0752017-03-14 14:43:42 -0400188 float fPopupHeight;
dsinclaira440bb32016-09-14 07:01:54 -0700189 if (t < fMinPopup)
190 fPopupHeight = fMinPopup;
191 else if (t > fMaxPopup)
192 fPopupHeight = fMaxPopup;
193 else
Dan Sinclair05df0752017-03-14 14:43:42 -0400194 fPopupHeight = static_cast<float>(t);
dsinclaira440bb32016-09-14 07:01:54 -0700195
196 switch (nRotate) {
197 case 0:
198 case 180: {
199 if (dwPos == 0) {
200 rtPopup.top = rtAnchor.height;
201 rtPopup.height = fPopupHeight;
202 } else {
203 rtPopup.top = -fPopupHeight;
204 rtPopup.height = fPopupHeight;
205 }
206 break;
207 }
208 case 90:
209 case 270: {
210 if (dwPos == 0) {
211 rtPopup.top = rtAnchor.width;
212 rtPopup.height = fPopupHeight;
213 } else {
214 rtPopup.top = -fPopupHeight;
215 rtPopup.height = fPopupHeight;
216 }
217 break;
218 }
219 default:
220 break;
221 }
222
tsepez4cf55152016-11-02 14:37:54 -0700223 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700224}
225
tsepez4cf55152016-11-02 14:37:54 -0700226bool CPDFXFA_DocEnvironment::PopupMenu(CXFA_FFWidget* hWidget,
227 CFX_PointF ptPopup) {
dsinclaira440bb32016-09-14 07:01:54 -0700228 if (!hWidget)
tsepez4cf55152016-11-02 14:37:54 -0700229 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700230
231 CXFA_FFPageView* pXFAPageView = hWidget->GetPageView();
232 if (!pXFAPageView)
tsepez4cf55152016-11-02 14:37:54 -0700233 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700234
dsinclair521b7502016-11-02 13:02:28 -0700235 CPDFXFA_Page* pPage = m_pContext->GetXFAPage(pXFAPageView);
dsinclaira440bb32016-09-14 07:01:54 -0700236 if (!pPage)
tsepez4cf55152016-11-02 14:37:54 -0700237 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700238
dsinclair521b7502016-11-02 13:02:28 -0700239 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700240 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700241 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700242
243 int menuFlag = 0;
244 if (hWidget->CanUndo())
245 menuFlag |= FXFA_MENU_UNDO;
246 if (hWidget->CanRedo())
247 menuFlag |= FXFA_MENU_REDO;
248 if (hWidget->CanPaste())
249 menuFlag |= FXFA_MENU_PASTE;
250 if (hWidget->CanCopy())
251 menuFlag |= FXFA_MENU_COPY;
252 if (hWidget->CanCut())
253 menuFlag |= FXFA_MENU_CUT;
254 if (hWidget->CanSelectAll())
255 menuFlag |= FXFA_MENU_SELECTALL;
256
dsinclair655fcca2016-10-11 13:53:37 -0700257 return pFormFillEnv->PopupMenu(pPage, hWidget, menuFlag, ptPopup);
dsinclaira440bb32016-09-14 07:01:54 -0700258}
259
260void CPDFXFA_DocEnvironment::PageViewEvent(CXFA_FFPageView* pPageView,
261 uint32_t dwFlags) {
dsinclair521b7502016-11-02 13:02:28 -0700262 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700263 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -0700264 return;
265
dsinclair521b7502016-11-02 13:02:28 -0700266 if (m_pContext->GetLoadStatus() == FXFA_LOADSTATUS_LOADING ||
267 m_pContext->GetLoadStatus() == FXFA_LOADSTATUS_CLOSING ||
dsinclaira440bb32016-09-14 07:01:54 -0700268 XFA_PAGEVIEWEVENT_StopLayout != dwFlags)
269 return;
270
dsinclair521b7502016-11-02 13:02:28 -0700271 int nNewCount = m_pContext->GetPageCount();
272 if (nNewCount == m_pContext->GetOriginalPageCount())
dsinclaira440bb32016-09-14 07:01:54 -0700273 return;
274
dsinclair521b7502016-11-02 13:02:28 -0700275 CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView();
dsinclaira440bb32016-09-14 07:01:54 -0700276 if (!pXFADocView)
277 return;
278
dsinclair521b7502016-11-02 13:02:28 -0700279 for (int iPageIter = 0; iPageIter < m_pContext->GetOriginalPageCount();
dsinclaira440bb32016-09-14 07:01:54 -0700280 iPageIter++) {
Tom Sepez5535ac72017-01-27 12:11:38 -0800281 CPDFXFA_Page* pPage = (*m_pContext->GetXFAPageList())[iPageIter];
dsinclaira440bb32016-09-14 07:01:54 -0700282 if (!pPage)
283 continue;
284
dsinclair521b7502016-11-02 13:02:28 -0700285 m_pContext->GetFormFillEnv()->RemovePageView(pPage);
dsinclaira440bb32016-09-14 07:01:54 -0700286 pPage->SetXFAPageView(pXFADocView->GetPageView(iPageIter));
287 }
288
dsinclair521b7502016-11-02 13:02:28 -0700289 int flag = (nNewCount < m_pContext->GetOriginalPageCount())
dsinclaira440bb32016-09-14 07:01:54 -0700290 ? FXFA_PAGEVIEWEVENT_POSTREMOVED
291 : FXFA_PAGEVIEWEVENT_POSTADDED;
Dan Sinclair669a4182017-04-03 14:51:45 -0400292 int count = abs(nNewCount - m_pContext->GetOriginalPageCount());
dsinclair521b7502016-11-02 13:02:28 -0700293 m_pContext->SetOriginalPageCount(nNewCount);
dsinclair655fcca2016-10-11 13:53:37 -0700294 pFormFillEnv->PageEvent(count, flag);
dsinclaira440bb32016-09-14 07:01:54 -0700295}
296
297void CPDFXFA_DocEnvironment::WidgetPostAdd(CXFA_FFWidget* hWidget,
298 CXFA_WidgetAcc* pWidgetData) {
Dan Sinclaircdba7472017-03-23 09:17:10 -0400299 if (m_pContext->GetDocType() != XFA_DocType::Dynamic || !hWidget)
dsinclaira440bb32016-09-14 07:01:54 -0700300 return;
301
302 CXFA_FFPageView* pPageView = hWidget->GetPageView();
303 if (!pPageView)
304 return;
305
dsinclair521b7502016-11-02 13:02:28 -0700306 CPDFXFA_Page* pXFAPage = m_pContext->GetXFAPage(pPageView);
dsinclaira440bb32016-09-14 07:01:54 -0700307 if (!pXFAPage)
308 return;
309
dsinclair521b7502016-11-02 13:02:28 -0700310 m_pContext->GetFormFillEnv()->GetPageView(pXFAPage, true)->AddAnnot(hWidget);
dsinclaira440bb32016-09-14 07:01:54 -0700311}
312
313void CPDFXFA_DocEnvironment::WidgetPreRemove(CXFA_FFWidget* hWidget,
314 CXFA_WidgetAcc* pWidgetData) {
Dan Sinclaircdba7472017-03-23 09:17:10 -0400315 if (m_pContext->GetDocType() != XFA_DocType::Dynamic || !hWidget)
dsinclaira440bb32016-09-14 07:01:54 -0700316 return;
317
318 CXFA_FFPageView* pPageView = hWidget->GetPageView();
319 if (!pPageView)
320 return;
321
dsinclair521b7502016-11-02 13:02:28 -0700322 CPDFXFA_Page* pXFAPage = m_pContext->GetXFAPage(pPageView);
dsinclaira440bb32016-09-14 07:01:54 -0700323 if (!pXFAPage)
324 return;
325
326 CPDFSDK_PageView* pSdkPageView =
dsinclair521b7502016-11-02 13:02:28 -0700327 m_pContext->GetFormFillEnv()->GetPageView(pXFAPage, true);
dsinclaira440bb32016-09-14 07:01:54 -0700328 if (CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget))
329 pSdkPageView->DeleteAnnot(pAnnot);
330}
331
332int32_t CPDFXFA_DocEnvironment::CountPages(CXFA_FFDoc* hDoc) {
dsinclair521b7502016-11-02 13:02:28 -0700333 if (hDoc == m_pContext->GetXFADoc() && m_pContext->GetFormFillEnv())
334 return m_pContext->GetPageCount();
dsinclaira440bb32016-09-14 07:01:54 -0700335 return 0;
336}
337
338int32_t CPDFXFA_DocEnvironment::GetCurrentPage(CXFA_FFDoc* hDoc) {
dsinclair521b7502016-11-02 13:02:28 -0700339 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
dsinclaira440bb32016-09-14 07:01:54 -0700340 return -1;
Dan Sinclaircdba7472017-03-23 09:17:10 -0400341 if (m_pContext->GetDocType() != XFA_DocType::Dynamic)
dsinclaira440bb32016-09-14 07:01:54 -0700342 return -1;
343
dsinclair521b7502016-11-02 13:02:28 -0700344 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700345 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -0700346 return -1;
347
dsinclair655fcca2016-10-11 13:53:37 -0700348 return pFormFillEnv->GetCurrentPageIndex(this);
dsinclaira440bb32016-09-14 07:01:54 -0700349}
350
351void CPDFXFA_DocEnvironment::SetCurrentPage(CXFA_FFDoc* hDoc,
352 int32_t iCurPage) {
dsinclair521b7502016-11-02 13:02:28 -0700353 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv() ||
Dan Sinclaircdba7472017-03-23 09:17:10 -0400354 m_pContext->GetDocType() != XFA_DocType::Dynamic || iCurPage < 0 ||
dsinclair521b7502016-11-02 13:02:28 -0700355 iCurPage >= m_pContext->GetFormFillEnv()->GetPageCount()) {
dsinclaira440bb32016-09-14 07:01:54 -0700356 return;
357 }
358
dsinclair521b7502016-11-02 13:02:28 -0700359 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700360 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -0700361 return;
dsinclair655fcca2016-10-11 13:53:37 -0700362 pFormFillEnv->SetCurrentPage(this, iCurPage);
dsinclaira440bb32016-09-14 07:01:54 -0700363}
364
tsepez4cf55152016-11-02 14:37:54 -0700365bool CPDFXFA_DocEnvironment::IsCalculationsEnabled(CXFA_FFDoc* hDoc) {
dsinclair521b7502016-11-02 13:02:28 -0700366 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
tsepez4cf55152016-11-02 14:37:54 -0700367 return false;
dsinclair521b7502016-11-02 13:02:28 -0700368 if (m_pContext->GetFormFillEnv()->GetInterForm()) {
369 return m_pContext->GetFormFillEnv()
dsinclair655fcca2016-10-11 13:53:37 -0700370 ->GetInterForm()
371 ->IsXfaCalculateEnabled();
dsinclair7cbe68e2016-10-12 11:56:23 -0700372 }
tsepez4cf55152016-11-02 14:37:54 -0700373 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700374}
375
376void CPDFXFA_DocEnvironment::SetCalculationsEnabled(CXFA_FFDoc* hDoc,
tsepez4cf55152016-11-02 14:37:54 -0700377 bool bEnabled) {
dsinclair521b7502016-11-02 13:02:28 -0700378 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
dsinclaira440bb32016-09-14 07:01:54 -0700379 return;
dsinclair521b7502016-11-02 13:02:28 -0700380 if (m_pContext->GetFormFillEnv()->GetInterForm()) {
381 m_pContext->GetFormFillEnv()->GetInterForm()->XfaEnableCalculate(bEnabled);
dsinclair7cbe68e2016-10-12 11:56:23 -0700382 }
dsinclaira440bb32016-09-14 07:01:54 -0700383}
384
385void CPDFXFA_DocEnvironment::GetTitle(CXFA_FFDoc* hDoc,
386 CFX_WideString& wsTitle) {
dsinclair521b7502016-11-02 13:02:28 -0700387 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetPDFDoc())
dsinclaira440bb32016-09-14 07:01:54 -0700388 return;
389
dsinclair521b7502016-11-02 13:02:28 -0700390 CPDF_Dictionary* pInfoDict = m_pContext->GetPDFDoc()->GetInfo();
dsinclaira440bb32016-09-14 07:01:54 -0700391 if (!pInfoDict)
392 return;
393
dsinclair38fd8442016-09-15 10:15:32 -0700394 CFX_ByteString csTitle = pInfoDict->GetStringFor("Title");
dsinclaira440bb32016-09-14 07:01:54 -0700395 wsTitle = wsTitle.FromLocal(csTitle.GetBuffer(csTitle.GetLength()));
396 csTitle.ReleaseBuffer(csTitle.GetLength());
397}
398
399void CPDFXFA_DocEnvironment::SetTitle(CXFA_FFDoc* hDoc,
400 const CFX_WideString& wsTitle) {
dsinclair521b7502016-11-02 13:02:28 -0700401 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetPDFDoc())
dsinclaira440bb32016-09-14 07:01:54 -0700402 return;
403
dsinclair521b7502016-11-02 13:02:28 -0700404 if (CPDF_Dictionary* pInfoDict = m_pContext->GetPDFDoc()->GetInfo())
tsepez0e606b52016-11-18 16:22:41 -0800405 pInfoDict->SetNewFor<CPDF_String>("Title", wsTitle);
dsinclaira440bb32016-09-14 07:01:54 -0700406}
407
408void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc,
409 const CFX_WideString& wsFilePath,
tsepez4cf55152016-11-02 14:37:54 -0700410 bool bXDP) {
dsinclair521b7502016-11-02 13:02:28 -0700411 if (hDoc != m_pContext->GetXFADoc())
dsinclaira440bb32016-09-14 07:01:54 -0700412 return;
413
Dan Sinclaircdba7472017-03-23 09:17:10 -0400414 if (m_pContext->GetDocType() != XFA_DocType::Dynamic &&
415 m_pContext->GetDocType() != XFA_DocType::Static) {
dsinclaira440bb32016-09-14 07:01:54 -0700416 return;
dsinclair7cbe68e2016-10-12 11:56:23 -0700417 }
dsinclaira440bb32016-09-14 07:01:54 -0700418
dsinclair521b7502016-11-02 13:02:28 -0700419 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700420 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -0700421 return;
422
423 int fileType = bXDP ? FXFA_SAVEAS_XDP : FXFA_SAVEAS_XML;
424 CFX_ByteString bs = wsFilePath.UTF16LE_Encode();
425 if (wsFilePath.IsEmpty()) {
dsinclair655fcca2016-10-11 13:53:37 -0700426 if (!pFormFillEnv->GetFormFillInfo() ||
dsinclair7cbe68e2016-10-12 11:56:23 -0700427 !pFormFillEnv->GetFormFillInfo()->m_pJsPlatform) {
dsinclaira440bb32016-09-14 07:01:54 -0700428 return;
dsinclair7cbe68e2016-10-12 11:56:23 -0700429 }
dsinclaira440bb32016-09-14 07:01:54 -0700430
dsinclair655fcca2016-10-11 13:53:37 -0700431 CFX_WideString filepath = pFormFillEnv->JS_fieldBrowse();
dsinclaira440bb32016-09-14 07:01:54 -0700432 bs = filepath.UTF16LE_Encode();
433 }
434 int len = bs.GetLength();
435 FPDF_FILEHANDLER* pFileHandler =
dsinclair655fcca2016-10-11 13:53:37 -0700436 pFormFillEnv->OpenFile(bXDP ? FXFA_SAVEAS_XDP : FXFA_SAVEAS_XML,
437 (FPDF_WIDESTRING)bs.GetBuffer(len), "wb");
dsinclaira440bb32016-09-14 07:01:54 -0700438 bs.ReleaseBuffer(len);
439 if (!pFileHandler)
440 return;
441
tsepez833619b2016-12-07 09:21:17 -0800442 CFX_RetainPtr<IFX_SeekableStream> fileWrite =
443 MakeSeekableStream(pFileHandler);
dsinclaira440bb32016-09-14 07:01:54 -0700444 CFX_ByteString content;
445 if (fileType == FXFA_SAVEAS_XML) {
446 content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
tsepezfa89a202016-12-02 09:48:30 -0800447 fileWrite->WriteBlock(content.c_str(), fileWrite->GetSize(),
448 content.GetLength());
tsepez833619b2016-12-07 09:21:17 -0800449 m_pContext->GetXFADocView()->GetDoc()->SavePackage(XFA_HASHCODE_Data,
450 fileWrite, nullptr);
dsinclaira440bb32016-09-14 07:01:54 -0700451 } else if (fileType == FXFA_SAVEAS_XDP) {
dsinclair521b7502016-11-02 13:02:28 -0700452 if (!m_pContext->GetPDFDoc())
dsinclaira440bb32016-09-14 07:01:54 -0700453 return;
454
dsinclair521b7502016-11-02 13:02:28 -0700455 CPDF_Dictionary* pRoot = m_pContext->GetPDFDoc()->GetRoot();
dsinclaira440bb32016-09-14 07:01:54 -0700456 if (!pRoot)
457 return;
458
dsinclair38fd8442016-09-15 10:15:32 -0700459 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
dsinclaira440bb32016-09-14 07:01:54 -0700460 if (!pAcroForm)
461 return;
462
dsinclair38fd8442016-09-15 10:15:32 -0700463 CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA"));
dsinclaira440bb32016-09-14 07:01:54 -0700464 if (!pArray)
465 return;
466
467 int size = pArray->GetCount();
468 for (int i = 1; i < size; i += 2) {
469 CPDF_Object* pPDFObj = pArray->GetObjectAt(i);
470 CPDF_Object* pPrePDFObj = pArray->GetObjectAt(i - 1);
471 if (!pPrePDFObj->IsString())
472 continue;
473 if (!pPDFObj->IsReference())
474 continue;
475
476 CPDF_Stream* pStream = ToStream(pPDFObj->GetDirect());
477 if (!pStream)
478 continue;
479 if (pPrePDFObj->GetString() == "form") {
tsepez833619b2016-12-07 09:21:17 -0800480 m_pContext->GetXFADocView()->GetDoc()->SavePackage(XFA_HASHCODE_Form,
481 fileWrite, nullptr);
dsinclaira440bb32016-09-14 07:01:54 -0700482 continue;
483 }
484 if (pPrePDFObj->GetString() == "datasets") {
dsinclair521b7502016-11-02 13:02:28 -0700485 m_pContext->GetXFADocView()->GetDoc()->SavePackage(
tsepez833619b2016-12-07 09:21:17 -0800486 XFA_HASHCODE_Datasets, fileWrite, nullptr);
dsinclaira440bb32016-09-14 07:01:54 -0700487 continue;
488 }
489 if (i == size - 1) {
490 CFX_WideString wPath = CFX_WideString::FromUTF16LE(
491 reinterpret_cast<const unsigned short*>(bs.c_str()),
492 bs.GetLength() / sizeof(unsigned short));
493 CFX_ByteString bPath = wPath.UTF8Encode();
494 const char* szFormat =
495 "\n<pdf href=\"%s\" xmlns=\"http://ns.adobe.com/xdp/pdf/\"/>";
496 content.Format(szFormat, bPath.c_str());
tsepezfa89a202016-12-02 09:48:30 -0800497 fileWrite->WriteBlock(content.c_str(), fileWrite->GetSize(),
498 content.GetLength());
dsinclaira440bb32016-09-14 07:01:54 -0700499 }
Dan Sinclair0bb13332017-03-30 16:12:02 -0400500 auto pAcc = pdfium::MakeUnique<CPDF_StreamAcc>();
dsinclaira440bb32016-09-14 07:01:54 -0700501 pAcc->LoadAllData(pStream);
tsepezfa89a202016-12-02 09:48:30 -0800502 fileWrite->WriteBlock(pAcc->GetData(), fileWrite->GetSize(),
503 pAcc->GetSize());
dsinclaira440bb32016-09-14 07:01:54 -0700504 }
505 }
tsepezfa89a202016-12-02 09:48:30 -0800506 fileWrite->Flush();
dsinclaira440bb32016-09-14 07:01:54 -0700507}
508
509void CPDFXFA_DocEnvironment::GotoURL(CXFA_FFDoc* hDoc,
dsinclair577ad2c2016-09-22 10:20:43 -0700510 const CFX_WideString& bsURL) {
dsinclair521b7502016-11-02 13:02:28 -0700511 if (hDoc != m_pContext->GetXFADoc())
dsinclaira440bb32016-09-14 07:01:54 -0700512 return;
513
Dan Sinclaircdba7472017-03-23 09:17:10 -0400514 if (m_pContext->GetDocType() != XFA_DocType::Dynamic)
dsinclaira440bb32016-09-14 07:01:54 -0700515 return;
516
dsinclair521b7502016-11-02 13:02:28 -0700517 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700518 if (!pFormFillEnv)
dsinclaira440bb32016-09-14 07:01:54 -0700519 return;
520
521 CFX_WideStringC str(bsURL.c_str());
dsinclair655fcca2016-10-11 13:53:37 -0700522 pFormFillEnv->GotoURL(this, str);
dsinclaira440bb32016-09-14 07:01:54 -0700523}
524
tsepez4cf55152016-11-02 14:37:54 -0700525bool CPDFXFA_DocEnvironment::IsValidationsEnabled(CXFA_FFDoc* hDoc) {
dsinclair521b7502016-11-02 13:02:28 -0700526 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
tsepez4cf55152016-11-02 14:37:54 -0700527 return false;
dsinclair521b7502016-11-02 13:02:28 -0700528 if (m_pContext->GetFormFillEnv()->GetInterForm()) {
529 return m_pContext->GetFormFillEnv()
dsinclair655fcca2016-10-11 13:53:37 -0700530 ->GetInterForm()
531 ->IsXfaValidationsEnabled();
dsinclair7cbe68e2016-10-12 11:56:23 -0700532 }
tsepez4cf55152016-11-02 14:37:54 -0700533 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700534}
535
536void CPDFXFA_DocEnvironment::SetValidationsEnabled(CXFA_FFDoc* hDoc,
tsepez4cf55152016-11-02 14:37:54 -0700537 bool bEnabled) {
dsinclair521b7502016-11-02 13:02:28 -0700538 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
dsinclaira440bb32016-09-14 07:01:54 -0700539 return;
dsinclair521b7502016-11-02 13:02:28 -0700540 if (m_pContext->GetFormFillEnv()->GetInterForm()) {
541 m_pContext->GetFormFillEnv()->GetInterForm()->XfaSetValidationsEnabled(
542 bEnabled);
dsinclair7cbe68e2016-10-12 11:56:23 -0700543 }
dsinclaira440bb32016-09-14 07:01:54 -0700544}
545
546void CPDFXFA_DocEnvironment::SetFocusWidget(CXFA_FFDoc* hDoc,
547 CXFA_FFWidget* hWidget) {
dsinclair521b7502016-11-02 13:02:28 -0700548 if (hDoc != m_pContext->GetXFADoc())
dsinclaira440bb32016-09-14 07:01:54 -0700549 return;
550
551 if (!hWidget) {
tsepezf8074ce2016-09-27 14:29:57 -0700552 CPDFSDK_Annot::ObservedPtr pNull;
dsinclair521b7502016-11-02 13:02:28 -0700553 m_pContext->GetFormFillEnv()->SetFocusAnnot(&pNull);
dsinclaira440bb32016-09-14 07:01:54 -0700554 return;
555 }
556
dsinclair521b7502016-11-02 13:02:28 -0700557 int pageViewCount = m_pContext->GetFormFillEnv()->GetPageViewCount();
dsinclaira440bb32016-09-14 07:01:54 -0700558 for (int i = 0; i < pageViewCount; i++) {
dsinclair521b7502016-11-02 13:02:28 -0700559 CPDFSDK_PageView* pPageView = m_pContext->GetFormFillEnv()->GetPageView(i);
dsinclaira440bb32016-09-14 07:01:54 -0700560 if (!pPageView)
561 continue;
562
tsepezf8074ce2016-09-27 14:29:57 -0700563 CPDFSDK_Annot::ObservedPtr pAnnot(pPageView->GetAnnotByXFAWidget(hWidget));
dsinclaira440bb32016-09-14 07:01:54 -0700564 if (pAnnot) {
dsinclair521b7502016-11-02 13:02:28 -0700565 m_pContext->GetFormFillEnv()->SetFocusAnnot(&pAnnot);
dsinclaira440bb32016-09-14 07:01:54 -0700566 break;
567 }
568 }
569}
570
571void CPDFXFA_DocEnvironment::Print(CXFA_FFDoc* hDoc,
572 int32_t nStartPage,
573 int32_t nEndPage,
574 uint32_t dwOptions) {
dsinclair521b7502016-11-02 13:02:28 -0700575 if (hDoc != m_pContext->GetXFADoc())
dsinclaira440bb32016-09-14 07:01:54 -0700576 return;
577
dsinclair521b7502016-11-02 13:02:28 -0700578 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700579 if (!pFormFillEnv || !pFormFillEnv->GetFormFillInfo() ||
580 !pFormFillEnv->GetFormFillInfo()->m_pJsPlatform ||
581 !pFormFillEnv->GetFormFillInfo()->m_pJsPlatform->Doc_print) {
dsinclaira440bb32016-09-14 07:01:54 -0700582 return;
583 }
584
dsinclair655fcca2016-10-11 13:53:37 -0700585 pFormFillEnv->GetFormFillInfo()->m_pJsPlatform->Doc_print(
586 pFormFillEnv->GetFormFillInfo()->m_pJsPlatform,
dsinclaira440bb32016-09-14 07:01:54 -0700587 dwOptions & XFA_PRINTOPT_ShowDialog, nStartPage, nEndPage,
588 dwOptions & XFA_PRINTOPT_CanCancel, dwOptions & XFA_PRINTOPT_ShrinkPage,
589 dwOptions & XFA_PRINTOPT_AsImage, dwOptions & XFA_PRINTOPT_ReverseOrder,
590 dwOptions & XFA_PRINTOPT_PrintAnnot);
591}
592
593FX_ARGB CPDFXFA_DocEnvironment::GetHighlightColor(CXFA_FFDoc* hDoc) {
dsinclair521b7502016-11-02 13:02:28 -0700594 if (hDoc != m_pContext->GetXFADoc() || !m_pContext->GetFormFillEnv())
dsinclaira440bb32016-09-14 07:01:54 -0700595 return 0;
596
dsinclair521b7502016-11-02 13:02:28 -0700597 CPDFSDK_InterForm* pInterForm = m_pContext->GetFormFillEnv()->GetInterForm();
dsinclaira440bb32016-09-14 07:01:54 -0700598 if (!pInterForm)
599 return 0;
600
601 return ArgbEncode(pInterForm->GetHighlightAlpha(),
602 pInterForm->GetHighlightColor(FPDF_FORMFIELD_XFA));
603}
604
tsepez4cf55152016-11-02 14:37:54 -0700605bool CPDFXFA_DocEnvironment::NotifySubmit(bool bPrevOrPost) {
dsinclaira440bb32016-09-14 07:01:54 -0700606 if (bPrevOrPost)
607 return OnBeforeNotifySubmit();
608
609 OnAfterNotifySubmit();
tsepez4cf55152016-11-02 14:37:54 -0700610 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700611}
612
tsepez4cf55152016-11-02 14:37:54 -0700613bool CPDFXFA_DocEnvironment::OnBeforeNotifySubmit() {
Dan Sinclaircdba7472017-03-23 09:17:10 -0400614 if (m_pContext->GetDocType() != XFA_DocType::Dynamic &&
615 m_pContext->GetDocType() != XFA_DocType::Static) {
tsepez4cf55152016-11-02 14:37:54 -0700616 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700617 }
dsinclaira440bb32016-09-14 07:01:54 -0700618
dsinclair521b7502016-11-02 13:02:28 -0700619 if (!m_pContext->GetXFADocView())
tsepez4cf55152016-11-02 14:37:54 -0700620 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700621
622 CXFA_FFWidgetHandler* pWidgetHandler =
dsinclair521b7502016-11-02 13:02:28 -0700623 m_pContext->GetXFADocView()->GetWidgetHandler();
dsinclaira440bb32016-09-14 07:01:54 -0700624 if (!pWidgetHandler)
tsepez4cf55152016-11-02 14:37:54 -0700625 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700626
627 std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator(
dsinclair521b7502016-11-02 13:02:28 -0700628 m_pContext->GetXFADocView()->CreateWidgetAccIterator());
dsinclaira440bb32016-09-14 07:01:54 -0700629 if (pWidgetAccIterator) {
630 CXFA_EventParam Param;
631 Param.m_eType = XFA_EVENT_PreSubmit;
632 while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext())
633 pWidgetHandler->ProcessEvent(pWidgetAcc, &Param);
634 }
635
636 pWidgetAccIterator.reset(
dsinclair521b7502016-11-02 13:02:28 -0700637 m_pContext->GetXFADocView()->CreateWidgetAccIterator());
dsinclaira440bb32016-09-14 07:01:54 -0700638 if (!pWidgetAccIterator)
tsepez4cf55152016-11-02 14:37:54 -0700639 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700640
641 CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();
642 pWidgetAcc = pWidgetAccIterator->MoveToNext();
643 while (pWidgetAcc) {
644 int fRet = pWidgetAcc->ProcessValidate(-1);
645 if (fRet == XFA_EVENTERROR_Error) {
dsinclair521b7502016-11-02 13:02:28 -0700646 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700647 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700648 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700649
650 CFX_WideString ws;
651 ws.FromLocal(IDS_XFA_Validate_Input);
652 CFX_ByteString bs = ws.UTF16LE_Encode();
653 int len = bs.GetLength();
dsinclair655fcca2016-10-11 13:53:37 -0700654 pFormFillEnv->Alert((FPDF_WIDESTRING)bs.GetBuffer(len),
655 (FPDF_WIDESTRING)L"", 0, 1);
dsinclaira440bb32016-09-14 07:01:54 -0700656 bs.ReleaseBuffer(len);
tsepez4cf55152016-11-02 14:37:54 -0700657 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700658 }
659 pWidgetAcc = pWidgetAccIterator->MoveToNext();
660 }
dsinclair521b7502016-11-02 13:02:28 -0700661 m_pContext->GetXFADocView()->UpdateDocView();
dsinclaira440bb32016-09-14 07:01:54 -0700662
tsepez4cf55152016-11-02 14:37:54 -0700663 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700664}
665
666void CPDFXFA_DocEnvironment::OnAfterNotifySubmit() {
Dan Sinclaircdba7472017-03-23 09:17:10 -0400667 if (m_pContext->GetDocType() != XFA_DocType::Dynamic &&
668 m_pContext->GetDocType() != XFA_DocType::Static)
dsinclaira440bb32016-09-14 07:01:54 -0700669 return;
670
dsinclair521b7502016-11-02 13:02:28 -0700671 if (!m_pContext->GetXFADocView())
dsinclaira440bb32016-09-14 07:01:54 -0700672 return;
673
674 CXFA_FFWidgetHandler* pWidgetHandler =
dsinclair521b7502016-11-02 13:02:28 -0700675 m_pContext->GetXFADocView()->GetWidgetHandler();
dsinclaira440bb32016-09-14 07:01:54 -0700676 if (!pWidgetHandler)
677 return;
678
679 std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator(
dsinclair521b7502016-11-02 13:02:28 -0700680 m_pContext->GetXFADocView()->CreateWidgetAccIterator());
dsinclaira440bb32016-09-14 07:01:54 -0700681 if (!pWidgetAccIterator)
682 return;
683
684 CXFA_EventParam Param;
685 Param.m_eType = XFA_EVENT_PostSubmit;
686 CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();
687 while (pWidgetAcc) {
688 pWidgetHandler->ProcessEvent(pWidgetAcc, &Param);
689 pWidgetAcc = pWidgetAccIterator->MoveToNext();
690 }
dsinclair521b7502016-11-02 13:02:28 -0700691 m_pContext->GetXFADocView()->UpdateDocView();
dsinclaira440bb32016-09-14 07:01:54 -0700692}
693
tsepez4cf55152016-11-02 14:37:54 -0700694bool CPDFXFA_DocEnvironment::SubmitData(CXFA_FFDoc* hDoc, CXFA_Submit submit) {
695 if (!NotifySubmit(true) || !m_pContext->GetXFADocView())
696 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700697
dsinclair521b7502016-11-02 13:02:28 -0700698 m_pContext->GetXFADocView()->UpdateDocView();
tsepez4cf55152016-11-02 14:37:54 -0700699 bool ret = SubmitDataInternal(hDoc, submit);
700 NotifySubmit(false);
dsinclaira440bb32016-09-14 07:01:54 -0700701 return ret;
702}
703
tsepez833619b2016-12-07 09:21:17 -0800704CFX_RetainPtr<IFX_SeekableReadStream> CPDFXFA_DocEnvironment::OpenLinkedFile(
dsinclaira440bb32016-09-14 07:01:54 -0700705 CXFA_FFDoc* hDoc,
706 const CFX_WideString& wsLink) {
dsinclair521b7502016-11-02 13:02:28 -0700707 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700708 if (!pFormFillEnv)
tsepez04c10092016-10-25 15:16:40 -0700709 return nullptr;
dsinclaira440bb32016-09-14 07:01:54 -0700710
711 CFX_ByteString bs = wsLink.UTF16LE_Encode();
712 int len = bs.GetLength();
713 FPDF_FILEHANDLER* pFileHandler =
dsinclair655fcca2016-10-11 13:53:37 -0700714 pFormFillEnv->OpenFile(0, (FPDF_WIDESTRING)bs.GetBuffer(len), "rb");
dsinclaira440bb32016-09-14 07:01:54 -0700715 bs.ReleaseBuffer(len);
dsinclaira440bb32016-09-14 07:01:54 -0700716 if (!pFileHandler)
717 return nullptr;
tsepez04c10092016-10-25 15:16:40 -0700718
tsepezfa89a202016-12-02 09:48:30 -0800719 return MakeSeekableStream(pFileHandler);
dsinclaira440bb32016-09-14 07:01:54 -0700720}
721
tsepez4cf55152016-11-02 14:37:54 -0700722bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler,
723 int fileType,
724 FPDF_DWORD encodeType,
725 FPDF_DWORD flag) {
dsinclair521b7502016-11-02 13:02:28 -0700726 if (!m_pContext->GetXFADocView())
tsepez4cf55152016-11-02 14:37:54 -0700727 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700728
729 CFX_ByteString content;
dsinclair521b7502016-11-02 13:02:28 -0700730 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700731 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700732 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700733
tsepez833619b2016-12-07 09:21:17 -0800734 CFX_RetainPtr<IFX_SeekableStream> fileStream =
735 MakeSeekableStream(pFileHandler);
tsepezfa89a202016-12-02 09:48:30 -0800736
dsinclaira440bb32016-09-14 07:01:54 -0700737 if (fileType == FXFA_SAVEAS_XML) {
738 const char kContent[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
tsepezfa89a202016-12-02 09:48:30 -0800739 fileStream->WriteBlock(kContent, 0, strlen(kContent));
tsepez833619b2016-12-07 09:21:17 -0800740 m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Data, fileStream,
dsinclair521b7502016-11-02 13:02:28 -0700741 nullptr);
tsepez4cf55152016-11-02 14:37:54 -0700742 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700743 }
744
745 if (fileType != FXFA_SAVEAS_XDP)
tsepez4cf55152016-11-02 14:37:54 -0700746 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700747
748 if (!flag) {
749 flag = FXFA_CONFIG | FXFA_TEMPLATE | FXFA_LOCALESET | FXFA_DATASETS |
750 FXFA_XMPMETA | FXFA_XFDF | FXFA_FORM;
751 }
dsinclair521b7502016-11-02 13:02:28 -0700752 if (!m_pContext->GetPDFDoc()) {
tsepezfa89a202016-12-02 09:48:30 -0800753 fileStream->Flush();
tsepez4cf55152016-11-02 14:37:54 -0700754 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700755 }
756
dsinclair521b7502016-11-02 13:02:28 -0700757 CPDF_Dictionary* pRoot = m_pContext->GetPDFDoc()->GetRoot();
dsinclaira440bb32016-09-14 07:01:54 -0700758 if (!pRoot) {
tsepezfa89a202016-12-02 09:48:30 -0800759 fileStream->Flush();
tsepez4cf55152016-11-02 14:37:54 -0700760 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700761 }
762
dsinclair38fd8442016-09-15 10:15:32 -0700763 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
dsinclaira440bb32016-09-14 07:01:54 -0700764 if (!pAcroForm) {
tsepezfa89a202016-12-02 09:48:30 -0800765 fileStream->Flush();
tsepez4cf55152016-11-02 14:37:54 -0700766 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700767 }
768
dsinclair38fd8442016-09-15 10:15:32 -0700769 CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA"));
dsinclaira440bb32016-09-14 07:01:54 -0700770 if (!pArray) {
tsepezfa89a202016-12-02 09:48:30 -0800771 fileStream->Flush();
tsepez4cf55152016-11-02 14:37:54 -0700772 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700773 }
774
775 int size = pArray->GetCount();
776 for (int i = 1; i < size; i += 2) {
777 CPDF_Object* pPDFObj = pArray->GetObjectAt(i);
778 CPDF_Object* pPrePDFObj = pArray->GetObjectAt(i - 1);
779 if (!pPrePDFObj->IsString())
780 continue;
781 if (!pPDFObj->IsReference())
782 continue;
783
784 CPDF_Object* pDirectObj = pPDFObj->GetDirect();
785 if (!pDirectObj->IsStream())
786 continue;
787 if (pPrePDFObj->GetString() == "config" && !(flag & FXFA_CONFIG))
788 continue;
789 if (pPrePDFObj->GetString() == "template" && !(flag & FXFA_TEMPLATE))
790 continue;
791 if (pPrePDFObj->GetString() == "localeSet" && !(flag & FXFA_LOCALESET))
792 continue;
793 if (pPrePDFObj->GetString() == "datasets" && !(flag & FXFA_DATASETS))
794 continue;
795 if (pPrePDFObj->GetString() == "xmpmeta" && !(flag & FXFA_XMPMETA))
796 continue;
797 if (pPrePDFObj->GetString() == "xfdf" && !(flag & FXFA_XFDF))
798 continue;
799 if (pPrePDFObj->GetString() == "form" && !(flag & FXFA_FORM))
800 continue;
801 if (pPrePDFObj->GetString() == "form") {
tsepez833619b2016-12-07 09:21:17 -0800802 m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Form, fileStream,
dsinclair521b7502016-11-02 13:02:28 -0700803 nullptr);
dsinclaira440bb32016-09-14 07:01:54 -0700804 } else if (pPrePDFObj->GetString() == "datasets") {
tsepez833619b2016-12-07 09:21:17 -0800805 m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Datasets, fileStream,
806 nullptr);
dsinclaira440bb32016-09-14 07:01:54 -0700807 } else {
808 // PDF,creator.
809 }
810 }
tsepez4cf55152016-11-02 14:37:54 -0700811 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700812}
813
814void CPDFXFA_DocEnvironment::ToXFAContentFlags(CFX_WideString csSrcContent,
815 FPDF_DWORD& flag) {
816 if (csSrcContent.Find(L" config ", 0) != -1)
817 flag |= FXFA_CONFIG;
818 if (csSrcContent.Find(L" template ", 0) != -1)
819 flag |= FXFA_TEMPLATE;
820 if (csSrcContent.Find(L" localeSet ", 0) != -1)
821 flag |= FXFA_LOCALESET;
822 if (csSrcContent.Find(L" datasets ", 0) != -1)
823 flag |= FXFA_DATASETS;
824 if (csSrcContent.Find(L" xmpmeta ", 0) != -1)
825 flag |= FXFA_XMPMETA;
826 if (csSrcContent.Find(L" xfdf ", 0) != -1)
827 flag |= FXFA_XFDF;
828 if (csSrcContent.Find(L" form ", 0) != -1)
829 flag |= FXFA_FORM;
830 if (flag == 0) {
831 flag = FXFA_CONFIG | FXFA_TEMPLATE | FXFA_LOCALESET | FXFA_DATASETS |
832 FXFA_XMPMETA | FXFA_XFDF | FXFA_FORM;
833 }
834}
835
tsepez4cf55152016-11-02 14:37:54 -0700836bool CPDFXFA_DocEnvironment::MailToInfo(CFX_WideString& csURL,
837 CFX_WideString& csToAddress,
838 CFX_WideString& csCCAddress,
839 CFX_WideString& csBCCAddress,
840 CFX_WideString& csSubject,
841 CFX_WideString& csMsg) {
dsinclaira440bb32016-09-14 07:01:54 -0700842 CFX_WideString srcURL = csURL;
843 srcURL.TrimLeft();
844 if (srcURL.Left(7).CompareNoCase(L"mailto:") != 0)
tsepez4cf55152016-11-02 14:37:54 -0700845 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700846
847 int pos = srcURL.Find(L'?', 0);
848 CFX_WideString tmp;
849 if (pos == -1) {
850 pos = srcURL.Find(L'@', 0);
851 if (pos == -1)
tsepez4cf55152016-11-02 14:37:54 -0700852 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700853
854 tmp = srcURL.Right(csURL.GetLength() - 7);
855 } else {
856 tmp = srcURL.Left(pos);
857 tmp = tmp.Right(tmp.GetLength() - 7);
858 }
859 tmp.TrimLeft();
860 tmp.TrimRight();
861
862 csToAddress = tmp;
863
864 srcURL = srcURL.Right(srcURL.GetLength() - (pos + 1));
865 while (!srcURL.IsEmpty()) {
866 srcURL.TrimLeft();
867 srcURL.TrimRight();
868 pos = srcURL.Find(L'&', 0);
869
870 tmp = (pos == -1) ? srcURL : srcURL.Left(pos);
871 tmp.TrimLeft();
872 tmp.TrimRight();
873 if (tmp.GetLength() >= 3 && tmp.Left(3).CompareNoCase(L"cc=") == 0) {
874 tmp = tmp.Right(tmp.GetLength() - 3);
875 if (!csCCAddress.IsEmpty())
876 csCCAddress += L';';
877 csCCAddress += tmp;
878 } else if (tmp.GetLength() >= 4 &&
879 tmp.Left(4).CompareNoCase(L"bcc=") == 0) {
880 tmp = tmp.Right(tmp.GetLength() - 4);
881 if (!csBCCAddress.IsEmpty())
882 csBCCAddress += L';';
883 csBCCAddress += tmp;
884 } else if (tmp.GetLength() >= 8 &&
885 tmp.Left(8).CompareNoCase(L"subject=") == 0) {
886 tmp = tmp.Right(tmp.GetLength() - 8);
887 csSubject += tmp;
888 } else if (tmp.GetLength() >= 5 &&
889 tmp.Left(5).CompareNoCase(L"body=") == 0) {
890 tmp = tmp.Right(tmp.GetLength() - 5);
891 csMsg += tmp;
892 }
893 srcURL = (pos == -1) ? L"" : srcURL.Right(csURL.GetLength() - (pos + 1));
894 }
895 csToAddress.Replace(L",", L";");
896 csCCAddress.Replace(L",", L";");
897 csBCCAddress.Replace(L",", L";");
tsepez4cf55152016-11-02 14:37:54 -0700898 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700899}
900
tsepez4cf55152016-11-02 14:37:54 -0700901bool CPDFXFA_DocEnvironment::SubmitDataInternal(CXFA_FFDoc* hDoc,
902 CXFA_Submit submit) {
dsinclair521b7502016-11-02 13:02:28 -0700903 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
dsinclair655fcca2016-10-11 13:53:37 -0700904 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700905 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700906
907 CFX_WideStringC csURLC;
908 submit.GetSubmitTarget(csURLC);
909 CFX_WideString csURL(csURLC);
910 if (csURL.IsEmpty()) {
911 CFX_WideString ws;
912 ws.FromLocal("Submit cancelled.");
913 CFX_ByteString bs = ws.UTF16LE_Encode();
914 int len = bs.GetLength();
dsinclair655fcca2016-10-11 13:53:37 -0700915 pFormFillEnv->Alert((FPDF_WIDESTRING)bs.GetBuffer(len),
916 (FPDF_WIDESTRING)L"", 0, 4);
dsinclaira440bb32016-09-14 07:01:54 -0700917 bs.ReleaseBuffer(len);
tsepez4cf55152016-11-02 14:37:54 -0700918 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700919 }
920
dsinclaira440bb32016-09-14 07:01:54 -0700921 FPDF_FILEHANDLER* pFileHandler = nullptr;
922 int fileFlag = -1;
923 switch (submit.GetSubmitFormat()) {
924 case XFA_ATTRIBUTEENUM_Xdp: {
925 CFX_WideStringC csContentC;
926 submit.GetSubmitXDPContent(csContentC);
927 CFX_WideString csContent;
928 csContent = csContentC;
929 csContent.TrimLeft();
930 csContent.TrimRight();
931 CFX_WideString space;
932 space.FromLocal(" ");
933 csContent = space + csContent + space;
934 FPDF_DWORD flag = 0;
935 if (submit.IsSubmitEmbedPDF())
936 flag |= FXFA_PDF;
937
938 ToXFAContentFlags(csContent, flag);
dsinclair655fcca2016-10-11 13:53:37 -0700939 pFileHandler = pFormFillEnv->OpenFile(FXFA_SAVEAS_XDP, nullptr, "wb");
dsinclaira440bb32016-09-14 07:01:54 -0700940 fileFlag = FXFA_SAVEAS_XDP;
941 ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XDP, 0, flag);
942 break;
943 }
944 case XFA_ATTRIBUTEENUM_Xml:
dsinclair655fcca2016-10-11 13:53:37 -0700945 pFileHandler = pFormFillEnv->OpenFile(FXFA_SAVEAS_XML, nullptr, "wb");
dsinclaira440bb32016-09-14 07:01:54 -0700946 fileFlag = FXFA_SAVEAS_XML;
947 ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XML, 0, FXFA_XFA_ALL);
948 break;
949 case XFA_ATTRIBUTEENUM_Pdf:
950 break;
951 case XFA_ATTRIBUTEENUM_Urlencoded:
dsinclair655fcca2016-10-11 13:53:37 -0700952 pFileHandler = pFormFillEnv->OpenFile(FXFA_SAVEAS_XML, nullptr, "wb");
dsinclaira440bb32016-09-14 07:01:54 -0700953 fileFlag = FXFA_SAVEAS_XML;
954 ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XML, 0, FXFA_XFA_ALL);
955 break;
956 default:
957 return false;
958 }
959 if (!pFileHandler)
tsepez4cf55152016-11-02 14:37:54 -0700960 return false;
dsinclaira440bb32016-09-14 07:01:54 -0700961 if (csURL.Left(7).CompareNoCase(L"mailto:") == 0) {
962 CFX_WideString csToAddress;
963 CFX_WideString csCCAddress;
964 CFX_WideString csBCCAddress;
965 CFX_WideString csSubject;
966 CFX_WideString csMsg;
tsepez16b703c2016-10-26 08:45:31 -0700967 if (!MailToInfo(csURL, csToAddress, csCCAddress, csBCCAddress, csSubject,
968 csMsg)) {
tsepez4cf55152016-11-02 14:37:54 -0700969 return false;
tsepez16b703c2016-10-26 08:45:31 -0700970 }
dsinclaira440bb32016-09-14 07:01:54 -0700971 CFX_ByteString bsTo = CFX_WideString(csToAddress).UTF16LE_Encode();
972 CFX_ByteString bsCC = CFX_WideString(csCCAddress).UTF16LE_Encode();
973 CFX_ByteString bsBcc = CFX_WideString(csBCCAddress).UTF16LE_Encode();
974 CFX_ByteString bsSubject = CFX_WideString(csSubject).UTF16LE_Encode();
975 CFX_ByteString bsMsg = CFX_WideString(csMsg).UTF16LE_Encode();
976 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
977 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
978 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
979 FPDF_WIDESTRING pSubject =
980 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
981 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
dsinclair655fcca2016-10-11 13:53:37 -0700982 pFormFillEnv->EmailTo(pFileHandler, pTo, pSubject, pCC, pBcc, pMsg);
dsinclaira440bb32016-09-14 07:01:54 -0700983 bsTo.ReleaseBuffer();
984 bsCC.ReleaseBuffer();
985 bsBcc.ReleaseBuffer();
986 bsSubject.ReleaseBuffer();
987 bsMsg.ReleaseBuffer();
988 } else {
989 // HTTP or FTP
990 CFX_WideString ws;
991 CFX_ByteString bs = csURL.UTF16LE_Encode();
992 int len = bs.GetLength();
dsinclair655fcca2016-10-11 13:53:37 -0700993 pFormFillEnv->UploadTo(pFileHandler, fileFlag,
994 (FPDF_WIDESTRING)bs.GetBuffer(len));
dsinclaira440bb32016-09-14 07:01:54 -0700995 bs.ReleaseBuffer(len);
996 }
tsepez4cf55152016-11-02 14:37:54 -0700997 return true;
dsinclaira440bb32016-09-14 07:01:54 -0700998}
999
tsepez4cf55152016-11-02 14:37:54 -07001000bool CPDFXFA_DocEnvironment::SetGlobalProperty(
dsinclaira440bb32016-09-14 07:01:54 -07001001 CXFA_FFDoc* hDoc,
1002 const CFX_ByteStringC& szPropName,
1003 CFXJSE_Value* pValue) {
dsinclair521b7502016-11-02 13:02:28 -07001004 if (hDoc != m_pContext->GetXFADoc())
tsepez4cf55152016-11-02 14:37:54 -07001005 return false;
dsinclaira440bb32016-09-14 07:01:54 -07001006
dsinclair521b7502016-11-02 13:02:28 -07001007 if (m_pContext->GetFormFillEnv() &&
1008 m_pContext->GetFormFillEnv()->GetJSRuntime()) {
1009 return m_pContext->GetFormFillEnv()->GetJSRuntime()->SetValueByName(
dsinclaira440bb32016-09-14 07:01:54 -07001010 szPropName, pValue);
dsinclair655fcca2016-10-11 13:53:37 -07001011 }
tsepez4cf55152016-11-02 14:37:54 -07001012 return false;
dsinclaira440bb32016-09-14 07:01:54 -07001013}
1014
tsepez4cf55152016-11-02 14:37:54 -07001015bool CPDFXFA_DocEnvironment::GetGlobalProperty(
dsinclaira440bb32016-09-14 07:01:54 -07001016 CXFA_FFDoc* hDoc,
1017 const CFX_ByteStringC& szPropName,
1018 CFXJSE_Value* pValue) {
dsinclair521b7502016-11-02 13:02:28 -07001019 if (hDoc != m_pContext->GetXFADoc())
tsepez4cf55152016-11-02 14:37:54 -07001020 return false;
dsinclair521b7502016-11-02 13:02:28 -07001021 if (!m_pContext->GetFormFillEnv() ||
1022 !m_pContext->GetFormFillEnv()->GetJSRuntime()) {
tsepez4cf55152016-11-02 14:37:54 -07001023 return false;
dsinclair655fcca2016-10-11 13:53:37 -07001024 }
dsinclaira440bb32016-09-14 07:01:54 -07001025
dsinclair521b7502016-11-02 13:02:28 -07001026 CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pContext->GetFormFillEnv();
Tom Sepezd6ae2af2017-02-16 11:49:55 -08001027 if (!m_pJSEventContext)
1028 m_pJSEventContext = pFormFillEnv->GetJSRuntime()->NewEventContext();
dsinclaira440bb32016-09-14 07:01:54 -07001029
dsinclair82e17672016-10-11 12:38:01 -07001030 return pFormFillEnv->GetJSRuntime()->GetValueByName(szPropName, pValue);
dsinclaira440bb32016-09-14 07:01:54 -07001031}