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> |
| 10 | |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/cpdfsdk_annothandlermgr.h" |
| 12 | #include "fpdfsdk/cpdfsdk_document.h" |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 14 | #include "fpdfsdk/fsdk_actionhandler.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 15 | #include "fpdfsdk/javascript/ijs_runtime.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 17 | |
| 18 | #ifdef PDF_ENABLE_XFA |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/fpdfxfa/cpdfxfa_app.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 20 | #endif // PDF_ENABLE_XFA |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken |
| 25 | // since modifying the result would impact |bsUTF16LE|. |
| 26 | FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { |
| 27 | return reinterpret_cast<FPDF_WIDESTRING>( |
| 28 | bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 33 | CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( |
| 34 | UnderlyingDocumentType* pDoc, |
| 35 | FPDF_FORMFILLINFO* pFFinfo) |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 36 | : m_pInfo(pFFinfo), |
| 37 | m_pSDKDoc(new CPDFSDK_Document(pDoc, this)), |
| 38 | m_pUnderlyingDoc(pDoc), |
| 39 | m_pSysHandler(new CFX_SystemHandler(this)) {} |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 40 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 41 | CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() { |
dsinclair | 709f5a9 | 2016-10-11 14:21:16 -0700 | [diff] [blame] | 42 | // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller| |
| 43 | // when it cleans up. So, we must make sure it is cleaned up before |
| 44 | // |m_pFormFiller|. |
| 45 | m_pAnnotHandlerMgr.reset(); |
| 46 | |
| 47 | // Must destroy the |m_pFormFiller| before the environment (|this|) |
| 48 | // because any created form widgets hold a pointer to the environment. |
| 49 | // Those widgets may call things like KillTimer() as they are shutdown. |
| 50 | m_pFormFiller.reset(); |
| 51 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 52 | #ifdef PDF_ENABLE_XFA |
| 53 | CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); |
| 54 | if (pProvider->m_pEnvList.GetSize() == 0) |
| 55 | pProvider->SetJavaScriptInitialized(FALSE); |
| 56 | #endif // PDF_ENABLE_XFA |
| 57 | if (m_pInfo && m_pInfo->Release) |
| 58 | m_pInfo->Release(m_pInfo); |
| 59 | } |
| 60 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 61 | int CPDFSDK_FormFillEnvironment::JS_appAlert(const FX_WCHAR* Msg, |
| 62 | const FX_WCHAR* Title, |
| 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 | } |
| 69 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 70 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 71 | return m_pInfo->m_pJsPlatform->app_alert( |
| 72 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), |
| 73 | AsFPDFWideString(&bsTitle), Type, Icon); |
| 74 | } |
| 75 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 76 | int CPDFSDK_FormFillEnvironment::JS_appResponse(const FX_WCHAR* Question, |
| 77 | const FX_WCHAR* Title, |
| 78 | const FX_WCHAR* Default, |
| 79 | const FX_WCHAR* cLabel, |
| 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 | } |
| 87 | CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); |
| 88 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 89 | CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); |
| 90 | CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); |
| 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 105 | CFX_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) { |
| 108 | return CFX_WideString(); |
| 109 | } |
| 110 | const int nRequiredLen = |
| 111 | m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
| 112 | if (nRequiredLen <= 0) |
| 113 | return CFX_WideString(); |
| 114 | |
| 115 | std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
| 116 | memset(pBuff.get(), 0, nRequiredLen); |
| 117 | const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
| 118 | m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
| 119 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 120 | return CFX_WideString(); |
| 121 | |
| 122 | return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); |
| 123 | } |
| 124 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 125 | CFX_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) { |
| 128 | return CFX_WideString(); |
| 129 | } |
| 130 | const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 131 | m_pInfo->m_pJsPlatform, nullptr, 0); |
| 132 | if (nRequiredLen <= 0) |
| 133 | return CFX_WideString(); |
| 134 | |
| 135 | std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
| 136 | memset(pBuff.get(), 0, nRequiredLen); |
| 137 | const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 138 | m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
| 139 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 140 | return CFX_WideString(); |
| 141 | |
| 142 | return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); |
| 143 | } |
| 144 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 145 | void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData, |
| 146 | int length, |
| 147 | const FX_WCHAR* 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 | } |
| 152 | CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); |
| 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, |
| 161 | const FX_WCHAR* To, |
| 162 | const FX_WCHAR* Subject, |
| 163 | const FX_WCHAR* CC, |
| 164 | const FX_WCHAR* BCC, |
| 165 | const FX_WCHAR* 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 | } |
| 170 | CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); |
| 171 | CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); |
| 172 | CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); |
| 173 | CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); |
| 174 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 206 | IJS_Runtime* CPDFSDK_FormFillEnvironment::GetJSRuntime() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 207 | if (!IsJSInitiated()) |
| 208 | return nullptr; |
| 209 | if (!m_pJSRuntime) |
| 210 | m_pJSRuntime.reset(IJS_Runtime::Create(this)); |
| 211 | return m_pJSRuntime.get(); |
| 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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 220 | CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHander() { |
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 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 233 | void CPDFSDK_FormFillEnvironment::Invalidate(FPDF_PAGE page, |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 234 | double left, |
| 235 | double top, |
| 236 | double right, |
| 237 | double bottom) { |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 238 | if (m_pInfo && m_pInfo->FFI_Invalidate) |
| 239 | m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom); |
| 240 | } |
| 241 | |
| 242 | void CPDFSDK_FormFillEnvironment::OutputSelectedRect(FPDF_PAGE page, |
| 243 | double left, |
| 244 | double top, |
| 245 | double right, |
| 246 | double bottom) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 247 | if (m_pInfo && m_pInfo->FFI_OutputSelectedRect) |
| 248 | m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom); |
| 249 | } |
| 250 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 251 | void CPDFSDK_FormFillEnvironment::SetCursor(int nCursorType) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 252 | if (m_pInfo && m_pInfo->FFI_SetCursor) |
| 253 | m_pInfo->FFI_SetCursor(m_pInfo, nCursorType); |
| 254 | } |
| 255 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 256 | int CPDFSDK_FormFillEnvironment::SetTimer(int uElapse, |
| 257 | TimerCallback lpTimerFunc) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 258 | if (m_pInfo && m_pInfo->FFI_SetTimer) |
| 259 | return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc); |
| 260 | return -1; |
| 261 | } |
| 262 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 263 | void CPDFSDK_FormFillEnvironment::KillTimer(int nTimerID) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 264 | if (m_pInfo && m_pInfo->FFI_KillTimer) |
| 265 | m_pInfo->FFI_KillTimer(m_pInfo, nTimerID); |
| 266 | } |
| 267 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 268 | FX_SYSTEMTIME CPDFSDK_FormFillEnvironment::GetLocalTime() const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 269 | FX_SYSTEMTIME fxtime; |
| 270 | if (!m_pInfo || !m_pInfo->FFI_GetLocalTime) |
| 271 | return fxtime; |
| 272 | |
| 273 | FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo); |
| 274 | fxtime.wDay = systime.wDay; |
| 275 | fxtime.wDayOfWeek = systime.wDayOfWeek; |
| 276 | fxtime.wHour = systime.wHour; |
| 277 | fxtime.wMilliseconds = systime.wMilliseconds; |
| 278 | fxtime.wMinute = systime.wMinute; |
| 279 | fxtime.wMonth = systime.wMonth; |
| 280 | fxtime.wSecond = systime.wSecond; |
| 281 | fxtime.wYear = systime.wYear; |
| 282 | return fxtime; |
| 283 | } |
| 284 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 285 | void CPDFSDK_FormFillEnvironment::OnChange() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 286 | if (m_pInfo && m_pInfo->FFI_OnChange) |
| 287 | m_pInfo->FFI_OnChange(m_pInfo); |
| 288 | } |
| 289 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 290 | FX_BOOL CPDFSDK_FormFillEnvironment::IsSHIFTKeyDown(uint32_t nFlag) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 291 | return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0; |
| 292 | } |
| 293 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 294 | FX_BOOL CPDFSDK_FormFillEnvironment::IsCTRLKeyDown(uint32_t nFlag) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 295 | return (nFlag & FWL_EVENTFLAG_ControlKey) != 0; |
| 296 | } |
| 297 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 298 | FX_BOOL CPDFSDK_FormFillEnvironment::IsALTKeyDown(uint32_t nFlag) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 299 | return (nFlag & FWL_EVENTFLAG_AltKey) != 0; |
| 300 | } |
| 301 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 302 | FPDF_PAGE CPDFSDK_FormFillEnvironment::GetPage(FPDF_DOCUMENT document, |
| 303 | int nPageIndex) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 304 | if (m_pInfo && m_pInfo->FFI_GetPage) |
| 305 | return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex); |
| 306 | return nullptr; |
| 307 | } |
| 308 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 309 | FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage(FPDF_DOCUMENT document) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 310 | if (m_pInfo && m_pInfo->FFI_GetCurrentPage) |
| 311 | return m_pInfo->FFI_GetCurrentPage(m_pInfo, document); |
| 312 | return nullptr; |
| 313 | } |
| 314 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 315 | void CPDFSDK_FormFillEnvironment::ExecuteNamedAction( |
| 316 | const FX_CHAR* namedAction) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 317 | if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction) |
| 318 | m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction); |
| 319 | } |
| 320 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 321 | void CPDFSDK_FormFillEnvironment::OnSetFieldInputFocus( |
| 322 | FPDF_WIDESTRING focusText, |
| 323 | FPDF_DWORD nTextLen, |
| 324 | FX_BOOL bFocus) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 325 | if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus) |
| 326 | m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus); |
| 327 | } |
| 328 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 329 | void CPDFSDK_FormFillEnvironment::DoURIAction(const FX_CHAR* bsURI) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 330 | if (m_pInfo && m_pInfo->FFI_DoURIAction) |
| 331 | m_pInfo->FFI_DoURIAction(m_pInfo, bsURI); |
| 332 | } |
| 333 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 334 | void CPDFSDK_FormFillEnvironment::DoGoToAction(int nPageIndex, |
| 335 | int zoomMode, |
| 336 | float* fPosArray, |
| 337 | int sizeOfArray) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 338 | if (m_pInfo && m_pInfo->FFI_DoGoToAction) { |
| 339 | m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray, |
| 340 | sizeOfArray); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | #ifdef PDF_ENABLE_XFA |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 345 | void CPDFSDK_FormFillEnvironment::DisplayCaret(FPDF_PAGE page, |
| 346 | FPDF_BOOL bVisible, |
| 347 | double left, |
| 348 | double top, |
| 349 | double right, |
| 350 | double bottom) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 351 | if (m_pInfo && m_pInfo->FFI_DisplayCaret) { |
| 352 | m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right, |
| 353 | bottom); |
| 354 | } |
| 355 | } |
| 356 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 357 | int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex(FPDF_DOCUMENT document) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 358 | if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex) |
| 359 | return -1; |
| 360 | return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document); |
| 361 | } |
| 362 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 363 | void CPDFSDK_FormFillEnvironment::SetCurrentPage(FPDF_DOCUMENT document, |
| 364 | int iCurPage) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 365 | if (m_pInfo && m_pInfo->FFI_SetCurrentPage) |
| 366 | m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage); |
| 367 | } |
| 368 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 369 | CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 370 | if (!m_pInfo || !m_pInfo->FFI_GetPlatform) |
| 371 | return L""; |
| 372 | |
| 373 | int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0); |
| 374 | if (nRequiredLen <= 0) |
| 375 | return L""; |
| 376 | |
| 377 | char* pbuff = new char[nRequiredLen]; |
| 378 | memset(pbuff, 0, nRequiredLen); |
| 379 | int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen); |
| 380 | if (nActualLen <= 0 || nActualLen > nRequiredLen) { |
| 381 | delete[] pbuff; |
| 382 | return L""; |
| 383 | } |
| 384 | CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen); |
| 385 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 386 | (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), |
| 387 | bsRet.GetLength() / sizeof(unsigned short)); |
| 388 | delete[] pbuff; |
| 389 | return wsRet; |
| 390 | } |
| 391 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 392 | void CPDFSDK_FormFillEnvironment::GotoURL(FPDF_DOCUMENT document, |
| 393 | const CFX_WideStringC& wsURL) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 394 | if (!m_pInfo || !m_pInfo->FFI_GotoURL) |
| 395 | return; |
| 396 | |
| 397 | CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 398 | FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength()); |
| 399 | m_pInfo->FFI_GotoURL(m_pInfo, document, pTo); |
| 400 | bsTo.ReleaseBuffer(); |
| 401 | } |
| 402 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 403 | void CPDFSDK_FormFillEnvironment::GetPageViewRect(FPDF_PAGE page, |
| 404 | FS_RECTF& dstRect) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 405 | if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect) |
| 406 | return; |
| 407 | |
| 408 | double left; |
| 409 | double top; |
| 410 | double right; |
| 411 | double bottom; |
| 412 | m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom); |
| 413 | |
| 414 | dstRect.left = static_cast<float>(left); |
| 415 | dstRect.top = static_cast<float>(top < bottom ? bottom : top); |
| 416 | dstRect.bottom = static_cast<float>(top < bottom ? top : bottom); |
| 417 | dstRect.right = static_cast<float>(right); |
| 418 | } |
| 419 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 420 | FX_BOOL CPDFSDK_FormFillEnvironment::PopupMenu(FPDF_PAGE page, |
| 421 | FPDF_WIDGET hWidget, |
| 422 | int menuFlag, |
| 423 | CFX_PointF pt) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 424 | if (!m_pInfo || !m_pInfo->FFI_PopupMenu) |
| 425 | return FALSE; |
| 426 | return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y); |
| 427 | } |
| 428 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 429 | void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg, |
| 430 | FPDF_WIDESTRING Title, |
| 431 | int Type, |
| 432 | int Icon) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 433 | if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { |
| 434 | m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type, |
| 435 | Icon); |
| 436 | } |
| 437 | } |
| 438 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 439 | void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler, |
| 440 | FPDF_WIDESTRING pTo, |
| 441 | FPDF_WIDESTRING pSubject, |
| 442 | FPDF_WIDESTRING pCC, |
| 443 | FPDF_WIDESTRING pBcc, |
| 444 | FPDF_WIDESTRING pMsg) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 445 | if (m_pInfo && m_pInfo->FFI_EmailTo) |
| 446 | m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg); |
| 447 | } |
| 448 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 449 | void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler, |
| 450 | int fileFlag, |
| 451 | FPDF_WIDESTRING uploadTo) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 452 | if (m_pInfo && m_pInfo->FFI_UploadTo) |
| 453 | m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo); |
| 454 | } |
| 455 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 456 | FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType, |
| 457 | FPDF_WIDESTRING wsURL, |
| 458 | const char* mode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 459 | if (m_pInfo && m_pInfo->FFI_OpenFile) |
| 460 | return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode); |
| 461 | return nullptr; |
| 462 | } |
| 463 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 464 | IFX_FileRead* CPDFSDK_FormFillEnvironment::DownloadFromURL( |
| 465 | const FX_WCHAR* url) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 466 | if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL) |
| 467 | return nullptr; |
| 468 | |
| 469 | CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode(); |
| 470 | FPDF_WIDESTRING wsURL = |
| 471 | (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength()); |
| 472 | |
| 473 | FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL); |
| 474 | |
| 475 | return new CFPDF_FileStream(fileHandler); |
| 476 | } |
| 477 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 478 | CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL( |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 479 | const FX_WCHAR* wsURL, |
| 480 | const FX_WCHAR* wsData, |
| 481 | const FX_WCHAR* wsContentType, |
| 482 | const FX_WCHAR* wsEncode, |
| 483 | const FX_WCHAR* wsHeader) { |
| 484 | if (!m_pInfo || !m_pInfo->FFI_PostRequestURL) |
| 485 | return L""; |
| 486 | |
| 487 | CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 488 | FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); |
| 489 | |
| 490 | CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode(); |
| 491 | FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); |
| 492 | |
| 493 | CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode(); |
| 494 | FPDF_WIDESTRING contentType = |
| 495 | (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength()); |
| 496 | |
| 497 | CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode(); |
| 498 | FPDF_WIDESTRING encode = |
| 499 | (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); |
| 500 | |
| 501 | CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode(); |
| 502 | FPDF_WIDESTRING header = |
| 503 | (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength()); |
| 504 | |
| 505 | FPDF_BSTR response; |
| 506 | FPDF_BStr_Init(&response); |
| 507 | m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header, |
| 508 | &response); |
| 509 | |
| 510 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 511 | (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING)); |
| 512 | FPDF_BStr_Clear(&response); |
| 513 | |
| 514 | return wsRet; |
| 515 | } |
| 516 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 517 | FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const FX_WCHAR* wsURL, |
| 518 | const FX_WCHAR* wsData, |
| 519 | const FX_WCHAR* wsEncode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 520 | if (!m_pInfo || !m_pInfo->FFI_PutRequestURL) |
| 521 | return FALSE; |
| 522 | |
| 523 | CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 524 | FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); |
| 525 | |
| 526 | CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode(); |
| 527 | FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); |
| 528 | |
| 529 | CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode(); |
| 530 | FPDF_WIDESTRING encode = |
| 531 | (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); |
| 532 | |
| 533 | return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode); |
| 534 | } |
| 535 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 536 | CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 537 | if (!m_pInfo || !m_pInfo->FFI_GetLanguage) |
| 538 | return L""; |
| 539 | |
| 540 | int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0); |
| 541 | if (nRequiredLen <= 0) |
| 542 | return L""; |
| 543 | |
| 544 | char* pbuff = new char[nRequiredLen]; |
| 545 | memset(pbuff, 0, nRequiredLen); |
| 546 | int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen); |
| 547 | if (nActualLen <= 0 || nActualLen > nRequiredLen) { |
| 548 | delete[] pbuff; |
| 549 | return L""; |
| 550 | } |
| 551 | CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen); |
| 552 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 553 | (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()), |
| 554 | bsRet.GetLength() / sizeof(FPDF_WIDESTRING)); |
| 555 | delete[] pbuff; |
| 556 | return wsRet; |
| 557 | } |
| 558 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 559 | void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount, |
| 560 | uint32_t dwEventType) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 561 | if (m_pInfo && m_pInfo->FFI_PageEvent) |
| 562 | m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType); |
| 563 | } |
| 564 | #endif // PDF_ENABLE_XFA |