blob: 1f39de9069b1851217b3db4e115b4bbe3c1dddc4 [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
dsinclaira282c732016-10-13 12:14:34 -070061#ifdef PDF_ENABLE_XFA
62 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
63 if (pProvider->m_pFormFillEnvList.GetSize() == 0)
64 pProvider->SetJavaScriptInitialized(FALSE);
65#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -070066 if (m_pInfo && m_pInfo->Release)
67 m_pInfo->Release(m_pInfo);
68}
69
dsinclair735606d2016-10-05 15:47:02 -070070int CPDFSDK_FormFillEnvironment::JS_appAlert(const FX_WCHAR* Msg,
71 const FX_WCHAR* Title,
72 uint32_t Type,
73 uint32_t Icon) {
dsinclairf34518b2016-09-13 12:03:48 -070074 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
75 !m_pInfo->m_pJsPlatform->app_alert) {
76 return -1;
77 }
78 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
79 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
80 return m_pInfo->m_pJsPlatform->app_alert(
81 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
82 AsFPDFWideString(&bsTitle), Type, Icon);
83}
84
dsinclair735606d2016-10-05 15:47:02 -070085int CPDFSDK_FormFillEnvironment::JS_appResponse(const FX_WCHAR* Question,
86 const FX_WCHAR* Title,
87 const FX_WCHAR* Default,
88 const FX_WCHAR* cLabel,
89 FPDF_BOOL bPassword,
90 void* response,
91 int length) {
dsinclairf34518b2016-09-13 12:03:48 -070092 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
93 !m_pInfo->m_pJsPlatform->app_response) {
94 return -1;
95 }
96 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
97 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
98 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
99 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
100 return m_pInfo->m_pJsPlatform->app_response(
101 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
102 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
103 AsFPDFWideString(&bsLabel), bPassword, response, length);
104}
105
dsinclair735606d2016-10-05 15:47:02 -0700106void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) {
dsinclairf34518b2016-09-13 12:03:48 -0700107 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
108 !m_pInfo->m_pJsPlatform->app_beep) {
109 return;
110 }
111 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
112}
113
dsinclair735606d2016-10-05 15:47:02 -0700114CFX_WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() {
dsinclairf34518b2016-09-13 12:03:48 -0700115 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
116 !m_pInfo->m_pJsPlatform->Field_browse) {
117 return CFX_WideString();
118 }
119 const int nRequiredLen =
120 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
121 if (nRequiredLen <= 0)
122 return CFX_WideString();
123
124 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
125 memset(pBuff.get(), 0, nRequiredLen);
126 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
127 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
128 if (nActualLen <= 0 || nActualLen > nRequiredLen)
129 return CFX_WideString();
130
131 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
132}
133
dsinclair735606d2016-10-05 15:47:02 -0700134CFX_WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() {
dsinclairf34518b2016-09-13 12:03:48 -0700135 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
136 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
137 return CFX_WideString();
138 }
139 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
140 m_pInfo->m_pJsPlatform, nullptr, 0);
141 if (nRequiredLen <= 0)
142 return CFX_WideString();
143
144 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
145 memset(pBuff.get(), 0, nRequiredLen);
146 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
147 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
148 if (nActualLen <= 0 || nActualLen > nRequiredLen)
149 return CFX_WideString();
150
151 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
152}
153
dsinclair735606d2016-10-05 15:47:02 -0700154void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData,
155 int length,
156 const FX_WCHAR* URL) {
dsinclairf34518b2016-09-13 12:03:48 -0700157 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
158 !m_pInfo->m_pJsPlatform->Doc_submitForm) {
159 return;
160 }
161 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
162 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
163 length,
164 AsFPDFWideString(&bsDestination));
165}
166
dsinclair735606d2016-10-05 15:47:02 -0700167void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData,
168 int length,
169 FPDF_BOOL bUI,
170 const FX_WCHAR* To,
171 const FX_WCHAR* Subject,
172 const FX_WCHAR* CC,
173 const FX_WCHAR* BCC,
174 const FX_WCHAR* Msg) {
dsinclairf34518b2016-09-13 12:03:48 -0700175 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
176 !m_pInfo->m_pJsPlatform->Doc_mail) {
177 return;
178 }
179 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
180 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
181 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode();
182 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
183 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
184 m_pInfo->m_pJsPlatform->Doc_mail(
185 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
186 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
187 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg));
188}
189
dsinclair735606d2016-10-05 15:47:02 -0700190void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI,
191 int nStart,
192 int nEnd,
193 FPDF_BOOL bSilent,
194 FPDF_BOOL bShrinkToFit,
195 FPDF_BOOL bPrintAsImage,
196 FPDF_BOOL bReverse,
197 FPDF_BOOL bAnnotations) {
dsinclairf34518b2016-09-13 12:03:48 -0700198 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
199 !m_pInfo->m_pJsPlatform->Doc_print) {
200 return;
201 }
202 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd,
203 bSilent, bShrinkToFit, bPrintAsImage,
204 bReverse, bAnnotations);
205}
206
dsinclair735606d2016-10-05 15:47:02 -0700207void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) {
dsinclairf34518b2016-09-13 12:03:48 -0700208 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
209 !m_pInfo->m_pJsPlatform->Doc_gotoPage) {
210 return;
211 }
212 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
213}
214
dsinclair735606d2016-10-05 15:47:02 -0700215IJS_Runtime* CPDFSDK_FormFillEnvironment::GetJSRuntime() {
dsinclairf34518b2016-09-13 12:03:48 -0700216 if (!IsJSInitiated())
217 return nullptr;
218 if (!m_pJSRuntime)
219 m_pJSRuntime.reset(IJS_Runtime::Create(this));
220 return m_pJSRuntime.get();
221}
222
dsinclair735606d2016-10-05 15:47:02 -0700223CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() {
dsinclairf34518b2016-09-13 12:03:48 -0700224 if (!m_pAnnotHandlerMgr)
tsepez36eb4bd2016-10-03 15:24:27 -0700225 m_pAnnotHandlerMgr = pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(this);
dsinclairf34518b2016-09-13 12:03:48 -0700226 return m_pAnnotHandlerMgr.get();
227}
228
dsinclair735606d2016-10-05 15:47:02 -0700229CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHander() {
dsinclairf34518b2016-09-13 12:03:48 -0700230 if (!m_pActionHandler)
tsepez36eb4bd2016-10-03 15:24:27 -0700231 m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>();
dsinclairf34518b2016-09-13 12:03:48 -0700232 return m_pActionHandler.get();
233}
234
dsinclair735606d2016-10-05 15:47:02 -0700235CFFL_InteractiveFormFiller*
236CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() {
dsinclairb94d7c92016-09-21 12:07:00 -0700237 if (!m_pFormFiller)
tsepez36eb4bd2016-10-03 15:24:27 -0700238 m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this);
dsinclairb94d7c92016-09-21 12:07:00 -0700239 return m_pFormFiller.get();
dsinclairf34518b2016-09-13 12:03:48 -0700240}
dsinclair577ad2c2016-09-22 10:20:43 -0700241
dsinclair735606d2016-10-05 15:47:02 -0700242void CPDFSDK_FormFillEnvironment::Invalidate(FPDF_PAGE page,
dsinclair577ad2c2016-09-22 10:20:43 -0700243 double left,
244 double top,
245 double right,
246 double bottom) {
dsinclair735606d2016-10-05 15:47:02 -0700247 if (m_pInfo && m_pInfo->FFI_Invalidate)
248 m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom);
249}
250
251void CPDFSDK_FormFillEnvironment::OutputSelectedRect(FPDF_PAGE page,
252 double left,
253 double top,
254 double right,
255 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700256 if (m_pInfo && m_pInfo->FFI_OutputSelectedRect)
257 m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom);
258}
259
dsinclair735606d2016-10-05 15:47:02 -0700260void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) {
dsinclair577ad2c2016-09-22 10:20:43 -0700261 if (m_pInfo && m_pInfo->FFI_SetCursor)
262 m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
263}
264
dsinclair735606d2016-10-05 15:47:02 -0700265int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse,
266 TimerCallback lpTimerFunc) {
dsinclair577ad2c2016-09-22 10:20:43 -0700267 if (m_pInfo && m_pInfo->FFI_SetTimer)
268 return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
269 return -1;
270}
271
dsinclair735606d2016-10-05 15:47:02 -0700272void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) {
dsinclair577ad2c2016-09-22 10:20:43 -0700273 if (m_pInfo && m_pInfo->FFI_KillTimer)
274 m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
275}
276
dsinclair735606d2016-10-05 15:47:02 -0700277FX_SYSTEMTIME CPDFSDK_FormFillEnvironment::GetLocalTime() const {
dsinclair577ad2c2016-09-22 10:20:43 -0700278 FX_SYSTEMTIME fxtime;
279 if (!m_pInfo || !m_pInfo->FFI_GetLocalTime)
280 return fxtime;
281
282 FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo);
283 fxtime.wDay = systime.wDay;
284 fxtime.wDayOfWeek = systime.wDayOfWeek;
285 fxtime.wHour = systime.wHour;
286 fxtime.wMilliseconds = systime.wMilliseconds;
287 fxtime.wMinute = systime.wMinute;
288 fxtime.wMonth = systime.wMonth;
289 fxtime.wSecond = systime.wSecond;
290 fxtime.wYear = systime.wYear;
291 return fxtime;
292}
293
dsinclair735606d2016-10-05 15:47:02 -0700294void CPDFSDK_FormFillEnvironment::OnChange() {
dsinclair577ad2c2016-09-22 10:20:43 -0700295 if (m_pInfo && m_pInfo->FFI_OnChange)
296 m_pInfo->FFI_OnChange(m_pInfo);
297}
298
dsinclair735606d2016-10-05 15:47:02 -0700299FX_BOOL CPDFSDK_FormFillEnvironment::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700300 return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0;
301}
302
dsinclair735606d2016-10-05 15:47:02 -0700303FX_BOOL CPDFSDK_FormFillEnvironment::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700304 return (nFlag & FWL_EVENTFLAG_ControlKey) != 0;
305}
306
dsinclair735606d2016-10-05 15:47:02 -0700307FX_BOOL CPDFSDK_FormFillEnvironment::IsALTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700308 return (nFlag & FWL_EVENTFLAG_AltKey) != 0;
309}
310
dsinclair735606d2016-10-05 15:47:02 -0700311FPDF_PAGE CPDFSDK_FormFillEnvironment::GetPage(FPDF_DOCUMENT document,
312 int nPageIndex) {
dsinclair577ad2c2016-09-22 10:20:43 -0700313 if (m_pInfo && m_pInfo->FFI_GetPage)
314 return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
315 return nullptr;
316}
317
dsinclair735606d2016-10-05 15:47:02 -0700318FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700319 if (m_pInfo && m_pInfo->FFI_GetCurrentPage)
320 return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
321 return nullptr;
322}
323
dsinclair735606d2016-10-05 15:47:02 -0700324void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(
325 const FX_CHAR* namedAction) {
dsinclair577ad2c2016-09-22 10:20:43 -0700326 if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
327 m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
328}
329
dsinclair735606d2016-10-05 15:47:02 -0700330void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus(
331 FPDF_WIDESTRING focusText,
332 FPDF_DWORD nTextLen,
333 FX_BOOL bFocus) {
dsinclair577ad2c2016-09-22 10:20:43 -0700334 if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
335 m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
336}
337
dsinclair735606d2016-10-05 15:47:02 -0700338void CPDFSDK_FormFillEnvironment::DoURIAction(const FX_CHAR* bsURI) {
dsinclair577ad2c2016-09-22 10:20:43 -0700339 if (m_pInfo && m_pInfo->FFI_DoURIAction)
340 m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
341}
342
dsinclair735606d2016-10-05 15:47:02 -0700343void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex,
344 int zoomMode,
345 float* fPosArray,
346 int sizeOfArray) {
dsinclair577ad2c2016-09-22 10:20:43 -0700347 if (m_pInfo && m_pInfo->FFI_DoGoToAction) {
348 m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray,
349 sizeOfArray);
350 }
351}
352
353#ifdef PDF_ENABLE_XFA
dsinclair735606d2016-10-05 15:47:02 -0700354void CPDFSDK_FormFillEnvironment::DisplayCaret(FPDF_PAGE page,
355 FPDF_BOOL bVisible,
356 double left,
357 double top,
358 double right,
359 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700360 if (m_pInfo && m_pInfo->FFI_DisplayCaret) {
361 m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right,
362 bottom);
363 }
364}
365
dsinclair735606d2016-10-05 15:47:02 -0700366int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700367 if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex)
368 return -1;
369 return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
370}
371
dsinclair735606d2016-10-05 15:47:02 -0700372void CPDFSDK_FormFillEnvironment::SetCurrentPage(FPDF_DOCUMENT document,
373 int iCurPage) {
dsinclair577ad2c2016-09-22 10:20:43 -0700374 if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
375 m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
376}
377
dsinclair735606d2016-10-05 15:47:02 -0700378CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() {
dsinclair577ad2c2016-09-22 10:20:43 -0700379 if (!m_pInfo || !m_pInfo->FFI_GetPlatform)
380 return L"";
381
382 int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0);
383 if (nRequiredLen <= 0)
384 return L"";
385
386 char* pbuff = new char[nRequiredLen];
387 memset(pbuff, 0, nRequiredLen);
388 int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
389 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
390 delete[] pbuff;
391 return L"";
392 }
393 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
394 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
395 (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
396 bsRet.GetLength() / sizeof(unsigned short));
397 delete[] pbuff;
398 return wsRet;
399}
400
dsinclair735606d2016-10-05 15:47:02 -0700401void CPDFSDK_FormFillEnvironment::GotoURL(FPDF_DOCUMENT document,
402 const CFX_WideStringC& wsURL) {
dsinclair577ad2c2016-09-22 10:20:43 -0700403 if (!m_pInfo || !m_pInfo->FFI_GotoURL)
404 return;
405
406 CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
407 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
408 m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
409 bsTo.ReleaseBuffer();
410}
411
dsinclair735606d2016-10-05 15:47:02 -0700412void CPDFSDK_FormFillEnvironment::GetPageViewRect(FPDF_PAGE page,
413 FS_RECTF& dstRect) {
dsinclair577ad2c2016-09-22 10:20:43 -0700414 if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect)
415 return;
416
417 double left;
418 double top;
419 double right;
420 double bottom;
421 m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
422
423 dstRect.left = static_cast<float>(left);
424 dstRect.top = static_cast<float>(top < bottom ? bottom : top);
425 dstRect.bottom = static_cast<float>(top < bottom ? top : bottom);
426 dstRect.right = static_cast<float>(right);
427}
428
dsinclair735606d2016-10-05 15:47:02 -0700429FX_BOOL CPDFSDK_FormFillEnvironment::PopupMenu(FPDF_PAGE page,
430 FPDF_WIDGET hWidget,
431 int menuFlag,
432 CFX_PointF pt) {
tsepezdc0401a2016-10-28 13:10:35 -0700433 return m_pInfo && m_pInfo->FFI_PopupMenu &&
434 m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y);
dsinclair577ad2c2016-09-22 10:20:43 -0700435}
436
dsinclair735606d2016-10-05 15:47:02 -0700437void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg,
438 FPDF_WIDESTRING Title,
439 int Type,
440 int Icon) {
dsinclair577ad2c2016-09-22 10:20:43 -0700441 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
442 m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type,
443 Icon);
444 }
445}
446
dsinclair735606d2016-10-05 15:47:02 -0700447void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler,
448 FPDF_WIDESTRING pTo,
449 FPDF_WIDESTRING pSubject,
450 FPDF_WIDESTRING pCC,
451 FPDF_WIDESTRING pBcc,
452 FPDF_WIDESTRING pMsg) {
dsinclair577ad2c2016-09-22 10:20:43 -0700453 if (m_pInfo && m_pInfo->FFI_EmailTo)
454 m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
455}
456
dsinclair735606d2016-10-05 15:47:02 -0700457void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler,
458 int fileFlag,
459 FPDF_WIDESTRING uploadTo) {
dsinclair577ad2c2016-09-22 10:20:43 -0700460 if (m_pInfo && m_pInfo->FFI_UploadTo)
461 m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
462}
463
dsinclair735606d2016-10-05 15:47:02 -0700464FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType,
465 FPDF_WIDESTRING wsURL,
466 const char* mode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700467 if (m_pInfo && m_pInfo->FFI_OpenFile)
468 return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
469 return nullptr;
470}
471
tsepezad2441e2016-10-24 10:19:11 -0700472IFX_SeekableReadStream* CPDFSDK_FormFillEnvironment::DownloadFromURL(
dsinclair735606d2016-10-05 15:47:02 -0700473 const FX_WCHAR* url) {
dsinclair577ad2c2016-09-22 10:20:43 -0700474 if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL)
475 return nullptr;
476
477 CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
478 FPDF_WIDESTRING wsURL =
479 (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
480
481 FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
482
483 return new CFPDF_FileStream(fileHandler);
484}
485
dsinclair735606d2016-10-05 15:47:02 -0700486CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
dsinclair577ad2c2016-09-22 10:20:43 -0700487 const FX_WCHAR* wsURL,
488 const FX_WCHAR* wsData,
489 const FX_WCHAR* wsContentType,
490 const FX_WCHAR* wsEncode,
491 const FX_WCHAR* wsHeader) {
492 if (!m_pInfo || !m_pInfo->FFI_PostRequestURL)
493 return L"";
494
495 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
496 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
497
498 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
499 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
500
501 CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
502 FPDF_WIDESTRING contentType =
503 (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
504
505 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
506 FPDF_WIDESTRING encode =
507 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
508
509 CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
510 FPDF_WIDESTRING header =
511 (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
512
513 FPDF_BSTR response;
514 FPDF_BStr_Init(&response);
515 m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header,
516 &response);
517
518 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
519 (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING));
520 FPDF_BStr_Clear(&response);
521
522 return wsRet;
523}
524
dsinclair735606d2016-10-05 15:47:02 -0700525FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const FX_WCHAR* wsURL,
526 const FX_WCHAR* wsData,
527 const FX_WCHAR* wsEncode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700528 if (!m_pInfo || !m_pInfo->FFI_PutRequestURL)
529 return FALSE;
530
531 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
532 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
533
534 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
535 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
536
537 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
538 FPDF_WIDESTRING encode =
539 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
540
541 return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
542}
543
dsinclair735606d2016-10-05 15:47:02 -0700544CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
dsinclair577ad2c2016-09-22 10:20:43 -0700545 if (!m_pInfo || !m_pInfo->FFI_GetLanguage)
546 return L"";
547
548 int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
549 if (nRequiredLen <= 0)
550 return L"";
551
552 char* pbuff = new char[nRequiredLen];
553 memset(pbuff, 0, nRequiredLen);
554 int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
555 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
556 delete[] pbuff;
557 return L"";
558 }
559 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
560 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
561 (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()),
562 bsRet.GetLength() / sizeof(FPDF_WIDESTRING));
563 delete[] pbuff;
564 return wsRet;
565}
566
dsinclair735606d2016-10-05 15:47:02 -0700567void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
568 uint32_t dwEventType) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700569 if (m_pInfo && m_pInfo->FFI_PageEvent)
570 m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
571}
572#endif // PDF_ENABLE_XFA
dsinclair7cbe68e2016-10-12 11:56:23 -0700573
574void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
575 for (auto& it : m_pageMap) {
576 if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
577 KillFocusAnnot(0);
578 }
579}
580
581CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
582 UnderlyingPageType* pUnderlyingPage,
dsinclair6c659ab2016-10-12 13:41:38 -0700583 bool renew) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700584 auto it = m_pageMap.find(pUnderlyingPage);
585 if (it != m_pageMap.end())
dsinclair6c659ab2016-10-12 13:41:38 -0700586 return it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700587
dsinclair6c659ab2016-10-12 13:41:38 -0700588 if (!renew)
dsinclair7cbe68e2016-10-12 11:56:23 -0700589 return nullptr;
590
591 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
dsinclair6c659ab2016-10-12 13:41:38 -0700592 m_pageMap[pUnderlyingPage].reset(pPageView);
dsinclair7cbe68e2016-10-12 11:56:23 -0700593 // Delay to load all the annotations, to avoid endless loop.
594 pPageView->LoadFXAnnots();
595 return pPageView;
596}
597
598CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
599 UnderlyingPageType* pPage =
600 UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc));
601 return pPage ? GetPageView(pPage, true) : nullptr;
602}
603
604CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
605 UnderlyingPageType* pTempPage =
606 UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
607 if (!pTempPage)
608 return nullptr;
609
610 auto it = m_pageMap.find(pTempPage);
dsinclair6c659ab2016-10-12 13:41:38 -0700611 return it != m_pageMap.end() ? it->second.get() : nullptr;
dsinclair7cbe68e2016-10-12 11:56:23 -0700612}
613
614void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
615 CPDF_Document* pPDFDoc = GetPDFDocument();
616 CPDF_DocJSActions docJS(pPDFDoc);
617 int iCount = docJS.CountJSActions();
618 if (iCount < 1)
619 return;
620 for (int i = 0; i < iCount; i++) {
621 CFX_ByteString csJSName;
622 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
623 if (GetActionHander()) {
624 GetActionHander()->DoAction_JavaScript(
625 jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this);
626 }
627 }
628}
629
630FX_BOOL CPDFSDK_FormFillEnvironment::ProcOpenAction() {
631 if (!m_pUnderlyingDoc)
632 return FALSE;
633
634 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
635 if (!pRoot)
636 return FALSE;
637
638 CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
639 if (!pOpenAction)
640 pOpenAction = pRoot->GetArrayFor("OpenAction");
641
642 if (!pOpenAction)
643 return FALSE;
644
645 if (pOpenAction->IsArray())
646 return TRUE;
647
648 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
649 CPDF_Action action(pDict);
650 if (GetActionHander())
651 GetActionHander()->DoAction_DocOpen(action, this);
652 return TRUE;
653 }
654 return FALSE;
655}
656
657void CPDFSDK_FormFillEnvironment::RemovePageView(
658 UnderlyingPageType* pUnderlyingPage) {
659 auto it = m_pageMap.find(pUnderlyingPage);
660 if (it == m_pageMap.end())
661 return;
662
dsinclair6c659ab2016-10-12 13:41:38 -0700663 CPDFSDK_PageView* pPageView = it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700664 if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
665 return;
666
667 // Mark the page view so we do not come into |RemovePageView| a second
668 // time while we're in the process of removing.
669 pPageView->SetBeingDestroyed();
670
671 // This must happen before we remove |pPageView| from the map because
672 // |KillFocusAnnot| can call into the |GetPage| method which will
673 // look for this page view in the map, if it doesn't find it a new one will
674 // be created. We then have two page views pointing to the same page and
675 // bad things happen.
676 if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
677 KillFocusAnnot(0);
678
679 // Remove the page from the map to make sure we don't accidentally attempt
680 // to use the |pPageView| while we're cleaning it up.
681 m_pageMap.erase(it);
dsinclair7cbe68e2016-10-12 11:56:23 -0700682}
683
684UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
685 return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
686}
687
688CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() {
689 if (!m_pInterForm)
690 m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this);
691 return m_pInterForm.get();
692}
693
694void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
695 CPDFSDK_Annot* pAnnot) {
696 for (const auto& it : m_pageMap) {
dsinclair6c659ab2016-10-12 13:41:38 -0700697 CPDFSDK_PageView* pPageView = it.second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700698 if (pPageView != pSender)
699 pPageView->UpdateView(pAnnot);
700 }
701}
702
703FX_BOOL CPDFSDK_FormFillEnvironment::SetFocusAnnot(
704 CPDFSDK_Annot::ObservedPtr* pAnnot) {
705 if (m_bBeingDestroyed)
706 return FALSE;
707 if (m_pFocusAnnot == *pAnnot)
708 return TRUE;
709 if (m_pFocusAnnot && !KillFocusAnnot(0))
710 return FALSE;
711 if (!*pAnnot)
712 return FALSE;
713
714#ifdef PDF_ENABLE_XFA
715 CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
716#endif // PDF_ENABLE_XFA
717 CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
718 if (pPageView && pPageView->IsValid()) {
719 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
720 if (!m_pFocusAnnot) {
721#ifdef PDF_ENABLE_XFA
722 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
723 return FALSE;
724#endif // PDF_ENABLE_XFA
725 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
726 return FALSE;
727 if (!m_pFocusAnnot) {
728 m_pFocusAnnot.Reset(pAnnot->Get());
729 return TRUE;
730 }
731 }
732 }
733 return FALSE;
734}
735
736FX_BOOL CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
737 if (m_pFocusAnnot) {
738 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
739 CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
740 m_pFocusAnnot.Reset();
741
742#ifdef PDF_ENABLE_XFA
743 CPDFSDK_Annot::ObservedPtr pNull;
744 if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
745 return FALSE;
746#endif // PDF_ENABLE_XFA
747
748 if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
749 if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
750 CPDFSDK_Widget* pWidget =
751 static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
752 int nFieldType = pWidget->GetFieldType();
753 if (FIELDTYPE_TEXTFIELD == nFieldType ||
754 FIELDTYPE_COMBOBOX == nFieldType) {
755 OnSetFieldInputFocus(nullptr, 0, FALSE);
756 }
757 }
758 if (!m_pFocusAnnot)
759 return TRUE;
760 } else {
761 m_pFocusAnnot.Reset(pFocusAnnot.Get());
762 }
763 }
764 return FALSE;
765}
766
767FX_BOOL CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) {
tsepez95e58342016-10-28 11:34:42 -0700768 return !!(GetPDFDocument()->GetUserPermissions() & nFlag);
dsinclair7cbe68e2016-10-12 11:56:23 -0700769}