blob: 5f3b7e1c656d1c5f31095103bfac3868117fd655 [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>
Tom Sepezfe91c6c2017-05-16 15:33:20 -070010#include <utility>
Tom Sepezd04de262019-12-03 23:27:59 +000011#include <vector>
thestigd4c34f22016-09-28 17:04:51 -070012
dsinclair7cbe68e2016-10-12 11:56:23 -070013#include "core/fpdfapi/parser/cpdf_array.h"
Lei Zhang81535612018-10-09 21:15:17 +000014#include "core/fpdfapi/parser/cpdf_dictionary.h"
dsinclair7cbe68e2016-10-12 11:56:23 -070015#include "core/fpdfdoc/cpdf_docjsactions.h"
Tom Sepezd04de262019-12-03 23:27:59 +000016#include "core/fxcrt/fx_memory_wrappers.h"
Dan Sinclair7d125322018-03-28 18:49:34 +000017#include "fpdfsdk/cpdfsdk_actionhandler.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_annothandlermgr.h"
Tom Sepez8e94c182019-06-11 23:57:25 +000019#include "fpdfsdk/cpdfsdk_helpers.h"
Lei Zhangc3450652018-10-11 16:54:42 +000020#include "fpdfsdk/cpdfsdk_interactiveform.h"
dsinclair7cbe68e2016-10-12 11:56:23 -070021#include "fpdfsdk/cpdfsdk_pageview.h"
22#include "fpdfsdk/cpdfsdk_widget.h"
Tom Sepez3c168bb2019-08-07 23:00:06 +000023#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclairb94d7c92016-09-21 12:07:00 -070024#include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000025#include "fxjs/ijs_runtime.h"
tsepez36eb4bd2016-10-03 15:24:27 -070026#include "third_party/base/ptr_util.h"
Lei Zhangad391412018-08-01 17:35:47 +000027#include "third_party/base/stl_util.h"
dsinclairf34518b2016-09-13 12:03:48 -070028
Ryan Harrison275e2602017-09-18 14:23:18 -040029FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE) {
Tom Sepez1e934f62018-04-11 21:11:28 +000030 // Force a private version of the string, since we're about to hand it off
31 // to the embedder. Should the embedder modify it by accident, it won't
32 // corrupt other shares of the string beyond |bsUTF16LE|.
dsinclairf34518b2016-09-13 12:03:48 -070033 return reinterpret_cast<FPDF_WIDESTRING>(
Tom Sepez1dbfe992018-04-17 17:19:30 +000034 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()).data());
dsinclairf34518b2016-09-13 12:03:48 -070035}
36
dsinclair735606d2016-10-05 15:47:02 -070037CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment(
Tom Sepezda8063f2018-04-30 17:41:49 +000038 CPDF_Document* pDoc,
Tom Sepez49357702019-08-19 17:17:18 +000039 FPDF_FORMFILLINFO* pFFinfo,
40 std::unique_ptr<CPDFSDK_AnnotHandlerMgr> pHandlerMgr)
41 : m_pInfo(pFFinfo),
42 m_pCPDFDoc(pDoc),
43 m_pAnnotHandlerMgr(std::move(pHandlerMgr)) {
Lei Zhangf97fd062019-08-09 22:18:39 +000044 ASSERT(m_pCPDFDoc);
Tom Sepez49357702019-08-19 17:17:18 +000045 m_pAnnotHandlerMgr->SetFormFillEnv(this);
Lei Zhangf97fd062019-08-09 22:18:39 +000046}
dsinclairf34518b2016-09-13 12:03:48 -070047
dsinclair735606d2016-10-05 15:47:02 -070048CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() {
dsinclair6c659ab2016-10-12 13:41:38 -070049 m_bBeingDestroyed = true;
dsinclair7cbe68e2016-10-12 11:56:23 -070050 ClearAllFocusedAnnots();
dsinclair7cbe68e2016-10-12 11:56:23 -070051
Lei Zhang073ecf42018-10-11 16:56:00 +000052 // |m_PageMap| will try to access |m_pInteractiveForm| when it cleans itself
53 // up. Make sure it is deleted before |m_pInteractiveForm|.
tsepez2599ff72016-11-08 14:38:59 -080054 m_PageMap.clear();
55
56 // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller| when it cleans
57 // itself up. Make sure it is deleted before |m_pFormFiller|.
dsinclair709f5a92016-10-11 14:21:16 -070058 m_pAnnotHandlerMgr.reset();
59
60 // Must destroy the |m_pFormFiller| before the environment (|this|)
61 // because any created form widgets hold a pointer to the environment.
62 // Those widgets may call things like KillTimer() as they are shutdown.
63 m_pFormFiller.reset();
64
dsinclairf34518b2016-09-13 12:03:48 -070065 if (m_pInfo && m_pInfo->Release)
66 m_pInfo->Release(m_pInfo);
67}
68
Tom Sepez3c168bb2019-08-07 23:00:06 +000069void CPDFSDK_FormFillEnvironment::InvalidateRect(PerWindowData* pWidgetData,
70 const CFX_FloatRect& rect) {
71 auto* pPrivateData = static_cast<CFFL_PrivateData*>(pWidgetData);
72 CPDFSDK_Widget* widget = pPrivateData->pWidget.Get();
73 if (!widget)
74 return;
75
76 CPDFSDK_PageView* pPageView = widget->GetPageView();
77 IPDF_Page* pPage = widget->GetPage();
78 if (!pPage || !pPageView)
79 return;
80
81 CFX_Matrix device2page = pPageView->GetCurrentMatrix().GetInverse();
82 CFX_PointF left_top = device2page.Transform(CFX_PointF(rect.left, rect.top));
83 CFX_PointF right_bottom =
84 device2page.Transform(CFX_PointF(rect.right, rect.bottom));
85
86 CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y);
87 rcPDF.Normalize();
88 Invalidate(pPage, rcPDF.GetOuterRect());
89}
90
91void CPDFSDK_FormFillEnvironment::OutputSelectedRect(
92 CFFL_FormFiller* pFormFiller,
93 const CFX_FloatRect& rect) {
94 if (!pFormFiller || !m_pInfo || !m_pInfo->FFI_OutputSelectedRect)
95 return;
96
97 auto* pPage = FPDFPageFromIPDFPage(pFormFiller->GetSDKAnnot()->GetPage());
98 ASSERT(pPage);
99
100 CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
101 CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
102 m_pInfo->FFI_OutputSelectedRect(m_pInfo, pPage, ptA.x, ptB.y, ptB.x, ptA.y);
103}
104
105bool CPDFSDK_FormFillEnvironment::IsSelectionImplemented() const {
106 FPDF_FORMFILLINFO* pInfo = GetFormFillInfo();
107 return pInfo && pInfo->FFI_OutputSelectedRect;
108}
109
Tom Sepezeb98dac2018-11-07 20:04:03 +0000110#ifdef PDF_ENABLE_V8
Tom Sepezd48bd292019-08-14 19:48:55 +0000111CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
112 IPDF_Page* pPage = IPDFPageFromFPDFPage(GetCurrentPage());
113 return pPage ? GetPageView(pPage, true) : nullptr;
114}
115
116FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage() const {
117 if (m_pInfo && m_pInfo->FFI_GetCurrentPage) {
118 return m_pInfo->FFI_GetCurrentPage(
119 m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()));
120 }
121 return nullptr;
122}
123
124WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
125#ifdef PDF_ENABLE_XFA
Tom Sepez197d0d82019-12-18 20:28:20 +0000126 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetLanguage)
Tom Sepezd48bd292019-08-14 19:48:55 +0000127 return WideString();
128
129 int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
130 if (nRequiredLen <= 0)
131 return WideString();
132
Tom Sepezd04de262019-12-03 23:27:59 +0000133 std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen);
Tom Sepezd48bd292019-08-14 19:48:55 +0000134 int nActualLen =
135 m_pInfo->FFI_GetLanguage(m_pInfo, pBuff.data(), nRequiredLen);
136 if (nActualLen <= 0 || nActualLen > nRequiredLen)
137 return WideString();
138
139 return WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
140 nActualLen / sizeof(uint16_t));
141#else // PDF_ENABLE_XFA
142 return WideString();
143#endif // PDF_ENABLE_XFA
144}
145
146WideString CPDFSDK_FormFillEnvironment::GetPlatform() {
147#ifdef PDF_ENABLE_XFA
Tom Sepez197d0d82019-12-18 20:28:20 +0000148 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetPlatform)
Tom Sepezd48bd292019-08-14 19:48:55 +0000149 return WideString();
150
151 int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0);
152 if (nRequiredLen <= 0)
153 return WideString();
154
Tom Sepezd04de262019-12-03 23:27:59 +0000155 std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen);
Tom Sepezd48bd292019-08-14 19:48:55 +0000156 int nActualLen =
157 m_pInfo->FFI_GetPlatform(m_pInfo, pBuff.data(), nRequiredLen);
158 if (nActualLen <= 0 || nActualLen > nRequiredLen)
159 return WideString();
160
161 return WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
162 nActualLen / sizeof(uint16_t));
163#else // PDF_ENABLE_XFA
164 return WideString();
165#endif // PDF_ENABLE_XFA
166}
167
Tom Sepez35939f82018-04-17 21:23:58 +0000168int CPDFSDK_FormFillEnvironment::JS_appAlert(const WideString& Msg,
169 const WideString& Title,
Ryan Harrisonc3cc2ab2018-06-21 21:09:54 +0000170 int Type,
171 int Icon) {
dsinclairf34518b2016-09-13 12:03:48 -0700172 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
173 !m_pInfo->m_pJsPlatform->app_alert) {
174 return -1;
175 }
Ryan Harrisonc3cc2ab2018-06-21 21:09:54 +0000176
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000177 ByteString bsMsg = Msg.ToUTF16LE();
178 ByteString bsTitle = Title.ToUTF16LE();
dsinclairf34518b2016-09-13 12:03:48 -0700179 return m_pInfo->m_pJsPlatform->app_alert(
180 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg),
181 AsFPDFWideString(&bsTitle), Type, Icon);
182}
183
Tom Sepez35939f82018-04-17 21:23:58 +0000184int CPDFSDK_FormFillEnvironment::JS_appResponse(const WideString& Question,
185 const WideString& Title,
186 const WideString& Default,
187 const WideString& Label,
dsinclair735606d2016-10-05 15:47:02 -0700188 FPDF_BOOL bPassword,
189 void* response,
190 int length) {
dsinclairf34518b2016-09-13 12:03:48 -0700191 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
192 !m_pInfo->m_pJsPlatform->app_response) {
193 return -1;
194 }
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000195 ByteString bsQuestion = Question.ToUTF16LE();
196 ByteString bsTitle = Title.ToUTF16LE();
197 ByteString bsDefault = Default.ToUTF16LE();
198 ByteString bsLabel = Label.ToUTF16LE();
dsinclairf34518b2016-09-13 12:03:48 -0700199 return m_pInfo->m_pJsPlatform->app_response(
200 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion),
201 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault),
202 AsFPDFWideString(&bsLabel), bPassword, response, length);
203}
204
dsinclair735606d2016-10-05 15:47:02 -0700205void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) {
dsinclairf34518b2016-09-13 12:03:48 -0700206 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
207 !m_pInfo->m_pJsPlatform->app_beep) {
208 return;
209 }
210 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
211}
212
Ryan Harrison275e2602017-09-18 14:23:18 -0400213WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() {
dsinclairf34518b2016-09-13 12:03:48 -0700214 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
215 !m_pInfo->m_pJsPlatform->Field_browse) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400216 return WideString();
dsinclairf34518b2016-09-13 12:03:48 -0700217 }
218 const int nRequiredLen =
219 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
220 if (nRequiredLen <= 0)
Ryan Harrison275e2602017-09-18 14:23:18 -0400221 return WideString();
dsinclairf34518b2016-09-13 12:03:48 -0700222
Tom Sepezd04de262019-12-03 23:27:59 +0000223 std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen);
dsinclairf34518b2016-09-13 12:03:48 -0700224 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
Tom Sepez4ad46902017-05-02 13:19:56 -0700225 m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen);
dsinclairf34518b2016-09-13 12:03:48 -0700226 if (nActualLen <= 0 || nActualLen > nRequiredLen)
Ryan Harrison275e2602017-09-18 14:23:18 -0400227 return WideString();
dsinclairf34518b2016-09-13 12:03:48 -0700228
Tom Sepez322c0af2019-12-09 21:44:14 +0000229 // Don't include trailing NUL.
230 pBuff.resize(nActualLen - 1);
Tom Sepez40317142018-11-27 00:18:33 +0000231 return WideString::FromDefANSI(ByteStringView(pBuff));
dsinclairf34518b2016-09-13 12:03:48 -0700232}
233
dsinclair735606d2016-10-05 15:47:02 -0700234void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData,
235 int length,
236 FPDF_BOOL bUI,
Tom Sepez35939f82018-04-17 21:23:58 +0000237 const WideString& To,
238 const WideString& Subject,
239 const WideString& CC,
240 const WideString& BCC,
241 const WideString& Msg) {
dsinclairf34518b2016-09-13 12:03:48 -0700242 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
243 !m_pInfo->m_pJsPlatform->Doc_mail) {
244 return;
245 }
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000246 ByteString bsTo = To.ToUTF16LE();
247 ByteString bsSubject = Subject.ToUTF16LE();
248 ByteString bsCC = CC.ToUTF16LE();
249 ByteString bsBcc = BCC.ToUTF16LE();
250 ByteString bsMsg = Msg.ToUTF16LE();
dsinclairf34518b2016-09-13 12:03:48 -0700251 m_pInfo->m_pJsPlatform->Doc_mail(
252 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo),
253 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC),
254 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg));
255}
256
dsinclair735606d2016-10-05 15:47:02 -0700257void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI,
258 int nStart,
259 int nEnd,
260 FPDF_BOOL bSilent,
261 FPDF_BOOL bShrinkToFit,
262 FPDF_BOOL bPrintAsImage,
263 FPDF_BOOL bReverse,
264 FPDF_BOOL bAnnotations) {
dsinclairf34518b2016-09-13 12:03:48 -0700265 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
266 !m_pInfo->m_pJsPlatform->Doc_print) {
267 return;
268 }
269 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd,
270 bSilent, bShrinkToFit, bPrintAsImage,
271 bReverse, bAnnotations);
272}
273
dsinclair735606d2016-10-05 15:47:02 -0700274void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) {
dsinclairf34518b2016-09-13 12:03:48 -0700275 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
276 !m_pInfo->m_pJsPlatform->Doc_gotoPage) {
277 return;
278 }
279 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
280}
Tom Sepezeb98dac2018-11-07 20:04:03 +0000281
282WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() {
Tom Sepeze9703972019-08-09 21:24:29 +0000283 return GetFilePath();
284}
285#endif // PDF_ENABLE_V8
286
287WideString CPDFSDK_FormFillEnvironment::GetFilePath() const {
Tom Sepezeb98dac2018-11-07 20:04:03 +0000288 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
289 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
290 return WideString();
291 }
292 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
293 m_pInfo->m_pJsPlatform, nullptr, 0);
294 if (nRequiredLen <= 0)
295 return WideString();
296
Tom Sepezd04de262019-12-03 23:27:59 +0000297 std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen);
Tom Sepezeb98dac2018-11-07 20:04:03 +0000298 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
299 m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen);
300 if (nActualLen <= 0 || nActualLen > nRequiredLen)
301 return WideString();
302
Tom Sepez322c0af2019-12-09 21:44:14 +0000303 // Don't include trailing NUL.
304 pBuff.resize(nActualLen - 1);
Tom Sepez40317142018-11-27 00:18:33 +0000305 return WideString::FromDefANSI(ByteStringView(pBuff));
Tom Sepezeb98dac2018-11-07 20:04:03 +0000306}
307
Tom Sepeze9703972019-08-09 21:24:29 +0000308void CPDFSDK_FormFillEnvironment::SubmitForm(pdfium::span<uint8_t> form_data,
309 const WideString& URL) {
Tom Sepezeb98dac2018-11-07 20:04:03 +0000310 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
311 !m_pInfo->m_pJsPlatform->Doc_submitForm) {
312 return;
313 }
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000314 ByteString bsUrl = URL.ToUTF16LE();
Tom Sepeze9703972019-08-09 21:24:29 +0000315 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform,
316 form_data.data(), form_data.size(),
317 AsFPDFWideString(&bsUrl));
Tom Sepezeb98dac2018-11-07 20:04:03 +0000318}
dsinclairf34518b2016-09-13 12:03:48 -0700319
Tom Sepezf5ca90c2018-02-01 02:15:44 +0000320IJS_Runtime* CPDFSDK_FormFillEnvironment::GetIJSRuntime() {
Tom Sepezf5ca90c2018-02-01 02:15:44 +0000321 if (!m_pIJSRuntime)
322 m_pIJSRuntime = IJS_Runtime::Create(this);
323 return m_pIJSRuntime.get();
dsinclairf34518b2016-09-13 12:03:48 -0700324}
325
dsinclair735606d2016-10-05 15:47:02 -0700326CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() {
dsinclairf34518b2016-09-13 12:03:48 -0700327 return m_pAnnotHandlerMgr.get();
328}
329
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700330CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHandler() {
dsinclairf34518b2016-09-13 12:03:48 -0700331 if (!m_pActionHandler)
tsepez36eb4bd2016-10-03 15:24:27 -0700332 m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>();
dsinclairf34518b2016-09-13 12:03:48 -0700333 return m_pActionHandler.get();
334}
335
dsinclair735606d2016-10-05 15:47:02 -0700336CFFL_InteractiveFormFiller*
337CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() {
dsinclairb94d7c92016-09-21 12:07:00 -0700338 if (!m_pFormFiller)
tsepez36eb4bd2016-10-03 15:24:27 -0700339 m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this);
dsinclairb94d7c92016-09-21 12:07:00 -0700340 return m_pFormFiller.get();
dsinclairf34518b2016-09-13 12:03:48 -0700341}
dsinclair577ad2c2016-09-22 10:20:43 -0700342
Tom Sepez101535f2018-06-12 13:36:05 +0000343void CPDFSDK_FormFillEnvironment::Invalidate(IPDF_Page* page,
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500344 const FX_RECT& rect) {
345 if (m_pInfo && m_pInfo->FFI_Invalidate) {
Tom Sepez101535f2018-06-12 13:36:05 +0000346 m_pInfo->FFI_Invalidate(m_pInfo, FPDFPageFromIPDFPage(page), rect.left,
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000347 rect.top, rect.right, rect.bottom);
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500348 }
dsinclair735606d2016-10-05 15:47:02 -0700349}
350
dsinclair735606d2016-10-05 15:47:02 -0700351void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) {
dsinclair577ad2c2016-09-22 10:20:43 -0700352 if (m_pInfo && m_pInfo->FFI_SetCursor)
353 m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
354}
355
dsinclair735606d2016-10-05 15:47:02 -0700356int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse,
357 TimerCallback lpTimerFunc) {
dsinclair577ad2c2016-09-22 10:20:43 -0700358 if (m_pInfo && m_pInfo->FFI_SetTimer)
359 return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
Tom Sepez3c168bb2019-08-07 23:00:06 +0000360 return TimerHandlerIface::kInvalidTimerID;
dsinclair577ad2c2016-09-22 10:20:43 -0700361}
362
dsinclair735606d2016-10-05 15:47:02 -0700363void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) {
dsinclair577ad2c2016-09-22 10:20:43 -0700364 if (m_pInfo && m_pInfo->FFI_KillTimer)
365 m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
366}
367
dsinclair735606d2016-10-05 15:47:02 -0700368void CPDFSDK_FormFillEnvironment::OnChange() {
dsinclair577ad2c2016-09-22 10:20:43 -0700369 if (m_pInfo && m_pInfo->FFI_OnChange)
370 m_pInfo->FFI_OnChange(m_pInfo);
371}
372
Dan Sinclair812e96c2017-03-13 16:43:37 -0400373void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(const char* namedAction) {
dsinclair577ad2c2016-09-22 10:20:43 -0700374 if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
375 m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
376}
377
dsinclair735606d2016-10-05 15:47:02 -0700378void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus(
379 FPDF_WIDESTRING focusText,
380 FPDF_DWORD nTextLen,
tsepez4cf55152016-11-02 14:37:54 -0700381 bool bFocus) {
dsinclair577ad2c2016-09-22 10:20:43 -0700382 if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
383 m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
384}
385
Dan Sinclair812e96c2017-03-13 16:43:37 -0400386void CPDFSDK_FormFillEnvironment::DoURIAction(const char* bsURI) {
dsinclair577ad2c2016-09-22 10:20:43 -0700387 if (m_pInfo && m_pInfo->FFI_DoURIAction)
388 m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
389}
390
dsinclair735606d2016-10-05 15:47:02 -0700391void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex,
392 int zoomMode,
393 float* fPosArray,
394 int sizeOfArray) {
dsinclair577ad2c2016-09-22 10:20:43 -0700395 if (m_pInfo && m_pInfo->FFI_DoGoToAction) {
396 m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray,
397 sizeOfArray);
398 }
399}
400
401#ifdef PDF_ENABLE_XFA
Tom Sepezd48bd292019-08-14 19:48:55 +0000402int CPDFSDK_FormFillEnvironment::GetPageViewCount() const {
403 return pdfium::CollectionSize<int>(m_PageMap);
404}
405
406void CPDFSDK_FormFillEnvironment::DisplayCaret(IPDF_Page* page,
dsinclair735606d2016-10-05 15:47:02 -0700407 FPDF_BOOL bVisible,
408 double left,
409 double top,
410 double right,
411 double bottom) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000412 if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_DisplayCaret) {
Tom Sepez101535f2018-06-12 13:36:05 +0000413 m_pInfo->FFI_DisplayCaret(m_pInfo, FPDFPageFromIPDFPage(page), bVisible,
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000414 left, top, right, bottom);
dsinclair577ad2c2016-09-22 10:20:43 -0700415 }
416}
417
Tom Sepez74136f52018-04-27 20:29:37 +0000418int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex() const {
Tom Sepez197d0d82019-12-18 20:28:20 +0000419 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetCurrentPageIndex)
dsinclair577ad2c2016-09-22 10:20:43 -0700420 return -1;
Tom Sepezda8063f2018-04-30 17:41:49 +0000421 return m_pInfo->FFI_GetCurrentPageIndex(
422 m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()));
dsinclair577ad2c2016-09-22 10:20:43 -0700423}
424
Tom Sepez74136f52018-04-27 20:29:37 +0000425void CPDFSDK_FormFillEnvironment::SetCurrentPage(int iCurPage) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000426 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_SetCurrentPage)
Tom Sepezda8063f2018-04-30 17:41:49 +0000427 return;
428 m_pInfo->FFI_SetCurrentPage(
429 m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()), iCurPage);
dsinclair577ad2c2016-09-22 10:20:43 -0700430}
431
Tom Sepez74136f52018-04-27 20:29:37 +0000432void CPDFSDK_FormFillEnvironment::GotoURL(const WideString& wsURL) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000433 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GotoURL)
dsinclair577ad2c2016-09-22 10:20:43 -0700434 return;
435
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000436 ByteString bsTo = wsURL.ToUTF16LE();
Tom Sepezda8063f2018-04-30 17:41:49 +0000437 m_pInfo->FFI_GotoURL(m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()),
Tom Sepez74136f52018-04-27 20:29:37 +0000438 AsFPDFWideString(&bsTo));
dsinclair577ad2c2016-09-22 10:20:43 -0700439}
440
Tom Sepezd48bd292019-08-14 19:48:55 +0000441FS_RECTF CPDFSDK_FormFillEnvironment::GetPageViewRect(IPDF_Page* page) {
Lei Zhang95008dd2018-12-13 21:10:23 +0000442 FS_RECTF rect = {0.0f, 0.0f, 0.0f, 0.0f};
Tom Sepez197d0d82019-12-18 20:28:20 +0000443 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetPageViewRect)
Lei Zhang95008dd2018-12-13 21:10:23 +0000444 return rect;
dsinclair577ad2c2016-09-22 10:20:43 -0700445
446 double left;
447 double top;
448 double right;
449 double bottom;
Tom Sepez101535f2018-06-12 13:36:05 +0000450 m_pInfo->FFI_GetPageViewRect(m_pInfo, FPDFPageFromIPDFPage(page), &left, &top,
451 &right, &bottom);
dsinclair577ad2c2016-09-22 10:20:43 -0700452
Lei Zhang95008dd2018-12-13 21:10:23 +0000453 rect.left = static_cast<float>(left);
454 rect.top = static_cast<float>(top);
455 rect.bottom = static_cast<float>(bottom);
456 rect.right = static_cast<float>(right);
457 return rect;
dsinclair577ad2c2016-09-22 10:20:43 -0700458}
459
Tom Sepezd48bd292019-08-14 19:48:55 +0000460bool CPDFSDK_FormFillEnvironment::PopupMenu(IPDF_Page* page,
tsepez4cf55152016-11-02 14:37:54 -0700461 FPDF_WIDGET hWidget,
462 int menuFlag,
Lei Zhangc1cd2ea2019-07-10 20:00:42 +0000463 const CFX_PointF& pt) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000464 return m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_PopupMenu &&
Tom Sepez101535f2018-06-12 13:36:05 +0000465 m_pInfo->FFI_PopupMenu(m_pInfo, FPDFPageFromIPDFPage(page), hWidget,
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000466 menuFlag, pt.x, pt.y);
dsinclair577ad2c2016-09-22 10:20:43 -0700467}
468
dsinclair735606d2016-10-05 15:47:02 -0700469void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler,
470 FPDF_WIDESTRING pTo,
471 FPDF_WIDESTRING pSubject,
472 FPDF_WIDESTRING pCC,
473 FPDF_WIDESTRING pBcc,
474 FPDF_WIDESTRING pMsg) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000475 if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_EmailTo)
dsinclair577ad2c2016-09-22 10:20:43 -0700476 m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
477}
478
dsinclair735606d2016-10-05 15:47:02 -0700479void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler,
480 int fileFlag,
481 FPDF_WIDESTRING uploadTo) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000482 if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_UploadTo)
dsinclair577ad2c2016-09-22 10:20:43 -0700483 m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
484}
485
dsinclair735606d2016-10-05 15:47:02 -0700486FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType,
487 FPDF_WIDESTRING wsURL,
488 const char* mode) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000489 if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_OpenFile)
dsinclair577ad2c2016-09-22 10:20:43 -0700490 return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
491 return nullptr;
492}
493
Dan Sinclair0b950422017-09-21 15:49:49 -0400494RetainPtr<IFX_SeekableReadStream> CPDFSDK_FormFillEnvironment::DownloadFromURL(
Tom Sepez35939f82018-04-17 21:23:58 +0000495 const WideString& url) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000496 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_DownloadFromURL)
dsinclair577ad2c2016-09-22 10:20:43 -0700497 return nullptr;
498
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000499 ByteString bstrURL = url.ToUTF16LE();
Tom Sepez127ad512019-12-10 23:44:52 +0000500 FPDF_FILEHANDLER* fileHandler =
Tom Sepez1e934f62018-04-11 21:11:28 +0000501 m_pInfo->FFI_DownloadFromURL(m_pInfo, AsFPDFWideString(&bstrURL));
dsinclair577ad2c2016-09-22 10:20:43 -0700502
tsepezfa89a202016-12-02 09:48:30 -0800503 return MakeSeekableStream(fileHandler);
dsinclair577ad2c2016-09-22 10:20:43 -0700504}
505
Ryan Harrison275e2602017-09-18 14:23:18 -0400506WideString CPDFSDK_FormFillEnvironment::PostRequestURL(
Tom Sepez35939f82018-04-17 21:23:58 +0000507 const WideString& wsURL,
508 const WideString& wsData,
509 const WideString& wsContentType,
510 const WideString& wsEncode,
511 const WideString& wsHeader) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000512 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_PostRequestURL)
Tom Sepez890eac22018-12-03 20:35:51 +0000513 return WideString();
dsinclair577ad2c2016-09-22 10:20:43 -0700514
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000515 ByteString bsURL = wsURL.ToUTF16LE();
516 ByteString bsData = wsData.ToUTF16LE();
517 ByteString bsContentType = wsContentType.ToUTF16LE();
518 ByteString bsEncode = wsEncode.ToUTF16LE();
519 ByteString bsHeader = wsHeader.ToUTF16LE();
dsinclair577ad2c2016-09-22 10:20:43 -0700520
521 FPDF_BSTR response;
522 FPDF_BStr_Init(&response);
Tom Sepez1e934f62018-04-11 21:11:28 +0000523 m_pInfo->FFI_PostRequestURL(
524 m_pInfo, AsFPDFWideString(&bsURL), AsFPDFWideString(&bsData),
525 AsFPDFWideString(&bsContentType), AsFPDFWideString(&bsEncode),
526 AsFPDFWideString(&bsHeader), &response);
dsinclair577ad2c2016-09-22 10:20:43 -0700527
Tom Sepez1e934f62018-04-11 21:11:28 +0000528 WideString wsRet =
529 WideString::FromUTF16LE(reinterpret_cast<FPDF_WIDESTRING>(response.str),
530 response.len / sizeof(FPDF_WIDESTRING));
531
dsinclair577ad2c2016-09-22 10:20:43 -0700532 FPDF_BStr_Clear(&response);
dsinclair577ad2c2016-09-22 10:20:43 -0700533 return wsRet;
534}
535
Tom Sepez35939f82018-04-17 21:23:58 +0000536FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(
537 const WideString& wsURL,
538 const WideString& wsData,
539 const WideString& wsEncode) {
Tom Sepez197d0d82019-12-18 20:28:20 +0000540 if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_PutRequestURL)
tsepez4cf55152016-11-02 14:37:54 -0700541 return false;
dsinclair577ad2c2016-09-22 10:20:43 -0700542
Tom Sepezb4c95fe2018-11-27 01:09:44 +0000543 ByteString bsURL = wsURL.ToUTF16LE();
544 ByteString bsData = wsData.ToUTF16LE();
545 ByteString bsEncode = wsEncode.ToUTF16LE();
dsinclair577ad2c2016-09-22 10:20:43 -0700546
Tom Sepez1e934f62018-04-11 21:11:28 +0000547 return m_pInfo->FFI_PutRequestURL(m_pInfo, AsFPDFWideString(&bsURL),
548 AsFPDFWideString(&bsData),
549 AsFPDFWideString(&bsEncode));
dsinclair577ad2c2016-09-22 10:20:43 -0700550}
551
dsinclair735606d2016-10-05 15:47:02 -0700552void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
553 uint32_t dwEventType) const {
Tom Sepez197d0d82019-12-18 20:28:20 +0000554 if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_PageEvent)
dsinclair577ad2c2016-09-22 10:20:43 -0700555 m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
556}
557#endif // PDF_ENABLE_XFA
dsinclair7cbe68e2016-10-12 11:56:23 -0700558
559void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
tsepez2599ff72016-11-08 14:38:59 -0800560 for (auto& it : m_PageMap) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700561 if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
562 KillFocusAnnot(0);
563 }
564}
565
566CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
Tom Sepez101535f2018-06-12 13:36:05 +0000567 IPDF_Page* pUnderlyingPage,
dsinclair6c659ab2016-10-12 13:41:38 -0700568 bool renew) {
tsepez2599ff72016-11-08 14:38:59 -0800569 auto it = m_PageMap.find(pUnderlyingPage);
570 if (it != m_PageMap.end())
dsinclair6c659ab2016-10-12 13:41:38 -0700571 return it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700572
dsinclair6c659ab2016-10-12 13:41:38 -0700573 if (!renew)
dsinclair7cbe68e2016-10-12 11:56:23 -0700574 return nullptr;
575
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700576 auto pNew = pdfium::MakeUnique<CPDFSDK_PageView>(this, pUnderlyingPage);
577 CPDFSDK_PageView* pPageView = pNew.get();
578 m_PageMap[pUnderlyingPage] = std::move(pNew);
579
dsinclair7cbe68e2016-10-12 11:56:23 -0700580 // Delay to load all the annotations, to avoid endless loop.
581 pPageView->LoadFXAnnots();
582 return pPageView;
583}
584
dsinclair7cbe68e2016-10-12 11:56:23 -0700585CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
Tom Sepez101535f2018-06-12 13:36:05 +0000586 IPDF_Page* pTempPage = GetPage(nIndex);
dsinclair7cbe68e2016-10-12 11:56:23 -0700587 if (!pTempPage)
588 return nullptr;
589
tsepez2599ff72016-11-08 14:38:59 -0800590 auto it = m_PageMap.find(pTempPage);
591 return it != m_PageMap.end() ? it->second.get() : nullptr;
dsinclair7cbe68e2016-10-12 11:56:23 -0700592}
593
594void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
Tom Sepezda8063f2018-04-30 17:41:49 +0000595 CPDF_DocJSActions docJS(m_pCPDFDoc.Get());
dsinclair7cbe68e2016-10-12 11:56:23 -0700596 int iCount = docJS.CountJSActions();
dsinclair7cbe68e2016-10-12 11:56:23 -0700597 for (int i = 0; i < iCount; i++) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400598 WideString csJSName;
Tom Sepezc4a2b752017-04-07 13:56:13 -0700599 CPDF_Action jsAction = docJS.GetJSActionAndName(i, &csJSName);
Jane Liu67ccef72017-07-19 13:10:50 -0400600 GetActionHandler()->DoAction_JavaScript(jsAction, csJSName, this);
dsinclair7cbe68e2016-10-12 11:56:23 -0700601 }
602}
603
tsepez4cf55152016-11-02 14:37:54 -0700604bool CPDFSDK_FormFillEnvironment::ProcOpenAction() {
Tom Sepez22923602019-07-15 21:33:35 +0000605 CPDF_Dictionary* pRoot = m_pCPDFDoc->GetRoot();
dsinclair7cbe68e2016-10-12 11:56:23 -0700606 if (!pRoot)
tsepez4cf55152016-11-02 14:37:54 -0700607 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700608
Tom Sepez22923602019-07-15 21:33:35 +0000609 CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
dsinclair7cbe68e2016-10-12 11:56:23 -0700610 if (!pOpenAction)
611 pOpenAction = pRoot->GetArrayFor("OpenAction");
dsinclair7cbe68e2016-10-12 11:56:23 -0700612 if (!pOpenAction)
tsepez4cf55152016-11-02 14:37:54 -0700613 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700614
615 if (pOpenAction->IsArray())
tsepez4cf55152016-11-02 14:37:54 -0700616 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700617
Tom Sepez22923602019-07-15 21:33:35 +0000618 CPDF_Dictionary* pDict = pOpenAction->AsDictionary();
Lei Zhang222e1a42017-06-20 14:47:00 -0700619 if (!pDict)
620 return false;
621
622 CPDF_Action action(pDict);
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700623 GetActionHandler()->DoAction_DocOpen(action, this);
Lei Zhang222e1a42017-06-20 14:47:00 -0700624 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700625}
626
Tom Sepez101535f2018-06-12 13:36:05 +0000627void CPDFSDK_FormFillEnvironment::RemovePageView(IPDF_Page* pUnderlyingPage) {
tsepez2599ff72016-11-08 14:38:59 -0800628 auto it = m_PageMap.find(pUnderlyingPage);
629 if (it == m_PageMap.end())
dsinclair7cbe68e2016-10-12 11:56:23 -0700630 return;
631
dsinclair6c659ab2016-10-12 13:41:38 -0700632 CPDFSDK_PageView* pPageView = it->second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700633 if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
634 return;
635
636 // Mark the page view so we do not come into |RemovePageView| a second
637 // time while we're in the process of removing.
638 pPageView->SetBeingDestroyed();
639
640 // This must happen before we remove |pPageView| from the map because
641 // |KillFocusAnnot| can call into the |GetPage| method which will
642 // look for this page view in the map, if it doesn't find it a new one will
643 // be created. We then have two page views pointing to the same page and
644 // bad things happen.
645 if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
646 KillFocusAnnot(0);
647
648 // Remove the page from the map to make sure we don't accidentally attempt
649 // to use the |pPageView| while we're cleaning it up.
tsepez2599ff72016-11-08 14:38:59 -0800650 m_PageMap.erase(it);
dsinclair7cbe68e2016-10-12 11:56:23 -0700651}
652
Tom Sepez101535f2018-06-12 13:36:05 +0000653IPDF_Page* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
Lei Zhang60fa2fc2017-07-21 17:42:19 -0700654 if (!m_pInfo || !m_pInfo->FFI_GetPage)
655 return nullptr;
Tom Sepez101535f2018-06-12 13:36:05 +0000656 return IPDFPageFromFPDFPage(m_pInfo->FFI_GetPage(
Tom Sepezda8063f2018-04-30 17:41:49 +0000657 m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()), nIndex));
dsinclair7cbe68e2016-10-12 11:56:23 -0700658}
659
Lei Zhang073ecf42018-10-11 16:56:00 +0000660CPDFSDK_InteractiveForm* CPDFSDK_FormFillEnvironment::GetInteractiveForm() {
661 if (!m_pInteractiveForm)
662 m_pInteractiveForm = pdfium::MakeUnique<CPDFSDK_InteractiveForm>(this);
663 return m_pInteractiveForm.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700664}
665
666void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
667 CPDFSDK_Annot* pAnnot) {
tsepez2599ff72016-11-08 14:38:59 -0800668 for (const auto& it : m_PageMap) {
dsinclair6c659ab2016-10-12 13:41:38 -0700669 CPDFSDK_PageView* pPageView = it.second.get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700670 if (pPageView != pSender)
671 pPageView->UpdateView(pAnnot);
672 }
673}
674
tsepez4cf55152016-11-02 14:37:54 -0700675bool CPDFSDK_FormFillEnvironment::SetFocusAnnot(
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000676 ObservedPtr<CPDFSDK_Annot>* pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700677 if (m_bBeingDestroyed)
tsepez4cf55152016-11-02 14:37:54 -0700678 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700679 if (m_pFocusAnnot == *pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700680 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700681 if (m_pFocusAnnot && !KillFocusAnnot(0))
tsepez4cf55152016-11-02 14:37:54 -0700682 return false;
Lei Zhangf0b7d592018-10-10 18:54:24 +0000683 if (!pAnnot->HasObservable())
tsepez4cf55152016-11-02 14:37:54 -0700684 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700685
Lei Zhang222e1a42017-06-20 14:47:00 -0700686 CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
687 if (!pPageView || !pPageView->IsValid())
688 return false;
689
690 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
691 if (m_pFocusAnnot)
692 return false;
693
dsinclair7cbe68e2016-10-12 11:56:23 -0700694#ifdef PDF_ENABLE_XFA
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000695 ObservedPtr<CPDFSDK_Annot> pLastFocusAnnot(m_pFocusAnnot.Get());
Lei Zhang222e1a42017-06-20 14:47:00 -0700696 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
697 return false;
Minh Tran0e71ce22019-07-29 22:28:49 +0000698
699 // |pAnnot| may be destroyed in |Annot_OnChangeFocus|.
700 if (!pAnnot->HasObservable())
701 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700702#endif // PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -0700703 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
704 return false;
705 if (m_pFocusAnnot)
706 return false;
707
708 m_pFocusAnnot.Reset(pAnnot->Get());
709 return true;
dsinclair7cbe68e2016-10-12 11:56:23 -0700710}
711
tsepez4cf55152016-11-02 14:37:54 -0700712bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
Lei Zhang222e1a42017-06-20 14:47:00 -0700713 if (!m_pFocusAnnot)
714 return false;
715
716 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000717 ObservedPtr<CPDFSDK_Annot> pFocusAnnot(m_pFocusAnnot.Get());
Lei Zhang222e1a42017-06-20 14:47:00 -0700718 m_pFocusAnnot.Reset();
dsinclair7cbe68e2016-10-12 11:56:23 -0700719
720#ifdef PDF_ENABLE_XFA
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000721 ObservedPtr<CPDFSDK_Annot> pNull;
Lei Zhang222e1a42017-06-20 14:47:00 -0700722 if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
723 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700724#endif // PDF_ENABLE_XFA
725
Lei Zhang222e1a42017-06-20 14:47:00 -0700726 if (!pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
727 m_pFocusAnnot.Reset(pFocusAnnot.Get());
728 return false;
729 }
730
731 if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
Tom Sepez4ef943b2018-07-26 19:06:06 +0000732 CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pFocusAnnot.Get());
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000733 FormFieldType fieldType = pWidget->GetFieldType();
734 if (fieldType == FormFieldType::kTextField ||
735 fieldType == FormFieldType::kComboBox) {
Lei Zhang222e1a42017-06-20 14:47:00 -0700736 OnSetFieldInputFocus(nullptr, 0, false);
dsinclair7cbe68e2016-10-12 11:56:23 -0700737 }
738 }
Lei Zhang222e1a42017-06-20 14:47:00 -0700739 return !m_pFocusAnnot;
dsinclair7cbe68e2016-10-12 11:56:23 -0700740}
Tom Sepeza88399c2019-06-11 16:39:51 +0000741
Tom Sepeza1d34422018-04-24 20:54:41 +0000742int CPDFSDK_FormFillEnvironment::GetPageCount() const {
Tom Sepezdf93cd32018-05-25 22:58:39 +0000743 CPDF_Document::Extension* pExtension = m_pCPDFDoc->GetExtension();
744 return pExtension ? pExtension->GetPageCount() : m_pCPDFDoc->GetPageCount();
Tom Sepeza1d34422018-04-24 20:54:41 +0000745}
746
Lei Zhangad0982a2017-06-16 19:30:38 -0700747bool CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) const {
Tom Sepezda8063f2018-04-30 17:41:49 +0000748 return !!(m_pCPDFDoc->GetUserPermissions() & nFlag);
dsinclair7cbe68e2016-10-12 11:56:23 -0700749}