dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 8 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 9 | #include <memory> |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 10 | #include <utility> |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 11 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 13 | #include "core/fpdfdoc/cpdf_docjsactions.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 14 | #include "fpdfsdk/cpdfsdk_annothandlermgr.h" |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 15 | #include "fpdfsdk/cpdfsdk_interform.h" |
| 16 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 17 | #include "fpdfsdk/cpdfsdk_widget.h" |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 18 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/fsdk_actionhandler.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 20 | #include "fpdfsdk/javascript/ijs_runtime.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 21 | #include "third_party/base/ptr_util.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 22 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken |
| 26 | // since modifying the result would impact |bsUTF16LE|. |
| 27 | FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { |
| 28 | return reinterpret_cast<FPDF_WIDESTRING>( |
| 29 | bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 34 | CPDFSDK_FormFillEnvironment::CPDFSDK_FormFillEnvironment( |
| 35 | UnderlyingDocumentType* pDoc, |
| 36 | FPDF_FORMFILLINFO* pFFinfo) |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 37 | : m_pInfo(pFFinfo), |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 38 | m_pUnderlyingDoc(pDoc), |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 39 | m_pSysHandler(pdfium::MakeUnique<CFX_SystemHandler>(this)), |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 40 | m_bChangeMask(false), |
| 41 | m_bBeingDestroyed(false) {} |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 42 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 43 | CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 44 | m_bBeingDestroyed = true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 45 | ClearAllFocusedAnnots(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 46 | |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 47 | // |m_PageMap| will try to access |m_pInterForm| when it cleans itself up. |
| 48 | // Make sure it is deleted before |m_pInterForm|. |
| 49 | m_PageMap.clear(); |
| 50 | |
| 51 | // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller| when it cleans |
| 52 | // itself up. Make sure it is deleted before |m_pFormFiller|. |
dsinclair | 709f5a9 | 2016-10-11 14:21:16 -0700 | [diff] [blame] | 53 | m_pAnnotHandlerMgr.reset(); |
| 54 | |
| 55 | // Must destroy the |m_pFormFiller| before the environment (|this|) |
| 56 | // because any created form widgets hold a pointer to the environment. |
| 57 | // Those widgets may call things like KillTimer() as they are shutdown. |
| 58 | m_pFormFiller.reset(); |
| 59 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 60 | if (m_pInfo && m_pInfo->Release) |
| 61 | m_pInfo->Release(m_pInfo); |
| 62 | } |
| 63 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 64 | int CPDFSDK_FormFillEnvironment::JS_appAlert(const wchar_t* Msg, |
| 65 | const wchar_t* Title, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 66 | uint32_t Type, |
| 67 | uint32_t Icon) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 68 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 69 | !m_pInfo->m_pJsPlatform->app_alert) { |
| 70 | return -1; |
| 71 | } |
| 72 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 73 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 74 | return m_pInfo->m_pJsPlatform->app_alert( |
| 75 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), |
| 76 | AsFPDFWideString(&bsTitle), Type, Icon); |
| 77 | } |
| 78 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 79 | int CPDFSDK_FormFillEnvironment::JS_appResponse(const wchar_t* Question, |
| 80 | const wchar_t* Title, |
| 81 | const wchar_t* Default, |
| 82 | const wchar_t* cLabel, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 83 | FPDF_BOOL bPassword, |
| 84 | void* response, |
| 85 | int length) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 86 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 87 | !m_pInfo->m_pJsPlatform->app_response) { |
| 88 | return -1; |
| 89 | } |
| 90 | CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); |
| 91 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 92 | CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); |
| 93 | CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); |
| 94 | return m_pInfo->m_pJsPlatform->app_response( |
| 95 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), |
| 96 | AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), |
| 97 | AsFPDFWideString(&bsLabel), bPassword, response, length); |
| 98 | } |
| 99 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 100 | void CPDFSDK_FormFillEnvironment::JS_appBeep(int nType) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 101 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 102 | !m_pInfo->m_pJsPlatform->app_beep) { |
| 103 | return; |
| 104 | } |
| 105 | m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); |
| 106 | } |
| 107 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 108 | CFX_WideString CPDFSDK_FormFillEnvironment::JS_fieldBrowse() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 109 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 110 | !m_pInfo->m_pJsPlatform->Field_browse) { |
| 111 | return CFX_WideString(); |
| 112 | } |
| 113 | const int nRequiredLen = |
| 114 | m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
| 115 | if (nRequiredLen <= 0) |
| 116 | return CFX_WideString(); |
| 117 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 118 | std::vector<uint8_t> pBuff(nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 119 | const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 120 | m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 121 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 122 | return CFX_WideString(); |
| 123 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 124 | pBuff.resize(nActualLen); |
| 125 | return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 128 | CFX_WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 129 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 130 | !m_pInfo->m_pJsPlatform->Doc_getFilePath) { |
| 131 | return CFX_WideString(); |
| 132 | } |
| 133 | const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 134 | m_pInfo->m_pJsPlatform, nullptr, 0); |
| 135 | if (nRequiredLen <= 0) |
| 136 | return CFX_WideString(); |
| 137 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 138 | std::vector<uint8_t> pBuff(nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 139 | const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 140 | m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 141 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 142 | return CFX_WideString(); |
| 143 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 144 | pBuff.resize(nActualLen); |
| 145 | return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 146 | } |
| 147 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 148 | void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData, |
| 149 | int length, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 150 | const wchar_t* URL) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 151 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 152 | !m_pInfo->m_pJsPlatform->Doc_submitForm) { |
| 153 | return; |
| 154 | } |
| 155 | CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); |
| 156 | m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, |
| 157 | length, |
| 158 | AsFPDFWideString(&bsDestination)); |
| 159 | } |
| 160 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 161 | void CPDFSDK_FormFillEnvironment::JS_docmailForm(void* mailData, |
| 162 | int length, |
| 163 | FPDF_BOOL bUI, |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 164 | const wchar_t* To, |
| 165 | const wchar_t* Subject, |
| 166 | const wchar_t* CC, |
| 167 | const wchar_t* BCC, |
| 168 | const wchar_t* Msg) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 169 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 170 | !m_pInfo->m_pJsPlatform->Doc_mail) { |
| 171 | return; |
| 172 | } |
| 173 | CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); |
| 174 | CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); |
| 175 | CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); |
| 176 | CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); |
| 177 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 178 | m_pInfo->m_pJsPlatform->Doc_mail( |
| 179 | m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), |
| 180 | AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), |
| 181 | AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); |
| 182 | } |
| 183 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 184 | void CPDFSDK_FormFillEnvironment::JS_docprint(FPDF_BOOL bUI, |
| 185 | int nStart, |
| 186 | int nEnd, |
| 187 | FPDF_BOOL bSilent, |
| 188 | FPDF_BOOL bShrinkToFit, |
| 189 | FPDF_BOOL bPrintAsImage, |
| 190 | FPDF_BOOL bReverse, |
| 191 | FPDF_BOOL bAnnotations) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 192 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 193 | !m_pInfo->m_pJsPlatform->Doc_print) { |
| 194 | return; |
| 195 | } |
| 196 | m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, |
| 197 | bSilent, bShrinkToFit, bPrintAsImage, |
| 198 | bReverse, bAnnotations); |
| 199 | } |
| 200 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 201 | void CPDFSDK_FormFillEnvironment::JS_docgotoPage(int nPageNum) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 202 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 203 | !m_pInfo->m_pJsPlatform->Doc_gotoPage) { |
| 204 | return; |
| 205 | } |
| 206 | m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); |
| 207 | } |
| 208 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 209 | IJS_Runtime* CPDFSDK_FormFillEnvironment::GetJSRuntime() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 210 | if (!IsJSInitiated()) |
| 211 | return nullptr; |
| 212 | if (!m_pJSRuntime) |
| 213 | m_pJSRuntime.reset(IJS_Runtime::Create(this)); |
| 214 | return m_pJSRuntime.get(); |
| 215 | } |
| 216 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 217 | CPDFSDK_AnnotHandlerMgr* CPDFSDK_FormFillEnvironment::GetAnnotHandlerMgr() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 218 | if (!m_pAnnotHandlerMgr) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 219 | m_pAnnotHandlerMgr = pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(this); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 220 | return m_pAnnotHandlerMgr.get(); |
| 221 | } |
| 222 | |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 223 | CPDFSDK_ActionHandler* CPDFSDK_FormFillEnvironment::GetActionHandler() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 224 | if (!m_pActionHandler) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 225 | m_pActionHandler = pdfium::MakeUnique<CPDFSDK_ActionHandler>(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 226 | return m_pActionHandler.get(); |
| 227 | } |
| 228 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 229 | CFFL_InteractiveFormFiller* |
| 230 | CPDFSDK_FormFillEnvironment::GetInteractiveFormFiller() { |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 231 | if (!m_pFormFiller) |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 232 | m_pFormFiller = pdfium::MakeUnique<CFFL_InteractiveFormFiller>(this); |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 233 | return m_pFormFiller.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 234 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 235 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 236 | void CPDFSDK_FormFillEnvironment::Invalidate(UnderlyingPageType* page, |
Dan Sinclair | 6eec1c4 | 2017-02-21 17:20:43 -0500 | [diff] [blame] | 237 | const FX_RECT& rect) { |
| 238 | if (m_pInfo && m_pInfo->FFI_Invalidate) { |
| 239 | m_pInfo->FFI_Invalidate(m_pInfo, page, rect.left, rect.top, rect.right, |
| 240 | rect.bottom); |
| 241 | } |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Dan Sinclair | 60fd9fc | 2017-02-21 17:21:08 -0500 | [diff] [blame] | 244 | void CPDFSDK_FormFillEnvironment::OutputSelectedRect( |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 245 | UnderlyingPageType* page, |
Dan Sinclair | 60fd9fc | 2017-02-21 17:21:08 -0500 | [diff] [blame] | 246 | const CFX_FloatRect& rect) { |
| 247 | if (m_pInfo && m_pInfo->FFI_OutputSelectedRect) { |
| 248 | m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, rect.left, rect.top, |
| 249 | rect.right, rect.bottom); |
| 250 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 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 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 304 | FPDF_PAGE CPDFSDK_FormFillEnvironment::GetPage(UnderlyingDocumentType* document, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 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 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 311 | FPDF_PAGE CPDFSDK_FormFillEnvironment::GetCurrentPage( |
| 312 | UnderlyingDocumentType* document) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 313 | if (m_pInfo && m_pInfo->FFI_GetCurrentPage) |
| 314 | return m_pInfo->FFI_GetCurrentPage(m_pInfo, document); |
| 315 | return nullptr; |
| 316 | } |
| 317 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 318 | void CPDFSDK_FormFillEnvironment::ExecuteNamedAction(const 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 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 331 | void CPDFSDK_FormFillEnvironment::DoURIAction(const 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 |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 347 | void CPDFSDK_FormFillEnvironment::DisplayCaret(CPDFXFA_Page* page, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 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 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 359 | int CPDFSDK_FormFillEnvironment::GetCurrentPageIndex( |
| 360 | CPDFXFA_Context* document) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 361 | if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex) |
| 362 | return -1; |
| 363 | return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document); |
| 364 | } |
| 365 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 366 | void CPDFSDK_FormFillEnvironment::SetCurrentPage(CPDFXFA_Context* document, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 367 | int iCurPage) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 368 | if (m_pInfo && m_pInfo->FFI_SetCurrentPage) |
| 369 | m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage); |
| 370 | } |
| 371 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 372 | CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 373 | if (!m_pInfo || !m_pInfo->FFI_GetPlatform) |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 374 | return CFX_WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 375 | |
| 376 | int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0); |
| 377 | if (nRequiredLen <= 0) |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 378 | return CFX_WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 379 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 380 | std::vector<uint8_t> pBuff(nRequiredLen); |
| 381 | int nActualLen = |
| 382 | m_pInfo->FFI_GetPlatform(m_pInfo, pBuff.data(), nRequiredLen); |
| 383 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 384 | return CFX_WideString(); |
| 385 | |
| 386 | return CFX_WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()), |
| 387 | nActualLen / sizeof(uint16_t)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 390 | void CPDFSDK_FormFillEnvironment::GotoURL(CPDFXFA_Context* document, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 391 | const CFX_WideStringC& wsURL) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 392 | if (!m_pInfo || !m_pInfo->FFI_GotoURL) |
| 393 | return; |
| 394 | |
| 395 | CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 396 | FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength()); |
| 397 | m_pInfo->FFI_GotoURL(m_pInfo, document, pTo); |
| 398 | bsTo.ReleaseBuffer(); |
| 399 | } |
| 400 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 401 | void CPDFSDK_FormFillEnvironment::GetPageViewRect(CPDFXFA_Page* page, |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 402 | FS_RECTF& dstRect) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 403 | if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect) |
| 404 | return; |
| 405 | |
| 406 | double left; |
| 407 | double top; |
| 408 | double right; |
| 409 | double bottom; |
| 410 | m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 411 | if (top < bottom) |
| 412 | std::swap(top, bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 413 | |
| 414 | dstRect.left = static_cast<float>(left); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 415 | dstRect.top = static_cast<float>(top); |
| 416 | dstRect.bottom = static_cast<float>(bottom); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 417 | dstRect.right = static_cast<float>(right); |
| 418 | } |
| 419 | |
Lei Zhang | 671630e | 2017-05-19 19:25:16 -0700 | [diff] [blame] | 420 | bool CPDFSDK_FormFillEnvironment::PopupMenu(CPDFXFA_Page* page, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 421 | FPDF_WIDGET hWidget, |
| 422 | int menuFlag, |
| 423 | CFX_PointF pt) { |
tsepez | dc0401a | 2016-10-28 13:10:35 -0700 | [diff] [blame] | 424 | return m_pInfo && m_pInfo->FFI_PopupMenu && |
| 425 | m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 426 | } |
| 427 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 428 | void CPDFSDK_FormFillEnvironment::Alert(FPDF_WIDESTRING Msg, |
| 429 | FPDF_WIDESTRING Title, |
| 430 | int Type, |
| 431 | int Icon) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 432 | if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { |
| 433 | m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type, |
| 434 | Icon); |
| 435 | } |
| 436 | } |
| 437 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 438 | void CPDFSDK_FormFillEnvironment::EmailTo(FPDF_FILEHANDLER* fileHandler, |
| 439 | FPDF_WIDESTRING pTo, |
| 440 | FPDF_WIDESTRING pSubject, |
| 441 | FPDF_WIDESTRING pCC, |
| 442 | FPDF_WIDESTRING pBcc, |
| 443 | FPDF_WIDESTRING pMsg) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 444 | if (m_pInfo && m_pInfo->FFI_EmailTo) |
| 445 | m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg); |
| 446 | } |
| 447 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 448 | void CPDFSDK_FormFillEnvironment::UploadTo(FPDF_FILEHANDLER* fileHandler, |
| 449 | int fileFlag, |
| 450 | FPDF_WIDESTRING uploadTo) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 451 | if (m_pInfo && m_pInfo->FFI_UploadTo) |
| 452 | m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo); |
| 453 | } |
| 454 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 455 | FPDF_FILEHANDLER* CPDFSDK_FormFillEnvironment::OpenFile(int fileType, |
| 456 | FPDF_WIDESTRING wsURL, |
| 457 | const char* mode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 458 | if (m_pInfo && m_pInfo->FFI_OpenFile) |
| 459 | return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode); |
| 460 | return nullptr; |
| 461 | } |
| 462 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 463 | CFX_RetainPtr<IFX_SeekableReadStream> |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 464 | CPDFSDK_FormFillEnvironment::DownloadFromURL(const wchar_t* url) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 465 | if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL) |
| 466 | return nullptr; |
| 467 | |
| 468 | CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode(); |
| 469 | FPDF_WIDESTRING wsURL = |
| 470 | (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength()); |
| 471 | |
| 472 | FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL); |
tsepez | fa89a20 | 2016-12-02 09:48:30 -0800 | [diff] [blame] | 473 | return MakeSeekableStream(fileHandler); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 474 | } |
| 475 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 476 | CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL( |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 477 | const wchar_t* wsURL, |
| 478 | const wchar_t* wsData, |
| 479 | const wchar_t* wsContentType, |
| 480 | const wchar_t* wsEncode, |
| 481 | const wchar_t* wsHeader) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 482 | if (!m_pInfo || !m_pInfo->FFI_PostRequestURL) |
| 483 | return L""; |
| 484 | |
| 485 | CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 486 | FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); |
| 487 | |
| 488 | CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode(); |
| 489 | FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); |
| 490 | |
| 491 | CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode(); |
| 492 | FPDF_WIDESTRING contentType = |
| 493 | (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength()); |
| 494 | |
| 495 | CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode(); |
| 496 | FPDF_WIDESTRING encode = |
| 497 | (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); |
| 498 | |
| 499 | CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode(); |
| 500 | FPDF_WIDESTRING header = |
| 501 | (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength()); |
| 502 | |
| 503 | FPDF_BSTR response; |
| 504 | FPDF_BStr_Init(&response); |
| 505 | m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header, |
| 506 | &response); |
| 507 | |
| 508 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 509 | (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING)); |
| 510 | FPDF_BStr_Clear(&response); |
| 511 | |
| 512 | return wsRet; |
| 513 | } |
| 514 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 515 | FPDF_BOOL CPDFSDK_FormFillEnvironment::PutRequestURL(const wchar_t* wsURL, |
| 516 | const wchar_t* wsData, |
| 517 | const wchar_t* wsEncode) { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 518 | if (!m_pInfo || !m_pInfo->FFI_PutRequestURL) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 519 | return false; |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 520 | |
| 521 | CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 522 | FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); |
| 523 | |
| 524 | CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode(); |
| 525 | FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); |
| 526 | |
| 527 | CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode(); |
| 528 | FPDF_WIDESTRING encode = |
| 529 | (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); |
| 530 | |
| 531 | return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode); |
| 532 | } |
| 533 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 534 | CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 535 | if (!m_pInfo || !m_pInfo->FFI_GetLanguage) |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 536 | return CFX_WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 537 | |
| 538 | int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0); |
| 539 | if (nRequiredLen <= 0) |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 540 | return CFX_WideString(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 541 | |
Tom Sepez | 4ad4690 | 2017-05-02 13:19:56 -0700 | [diff] [blame] | 542 | std::vector<uint8_t> pBuff(nRequiredLen); |
| 543 | int nActualLen = |
| 544 | m_pInfo->FFI_GetLanguage(m_pInfo, pBuff.data(), nRequiredLen); |
| 545 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 546 | return CFX_WideString(); |
| 547 | |
| 548 | return CFX_WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()), |
| 549 | nActualLen / sizeof(uint16_t)); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 550 | } |
| 551 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 552 | void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount, |
| 553 | uint32_t dwEventType) const { |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 554 | if (m_pInfo && m_pInfo->FFI_PageEvent) |
| 555 | m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType); |
| 556 | } |
| 557 | #endif // PDF_ENABLE_XFA |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 558 | |
| 559 | void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 560 | for (auto& it : m_PageMap) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 561 | if (it.second->IsValidSDKAnnot(GetFocusAnnot())) |
| 562 | KillFocusAnnot(0); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView( |
| 567 | UnderlyingPageType* pUnderlyingPage, |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 568 | bool renew) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 569 | auto it = m_PageMap.find(pUnderlyingPage); |
| 570 | if (it != m_PageMap.end()) |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 571 | return it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 572 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 573 | if (!renew) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 574 | return nullptr; |
| 575 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 576 | auto pNew = pdfium::MakeUnique<CPDFSDK_PageView>(this, pUnderlyingPage); |
| 577 | CPDFSDK_PageView* pPageView = pNew.get(); |
| 578 | m_PageMap[pUnderlyingPage] = std::move(pNew); |
| 579 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 580 | // Delay to load all the annotations, to avoid endless loop. |
| 581 | pPageView->LoadFXAnnots(); |
| 582 | return pPageView; |
| 583 | } |
| 584 | |
| 585 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() { |
| 586 | UnderlyingPageType* pPage = |
Tom Sepez | 940967d | 2017-05-18 12:32:20 -0700 | [diff] [blame] | 587 | UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc.Get())); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 588 | return pPage ? GetPageView(pPage, true) : nullptr; |
| 589 | } |
| 590 | |
| 591 | CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) { |
| 592 | UnderlyingPageType* pTempPage = |
Tom Sepez | 940967d | 2017-05-18 12:32:20 -0700 | [diff] [blame] | 593 | UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc.Get(), nIndex)); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 594 | if (!pTempPage) |
| 595 | return nullptr; |
| 596 | |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 597 | auto it = m_PageMap.find(pTempPage); |
| 598 | return it != m_PageMap.end() ? it->second.get() : nullptr; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() { |
| 602 | CPDF_Document* pPDFDoc = GetPDFDocument(); |
| 603 | CPDF_DocJSActions docJS(pPDFDoc); |
| 604 | int iCount = docJS.CountJSActions(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 605 | for (int i = 0; i < iCount; i++) { |
| 606 | CFX_ByteString csJSName; |
Tom Sepez | c4a2b75 | 2017-04-07 13:56:13 -0700 | [diff] [blame] | 607 | CPDF_Action jsAction = docJS.GetJSActionAndName(i, &csJSName); |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 608 | GetActionHandler()->DoAction_JavaScript( |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 609 | jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 613 | bool CPDFSDK_FormFillEnvironment::ProcOpenAction() { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 614 | if (!m_pUnderlyingDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 615 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 616 | |
| 617 | CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot(); |
| 618 | if (!pRoot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 619 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 620 | |
| 621 | CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction"); |
| 622 | if (!pOpenAction) |
| 623 | pOpenAction = pRoot->GetArrayFor("OpenAction"); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 624 | if (!pOpenAction) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 625 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 626 | |
| 627 | if (pOpenAction->IsArray()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 628 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 629 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 630 | CPDF_Dictionary* pDict = pOpenAction->AsDictionary(); |
| 631 | if (!pDict) |
| 632 | return false; |
| 633 | |
| 634 | CPDF_Action action(pDict); |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 635 | GetActionHandler()->DoAction_DocOpen(action, this); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 636 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void CPDFSDK_FormFillEnvironment::RemovePageView( |
| 640 | UnderlyingPageType* pUnderlyingPage) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 641 | auto it = m_PageMap.find(pUnderlyingPage); |
| 642 | if (it == m_PageMap.end()) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 643 | return; |
| 644 | |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 645 | CPDFSDK_PageView* pPageView = it->second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 646 | if (pPageView->IsLocked() || pPageView->IsBeingDestroyed()) |
| 647 | return; |
| 648 | |
| 649 | // Mark the page view so we do not come into |RemovePageView| a second |
| 650 | // time while we're in the process of removing. |
| 651 | pPageView->SetBeingDestroyed(); |
| 652 | |
| 653 | // This must happen before we remove |pPageView| from the map because |
| 654 | // |KillFocusAnnot| can call into the |GetPage| method which will |
| 655 | // look for this page view in the map, if it doesn't find it a new one will |
| 656 | // be created. We then have two page views pointing to the same page and |
| 657 | // bad things happen. |
| 658 | if (pPageView->IsValidSDKAnnot(GetFocusAnnot())) |
| 659 | KillFocusAnnot(0); |
| 660 | |
| 661 | // Remove the page from the map to make sure we don't accidentally attempt |
| 662 | // to use the |pPageView| while we're cleaning it up. |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 663 | m_PageMap.erase(it); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) { |
Tom Sepez | 940967d | 2017-05-18 12:32:20 -0700 | [diff] [blame] | 667 | return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc.Get(), nIndex)); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() { |
| 671 | if (!m_pInterForm) |
| 672 | m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this); |
| 673 | return m_pInterForm.get(); |
| 674 | } |
| 675 | |
| 676 | void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender, |
| 677 | CPDFSDK_Annot* pAnnot) { |
tsepez | 2599ff7 | 2016-11-08 14:38:59 -0800 | [diff] [blame] | 678 | for (const auto& it : m_PageMap) { |
dsinclair | 6c659ab | 2016-10-12 13:41:38 -0700 | [diff] [blame] | 679 | CPDFSDK_PageView* pPageView = it.second.get(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 680 | if (pPageView != pSender) |
| 681 | pPageView->UpdateView(pAnnot); |
| 682 | } |
| 683 | } |
| 684 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 685 | bool CPDFSDK_FormFillEnvironment::SetFocusAnnot( |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 686 | CPDFSDK_Annot::ObservedPtr* pAnnot) { |
| 687 | if (m_bBeingDestroyed) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 688 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 689 | if (m_pFocusAnnot == *pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 690 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 691 | if (m_pFocusAnnot && !KillFocusAnnot(0)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 692 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 693 | if (!*pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 694 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 695 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 696 | CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView(); |
| 697 | if (!pPageView || !pPageView->IsValid()) |
| 698 | return false; |
| 699 | |
| 700 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr(); |
| 701 | if (m_pFocusAnnot) |
| 702 | return false; |
| 703 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 704 | #ifdef PDF_ENABLE_XFA |
| 705 | CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get()); |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 706 | if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot)) |
| 707 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 708 | #endif // PDF_ENABLE_XFA |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 709 | if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0)) |
| 710 | return false; |
| 711 | if (m_pFocusAnnot) |
| 712 | return false; |
| 713 | |
| 714 | m_pFocusAnnot.Reset(pAnnot->Get()); |
| 715 | return true; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 716 | } |
| 717 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 718 | bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) { |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 719 | if (!m_pFocusAnnot) |
| 720 | return false; |
| 721 | |
| 722 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr(); |
| 723 | CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get()); |
| 724 | m_pFocusAnnot.Reset(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 725 | |
| 726 | #ifdef PDF_ENABLE_XFA |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 727 | CPDFSDK_Annot::ObservedPtr pNull; |
| 728 | if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot)) |
| 729 | return false; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 730 | #endif // PDF_ENABLE_XFA |
| 731 | |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 732 | if (!pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) { |
| 733 | m_pFocusAnnot.Reset(pFocusAnnot.Get()); |
| 734 | return false; |
| 735 | } |
| 736 | |
| 737 | if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) { |
| 738 | CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get()); |
| 739 | int nFieldType = pWidget->GetFieldType(); |
| 740 | if (FIELDTYPE_TEXTFIELD == nFieldType || FIELDTYPE_COMBOBOX == nFieldType) { |
| 741 | OnSetFieldInputFocus(nullptr, 0, false); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
Lei Zhang | 222e1a4 | 2017-06-20 14:47:00 -0700 | [diff] [blame] | 744 | return !m_pFocusAnnot; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Lei Zhang | ad0982a | 2017-06-16 19:30:38 -0700 | [diff] [blame] | 747 | bool CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) const { |
tsepez | 95e5834 | 2016-10-28 11:34:42 -0700 | [diff] [blame] | 748 | return !!(GetPDFDocument()->GetUserPermissions() & nFlag); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 749 | } |