blob: b91c97842f7ddf324c6957f7016b0b5a2b49e9d9 [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
dsinclairf34518b2016-09-13 12:03:48 -070022namespace {
23
24// NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken
25// since modifying the result would impact |bsUTF16LE|.
26FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) {
27 return reinterpret_cast<FPDF_WIDESTRING>(
28 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()));
29}
30
31} // namespace
32
dsinclair735606d2016-10-05 15:47:02 -070033CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment(
34 UnderlyingDocumentType* pDoc,
35 FPDF_FORMFILLINFO* pFFinfo)
dsinclaira939bfe2016-09-22 13:18:45 -070036 : m_pInfo(pFFinfo),
dsinclaira939bfe2016-09-22 13:18:45 -070037 m_pUnderlyingDoc(pDoc),
dsinclair7cbe68e2016-10-12 11:56:23 -070038 m_pSysHandler(new CFX_SystemHandler(this)),
dsinclair6c659ab2016-10-12 13:41:38 -070039 m_bChangeMask(false),
40 m_bBeingDestroyed(false) {}
dsinclairf34518b2016-09-13 12:03:48 -070041
dsinclair735606d2016-10-05 15:47:02 -070042CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() {
dsinclair6c659ab2016-10-12 13:41:38 -070043 m_bBeingDestroyed = true;
dsinclair7cbe68e2016-10-12 11:56:23 -070044 ClearAllFocusedAnnots();
dsinclair7cbe68e2016-10-12 11:56:23 -070045
tsepez2599ff72016-11-08 14:38:59 -080046 // |m_PageMap| will try to access |m_pInterForm| when it cleans itself up.
47 // Make sure it is deleted before |m_pInterForm|.
48 m_PageMap.clear();
49
50 // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller| when it cleans
51 // itself up. Make sure it is deleted before |m_pFormFiller|.
dsinclair709f5a92016-10-11 14:21:16 -070052 m_pAnnotHandlerMgr.reset();
53
54 // Must destroy the |m_pFormFiller| before the environment (|this|)
55 // because any created form widgets hold a pointer to the environment.
56 // Those widgets may call things like KillTimer() as they are shutdown.
57 m_pFormFiller.reset();
58
dsinclairf34518b2016-09-13 12:03:48 -070059 if (m_pInfo && m_pInfo->Release)
60 m_pInfo->Release(m_pInfo);
61}
62
dsinclair735606d2016-10-05 15:47:02 -070063int CPDFSDK_FormFillEnvironment::JS_appAlert(const FX_WCHAR* Msg,
64 const FX_WCHAR* Title,
65 uint32_t Type,
66 uint32_t Icon) {
dsinclairf34518b2016-09-13 12:03:48 -070067 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
68 !m_pInfo->m_pJsPlatform->app_alert) {
69 return -1;
70 }
71 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
72 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
73 return m_pInfo->m_pJsPlatform->app_alert(
74 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
75 AsFPDFWideString(&bsTitle), Type, Icon);
76}
77
dsinclair735606d2016-10-05 15:47:02 -070078int CPDFSDK_FormFillEnvironment::JS_appResponse(const FX_WCHAR* Question,
79 const FX_WCHAR* Title,
80 const FX_WCHAR* Default,
81 const FX_WCHAR* cLabel,
82 FPDF_BOOL bPassword,
83 void* response,
84 int length) {
dsinclairf34518b2016-09-13 12:03:48 -070085 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
86 !m_pInfo->m_pJsPlatform->app_response) {
87 return -1;
88 }
89 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
90 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
91 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
92 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
93 return m_pInfo->m_pJsPlatform->app_response(
94 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
95 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
96 AsFPDFWideString(&bsLabel), bPassword, response, length);
97}
98
dsinclair735606d2016-10-05 15:47:02 -070099void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) {
dsinclairf34518b2016-09-13 12:03:48 -0700100 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
101 !m_pInfo->m_pJsPlatform->app_beep) {
102 return;
103 }
104 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
105}
106
dsinclair735606d2016-10-05 15:47:02 -0700107CFX_WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() {
dsinclairf34518b2016-09-13 12:03:48 -0700108 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
109 !m_pInfo->m_pJsPlatform->Field_browse) {
110 return CFX_WideString();
111 }
112 const int nRequiredLen =
113 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
114 if (nRequiredLen <= 0)
115 return CFX_WideString();
116
117 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
118 memset(pBuff.get(), 0, nRequiredLen);
119 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
120 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
121 if (nActualLen <= 0 || nActualLen > nRequiredLen)
122 return CFX_WideString();
123
124 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
125}
126
dsinclair735606d2016-10-05 15:47:02 -0700127CFX_WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() {
dsinclairf34518b2016-09-13 12:03:48 -0700128 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
129 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
130 return CFX_WideString();
131 }
132 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
133 m_pInfo->m_pJsPlatform, nullptr, 0);
134 if (nRequiredLen <= 0)
135 return CFX_WideString();
136
137 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
138 memset(pBuff.get(), 0, nRequiredLen);
139 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
140 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
141 if (nActualLen <= 0 || nActualLen > nRequiredLen)
142 return CFX_WideString();
143
144 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
145}
146
dsinclair735606d2016-10-05 15:47:02 -0700147void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData,
148 int length,
149 const FX_WCHAR* URL) {
dsinclairf34518b2016-09-13 12:03:48 -0700150 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
151 !m_pInfo->m_pJsPlatform->Doc_submitForm) {
152 return;
153 }
154 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
155 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
156 length,
157 AsFPDFWideString(&bsDestination));
158}
159
dsinclair735606d2016-10-05 15:47:02 -0700160void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData,
161 int length,
162 FPDF_BOOL bUI,
163 const FX_WCHAR* To,
164 const FX_WCHAR* Subject,
165 const FX_WCHAR* CC,
166 const FX_WCHAR* BCC,
167 const FX_WCHAR* Msg) {
dsinclairf34518b2016-09-13 12:03:48 -0700168 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
169 !m_pInfo->m_pJsPlatform->Doc_mail) {
170 return;
171 }
172 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
173 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
174 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode();
175 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
176 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
177 m_pInfo->m_pJsPlatform->Doc_mail(
178 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
179 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
180 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg));
181}
182
dsinclair735606d2016-10-05 15:47:02 -0700183void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI,
184 int nStart,
185 int nEnd,
186 FPDF_BOOL bSilent,
187 FPDF_BOOL bShrinkToFit,
188 FPDF_BOOL bPrintAsImage,
189 FPDF_BOOL bReverse,
190 FPDF_BOOL bAnnotations) {
dsinclairf34518b2016-09-13 12:03:48 -0700191 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
192 !m_pInfo->m_pJsPlatform->Doc_print) {
193 return;
194 }
195 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd,
196 bSilent, bShrinkToFit, bPrintAsImage,
197 bReverse, bAnnotations);
198}
199
dsinclair735606d2016-10-05 15:47:02 -0700200void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) {
dsinclairf34518b2016-09-13 12:03:48 -0700201 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
202 !m_pInfo->m_pJsPlatform->Doc_gotoPage) {
203 return;
204 }
205 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
206}
207
dsinclair735606d2016-10-05 15:47:02 -0700208IJS_Runtime* CPDFSDK_FormFillEnvironment::GetJSRuntime() {
dsinclairf34518b2016-09-13 12:03:48 -0700209 if (!IsJSInitiated())
210 return nullptr;
211 if (!m_pJSRuntime)
212 m_pJSRuntime.reset(IJS_Runtime::Create(this));
213 return m_pJSRuntime.get();
214}
215
dsinclair735606d2016-10-05 15:47:02 -0700216CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() {
dsinclairf34518b2016-09-13 12:03:48 -0700217 if (!m_pAnnotHandlerMgr)
tsepez36eb4bd2016-10-03 15:24:27 -0700218 m_pAnnotHandlerMgr = pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(this);
dsinclairf34518b2016-09-13 12:03:48 -0700219 return m_pAnnotHandlerMgr.get();
220}
221
dsinclair735606d2016-10-05 15:47:02 -0700222CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHander() {
dsinclairf34518b2016-09-13 12:03:48 -0700223 if (!m_pActionHandler)
tsepez36eb4bd2016-10-03 15:24:27 -0700224 m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>();
dsinclairf34518b2016-09-13 12:03:48 -0700225 return m_pActionHandler.get();
226}
227
dsinclair735606d2016-10-05 15:47:02 -0700228CFFL_InteractiveFormFiller*
229CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() {
dsinclairb94d7c92016-09-21 12:07:00 -0700230 if (!m_pFormFiller)
tsepez36eb4bd2016-10-03 15:24:27 -0700231 m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this);
dsinclairb94d7c92016-09-21 12:07:00 -0700232 return m_pFormFiller.get();
dsinclairf34518b2016-09-13 12:03:48 -0700233}
dsinclair577ad2c2016-09-22 10:20:43 -0700234
dsinclair735606d2016-10-05 15:47:02 -0700235void CPDFSDK_FormFillEnvironment::Invalidate(FPDF_PAGE page,
dsinclair577ad2c2016-09-22 10:20:43 -0700236 double left,
237 double top,
238 double right,
239 double bottom) {
dsinclair735606d2016-10-05 15:47:02 -0700240 if (m_pInfo && m_pInfo->FFI_Invalidate)
241 m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom);
242}
243
244void CPDFSDK_FormFillEnvironment::OutputSelectedRect(FPDF_PAGE page,
245 double left,
246 double top,
247 double right,
248 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700249 if (m_pInfo && m_pInfo->FFI_OutputSelectedRect)
250 m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom);
251}
252
dsinclair735606d2016-10-05 15:47:02 -0700253void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) {
dsinclair577ad2c2016-09-22 10:20:43 -0700254 if (m_pInfo && m_pInfo->FFI_SetCursor)
255 m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
256}
257
dsinclair735606d2016-10-05 15:47:02 -0700258int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse,
259 TimerCallback lpTimerFunc) {
dsinclair577ad2c2016-09-22 10:20:43 -0700260 if (m_pInfo && m_pInfo->FFI_SetTimer)
261 return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
262 return -1;
263}
264
dsinclair735606d2016-10-05 15:47:02 -0700265void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) {
dsinclair577ad2c2016-09-22 10:20:43 -0700266 if (m_pInfo && m_pInfo->FFI_KillTimer)
267 m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
268}
269
dsinclair735606d2016-10-05 15:47:02 -0700270FX_SYSTEMTIME CPDFSDK_FormFillEnvironment::GetLocalTime() const {
dsinclair577ad2c2016-09-22 10:20:43 -0700271 FX_SYSTEMTIME fxtime;
272 if (!m_pInfo || !m_pInfo->FFI_GetLocalTime)
273 return fxtime;
274
275 FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo);
276 fxtime.wDay = systime.wDay;
277 fxtime.wDayOfWeek = systime.wDayOfWeek;
278 fxtime.wHour = systime.wHour;
279 fxtime.wMilliseconds = systime.wMilliseconds;
280 fxtime.wMinute = systime.wMinute;
281 fxtime.wMonth = systime.wMonth;
282 fxtime.wSecond = systime.wSecond;
283 fxtime.wYear = systime.wYear;
284 return fxtime;
285}
286
dsinclair735606d2016-10-05 15:47:02 -0700287void CPDFSDK_FormFillEnvironment::OnChange() {
dsinclair577ad2c2016-09-22 10:20:43 -0700288 if (m_pInfo && m_pInfo->FFI_OnChange)
289 m_pInfo->FFI_OnChange(m_pInfo);
290}
291
tsepez4cf55152016-11-02 14:37:54 -0700292bool CPDFSDK_FormFillEnvironment::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700293 return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0;
294}
295
tsepez4cf55152016-11-02 14:37:54 -0700296bool CPDFSDK_FormFillEnvironment::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700297 return (nFlag & FWL_EVENTFLAG_ControlKey) != 0;
298}
299
tsepez4cf55152016-11-02 14:37:54 -0700300bool CPDFSDK_FormFillEnvironment::IsALTKeyDown(uint32_t nFlag) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700301 return (nFlag & FWL_EVENTFLAG_AltKey) != 0;
302}
303
dsinclair735606d2016-10-05 15:47:02 -0700304FPDF_PAGE CPDFSDK_FormFillEnvironment::GetPage(FPDF_DOCUMENT document,
305 int nPageIndex) {
dsinclair577ad2c2016-09-22 10:20:43 -0700306 if (m_pInfo && m_pInfo->FFI_GetPage)
307 return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
308 return nullptr;
309}
310
dsinclair735606d2016-10-05 15:47:02 -0700311FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700312 if (m_pInfo && m_pInfo->FFI_GetCurrentPage)
313 return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
314 return nullptr;
315}
316
dsinclair735606d2016-10-05 15:47:02 -0700317void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(
318 const FX_CHAR* namedAction) {
dsinclair577ad2c2016-09-22 10:20:43 -0700319 if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
320 m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
321}
322
dsinclair735606d2016-10-05 15:47:02 -0700323void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus(
324 FPDF_WIDESTRING focusText,
325 FPDF_DWORD nTextLen,
tsepez4cf55152016-11-02 14:37:54 -0700326 bool bFocus) {
dsinclair577ad2c2016-09-22 10:20:43 -0700327 if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
328 m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
329}
330
dsinclair735606d2016-10-05 15:47:02 -0700331void CPDFSDK_FormFillEnvironment::DoURIAction(const FX_CHAR* bsURI) {
dsinclair577ad2c2016-09-22 10:20:43 -0700332 if (m_pInfo && m_pInfo->FFI_DoURIAction)
333 m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
334}
335
dsinclair735606d2016-10-05 15:47:02 -0700336void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex,
337 int zoomMode,
338 float* fPosArray,
339 int sizeOfArray) {
dsinclair577ad2c2016-09-22 10:20:43 -0700340 if (m_pInfo && m_pInfo->FFI_DoGoToAction) {
341 m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray,
342 sizeOfArray);
343 }
344}
345
346#ifdef PDF_ENABLE_XFA
dsinclair735606d2016-10-05 15:47:02 -0700347void CPDFSDK_FormFillEnvironment::DisplayCaret(FPDF_PAGE page,
348 FPDF_BOOL bVisible,
349 double left,
350 double top,
351 double right,
352 double bottom) {
dsinclair577ad2c2016-09-22 10:20:43 -0700353 if (m_pInfo && m_pInfo->FFI_DisplayCaret) {
354 m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right,
355 bottom);
356 }
357}
358
dsinclair735606d2016-10-05 15:47:02 -0700359int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex(FPDF_DOCUMENT document) {
dsinclair577ad2c2016-09-22 10:20:43 -0700360 if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex)
361 return -1;
362 return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
363}
364
dsinclair735606d2016-10-05 15:47:02 -0700365void CPDFSDK_FormFillEnvironment::SetCurrentPage(FPDF_DOCUMENT document,
366 int iCurPage) {
dsinclair577ad2c2016-09-22 10:20:43 -0700367 if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
368 m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
369}
370
dsinclair735606d2016-10-05 15:47:02 -0700371CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() {
dsinclair577ad2c2016-09-22 10:20:43 -0700372 if (!m_pInfo || !m_pInfo->FFI_GetPlatform)
373 return L"";
374
375 int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0);
376 if (nRequiredLen <= 0)
377 return L"";
378
379 char* pbuff = new char[nRequiredLen];
380 memset(pbuff, 0, nRequiredLen);
381 int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
382 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
383 delete[] pbuff;
384 return L"";
385 }
386 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
387 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
388 (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
389 bsRet.GetLength() / sizeof(unsigned short));
390 delete[] pbuff;
391 return wsRet;
392}
393
dsinclair735606d2016-10-05 15:47:02 -0700394void CPDFSDK_FormFillEnvironment::GotoURL(FPDF_DOCUMENT document,
395 const CFX_WideStringC& wsURL) {
dsinclair577ad2c2016-09-22 10:20:43 -0700396 if (!m_pInfo || !m_pInfo->FFI_GotoURL)
397 return;
398
399 CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
400 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
401 m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
402 bsTo.ReleaseBuffer();
403}
404
dsinclair735606d2016-10-05 15:47:02 -0700405void CPDFSDK_FormFillEnvironment::GetPageViewRect(FPDF_PAGE page,
406 FS_RECTF& dstRect) {
dsinclair577ad2c2016-09-22 10:20:43 -0700407 if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect)
408 return;
409
410 double left;
411 double top;
412 double right;
413 double bottom;
414 m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
415
416 dstRect.left = static_cast<float>(left);
417 dstRect.top = static_cast<float>(top < bottom ? bottom : top);
418 dstRect.bottom = static_cast<float>(top < bottom ? top : bottom);
419 dstRect.right = static_cast<float>(right);
420}
421
tsepez4cf55152016-11-02 14:37:54 -0700422bool CPDFSDK_FormFillEnvironment::PopupMenu(FPDF_PAGE page,
423 FPDF_WIDGET hWidget,
424 int menuFlag,
425 CFX_PointF pt) {
tsepezdc0401a2016-10-28 13:10:35 -0700426 return m_pInfo && m_pInfo->FFI_PopupMenu &&
427 m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y);
dsinclair577ad2c2016-09-22 10:20:43 -0700428}
429
dsinclair735606d2016-10-05 15:47:02 -0700430void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg,
431 FPDF_WIDESTRING Title,
432 int Type,
433 int Icon) {
dsinclair577ad2c2016-09-22 10:20:43 -0700434 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
435 m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type,
436 Icon);
437 }
438}
439
dsinclair735606d2016-10-05 15:47:02 -0700440void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler,
441 FPDF_WIDESTRING pTo,
442 FPDF_WIDESTRING pSubject,
443 FPDF_WIDESTRING pCC,
444 FPDF_WIDESTRING pBcc,
445 FPDF_WIDESTRING pMsg) {
dsinclair577ad2c2016-09-22 10:20:43 -0700446 if (m_pInfo && m_pInfo->FFI_EmailTo)
447 m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
448}
449
dsinclair735606d2016-10-05 15:47:02 -0700450void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler,
451 int fileFlag,
452 FPDF_WIDESTRING uploadTo) {
dsinclair577ad2c2016-09-22 10:20:43 -0700453 if (m_pInfo && m_pInfo->FFI_UploadTo)
454 m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
455}
456
dsinclair735606d2016-10-05 15:47:02 -0700457FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType,
458 FPDF_WIDESTRING wsURL,
459 const char* mode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700460 if (m_pInfo && m_pInfo->FFI_OpenFile)
461 return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
462 return nullptr;
463}
464
tsepez833619b2016-12-07 09:21:17 -0800465CFX_RetainPtr<IFX_SeekableReadStream>
466CPDFSDK_FormFillEnvironment::DownloadFromURL(const FX_WCHAR* url) {
dsinclair577ad2c2016-09-22 10:20:43 -0700467 if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL)
468 return nullptr;
469
470 CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
471 FPDF_WIDESTRING wsURL =
472 (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
473
474 FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
tsepezfa89a202016-12-02 09:48:30 -0800475 return MakeSeekableStream(fileHandler);
dsinclair577ad2c2016-09-22 10:20:43 -0700476}
477
dsinclair735606d2016-10-05 15:47:02 -0700478CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
dsinclair577ad2c2016-09-22 10:20:43 -0700479 const FX_WCHAR* wsURL,
480 const FX_WCHAR* wsData,
481 const FX_WCHAR* wsContentType,
482 const FX_WCHAR* wsEncode,
483 const FX_WCHAR* wsHeader) {
484 if (!m_pInfo || !m_pInfo->FFI_PostRequestURL)
485 return L"";
486
487 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
488 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
489
490 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
491 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
492
493 CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
494 FPDF_WIDESTRING contentType =
495 (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
496
497 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
498 FPDF_WIDESTRING encode =
499 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
500
501 CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
502 FPDF_WIDESTRING header =
503 (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
504
505 FPDF_BSTR response;
506 FPDF_BStr_Init(&response);
507 m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header,
508 &response);
509
510 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
511 (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING));
512 FPDF_BStr_Clear(&response);
513
514 return wsRet;
515}
516
dsinclair735606d2016-10-05 15:47:02 -0700517FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const FX_WCHAR* wsURL,
518 const FX_WCHAR* wsData,
519 const FX_WCHAR* wsEncode) {
dsinclair577ad2c2016-09-22 10:20:43 -0700520 if (!m_pInfo || !m_pInfo->FFI_PutRequestURL)
tsepez4cf55152016-11-02 14:37:54 -0700521 return false;
dsinclair577ad2c2016-09-22 10:20:43 -0700522
523 CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
524 FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
525
526 CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
527 FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
528
529 CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
530 FPDF_WIDESTRING encode =
531 (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
532
533 return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
534}
535
dsinclair735606d2016-10-05 15:47:02 -0700536CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
dsinclair577ad2c2016-09-22 10:20:43 -0700537 if (!m_pInfo || !m_pInfo->FFI_GetLanguage)
538 return L"";
539
540 int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
541 if (nRequiredLen <= 0)
542 return L"";
543
544 char* pbuff = new char[nRequiredLen];
545 memset(pbuff, 0, nRequiredLen);
546 int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
547 if (nActualLen <= 0 || nActualLen > nRequiredLen) {
548 delete[] pbuff;
549 return L"";
550 }
551 CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
552 CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
553 (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()),
554 bsRet.GetLength() / sizeof(FPDF_WIDESTRING));
555 delete[] pbuff;
556 return wsRet;
557}
558
dsinclair735606d2016-10-05 15:47:02 -0700559void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
560 uint32_t dwEventType) const {
dsinclair577ad2c2016-09-22 10:20:43 -0700561 if (m_pInfo && m_pInfo->FFI_PageEvent)
562 m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
563}
564#endif // PDF_ENABLE_XFA
dsinclair7cbe68e2016-10-12 11:56:23 -0700565
566void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
tsepez2599ff72016-11-08 14:38:59 -0800567 for (auto& it : m_PageMap) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700568 if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
569 KillFocusAnnot(0);
570 }
571}
572
573CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
574 UnderlyingPageType* pUnderlyingPage,
dsinclair6c659ab2016-10-12 13:41:38 -0700575 bool renew) {
tsepez2599ff72016-11-08 14:38:59 -0800576 auto it = m_PageMap.find(pUnderlyingPage);
577 if (it != m_PageMap.end())
dsinclair6c659ab2016-10-12 13:41:38 -0700578 return it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700579
dsinclair6c659ab2016-10-12 13:41:38 -0700580 if (!renew)
dsinclair7cbe68e2016-10-12 11:56:23 -0700581 return nullptr;
582
583 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
tsepez2599ff72016-11-08 14:38:59 -0800584 m_PageMap[pUnderlyingPage].reset(pPageView);
dsinclair7cbe68e2016-10-12 11:56:23 -0700585 // Delay to load all the annotations, to avoid endless loop.
586 pPageView->LoadFXAnnots();
587 return pPageView;
588}
589
590CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
591 UnderlyingPageType* pPage =
592 UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc));
593 return pPage ? GetPageView(pPage, true) : nullptr;
594}
595
596CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
597 UnderlyingPageType* pTempPage =
598 UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
599 if (!pTempPage)
600 return nullptr;
601
tsepez2599ff72016-11-08 14:38:59 -0800602 auto it = m_PageMap.find(pTempPage);
603 return it != m_PageMap.end() ? it->second.get() : nullptr;
dsinclair7cbe68e2016-10-12 11:56:23 -0700604}
605
606void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
607 CPDF_Document* pPDFDoc = GetPDFDocument();
608 CPDF_DocJSActions docJS(pPDFDoc);
609 int iCount = docJS.CountJSActions();
610 if (iCount < 1)
611 return;
612 for (int i = 0; i < iCount; i++) {
613 CFX_ByteString csJSName;
614 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
615 if (GetActionHander()) {
616 GetActionHander()->DoAction_JavaScript(
617 jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this);
618 }
619 }
620}
621
tsepez4cf55152016-11-02 14:37:54 -0700622bool CPDFSDK_FormFillEnvironment::ProcOpenAction() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700623 if (!m_pUnderlyingDoc)
tsepez4cf55152016-11-02 14:37:54 -0700624 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700625
626 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
627 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700628 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700629
630 CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
631 if (!pOpenAction)
632 pOpenAction = pRoot->GetArrayFor("OpenAction");
633
634 if (!pOpenAction)
tsepez4cf55152016-11-02 14:37:54 -0700635 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700636
637 if (pOpenAction->IsArray())
tsepez4cf55152016-11-02 14:37:54 -0700638 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700639
640 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
641 CPDF_Action action(pDict);
642 if (GetActionHander())
643 GetActionHander()->DoAction_DocOpen(action, this);
tsepez4cf55152016-11-02 14:37:54 -0700644 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700645 }
tsepez4cf55152016-11-02 14:37:54 -0700646 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700647}
648
649void CPDFSDK_FormFillEnvironment::RemovePageView(
650 UnderlyingPageType* pUnderlyingPage) {
tsepez2599ff72016-11-08 14:38:59 -0800651 auto it = m_PageMap.find(pUnderlyingPage);
652 if (it == m_PageMap.end())
dsinclair7cbe68e2016-10-12 11:56:23 -0700653 return;
654
dsinclair6c659ab2016-10-12 13:41:38 -0700655 CPDFSDK_PageView* pPageView = it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700656 if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
657 return;
658
659 // Mark the page view so we do not come into |RemovePageView| a second
660 // time while we're in the process of removing.
661 pPageView->SetBeingDestroyed();
662
663 // This must happen before we remove |pPageView| from the map because
664 // |KillFocusAnnot| can call into the |GetPage| method which will
665 // look for this page view in the map, if it doesn't find it a new one will
666 // be created. We then have two page views pointing to the same page and
667 // bad things happen.
668 if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
669 KillFocusAnnot(0);
670
671 // Remove the page from the map to make sure we don't accidentally attempt
672 // to use the |pPageView| while we're cleaning it up.
tsepez2599ff72016-11-08 14:38:59 -0800673 m_PageMap.erase(it);
dsinclair7cbe68e2016-10-12 11:56:23 -0700674}
675
676UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
677 return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
678}
679
680CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() {
681 if (!m_pInterForm)
682 m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this);
683 return m_pInterForm.get();
684}
685
686void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
687 CPDFSDK_Annot* pAnnot) {
tsepez2599ff72016-11-08 14:38:59 -0800688 for (const auto& it : m_PageMap) {
dsinclair6c659ab2016-10-12 13:41:38 -0700689 CPDFSDK_PageView* pPageView = it.second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700690 if (pPageView != pSender)
691 pPageView->UpdateView(pAnnot);
692 }
693}
694
tsepez4cf55152016-11-02 14:37:54 -0700695bool CPDFSDK_FormFillEnvironment::SetFocusAnnot(
dsinclair7cbe68e2016-10-12 11:56:23 -0700696 CPDFSDK_Annot::ObservedPtr* pAnnot) {
697 if (m_bBeingDestroyed)
tsepez4cf55152016-11-02 14:37:54 -0700698 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700699 if (m_pFocusAnnot == *pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700700 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700701 if (m_pFocusAnnot && !KillFocusAnnot(0))
tsepez4cf55152016-11-02 14:37:54 -0700702 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700703 if (!*pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700704 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700705
706#ifdef PDF_ENABLE_XFA
707 CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
708#endif // PDF_ENABLE_XFA
709 CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
710 if (pPageView && pPageView->IsValid()) {
711 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
712 if (!m_pFocusAnnot) {
713#ifdef PDF_ENABLE_XFA
714 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
tsepez4cf55152016-11-02 14:37:54 -0700715 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700716#endif // PDF_ENABLE_XFA
717 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
tsepez4cf55152016-11-02 14:37:54 -0700718 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700719 if (!m_pFocusAnnot) {
720 m_pFocusAnnot.Reset(pAnnot->Get());
tsepez4cf55152016-11-02 14:37:54 -0700721 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700722 }
723 }
724 }
tsepez4cf55152016-11-02 14:37:54 -0700725 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700726}
727
tsepez4cf55152016-11-02 14:37:54 -0700728bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700729 if (m_pFocusAnnot) {
730 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
731 CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
732 m_pFocusAnnot.Reset();
733
734#ifdef PDF_ENABLE_XFA
735 CPDFSDK_Annot::ObservedPtr pNull;
736 if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
tsepez4cf55152016-11-02 14:37:54 -0700737 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700738#endif // PDF_ENABLE_XFA
739
740 if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
741 if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
742 CPDFSDK_Widget* pWidget =
743 static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
744 int nFieldType = pWidget->GetFieldType();
745 if (FIELDTYPE_TEXTFIELD == nFieldType ||
746 FIELDTYPE_COMBOBOX == nFieldType) {
tsepez4cf55152016-11-02 14:37:54 -0700747 OnSetFieldInputFocus(nullptr, 0, false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700748 }
749 }
750 if (!m_pFocusAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700751 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700752 } else {
753 m_pFocusAnnot.Reset(pFocusAnnot.Get());
754 }
755 }
tsepez4cf55152016-11-02 14:37:54 -0700756 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700757}
758
tsepez4cf55152016-11-02 14:37:54 -0700759bool CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) {
tsepez95e58342016-10-28 11:34:42 -0700760 return !!(GetPDFDocument()->GetUserPermissions() & nFlag);
dsinclair7cbe68e2016-10-12 11:56:23 -0700761}