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 | |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame^] | 9 | #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 29 | CPDFSDK_Environment::CPDFSDK_Environment(UnderlyingDocumentType* pDoc, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 30 | FPDF_FORMFILLINFO* pFFinfo) |
| 31 | : m_pInfo(pFFinfo), m_pSDKDoc(nullptr), m_pUnderlyingDoc(pDoc) { |
| 32 | m_pSysHandler.reset(new CFX_SystemHandler(this)); |
| 33 | } |
| 34 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 35 | CPDFSDK_Environment::~CPDFSDK_Environment() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 45 | int CPDFSDK_Environment::JS_appAlert(const FX_WCHAR* Msg, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 46 | const FX_WCHAR* Title, |
dsinclair | 72177da | 2016-09-15 12:07:23 -0700 | [diff] [blame] | 47 | uint32_t Type, |
| 48 | uint32_t Icon) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 60 | int CPDFSDK_Environment::JS_appResponse(const FX_WCHAR* Question, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 81 | void CPDFSDK_Environment::JS_appBeep(int nType) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 89 | CFX_WideString CPDFSDK_Environment::JS_fieldBrowse() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 109 | CFX_WideString CPDFSDK_Environment::JS_docGetFilePath() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 129 | void CPDFSDK_Environment::JS_docSubmitForm(void* formData, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 142 | void CPDFSDK_Environment::JS_docmailForm(void* mailData, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 165 | void CPDFSDK_Environment::JS_docprint(FPDF_BOOL bUI, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 182 | void CPDFSDK_Environment::JS_docgotoPage(int nPageNum) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 190 | IJS_Runtime* CPDFSDK_Environment::GetJSRuntime() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 198 | CPDFSDK_AnnotHandlerMgr* CPDFSDK_Environment::GetAnnotHandlerMgr() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 199 | if (!m_pAnnotHandlerMgr) |
| 200 | m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this)); |
| 201 | return m_pAnnotHandlerMgr.get(); |
| 202 | } |
| 203 | |
dsinclair | 79db609 | 2016-09-14 07:27:21 -0700 | [diff] [blame] | 204 | CPDFSDK_ActionHandler* CPDFSDK_Environment::GetActionHander() { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 205 | if (!m_pActionHandler) |
| 206 | m_pActionHandler.reset(new CPDFSDK_ActionHandler()); |
| 207 | return m_pActionHandler.get(); |
| 208 | } |
| 209 | |
dsinclair | b94d7c9 | 2016-09-21 12:07:00 -0700 | [diff] [blame^] | 210 | CFFL_InteractiveFormFiller* CPDFSDK_Environment::GetInteractiveFormFiller() { |
| 211 | if (!m_pFormFiller) |
| 212 | m_pFormFiller.reset(new CFFL_InteractiveFormFiller(this)); |
| 213 | return m_pFormFiller.get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 214 | } |