blob: 71b83b26553cd9953ad740f40d4a0ff7ec24a980 [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) {
dsinclair577ad2c2016-09-22 10:20:43 -0700433 if (!m_pInfo || !m_pInfo->FFI_PopupMenu)
434 return FALSE;
435 return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y);
436}
437
dsinclair735606d2016-10-05 15:47:02 -0700438void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg,
439 FPDF_WIDESTRING Title,
440 int Type,
441 int Icon) {
dsinclair577ad2c2016-09-22 10:20:43 -0700442 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
443 m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type,
444 Icon);
445 }
446}
447
dsinclair735606d2016-10-05 15:47:02 -0700448void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler,
449 FPDF_WIDESTRING pTo,
450 FPDF_WIDESTRING pSubject,
451 FPDF_WIDESTRING pCC,
452 FPDF_WIDESTRING pBcc,
453 FPDF_WIDESTRING pMsg) {
dsinclair577ad2c2016-09-22 10:20:43 -0700454 if (m_pInfo && m_pInfo->FFI_EmailTo)
455 m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
456}
457
dsinclair735606d2016-10-05 15:47:02 -0700458void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler,
459 int fileFlag,
460 FPDF_WIDESTRING uploadTo) {
dsinclair577ad2c2016-09-22 10:20:43 -0700461 if (m_pInfo && m_pInfo->FFI_UploadTo)
462 m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
463}
464
dsinclair735606d2016-10-05 15:47:02 -0700465FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType,
466 FPDF_WIDESTRING wsURL,
467 const char* mode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700468 if (m_pInfo && m_pInfo->FFI_OpenFile)
469 return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
470 return nullptr;
471}
472
dsinclair735606d2016-10-05 15:47:02 -0700473IFX_FileRead* CPDFSDK_FormFillEnvironment::DownloadFromURL(
474 const FX_WCHAR* url) {
dsinclair577ad2c2016-09-22 10:20:43 -0700475 if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL)
476 return nullptr;
477
478 CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
479 FPDF_WIDESTRING wsURL =
480 (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
481
482 FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
483
484 return new CFPDF_FileStream(fileHandler);
485}
486
dsinclair735606d2016-10-05 15:47:02 -0700487CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
dsinclair577ad2c2016-09-22 10:20:43 -0700488 const FX_WCHAR* wsURL,
489 const FX_WCHAR* wsData,
490 const FX_WCHAR* wsContentType,
491 const FX_WCHAR* wsEncode,
492 const FX_WCHAR* wsHeader) {
493 if (!m_pInfo || !m_pInfo->FFI_PostRequestURL)
494 return L"";
495
496 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
497 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
498
499 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
500 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
501
502 CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
503 FPDF_WIDESTRING contentType =
504 (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
505
506 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
507 FPDF_WIDESTRING encode =
508 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
509
510 CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
511 FPDF_WIDESTRING header =
512 (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
513
514 FPDF_BSTR response;
515 FPDF_BStr_Init(&response);
516 m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header,
517 &response);
518
519 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
520 (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING));
521 FPDF_BStr_Clear(&response);
522
523 return wsRet;
524}
525
dsinclair735606d2016-10-05 15:47:02 -0700526FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const FX_WCHAR* wsURL,
527 const FX_WCHAR* wsData,
528 const FX_WCHAR* wsEncode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700529 if (!m_pInfo || !m_pInfo->FFI_PutRequestURL)
530 return FALSE;
531
532 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
533 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
534
535 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
536 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
537
538 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
539 FPDF_WIDESTRING encode =
540 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
541
542 return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
543}
544
dsinclair735606d2016-10-05 15:47:02 -0700545CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
dsinclair577ad2c2016-09-22 10:20:43 -0700546 if (!m_pInfo || !m_pInfo->FFI_GetLanguage)
547 return L"";
548
549 int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
550 if (nRequiredLen <= 0)
551 return L"";
552
553 char* pbuff = new char[nRequiredLen];
554 memset(pbuff, 0, nRequiredLen);
555 int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
556 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
557 delete[] pbuff;
558 return L"";
559 }
560 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
561 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
562 (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()),
563 bsRet.GetLength() / sizeof(FPDF_WIDESTRING));
564 delete[] pbuff;
565 return wsRet;
566}
567
dsinclair735606d2016-10-05 15:47:02 -0700568void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
569 uint32_t dwEventType) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700570 if (m_pInfo && m_pInfo->FFI_PageEvent)
571 m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
572}
573#endif // PDF_ENABLE_XFA
dsinclair7cbe68e2016-10-12 11:56:23 -0700574
575void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
576 for (auto& it : m_pageMap) {
577 if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
578 KillFocusAnnot(0);
579 }
580}
581
582CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
583 UnderlyingPageType* pUnderlyingPage,
dsinclair6c659ab2016-10-12 13:41:38 -0700584 bool renew) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700585 auto it = m_pageMap.find(pUnderlyingPage);
586 if (it != m_pageMap.end())
dsinclair6c659ab2016-10-12 13:41:38 -0700587 return it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700588
dsinclair6c659ab2016-10-12 13:41:38 -0700589 if (!renew)
dsinclair7cbe68e2016-10-12 11:56:23 -0700590 return nullptr;
591
592 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
dsinclair6c659ab2016-10-12 13:41:38 -0700593 m_pageMap[pUnderlyingPage].reset(pPageView);
dsinclair7cbe68e2016-10-12 11:56:23 -0700594 // Delay to load all the annotations, to avoid endless loop.
595 pPageView->LoadFXAnnots();
596 return pPageView;
597}
598
599CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
600 UnderlyingPageType* pPage =
601 UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc));
602 return pPage ? GetPageView(pPage, true) : nullptr;
603}
604
605CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
606 UnderlyingPageType* pTempPage =
607 UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
608 if (!pTempPage)
609 return nullptr;
610
611 auto it = m_pageMap.find(pTempPage);
dsinclair6c659ab2016-10-12 13:41:38 -0700612 return it != m_pageMap.end() ? it->second.get() : nullptr;
dsinclair7cbe68e2016-10-12 11:56:23 -0700613}
614
615void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
616 CPDF_Document* pPDFDoc = GetPDFDocument();
617 CPDF_DocJSActions docJS(pPDFDoc);
618 int iCount = docJS.CountJSActions();
619 if (iCount < 1)
620 return;
621 for (int i = 0; i < iCount; i++) {
622 CFX_ByteString csJSName;
623 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
624 if (GetActionHander()) {
625 GetActionHander()->DoAction_JavaScript(
626 jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this);
627 }
628 }
629}
630
631FX_BOOL CPDFSDK_FormFillEnvironment::ProcOpenAction() {
632 if (!m_pUnderlyingDoc)
633 return FALSE;
634
635 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
636 if (!pRoot)
637 return FALSE;
638
639 CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
640 if (!pOpenAction)
641 pOpenAction = pRoot->GetArrayFor("OpenAction");
642
643 if (!pOpenAction)
644 return FALSE;
645
646 if (pOpenAction->IsArray())
647 return TRUE;
648
649 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
650 CPDF_Action action(pDict);
651 if (GetActionHander())
652 GetActionHander()->DoAction_DocOpen(action, this);
653 return TRUE;
654 }
655 return FALSE;
656}
657
658void CPDFSDK_FormFillEnvironment::RemovePageView(
659 UnderlyingPageType* pUnderlyingPage) {
660 auto it = m_pageMap.find(pUnderlyingPage);
661 if (it == m_pageMap.end())
662 return;
663
dsinclair6c659ab2016-10-12 13:41:38 -0700664 CPDFSDK_PageView* pPageView = it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700665 if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
666 return;
667
668 // Mark the page view so we do not come into |RemovePageView| a second
669 // time while we're in the process of removing.
670 pPageView->SetBeingDestroyed();
671
672 // This must happen before we remove |pPageView| from the map because
673 // |KillFocusAnnot| can call into the |GetPage| method which will
674 // look for this page view in the map, if it doesn't find it a new one will
675 // be created. We then have two page views pointing to the same page and
676 // bad things happen.
677 if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
678 KillFocusAnnot(0);
679
680 // Remove the page from the map to make sure we don't accidentally attempt
681 // to use the |pPageView| while we're cleaning it up.
682 m_pageMap.erase(it);
dsinclair7cbe68e2016-10-12 11:56:23 -0700683}
684
685UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
686 return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
687}
688
689CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() {
690 if (!m_pInterForm)
691 m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this);
692 return m_pInterForm.get();
693}
694
695void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
696 CPDFSDK_Annot* pAnnot) {
697 for (const auto& it : m_pageMap) {
dsinclair6c659ab2016-10-12 13:41:38 -0700698 CPDFSDK_PageView* pPageView = it.second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700699 if (pPageView != pSender)
700 pPageView->UpdateView(pAnnot);
701 }
702}
703
704FX_BOOL CPDFSDK_FormFillEnvironment::SetFocusAnnot(
705 CPDFSDK_Annot::ObservedPtr* pAnnot) {
706 if (m_bBeingDestroyed)
707 return FALSE;
708 if (m_pFocusAnnot == *pAnnot)
709 return TRUE;
710 if (m_pFocusAnnot && !KillFocusAnnot(0))
711 return FALSE;
712 if (!*pAnnot)
713 return FALSE;
714
715#ifdef PDF_ENABLE_XFA
716 CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
717#endif // PDF_ENABLE_XFA
718 CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
719 if (pPageView && pPageView->IsValid()) {
720 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
721 if (!m_pFocusAnnot) {
722#ifdef PDF_ENABLE_XFA
723 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
724 return FALSE;
725#endif // PDF_ENABLE_XFA
726 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
727 return FALSE;
728 if (!m_pFocusAnnot) {
729 m_pFocusAnnot.Reset(pAnnot->Get());
730 return TRUE;
731 }
732 }
733 }
734 return FALSE;
735}
736
737FX_BOOL CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
738 if (m_pFocusAnnot) {
739 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
740 CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
741 m_pFocusAnnot.Reset();
742
743#ifdef PDF_ENABLE_XFA
744 CPDFSDK_Annot::ObservedPtr pNull;
745 if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
746 return FALSE;
747#endif // PDF_ENABLE_XFA
748
749 if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
750 if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
751 CPDFSDK_Widget* pWidget =
752 static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
753 int nFieldType = pWidget->GetFieldType();
754 if (FIELDTYPE_TEXTFIELD == nFieldType ||
755 FIELDTYPE_COMBOBOX == nFieldType) {
756 OnSetFieldInputFocus(nullptr, 0, FALSE);
757 }
758 }
759 if (!m_pFocusAnnot)
760 return TRUE;
761 } else {
762 m_pFocusAnnot.Reset(pFocusAnnot.Get());
763 }
764 }
765 return FALSE;
766}
767
768FX_BOOL CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) {
769 return GetPDFDocument()->GetUserPermissions() & nFlag;
770}