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