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> |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 11 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 13 | #include "core/fpdfdoc/cpdf_docjsactions.h" |
Dan Sinclair | 7d12532 | 2018-03-28 18:49:34 +0000 | [diff] [blame] | 14 | #include "fpdfsdk/cpdfsdk_actionhandler.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 15 | #include "fpdfsdk/cpdfsdk_annothandlermgr.h" |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 16 | #include "fpdfsdk/cpdfsdk_interform.h" |
| 17 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 18 | #include "fpdfsdk/cpdfsdk_widget.h" |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 20 | #include "fxjs/ijs_runtime.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 21 | #include "third_party/base/ptr_util.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 22 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 23 | FPDF_WIDESTRING AsFPDFWideString(ByteString* bsUTF16LE) { |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 24 | // Force a private version of the string, since we're about to hand it off |
| 25 | // to the embedder. Should the embedder modify it by accident, it won't |
| 26 | // corrupt other shares of the string beyond |bsUTF16LE|. |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 27 | return reinterpret_cast<FPDF_WIDESTRING>( |
Tom Sepez | 1dbfe99 | 2018-04-17 17:19:30 +0000 | [diff] [blame^] | 28 | bsUTF16LE->GetBuffer(bsUTF16LE->GetLength()).data()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 29 | } |
| 30 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 31 | CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( |
| 32 | UnderlyingDocumentType* pDoc, |
| 33 | FPDF_FORMFILLINFO* pFFinfo) |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 34 | : m_pInfo(pFFinfo), |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 35 | m_pUnderlyingDoc(pDoc), |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 36 | m_pSysHandler(pdfium::MakeUnique<CFX_SystemHandler>(this)), |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 37 | m_bChangeMask(false), |
| 38 | m_bBeingDestroyed(false) {} |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 39 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 40 | CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 41 | m_bBeingDestroyed = true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 42 | ClearAllFocusedAnnots(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 43 | |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 44 | // |m_PageMap| will try to access |m_pInterForm| when it cleans itself up. |
| 45 | // Make sure it is deleted before |m_pInterForm|. |
| 46 | m_PageMap.clear(); |
| 47 | |
| 48 | // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller| when it cleans |
| 49 | // itself up. Make sure it is deleted before |m_pFormFiller|. |
dsinclair | 709f5a9 | 2016-10-11 14:21:16 -0700 | [diff] [blame] | 50 | m_pAnnotHandlerMgr.reset(); |
| 51 | |
| 52 | // Must destroy the |m_pFormFiller| before the environment (|this|) |
| 53 | // because any created form widgets hold a pointer to the environment. |
| 54 | // Those widgets may call things like KillTimer() as they are shutdown. |
| 55 | m_pFormFiller.reset(); |
| 56 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 57 | if (m_pInfo && m_pInfo->Release) |
| 58 | m_pInfo->Release(m_pInfo); |
| 59 | } |
| 60 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 61 | int CPDFSDK_FormFillEnvironment::JS_appAlert(const wchar_t* Msg, |
| 62 | const wchar_t* Title, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 63 | uint32_t Type, |
| 64 | uint32_t Icon) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 65 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 66 | !m_pInfo->m_pJsPlatform->app_alert) { |
| 67 | return -1; |
| 68 | } |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 69 | ByteString bsMsg = WideString(Msg).UTF16LE_Encode(); |
| 70 | ByteString bsTitle = WideString(Title).UTF16LE_Encode(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 71 | return m_pInfo->m_pJsPlatform->app_alert( |
| 72 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), |
| 73 | AsFPDFWideString(&bsTitle), Type, Icon); |
| 74 | } |
| 75 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 76 | int CPDFSDK_FormFillEnvironment::JS_appResponse(const wchar_t* Question, |
| 77 | const wchar_t* Title, |
| 78 | const wchar_t* Default, |
| 79 | const wchar_t* cLabel, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 80 | FPDF_BOOL bPassword, |
| 81 | void* response, |
| 82 | int length) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 83 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 84 | !m_pInfo->m_pJsPlatform->app_response) { |
| 85 | return -1; |
| 86 | } |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 87 | ByteString bsQuestion = WideString(Question).UTF16LE_Encode(); |
| 88 | ByteString bsTitle = WideString(Title).UTF16LE_Encode(); |
| 89 | ByteString bsDefault = WideString(Default).UTF16LE_Encode(); |
| 90 | ByteString bsLabel = WideString(cLabel).UTF16LE_Encode(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 91 | return m_pInfo->m_pJsPlatform->app_response( |
| 92 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), |
| 93 | AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), |
| 94 | AsFPDFWideString(&bsLabel), bPassword, response, length); |
| 95 | } |
| 96 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 97 | void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 98 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 99 | !m_pInfo->m_pJsPlatform->app_beep) { |
| 100 | return; |
| 101 | } |
| 102 | m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); |
| 103 | } |
| 104 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 105 | WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 106 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 107 | !m_pInfo->m_pJsPlatform->Field_browse) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 108 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 109 | } |
| 110 | const int nRequiredLen = |
| 111 | m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
| 112 | if (nRequiredLen <= 0) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 113 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 114 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 115 | std::vector<uint8_t> pBuff(nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 116 | const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 117 | m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 118 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 119 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 120 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 121 | pBuff.resize(nActualLen); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 122 | return WideString::FromLocal(ByteStringView(pBuff)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 125 | WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 126 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 127 | !m_pInfo->m_pJsPlatform->Doc_getFilePath) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 128 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 129 | } |
| 130 | const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 131 | m_pInfo->m_pJsPlatform, nullptr, 0); |
| 132 | if (nRequiredLen <= 0) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 133 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 134 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 135 | std::vector<uint8_t> pBuff(nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 136 | const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 137 | m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 138 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 139 | return WideString(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 140 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 141 | pBuff.resize(nActualLen); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 142 | return WideString::FromLocal(ByteStringView(pBuff)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 143 | } |
| 144 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 145 | void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData, |
| 146 | int length, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 147 | const wchar_t* URL) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 148 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 149 | !m_pInfo->m_pJsPlatform->Doc_submitForm) { |
| 150 | return; |
| 151 | } |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 152 | ByteString bsDestination = WideString(URL).UTF16LE_Encode(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 153 | m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, |
| 154 | length, |
| 155 | AsFPDFWideString(&bsDestination)); |
| 156 | } |
| 157 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 158 | void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData, |
| 159 | int length, |
| 160 | FPDF_BOOL bUI, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 161 | const wchar_t* To, |
| 162 | const wchar_t* Subject, |
| 163 | const wchar_t* CC, |
| 164 | const wchar_t* BCC, |
| 165 | const wchar_t* Msg) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 166 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 167 | !m_pInfo->m_pJsPlatform->Doc_mail) { |
| 168 | return; |
| 169 | } |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 170 | ByteString bsTo = WideString(To).UTF16LE_Encode(); |
| 171 | ByteString bsSubject = WideString(Subject).UTF16LE_Encode(); |
| 172 | ByteString bsCC = WideString(CC).UTF16LE_Encode(); |
| 173 | ByteString bsBcc = WideString(BCC).UTF16LE_Encode(); |
| 174 | ByteString bsMsg = WideString(Msg).UTF16LE_Encode(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 175 | m_pInfo->m_pJsPlatform->Doc_mail( |
| 176 | m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), |
| 177 | AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), |
| 178 | AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); |
| 179 | } |
| 180 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 181 | void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI, |
| 182 | int nStart, |
| 183 | int nEnd, |
| 184 | FPDF_BOOL bSilent, |
| 185 | FPDF_BOOL bShrinkToFit, |
| 186 | FPDF_BOOL bPrintAsImage, |
| 187 | FPDF_BOOL bReverse, |
| 188 | FPDF_BOOL bAnnotations) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 189 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 190 | !m_pInfo->m_pJsPlatform->Doc_print) { |
| 191 | return; |
| 192 | } |
| 193 | m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, |
| 194 | bSilent, bShrinkToFit, bPrintAsImage, |
| 195 | bReverse, bAnnotations); |
| 196 | } |
| 197 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 198 | void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 199 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 200 | !m_pInfo->m_pJsPlatform->Doc_gotoPage) { |
| 201 | return; |
| 202 | } |
| 203 | m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); |
| 204 | } |
| 205 | |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 206 | IJS_Runtime* CPDFSDK_FormFillEnvironment::GetIJSRuntime() { |
| 207 | if (!IsJSPlatformPresent()) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 208 | return nullptr; |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 209 | if (!m_pIJSRuntime) |
| 210 | m_pIJSRuntime = IJS_Runtime::Create(this); |
| 211 | return m_pIJSRuntime.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 212 | } |
| 213 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 214 | CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 215 | if (!m_pAnnotHandlerMgr) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 216 | m_pAnnotHandlerMgr = pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(this); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 217 | return m_pAnnotHandlerMgr.get(); |
| 218 | } |
| 219 | |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 220 | CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHandler() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 221 | if (!m_pActionHandler) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 222 | m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 223 | return m_pActionHandler.get(); |
| 224 | } |
| 225 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 226 | CFFL_InteractiveFormFiller* |
| 227 | CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() { |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 228 | if (!m_pFormFiller) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 229 | m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this); |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 230 | return m_pFormFiller.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 231 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 232 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 233 | void CPDFSDK_FormFillEnvironment::Invalidate(UnderlyingPageType* page, |
Dan Sinclair | 6eec1c4 | 2017-02-21 17:20:43 -0500 | [diff] [blame] | 234 | const FX_RECT& rect) { |
| 235 | if (m_pInfo && m_pInfo->FFI_Invalidate) { |
| 236 | m_pInfo->FFI_Invalidate(m_pInfo, page, rect.left, rect.top, rect.right, |
| 237 | rect.bottom); |
| 238 | } |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Dan Sinclair | 60fd9fc | 2017-02-21 17:21:08 -0500 | [diff] [blame] | 241 | void CPDFSDK_FormFillEnvironment::OutputSelectedRect( |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 242 | UnderlyingPageType* page, |
Dan Sinclair | 60fd9fc | 2017-02-21 17:21:08 -0500 | [diff] [blame] | 243 | const CFX_FloatRect& rect) { |
| 244 | if (m_pInfo && m_pInfo->FFI_OutputSelectedRect) { |
| 245 | m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, rect.left, rect.top, |
| 246 | rect.right, rect.bottom); |
| 247 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 248 | } |
| 249 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 250 | void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 251 | if (m_pInfo && m_pInfo->FFI_SetCursor) |
| 252 | m_pInfo->FFI_SetCursor(m_pInfo, nCursorType); |
| 253 | } |
| 254 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 255 | int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse, |
| 256 | TimerCallback lpTimerFunc) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 257 | if (m_pInfo && m_pInfo->FFI_SetTimer) |
| 258 | return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc); |
| 259 | return -1; |
| 260 | } |
| 261 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 262 | void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 263 | if (m_pInfo && m_pInfo->FFI_KillTimer) |
| 264 | m_pInfo->FFI_KillTimer(m_pInfo, nTimerID); |
| 265 | } |
| 266 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 267 | FX_SYSTEMTIME CPDFSDK_FormFillEnvironment::GetLocalTime() const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 268 | FX_SYSTEMTIME fxtime; |
| 269 | if (!m_pInfo || !m_pInfo->FFI_GetLocalTime) |
| 270 | return fxtime; |
| 271 | |
| 272 | FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo); |
| 273 | fxtime.wDay = systime.wDay; |
| 274 | fxtime.wDayOfWeek = systime.wDayOfWeek; |
| 275 | fxtime.wHour = systime.wHour; |
| 276 | fxtime.wMilliseconds = systime.wMilliseconds; |
| 277 | fxtime.wMinute = systime.wMinute; |
| 278 | fxtime.wMonth = systime.wMonth; |
| 279 | fxtime.wSecond = systime.wSecond; |
| 280 | fxtime.wYear = systime.wYear; |
| 281 | return fxtime; |
| 282 | } |
| 283 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 284 | void CPDFSDK_FormFillEnvironment::OnChange() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 285 | if (m_pInfo && m_pInfo->FFI_OnChange) |
| 286 | m_pInfo->FFI_OnChange(m_pInfo); |
| 287 | } |
| 288 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 289 | FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage( |
| 290 | UnderlyingDocumentType* document) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 291 | if (m_pInfo && m_pInfo->FFI_GetCurrentPage) |
| 292 | return m_pInfo->FFI_GetCurrentPage(m_pInfo, document); |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 296 | void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(const char* namedAction) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 297 | if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction) |
| 298 | m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction); |
| 299 | } |
| 300 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 301 | void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus( |
| 302 | FPDF_WIDESTRING focusText, |
| 303 | FPDF_DWORD nTextLen, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 304 | bool bFocus) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 305 | if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus) |
| 306 | m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus); |
| 307 | } |
| 308 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 309 | void CPDFSDK_FormFillEnvironment::DoURIAction(const char* bsURI) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 310 | if (m_pInfo && m_pInfo->FFI_DoURIAction) |
| 311 | m_pInfo->FFI_DoURIAction(m_pInfo, bsURI); |
| 312 | } |
| 313 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 314 | void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex, |
| 315 | int zoomMode, |
| 316 | float* fPosArray, |
| 317 | int sizeOfArray) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 318 | if (m_pInfo && m_pInfo->FFI_DoGoToAction) { |
| 319 | m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray, |
| 320 | sizeOfArray); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | #ifdef PDF_ENABLE_XFA |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 325 | void CPDFSDK_FormFillEnvironment::DisplayCaret(CPDFXFA_Page* page, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 326 | FPDF_BOOL bVisible, |
| 327 | double left, |
| 328 | double top, |
| 329 | double right, |
| 330 | double bottom) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 331 | if (m_pInfo && m_pInfo->FFI_DisplayCaret) { |
| 332 | m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right, |
| 333 | bottom); |
| 334 | } |
| 335 | } |
| 336 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 337 | int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex( |
| 338 | CPDFXFA_Context* document) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 339 | if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex) |
| 340 | return -1; |
| 341 | return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document); |
| 342 | } |
| 343 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 344 | void CPDFSDK_FormFillEnvironment::SetCurrentPage(CPDFXFA_Context* document, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 345 | int iCurPage) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 346 | if (m_pInfo && m_pInfo->FFI_SetCurrentPage) |
| 347 | m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage); |
| 348 | } |
| 349 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 350 | WideString CPDFSDK_FormFillEnvironment::GetPlatform() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 351 | if (!m_pInfo || !m_pInfo->FFI_GetPlatform) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 352 | return WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 353 | |
| 354 | int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0); |
| 355 | if (nRequiredLen <= 0) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 356 | return WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 357 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 358 | std::vector<uint8_t> pBuff(nRequiredLen); |
| 359 | int nActualLen = |
| 360 | m_pInfo->FFI_GetPlatform(m_pInfo, pBuff.data(), nRequiredLen); |
| 361 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 362 | return WideString(); |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 363 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 364 | return WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()), |
| 365 | nActualLen / sizeof(uint16_t)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 368 | void CPDFSDK_FormFillEnvironment::GotoURL(CPDFXFA_Context* document, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 369 | const WideStringView& wsURL) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 370 | if (!m_pInfo || !m_pInfo->FFI_GotoURL) |
| 371 | return; |
| 372 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 373 | ByteString bsTo = WideString(wsURL).UTF16LE_Encode(); |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 374 | m_pInfo->FFI_GotoURL(m_pInfo, document, AsFPDFWideString(&bsTo)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 377 | void CPDFSDK_FormFillEnvironment::GetPageViewRect(CPDFXFA_Page* page, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 378 | FS_RECTF& dstRect) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 379 | if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect) |
| 380 | return; |
| 381 | |
| 382 | double left; |
| 383 | double top; |
| 384 | double right; |
| 385 | double bottom; |
| 386 | m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom); |
| 387 | |
| 388 | dstRect.left = static_cast<float>(left); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 389 | dstRect.top = static_cast<float>(top); |
| 390 | dstRect.bottom = static_cast<float>(bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 391 | dstRect.right = static_cast<float>(right); |
| 392 | } |
| 393 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 394 | bool CPDFSDK_FormFillEnvironment::PopupMenu(CPDFXFA_Page* page, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 395 | FPDF_WIDGET hWidget, |
| 396 | int menuFlag, |
| 397 | CFX_PointF pt) { |
tsepez | dc0401a | 2016-10-28 13:10:35 -0700 | [diff] [blame] | 398 | return m_pInfo && m_pInfo->FFI_PopupMenu && |
| 399 | m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 400 | } |
| 401 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 402 | void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg, |
| 403 | FPDF_WIDESTRING Title, |
| 404 | int Type, |
| 405 | int Icon) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 406 | if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { |
| 407 | m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type, |
| 408 | Icon); |
| 409 | } |
| 410 | } |
| 411 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 412 | void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler, |
| 413 | FPDF_WIDESTRING pTo, |
| 414 | FPDF_WIDESTRING pSubject, |
| 415 | FPDF_WIDESTRING pCC, |
| 416 | FPDF_WIDESTRING pBcc, |
| 417 | FPDF_WIDESTRING pMsg) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 418 | if (m_pInfo && m_pInfo->FFI_EmailTo) |
| 419 | m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg); |
| 420 | } |
| 421 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 422 | void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler, |
| 423 | int fileFlag, |
| 424 | FPDF_WIDESTRING uploadTo) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 425 | if (m_pInfo && m_pInfo->FFI_UploadTo) |
| 426 | m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo); |
| 427 | } |
| 428 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 429 | FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType, |
| 430 | FPDF_WIDESTRING wsURL, |
| 431 | const char* mode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 432 | if (m_pInfo && m_pInfo->FFI_OpenFile) |
| 433 | return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode); |
| 434 | return nullptr; |
| 435 | } |
| 436 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 437 | RetainPtr<IFX_SeekableReadStream> CPDFSDK_FormFillEnvironment::DownloadFromURL( |
| 438 | const wchar_t* url) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 439 | if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL) |
| 440 | return nullptr; |
| 441 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 442 | ByteString bstrURL = WideString(url).UTF16LE_Encode(); |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 443 | FPDF_LPFILEHANDLER fileHandler = |
| 444 | m_pInfo->FFI_DownloadFromURL(m_pInfo, AsFPDFWideString(&bstrURL)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 445 | |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 446 | return MakeSeekableStream(fileHandler); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 449 | WideString CPDFSDK_FormFillEnvironment::PostRequestURL( |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 450 | const wchar_t* wsURL, |
| 451 | const wchar_t* wsData, |
| 452 | const wchar_t* wsContentType, |
| 453 | const wchar_t* wsEncode, |
| 454 | const wchar_t* wsHeader) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 455 | if (!m_pInfo || !m_pInfo->FFI_PostRequestURL) |
| 456 | return L""; |
| 457 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 458 | ByteString bsURL = WideString(wsURL).UTF16LE_Encode(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 459 | ByteString bsData = WideString(wsData).UTF16LE_Encode(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 460 | ByteString bsContentType = WideString(wsContentType).UTF16LE_Encode(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 461 | ByteString bsEncode = WideString(wsEncode).UTF16LE_Encode(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 462 | ByteString bsHeader = WideString(wsHeader).UTF16LE_Encode(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 463 | |
| 464 | FPDF_BSTR response; |
| 465 | FPDF_BStr_Init(&response); |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 466 | m_pInfo->FFI_PostRequestURL( |
| 467 | m_pInfo, AsFPDFWideString(&bsURL), AsFPDFWideString(&bsData), |
| 468 | AsFPDFWideString(&bsContentType), AsFPDFWideString(&bsEncode), |
| 469 | AsFPDFWideString(&bsHeader), &response); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 470 | |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 471 | WideString wsRet = |
| 472 | WideString::FromUTF16LE(reinterpret_cast<FPDF_WIDESTRING>(response.str), |
| 473 | response.len / sizeof(FPDF_WIDESTRING)); |
| 474 | |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 475 | FPDF_BStr_Clear(&response); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 476 | return wsRet; |
| 477 | } |
| 478 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 479 | FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const wchar_t* wsURL, |
| 480 | const wchar_t* wsData, |
| 481 | const wchar_t* wsEncode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 482 | if (!m_pInfo || !m_pInfo->FFI_PutRequestURL) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 483 | return false; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 484 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 485 | ByteString bsURL = WideString(wsURL).UTF16LE_Encode(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 486 | ByteString bsData = WideString(wsData).UTF16LE_Encode(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 487 | ByteString bsEncode = WideString(wsEncode).UTF16LE_Encode(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 488 | |
Tom Sepez | 1e934f6 | 2018-04-11 21:11:28 +0000 | [diff] [blame] | 489 | return m_pInfo->FFI_PutRequestURL(m_pInfo, AsFPDFWideString(&bsURL), |
| 490 | AsFPDFWideString(&bsData), |
| 491 | AsFPDFWideString(&bsEncode)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 494 | WideString CPDFSDK_FormFillEnvironment::GetLanguage() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 495 | if (!m_pInfo || !m_pInfo->FFI_GetLanguage) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 496 | return WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 497 | |
| 498 | int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0); |
| 499 | if (nRequiredLen <= 0) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 500 | return WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 501 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 502 | std::vector<uint8_t> pBuff(nRequiredLen); |
| 503 | int nActualLen = |
| 504 | m_pInfo->FFI_GetLanguage(m_pInfo, pBuff.data(), nRequiredLen); |
| 505 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 506 | return WideString(); |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 507 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 508 | return WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()), |
| 509 | nActualLen / sizeof(uint16_t)); |
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::PageEvent(int iPageCount, |
| 513 | uint32_t dwEventType) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 514 | if (m_pInfo && m_pInfo->FFI_PageEvent) |
| 515 | m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType); |
| 516 | } |
| 517 | #endif // PDF_ENABLE_XFA |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 518 | |
| 519 | void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 520 | for (auto& it : m_PageMap) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 521 | if (it.second->IsValidSDKAnnot(GetFocusAnnot())) |
| 522 | KillFocusAnnot(0); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView( |
| 527 | UnderlyingPageType* pUnderlyingPage, |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 528 | bool renew) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 529 | auto it = m_PageMap.find(pUnderlyingPage); |
| 530 | if (it != m_PageMap.end()) |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 531 | return it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 532 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 533 | if (!renew) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 534 | return nullptr; |
| 535 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 536 | auto pNew = pdfium::MakeUnique<CPDFSDK_PageView>(this, pUnderlyingPage); |
| 537 | CPDFSDK_PageView* pPageView = pNew.get(); |
| 538 | m_PageMap[pUnderlyingPage] = std::move(pNew); |
| 539 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 540 | // Delay to load all the annotations, to avoid endless loop. |
| 541 | pPageView->LoadFXAnnots(); |
| 542 | return pPageView; |
| 543 | } |
| 544 | |
| 545 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() { |
| 546 | UnderlyingPageType* pPage = |
Tom Sepez | 940967d | 2017-05-18 12:32:20 -0700 | [diff] [blame] | 547 | UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc.Get())); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 548 | return pPage ? GetPageView(pPage, true) : nullptr; |
| 549 | } |
| 550 | |
| 551 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) { |
Lei Zhang | 60fa2fc | 2017-07-21 17:42:19 -0700 | [diff] [blame] | 552 | UnderlyingPageType* pTempPage = GetPage(nIndex); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 553 | if (!pTempPage) |
| 554 | return nullptr; |
| 555 | |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 556 | auto it = m_PageMap.find(pTempPage); |
| 557 | return it != m_PageMap.end() ? it->second.get() : nullptr; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() { |
| 561 | CPDF_Document* pPDFDoc = GetPDFDocument(); |
| 562 | CPDF_DocJSActions docJS(pPDFDoc); |
| 563 | int iCount = docJS.CountJSActions(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 564 | for (int i = 0; i < iCount; i++) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 565 | WideString csJSName; |
Tom Sepez | c4a2b75 | 2017-04-07 13:56:13 -0700 | [diff] [blame] | 566 | CPDF_Action jsAction = docJS.GetJSActionAndName(i, &csJSName); |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 567 | GetActionHandler()->DoAction_JavaScript(jsAction, csJSName, this); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 571 | bool CPDFSDK_FormFillEnvironment::ProcOpenAction() { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 572 | if (!m_pUnderlyingDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 573 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 574 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 575 | const CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 576 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 577 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 578 | |
| 579 | CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction"); |
| 580 | if (!pOpenAction) |
| 581 | pOpenAction = pRoot->GetArrayFor("OpenAction"); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 582 | if (!pOpenAction) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 583 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 584 | |
| 585 | if (pOpenAction->IsArray()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 586 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 587 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 588 | CPDF_Dictionary* pDict = pOpenAction->AsDictionary(); |
| 589 | if (!pDict) |
| 590 | return false; |
| 591 | |
| 592 | CPDF_Action action(pDict); |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 593 | GetActionHandler()->DoAction_DocOpen(action, this); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 594 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void CPDFSDK_FormFillEnvironment::RemovePageView( |
| 598 | UnderlyingPageType* pUnderlyingPage) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 599 | auto it = m_PageMap.find(pUnderlyingPage); |
| 600 | if (it == m_PageMap.end()) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 601 | return; |
| 602 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 603 | CPDFSDK_PageView* pPageView = it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 604 | if (pPageView->IsLocked() || pPageView->IsBeingDestroyed()) |
| 605 | return; |
| 606 | |
| 607 | // Mark the page view so we do not come into |RemovePageView| a second |
| 608 | // time while we're in the process of removing. |
| 609 | pPageView->SetBeingDestroyed(); |
| 610 | |
| 611 | // This must happen before we remove |pPageView| from the map because |
| 612 | // |KillFocusAnnot| can call into the |GetPage| method which will |
| 613 | // look for this page view in the map, if it doesn't find it a new one will |
| 614 | // be created. We then have two page views pointing to the same page and |
| 615 | // bad things happen. |
| 616 | if (pPageView->IsValidSDKAnnot(GetFocusAnnot())) |
| 617 | KillFocusAnnot(0); |
| 618 | |
| 619 | // Remove the page from the map to make sure we don't accidentally attempt |
| 620 | // to use the |pPageView| while we're cleaning it up. |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 621 | m_PageMap.erase(it); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) { |
Lei Zhang | 60fa2fc | 2017-07-21 17:42:19 -0700 | [diff] [blame] | 625 | if (!m_pInfo || !m_pInfo->FFI_GetPage) |
| 626 | return nullptr; |
| 627 | return UnderlyingFromFPDFPage( |
| 628 | m_pInfo->FFI_GetPage(m_pInfo, m_pUnderlyingDoc.Get(), nIndex)); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() { |
| 632 | if (!m_pInterForm) |
| 633 | m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this); |
| 634 | return m_pInterForm.get(); |
| 635 | } |
| 636 | |
| 637 | void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender, |
| 638 | CPDFSDK_Annot* pAnnot) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 639 | for (const auto& it : m_PageMap) { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 640 | CPDFSDK_PageView* pPageView = it.second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 641 | if (pPageView != pSender) |
| 642 | pPageView->UpdateView(pAnnot); |
| 643 | } |
| 644 | } |
| 645 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 646 | bool CPDFSDK_FormFillEnvironment::SetFocusAnnot( |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 647 | CPDFSDK_Annot::ObservedPtr* pAnnot) { |
| 648 | if (m_bBeingDestroyed) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 649 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 650 | if (m_pFocusAnnot == *pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 651 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 652 | if (m_pFocusAnnot && !KillFocusAnnot(0)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 653 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 654 | if (!*pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 655 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 656 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 657 | CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView(); |
| 658 | if (!pPageView || !pPageView->IsValid()) |
| 659 | return false; |
| 660 | |
| 661 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr(); |
| 662 | if (m_pFocusAnnot) |
| 663 | return false; |
| 664 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 665 | #ifdef PDF_ENABLE_XFA |
| 666 | CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get()); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 667 | if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot)) |
| 668 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 669 | #endif // PDF_ENABLE_XFA |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 670 | if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0)) |
| 671 | return false; |
| 672 | if (m_pFocusAnnot) |
| 673 | return false; |
| 674 | |
| 675 | m_pFocusAnnot.Reset(pAnnot->Get()); |
| 676 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 677 | } |
| 678 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 679 | bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 680 | if (!m_pFocusAnnot) |
| 681 | return false; |
| 682 | |
| 683 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr(); |
| 684 | CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get()); |
| 685 | m_pFocusAnnot.Reset(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 686 | |
| 687 | #ifdef PDF_ENABLE_XFA |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 688 | CPDFSDK_Annot::ObservedPtr pNull; |
| 689 | if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot)) |
| 690 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 691 | #endif // PDF_ENABLE_XFA |
| 692 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 693 | if (!pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) { |
| 694 | m_pFocusAnnot.Reset(pFocusAnnot.Get()); |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) { |
| 699 | CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get()); |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 700 | FormFieldType fieldType = pWidget->GetFieldType(); |
| 701 | if (fieldType == FormFieldType::kTextField || |
| 702 | fieldType == FormFieldType::kComboBox) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 703 | OnSetFieldInputFocus(nullptr, 0, false); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 706 | return !m_pFocusAnnot; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Lei Zhang | ad0982a | 2017-06-16 19:30:38 -0700 | [diff] [blame] | 709 | bool CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) const { |
tsepez | 95e5834 | 2016-10-28 11:34:42 -0700 | [diff] [blame] | 710 | return !!(GetPDFDocument()->GetUserPermissions() & nFlag); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 711 | } |