blob: 2beaadeb1a9e5d59349eb7503f693159a02b5526 [file] [log] [blame]
dsinclairf34518b2016-09-13 12:03:48 -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
dsinclair735606d2016-10-05 15:47:02 -07007#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclairf34518b2016-09-13 12:03:48 -07008
thestigd4c34f22016-09-28 17:04:51 -07009#include <memory>
10
dsinclair7cbe68e2016-10-12 11:56:23 -070011#include "core/fpdfapi/parser/cpdf_array.h"
12#include "core/fpdfdoc/cpdf_docjsactions.h"
dsinclair114e46a2016-09-29 17:18:21 -070013#include "fpdfsdk/cpdfsdk_annothandlermgr.h"
dsinclair7cbe68e2016-10-12 11:56:23 -070014#include "fpdfsdk/cpdfsdk_interform.h"
15#include "fpdfsdk/cpdfsdk_pageview.h"
16#include "fpdfsdk/cpdfsdk_widget.h"
dsinclairb94d7c92016-09-21 12:07:00 -070017#include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/fsdk_actionhandler.h"
dsinclairf34518b2016-09-13 12:03:48 -070019#include "fpdfsdk/javascript/ijs_runtime.h"
tsepez36eb4bd2016-10-03 15:24:27 -070020#include "third_party/base/ptr_util.h"
dsinclairf34518b2016-09-13 12:03:48 -070021
22#ifdef PDF_ENABLE_XFA
dsinclair4d29e782016-10-04 14:02:47 -070023#include "fpdfsdk/fpdfxfa/cpdfxfa_app.h"
dsinclairf34518b2016-09-13 12:03:48 -070024#endif // PDF_ENABLE_XFA
25
26namespace {
27
28// NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken
29// since modifying the result would impact |bsUTF16LE|.
30FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) {
31 return reinterpret_cast<FPDF_WIDESTRING>(
32 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()));
33}
34
35} // namespace
36
dsinclair735606d2016-10-05 15:47:02 -070037CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment(
38 UnderlyingDocumentType* pDoc,
39 FPDF_FORMFILLINFO* pFFinfo)
dsinclaira939bfe2016-09-22 13:18:45 -070040 : m_pInfo(pFFinfo),
dsinclaira939bfe2016-09-22 13:18:45 -070041 m_pUnderlyingDoc(pDoc),
dsinclair7cbe68e2016-10-12 11:56:23 -070042 m_pSysHandler(new CFX_SystemHandler(this)),
43 m_bChangeMask(FALSE),
44 m_bBeingDestroyed(FALSE) {}
dsinclairf34518b2016-09-13 12:03:48 -070045
dsinclair735606d2016-10-05 15:47:02 -070046CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() {
dsinclair7cbe68e2016-10-12 11:56:23 -070047 m_bBeingDestroyed = TRUE;
48
49 ClearAllFocusedAnnots();
50 for (auto& it : m_pageMap)
51 delete it.second;
52 m_pageMap.clear();
53
dsinclair709f5a92016-10-11 14:21:16 -070054 // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller|
55 // when it cleans up. So, we must make sure it is cleaned up before
56 // |m_pFormFiller|.
57 m_pAnnotHandlerMgr.reset();
58
59 // Must destroy the |m_pFormFiller| before the environment (|this|)
60 // because any created form widgets hold a pointer to the environment.
61 // Those widgets may call things like KillTimer() as they are shutdown.
62 m_pFormFiller.reset();
63
dsinclairf34518b2016-09-13 12:03:48 -070064#ifdef PDF_ENABLE_XFA
65 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
66 if (pProvider->m_pEnvList.GetSize() == 0)
67 pProvider->SetJavaScriptInitialized(FALSE);
68#endif // PDF_ENABLE_XFA
69 if (m_pInfo && m_pInfo->Release)
70 m_pInfo->Release(m_pInfo);
71}
72
dsinclair735606d2016-10-05 15:47:02 -070073int CPDFSDK_FormFillEnvironment::JS_appAlert(const FX_WCHAR* Msg,
74 const FX_WCHAR* Title,
75 uint32_t Type,
76 uint32_t Icon) {
dsinclairf34518b2016-09-13 12:03:48 -070077 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
78 !m_pInfo->m_pJsPlatform->app_alert) {
79 return -1;
80 }
81 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
82 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
83 return m_pInfo->m_pJsPlatform->app_alert(
84 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
85 AsFPDFWideString(&bsTitle), Type, Icon);
86}
87
dsinclair735606d2016-10-05 15:47:02 -070088int CPDFSDK_FormFillEnvironment::JS_appResponse(const FX_WCHAR* Question,
89 const FX_WCHAR* Title,
90 const FX_WCHAR* Default,
91 const FX_WCHAR* cLabel,
92 FPDF_BOOL bPassword,
93 void* response,
94 int length) {
dsinclairf34518b2016-09-13 12:03:48 -070095 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
96 !m_pInfo->m_pJsPlatform->app_response) {
97 return -1;
98 }
99 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
100 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
101 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
102 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
103 return m_pInfo->m_pJsPlatform->app_response(
104 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
105 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
106 AsFPDFWideString(&bsLabel), bPassword, response, length);
107}
108
dsinclair735606d2016-10-05 15:47:02 -0700109void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) {
dsinclairf34518b2016-09-13 12:03:48 -0700110 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
111 !m_pInfo->m_pJsPlatform->app_beep) {
112 return;
113 }
114 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
115}
116
dsinclair735606d2016-10-05 15:47:02 -0700117CFX_WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() {
dsinclairf34518b2016-09-13 12:03:48 -0700118 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
119 !m_pInfo->m_pJsPlatform->Field_browse) {
120 return CFX_WideString();
121 }
122 const int nRequiredLen =
123 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
124 if (nRequiredLen <= 0)
125 return CFX_WideString();
126
127 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
128 memset(pBuff.get(), 0, nRequiredLen);
129 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
130 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
131 if (nActualLen <= 0 || nActualLen > nRequiredLen)
132 return CFX_WideString();
133
134 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
135}
136
dsinclair735606d2016-10-05 15:47:02 -0700137CFX_WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() {
dsinclairf34518b2016-09-13 12:03:48 -0700138 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
139 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
140 return CFX_WideString();
141 }
142 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
143 m_pInfo->m_pJsPlatform, nullptr, 0);
144 if (nRequiredLen <= 0)
145 return CFX_WideString();
146
147 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
148 memset(pBuff.get(), 0, nRequiredLen);
149 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
150 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
151 if (nActualLen <= 0 || nActualLen > nRequiredLen)
152 return CFX_WideString();
153
154 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
155}
156
dsinclair735606d2016-10-05 15:47:02 -0700157void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData,
158 int length,
159 const FX_WCHAR* URL) {
dsinclairf34518b2016-09-13 12:03:48 -0700160 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
161 !m_pInfo->m_pJsPlatform->Doc_submitForm) {
162 return;
163 }
164 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
165 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
166 length,
167 AsFPDFWideString(&bsDestination));
168}
169
dsinclair735606d2016-10-05 15:47:02 -0700170void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData,
171 int length,
172 FPDF_BOOL bUI,
173 const FX_WCHAR* To,
174 const FX_WCHAR* Subject,
175 const FX_WCHAR* CC,
176 const FX_WCHAR* BCC,
177 const FX_WCHAR* Msg) {
dsinclairf34518b2016-09-13 12:03:48 -0700178 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
179 !m_pInfo->m_pJsPlatform->Doc_mail) {
180 return;
181 }
182 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
183 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
184 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode();
185 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
186 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
187 m_pInfo->m_pJsPlatform->Doc_mail(
188 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
189 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
190 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg));
191}
192
dsinclair735606d2016-10-05 15:47:02 -0700193void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI,
194 int nStart,
195 int nEnd,
196 FPDF_BOOL bSilent,
197 FPDF_BOOL bShrinkToFit,
198 FPDF_BOOL bPrintAsImage,
199 FPDF_BOOL bReverse,
200 FPDF_BOOL bAnnotations) {
dsinclairf34518b2016-09-13 12:03:48 -0700201 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
202 !m_pInfo->m_pJsPlatform->Doc_print) {
203 return;
204 }
205 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd,
206 bSilent, bShrinkToFit, bPrintAsImage,
207 bReverse, bAnnotations);
208}
209
dsinclair735606d2016-10-05 15:47:02 -0700210void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) {
dsinclairf34518b2016-09-13 12:03:48 -0700211 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
212 !m_pInfo->m_pJsPlatform->Doc_gotoPage) {
213 return;
214 }
215 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
216}
217
dsinclair735606d2016-10-05 15:47:02 -0700218IJS_Runtime* CPDFSDK_FormFillEnvironment::GetJSRuntime() {
dsinclairf34518b2016-09-13 12:03:48 -0700219 if (!IsJSInitiated())
220 return nullptr;
221 if (!m_pJSRuntime)
222 m_pJSRuntime.reset(IJS_Runtime::Create(this));
223 return m_pJSRuntime.get();
224}
225
dsinclair735606d2016-10-05 15:47:02 -0700226CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() {
dsinclairf34518b2016-09-13 12:03:48 -0700227 if (!m_pAnnotHandlerMgr)
tsepez36eb4bd2016-10-03 15:24:27 -0700228 m_pAnnotHandlerMgr = pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(this);
dsinclairf34518b2016-09-13 12:03:48 -0700229 return m_pAnnotHandlerMgr.get();
230}
231
dsinclair735606d2016-10-05 15:47:02 -0700232CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHander() {
dsinclairf34518b2016-09-13 12:03:48 -0700233 if (!m_pActionHandler)
tsepez36eb4bd2016-10-03 15:24:27 -0700234 m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>();
dsinclairf34518b2016-09-13 12:03:48 -0700235 return m_pActionHandler.get();
236}
237
dsinclair735606d2016-10-05 15:47:02 -0700238CFFL_InteractiveFormFiller*
239CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() {
dsinclairb94d7c92016-09-21 12:07:00 -0700240 if (!m_pFormFiller)
tsepez36eb4bd2016-10-03 15:24:27 -0700241 m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this);
dsinclairb94d7c92016-09-21 12:07:00 -0700242 return m_pFormFiller.get();
dsinclairf34518b2016-09-13 12:03:48 -0700243}
dsinclair577ad2c2016-09-22 10:20:43 -0700244
dsinclair735606d2016-10-05 15:47:02 -0700245void CPDFSDK_FormFillEnvironment::Invalidate(FPDF_PAGE page,
dsinclair577ad2c2016-09-22 10:20:43 -0700246 double left,
247 double top,
248 double right,
249 double bottom) {
dsinclair735606d2016-10-05 15:47:02 -0700250 if (m_pInfo && m_pInfo->FFI_Invalidate)
251 m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom);
252}
253
254void CPDFSDK_FormFillEnvironment::OutputSelectedRect(FPDF_PAGE page,
255 double left,
256 double top,
257 double right,
258 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700259 if (m_pInfo && m_pInfo->FFI_OutputSelectedRect)
260 m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom);
261}
262
dsinclair735606d2016-10-05 15:47:02 -0700263void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) {
dsinclair577ad2c2016-09-22 10:20:43 -0700264 if (m_pInfo && m_pInfo->FFI_SetCursor)
265 m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
266}
267
dsinclair735606d2016-10-05 15:47:02 -0700268int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse,
269 TimerCallback lpTimerFunc) {
dsinclair577ad2c2016-09-22 10:20:43 -0700270 if (m_pInfo && m_pInfo->FFI_SetTimer)
271 return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
272 return -1;
273}
274
dsinclair735606d2016-10-05 15:47:02 -0700275void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) {
dsinclair577ad2c2016-09-22 10:20:43 -0700276 if (m_pInfo && m_pInfo->FFI_KillTimer)
277 m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
278}
279
dsinclair735606d2016-10-05 15:47:02 -0700280FX_SYSTEMTIME CPDFSDK_FormFillEnvironment::GetLocalTime() const {
dsinclair577ad2c2016-09-22 10:20:43 -0700281 FX_SYSTEMTIME fxtime;
282 if (!m_pInfo || !m_pInfo->FFI_GetLocalTime)
283 return fxtime;
284
285 FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo);
286 fxtime.wDay = systime.wDay;
287 fxtime.wDayOfWeek = systime.wDayOfWeek;
288 fxtime.wHour = systime.wHour;
289 fxtime.wMilliseconds = systime.wMilliseconds;
290 fxtime.wMinute = systime.wMinute;
291 fxtime.wMonth = systime.wMonth;
292 fxtime.wSecond = systime.wSecond;
293 fxtime.wYear = systime.wYear;
294 return fxtime;
295}
296
dsinclair735606d2016-10-05 15:47:02 -0700297void CPDFSDK_FormFillEnvironment::OnChange() {
dsinclair577ad2c2016-09-22 10:20:43 -0700298 if (m_pInfo && m_pInfo->FFI_OnChange)
299 m_pInfo->FFI_OnChange(m_pInfo);
300}
301
dsinclair735606d2016-10-05 15:47:02 -0700302FX_BOOL CPDFSDK_FormFillEnvironment::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700303 return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0;
304}
305
dsinclair735606d2016-10-05 15:47:02 -0700306FX_BOOL CPDFSDK_FormFillEnvironment::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700307 return (nFlag & FWL_EVENTFLAG_ControlKey) != 0;
308}
309
dsinclair735606d2016-10-05 15:47:02 -0700310FX_BOOL CPDFSDK_FormFillEnvironment::IsALTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700311 return (nFlag & FWL_EVENTFLAG_AltKey) != 0;
312}
313
dsinclair735606d2016-10-05 15:47:02 -0700314FPDF_PAGE CPDFSDK_FormFillEnvironment::GetPage(FPDF_DOCUMENT document,
315 int nPageIndex) {
dsinclair577ad2c2016-09-22 10:20:43 -0700316 if (m_pInfo && m_pInfo->FFI_GetPage)
317 return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
318 return nullptr;
319}
320
dsinclair735606d2016-10-05 15:47:02 -0700321FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700322 if (m_pInfo && m_pInfo->FFI_GetCurrentPage)
323 return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
324 return nullptr;
325}
326
dsinclair735606d2016-10-05 15:47:02 -0700327void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(
328 const FX_CHAR* namedAction) {
dsinclair577ad2c2016-09-22 10:20:43 -0700329 if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
330 m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
331}
332
dsinclair735606d2016-10-05 15:47:02 -0700333void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus(
334 FPDF_WIDESTRING focusText,
335 FPDF_DWORD nTextLen,
336 FX_BOOL bFocus) {
dsinclair577ad2c2016-09-22 10:20:43 -0700337 if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
338 m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
339}
340
dsinclair735606d2016-10-05 15:47:02 -0700341void CPDFSDK_FormFillEnvironment::DoURIAction(const FX_CHAR* bsURI) {
dsinclair577ad2c2016-09-22 10:20:43 -0700342 if (m_pInfo && m_pInfo->FFI_DoURIAction)
343 m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
344}
345
dsinclair735606d2016-10-05 15:47:02 -0700346void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex,
347 int zoomMode,
348 float* fPosArray,
349 int sizeOfArray) {
dsinclair577ad2c2016-09-22 10:20:43 -0700350 if (m_pInfo && m_pInfo->FFI_DoGoToAction) {
351 m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray,
352 sizeOfArray);
353 }
354}
355
356#ifdef PDF_ENABLE_XFA
dsinclair735606d2016-10-05 15:47:02 -0700357void CPDFSDK_FormFillEnvironment::DisplayCaret(FPDF_PAGE page,
358 FPDF_BOOL bVisible,
359 double left,
360 double top,
361 double right,
362 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700363 if (m_pInfo && m_pInfo->FFI_DisplayCaret) {
364 m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right,
365 bottom);
366 }
367}
368
dsinclair735606d2016-10-05 15:47:02 -0700369int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700370 if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex)
371 return -1;
372 return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
373}
374
dsinclair735606d2016-10-05 15:47:02 -0700375void CPDFSDK_FormFillEnvironment::SetCurrentPage(FPDF_DOCUMENT document,
376 int iCurPage) {
dsinclair577ad2c2016-09-22 10:20:43 -0700377 if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
378 m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
379}
380
dsinclair735606d2016-10-05 15:47:02 -0700381CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() {
dsinclair577ad2c2016-09-22 10:20:43 -0700382 if (!m_pInfo || !m_pInfo->FFI_GetPlatform)
383 return L"";
384
385 int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0);
386 if (nRequiredLen <= 0)
387 return L"";
388
389 char* pbuff = new char[nRequiredLen];
390 memset(pbuff, 0, nRequiredLen);
391 int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
392 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
393 delete[] pbuff;
394 return L"";
395 }
396 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
397 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
398 (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
399 bsRet.GetLength() / sizeof(unsigned short));
400 delete[] pbuff;
401 return wsRet;
402}
403
dsinclair735606d2016-10-05 15:47:02 -0700404void CPDFSDK_FormFillEnvironment::GotoURL(FPDF_DOCUMENT document,
405 const CFX_WideStringC& wsURL) {
dsinclair577ad2c2016-09-22 10:20:43 -0700406 if (!m_pInfo || !m_pInfo->FFI_GotoURL)
407 return;
408
409 CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
410 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
411 m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
412 bsTo.ReleaseBuffer();
413}
414
dsinclair735606d2016-10-05 15:47:02 -0700415void CPDFSDK_FormFillEnvironment::GetPageViewRect(FPDF_PAGE page,
416 FS_RECTF& dstRect) {
dsinclair577ad2c2016-09-22 10:20:43 -0700417 if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect)
418 return;
419
420 double left;
421 double top;
422 double right;
423 double bottom;
424 m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
425
426 dstRect.left = static_cast<float>(left);
427 dstRect.top = static_cast<float>(top < bottom ? bottom : top);
428 dstRect.bottom = static_cast<float>(top < bottom ? top : bottom);
429 dstRect.right = static_cast<float>(right);
430}
431
dsinclair735606d2016-10-05 15:47:02 -0700432FX_BOOL CPDFSDK_FormFillEnvironment::PopupMenu(FPDF_PAGE page,
433 FPDF_WIDGET hWidget,
434 int menuFlag,
435 CFX_PointF pt) {
dsinclair577ad2c2016-09-22 10:20:43 -0700436 if (!m_pInfo || !m_pInfo->FFI_PopupMenu)
437 return FALSE;
438 return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y);
439}
440
dsinclair735606d2016-10-05 15:47:02 -0700441void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg,
442 FPDF_WIDESTRING Title,
443 int Type,
444 int Icon) {
dsinclair577ad2c2016-09-22 10:20:43 -0700445 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
446 m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type,
447 Icon);
448 }
449}
450
dsinclair735606d2016-10-05 15:47:02 -0700451void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler,
452 FPDF_WIDESTRING pTo,
453 FPDF_WIDESTRING pSubject,
454 FPDF_WIDESTRING pCC,
455 FPDF_WIDESTRING pBcc,
456 FPDF_WIDESTRING pMsg) {
dsinclair577ad2c2016-09-22 10:20:43 -0700457 if (m_pInfo && m_pInfo->FFI_EmailTo)
458 m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
459}
460
dsinclair735606d2016-10-05 15:47:02 -0700461void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler,
462 int fileFlag,
463 FPDF_WIDESTRING uploadTo) {
dsinclair577ad2c2016-09-22 10:20:43 -0700464 if (m_pInfo && m_pInfo->FFI_UploadTo)
465 m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
466}
467
dsinclair735606d2016-10-05 15:47:02 -0700468FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType,
469 FPDF_WIDESTRING wsURL,
470 const char* mode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700471 if (m_pInfo && m_pInfo->FFI_OpenFile)
472 return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
473 return nullptr;
474}
475
dsinclair735606d2016-10-05 15:47:02 -0700476IFX_FileRead* CPDFSDK_FormFillEnvironment::DownloadFromURL(
477 const FX_WCHAR* url) {
dsinclair577ad2c2016-09-22 10:20:43 -0700478 if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL)
479 return nullptr;
480
481 CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
482 FPDF_WIDESTRING wsURL =
483 (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
484
485 FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
486
487 return new CFPDF_FileStream(fileHandler);
488}
489
dsinclair735606d2016-10-05 15:47:02 -0700490CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
dsinclair577ad2c2016-09-22 10:20:43 -0700491 const FX_WCHAR* wsURL,
492 const FX_WCHAR* wsData,
493 const FX_WCHAR* wsContentType,
494 const FX_WCHAR* wsEncode,
495 const FX_WCHAR* wsHeader) {
496 if (!m_pInfo || !m_pInfo->FFI_PostRequestURL)
497 return L"";
498
499 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
500 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
501
502 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
503 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
504
505 CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
506 FPDF_WIDESTRING contentType =
507 (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
508
509 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
510 FPDF_WIDESTRING encode =
511 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
512
513 CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
514 FPDF_WIDESTRING header =
515 (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
516
517 FPDF_BSTR response;
518 FPDF_BStr_Init(&response);
519 m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header,
520 &response);
521
522 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
523 (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING));
524 FPDF_BStr_Clear(&response);
525
526 return wsRet;
527}
528
dsinclair735606d2016-10-05 15:47:02 -0700529FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const FX_WCHAR* wsURL,
530 const FX_WCHAR* wsData,
531 const FX_WCHAR* wsEncode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700532 if (!m_pInfo || !m_pInfo->FFI_PutRequestURL)
533 return FALSE;
534
535 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
536 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
537
538 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
539 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
540
541 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
542 FPDF_WIDESTRING encode =
543 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
544
545 return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
546}
547
dsinclair735606d2016-10-05 15:47:02 -0700548CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
dsinclair577ad2c2016-09-22 10:20:43 -0700549 if (!m_pInfo || !m_pInfo->FFI_GetLanguage)
550 return L"";
551
552 int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
553 if (nRequiredLen <= 0)
554 return L"";
555
556 char* pbuff = new char[nRequiredLen];
557 memset(pbuff, 0, nRequiredLen);
558 int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
559 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
560 delete[] pbuff;
561 return L"";
562 }
563 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
564 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
565 (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()),
566 bsRet.GetLength() / sizeof(FPDF_WIDESTRING));
567 delete[] pbuff;
568 return wsRet;
569}
570
dsinclair735606d2016-10-05 15:47:02 -0700571void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
572 uint32_t dwEventType) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700573 if (m_pInfo && m_pInfo->FFI_PageEvent)
574 m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
575}
576#endif // PDF_ENABLE_XFA
dsinclair7cbe68e2016-10-12 11:56:23 -0700577
578void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
579 for (auto& it : m_pageMap) {
580 if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
581 KillFocusAnnot(0);
582 }
583}
584
585CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
586 UnderlyingPageType* pUnderlyingPage,
587 bool ReNew) {
588 auto it = m_pageMap.find(pUnderlyingPage);
589 if (it != m_pageMap.end())
590 return it->second;
591
592 if (!ReNew)
593 return nullptr;
594
595 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
596 m_pageMap[pUnderlyingPage] = pPageView;
597 // Delay to load all the annotations, to avoid endless loop.
598 pPageView->LoadFXAnnots();
599 return pPageView;
600}
601
602CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
603 UnderlyingPageType* pPage =
604 UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc));
605 return pPage ? GetPageView(pPage, true) : nullptr;
606}
607
608CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
609 UnderlyingPageType* pTempPage =
610 UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
611 if (!pTempPage)
612 return nullptr;
613
614 auto it = m_pageMap.find(pTempPage);
615 return it != m_pageMap.end() ? it->second : nullptr;
616}
617
618void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
619 CPDF_Document* pPDFDoc = GetPDFDocument();
620 CPDF_DocJSActions docJS(pPDFDoc);
621 int iCount = docJS.CountJSActions();
622 if (iCount < 1)
623 return;
624 for (int i = 0; i < iCount; i++) {
625 CFX_ByteString csJSName;
626 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
627 if (GetActionHander()) {
628 GetActionHander()->DoAction_JavaScript(
629 jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this);
630 }
631 }
632}
633
634FX_BOOL CPDFSDK_FormFillEnvironment::ProcOpenAction() {
635 if (!m_pUnderlyingDoc)
636 return FALSE;
637
638 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
639 if (!pRoot)
640 return FALSE;
641
642 CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
643 if (!pOpenAction)
644 pOpenAction = pRoot->GetArrayFor("OpenAction");
645
646 if (!pOpenAction)
647 return FALSE;
648
649 if (pOpenAction->IsArray())
650 return TRUE;
651
652 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
653 CPDF_Action action(pDict);
654 if (GetActionHander())
655 GetActionHander()->DoAction_DocOpen(action, this);
656 return TRUE;
657 }
658 return FALSE;
659}
660
661void CPDFSDK_FormFillEnvironment::RemovePageView(
662 UnderlyingPageType* pUnderlyingPage) {
663 auto it = m_pageMap.find(pUnderlyingPage);
664 if (it == m_pageMap.end())
665 return;
666
667 CPDFSDK_PageView* pPageView = it->second;
668 if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
669 return;
670
671 // Mark the page view so we do not come into |RemovePageView| a second
672 // time while we're in the process of removing.
673 pPageView->SetBeingDestroyed();
674
675 // This must happen before we remove |pPageView| from the map because
676 // |KillFocusAnnot| can call into the |GetPage| method which will
677 // look for this page view in the map, if it doesn't find it a new one will
678 // be created. We then have two page views pointing to the same page and
679 // bad things happen.
680 if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
681 KillFocusAnnot(0);
682
683 // Remove the page from the map to make sure we don't accidentally attempt
684 // to use the |pPageView| while we're cleaning it up.
685 m_pageMap.erase(it);
686
687 delete pPageView;
688}
689
690UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
691 return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
692}
693
694CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() {
695 if (!m_pInterForm)
696 m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this);
697 return m_pInterForm.get();
698}
699
700void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
701 CPDFSDK_Annot* pAnnot) {
702 for (const auto& it : m_pageMap) {
703 CPDFSDK_PageView* pPageView = it.second;
704 if (pPageView != pSender)
705 pPageView->UpdateView(pAnnot);
706 }
707}
708
709FX_BOOL CPDFSDK_FormFillEnvironment::SetFocusAnnot(
710 CPDFSDK_Annot::ObservedPtr* pAnnot) {
711 if (m_bBeingDestroyed)
712 return FALSE;
713 if (m_pFocusAnnot == *pAnnot)
714 return TRUE;
715 if (m_pFocusAnnot && !KillFocusAnnot(0))
716 return FALSE;
717 if (!*pAnnot)
718 return FALSE;
719
720#ifdef PDF_ENABLE_XFA
721 CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
722#endif // PDF_ENABLE_XFA
723 CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
724 if (pPageView && pPageView->IsValid()) {
725 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
726 if (!m_pFocusAnnot) {
727#ifdef PDF_ENABLE_XFA
728 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
729 return FALSE;
730#endif // PDF_ENABLE_XFA
731 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
732 return FALSE;
733 if (!m_pFocusAnnot) {
734 m_pFocusAnnot.Reset(pAnnot->Get());
735 return TRUE;
736 }
737 }
738 }
739 return FALSE;
740}
741
742FX_BOOL CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
743 if (m_pFocusAnnot) {
744 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
745 CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
746 m_pFocusAnnot.Reset();
747
748#ifdef PDF_ENABLE_XFA
749 CPDFSDK_Annot::ObservedPtr pNull;
750 if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
751 return FALSE;
752#endif // PDF_ENABLE_XFA
753
754 if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
755 if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
756 CPDFSDK_Widget* pWidget =
757 static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
758 int nFieldType = pWidget->GetFieldType();
759 if (FIELDTYPE_TEXTFIELD == nFieldType ||
760 FIELDTYPE_COMBOBOX == nFieldType) {
761 OnSetFieldInputFocus(nullptr, 0, FALSE);
762 }
763 }
764 if (!m_pFocusAnnot)
765 return TRUE;
766 } else {
767 m_pFocusAnnot.Reset(pFocusAnnot.Get());
768 }
769 }
770 return FALSE;
771}
772
773FX_BOOL CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) {
774 return GetPDFDocument()->GetUserPermissions() & nFlag;
775}