blob: 9ad7c9fd71ede3dc980286fdcdbe6eef7f0b2ef4 [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)),
dsinclair6c659ab2016-10-12 13:41:38 -070043 m_bChangeMask(false),
44 m_bBeingDestroyed(false) {}
dsinclairf34518b2016-09-13 12:03:48 -070045
dsinclair735606d2016-10-05 15:47:02 -070046CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() {
dsinclair6c659ab2016-10-12 13:41:38 -070047 m_bBeingDestroyed = true;
dsinclair7cbe68e2016-10-12 11:56:23 -070048
49 ClearAllFocusedAnnots();
dsinclair7cbe68e2016-10-12 11:56:23 -070050
dsinclair709f5a92016-10-11 14:21:16 -070051 // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller|
52 // when it cleans up. So, we must make sure it is cleaned up before
53 // |m_pFormFiller|.
54 m_pAnnotHandlerMgr.reset();
55
56 // Must destroy the |m_pFormFiller| before the environment (|this|)
57 // because any created form widgets hold a pointer to the environment.
58 // Those widgets may call things like KillTimer() as they are shutdown.
59 m_pFormFiller.reset();
60
dsinclairf34518b2016-09-13 12:03:48 -070061 if (m_pInfo && m_pInfo->Release)
62 m_pInfo->Release(m_pInfo);
63}
64
dsinclair735606d2016-10-05 15:47:02 -070065int CPDFSDK_FormFillEnvironment::JS_appAlert(const FX_WCHAR* Msg,
66 const FX_WCHAR* Title,
67 uint32_t Type,
68 uint32_t Icon) {
dsinclairf34518b2016-09-13 12:03:48 -070069 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
70 !m_pInfo->m_pJsPlatform->app_alert) {
71 return -1;
72 }
73 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
74 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
75 return m_pInfo->m_pJsPlatform->app_alert(
76 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
77 AsFPDFWideString(&bsTitle), Type, Icon);
78}
79
dsinclair735606d2016-10-05 15:47:02 -070080int CPDFSDK_FormFillEnvironment::JS_appResponse(const FX_WCHAR* Question,
81 const FX_WCHAR* Title,
82 const FX_WCHAR* Default,
83 const FX_WCHAR* cLabel,
84 FPDF_BOOL bPassword,
85 void* response,
86 int length) {
dsinclairf34518b2016-09-13 12:03:48 -070087 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
88 !m_pInfo->m_pJsPlatform->app_response) {
89 return -1;
90 }
91 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
92 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
93 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
94 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
95 return m_pInfo->m_pJsPlatform->app_response(
96 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
97 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
98 AsFPDFWideString(&bsLabel), bPassword, response, length);
99}
100
dsinclair735606d2016-10-05 15:47:02 -0700101void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) {
dsinclairf34518b2016-09-13 12:03:48 -0700102 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
103 !m_pInfo->m_pJsPlatform->app_beep) {
104 return;
105 }
106 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
107}
108
dsinclair735606d2016-10-05 15:47:02 -0700109CFX_WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() {
dsinclairf34518b2016-09-13 12:03:48 -0700110 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
111 !m_pInfo->m_pJsPlatform->Field_browse) {
112 return CFX_WideString();
113 }
114 const int nRequiredLen =
115 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
116 if (nRequiredLen <= 0)
117 return CFX_WideString();
118
119 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
120 memset(pBuff.get(), 0, nRequiredLen);
121 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
122 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
123 if (nActualLen <= 0 || nActualLen > nRequiredLen)
124 return CFX_WideString();
125
126 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
127}
128
dsinclair735606d2016-10-05 15:47:02 -0700129CFX_WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() {
dsinclairf34518b2016-09-13 12:03:48 -0700130 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
131 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
132 return CFX_WideString();
133 }
134 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
135 m_pInfo->m_pJsPlatform, nullptr, 0);
136 if (nRequiredLen <= 0)
137 return CFX_WideString();
138
139 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
140 memset(pBuff.get(), 0, nRequiredLen);
141 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
142 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
143 if (nActualLen <= 0 || nActualLen > nRequiredLen)
144 return CFX_WideString();
145
146 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
147}
148
dsinclair735606d2016-10-05 15:47:02 -0700149void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData,
150 int length,
151 const FX_WCHAR* URL) {
dsinclairf34518b2016-09-13 12:03:48 -0700152 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
153 !m_pInfo->m_pJsPlatform->Doc_submitForm) {
154 return;
155 }
156 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
157 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
158 length,
159 AsFPDFWideString(&bsDestination));
160}
161
dsinclair735606d2016-10-05 15:47:02 -0700162void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData,
163 int length,
164 FPDF_BOOL bUI,
165 const FX_WCHAR* To,
166 const FX_WCHAR* Subject,
167 const FX_WCHAR* CC,
168 const FX_WCHAR* BCC,
169 const FX_WCHAR* Msg) {
dsinclairf34518b2016-09-13 12:03:48 -0700170 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
171 !m_pInfo->m_pJsPlatform->Doc_mail) {
172 return;
173 }
174 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
175 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
176 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode();
177 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
178 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
179 m_pInfo->m_pJsPlatform->Doc_mail(
180 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
181 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
182 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg));
183}
184
dsinclair735606d2016-10-05 15:47:02 -0700185void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI,
186 int nStart,
187 int nEnd,
188 FPDF_BOOL bSilent,
189 FPDF_BOOL bShrinkToFit,
190 FPDF_BOOL bPrintAsImage,
191 FPDF_BOOL bReverse,
192 FPDF_BOOL bAnnotations) {
dsinclairf34518b2016-09-13 12:03:48 -0700193 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
194 !m_pInfo->m_pJsPlatform->Doc_print) {
195 return;
196 }
197 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd,
198 bSilent, bShrinkToFit, bPrintAsImage,
199 bReverse, bAnnotations);
200}
201
dsinclair735606d2016-10-05 15:47:02 -0700202void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) {
dsinclairf34518b2016-09-13 12:03:48 -0700203 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
204 !m_pInfo->m_pJsPlatform->Doc_gotoPage) {
205 return;
206 }
207 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
208}
209
dsinclair735606d2016-10-05 15:47:02 -0700210IJS_Runtime* CPDFSDK_FormFillEnvironment::GetJSRuntime() {
dsinclairf34518b2016-09-13 12:03:48 -0700211 if (!IsJSInitiated())
212 return nullptr;
213 if (!m_pJSRuntime)
214 m_pJSRuntime.reset(IJS_Runtime::Create(this));
215 return m_pJSRuntime.get();
216}
217
dsinclair735606d2016-10-05 15:47:02 -0700218CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() {
dsinclairf34518b2016-09-13 12:03:48 -0700219 if (!m_pAnnotHandlerMgr)
tsepez36eb4bd2016-10-03 15:24:27 -0700220 m_pAnnotHandlerMgr = pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(this);
dsinclairf34518b2016-09-13 12:03:48 -0700221 return m_pAnnotHandlerMgr.get();
222}
223
dsinclair735606d2016-10-05 15:47:02 -0700224CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHander() {
dsinclairf34518b2016-09-13 12:03:48 -0700225 if (!m_pActionHandler)
tsepez36eb4bd2016-10-03 15:24:27 -0700226 m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>();
dsinclairf34518b2016-09-13 12:03:48 -0700227 return m_pActionHandler.get();
228}
229
dsinclair735606d2016-10-05 15:47:02 -0700230CFFL_InteractiveFormFiller*
231CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() {
dsinclairb94d7c92016-09-21 12:07:00 -0700232 if (!m_pFormFiller)
tsepez36eb4bd2016-10-03 15:24:27 -0700233 m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this);
dsinclairb94d7c92016-09-21 12:07:00 -0700234 return m_pFormFiller.get();
dsinclairf34518b2016-09-13 12:03:48 -0700235}
dsinclair577ad2c2016-09-22 10:20:43 -0700236
dsinclair735606d2016-10-05 15:47:02 -0700237void CPDFSDK_FormFillEnvironment::Invalidate(FPDF_PAGE page,
dsinclair577ad2c2016-09-22 10:20:43 -0700238 double left,
239 double top,
240 double right,
241 double bottom) {
dsinclair735606d2016-10-05 15:47:02 -0700242 if (m_pInfo && m_pInfo->FFI_Invalidate)
243 m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom);
244}
245
246void CPDFSDK_FormFillEnvironment::OutputSelectedRect(FPDF_PAGE page,
247 double left,
248 double top,
249 double right,
250 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700251 if (m_pInfo && m_pInfo->FFI_OutputSelectedRect)
252 m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom);
253}
254
dsinclair735606d2016-10-05 15:47:02 -0700255void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) {
dsinclair577ad2c2016-09-22 10:20:43 -0700256 if (m_pInfo && m_pInfo->FFI_SetCursor)
257 m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
258}
259
dsinclair735606d2016-10-05 15:47:02 -0700260int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse,
261 TimerCallback lpTimerFunc) {
dsinclair577ad2c2016-09-22 10:20:43 -0700262 if (m_pInfo && m_pInfo->FFI_SetTimer)
263 return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
264 return -1;
265}
266
dsinclair735606d2016-10-05 15:47:02 -0700267void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) {
dsinclair577ad2c2016-09-22 10:20:43 -0700268 if (m_pInfo && m_pInfo->FFI_KillTimer)
269 m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
270}
271
dsinclair735606d2016-10-05 15:47:02 -0700272FX_SYSTEMTIME CPDFSDK_FormFillEnvironment::GetLocalTime() const {
dsinclair577ad2c2016-09-22 10:20:43 -0700273 FX_SYSTEMTIME fxtime;
274 if (!m_pInfo || !m_pInfo->FFI_GetLocalTime)
275 return fxtime;
276
277 FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo);
278 fxtime.wDay = systime.wDay;
279 fxtime.wDayOfWeek = systime.wDayOfWeek;
280 fxtime.wHour = systime.wHour;
281 fxtime.wMilliseconds = systime.wMilliseconds;
282 fxtime.wMinute = systime.wMinute;
283 fxtime.wMonth = systime.wMonth;
284 fxtime.wSecond = systime.wSecond;
285 fxtime.wYear = systime.wYear;
286 return fxtime;
287}
288
dsinclair735606d2016-10-05 15:47:02 -0700289void CPDFSDK_FormFillEnvironment::OnChange() {
dsinclair577ad2c2016-09-22 10:20:43 -0700290 if (m_pInfo && m_pInfo->FFI_OnChange)
291 m_pInfo->FFI_OnChange(m_pInfo);
292}
293
dsinclair735606d2016-10-05 15:47:02 -0700294FX_BOOL CPDFSDK_FormFillEnvironment::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700295 return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0;
296}
297
dsinclair735606d2016-10-05 15:47:02 -0700298FX_BOOL CPDFSDK_FormFillEnvironment::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700299 return (nFlag & FWL_EVENTFLAG_ControlKey) != 0;
300}
301
dsinclair735606d2016-10-05 15:47:02 -0700302FX_BOOL CPDFSDK_FormFillEnvironment::IsALTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700303 return (nFlag & FWL_EVENTFLAG_AltKey) != 0;
304}
305
dsinclair735606d2016-10-05 15:47:02 -0700306FPDF_PAGE CPDFSDK_FormFillEnvironment::GetPage(FPDF_DOCUMENT document,
307 int nPageIndex) {
dsinclair577ad2c2016-09-22 10:20:43 -0700308 if (m_pInfo && m_pInfo->FFI_GetPage)
309 return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
310 return nullptr;
311}
312
dsinclair735606d2016-10-05 15:47:02 -0700313FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700314 if (m_pInfo && m_pInfo->FFI_GetCurrentPage)
315 return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
316 return nullptr;
317}
318
dsinclair735606d2016-10-05 15:47:02 -0700319void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(
320 const FX_CHAR* namedAction) {
dsinclair577ad2c2016-09-22 10:20:43 -0700321 if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
322 m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
323}
324
dsinclair735606d2016-10-05 15:47:02 -0700325void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus(
326 FPDF_WIDESTRING focusText,
327 FPDF_DWORD nTextLen,
328 FX_BOOL bFocus) {
dsinclair577ad2c2016-09-22 10:20:43 -0700329 if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
330 m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
331}
332
dsinclair735606d2016-10-05 15:47:02 -0700333void CPDFSDK_FormFillEnvironment::DoURIAction(const FX_CHAR* bsURI) {
dsinclair577ad2c2016-09-22 10:20:43 -0700334 if (m_pInfo && m_pInfo->FFI_DoURIAction)
335 m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
336}
337
dsinclair735606d2016-10-05 15:47:02 -0700338void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex,
339 int zoomMode,
340 float* fPosArray,
341 int sizeOfArray) {
dsinclair577ad2c2016-09-22 10:20:43 -0700342 if (m_pInfo && m_pInfo->FFI_DoGoToAction) {
343 m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray,
344 sizeOfArray);
345 }
346}
347
348#ifdef PDF_ENABLE_XFA
dsinclair735606d2016-10-05 15:47:02 -0700349void CPDFSDK_FormFillEnvironment::DisplayCaret(FPDF_PAGE page,
350 FPDF_BOOL bVisible,
351 double left,
352 double top,
353 double right,
354 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700355 if (m_pInfo && m_pInfo->FFI_DisplayCaret) {
356 m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right,
357 bottom);
358 }
359}
360
dsinclair735606d2016-10-05 15:47:02 -0700361int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700362 if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex)
363 return -1;
364 return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
365}
366
dsinclair735606d2016-10-05 15:47:02 -0700367void CPDFSDK_FormFillEnvironment::SetCurrentPage(FPDF_DOCUMENT document,
368 int iCurPage) {
dsinclair577ad2c2016-09-22 10:20:43 -0700369 if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
370 m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
371}
372
dsinclair735606d2016-10-05 15:47:02 -0700373CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() {
dsinclair577ad2c2016-09-22 10:20:43 -0700374 if (!m_pInfo || !m_pInfo->FFI_GetPlatform)
375 return L"";
376
377 int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0);
378 if (nRequiredLen <= 0)
379 return L"";
380
381 char* pbuff = new char[nRequiredLen];
382 memset(pbuff, 0, nRequiredLen);
383 int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
384 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
385 delete[] pbuff;
386 return L"";
387 }
388 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
389 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
390 (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
391 bsRet.GetLength() / sizeof(unsigned short));
392 delete[] pbuff;
393 return wsRet;
394}
395
dsinclair735606d2016-10-05 15:47:02 -0700396void CPDFSDK_FormFillEnvironment::GotoURL(FPDF_DOCUMENT document,
397 const CFX_WideStringC& wsURL) {
dsinclair577ad2c2016-09-22 10:20:43 -0700398 if (!m_pInfo || !m_pInfo->FFI_GotoURL)
399 return;
400
401 CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
402 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
403 m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
404 bsTo.ReleaseBuffer();
405}
406
dsinclair735606d2016-10-05 15:47:02 -0700407void CPDFSDK_FormFillEnvironment::GetPageViewRect(FPDF_PAGE page,
408 FS_RECTF& dstRect) {
dsinclair577ad2c2016-09-22 10:20:43 -0700409 if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect)
410 return;
411
412 double left;
413 double top;
414 double right;
415 double bottom;
416 m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
417
418 dstRect.left = static_cast<float>(left);
419 dstRect.top = static_cast<float>(top < bottom ? bottom : top);
420 dstRect.bottom = static_cast<float>(top < bottom ? top : bottom);
421 dstRect.right = static_cast<float>(right);
422}
423
dsinclair735606d2016-10-05 15:47:02 -0700424FX_BOOL CPDFSDK_FormFillEnvironment::PopupMenu(FPDF_PAGE page,
425 FPDF_WIDGET hWidget,
426 int menuFlag,
427 CFX_PointF pt) {
dsinclair577ad2c2016-09-22 10:20:43 -0700428 if (!m_pInfo || !m_pInfo->FFI_PopupMenu)
429 return FALSE;
430 return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y);
431}
432
dsinclair735606d2016-10-05 15:47:02 -0700433void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg,
434 FPDF_WIDESTRING Title,
435 int Type,
436 int Icon) {
dsinclair577ad2c2016-09-22 10:20:43 -0700437 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
438 m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type,
439 Icon);
440 }
441}
442
dsinclair735606d2016-10-05 15:47:02 -0700443void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler,
444 FPDF_WIDESTRING pTo,
445 FPDF_WIDESTRING pSubject,
446 FPDF_WIDESTRING pCC,
447 FPDF_WIDESTRING pBcc,
448 FPDF_WIDESTRING pMsg) {
dsinclair577ad2c2016-09-22 10:20:43 -0700449 if (m_pInfo && m_pInfo->FFI_EmailTo)
450 m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
451}
452
dsinclair735606d2016-10-05 15:47:02 -0700453void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler,
454 int fileFlag,
455 FPDF_WIDESTRING uploadTo) {
dsinclair577ad2c2016-09-22 10:20:43 -0700456 if (m_pInfo && m_pInfo->FFI_UploadTo)
457 m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
458}
459
dsinclair735606d2016-10-05 15:47:02 -0700460FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType,
461 FPDF_WIDESTRING wsURL,
462 const char* mode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700463 if (m_pInfo && m_pInfo->FFI_OpenFile)
464 return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
465 return nullptr;
466}
467
dsinclair735606d2016-10-05 15:47:02 -0700468IFX_FileRead* CPDFSDK_FormFillEnvironment::DownloadFromURL(
469 const FX_WCHAR* url) {
dsinclair577ad2c2016-09-22 10:20:43 -0700470 if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL)
471 return nullptr;
472
473 CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
474 FPDF_WIDESTRING wsURL =
475 (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
476
477 FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
478
479 return new CFPDF_FileStream(fileHandler);
480}
481
dsinclair735606d2016-10-05 15:47:02 -0700482CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
dsinclair577ad2c2016-09-22 10:20:43 -0700483 const FX_WCHAR* wsURL,
484 const FX_WCHAR* wsData,
485 const FX_WCHAR* wsContentType,
486 const FX_WCHAR* wsEncode,
487 const FX_WCHAR* wsHeader) {
488 if (!m_pInfo || !m_pInfo->FFI_PostRequestURL)
489 return L"";
490
491 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
492 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
493
494 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
495 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
496
497 CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
498 FPDF_WIDESTRING contentType =
499 (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
500
501 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
502 FPDF_WIDESTRING encode =
503 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
504
505 CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
506 FPDF_WIDESTRING header =
507 (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
508
509 FPDF_BSTR response;
510 FPDF_BStr_Init(&response);
511 m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header,
512 &response);
513
514 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
515 (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING));
516 FPDF_BStr_Clear(&response);
517
518 return wsRet;
519}
520
dsinclair735606d2016-10-05 15:47:02 -0700521FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const FX_WCHAR* wsURL,
522 const FX_WCHAR* wsData,
523 const FX_WCHAR* wsEncode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700524 if (!m_pInfo || !m_pInfo->FFI_PutRequestURL)
525 return FALSE;
526
527 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
528 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
529
530 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
531 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
532
533 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
534 FPDF_WIDESTRING encode =
535 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
536
537 return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
538}
539
dsinclair735606d2016-10-05 15:47:02 -0700540CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
dsinclair577ad2c2016-09-22 10:20:43 -0700541 if (!m_pInfo || !m_pInfo->FFI_GetLanguage)
542 return L"";
543
544 int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
545 if (nRequiredLen <= 0)
546 return L"";
547
548 char* pbuff = new char[nRequiredLen];
549 memset(pbuff, 0, nRequiredLen);
550 int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
551 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
552 delete[] pbuff;
553 return L"";
554 }
555 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
556 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
557 (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()),
558 bsRet.GetLength() / sizeof(FPDF_WIDESTRING));
559 delete[] pbuff;
560 return wsRet;
561}
562
dsinclair735606d2016-10-05 15:47:02 -0700563void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
564 uint32_t dwEventType) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700565 if (m_pInfo && m_pInfo->FFI_PageEvent)
566 m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
567}
568#endif // PDF_ENABLE_XFA
dsinclair7cbe68e2016-10-12 11:56:23 -0700569
570void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
571 for (auto& it : m_pageMap) {
572 if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
573 KillFocusAnnot(0);
574 }
575}
576
577CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
578 UnderlyingPageType* pUnderlyingPage,
dsinclair6c659ab2016-10-12 13:41:38 -0700579 bool renew) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700580 auto it = m_pageMap.find(pUnderlyingPage);
581 if (it != m_pageMap.end())
dsinclair6c659ab2016-10-12 13:41:38 -0700582 return it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700583
dsinclair6c659ab2016-10-12 13:41:38 -0700584 if (!renew)
dsinclair7cbe68e2016-10-12 11:56:23 -0700585 return nullptr;
586
587 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
dsinclair6c659ab2016-10-12 13:41:38 -0700588 m_pageMap[pUnderlyingPage].reset(pPageView);
dsinclair7cbe68e2016-10-12 11:56:23 -0700589 // Delay to load all the annotations, to avoid endless loop.
590 pPageView->LoadFXAnnots();
591 return pPageView;
592}
593
594CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
595 UnderlyingPageType* pPage =
596 UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc));
597 return pPage ? GetPageView(pPage, true) : nullptr;
598}
599
600CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
601 UnderlyingPageType* pTempPage =
602 UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
603 if (!pTempPage)
604 return nullptr;
605
606 auto it = m_pageMap.find(pTempPage);
dsinclair6c659ab2016-10-12 13:41:38 -0700607 return it != m_pageMap.end() ? it->second.get() : nullptr;
dsinclair7cbe68e2016-10-12 11:56:23 -0700608}
609
610void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
611 CPDF_Document* pPDFDoc = GetPDFDocument();
612 CPDF_DocJSActions docJS(pPDFDoc);
613 int iCount = docJS.CountJSActions();
614 if (iCount < 1)
615 return;
616 for (int i = 0; i < iCount; i++) {
617 CFX_ByteString csJSName;
618 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
619 if (GetActionHander()) {
620 GetActionHander()->DoAction_JavaScript(
621 jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this);
622 }
623 }
624}
625
626FX_BOOL CPDFSDK_FormFillEnvironment::ProcOpenAction() {
627 if (!m_pUnderlyingDoc)
628 return FALSE;
629
630 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
631 if (!pRoot)
632 return FALSE;
633
634 CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
635 if (!pOpenAction)
636 pOpenAction = pRoot->GetArrayFor("OpenAction");
637
638 if (!pOpenAction)
639 return FALSE;
640
641 if (pOpenAction->IsArray())
642 return TRUE;
643
644 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
645 CPDF_Action action(pDict);
646 if (GetActionHander())
647 GetActionHander()->DoAction_DocOpen(action, this);
648 return TRUE;
649 }
650 return FALSE;
651}
652
653void CPDFSDK_FormFillEnvironment::RemovePageView(
654 UnderlyingPageType* pUnderlyingPage) {
655 auto it = m_pageMap.find(pUnderlyingPage);
656 if (it == m_pageMap.end())
657 return;
658
dsinclair6c659ab2016-10-12 13:41:38 -0700659 CPDFSDK_PageView* pPageView = it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700660 if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
661 return;
662
663 // Mark the page view so we do not come into |RemovePageView| a second
664 // time while we're in the process of removing.
665 pPageView->SetBeingDestroyed();
666
667 // This must happen before we remove |pPageView| from the map because
668 // |KillFocusAnnot| can call into the |GetPage| method which will
669 // look for this page view in the map, if it doesn't find it a new one will
670 // be created. We then have two page views pointing to the same page and
671 // bad things happen.
672 if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
673 KillFocusAnnot(0);
674
675 // Remove the page from the map to make sure we don't accidentally attempt
676 // to use the |pPageView| while we're cleaning it up.
677 m_pageMap.erase(it);
dsinclair7cbe68e2016-10-12 11:56:23 -0700678}
679
680UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
681 return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
682}
683
684CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() {
685 if (!m_pInterForm)
686 m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this);
687 return m_pInterForm.get();
688}
689
690void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
691 CPDFSDK_Annot* pAnnot) {
692 for (const auto& it : m_pageMap) {
dsinclair6c659ab2016-10-12 13:41:38 -0700693 CPDFSDK_PageView* pPageView = it.second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700694 if (pPageView != pSender)
695 pPageView->UpdateView(pAnnot);
696 }
697}
698
699FX_BOOL CPDFSDK_FormFillEnvironment::SetFocusAnnot(
700 CPDFSDK_Annot::ObservedPtr* pAnnot) {
701 if (m_bBeingDestroyed)
702 return FALSE;
703 if (m_pFocusAnnot == *pAnnot)
704 return TRUE;
705 if (m_pFocusAnnot && !KillFocusAnnot(0))
706 return FALSE;
707 if (!*pAnnot)
708 return FALSE;
709
710#ifdef PDF_ENABLE_XFA
711 CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
712#endif // PDF_ENABLE_XFA
713 CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
714 if (pPageView && pPageView->IsValid()) {
715 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
716 if (!m_pFocusAnnot) {
717#ifdef PDF_ENABLE_XFA
718 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
719 return FALSE;
720#endif // PDF_ENABLE_XFA
721 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
722 return FALSE;
723 if (!m_pFocusAnnot) {
724 m_pFocusAnnot.Reset(pAnnot->Get());
725 return TRUE;
726 }
727 }
728 }
729 return FALSE;
730}
731
732FX_BOOL CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
733 if (m_pFocusAnnot) {
734 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
735 CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
736 m_pFocusAnnot.Reset();
737
738#ifdef PDF_ENABLE_XFA
739 CPDFSDK_Annot::ObservedPtr pNull;
740 if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
741 return FALSE;
742#endif // PDF_ENABLE_XFA
743
744 if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
745 if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
746 CPDFSDK_Widget* pWidget =
747 static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
748 int nFieldType = pWidget->GetFieldType();
749 if (FIELDTYPE_TEXTFIELD == nFieldType ||
750 FIELDTYPE_COMBOBOX == nFieldType) {
751 OnSetFieldInputFocus(nullptr, 0, FALSE);
752 }
753 }
754 if (!m_pFocusAnnot)
755 return TRUE;
756 } else {
757 m_pFocusAnnot.Reset(pFocusAnnot.Get());
758 }
759 }
760 return FALSE;
761}
762
763FX_BOOL CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) {
764 return GetPDFDocument()->GetUserPermissions() & nFlag;
765}