Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/fpdfxfa/cpdfxfa_app.h" |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 8 | |
thestig | d4c34f2 | 2016-09-28 17:04:51 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 12 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/fsdk_define.h" |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 14 | #include "fxjs/fxjs_v8.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 15 | #include "third_party/base/ptr_util.h" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 16 | #include "xfa/fxfa/xfa_ffapp.h" |
| 17 | #include "xfa/fxfa/xfa_fontmgr.h" |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 18 | |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 19 | CPDFXFA_App::CPDFXFA_App() : m_pIsolate(nullptr) { |
| 20 | m_bOwnsIsolate = FXJS_GetIsolate(&m_pIsolate); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 21 | |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 22 | m_pXFAApp = pdfium::MakeUnique<CXFA_FFApp>(this); |
| 23 | m_pXFAApp->SetDefaultFontMgr(pdfium::MakeUnique<CXFA_DefFontMgr>()); |
dsinclair | b685e64 | 2016-10-13 10:57:01 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | CPDFXFA_App::~CPDFXFA_App() { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 27 | if (m_bOwnsIsolate) |
| 28 | m_pIsolate->Dispose(); |
dsinclair | b685e64 | 2016-10-13 10:57:01 -0700 | [diff] [blame] | 29 | } |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 30 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 31 | void CPDFXFA_App::GetAppName(CFX_WideString& wsName) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 32 | if (m_pFormFillEnv) |
| 33 | wsName = m_pFormFillEnv->FFI_GetAppName(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 36 | void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 37 | if (m_pFormFillEnv) |
| 38 | wsLanguage = m_pFormFillEnv->GetLanguage(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 42 | if (m_pFormFillEnv) { |
| 43 | wsPlatform = m_pFormFillEnv->GetPlatform(); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 47 | void CPDFXFA_App::Beep(uint32_t dwType) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 48 | if (m_pFormFillEnv) |
| 49 | m_pFormFillEnv->JS_appBeep(dwType); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 50 | } |
| 51 | |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 52 | int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage, |
| 53 | const CFX_WideString& wsTitle, |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 54 | uint32_t dwIconType, |
| 55 | uint32_t dwButtonType) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 56 | if (!m_pFormFillEnv) |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 57 | return -1; |
| 58 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 59 | uint32_t iconType = 0; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 60 | int iButtonType = 0; |
| 61 | switch (dwIconType) { |
| 62 | case XFA_MBICON_Error: |
| 63 | iconType |= 0; |
| 64 | break; |
| 65 | case XFA_MBICON_Warning: |
| 66 | iconType |= 1; |
| 67 | break; |
| 68 | case XFA_MBICON_Question: |
| 69 | iconType |= 2; |
| 70 | break; |
| 71 | case XFA_MBICON_Status: |
| 72 | iconType |= 3; |
| 73 | break; |
| 74 | } |
| 75 | switch (dwButtonType) { |
| 76 | case XFA_MB_OK: |
| 77 | iButtonType |= 0; |
| 78 | break; |
| 79 | case XFA_MB_OKCancel: |
| 80 | iButtonType |= 1; |
| 81 | break; |
| 82 | case XFA_MB_YesNo: |
| 83 | iButtonType |= 2; |
| 84 | break; |
| 85 | case XFA_MB_YesNoCancel: |
| 86 | iButtonType |= 3; |
| 87 | break; |
| 88 | } |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 89 | int32_t iRet = m_pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(), |
| 90 | iButtonType, iconType); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 91 | switch (iRet) { |
| 92 | case 1: |
| 93 | return XFA_IDOK; |
| 94 | case 2: |
| 95 | return XFA_IDCancel; |
| 96 | case 3: |
| 97 | return XFA_IDNo; |
| 98 | case 4: |
| 99 | return XFA_IDYes; |
| 100 | } |
| 101 | return XFA_IDYes; |
| 102 | } |
| 103 | |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 104 | CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion, |
| 105 | const CFX_WideString& wsTitle, |
| 106 | const CFX_WideString& wsDefaultAnswer, |
| 107 | FX_BOOL bMark) { |
| 108 | CFX_WideString wsAnswer; |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 109 | if (!m_pFormFillEnv) |
| 110 | return wsAnswer; |
| 111 | |
| 112 | int nLength = 2048; |
| 113 | char* pBuff = new char[nLength]; |
| 114 | nLength = m_pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(), |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 115 | wsDefaultAnswer.c_str(), nullptr, |
| 116 | bMark, pBuff, nLength); |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 117 | if (nLength > 0) { |
| 118 | nLength = nLength > 2046 ? 2046 : nLength; |
| 119 | pBuff[nLength] = 0; |
| 120 | pBuff[nLength + 1] = 0; |
| 121 | wsAnswer = CFX_WideString::FromUTF16LE( |
| 122 | reinterpret_cast<const unsigned short*>(pBuff), |
| 123 | nLength / sizeof(unsigned short)); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 124 | } |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 125 | delete[] pBuff; |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 126 | return wsAnswer; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 127 | } |
| 128 | |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 129 | IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 130 | return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL.c_str()) |
| 131 | : nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 132 | } |
| 133 | |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 134 | FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL, |
| 135 | const CFX_WideString& wsData, |
| 136 | const CFX_WideString& wsContentType, |
| 137 | const CFX_WideString& wsEncode, |
| 138 | const CFX_WideString& wsHeader, |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 139 | CFX_WideString& wsResponse) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 140 | if (!m_pFormFillEnv) |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 141 | return FALSE; |
| 142 | |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 143 | wsResponse = m_pFormFillEnv->PostRequestURL( |
| 144 | wsURL.c_str(), wsData.c_str(), wsContentType.c_str(), wsEncode.c_str(), |
| 145 | wsHeader.c_str()); |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 146 | return TRUE; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 147 | } |
| 148 | |
tsepez | 3f80c86 | 2016-05-16 12:03:24 -0700 | [diff] [blame] | 149 | FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL, |
| 150 | const CFX_WideString& wsData, |
| 151 | const CFX_WideString& wsEncode) { |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 152 | return m_pFormFillEnv && |
| 153 | m_pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(), |
| 154 | wsEncode.c_str()); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) { |
| 158 | switch (iStringID) { |
| 159 | case XFA_IDS_ValidateFailed: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 160 | wsString = L"%s validation failed"; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 161 | return; |
| 162 | case XFA_IDS_CalcOverride: |
| 163 | wsString = L"Calculate Override"; |
| 164 | return; |
| 165 | case XFA_IDS_ModifyField: |
| 166 | wsString = L"Are you sure you want to modify this field?"; |
| 167 | return; |
| 168 | case XFA_IDS_NotModifyField: |
| 169 | wsString = L"You are not allowed to modify this field."; |
| 170 | return; |
| 171 | case XFA_IDS_AppName: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 172 | wsString = L"pdfium"; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 173 | return; |
| 174 | case XFA_IDS_Unable_TO_SET: |
| 175 | wsString = L"Unable to set "; |
| 176 | return; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 177 | case XFA_IDS_INVAlID_PROP_SET: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 178 | wsString = L"Invalid property set operation."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 179 | return; |
| 180 | case XFA_IDS_NOT_DEFAUL_VALUE: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 181 | wsString = L" doesn't have a default property."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 182 | return; |
| 183 | case XFA_IDS_UNABLE_SET_LANGUAGE: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 184 | wsString = L"Unable to set language value."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 185 | return; |
| 186 | case XFA_IDS_UNABLE_SET_NUMPAGES: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 187 | wsString = L"Unable to set numPages value."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 188 | return; |
| 189 | case XFA_IDS_UNABLE_SET_PLATFORM: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 190 | wsString = L"Unable to set platform value."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 191 | return; |
| 192 | case XFA_IDS_UNABLE_SET_VARIATION: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 193 | wsString = L"Unable to set variation value."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 194 | return; |
| 195 | case XFA_IDS_UNABLE_SET_VERSION: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 196 | wsString = L"Unable to set version value."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 197 | return; |
| 198 | case XFA_IDS_UNABLE_SET_READY: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 199 | wsString = L"Unable to set ready value."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 200 | return; |
| 201 | case XFA_IDS_COMPILER_ERROR: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 202 | wsString = L"Compiler error."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 203 | return; |
| 204 | case XFA_IDS_DIVIDE_ZERO: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 205 | wsString = L"Divide by zero."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 206 | return; |
| 207 | case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT: |
| 208 | wsString = |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 209 | L"An attempt was made to reference property '%s' of a non-object in " |
| 210 | L"SOM expression %s."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 211 | return; |
| 212 | case XFA_IDS_INDEX_OUT_OF_BOUNDS: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 213 | wsString = L"Index value is out of bounds."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 214 | return; |
| 215 | case XFA_IDS_INCORRECT_NUMBER_OF_METHOD: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 216 | wsString = L"Incorrect number of parameters calling method '%s'."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 217 | return; |
| 218 | case XFA_IDS_ARGUMENT_MISMATCH: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 219 | wsString = L"Argument mismatch in property or function argument."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 220 | return; |
| 221 | case XFA_IDS_NOT_HAVE_PROPERTY: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 222 | wsString = L"'%s' doesn't have property '%s'."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 223 | return; |
| 224 | case XFA_IDS_VIOLATE_BOUNDARY: |
| 225 | wsString = |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 226 | L"The element [%s] has violated its allowable number of occurrences."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 227 | return; |
| 228 | case XFA_IDS_SERVER_DENY: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 229 | wsString = L"Server does not permit."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 230 | return; |
| 231 | case XFA_IDS_ValidateLimit: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 232 | wsString = |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 233 | L"Message limit exceeded. Remaining %d validation errors not " |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 234 | L"reported."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 235 | return; |
| 236 | case XFA_IDS_ValidateNullWarning: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 237 | wsString = |
| 238 | L"%s cannot be blank. To ignore validations for %s, click Ignore."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 239 | return; |
| 240 | case XFA_IDS_ValidateNullError: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 241 | wsString = L"%s cannot be blank."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 242 | return; |
| 243 | case XFA_IDS_ValidateWarning: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 244 | wsString = |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 245 | L"The value you entered for %s is invalid. To ignore validations for " |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 246 | L"%s, click Ignore."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 247 | return; |
| 248 | case XFA_IDS_ValidateError: |
dsinclair | 0e0a86a | 2016-06-01 18:56:51 -0700 | [diff] [blame] | 249 | wsString = L"The value you entered for %s is invalid."; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 254 | IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 255 | CXFA_FWLAdapterTimerMgr* pAdapter = nullptr; |
dsinclair | f3736bc | 2016-10-13 12:07:42 -0700 | [diff] [blame^] | 256 | if (m_pFormFillEnv) |
| 257 | pAdapter = new CXFA_FWLAdapterTimerMgr(m_pFormFillEnv); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 258 | return pAdapter; |
| 259 | } |