| OLD | NEW |
| (Empty) |
| 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 | |
| 7 #include "fpdfsdk/include/cpdfdoc_environment.h" | |
| 8 | |
| 9 #include "fpdfsdk/formfiller/cffl_iformfiller.h" | |
| 10 #include "fpdfsdk/include/cpdfsdk_annothandlermgr.h" | |
| 11 #include "fpdfsdk/include/fsdk_actionhandler.h" | |
| 12 #include "fpdfsdk/javascript/ijs_runtime.h" | |
| 13 | |
| 14 #ifdef PDF_ENABLE_XFA | |
| 15 #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h" | |
| 16 #endif // PDF_ENABLE_XFA | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // NOTE: |bsUTF16LE| must outlive the use of the result. Care must be taken | |
| 21 // since modifying the result would impact |bsUTF16LE|. | |
| 22 FPDF_WIDESTRING AsFPDFWideString(CFX_ByteString* bsUTF16LE) { | |
| 23 return reinterpret_cast<FPDF_WIDESTRING>( | |
| 24 bsUTF16LE->GetBuffer(bsUTF16LE->GetLength())); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc, | |
| 30 FPDF_FORMFILLINFO* pFFinfo) | |
| 31 : m_pInfo(pFFinfo), m_pSDKDoc(nullptr), m_pUnderlyingDoc(pDoc) { | |
| 32 m_pSysHandler.reset(new CFX_SystemHandler(this)); | |
| 33 } | |
| 34 | |
| 35 CPDFDoc_Environment::~CPDFDoc_Environment() { | |
| 36 #ifdef PDF_ENABLE_XFA | |
| 37 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); | |
| 38 if (pProvider->m_pEnvList.GetSize() == 0) | |
| 39 pProvider->SetJavaScriptInitialized(FALSE); | |
| 40 #endif // PDF_ENABLE_XFA | |
| 41 if (m_pInfo && m_pInfo->Release) | |
| 42 m_pInfo->Release(m_pInfo); | |
| 43 } | |
| 44 | |
| 45 int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg, | |
| 46 const FX_WCHAR* Title, | |
| 47 FX_UINT Type, | |
| 48 FX_UINT Icon) { | |
| 49 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 50 !m_pInfo->m_pJsPlatform->app_alert) { | |
| 51 return -1; | |
| 52 } | |
| 53 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); | |
| 54 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); | |
| 55 return m_pInfo->m_pJsPlatform->app_alert( | |
| 56 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsMsg), | |
| 57 AsFPDFWideString(&bsTitle), Type, Icon); | |
| 58 } | |
| 59 | |
| 60 int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question, | |
| 61 const FX_WCHAR* Title, | |
| 62 const FX_WCHAR* Default, | |
| 63 const FX_WCHAR* cLabel, | |
| 64 FPDF_BOOL bPassword, | |
| 65 void* response, | |
| 66 int length) { | |
| 67 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 68 !m_pInfo->m_pJsPlatform->app_response) { | |
| 69 return -1; | |
| 70 } | |
| 71 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); | |
| 72 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); | |
| 73 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); | |
| 74 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); | |
| 75 return m_pInfo->m_pJsPlatform->app_response( | |
| 76 m_pInfo->m_pJsPlatform, AsFPDFWideString(&bsQuestion), | |
| 77 AsFPDFWideString(&bsTitle), AsFPDFWideString(&bsDefault), | |
| 78 AsFPDFWideString(&bsLabel), bPassword, response, length); | |
| 79 } | |
| 80 | |
| 81 void CPDFDoc_Environment::JS_appBeep(int nType) { | |
| 82 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 83 !m_pInfo->m_pJsPlatform->app_beep) { | |
| 84 return; | |
| 85 } | |
| 86 m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType); | |
| 87 } | |
| 88 | |
| 89 CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() { | |
| 90 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 91 !m_pInfo->m_pJsPlatform->Field_browse) { | |
| 92 return CFX_WideString(); | |
| 93 } | |
| 94 const int nRequiredLen = | |
| 95 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); | |
| 96 if (nRequiredLen <= 0) | |
| 97 return CFX_WideString(); | |
| 98 | |
| 99 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); | |
| 100 memset(pBuff.get(), 0, nRequiredLen); | |
| 101 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( | |
| 102 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); | |
| 103 if (nActualLen <= 0 || nActualLen > nRequiredLen) | |
| 104 return CFX_WideString(); | |
| 105 | |
| 106 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); | |
| 107 } | |
| 108 | |
| 109 CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { | |
| 110 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 111 !m_pInfo->m_pJsPlatform->Doc_getFilePath) { | |
| 112 return CFX_WideString(); | |
| 113 } | |
| 114 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( | |
| 115 m_pInfo->m_pJsPlatform, nullptr, 0); | |
| 116 if (nRequiredLen <= 0) | |
| 117 return CFX_WideString(); | |
| 118 | |
| 119 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]); | |
| 120 memset(pBuff.get(), 0, nRequiredLen); | |
| 121 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( | |
| 122 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); | |
| 123 if (nActualLen <= 0 || nActualLen > nRequiredLen) | |
| 124 return CFX_WideString(); | |
| 125 | |
| 126 return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); | |
| 127 } | |
| 128 | |
| 129 void CPDFDoc_Environment::JS_docSubmitForm(void* formData, | |
| 130 int length, | |
| 131 const FX_WCHAR* URL) { | |
| 132 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 133 !m_pInfo->m_pJsPlatform->Doc_submitForm) { | |
| 134 return; | |
| 135 } | |
| 136 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); | |
| 137 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, | |
| 138 length, | |
| 139 AsFPDFWideString(&bsDestination)); | |
| 140 } | |
| 141 | |
| 142 void CPDFDoc_Environment::JS_docmailForm(void* mailData, | |
| 143 int length, | |
| 144 FPDF_BOOL bUI, | |
| 145 const FX_WCHAR* To, | |
| 146 const FX_WCHAR* Subject, | |
| 147 const FX_WCHAR* CC, | |
| 148 const FX_WCHAR* BCC, | |
| 149 const FX_WCHAR* Msg) { | |
| 150 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 151 !m_pInfo->m_pJsPlatform->Doc_mail) { | |
| 152 return; | |
| 153 } | |
| 154 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); | |
| 155 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); | |
| 156 CFX_ByteString bsCC = CFX_WideString(CC).UTF16LE_Encode(); | |
| 157 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); | |
| 158 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); | |
| 159 m_pInfo->m_pJsPlatform->Doc_mail( | |
| 160 m_pInfo->m_pJsPlatform, mailData, length, bUI, AsFPDFWideString(&bsTo), | |
| 161 AsFPDFWideString(&bsSubject), AsFPDFWideString(&bsCC), | |
| 162 AsFPDFWideString(&bsBcc), AsFPDFWideString(&bsMsg)); | |
| 163 } | |
| 164 | |
| 165 void CPDFDoc_Environment::JS_docprint(FPDF_BOOL bUI, | |
| 166 int nStart, | |
| 167 int nEnd, | |
| 168 FPDF_BOOL bSilent, | |
| 169 FPDF_BOOL bShrinkToFit, | |
| 170 FPDF_BOOL bPrintAsImage, | |
| 171 FPDF_BOOL bReverse, | |
| 172 FPDF_BOOL bAnnotations) { | |
| 173 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 174 !m_pInfo->m_pJsPlatform->Doc_print) { | |
| 175 return; | |
| 176 } | |
| 177 m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, | |
| 178 bSilent, bShrinkToFit, bPrintAsImage, | |
| 179 bReverse, bAnnotations); | |
| 180 } | |
| 181 | |
| 182 void CPDFDoc_Environment::JS_docgotoPage(int nPageNum) { | |
| 183 if (!m_pInfo || !m_pInfo->m_pJsPlatform || | |
| 184 !m_pInfo->m_pJsPlatform->Doc_gotoPage) { | |
| 185 return; | |
| 186 } | |
| 187 m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum); | |
| 188 } | |
| 189 | |
| 190 IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() { | |
| 191 if (!IsJSInitiated()) | |
| 192 return nullptr; | |
| 193 if (!m_pJSRuntime) | |
| 194 m_pJSRuntime.reset(IJS_Runtime::Create(this)); | |
| 195 return m_pJSRuntime.get(); | |
| 196 } | |
| 197 | |
| 198 CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() { | |
| 199 if (!m_pAnnotHandlerMgr) | |
| 200 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this)); | |
| 201 return m_pAnnotHandlerMgr.get(); | |
| 202 } | |
| 203 | |
| 204 CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() { | |
| 205 if (!m_pActionHandler) | |
| 206 m_pActionHandler.reset(new CPDFSDK_ActionHandler()); | |
| 207 return m_pActionHandler.get(); | |
| 208 } | |
| 209 | |
| 210 CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() { | |
| 211 if (!m_pIFormFiller) | |
| 212 m_pIFormFiller.reset(new CFFL_IFormFiller(this)); | |
| 213 return m_pIFormFiller.get(); | |
| 214 } | |
| OLD | NEW |