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