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