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