K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2016 The PDFium Authors |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
Lei Zhang | 549bfb2 | 2022-08-11 19:45:29 +0000 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 11 | #include <memory> |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 12 | #include <utility> |
Tom Sepez | d04de26 | 2019-12-03 23:27:59 +0000 | [diff] [blame] | 13 | #include <vector> |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 14 | |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 15 | #include "core/fpdfapi/page/cpdf_annotcontext.h" |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_array.h" |
Lei Zhang | 8153561 | 2018-10-09 21:15:17 +0000 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Tom Sepez | 20d0809 | 2020-01-29 21:27:44 +0000 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpdf_nametree.h" |
Lei Zhang | 9108588 | 2024-02-22 00:49:57 +0000 | [diff] [blame] | 19 | #include "core/fxcrt/check.h" |
Lei Zhang | f778d3c | 2024-02-16 20:44:14 +0000 | [diff] [blame] | 20 | #include "core/fxcrt/containers/contains.h" |
Lei Zhang | 549bfb2 | 2022-08-11 19:45:29 +0000 | [diff] [blame] | 21 | #include "core/fxcrt/data_vector.h" |
Lei Zhang | 9108588 | 2024-02-22 00:49:57 +0000 | [diff] [blame] | 22 | #include "core/fxcrt/notreached.h" |
Lei Zhang | f36006c | 2024-02-17 00:56:24 +0000 | [diff] [blame] | 23 | #include "core/fxcrt/numerics/safe_conversions.h" |
Lei Zhang | 549fe5d | 2021-06-18 23:46:09 +0000 | [diff] [blame] | 24 | #include "core/fxcrt/stl_util.h" |
Tom Sepez | 8e94c18 | 2019-06-11 23:57:25 +0000 | [diff] [blame] | 25 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Lei Zhang | c345065 | 2018-10-11 16:54:42 +0000 | [diff] [blame] | 26 | #include "fpdfsdk/cpdfsdk_interactiveform.h" |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 27 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 28 | #include "fpdfsdk/cpdfsdk_widget.h" |
Tom Sepez | fa40864 | 2021-05-27 21:46:51 +0000 | [diff] [blame] | 29 | #include "fpdfsdk/formfiller/cffl_formfield.h" |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 30 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 31 | #include "fxjs/ijs_event_context.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 32 | #include "fxjs/ijs_runtime.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 33 | |
Lei Zhang | 822b6ea | 2022-04-05 03:40:40 +0000 | [diff] [blame] | 34 | #ifdef PDF_ENABLE_XFA |
| 35 | #include "fpdfsdk/fpdfxfa/cpdfxfa_widget.h" |
| 36 | #endif |
| 37 | |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 38 | static_assert(FXCT_ARROW == |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 39 | static_cast<int>(IPWL_FillerNotify::CursorStyle::kArrow), |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 40 | "kArrow value mismatch"); |
| 41 | static_assert(FXCT_NESW == |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 42 | static_cast<int>(IPWL_FillerNotify::CursorStyle::kNESW), |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 43 | "kNEWS value mismatch"); |
| 44 | static_assert(FXCT_NWSE == |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 45 | static_cast<int>(IPWL_FillerNotify::CursorStyle::kNWSE), |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 46 | "kNWSE value mismatch"); |
| 47 | static_assert(FXCT_VBEAM == |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 48 | static_cast<int>(IPWL_FillerNotify::CursorStyle::kVBeam), |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 49 | "kVBeam value mismatch"); |
| 50 | static_assert(FXCT_HBEAM == |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 51 | static_cast<int>(IPWL_FillerNotify::CursorStyle::kHBeam), |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 52 | "HBeam value mismatch"); |
| 53 | static_assert(FXCT_HAND == |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 54 | static_cast<int>(IPWL_FillerNotify::CursorStyle::kHand), |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 55 | "kHand value mismatch"); |
| 56 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 57 | FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE) { |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 58 | // Force a private version of the string, since we're about to hand it off |
| 59 | // to the embedder. Should the embedder modify it by accident, it won't |
| 60 | // corrupt other shares of the string beyond |bsUTF16LE|. |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 61 | return reinterpret_cast<FPDF_WIDESTRING>( |
Tom Sepez | 1dbfe99 | 2018-04-17 17:19:30 +0000 | [diff] [blame] | 62 | bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()).data()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 65 | CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 66 | CPDF_Document* pDoc, |
Lei Zhang | 2014328 | 2022-04-08 21:16:52 +0000 | [diff] [blame] | 67 | FPDF_FORMFILLINFO* pFFinfo) |
Tom Sepez | 4935770 | 2019-08-19 17:17:18 +0000 | [diff] [blame] | 68 | : m_pInfo(pFFinfo), |
| 69 | m_pCPDFDoc(pDoc), |
Tom Sepez | 50985a0 | 2021-09-13 18:25:19 +0000 | [diff] [blame] | 70 | m_pInteractiveFormFiller( |
| 71 | std::make_unique<CFFL_InteractiveFormFiller>(this)) { |
Tom Sepez | 9c78c0d | 2021-01-27 20:13:14 +0000 | [diff] [blame] | 72 | DCHECK(m_pCPDFDoc); |
Lei Zhang | f97fd06 | 2019-08-09 22:18:39 +0000 | [diff] [blame] | 73 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 74 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 75 | CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 76 | m_bBeingDestroyed = true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 77 | ClearAllFocusedAnnots(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 78 | |
Lei Zhang | 073ecf4 | 2018-10-11 16:56:00 +0000 | [diff] [blame] | 79 | // |m_PageMap| will try to access |m_pInteractiveForm| when it cleans itself |
| 80 | // up. Make sure it is deleted before |m_pInteractiveForm|. |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 81 | m_PageMap.clear(); |
| 82 | |
Tom Sepez | b566cd9 | 2021-05-28 00:45:28 +0000 | [diff] [blame] | 83 | // Must destroy the |m_pInteractiveFormFiller| before the environment (|this|) |
dsinclair | 709f5a9 | 2016-10-11 14:21:16 -0700 | [diff] [blame] | 84 | // because any created form widgets hold a pointer to the environment. |
| 85 | // Those widgets may call things like KillTimer() as they are shutdown. |
Tom Sepez | b566cd9 | 2021-05-28 00:45:28 +0000 | [diff] [blame] | 86 | m_pInteractiveFormFiller.reset(); |
dsinclair | 709f5a9 | 2016-10-11 14:21:16 -0700 | [diff] [blame] | 87 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 88 | if (m_pInfo && m_pInfo->Release) |
| 89 | m_pInfo->Release(m_pInfo); |
| 90 | } |
| 91 | |
Tom Sepez | ec0e07b | 2022-05-14 00:14:18 +0000 | [diff] [blame] | 92 | void CPDFSDK_FormFillEnvironment::InvalidateRect(CPDFSDK_Widget* widget, |
| 93 | const CFX_FloatRect& rect) { |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 94 | IPDF_Page* pPage = widget->GetPage(); |
Lei Zhang | c2ad628 | 2020-02-13 20:50:57 +0000 | [diff] [blame] | 95 | if (!pPage) |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 96 | return; |
| 97 | |
Lei Zhang | c2ad628 | 2020-02-13 20:50:57 +0000 | [diff] [blame] | 98 | CFX_Matrix device2page = |
| 99 | widget->GetPageView()->GetCurrentMatrix().GetInverse(); |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 100 | CFX_PointF left_top = device2page.Transform(CFX_PointF(rect.left, rect.top)); |
| 101 | CFX_PointF right_bottom = |
| 102 | device2page.Transform(CFX_PointF(rect.right, rect.bottom)); |
| 103 | |
| 104 | CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y); |
| 105 | rcPDF.Normalize(); |
| 106 | Invalidate(pPage, rcPDF.GetOuterRect()); |
| 107 | } |
| 108 | |
| 109 | void CPDFSDK_FormFillEnvironment::OutputSelectedRect( |
Tom Sepez | ec0e07b | 2022-05-14 00:14:18 +0000 | [diff] [blame] | 110 | CFFL_FormField* pFormField, |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 111 | const CFX_FloatRect& rect) { |
Tom Sepez | f351aab | 2021-09-10 22:38:29 +0000 | [diff] [blame] | 112 | if (!m_pInfo || !m_pInfo->FFI_OutputSelectedRect) |
| 113 | return; |
| 114 | |
Tom Sepez | e6d8821 | 2021-09-22 19:52:28 +0000 | [diff] [blame] | 115 | auto* pPage = FPDFPageFromIPDFPage(pFormField->GetSDKWidget()->GetPage()); |
Tom Sepez | 9c78c0d | 2021-01-27 20:13:14 +0000 | [diff] [blame] | 116 | DCHECK(pPage); |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 117 | |
Tom Sepez | c8f89af | 2021-05-28 15:22:58 +0000 | [diff] [blame] | 118 | CFX_PointF ptA = pFormField->PWLtoFFL(CFX_PointF(rect.left, rect.bottom)); |
| 119 | CFX_PointF ptB = pFormField->PWLtoFFL(CFX_PointF(rect.right, rect.top)); |
Tom Sepez | 3c168bb | 2019-08-07 23:00:06 +0000 | [diff] [blame] | 120 | m_pInfo->FFI_OutputSelectedRect(m_pInfo, pPage, ptA.x, ptB.y, ptB.x, ptA.y); |
| 121 | } |
| 122 | |
| 123 | bool CPDFSDK_FormFillEnvironment::IsSelectionImplemented() const { |
| 124 | FPDF_FORMFILLINFO* pInfo = GetFormFillInfo(); |
| 125 | return pInfo && pInfo->FFI_OutputSelectedRect; |
| 126 | } |
| 127 | |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 128 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 129 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() { |
Tom Sepez | 4b718ca | 2022-08-09 20:22:30 +0000 | [diff] [blame] | 130 | IPDF_Page* pPage = GetCurrentPage(); |
Tom Sepez | cee8fbd | 2021-06-04 19:21:40 +0000 | [diff] [blame] | 131 | return pPage ? GetOrCreatePageView(pPage) : nullptr; |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Tom Sepez | 4b718ca | 2022-08-09 20:22:30 +0000 | [diff] [blame] | 134 | IPDF_Page* CPDFSDK_FormFillEnvironment::GetCurrentPage() const { |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 135 | if (m_pInfo && m_pInfo->FFI_GetCurrentPage) { |
Tom Sepez | 4b718ca | 2022-08-09 20:22:30 +0000 | [diff] [blame] | 136 | return IPDFPageFromFPDFPage(m_pInfo->FFI_GetCurrentPage( |
Tom Sepez | c4acde5 | 2022-10-28 00:47:46 +0000 | [diff] [blame] | 137 | m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc))); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 138 | } |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | WideString CPDFSDK_FormFillEnvironment::GetLanguage() { |
| 143 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 144 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetLanguage) |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 145 | return WideString(); |
| 146 | |
| 147 | int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0); |
| 148 | if (nRequiredLen <= 0) |
| 149 | return WideString(); |
| 150 | |
Lei Zhang | 549bfb2 | 2022-08-11 19:45:29 +0000 | [diff] [blame] | 151 | DataVector<uint8_t> pBuff(nRequiredLen); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 152 | int nActualLen = |
| 153 | m_pInfo->FFI_GetLanguage(m_pInfo, pBuff.data(), nRequiredLen); |
| 154 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 155 | return WideString(); |
| 156 | |
Nico Weber | 40d92c4 | 2023-11-29 03:25:30 +0000 | [diff] [blame] | 157 | return WideString::FromUTF16LE( |
Tom Sepez | 3b679fe | 2024-04-09 18:49:32 +0000 | [diff] [blame] | 158 | pdfium::make_span(pBuff).first(static_cast<size_t>(nActualLen))); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 159 | #else // PDF_ENABLE_XFA |
| 160 | return WideString(); |
| 161 | #endif // PDF_ENABLE_XFA |
| 162 | } |
| 163 | |
| 164 | WideString CPDFSDK_FormFillEnvironment::GetPlatform() { |
| 165 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 166 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetPlatform) |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 167 | return WideString(); |
| 168 | |
| 169 | int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0); |
| 170 | if (nRequiredLen <= 0) |
| 171 | return WideString(); |
| 172 | |
Lei Zhang | 549bfb2 | 2022-08-11 19:45:29 +0000 | [diff] [blame] | 173 | DataVector<uint8_t> pBuff(nRequiredLen); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 174 | int nActualLen = |
| 175 | m_pInfo->FFI_GetPlatform(m_pInfo, pBuff.data(), nRequiredLen); |
| 176 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 177 | return WideString(); |
| 178 | |
Nico Weber | 40d92c4 | 2023-11-29 03:25:30 +0000 | [diff] [blame] | 179 | return WideString::FromUTF16LE( |
Tom Sepez | 3b679fe | 2024-04-09 18:49:32 +0000 | [diff] [blame] | 180 | pdfium::make_span(pBuff).first(static_cast<size_t>(nActualLen))); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 181 | #else // PDF_ENABLE_XFA |
| 182 | return WideString(); |
| 183 | #endif // PDF_ENABLE_XFA |
| 184 | } |
| 185 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 186 | int CPDFSDK_FormFillEnvironment::JS_appAlert(const WideString& Msg, |
| 187 | const WideString& Title, |
Ryan Harrison | c3cc2ab | 2018-06-21 21:09:54 +0000 | [diff] [blame] | 188 | int Type, |
| 189 | int Icon) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 190 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 191 | if (!js_platform || !js_platform->app_alert) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 192 | return -1; |
Ryan Harrison | c3cc2ab | 2018-06-21 21:09:54 +0000 | [diff] [blame] | 193 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 194 | ByteString bsMsg = Msg.ToUTF16LE(); |
| 195 | ByteString bsTitle = Title.ToUTF16LE(); |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 196 | return js_platform->app_alert(js_platform, AsFPDFWideString(&bsMsg), |
| 197 | AsFPDFWideString(&bsTitle), Type, Icon); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Tom Sepez | 8d64cb5 | 2021-11-11 21:41:27 +0000 | [diff] [blame] | 200 | int CPDFSDK_FormFillEnvironment::JS_appResponse( |
| 201 | const WideString& Question, |
| 202 | const WideString& Title, |
| 203 | const WideString& Default, |
| 204 | const WideString& Label, |
| 205 | FPDF_BOOL bPassword, |
| 206 | pdfium::span<uint8_t> response) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 207 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 208 | if (!js_platform || !js_platform->app_response) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 209 | return -1; |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 210 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 211 | ByteString bsQuestion = Question.ToUTF16LE(); |
| 212 | ByteString bsTitle = Title.ToUTF16LE(); |
| 213 | ByteString bsDefault = Default.ToUTF16LE(); |
| 214 | ByteString bsLabel = Label.ToUTF16LE(); |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 215 | return js_platform->app_response( |
| 216 | js_platform, AsFPDFWideString(&bsQuestion), AsFPDFWideString(&bsTitle), |
| 217 | AsFPDFWideString(&bsDefault), AsFPDFWideString(&bsLabel), bPassword, |
Lei Zhang | f36006c | 2024-02-17 00:56:24 +0000 | [diff] [blame] | 218 | response.data(), pdfium::checked_cast<int>(response.size())); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 219 | } |
| 220 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 221 | void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 222 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 223 | if (!js_platform || !js_platform->app_beep) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 224 | return; |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 225 | |
| 226 | js_platform->app_beep(js_platform, nType); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 229 | WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 230 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 231 | if (!js_platform || !js_platform->Field_browse) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 232 | return WideString(); |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 233 | |
| 234 | const int nRequiredLen = js_platform->Field_browse(js_platform, nullptr, 0); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 235 | if (nRequiredLen <= 0) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 236 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 237 | |
Lei Zhang | 549bfb2 | 2022-08-11 19:45:29 +0000 | [diff] [blame] | 238 | DataVector<uint8_t> pBuff(nRequiredLen); |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 239 | const int nActualLen = |
| 240 | js_platform->Field_browse(js_platform, pBuff.data(), nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 241 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 242 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 243 | |
Tom Sepez | 322c0af | 2019-12-09 21:44:14 +0000 | [diff] [blame] | 244 | // Don't include trailing NUL. |
| 245 | pBuff.resize(nActualLen - 1); |
Tom Sepez | 3da2eca | 2022-05-19 00:10:38 +0000 | [diff] [blame] | 246 | |
| 247 | // Use FromDefANSI() per "local encoding" comment in fpdf_formfill.h. |
Tom Sepez | 4031714 | 2018-11-27 00:18:33 +0000 | [diff] [blame] | 248 | return WideString::FromDefANSI(ByteStringView(pBuff)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Lei Zhang | 8052463 | 2022-08-16 20:31:46 +0000 | [diff] [blame] | 251 | void CPDFSDK_FormFillEnvironment::JS_docmailForm( |
| 252 | pdfium::span<const uint8_t> mailData, |
| 253 | FPDF_BOOL bUI, |
| 254 | const WideString& To, |
| 255 | const WideString& Subject, |
| 256 | const WideString& CC, |
| 257 | const WideString& BCC, |
| 258 | const WideString& Msg) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 259 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 260 | if (!js_platform || !js_platform->Doc_mail) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 261 | return; |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 262 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 263 | ByteString bsTo = To.ToUTF16LE(); |
| 264 | ByteString bsSubject = Subject.ToUTF16LE(); |
| 265 | ByteString bsCC = CC.ToUTF16LE(); |
| 266 | ByteString bsBcc = BCC.ToUTF16LE(); |
| 267 | ByteString bsMsg = Msg.ToUTF16LE(); |
Lei Zhang | 8052463 | 2022-08-16 20:31:46 +0000 | [diff] [blame] | 268 | js_platform->Doc_mail(js_platform, const_cast<uint8_t*>(mailData.data()), |
Lei Zhang | f36006c | 2024-02-17 00:56:24 +0000 | [diff] [blame] | 269 | pdfium::checked_cast<int>(mailData.size()), bUI, |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 270 | AsFPDFWideString(&bsTo), AsFPDFWideString(&bsSubject), |
| 271 | AsFPDFWideString(&bsCC), AsFPDFWideString(&bsBcc), |
| 272 | AsFPDFWideString(&bsMsg)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 273 | } |
| 274 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 275 | void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI, |
| 276 | int nStart, |
| 277 | int nEnd, |
| 278 | FPDF_BOOL bSilent, |
| 279 | FPDF_BOOL bShrinkToFit, |
| 280 | FPDF_BOOL bPrintAsImage, |
| 281 | FPDF_BOOL bReverse, |
| 282 | FPDF_BOOL bAnnotations) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 283 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 284 | if (!js_platform || !js_platform->Doc_print) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 285 | return; |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 286 | |
| 287 | js_platform->Doc_print(js_platform, bUI, nStart, nEnd, bSilent, bShrinkToFit, |
| 288 | bPrintAsImage, bReverse, bAnnotations); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 289 | } |
| 290 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 291 | void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 292 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 293 | if (!js_platform || !js_platform->Doc_gotoPage) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 294 | return; |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 295 | |
| 296 | js_platform->Doc_gotoPage(js_platform, nPageNum); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 297 | } |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 298 | |
| 299 | WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() { |
Tom Sepez | e970397 | 2019-08-09 21:24:29 +0000 | [diff] [blame] | 300 | return GetFilePath(); |
| 301 | } |
| 302 | #endif // PDF_ENABLE_V8 |
| 303 | |
| 304 | WideString CPDFSDK_FormFillEnvironment::GetFilePath() const { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 305 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 306 | if (!js_platform || !js_platform->Doc_getFilePath) |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 307 | return WideString(); |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 308 | |
| 309 | const int nRequiredLen = |
| 310 | js_platform->Doc_getFilePath(js_platform, nullptr, 0); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 311 | if (nRequiredLen <= 0) |
| 312 | return WideString(); |
| 313 | |
Lei Zhang | 549bfb2 | 2022-08-11 19:45:29 +0000 | [diff] [blame] | 314 | DataVector<uint8_t> pBuff(nRequiredLen); |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 315 | const int nActualLen = |
| 316 | js_platform->Doc_getFilePath(js_platform, pBuff.data(), nRequiredLen); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 317 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 318 | return WideString(); |
| 319 | |
Tom Sepez | 322c0af | 2019-12-09 21:44:14 +0000 | [diff] [blame] | 320 | // Don't include trailing NUL. |
| 321 | pBuff.resize(nActualLen - 1); |
Tom Sepez | 3da2eca | 2022-05-19 00:10:38 +0000 | [diff] [blame] | 322 | |
| 323 | // Use FromDefANSI() per "local encoding" comment in fpdf_formfill.h. |
Tom Sepez | 4031714 | 2018-11-27 00:18:33 +0000 | [diff] [blame] | 324 | return WideString::FromDefANSI(ByteStringView(pBuff)); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Lei Zhang | a4eb5f6 | 2022-08-15 19:42:45 +0000 | [diff] [blame] | 327 | void CPDFSDK_FormFillEnvironment::SubmitForm( |
| 328 | pdfium::span<const uint8_t> form_data, |
| 329 | const WideString& URL) { |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 330 | IPDF_JSPLATFORM* js_platform = GetJSPlatform(); |
| 331 | if (!js_platform || !js_platform->Doc_submitForm) |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 332 | return; |
Daniel Hosseinian | b67b94d | 2020-05-04 18:43:54 +0000 | [diff] [blame] | 333 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 334 | ByteString bsUrl = URL.ToUTF16LE(); |
Lei Zhang | a4eb5f6 | 2022-08-15 19:42:45 +0000 | [diff] [blame] | 335 | js_platform->Doc_submitForm( |
| 336 | js_platform, const_cast<uint8_t*>(form_data.data()), |
| 337 | fxcrt::CollectionSize<int>(form_data), AsFPDFWideString(&bsUrl)); |
Tom Sepez | eb98dac | 2018-11-07 20:04:03 +0000 | [diff] [blame] | 338 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 339 | |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 340 | IJS_Runtime* CPDFSDK_FormFillEnvironment::GetIJSRuntime() { |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 341 | if (!m_pIJSRuntime) |
| 342 | m_pIJSRuntime = IJS_Runtime::Create(this); |
| 343 | return m_pIJSRuntime.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 346 | void CPDFSDK_FormFillEnvironment::Invalidate(IPDF_Page* page, |
Dan Sinclair | 6eec1c4 | 2017-02-21 17:20:43 -0500 | [diff] [blame] | 347 | const FX_RECT& rect) { |
| 348 | if (m_pInfo && m_pInfo->FFI_Invalidate) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 349 | m_pInfo->FFI_Invalidate(m_pInfo, FPDFPageFromIPDFPage(page), rect.left, |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 350 | rect.top, rect.right, rect.bottom); |
Dan Sinclair | 6eec1c4 | 2017-02-21 17:20:43 -0500 | [diff] [blame] | 351 | } |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Tom Sepez | 02cb570 | 2022-05-09 23:41:19 +0000 | [diff] [blame] | 354 | void CPDFSDK_FormFillEnvironment::SetCursor( |
| 355 | IPWL_FillerNotify::CursorStyle nCursorType) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 356 | if (m_pInfo && m_pInfo->FFI_SetCursor) |
Tom Sepez | 230db1d | 2021-05-19 01:56:54 +0000 | [diff] [blame] | 357 | m_pInfo->FFI_SetCursor(m_pInfo, static_cast<int>(nCursorType)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 358 | } |
| 359 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 360 | int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse, |
| 361 | TimerCallback lpTimerFunc) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 362 | if (m_pInfo && m_pInfo->FFI_SetTimer) |
| 363 | return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc); |
Tom Sepez | 8a5699d | 2020-10-13 21:23:53 +0000 | [diff] [blame] | 364 | return CFX_Timer::HandlerIface::kInvalidTimerID; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 365 | } |
| 366 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 367 | void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 368 | if (m_pInfo && m_pInfo->FFI_KillTimer) |
| 369 | m_pInfo->FFI_KillTimer(m_pInfo, nTimerID); |
| 370 | } |
| 371 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 372 | void CPDFSDK_FormFillEnvironment::OnChange() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 373 | if (m_pInfo && m_pInfo->FFI_OnChange) |
| 374 | m_pInfo->FFI_OnChange(m_pInfo); |
| 375 | } |
| 376 | |
Tom Sepez | b3208f5 | 2021-07-30 17:41:12 +0000 | [diff] [blame] | 377 | void CPDFSDK_FormFillEnvironment::ExecuteNamedAction( |
| 378 | const ByteString& namedAction) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 379 | if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction) |
Tom Sepez | b3208f5 | 2021-07-30 17:41:12 +0000 | [diff] [blame] | 380 | m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction.c_str()); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 383 | void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus(const WideString& text) { |
| 384 | OnSetFieldInputFocusInternal(text, true); |
| 385 | } |
| 386 | |
| 387 | void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocusInternal( |
| 388 | const WideString& text, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 389 | bool bFocus) { |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 390 | if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus) { |
Tom Sepez | 5ea5fa9 | 2022-03-08 00:15:23 +0000 | [diff] [blame] | 391 | size_t nCharacters = text.GetLength(); |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 392 | ByteString bsUTFText = text.ToUTF16LE(); |
| 393 | auto* pBuffer = reinterpret_cast<const unsigned short*>(bsUTFText.c_str()); |
Tom Sepez | 8ef63b9 | 2022-03-08 19:53:34 +0000 | [diff] [blame] | 394 | m_pInfo->FFI_SetTextFieldFocus( |
Lei Zhang | f36006c | 2024-02-17 00:56:24 +0000 | [diff] [blame] | 395 | m_pInfo, pBuffer, pdfium::checked_cast<FPDF_DWORD>(nCharacters), |
Tom Sepez | 8ef63b9 | 2022-03-08 19:53:34 +0000 | [diff] [blame] | 396 | bFocus); |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 397 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Tom Sepez | bf6f9b0 | 2021-09-13 23:45:59 +0000 | [diff] [blame] | 400 | void CPDFSDK_FormFillEnvironment::OnCalculate( |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 401 | ObservedPtr<CPDFSDK_Annot>& pAnnot) { |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 402 | ObservedPtr<CPDFSDK_Widget> pWidget(ToCPDFSDKWidget(pAnnot.Get())); |
| 403 | if (pWidget) { |
Tom Sepez | bf6f9b0 | 2021-09-13 23:45:59 +0000 | [diff] [blame] | 404 | m_pInteractiveForm->OnCalculate(pWidget->GetFormField()); |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 405 | } |
Tom Sepez | bf6f9b0 | 2021-09-13 23:45:59 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 408 | void CPDFSDK_FormFillEnvironment::OnFormat(ObservedPtr<CPDFSDK_Annot>& pAnnot) { |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 409 | ObservedPtr<CPDFSDK_Widget> pWidget(ToCPDFSDKWidget(pAnnot.Get())); |
Lei Zhang | 24c6be6 | 2024-02-08 20:06:48 +0000 | [diff] [blame] | 410 | std::optional<WideString> sValue = |
Tom Sepez | bf6f9b0 | 2021-09-13 23:45:59 +0000 | [diff] [blame] | 411 | m_pInteractiveForm->OnFormat(pWidget->GetFormField()); |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 412 | if (!pWidget) { |
Tom Sepez | bf6f9b0 | 2021-09-13 23:45:59 +0000 | [diff] [blame] | 413 | return; |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 414 | } |
Tom Sepez | bf6f9b0 | 2021-09-13 23:45:59 +0000 | [diff] [blame] | 415 | if (sValue.has_value()) { |
| 416 | m_pInteractiveForm->ResetFieldAppearance(pWidget->GetFormField(), sValue); |
| 417 | m_pInteractiveForm->UpdateField(pWidget->GetFormField()); |
| 418 | } |
| 419 | } |
| 420 | |
Tom Sepez | b3208f5 | 2021-07-30 17:41:12 +0000 | [diff] [blame] | 421 | void CPDFSDK_FormFillEnvironment::DoURIAction(const ByteString& bsURI, |
Tom Sepez | 6f9e9f6 | 2021-08-11 17:01:53 +0000 | [diff] [blame] | 422 | Mask<FWL_EVENTFLAG> modifiers) { |
Badhri Ravikumar | dacaac7 | 2020-04-23 18:00:38 +0000 | [diff] [blame] | 423 | if (!m_pInfo) |
| 424 | return; |
| 425 | |
| 426 | if (m_pInfo->version >= 2 && m_pInfo->FFI_DoURIActionWithKeyboardModifier) { |
Tom Sepez | b3208f5 | 2021-07-30 17:41:12 +0000 | [diff] [blame] | 427 | m_pInfo->FFI_DoURIActionWithKeyboardModifier(m_pInfo, bsURI.c_str(), |
Tom Sepez | 6f9e9f6 | 2021-08-11 17:01:53 +0000 | [diff] [blame] | 428 | modifiers.UncheckedValue()); |
Badhri Ravikumar | dacaac7 | 2020-04-23 18:00:38 +0000 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
| 432 | if (m_pInfo->FFI_DoURIAction) |
Tom Sepez | b3208f5 | 2021-07-30 17:41:12 +0000 | [diff] [blame] | 433 | m_pInfo->FFI_DoURIAction(m_pInfo, bsURI.c_str()); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 434 | } |
| 435 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 436 | void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex, |
| 437 | int zoomMode, |
Tom Sepez | 1559cc2 | 2022-01-19 19:03:40 +0000 | [diff] [blame] | 438 | pdfium::span<float> fPosArray) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 439 | if (m_pInfo && m_pInfo->FFI_DoGoToAction) { |
Tom Sepez | 1559cc2 | 2022-01-19 19:03:40 +0000 | [diff] [blame] | 440 | m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray.data(), |
| 441 | fxcrt::CollectionSize<int>(fPosArray)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 446 | int CPDFSDK_FormFillEnvironment::GetPageViewCount() const { |
Lei Zhang | 549fe5d | 2021-06-18 23:46:09 +0000 | [diff] [blame] | 447 | return fxcrt::CollectionSize<int>(m_PageMap); |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void CPDFSDK_FormFillEnvironment::DisplayCaret(IPDF_Page* page, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 451 | FPDF_BOOL bVisible, |
| 452 | double left, |
| 453 | double top, |
| 454 | double right, |
| 455 | double bottom) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 456 | if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_DisplayCaret) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 457 | m_pInfo->FFI_DisplayCaret(m_pInfo, FPDFPageFromIPDFPage(page), bVisible, |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 458 | left, top, right, bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 462 | int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex() const { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 463 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetCurrentPageIndex) |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 464 | return -1; |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 465 | return m_pInfo->FFI_GetCurrentPageIndex( |
Tom Sepez | c4acde5 | 2022-10-28 00:47:46 +0000 | [diff] [blame] | 466 | m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 469 | void CPDFSDK_FormFillEnvironment::SetCurrentPage(int iCurPage) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 470 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_SetCurrentPage) |
Tom Sepez | da8063f | 2018-04-30 17:41:49 +0000 | [diff] [blame] | 471 | return; |
Tom Sepez | c4acde5 | 2022-10-28 00:47:46 +0000 | [diff] [blame] | 472 | m_pInfo->FFI_SetCurrentPage(m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc), |
| 473 | iCurPage); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 476 | void CPDFSDK_FormFillEnvironment::GotoURL(const WideString& wsURL) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 477 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GotoURL) |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 478 | return; |
| 479 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 480 | ByteString bsTo = wsURL.ToUTF16LE(); |
Tom Sepez | c4acde5 | 2022-10-28 00:47:46 +0000 | [diff] [blame] | 481 | m_pInfo->FFI_GotoURL(m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc), |
Tom Sepez | 74136f5 | 2018-04-27 20:29:37 +0000 | [diff] [blame] | 482 | AsFPDFWideString(&bsTo)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 485 | FS_RECTF CPDFSDK_FormFillEnvironment::GetPageViewRect(IPDF_Page* page) { |
Lei Zhang | 95008dd | 2018-12-13 21:10:23 +0000 | [diff] [blame] | 486 | FS_RECTF rect = {0.0f, 0.0f, 0.0f, 0.0f}; |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 487 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_GetPageViewRect) |
Lei Zhang | 95008dd | 2018-12-13 21:10:23 +0000 | [diff] [blame] | 488 | return rect; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 489 | |
| 490 | double left; |
| 491 | double top; |
| 492 | double right; |
| 493 | double bottom; |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 494 | m_pInfo->FFI_GetPageViewRect(m_pInfo, FPDFPageFromIPDFPage(page), &left, &top, |
| 495 | &right, &bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 496 | |
Lei Zhang | 95008dd | 2018-12-13 21:10:23 +0000 | [diff] [blame] | 497 | rect.left = static_cast<float>(left); |
| 498 | rect.top = static_cast<float>(top); |
| 499 | rect.bottom = static_cast<float>(bottom); |
| 500 | rect.right = static_cast<float>(right); |
| 501 | return rect; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Tom Sepez | d48bd29 | 2019-08-14 19:48:55 +0000 | [diff] [blame] | 504 | bool CPDFSDK_FormFillEnvironment::PopupMenu(IPDF_Page* page, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 505 | int menuFlag, |
Lei Zhang | c1cd2ea | 2019-07-10 20:00:42 +0000 | [diff] [blame] | 506 | const CFX_PointF& pt) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 507 | return m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_PopupMenu && |
Tom Sepez | 094f8e6 | 2020-04-01 21:01:44 +0000 | [diff] [blame] | 508 | m_pInfo->FFI_PopupMenu(m_pInfo, FPDFPageFromIPDFPage(page), nullptr, |
Tom Sepez | 3f3c39d | 2018-05-01 17:46:34 +0000 | [diff] [blame] | 509 | menuFlag, pt.x, pt.y); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 510 | } |
| 511 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 512 | void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler, |
| 513 | FPDF_WIDESTRING pTo, |
| 514 | FPDF_WIDESTRING pSubject, |
| 515 | FPDF_WIDESTRING pCC, |
| 516 | FPDF_WIDESTRING pBcc, |
| 517 | FPDF_WIDESTRING pMsg) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 518 | if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_EmailTo) |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 519 | m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg); |
| 520 | } |
| 521 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 522 | void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler, |
| 523 | int fileFlag, |
| 524 | FPDF_WIDESTRING uploadTo) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 525 | if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_UploadTo) |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 526 | m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo); |
| 527 | } |
| 528 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 529 | FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType, |
| 530 | FPDF_WIDESTRING wsURL, |
| 531 | const char* mode) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 532 | if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_OpenFile) |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 533 | return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode); |
| 534 | return nullptr; |
| 535 | } |
| 536 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 537 | RetainPtr<IFX_SeekableReadStream> CPDFSDK_FormFillEnvironment::DownloadFromURL( |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 538 | const WideString& url) { |
Lei Zhang | f93f439 | 2024-08-16 18:10:00 +0000 | [diff] [blame] | 539 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_DownloadFromURL) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 540 | return nullptr; |
Lei Zhang | f93f439 | 2024-08-16 18:10:00 +0000 | [diff] [blame] | 541 | } |
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 bstrURL = url.ToUTF16LE(); |
Lei Zhang | f93f439 | 2024-08-16 18:10:00 +0000 | [diff] [blame] | 544 | FPDF_FILEHANDLER* file_handler = |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 545 | m_pInfo->FFI_DownloadFromURL(m_pInfo, AsFPDFWideString(&bstrURL)); |
Lei Zhang | f93f439 | 2024-08-16 18:10:00 +0000 | [diff] [blame] | 546 | if (!file_handler) { |
| 547 | return nullptr; |
| 548 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 549 | |
Lei Zhang | f93f439 | 2024-08-16 18:10:00 +0000 | [diff] [blame] | 550 | return MakeSeekableStream(file_handler); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 553 | WideString CPDFSDK_FormFillEnvironment::PostRequestURL( |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 554 | const WideString& wsURL, |
| 555 | const WideString& wsData, |
| 556 | const WideString& wsContentType, |
| 557 | const WideString& wsEncode, |
| 558 | const WideString& wsHeader) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 559 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_PostRequestURL) |
Tom Sepez | 890eac2 | 2018-12-03 20:35:51 +0000 | [diff] [blame] | 560 | return WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 561 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 562 | ByteString bsURL = wsURL.ToUTF16LE(); |
| 563 | ByteString bsData = wsData.ToUTF16LE(); |
| 564 | ByteString bsContentType = wsContentType.ToUTF16LE(); |
| 565 | ByteString bsEncode = wsEncode.ToUTF16LE(); |
| 566 | ByteString bsHeader = wsHeader.ToUTF16LE(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 567 | |
| 568 | FPDF_BSTR response; |
| 569 | FPDF_BStr_Init(&response); |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 570 | m_pInfo->FFI_PostRequestURL( |
| 571 | m_pInfo, AsFPDFWideString(&bsURL), AsFPDFWideString(&bsData), |
| 572 | AsFPDFWideString(&bsContentType), AsFPDFWideString(&bsEncode), |
| 573 | AsFPDFWideString(&bsHeader), &response); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 574 | |
Tom Sepez | 3b679fe | 2024-04-09 18:49:32 +0000 | [diff] [blame] | 575 | // SAFETY: required from FFI callback. |
| 576 | WideString wsRet = WideString::FromUTF16LE(UNSAFE_BUFFERS( |
| 577 | pdfium::make_span(reinterpret_cast<const uint8_t*>(response.str), |
| 578 | static_cast<size_t>(response.len)))); |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 579 | |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 580 | FPDF_BStr_Clear(&response); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 581 | return wsRet; |
| 582 | } |
| 583 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 584 | FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL( |
| 585 | const WideString& wsURL, |
| 586 | const WideString& wsData, |
| 587 | const WideString& wsEncode) { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 588 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_PutRequestURL) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 589 | return false; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 590 | |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 591 | ByteString bsURL = wsURL.ToUTF16LE(); |
| 592 | ByteString bsData = wsData.ToUTF16LE(); |
| 593 | ByteString bsEncode = wsEncode.ToUTF16LE(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 594 | |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 595 | return m_pInfo->FFI_PutRequestURL(m_pInfo, AsFPDFWideString(&bsURL), |
| 596 | AsFPDFWideString(&bsData), |
| 597 | AsFPDFWideString(&bsEncode)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 598 | } |
| 599 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 600 | void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount, |
| 601 | uint32_t dwEventType) const { |
Tom Sepez | 197d0d8 | 2019-12-18 20:28:20 +0000 | [diff] [blame] | 602 | if (m_pInfo && m_pInfo->version >= 2 && m_pInfo->FFI_PageEvent) |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 603 | m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType); |
| 604 | } |
| 605 | #endif // PDF_ENABLE_XFA |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 606 | |
| 607 | void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 608 | for (auto& it : m_PageMap) { |
Tom Sepez | a6646db | 2022-06-22 21:56:03 +0000 | [diff] [blame] | 609 | if (it.second->IsValidSDKAnnot(GetFocusAnnot())) { |
| 610 | ObservedPtr<CPDFSDK_PageView> pObserved(it.second.get()); |
Tom Sepez | 6f9e9f6 | 2021-08-11 17:01:53 +0000 | [diff] [blame] | 611 | KillFocusAnnot({}); |
Tom Sepez | a6646db | 2022-06-22 21:56:03 +0000 | [diff] [blame] | 612 | if (!pObserved) |
| 613 | break; |
| 614 | } |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Tom Sepez | cee8fbd | 2021-06-04 19:21:40 +0000 | [diff] [blame] | 618 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetOrCreatePageView( |
| 619 | IPDF_Page* pUnderlyingPage) { |
| 620 | CPDFSDK_PageView* pExisting = GetPageView(pUnderlyingPage); |
| 621 | if (pExisting) |
| 622 | return pExisting; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 623 | |
Tom Sepez | 5af9543 | 2020-05-15 22:55:16 +0000 | [diff] [blame] | 624 | auto pNew = std::make_unique<CPDFSDK_PageView>(this, pUnderlyingPage); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 625 | CPDFSDK_PageView* pPageView = pNew.get(); |
| 626 | m_PageMap[pUnderlyingPage] = std::move(pNew); |
| 627 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 628 | // Delay to load all the annotations, to avoid endless loop. |
| 629 | pPageView->LoadFXAnnots(); |
| 630 | return pPageView; |
| 631 | } |
| 632 | |
Tom Sepez | cee8fbd | 2021-06-04 19:21:40 +0000 | [diff] [blame] | 633 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView( |
| 634 | IPDF_Page* pUnderlyingPage) { |
| 635 | auto it = m_PageMap.find(pUnderlyingPage); |
| 636 | return it != m_PageMap.end() ? it->second.get() : nullptr; |
| 637 | } |
| 638 | |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 639 | CFX_Timer::HandlerIface* CPDFSDK_FormFillEnvironment::GetTimerHandler() { |
| 640 | return this; |
| 641 | } |
| 642 | |
Lei Zhang | b243293 | 2020-03-20 18:17:44 +0000 | [diff] [blame] | 643 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageViewAtIndex(int nIndex) { |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 644 | IPDF_Page* pTempPage = GetPage(nIndex); |
Tom Sepez | cee8fbd | 2021-06-04 19:21:40 +0000 | [diff] [blame] | 645 | return pTempPage ? GetPageView(pTempPage) : nullptr; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Tom Sepez | 20d0809 | 2020-01-29 21:27:44 +0000 | [diff] [blame] | 648 | void CPDFSDK_FormFillEnvironment::ProcJavascriptAction() { |
Tom Sepez | c4acde5 | 2022-10-28 00:47:46 +0000 | [diff] [blame] | 649 | auto name_tree = CPDF_NameTree::Create(m_pCPDFDoc, "JavaScript"); |
Lei Zhang | 1575b47 | 2020-04-02 23:09:15 +0000 | [diff] [blame] | 650 | if (!name_tree) |
| 651 | return; |
| 652 | |
| 653 | size_t count = name_tree->GetCount(); |
| 654 | for (size_t i = 0; i < count; ++i) { |
Tom Sepez | 20d0809 | 2020-01-29 21:27:44 +0000 | [diff] [blame] | 655 | WideString name; |
Tom Sepez | 525a427 | 2022-09-28 21:35:24 +0000 | [diff] [blame] | 656 | CPDF_Action action(ToDictionary(name_tree->LookupValueAndName(i, &name))); |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 657 | DoActionJavaScript(action, name); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 661 | bool CPDFSDK_FormFillEnvironment::ProcOpenAction() { |
Tom Sepez | aa51f00 | 2022-06-25 00:46:38 +0000 | [diff] [blame] | 662 | const CPDF_Dictionary* pRoot = m_pCPDFDoc->GetRoot(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 663 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 664 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 665 | |
Tom Sepez | c84dc1f | 2022-09-19 23:06:33 +0000 | [diff] [blame] | 666 | RetainPtr<const CPDF_Object> pOpenAction(pRoot->GetDictFor("OpenAction")); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 667 | if (!pOpenAction) |
| 668 | pOpenAction = pRoot->GetArrayFor("OpenAction"); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 669 | if (!pOpenAction) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 670 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 671 | |
| 672 | if (pOpenAction->IsArray()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 673 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 674 | |
Tom Sepez | af0fcf1 | 2022-09-27 18:12:53 +0000 | [diff] [blame] | 675 | RetainPtr<const CPDF_Dictionary> pDict = ToDictionary(pOpenAction); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 676 | if (!pDict) |
| 677 | return false; |
| 678 | |
Tom Sepez | af0fcf1 | 2022-09-27 18:12:53 +0000 | [diff] [blame] | 679 | DoActionDocOpen(CPDF_Action(std::move(pDict))); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 680 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 683 | void CPDFSDK_FormFillEnvironment::RemovePageView(IPDF_Page* pUnderlyingPage) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 684 | auto it = m_PageMap.find(pUnderlyingPage); |
| 685 | if (it == m_PageMap.end()) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 686 | return; |
| 687 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 688 | CPDFSDK_PageView* pPageView = it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 689 | if (pPageView->IsLocked() || pPageView->IsBeingDestroyed()) |
| 690 | return; |
| 691 | |
| 692 | // Mark the page view so we do not come into |RemovePageView| a second |
| 693 | // time while we're in the process of removing. |
| 694 | pPageView->SetBeingDestroyed(); |
| 695 | |
| 696 | // This must happen before we remove |pPageView| from the map because |
| 697 | // |KillFocusAnnot| can call into the |GetPage| method which will |
| 698 | // look for this page view in the map, if it doesn't find it a new one will |
| 699 | // be created. We then have two page views pointing to the same page and |
| 700 | // bad things happen. |
| 701 | if (pPageView->IsValidSDKAnnot(GetFocusAnnot())) |
Tom Sepez | 6f9e9f6 | 2021-08-11 17:01:53 +0000 | [diff] [blame] | 702 | KillFocusAnnot({}); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 703 | |
| 704 | // Remove the page from the map to make sure we don't accidentally attempt |
| 705 | // to use the |pPageView| while we're cleaning it up. |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 706 | m_PageMap.erase(it); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Tom Sepez | cee8fbd | 2021-06-04 19:21:40 +0000 | [diff] [blame] | 709 | IPDF_Page* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) const { |
Lei Zhang | 60fa2fc | 2017-07-21 17:42:19 -0700 | [diff] [blame] | 710 | if (!m_pInfo || !m_pInfo->FFI_GetPage) |
| 711 | return nullptr; |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 712 | return IPDFPageFromFPDFPage(m_pInfo->FFI_GetPage( |
Tom Sepez | c4acde5 | 2022-10-28 00:47:46 +0000 | [diff] [blame] | 713 | m_pInfo, FPDFDocumentFromCPDFDocument(m_pCPDFDoc), nIndex)); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Lei Zhang | 073ecf4 | 2018-10-11 16:56:00 +0000 | [diff] [blame] | 716 | CPDFSDK_InteractiveForm* CPDFSDK_FormFillEnvironment::GetInteractiveForm() { |
| 717 | if (!m_pInteractiveForm) |
Tom Sepez | 5af9543 | 2020-05-15 22:55:16 +0000 | [diff] [blame] | 718 | m_pInteractiveForm = std::make_unique<CPDFSDK_InteractiveForm>(this); |
Lei Zhang | 073ecf4 | 2018-10-11 16:56:00 +0000 | [diff] [blame] | 719 | return m_pInteractiveForm.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Tom Sepez | 2120c85 | 2021-10-28 23:34:26 +0000 | [diff] [blame] | 722 | void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_Annot* pAnnot) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 723 | for (const auto& it : m_PageMap) { |
Tom Sepez | a6646db | 2022-06-22 21:56:03 +0000 | [diff] [blame] | 724 | ObservedPtr<CPDFSDK_PageView> pObserved(it.second.get()); |
| 725 | if (pObserved) { |
| 726 | pObserved->UpdateView(pAnnot); |
| 727 | if (!pObserved) |
| 728 | break; |
| 729 | } |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 733 | CPDFSDK_Annot* CPDFSDK_FormFillEnvironment::GetFocusAnnot() const { |
| 734 | return m_pFocusAnnot.Get(); |
| 735 | } |
| 736 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 737 | bool CPDFSDK_FormFillEnvironment::SetFocusAnnot( |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 738 | ObservedPtr<CPDFSDK_Annot>& pAnnot) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 739 | if (m_bBeingDestroyed) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 740 | return false; |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 741 | if (m_pFocusAnnot == pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 742 | return true; |
Tom Sepez | 6f9e9f6 | 2021-08-11 17:01:53 +0000 | [diff] [blame] | 743 | if (m_pFocusAnnot && !KillFocusAnnot({})) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 744 | return false; |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 745 | if (!pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 746 | return false; |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 747 | if (!pAnnot->GetPageView()->IsValid()) |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 748 | return false; |
| 749 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 750 | if (m_pFocusAnnot) |
| 751 | return false; |
| 752 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 753 | #ifdef PDF_ENABLE_XFA |
Lei Zhang | b5910cf | 2022-04-08 21:22:51 +0000 | [diff] [blame] | 754 | CPDFXFA_Widget* pXFAWidget = pAnnot->AsXFAWidget(); |
Lei Zhang | 822b6ea | 2022-04-05 03:40:40 +0000 | [diff] [blame] | 755 | if (pXFAWidget && pXFAWidget->OnChangedFocus()) |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 756 | return false; |
Minh Tran | 0e71ce2 | 2019-07-29 22:28:49 +0000 | [diff] [blame] | 757 | |
Lei Zhang | 822b6ea | 2022-04-05 03:40:40 +0000 | [diff] [blame] | 758 | // `pAnnot` may be destroyed in `OnChangedFocus()`. |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 759 | if (!pAnnot) |
Minh Tran | 0e71ce2 | 2019-07-29 22:28:49 +0000 | [diff] [blame] | 760 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 761 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 02d7107 | 2021-09-21 22:43:01 +0000 | [diff] [blame] | 762 | |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 763 | if (!CPDFSDK_Annot::OnSetFocus(pAnnot, {})) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 764 | return false; |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 765 | } |
| 766 | if (m_pFocusAnnot) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 767 | return false; |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 768 | } |
| 769 | m_pFocusAnnot = pAnnot; |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 770 | |
| 771 | // If we are not able to inform the client about the focus change, it |
| 772 | // shouldn't be considered as failure. |
| 773 | SendOnFocusChange(pAnnot); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 774 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Lei Zhang | 2ea512b | 2022-04-08 21:10:01 +0000 | [diff] [blame] | 777 | bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(Mask<FWL_EVENTFLAG> nFlags) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 778 | if (!m_pFocusAnnot) |
| 779 | return false; |
| 780 | |
Tom Sepez | d8ae8f8 | 2019-06-12 17:58:33 +0000 | [diff] [blame] | 781 | ObservedPtr<CPDFSDK_Annot> pFocusAnnot(m_pFocusAnnot.Get()); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 782 | m_pFocusAnnot.Reset(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 783 | |
Lei Zhang | 2ea512b | 2022-04-08 21:10:01 +0000 | [diff] [blame] | 784 | if (!CPDFSDK_Annot::OnKillFocus(pFocusAnnot, nFlags)) { |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 785 | m_pFocusAnnot = pFocusAnnot; |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | |
Lei Zhang | 2ea512b | 2022-04-08 21:10:01 +0000 | [diff] [blame] | 789 | // Might have been destroyed by OnKillFocus(). |
Tom Sepez | 1c21d86 | 2020-07-24 22:14:41 +0000 | [diff] [blame] | 790 | if (!pFocusAnnot) |
| 791 | return false; |
| 792 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 793 | if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) { |
Tom Sepez | a0d8558 | 2024-05-20 22:25:24 +0000 | [diff] [blame] | 794 | const FormFieldType field_type = |
| 795 | ToCPDFSDKWidget(pFocusAnnot.Get())->GetFieldType(); |
| 796 | if (field_type == FormFieldType::kTextField || |
| 797 | field_type == FormFieldType::kComboBox) { |
Tom Sepez | d5ba169 | 2021-09-13 20:36:19 +0000 | [diff] [blame] | 798 | OnSetFieldInputFocusInternal(WideString(), false); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 801 | return !m_pFocusAnnot; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 802 | } |
Tom Sepez | a88399c | 2019-06-11 16:39:51 +0000 | [diff] [blame] | 803 | |
Tom Sepez | a1d3442 | 2018-04-24 20:54:41 +0000 | [diff] [blame] | 804 | int CPDFSDK_FormFillEnvironment::GetPageCount() const { |
Tom Sepez | df93cd3 | 2018-05-25 22:58:39 +0000 | [diff] [blame] | 805 | CPDF_Document::Extension* pExtension = m_pCPDFDoc->GetExtension(); |
| 806 | return pExtension ? pExtension->GetPageCount() : m_pCPDFDoc->GetPageCount(); |
Tom Sepez | a1d3442 | 2018-04-24 20:54:41 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Lei Zhang | 1f89399 | 2020-01-31 00:20:24 +0000 | [diff] [blame] | 809 | bool CPDFSDK_FormFillEnvironment::HasPermissions(uint32_t flags) const { |
JD Pierce | 1d550b8 | 2023-09-28 08:35:42 +0000 | [diff] [blame] | 810 | return !!(m_pCPDFDoc->GetUserPermissions(/*get_owner_perms=*/true) & flags); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 811 | } |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 812 | |
| 813 | void CPDFSDK_FormFillEnvironment::SendOnFocusChange( |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 814 | ObservedPtr<CPDFSDK_Annot>& pAnnot) { |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 815 | if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_OnFocusChange) |
| 816 | return; |
| 817 | |
Lei Zhang | c0d7928 | 2020-04-11 00:04:56 +0000 | [diff] [blame] | 818 | // TODO(crbug.com/pdfium/1482): Handle XFA case. |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 819 | if (pAnnot->AsXFAWidget()) |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 820 | return; |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 821 | |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 822 | CPDFSDK_PageView* pPageView = pAnnot->GetPageView(); |
Lei Zhang | c2ad628 | 2020-02-13 20:50:57 +0000 | [diff] [blame] | 823 | if (!pPageView->IsValid()) |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 824 | return; |
| 825 | |
Tom Sepez | 1d737dd | 2021-09-22 22:46:29 +0000 | [diff] [blame] | 826 | IPDF_Page* page = pAnnot->GetPage(); |
Lei Zhang | c0d7928 | 2020-04-11 00:04:56 +0000 | [diff] [blame] | 827 | if (!page) |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 828 | return; |
| 829 | |
Tom Sepez | b5649d9 | 2022-07-19 00:45:22 +0000 | [diff] [blame] | 830 | RetainPtr<CPDF_Dictionary> annot_dict = |
| 831 | pAnnot->GetPDFAnnot()->GetMutableAnnotDict(); |
Tom Sepez | 5af9543 | 2020-05-15 22:55:16 +0000 | [diff] [blame] | 832 | auto focused_annot = std::make_unique<CPDF_AnnotContext>(annot_dict, page); |
Aayush Dhir | c63913e | 2020-02-12 09:45:34 +0000 | [diff] [blame] | 833 | FPDF_ANNOTATION fpdf_annot = |
| 834 | FPDFAnnotationFromCPDFAnnotContext(focused_annot.get()); |
| 835 | |
| 836 | m_pInfo->FFI_OnFocusChange(m_pInfo, fpdf_annot, pPageView->GetPageIndex()); |
Lei Zhang | c2ad628 | 2020-02-13 20:50:57 +0000 | [diff] [blame] | 837 | } |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 838 | |
| 839 | bool CPDFSDK_FormFillEnvironment::DoActionDocOpen(const CPDF_Action& action) { |
| 840 | std::set<const CPDF_Dictionary*> visited; |
| 841 | return ExecuteDocumentOpenAction(action, &visited); |
| 842 | } |
| 843 | |
| 844 | bool CPDFSDK_FormFillEnvironment::DoActionJavaScript( |
| 845 | const CPDF_Action& JsAction, |
| 846 | WideString csJSName) { |
| 847 | if (JsAction.GetType() == CPDF_Action::Type::kJavaScript) { |
| 848 | WideString swJS = JsAction.GetJavaScript(); |
| 849 | if (!swJS.IsEmpty()) { |
| 850 | RunDocumentOpenJavaScript(csJSName, swJS); |
| 851 | return true; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | bool CPDFSDK_FormFillEnvironment::DoActionFieldJavaScript( |
| 859 | const CPDF_Action& JsAction, |
| 860 | CPDF_AAction::AActionType type, |
| 861 | CPDF_FormField* pFormField, |
| 862 | CFFL_FieldAction* data) { |
| 863 | if (IsJSPlatformPresent() && |
| 864 | JsAction.GetType() == CPDF_Action::Type::kJavaScript) { |
| 865 | WideString swJS = JsAction.GetJavaScript(); |
| 866 | if (!swJS.IsEmpty()) { |
| 867 | RunFieldJavaScript(pFormField, type, data, swJS); |
| 868 | return true; |
| 869 | } |
| 870 | } |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | bool CPDFSDK_FormFillEnvironment::DoActionLink(const CPDF_Action& action, |
| 875 | CPDF_AAction::AActionType type, |
| 876 | Mask<FWL_EVENTFLAG> modifiers) { |
| 877 | if (!CPDF_AAction::IsUserInput(type)) |
| 878 | return false; |
| 879 | |
| 880 | switch (action.GetType()) { |
| 881 | case CPDF_Action::Type::kGoTo: |
| 882 | DoActionGoTo(action); |
| 883 | return true; |
| 884 | case CPDF_Action::Type::kURI: |
| 885 | DoActionURI(action, modifiers); |
| 886 | return true; |
| 887 | default: |
| 888 | return false; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | bool CPDFSDK_FormFillEnvironment::DoActionDestination(const CPDF_Dest& dest) { |
| 893 | CPDF_Document* document = GetPDFDocument(); |
| 894 | DCHECK(document); |
| 895 | |
Tom Sepez | 14b3b8c | 2022-10-07 23:41:41 +0000 | [diff] [blame] | 896 | std::vector<float> positions = dest.GetScrollPositionArray(); |
| 897 | DoGoToAction(dest.GetDestPageIndex(document), dest.GetZoomMode(), positions); |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 898 | return true; |
| 899 | } |
| 900 | |
| 901 | bool CPDFSDK_FormFillEnvironment::DoActionPage( |
| 902 | const CPDF_Action& action, |
| 903 | CPDF_AAction::AActionType eType) { |
| 904 | std::set<const CPDF_Dictionary*> visited; |
| 905 | return ExecuteDocumentPageAction(action, eType, &visited); |
| 906 | } |
| 907 | |
| 908 | bool CPDFSDK_FormFillEnvironment::DoActionDocument( |
| 909 | const CPDF_Action& action, |
| 910 | CPDF_AAction::AActionType eType) { |
| 911 | std::set<const CPDF_Dictionary*> visited; |
| 912 | return ExecuteDocumentPageAction(action, eType, &visited); |
| 913 | } |
| 914 | |
| 915 | bool CPDFSDK_FormFillEnvironment::DoActionField(const CPDF_Action& action, |
| 916 | CPDF_AAction::AActionType type, |
| 917 | CPDF_FormField* pFormField, |
| 918 | CFFL_FieldAction* data) { |
| 919 | std::set<const CPDF_Dictionary*> visited; |
| 920 | return ExecuteFieldAction(action, type, pFormField, data, &visited); |
| 921 | } |
| 922 | |
| 923 | bool CPDFSDK_FormFillEnvironment::ExecuteDocumentOpenAction( |
| 924 | const CPDF_Action& action, |
| 925 | std::set<const CPDF_Dictionary*>* visited) { |
| 926 | const CPDF_Dictionary* pDict = action.GetDict(); |
| 927 | if (pdfium::Contains(*visited, pDict)) |
| 928 | return false; |
| 929 | |
| 930 | visited->insert(pDict); |
| 931 | |
| 932 | if (action.GetType() == CPDF_Action::Type::kJavaScript) { |
| 933 | if (IsJSPlatformPresent()) { |
| 934 | WideString swJS = action.GetJavaScript(); |
| 935 | if (!swJS.IsEmpty()) |
| 936 | RunDocumentOpenJavaScript(WideString(), swJS); |
| 937 | } |
| 938 | } else { |
| 939 | DoActionNoJs(action, CPDF_AAction::AActionType::kDocumentOpen); |
| 940 | } |
| 941 | |
| 942 | for (size_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) { |
| 943 | CPDF_Action subaction = action.GetSubAction(i); |
| 944 | if (!ExecuteDocumentOpenAction(subaction, visited)) |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | return true; |
| 949 | } |
| 950 | |
| 951 | bool CPDFSDK_FormFillEnvironment::ExecuteDocumentPageAction( |
| 952 | const CPDF_Action& action, |
| 953 | CPDF_AAction::AActionType type, |
| 954 | std::set<const CPDF_Dictionary*>* visited) { |
| 955 | const CPDF_Dictionary* pDict = action.GetDict(); |
| 956 | if (pdfium::Contains(*visited, pDict)) |
| 957 | return false; |
| 958 | |
| 959 | visited->insert(pDict); |
| 960 | |
| 961 | if (action.GetType() == CPDF_Action::Type::kJavaScript) { |
| 962 | if (IsJSPlatformPresent()) { |
| 963 | WideString swJS = action.GetJavaScript(); |
| 964 | if (!swJS.IsEmpty()) |
| 965 | RunDocumentPageJavaScript(type, swJS); |
| 966 | } |
| 967 | } else { |
| 968 | DoActionNoJs(action, type); |
| 969 | } |
| 970 | |
| 971 | for (size_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) { |
| 972 | CPDF_Action subaction = action.GetSubAction(i); |
| 973 | if (!ExecuteDocumentPageAction(subaction, type, visited)) |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | return true; |
| 978 | } |
| 979 | |
Tom Sepez | b5649d9 | 2022-07-19 00:45:22 +0000 | [diff] [blame] | 980 | bool CPDFSDK_FormFillEnvironment::IsValidField( |
| 981 | const CPDF_Dictionary* pFieldDict) { |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 982 | DCHECK(pFieldDict); |
| 983 | |
| 984 | CPDFSDK_InteractiveForm* pForm = GetInteractiveForm(); |
| 985 | CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm(); |
| 986 | return !!pPDFForm->GetFieldByDict(pFieldDict); |
| 987 | } |
| 988 | |
| 989 | bool CPDFSDK_FormFillEnvironment::ExecuteFieldAction( |
| 990 | const CPDF_Action& action, |
| 991 | CPDF_AAction::AActionType type, |
| 992 | CPDF_FormField* pFormField, |
| 993 | CFFL_FieldAction* data, |
| 994 | std::set<const CPDF_Dictionary*>* visited) { |
| 995 | const CPDF_Dictionary* pDict = action.GetDict(); |
| 996 | if (pdfium::Contains(*visited, pDict)) |
| 997 | return false; |
| 998 | |
| 999 | visited->insert(pDict); |
| 1000 | |
| 1001 | if (action.GetType() == CPDF_Action::Type::kJavaScript) { |
| 1002 | if (IsJSPlatformPresent()) { |
| 1003 | WideString swJS = action.GetJavaScript(); |
| 1004 | if (!swJS.IsEmpty()) { |
| 1005 | RunFieldJavaScript(pFormField, type, data, swJS); |
| 1006 | if (!IsValidField(pFormField->GetFieldDict())) |
| 1007 | return false; |
| 1008 | } |
| 1009 | } |
| 1010 | } else { |
| 1011 | DoActionNoJs(action, type); |
| 1012 | } |
| 1013 | |
| 1014 | for (size_t i = 0, sz = action.GetSubActionsCount(); i < sz; i++) { |
| 1015 | CPDF_Action subaction = action.GetSubAction(i); |
| 1016 | if (!ExecuteFieldAction(subaction, type, pFormField, data, visited)) |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
| 1023 | void CPDFSDK_FormFillEnvironment::DoActionNoJs(const CPDF_Action& action, |
| 1024 | CPDF_AAction::AActionType type) { |
| 1025 | switch (action.GetType()) { |
| 1026 | case CPDF_Action::Type::kGoTo: |
| 1027 | DoActionGoTo(action); |
| 1028 | break; |
| 1029 | case CPDF_Action::Type::kURI: |
| 1030 | if (CPDF_AAction::IsUserInput(type)) |
| 1031 | DoActionURI(action, Mask<FWL_EVENTFLAG>{}); |
| 1032 | break; |
| 1033 | case CPDF_Action::Type::kHide: |
| 1034 | DoActionHide(action); |
| 1035 | break; |
| 1036 | case CPDF_Action::Type::kNamed: |
| 1037 | DoActionNamed(action); |
| 1038 | break; |
| 1039 | case CPDF_Action::Type::kSubmitForm: |
| 1040 | if (CPDF_AAction::IsUserInput(type)) |
| 1041 | DoActionSubmitForm(action); |
| 1042 | break; |
| 1043 | case CPDF_Action::Type::kResetForm: |
| 1044 | DoActionResetForm(action); |
| 1045 | break; |
| 1046 | case CPDF_Action::Type::kJavaScript: |
Peter Boström | 75a7c4d | 2024-11-12 02:14:47 +0000 | [diff] [blame] | 1047 | NOTREACHED(); |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 1048 | case CPDF_Action::Type::kSetOCGState: |
| 1049 | case CPDF_Action::Type::kThread: |
| 1050 | case CPDF_Action::Type::kSound: |
| 1051 | case CPDF_Action::Type::kMovie: |
| 1052 | case CPDF_Action::Type::kRendition: |
| 1053 | case CPDF_Action::Type::kTrans: |
| 1054 | case CPDF_Action::Type::kGoTo3DView: |
| 1055 | case CPDF_Action::Type::kGoToR: |
| 1056 | case CPDF_Action::Type::kGoToE: |
| 1057 | case CPDF_Action::Type::kLaunch: |
| 1058 | case CPDF_Action::Type::kImportData: |
| 1059 | // Unimplemented |
| 1060 | break; |
| 1061 | default: |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void CPDFSDK_FormFillEnvironment::DoActionGoTo(const CPDF_Action& action) { |
| 1067 | DCHECK(action.GetDict()); |
| 1068 | |
| 1069 | CPDF_Document* pPDFDocument = GetPDFDocument(); |
| 1070 | DCHECK(pPDFDocument); |
| 1071 | |
| 1072 | CPDF_Dest MyDest = action.GetDest(pPDFDocument); |
| 1073 | DoActionDestination(MyDest); |
| 1074 | } |
| 1075 | |
| 1076 | void CPDFSDK_FormFillEnvironment::DoActionURI(const CPDF_Action& action, |
| 1077 | Mask<FWL_EVENTFLAG> modifiers) { |
| 1078 | DCHECK(action.GetDict()); |
| 1079 | DoURIAction(action.GetURI(GetPDFDocument()), modifiers); |
| 1080 | } |
| 1081 | |
| 1082 | void CPDFSDK_FormFillEnvironment::DoActionNamed(const CPDF_Action& action) { |
| 1083 | DCHECK(action.GetDict()); |
| 1084 | ExecuteNamedAction(action.GetNamedAction()); |
| 1085 | } |
| 1086 | |
| 1087 | void CPDFSDK_FormFillEnvironment::RunFieldJavaScript( |
| 1088 | CPDF_FormField* pFormField, |
| 1089 | CPDF_AAction::AActionType type, |
| 1090 | CFFL_FieldAction* data, |
| 1091 | const WideString& script) { |
| 1092 | DCHECK(type != CPDF_AAction::kCalculate); |
| 1093 | DCHECK(type != CPDF_AAction::kFormat); |
| 1094 | |
| 1095 | RunScript(script, [type, data, pFormField](IJS_EventContext* context) { |
| 1096 | switch (type) { |
| 1097 | case CPDF_AAction::kCursorEnter: |
| 1098 | context->OnField_MouseEnter(data->bModifier, data->bShift, pFormField); |
| 1099 | break; |
| 1100 | case CPDF_AAction::kCursorExit: |
| 1101 | context->OnField_MouseExit(data->bModifier, data->bShift, pFormField); |
| 1102 | break; |
| 1103 | case CPDF_AAction::kButtonDown: |
| 1104 | context->OnField_MouseDown(data->bModifier, data->bShift, pFormField); |
| 1105 | break; |
| 1106 | case CPDF_AAction::kButtonUp: |
| 1107 | context->OnField_MouseUp(data->bModifier, data->bShift, pFormField); |
| 1108 | break; |
| 1109 | case CPDF_AAction::kGetFocus: |
| 1110 | context->OnField_Focus(data->bModifier, data->bShift, pFormField, |
| 1111 | &data->sValue); |
| 1112 | break; |
| 1113 | case CPDF_AAction::kLoseFocus: |
| 1114 | context->OnField_Blur(data->bModifier, data->bShift, pFormField, |
| 1115 | &data->sValue); |
| 1116 | break; |
| 1117 | case CPDF_AAction::kKeyStroke: |
| 1118 | context->OnField_Keystroke( |
| 1119 | &data->sChange, data->sChangeEx, data->bKeyDown, data->bModifier, |
| 1120 | &data->nSelEnd, &data->nSelStart, data->bShift, pFormField, |
| 1121 | &data->sValue, data->bWillCommit, data->bFieldFull, &data->bRC); |
| 1122 | break; |
| 1123 | case CPDF_AAction::kValidate: |
| 1124 | context->OnField_Validate(&data->sChange, data->sChangeEx, |
| 1125 | data->bKeyDown, data->bModifier, data->bShift, |
| 1126 | pFormField, &data->sValue, &data->bRC); |
| 1127 | break; |
| 1128 | default: |
Peter Boström | 75a7c4d | 2024-11-12 02:14:47 +0000 | [diff] [blame] | 1129 | NOTREACHED(); |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 1130 | } |
| 1131 | }); |
| 1132 | } |
| 1133 | |
| 1134 | void CPDFSDK_FormFillEnvironment::RunDocumentOpenJavaScript( |
| 1135 | const WideString& sScriptName, |
| 1136 | const WideString& script) { |
| 1137 | RunScript(script, [sScriptName](IJS_EventContext* context) { |
| 1138 | context->OnDoc_Open(sScriptName); |
| 1139 | }); |
| 1140 | } |
| 1141 | |
| 1142 | void CPDFSDK_FormFillEnvironment::RunDocumentPageJavaScript( |
| 1143 | CPDF_AAction::AActionType type, |
| 1144 | const WideString& script) { |
| 1145 | RunScript(script, [type](IJS_EventContext* context) { |
| 1146 | switch (type) { |
| 1147 | case CPDF_AAction::kOpenPage: |
| 1148 | context->OnPage_Open(); |
| 1149 | break; |
| 1150 | case CPDF_AAction::kClosePage: |
| 1151 | context->OnPage_Close(); |
| 1152 | break; |
| 1153 | case CPDF_AAction::kCloseDocument: |
| 1154 | context->OnDoc_WillClose(); |
| 1155 | break; |
| 1156 | case CPDF_AAction::kSaveDocument: |
| 1157 | context->OnDoc_WillSave(); |
| 1158 | break; |
| 1159 | case CPDF_AAction::kDocumentSaved: |
| 1160 | context->OnDoc_DidSave(); |
| 1161 | break; |
| 1162 | case CPDF_AAction::kPrintDocument: |
| 1163 | context->OnDoc_WillPrint(); |
| 1164 | break; |
| 1165 | case CPDF_AAction::kDocumentPrinted: |
| 1166 | context->OnDoc_DidPrint(); |
| 1167 | break; |
| 1168 | case CPDF_AAction::kPageVisible: |
| 1169 | context->OnPage_InView(); |
| 1170 | break; |
| 1171 | case CPDF_AAction::kPageInvisible: |
| 1172 | context->OnPage_OutView(); |
| 1173 | break; |
| 1174 | default: |
Peter Boström | 75a7c4d | 2024-11-12 02:14:47 +0000 | [diff] [blame] | 1175 | NOTREACHED(); |
Tom Sepez | 946bf0f | 2022-04-08 22:06:41 +0000 | [diff] [blame] | 1176 | } |
| 1177 | }); |
| 1178 | } |
| 1179 | |
| 1180 | bool CPDFSDK_FormFillEnvironment::DoActionHide(const CPDF_Action& action) { |
| 1181 | CPDFSDK_InteractiveForm* pForm = GetInteractiveForm(); |
| 1182 | if (pForm->DoAction_Hide(action)) { |
| 1183 | SetChangeMark(); |
| 1184 | return true; |
| 1185 | } |
| 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | bool CPDFSDK_FormFillEnvironment::DoActionSubmitForm( |
| 1190 | const CPDF_Action& action) { |
| 1191 | CPDFSDK_InteractiveForm* pForm = GetInteractiveForm(); |
| 1192 | return pForm->DoAction_SubmitForm(action); |
| 1193 | } |
| 1194 | |
| 1195 | void CPDFSDK_FormFillEnvironment::DoActionResetForm(const CPDF_Action& action) { |
| 1196 | CPDFSDK_InteractiveForm* pForm = GetInteractiveForm(); |
| 1197 | pForm->DoAction_ResetForm(action); |
| 1198 | } |
| 1199 | |
| 1200 | void CPDFSDK_FormFillEnvironment::RunScript(const WideString& script, |
| 1201 | const RunScriptCallback& cb) { |
| 1202 | IJS_Runtime::ScopedEventContext pContext(GetIJSRuntime()); |
| 1203 | cb(pContext.Get()); |
| 1204 | pContext->RunScript(script); |
| 1205 | // TODO(dsinclair): Return error if RunScript returns a IJS_Runtime::JS_Error. |
| 1206 | } |