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 | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/include/cpdfsdk_environment.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 | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 12 | #include "fpdfsdk/include/cpdfsdk_annothandlermgr.h" |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/include/cpdfsdk_document.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 14 | #include "fpdfsdk/include/fsdk_actionhandler.h" |
| 15 | #include "fpdfsdk/javascript/ijs_runtime.h" |
| 16 | |
| 17 | #ifdef PDF_ENABLE_XFA |
| 18 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h" |
| 19 | #endif // PDF_ENABLE_XFA |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken |
| 24 | // since modifying the result would impact |bsUTF16LE|. |
| 25 | FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { |
| 26 | return reinterpret_cast<FPDF_WIDESTRING>( |
| 27 | bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); |
| 28 | } |
| 29 | |
| 30 | } // namespace |
| 31 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 32 | CPDFSDK_Environment::CPDFSDK_Environment(UnderlyingDocumentType* pDoc, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 33 | FPDF_FORMFILLINFO* pFFinfo) |
dsinclair | a939bfe | 2016-09-22 13:18:45 -0700 | [diff] [blame] | 34 | : m_pInfo(pFFinfo), |
| 35 | m_pSDKDoc(new CPDFSDK_Document(pDoc, this)), |
| 36 | m_pUnderlyingDoc(pDoc), |
| 37 | m_pSysHandler(new CFX_SystemHandler(this)) {} |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 38 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 39 | CPDFSDK_Environment::~CPDFSDK_Environment() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 40 | #ifdef PDF_ENABLE_XFA |
| 41 | CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); |
| 42 | if (pProvider->m_pEnvList.GetSize() == 0) |
| 43 | pProvider->SetJavaScriptInitialized(FALSE); |
| 44 | #endif // PDF_ENABLE_XFA |
| 45 | if (m_pInfo && m_pInfo->Release) |
| 46 | m_pInfo->Release(m_pInfo); |
| 47 | } |
| 48 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 49 | int CPDFSDK_Environment::JS_appAlert(const FX_WCHAR* Msg, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 50 | const FX_WCHAR* Title, |
dsinclair | 72177da | 2016-09-15 12:07:23 -0700 | [diff] [blame] | 51 | uint32_t Type, |
| 52 | uint32_t Icon) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 53 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 54 | !m_pInfo->m_pJsPlatform->app_alert) { |
| 55 | return -1; |
| 56 | } |
| 57 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 58 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 59 | return m_pInfo->m_pJsPlatform->app_alert( |
| 60 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), |
| 61 | AsFPDFWideString(&bsTitle), Type, Icon); |
| 62 | } |
| 63 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 64 | int CPDFSDK_Environment::JS_appResponse(const FX_WCHAR* Question, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 65 | const FX_WCHAR* Title, |
| 66 | const FX_WCHAR* Default, |
| 67 | const FX_WCHAR* cLabel, |
| 68 | FPDF_BOOL bPassword, |
| 69 | void* response, |
| 70 | int length) { |
| 71 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 72 | !m_pInfo->m_pJsPlatform->app_response) { |
| 73 | return -1; |
| 74 | } |
| 75 | CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); |
| 76 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 77 | CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); |
| 78 | CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); |
| 79 | return m_pInfo->m_pJsPlatform->app_response( |
| 80 | m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), |
| 81 | AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), |
| 82 | AsFPDFWideString(&bsLabel), bPassword, response, length); |
| 83 | } |
| 84 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 85 | void CPDFSDK_Environment::JS_appBeep(int nType) { |
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_beep) { |
| 88 | return; |
| 89 | } |
| 90 | m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); |
| 91 | } |
| 92 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 93 | CFX_WideString CPDFSDK_Environment::JS_fieldBrowse() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 94 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 95 | !m_pInfo->m_pJsPlatform->Field_browse) { |
| 96 | return CFX_WideString(); |
| 97 | } |
| 98 | const int nRequiredLen = |
| 99 | m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
| 100 | if (nRequiredLen <= 0) |
| 101 | return CFX_WideString(); |
| 102 | |
| 103 | std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
| 104 | memset(pBuff.get(), 0, nRequiredLen); |
| 105 | const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
| 106 | m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
| 107 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 108 | return CFX_WideString(); |
| 109 | |
| 110 | return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); |
| 111 | } |
| 112 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 113 | CFX_WideString CPDFSDK_Environment::JS_docGetFilePath() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 114 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 115 | !m_pInfo->m_pJsPlatform->Doc_getFilePath) { |
| 116 | return CFX_WideString(); |
| 117 | } |
| 118 | const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 119 | m_pInfo->m_pJsPlatform, nullptr, 0); |
| 120 | if (nRequiredLen <= 0) |
| 121 | return CFX_WideString(); |
| 122 | |
| 123 | std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
| 124 | memset(pBuff.get(), 0, nRequiredLen); |
| 125 | const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 126 | m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
| 127 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 128 | return CFX_WideString(); |
| 129 | |
| 130 | return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); |
| 131 | } |
| 132 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 133 | void CPDFSDK_Environment::JS_docSubmitForm(void* formData, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 134 | int length, |
| 135 | const FX_WCHAR* URL) { |
| 136 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 137 | !m_pInfo->m_pJsPlatform->Doc_submitForm) { |
| 138 | return; |
| 139 | } |
| 140 | CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); |
| 141 | m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, |
| 142 | length, |
| 143 | AsFPDFWideString(&bsDestination)); |
| 144 | } |
| 145 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 146 | void CPDFSDK_Environment::JS_docmailForm(void* mailData, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 147 | int length, |
| 148 | FPDF_BOOL bUI, |
| 149 | const FX_WCHAR* To, |
| 150 | const FX_WCHAR* Subject, |
| 151 | const FX_WCHAR* CC, |
| 152 | const FX_WCHAR* BCC, |
| 153 | const FX_WCHAR* Msg) { |
| 154 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 155 | !m_pInfo->m_pJsPlatform->Doc_mail) { |
| 156 | return; |
| 157 | } |
| 158 | CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); |
| 159 | CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); |
| 160 | CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); |
| 161 | CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); |
| 162 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 163 | m_pInfo->m_pJsPlatform->Doc_mail( |
| 164 | m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), |
| 165 | AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), |
| 166 | AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); |
| 167 | } |
| 168 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 169 | void CPDFSDK_Environment::JS_docprint(FPDF_BOOL bUI, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 170 | int nStart, |
| 171 | int nEnd, |
| 172 | FPDF_BOOL bSilent, |
| 173 | FPDF_BOOL bShrinkToFit, |
| 174 | FPDF_BOOL bPrintAsImage, |
| 175 | FPDF_BOOL bReverse, |
| 176 | FPDF_BOOL bAnnotations) { |
| 177 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 178 | !m_pInfo->m_pJsPlatform->Doc_print) { |
| 179 | return; |
| 180 | } |
| 181 | m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, |
| 182 | bSilent, bShrinkToFit, bPrintAsImage, |
| 183 | bReverse, bAnnotations); |
| 184 | } |
| 185 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 186 | void CPDFSDK_Environment::JS_docgotoPage(int nPageNum) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 187 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 188 | !m_pInfo->m_pJsPlatform->Doc_gotoPage) { |
| 189 | return; |
| 190 | } |
| 191 | m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); |
| 192 | } |
| 193 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 194 | IJS_Runtime* CPDFSDK_Environment::GetJSRuntime() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 195 | if (!IsJSInitiated()) |
| 196 | return nullptr; |
| 197 | if (!m_pJSRuntime) |
| 198 | m_pJSRuntime.reset(IJS_Runtime::Create(this)); |
| 199 | return m_pJSRuntime.get(); |
| 200 | } |
| 201 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 202 | CPDFSDK_AnnotHandlerMgr* CPDFSDK_Environment::GetAnnotHandlerMgr() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 203 | if (!m_pAnnotHandlerMgr) |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame^] | 204 | m_pAnnotHandlerMgr = WrapUnique(new CPDFSDK_AnnotHandlerMgr(this)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 205 | return m_pAnnotHandlerMgr.get(); |
| 206 | } |
| 207 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 208 | CPDFSDK_ActionHandler* CPDFSDK_Environment::GetActionHander() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 209 | if (!m_pActionHandler) |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame^] | 210 | m_pActionHandler = WrapUnique(new CPDFSDK_ActionHandler()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 211 | return m_pActionHandler.get(); |
| 212 | } |
| 213 | |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 214 | CFFL_InteractiveFormFiller* CPDFSDK_Environment::GetInteractiveFormFiller() { |
| 215 | if (!m_pFormFiller) |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame^] | 216 | m_pFormFiller = WrapUnique(new CFFL_InteractiveFormFiller(this)); |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame] | 217 | return m_pFormFiller.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 218 | } |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 219 | |
| 220 | void CPDFSDK_Environment::Invalidate(FPDF_PAGE page, |
| 221 | double left, |
| 222 | double top, |
| 223 | double right, |
| 224 | double bottom) { |
| 225 | if (m_pInfo && m_pInfo->FFI_Invalidate) |
| 226 | m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom); |
| 227 | } |
| 228 | |
| 229 | void CPDFSDK_Environment::OutputSelectedRect(FPDF_PAGE page, |
| 230 | double left, |
| 231 | double top, |
| 232 | double right, |
| 233 | double bottom) { |
| 234 | if (m_pInfo && m_pInfo->FFI_OutputSelectedRect) |
| 235 | m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom); |
| 236 | } |
| 237 | |
| 238 | void CPDFSDK_Environment::SetCursor(int nCursorType) { |
| 239 | if (m_pInfo && m_pInfo->FFI_SetCursor) |
| 240 | m_pInfo->FFI_SetCursor(m_pInfo, nCursorType); |
| 241 | } |
| 242 | |
| 243 | int CPDFSDK_Environment::SetTimer(int uElapse, TimerCallback lpTimerFunc) { |
| 244 | if (m_pInfo && m_pInfo->FFI_SetTimer) |
| 245 | return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | void CPDFSDK_Environment::KillTimer(int nTimerID) { |
| 250 | if (m_pInfo && m_pInfo->FFI_KillTimer) |
| 251 | m_pInfo->FFI_KillTimer(m_pInfo, nTimerID); |
| 252 | } |
| 253 | |
| 254 | FX_SYSTEMTIME CPDFSDK_Environment::GetLocalTime() const { |
| 255 | FX_SYSTEMTIME fxtime; |
| 256 | if (!m_pInfo || !m_pInfo->FFI_GetLocalTime) |
| 257 | return fxtime; |
| 258 | |
| 259 | FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo); |
| 260 | fxtime.wDay = systime.wDay; |
| 261 | fxtime.wDayOfWeek = systime.wDayOfWeek; |
| 262 | fxtime.wHour = systime.wHour; |
| 263 | fxtime.wMilliseconds = systime.wMilliseconds; |
| 264 | fxtime.wMinute = systime.wMinute; |
| 265 | fxtime.wMonth = systime.wMonth; |
| 266 | fxtime.wSecond = systime.wSecond; |
| 267 | fxtime.wYear = systime.wYear; |
| 268 | return fxtime; |
| 269 | } |
| 270 | |
| 271 | void CPDFSDK_Environment::OnChange() { |
| 272 | if (m_pInfo && m_pInfo->FFI_OnChange) |
| 273 | m_pInfo->FFI_OnChange(m_pInfo); |
| 274 | } |
| 275 | |
| 276 | FX_BOOL CPDFSDK_Environment::IsSHIFTKeyDown(uint32_t nFlag) const { |
| 277 | return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0; |
| 278 | } |
| 279 | |
| 280 | FX_BOOL CPDFSDK_Environment::IsCTRLKeyDown(uint32_t nFlag) const { |
| 281 | return (nFlag & FWL_EVENTFLAG_ControlKey) != 0; |
| 282 | } |
| 283 | |
| 284 | FX_BOOL CPDFSDK_Environment::IsALTKeyDown(uint32_t nFlag) const { |
| 285 | return (nFlag & FWL_EVENTFLAG_AltKey) != 0; |
| 286 | } |
| 287 | |
| 288 | FPDF_PAGE CPDFSDK_Environment::GetPage(FPDF_DOCUMENT document, int nPageIndex) { |
| 289 | if (m_pInfo && m_pInfo->FFI_GetPage) |
| 290 | return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex); |
| 291 | return nullptr; |
| 292 | } |
| 293 | |
| 294 | FPDF_PAGE CPDFSDK_Environment::GetCurrentPage(FPDF_DOCUMENT document) { |
| 295 | if (m_pInfo && m_pInfo->FFI_GetCurrentPage) |
| 296 | return m_pInfo->FFI_GetCurrentPage(m_pInfo, document); |
| 297 | return nullptr; |
| 298 | } |
| 299 | |
| 300 | void CPDFSDK_Environment::ExecuteNamedAction(const FX_CHAR* namedAction) { |
| 301 | if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction) |
| 302 | m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction); |
| 303 | } |
| 304 | |
| 305 | void CPDFSDK_Environment::OnSetFieldInputFocus(FPDF_WIDESTRING focusText, |
| 306 | FPDF_DWORD nTextLen, |
| 307 | FX_BOOL bFocus) { |
| 308 | if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus) |
| 309 | m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus); |
| 310 | } |
| 311 | |
| 312 | void CPDFSDK_Environment::DoURIAction(const FX_CHAR* bsURI) { |
| 313 | if (m_pInfo && m_pInfo->FFI_DoURIAction) |
| 314 | m_pInfo->FFI_DoURIAction(m_pInfo, bsURI); |
| 315 | } |
| 316 | |
| 317 | void CPDFSDK_Environment::DoGoToAction(int nPageIndex, |
| 318 | int zoomMode, |
| 319 | float* fPosArray, |
| 320 | int sizeOfArray) { |
| 321 | if (m_pInfo && m_pInfo->FFI_DoGoToAction) { |
| 322 | m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray, |
| 323 | sizeOfArray); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | #ifdef PDF_ENABLE_XFA |
| 328 | void CPDFSDK_Environment::DisplayCaret(FPDF_PAGE page, |
| 329 | FPDF_BOOL bVisible, |
| 330 | double left, |
| 331 | double top, |
| 332 | double right, |
| 333 | double bottom) { |
| 334 | if (m_pInfo && m_pInfo->FFI_DisplayCaret) { |
| 335 | m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right, |
| 336 | bottom); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | int CPDFSDK_Environment::GetCurrentPageIndex(FPDF_DOCUMENT document) { |
| 341 | if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex) |
| 342 | return -1; |
| 343 | return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document); |
| 344 | } |
| 345 | |
| 346 | void CPDFSDK_Environment::SetCurrentPage(FPDF_DOCUMENT document, int iCurPage) { |
| 347 | if (m_pInfo && m_pInfo->FFI_SetCurrentPage) |
| 348 | m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage); |
| 349 | } |
| 350 | |
| 351 | CFX_WideString CPDFSDK_Environment::GetPlatform() { |
| 352 | if (!m_pInfo || !m_pInfo->FFI_GetPlatform) |
| 353 | return L""; |
| 354 | |
| 355 | int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0); |
| 356 | if (nRequiredLen <= 0) |
| 357 | return L""; |
| 358 | |
| 359 | char* pbuff = new char[nRequiredLen]; |
| 360 | memset(pbuff, 0, nRequiredLen); |
| 361 | int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen); |
| 362 | if (nActualLen <= 0 || nActualLen > nRequiredLen) { |
| 363 | delete[] pbuff; |
| 364 | return L""; |
| 365 | } |
| 366 | CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen); |
| 367 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 368 | (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), |
| 369 | bsRet.GetLength() / sizeof(unsigned short)); |
| 370 | delete[] pbuff; |
| 371 | return wsRet; |
| 372 | } |
| 373 | |
| 374 | void CPDFSDK_Environment::GotoURL(FPDF_DOCUMENT document, |
| 375 | const CFX_WideStringC& wsURL) { |
| 376 | if (!m_pInfo || !m_pInfo->FFI_GotoURL) |
| 377 | return; |
| 378 | |
| 379 | CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 380 | FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength()); |
| 381 | m_pInfo->FFI_GotoURL(m_pInfo, document, pTo); |
| 382 | bsTo.ReleaseBuffer(); |
| 383 | } |
| 384 | |
| 385 | void CPDFSDK_Environment::GetPageViewRect(FPDF_PAGE page, FS_RECTF& dstRect) { |
| 386 | if (!m_pInfo || !m_pInfo->FFI_GetPageViewRect) |
| 387 | return; |
| 388 | |
| 389 | double left; |
| 390 | double top; |
| 391 | double right; |
| 392 | double bottom; |
| 393 | m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom); |
| 394 | |
| 395 | dstRect.left = static_cast<float>(left); |
| 396 | dstRect.top = static_cast<float>(top < bottom ? bottom : top); |
| 397 | dstRect.bottom = static_cast<float>(top < bottom ? top : bottom); |
| 398 | dstRect.right = static_cast<float>(right); |
| 399 | } |
| 400 | |
| 401 | FX_BOOL CPDFSDK_Environment::PopupMenu(FPDF_PAGE page, |
| 402 | FPDF_WIDGET hWidget, |
| 403 | int menuFlag, |
| 404 | CFX_PointF pt) { |
| 405 | if (!m_pInfo || !m_pInfo->FFI_PopupMenu) |
| 406 | return FALSE; |
| 407 | return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, pt.x, pt.y); |
| 408 | } |
| 409 | |
| 410 | void CPDFSDK_Environment::Alert(FPDF_WIDESTRING Msg, |
| 411 | FPDF_WIDESTRING Title, |
| 412 | int Type, |
| 413 | int Icon) { |
| 414 | if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { |
| 415 | m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type, |
| 416 | Icon); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void CPDFSDK_Environment::EmailTo(FPDF_FILEHANDLER* fileHandler, |
| 421 | FPDF_WIDESTRING pTo, |
| 422 | FPDF_WIDESTRING pSubject, |
| 423 | FPDF_WIDESTRING pCC, |
| 424 | FPDF_WIDESTRING pBcc, |
| 425 | FPDF_WIDESTRING pMsg) { |
| 426 | if (m_pInfo && m_pInfo->FFI_EmailTo) |
| 427 | m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg); |
| 428 | } |
| 429 | |
| 430 | void CPDFSDK_Environment::UploadTo(FPDF_FILEHANDLER* fileHandler, |
| 431 | int fileFlag, |
| 432 | FPDF_WIDESTRING uploadTo) { |
| 433 | if (m_pInfo && m_pInfo->FFI_UploadTo) |
| 434 | m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo); |
| 435 | } |
| 436 | |
| 437 | FPDF_FILEHANDLER* CPDFSDK_Environment::OpenFile(int fileType, |
| 438 | FPDF_WIDESTRING wsURL, |
| 439 | const char* mode) { |
| 440 | if (m_pInfo && m_pInfo->FFI_OpenFile) |
| 441 | return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode); |
| 442 | return nullptr; |
| 443 | } |
| 444 | |
| 445 | IFX_FileRead* CPDFSDK_Environment::DownloadFromURL(const FX_WCHAR* url) { |
| 446 | if (!m_pInfo || !m_pInfo->FFI_DownloadFromURL) |
| 447 | return nullptr; |
| 448 | |
| 449 | CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode(); |
| 450 | FPDF_WIDESTRING wsURL = |
| 451 | (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength()); |
| 452 | |
| 453 | FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL); |
| 454 | |
| 455 | return new CFPDF_FileStream(fileHandler); |
| 456 | } |
| 457 | |
| 458 | CFX_WideString CPDFSDK_Environment::PostRequestURL( |
| 459 | const FX_WCHAR* wsURL, |
| 460 | const FX_WCHAR* wsData, |
| 461 | const FX_WCHAR* wsContentType, |
| 462 | const FX_WCHAR* wsEncode, |
| 463 | const FX_WCHAR* wsHeader) { |
| 464 | if (!m_pInfo || !m_pInfo->FFI_PostRequestURL) |
| 465 | return L""; |
| 466 | |
| 467 | CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 468 | FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); |
| 469 | |
| 470 | CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode(); |
| 471 | FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); |
| 472 | |
| 473 | CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode(); |
| 474 | FPDF_WIDESTRING contentType = |
| 475 | (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength()); |
| 476 | |
| 477 | CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode(); |
| 478 | FPDF_WIDESTRING encode = |
| 479 | (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); |
| 480 | |
| 481 | CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode(); |
| 482 | FPDF_WIDESTRING header = |
| 483 | (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength()); |
| 484 | |
| 485 | FPDF_BSTR response; |
| 486 | FPDF_BStr_Init(&response); |
| 487 | m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header, |
| 488 | &response); |
| 489 | |
| 490 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 491 | (FPDF_WIDESTRING)response.str, response.len / sizeof(FPDF_WIDESTRING)); |
| 492 | FPDF_BStr_Clear(&response); |
| 493 | |
| 494 | return wsRet; |
| 495 | } |
| 496 | |
| 497 | FPDF_BOOL CPDFSDK_Environment::PutRequestURL(const FX_WCHAR* wsURL, |
| 498 | const FX_WCHAR* wsData, |
| 499 | const FX_WCHAR* wsEncode) { |
| 500 | if (!m_pInfo || !m_pInfo->FFI_PutRequestURL) |
| 501 | return FALSE; |
| 502 | |
| 503 | CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode(); |
| 504 | FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength()); |
| 505 | |
| 506 | CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode(); |
| 507 | FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength()); |
| 508 | |
| 509 | CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode(); |
| 510 | FPDF_WIDESTRING encode = |
| 511 | (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength()); |
| 512 | |
| 513 | return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode); |
| 514 | } |
| 515 | |
| 516 | CFX_WideString CPDFSDK_Environment::GetLanguage() { |
| 517 | if (!m_pInfo || !m_pInfo->FFI_GetLanguage) |
| 518 | return L""; |
| 519 | |
| 520 | int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0); |
| 521 | if (nRequiredLen <= 0) |
| 522 | return L""; |
| 523 | |
| 524 | char* pbuff = new char[nRequiredLen]; |
| 525 | memset(pbuff, 0, nRequiredLen); |
| 526 | int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen); |
| 527 | if (nActualLen <= 0 || nActualLen > nRequiredLen) { |
| 528 | delete[] pbuff; |
| 529 | return L""; |
| 530 | } |
| 531 | CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen); |
| 532 | CFX_WideString wsRet = CFX_WideString::FromUTF16LE( |
| 533 | (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()), |
| 534 | bsRet.GetLength() / sizeof(FPDF_WIDESTRING)); |
| 535 | delete[] pbuff; |
| 536 | return wsRet; |
| 537 | } |
| 538 | |
| 539 | void CPDFSDK_Environment::PageEvent(int iPageCount, |
| 540 | uint32_t dwEventType) const { |
| 541 | if (m_pInfo && m_pInfo->FFI_PageEvent) |
| 542 | m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType); |
| 543 | } |
| 544 | #endif // PDF_ENABLE_XFA |