dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 1 | // 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 8 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 9 | #include <memory> |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 10 | #include <utility> |
Tom Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 11 | #include <vector> |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 12 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_array.h" |
Lei Zhang | 8153561 | 2018-10-09 21:15:17 +0000 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 15 | #include "core/fpdfdoc/cpdf_docjsactions.h" |
Tom Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 16 | #include "core/fxcrt/fx_memory_wrappers.h" |
Dan Sinclair | 7d12532 | 2018-03-28 18:49:34 +0000 | [diff] [blame] | 17 | #include "fpdfsdk/cpdfsdk_actionhandler.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 18 | #include "fpdfsdk/cpdfsdk_annothandlermgr.h" |
Tom Sepez | 8e94c18 | 2019-06-11 23:57:25 +0000 | [diff] [blame] | 19 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Lei Zhang | c345065 | 2018-10-11 16:54:42 +0000 | [diff] [blame] | 20 | #include "fpdfsdk/cpdfsdk_interactiveform.h" |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 21 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 22 | #include "fpdfsdk/cpdfsdk_widget.h" |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 23 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 24 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 25 | #include "fxjs/ijs_runtime.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 26 | #include "third_party/base/ptr_util.h" |
Lei Zhang | ad39141 | 2018-08-01 17:35:47 +0000 | [diff] [blame] | 27 | #include "third_party/base/stl_util.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 28 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 29 | FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE) { |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 30 | // 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|. |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 33 | return reinterpret_cast<FPDF_WIDESTRING>( |
Tom Sepez | 1dbfe99 | 2018-04-17 17:19:30 +0000 | [diff] [blame] | 34 | bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()).data()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 35 | } |
| 36 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 37 | CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 38 | CPDF_Document* pDoc, |
Tom Sepez | 4935770 | 2019-08-19 17:17:18 +0000 | [diff] [blame] | 39 | 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 Zhang | f97fd06 | 2019-08-09 22:18:39 +0000 | [diff] [blame] | 44 | ASSERT(m_pCPDFDoc); |
Tom Sepez | 4935770 | 2019-08-19 17:17:18 +0000 | [diff] [blame] | 45 | m_pAnnotHandlerMgr->SetFormFillEnv(this); |
Lei Zhang | f97fd06 | 2019-08-09 22:18:39 +0000 | [diff] [blame] | 46 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 47 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 48 | CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 49 | m_bBeingDestroyed = true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 50 | ClearAllFocusedAnnots(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 51 | |
Lei Zhang | 073ecf4 | 2018-10-11 16:56:00 +0000 | [diff] [blame] | 52 | // |m_PageMap| will try to access |m_pInteractiveForm| when it cleans itself |
| 53 | // up. Make sure it is deleted before |m_pInteractiveForm|. |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 54 | 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|. |
dsinclair | 709f5a9 | 2016-10-11 14:21:16 -0700 | [diff] [blame] | 58 | 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 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 65 | if (m_pInfo && m_pInfo->Release) |
| 66 | m_pInfo->Release(m_pInfo); |
| 67 | } |
| 68 | |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 69 | void 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 | |
| 91 | void 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 | |
| 105 | bool CPDFSDK_FormFillEnvironment::IsSelectionImplemented() const { |
| 106 | FPDF_FORMFILLINFO* pInfo = GetFormFillInfo(); |
| 107 | return pInfo && pInfo->FFI_OutputSelectedRect; |
| 108 | } |
| 109 | |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 110 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 111 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() { |
| 112 | IPDF_Page* pPage = IPDFPageFromFPDFPage(GetCurrentPage()); |
| 113 | return pPage ? GetPageView(pPage, true) : nullptr; |
| 114 | } |
| 115 | |
| 116 | FPDF_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 | |
| 124 | WideString CPDFSDK_FormFillEnvironment::GetLanguage() { |
| 125 | #ifdef PDF_ENABLE_XFA |
| 126 | if (!m_pInfo || !m_pInfo->FFI_GetLanguage) |
| 127 | return WideString(); |
| 128 | |
| 129 | int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0); |
| 130 | if (nRequiredLen <= 0) |
| 131 | return WideString(); |
| 132 | |
Tom Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 133 | std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 134 | 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 | |
| 146 | WideString CPDFSDK_FormFillEnvironment::GetPlatform() { |
| 147 | #ifdef PDF_ENABLE_XFA |
| 148 | if (!m_pInfo || !m_pInfo->FFI_GetPlatform) |
| 149 | return WideString(); |
| 150 | |
| 151 | int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0); |
| 152 | if (nRequiredLen <= 0) |
| 153 | return WideString(); |
| 154 | |
Tom Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 155 | std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 156 | 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 Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 168 | int CPDFSDK_FormFillEnvironment::JS_appAlert(const WideString& Msg, |
| 169 | const WideString& Title, |
Ryan Harrison | c3cc2ab | 2018-06-21 21:09:54 +0000 | [diff] [blame] | 170 | int Type, |
| 171 | int Icon) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 172 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 173 | !m_pInfo->m_pJsPlatform->app_alert) { |
| 174 | return -1; |
| 175 | } |
Ryan Harrison | c3cc2ab | 2018-06-21 21:09:54 +0000 | [diff] [blame] | 176 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 177 | ByteString bsMsg = Msg.ToUTF16LE(); |
| 178 | ByteString bsTitle = Title.ToUTF16LE(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 179 | return m_pInfo->m_pJsPlatform->app_alert( |
| 180 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), |
| 181 | AsFPDFWideString(&bsTitle), Type, Icon); |
| 182 | } |
| 183 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 184 | int CPDFSDK_FormFillEnvironment::JS_appResponse(const WideString& Question, |
| 185 | const WideString& Title, |
| 186 | const WideString& Default, |
| 187 | const WideString& Label, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 188 | FPDF_BOOL bPassword, |
| 189 | void* response, |
| 190 | int length) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 191 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 192 | !m_pInfo->m_pJsPlatform->app_response) { |
| 193 | return -1; |
| 194 | } |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 195 | ByteString bsQuestion = Question.ToUTF16LE(); |
| 196 | ByteString bsTitle = Title.ToUTF16LE(); |
| 197 | ByteString bsDefault = Default.ToUTF16LE(); |
| 198 | ByteString bsLabel = Label.ToUTF16LE(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 199 | 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 205 | void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 206 | 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 Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 213 | WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 214 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 215 | !m_pInfo->m_pJsPlatform->Field_browse) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 216 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 217 | } |
| 218 | const int nRequiredLen = |
| 219 | m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
| 220 | if (nRequiredLen <= 0) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 221 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 222 | |
Tom Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 223 | std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 224 | const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 225 | m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 226 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 227 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 228 | |
Tom Sepez | 322c0af | 2019-12-09 21:44:14 +0000 | [diff] [blame] | 229 | // Don't include trailing NUL. |
| 230 | pBuff.resize(nActualLen - 1); |
Tom Sepez | 4031714 | 2018-11-27 00:18:33 +0000 | [diff] [blame] | 231 | return WideString::FromDefANSI(ByteStringView(pBuff)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 232 | } |
| 233 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 234 | void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData, |
| 235 | int length, |
| 236 | FPDF_BOOL bUI, |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 237 | const WideString& To, |
| 238 | const WideString& Subject, |
| 239 | const WideString& CC, |
| 240 | const WideString& BCC, |
| 241 | const WideString& Msg) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 242 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 243 | !m_pInfo->m_pJsPlatform->Doc_mail) { |
| 244 | return; |
| 245 | } |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 246 | ByteString bsTo = To.ToUTF16LE(); |
| 247 | ByteString bsSubject = Subject.ToUTF16LE(); |
| 248 | ByteString bsCC = CC.ToUTF16LE(); |
| 249 | ByteString bsBcc = BCC.ToUTF16LE(); |
| 250 | ByteString bsMsg = Msg.ToUTF16LE(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 251 | 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 257 | void 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) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 265 | 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 274 | void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 275 | 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 Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 281 | |
| 282 | WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() { |
Tom Sepez | e970397 | 2019-08-09 21:24:29 +0000 | [diff] [blame] | 283 | return GetFilePath(); |
| 284 | } |
| 285 | #endif // PDF_ENABLE_V8 |
| 286 | |
| 287 | WideString CPDFSDK_FormFillEnvironment::GetFilePath() const { |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 288 | 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 Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 297 | std::vector<uint8_t, FxAllocAllocator<uint8_t>> pBuff(nRequiredLen); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 298 | 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 Sepez | 322c0af | 2019-12-09 21:44:14 +0000 | [diff] [blame] | 303 | // Don't include trailing NUL. |
| 304 | pBuff.resize(nActualLen - 1); |
Tom Sepez | 4031714 | 2018-11-27 00:18:33 +0000 | [diff] [blame] | 305 | return WideString::FromDefANSI(ByteStringView(pBuff)); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Tom Sepez | e970397 | 2019-08-09 21:24:29 +0000 | [diff] [blame] | 308 | void CPDFSDK_FormFillEnvironment::SubmitForm(pdfium::span<uint8_t> form_data, |
| 309 | const WideString& URL) { |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 310 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 311 | !m_pInfo->m_pJsPlatform->Doc_submitForm) { |
| 312 | return; |
| 313 | } |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 314 | ByteString bsUrl = URL.ToUTF16LE(); |
Tom Sepez | e970397 | 2019-08-09 21:24:29 +0000 | [diff] [blame] | 315 | m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, |
| 316 | form_data.data(), form_data.size(), |
| 317 | AsFPDFWideString(&bsUrl)); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 318 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 319 | |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 320 | IJS_Runtime* CPDFSDK_FormFillEnvironment::GetIJSRuntime() { |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 321 | if (!m_pIJSRuntime) |
| 322 | m_pIJSRuntime = IJS_Runtime::Create(this); |
| 323 | return m_pIJSRuntime.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 324 | } |
| 325 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 326 | CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 327 | return m_pAnnotHandlerMgr.get(); |
| 328 | } |
| 329 | |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 330 | CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHandler() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 331 | if (!m_pActionHandler) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 332 | m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 333 | return m_pActionHandler.get(); |
| 334 | } |
| 335 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 336 | CFFL_InteractiveFormFiller* |
| 337 | CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() { |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 338 | if (!m_pFormFiller) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 339 | m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this); |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 340 | return m_pFormFiller.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 341 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 342 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 343 | void CPDFSDK_FormFillEnvironment::Invalidate(IPDF_Page* page, |
Dan Sinclair | 6eec1c4 | 2017-02-21 17:20:43 -0500 | [diff] [blame] | 344 | const FX_RECT& rect) { |
| 345 | if (m_pInfo && m_pInfo->FFI_Invalidate) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 346 | m_pInfo->FFI_Invalidate(m_pInfo, FPDFPageFromIPDFPage(page), rect.left, |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 347 | rect.top, rect.right, rect.bottom); |
Dan Sinclair | 6eec1c4 | 2017-02-21 17:20:43 -0500 | [diff] [blame] | 348 | } |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 349 | } |
| 350 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 351 | void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 352 | if (m_pInfo && m_pInfo->FFI_SetCursor) |
| 353 | m_pInfo->FFI_SetCursor(m_pInfo, nCursorType); |
| 354 | } |
| 355 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 356 | int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse, |
| 357 | TimerCallback lpTimerFunc) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 358 | if (m_pInfo && m_pInfo->FFI_SetTimer) |
| 359 | return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc); |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 360 | return TimerHandlerIface::kInvalidTimerID; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 361 | } |
| 362 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 363 | void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 364 | if (m_pInfo && m_pInfo->FFI_KillTimer) |
| 365 | m_pInfo->FFI_KillTimer(m_pInfo, nTimerID); |
| 366 | } |
| 367 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 368 | void CPDFSDK_FormFillEnvironment::OnChange() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 369 | if (m_pInfo && m_pInfo->FFI_OnChange) |
| 370 | m_pInfo->FFI_OnChange(m_pInfo); |
| 371 | } |
| 372 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 373 | void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(const char* namedAction) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 374 | if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction) |
| 375 | m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction); |
| 376 | } |
| 377 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 378 | void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus( |
| 379 | FPDF_WIDESTRING focusText, |
| 380 | FPDF_DWORD nTextLen, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 381 | bool bFocus) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 382 | if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus) |
| 383 | m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus); |
| 384 | } |
| 385 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 386 | void CPDFSDK_FormFillEnvironment::DoURIAction(const char* bsURI) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 387 | if (m_pInfo && m_pInfo->FFI_DoURIAction) |
| 388 | m_pInfo->FFI_DoURIAction(m_pInfo, bsURI); |
| 389 | } |
| 390 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 391 | void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex, |
| 392 | int zoomMode, |
| 393 | float* fPosArray, |
| 394 | int sizeOfArray) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 395 | 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 Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 402 | int CPDFSDK_FormFillEnvironment::GetPageViewCount() const { |
| 403 | return pdfium::CollectionSize<int>(m_PageMap); |
| 404 | } |
| 405 | |
| 406 | void CPDFSDK_FormFillEnvironment::DisplayCaret(IPDF_Page* page, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 407 | FPDF_BOOL bVisible, |
| 408 | double left, |
| 409 | double top, |
| 410 | double right, |
| 411 | double bottom) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 412 | if (m_pInfo && m_pInfo->FFI_DisplayCaret) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 413 | m_pInfo->FFI_DisplayCaret(m_pInfo, FPDFPageFromIPDFPage(page), bVisible, |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 414 | left, top, right, bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 418 | int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex() const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 419 | if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex) |
| 420 | return -1; |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 421 | return m_pInfo->FFI_GetCurrentPageIndex( |
| 422 | m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get())); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 425 | void CPDFSDK_FormFillEnvironment::SetCurrentPage(int iCurPage) { |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 426 | if (!m_pInfo || !m_pInfo->FFI_SetCurrentPage) |
| 427 | return; |
| 428 | m_pInfo->FFI_SetCurrentPage( |
| 429 | m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()), iCurPage); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 432 | void CPDFSDK_FormFillEnvironment::GotoURL(const WideString& wsURL) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 433 | if (!m_pInfo || !m_pInfo->FFI_GotoURL) |
| 434 | return; |
| 435 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 436 | ByteString bsTo = wsURL.ToUTF16LE(); |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 437 | m_pInfo->FFI_GotoURL(m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()), |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 438 | AsFPDFWideString(&bsTo)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 441 | FS_RECTF CPDFSDK_FormFillEnvironment::GetPageViewRect(IPDF_Page* page) { |
Lei Zhang | 95008dd | 2018-12-13 21:10:23 +0000 | [diff] [blame] | 442 | FS_RECTF rect = {0.0f, 0.0f, 0.0f, 0.0f}; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 443 | if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect) |
Lei Zhang | 95008dd | 2018-12-13 21:10:23 +0000 | [diff] [blame] | 444 | return rect; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 445 | |
| 446 | double left; |
| 447 | double top; |
| 448 | double right; |
| 449 | double bottom; |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 450 | m_pInfo->FFI_GetPageViewRect(m_pInfo, FPDFPageFromIPDFPage(page), &left, &top, |
| 451 | &right, &bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 452 | |
Lei Zhang | 95008dd | 2018-12-13 21:10:23 +0000 | [diff] [blame] | 453 | 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; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 460 | bool CPDFSDK_FormFillEnvironment::PopupMenu(IPDF_Page* page, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 461 | FPDF_WIDGET hWidget, |
| 462 | int menuFlag, |
Lei Zhang | c1cd2ea | 2019-07-10 20:00:42 +0000 | [diff] [blame] | 463 | const CFX_PointF& pt) { |
tsepez | dc0401a | 2016-10-28 13:10:35 -0700 | [diff] [blame] | 464 | return m_pInfo && m_pInfo->FFI_PopupMenu && |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 465 | m_pInfo->FFI_PopupMenu(m_pInfo, FPDFPageFromIPDFPage(page), hWidget, |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 466 | menuFlag, pt.x, pt.y); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 467 | } |
| 468 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 469 | void 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) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 475 | if (m_pInfo && m_pInfo->FFI_EmailTo) |
| 476 | m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg); |
| 477 | } |
| 478 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 479 | void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler, |
| 480 | int fileFlag, |
| 481 | FPDF_WIDESTRING uploadTo) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 482 | if (m_pInfo && m_pInfo->FFI_UploadTo) |
| 483 | m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo); |
| 484 | } |
| 485 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 486 | FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType, |
| 487 | FPDF_WIDESTRING wsURL, |
| 488 | const char* mode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 489 | if (m_pInfo && m_pInfo->FFI_OpenFile) |
| 490 | return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode); |
| 491 | return nullptr; |
| 492 | } |
| 493 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 494 | RetainPtr<IFX_SeekableReadStream> CPDFSDK_FormFillEnvironment::DownloadFromURL( |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 495 | const WideString& url) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 496 | if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL) |
| 497 | return nullptr; |
| 498 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 499 | ByteString bstrURL = url.ToUTF16LE(); |
Tom Sepez | 127ad51 | 2019-12-10 23:44:52 +0000 | [diff] [blame^] | 500 | FPDF_FILEHANDLER* fileHandler = |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 501 | m_pInfo->FFI_DownloadFromURL(m_pInfo, AsFPDFWideString(&bstrURL)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 502 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 503 | return MakeSeekableStream(fileHandler); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 506 | WideString CPDFSDK_FormFillEnvironment::PostRequestURL( |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 507 | const WideString& wsURL, |
| 508 | const WideString& wsData, |
| 509 | const WideString& wsContentType, |
| 510 | const WideString& wsEncode, |
| 511 | const WideString& wsHeader) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 512 | if (!m_pInfo || !m_pInfo->FFI_PostRequestURL) |
Tom Sepez | 890eac2 | 2018-12-03 20:35:51 +0000 | [diff] [blame] | 513 | return WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 514 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 515 | ByteString bsURL = wsURL.ToUTF16LE(); |
| 516 | ByteString bsData = wsData.ToUTF16LE(); |
| 517 | ByteString bsContentType = wsContentType.ToUTF16LE(); |
| 518 | ByteString bsEncode = wsEncode.ToUTF16LE(); |
| 519 | ByteString bsHeader = wsHeader.ToUTF16LE(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 520 | |
| 521 | FPDF_BSTR response; |
| 522 | FPDF_BStr_Init(&response); |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 523 | m_pInfo->FFI_PostRequestURL( |
| 524 | m_pInfo, AsFPDFWideString(&bsURL), AsFPDFWideString(&bsData), |
| 525 | AsFPDFWideString(&bsContentType), AsFPDFWideString(&bsEncode), |
| 526 | AsFPDFWideString(&bsHeader), &response); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 527 | |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 528 | WideString wsRet = |
| 529 | WideString::FromUTF16LE(reinterpret_cast<FPDF_WIDESTRING>(response.str), |
| 530 | response.len / sizeof(FPDF_WIDESTRING)); |
| 531 | |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 532 | FPDF_BStr_Clear(&response); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 533 | return wsRet; |
| 534 | } |
| 535 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 536 | FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL( |
| 537 | const WideString& wsURL, |
| 538 | const WideString& wsData, |
| 539 | const WideString& wsEncode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 540 | if (!m_pInfo || !m_pInfo->FFI_PutRequestURL) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 541 | return false; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 542 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 543 | ByteString bsURL = wsURL.ToUTF16LE(); |
| 544 | ByteString bsData = wsData.ToUTF16LE(); |
| 545 | ByteString bsEncode = wsEncode.ToUTF16LE(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 546 | |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 547 | return m_pInfo->FFI_PutRequestURL(m_pInfo, AsFPDFWideString(&bsURL), |
| 548 | AsFPDFWideString(&bsData), |
| 549 | AsFPDFWideString(&bsEncode)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 550 | } |
| 551 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 552 | void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount, |
| 553 | uint32_t dwEventType) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 554 | if (m_pInfo && m_pInfo->FFI_PageEvent) |
| 555 | m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType); |
| 556 | } |
| 557 | #endif // PDF_ENABLE_XFA |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 558 | |
| 559 | void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 560 | for (auto& it : m_PageMap) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 561 | if (it.second->IsValidSDKAnnot(GetFocusAnnot())) |
| 562 | KillFocusAnnot(0); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView( |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 567 | IPDF_Page* pUnderlyingPage, |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 568 | bool renew) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 569 | auto it = m_PageMap.find(pUnderlyingPage); |
| 570 | if (it != m_PageMap.end()) |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 571 | return it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 572 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 573 | if (!renew) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 574 | return nullptr; |
| 575 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 576 | auto pNew = pdfium::MakeUnique<CPDFSDK_PageView>(this, pUnderlyingPage); |
| 577 | CPDFSDK_PageView* pPageView = pNew.get(); |
| 578 | m_PageMap[pUnderlyingPage] = std::move(pNew); |
| 579 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 580 | // Delay to load all the annotations, to avoid endless loop. |
| 581 | pPageView->LoadFXAnnots(); |
| 582 | return pPageView; |
| 583 | } |
| 584 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 585 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 586 | IPDF_Page* pTempPage = GetPage(nIndex); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 587 | if (!pTempPage) |
| 588 | return nullptr; |
| 589 | |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 590 | auto it = m_PageMap.find(pTempPage); |
| 591 | return it != m_PageMap.end() ? it->second.get() : nullptr; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() { |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 595 | CPDF_DocJSActions docJS(m_pCPDFDoc.Get()); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 596 | int iCount = docJS.CountJSActions(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 597 | for (int i = 0; i < iCount; i++) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 598 | WideString csJSName; |
Tom Sepez | c4a2b75 | 2017-04-07 13:56:13 -0700 | [diff] [blame] | 599 | CPDF_Action jsAction = docJS.GetJSActionAndName(i, &csJSName); |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 600 | GetActionHandler()->DoAction_JavaScript(jsAction, csJSName, this); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 604 | bool CPDFSDK_FormFillEnvironment::ProcOpenAction() { |
Tom Sepez | 2292360 | 2019-07-15 21:33:35 +0000 | [diff] [blame] | 605 | CPDF_Dictionary* pRoot = m_pCPDFDoc->GetRoot(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 606 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 607 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 608 | |
Tom Sepez | 2292360 | 2019-07-15 21:33:35 +0000 | [diff] [blame] | 609 | CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction"); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 610 | if (!pOpenAction) |
| 611 | pOpenAction = pRoot->GetArrayFor("OpenAction"); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 612 | if (!pOpenAction) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 613 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 614 | |
| 615 | if (pOpenAction->IsArray()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 616 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 617 | |
Tom Sepez | 2292360 | 2019-07-15 21:33:35 +0000 | [diff] [blame] | 618 | CPDF_Dictionary* pDict = pOpenAction->AsDictionary(); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 619 | if (!pDict) |
| 620 | return false; |
| 621 | |
| 622 | CPDF_Action action(pDict); |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 623 | GetActionHandler()->DoAction_DocOpen(action, this); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 624 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 627 | void CPDFSDK_FormFillEnvironment::RemovePageView(IPDF_Page* pUnderlyingPage) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 628 | auto it = m_PageMap.find(pUnderlyingPage); |
| 629 | if (it == m_PageMap.end()) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 630 | return; |
| 631 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 632 | CPDFSDK_PageView* pPageView = it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 633 | 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. |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 650 | m_PageMap.erase(it); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 653 | IPDF_Page* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) { |
Lei Zhang | 60fa2fc | 2017-07-21 17:42:19 -0700 | [diff] [blame] | 654 | if (!m_pInfo || !m_pInfo->FFI_GetPage) |
| 655 | return nullptr; |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 656 | return IPDFPageFromFPDFPage(m_pInfo->FFI_GetPage( |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 657 | m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc.Get()), nIndex)); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Lei Zhang | 073ecf4 | 2018-10-11 16:56:00 +0000 | [diff] [blame] | 660 | CPDFSDK_InteractiveForm* CPDFSDK_FormFillEnvironment::GetInteractiveForm() { |
| 661 | if (!m_pInteractiveForm) |
| 662 | m_pInteractiveForm = pdfium::MakeUnique<CPDFSDK_InteractiveForm>(this); |
| 663 | return m_pInteractiveForm.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender, |
| 667 | CPDFSDK_Annot* pAnnot) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 668 | for (const auto& it : m_PageMap) { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 669 | CPDFSDK_PageView* pPageView = it.second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 670 | if (pPageView != pSender) |
| 671 | pPageView->UpdateView(pAnnot); |
| 672 | } |
| 673 | } |
| 674 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 675 | bool CPDFSDK_FormFillEnvironment::SetFocusAnnot( |
Tom Sepez | d8ae8f8 | 2019-06-12 17:58:33 +0000 | [diff] [blame] | 676 | ObservedPtr<CPDFSDK_Annot>* pAnnot) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 677 | if (m_bBeingDestroyed) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 678 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 679 | if (m_pFocusAnnot == *pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 680 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 681 | if (m_pFocusAnnot && !KillFocusAnnot(0)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 682 | return false; |
Lei Zhang | f0b7d59 | 2018-10-10 18:54:24 +0000 | [diff] [blame] | 683 | if (!pAnnot->HasObservable()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 684 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 685 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 686 | 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 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 694 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | d8ae8f8 | 2019-06-12 17:58:33 +0000 | [diff] [blame] | 695 | ObservedPtr<CPDFSDK_Annot> pLastFocusAnnot(m_pFocusAnnot.Get()); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 696 | if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot)) |
| 697 | return false; |
Minh Tran | 0e71ce2 | 2019-07-29 22:28:49 +0000 | [diff] [blame] | 698 | |
| 699 | // |pAnnot| may be destroyed in |Annot_OnChangeFocus|. |
| 700 | if (!pAnnot->HasObservable()) |
| 701 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 702 | #endif // PDF_ENABLE_XFA |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 703 | 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; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 710 | } |
| 711 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 712 | bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 713 | if (!m_pFocusAnnot) |
| 714 | return false; |
| 715 | |
| 716 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr(); |
Tom Sepez | d8ae8f8 | 2019-06-12 17:58:33 +0000 | [diff] [blame] | 717 | ObservedPtr<CPDFSDK_Annot> pFocusAnnot(m_pFocusAnnot.Get()); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 718 | m_pFocusAnnot.Reset(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 719 | |
| 720 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | d8ae8f8 | 2019-06-12 17:58:33 +0000 | [diff] [blame] | 721 | ObservedPtr<CPDFSDK_Annot> pNull; |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 722 | if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot)) |
| 723 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 724 | #endif // PDF_ENABLE_XFA |
| 725 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 726 | 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 Sepez | 4ef943b | 2018-07-26 19:06:06 +0000 | [diff] [blame] | 732 | CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pFocusAnnot.Get()); |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 733 | FormFieldType fieldType = pWidget->GetFieldType(); |
| 734 | if (fieldType == FormFieldType::kTextField || |
| 735 | fieldType == FormFieldType::kComboBox) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 736 | OnSetFieldInputFocus(nullptr, 0, false); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 739 | return !m_pFocusAnnot; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 740 | } |
Tom Sepez | a88399c | 2019-06-11 16:39:51 +0000 | [diff] [blame] | 741 | |
Tom Sepez | a1d3442 | 2018-04-24 20:54:41 +0000 | [diff] [blame] | 742 | int CPDFSDK_FormFillEnvironment::GetPageCount() const { |
Tom Sepez | df93cd3 | 2018-05-25 22:58:39 +0000 | [diff] [blame] | 743 | CPDF_Document::Extension* pExtension = m_pCPDFDoc->GetExtension(); |
| 744 | return pExtension ? pExtension->GetPageCount() : m_pCPDFDoc->GetPageCount(); |
Tom Sepez | a1d3442 | 2018-04-24 20:54:41 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Lei Zhang | ad0982a | 2017-06-16 19:30:38 -0700 | [diff] [blame] | 747 | bool CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) const { |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 748 | return !!(m_pCPDFDoc->GetUserPermissions() & nFlag); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 749 | } |