blob: ffc4e5363ad4f022f39152500b1c685b37dd6926 [file] [log] [blame]
Lei Zhang94293682016-01-27 18:27:56 -08001// 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
dsinclair89bdd082016-04-06 10:47:54 -07007#include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h"
8#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
9#include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050010#include "fpdfsdk/include/fsdk_define.h"
11#include "fpdfsdk/include/fsdk_mgr.h"
Lei Zhang94293682016-01-27 18:27:56 -080012#include "public/fpdf_formfill.h"
Dan Sinclaira98600a2016-03-21 15:15:56 -040013#include "xfa/fxbarcode/include/BC_Library.h"
dsinclair7222ea62016-04-06 14:33:07 -070014#include "xfa/fxfa/include/xfa_ffapp.h"
15#include "xfa/fxfa/include/xfa_fontmgr.h"
Lei Zhang94293682016-01-27 18:27:56 -080016
thestig1cd352e2016-06-07 17:53:06 -070017CPDFXFA_App* CPDFXFA_App::g_pApp = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080018
19CPDFXFA_App* CPDFXFA_App::GetInstance() {
20 if (!g_pApp) {
21 g_pApp = new CPDFXFA_App();
22 }
23 return g_pApp;
24}
25
26void CPDFXFA_App::ReleaseInstance() {
27 delete g_pApp;
thestig1cd352e2016-06-07 17:53:06 -070028 g_pApp = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080029}
30
31CPDFXFA_App::CPDFXFA_App()
32 : m_bJavaScriptInitialized(FALSE),
thestig1cd352e2016-06-07 17:53:06 -070033 m_pXFAApp(nullptr),
dsinclairec3da5b2016-05-25 16:42:05 -070034 m_pIsolate(nullptr),
tsepezd5f72612016-06-01 14:01:31 -070035 m_csAppType(JS_STR_VIEWERTYPE_STANDARD) {
Lei Zhang94293682016-01-27 18:27:56 -080036 m_pEnvList.RemoveAll();
37}
38
39CPDFXFA_App::~CPDFXFA_App() {
Lei Zhang94293682016-01-27 18:27:56 -080040 delete m_pXFAApp;
thestig1cd352e2016-06-07 17:53:06 -070041 m_pXFAApp = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080042
tsepezd5f72612016-06-01 14:01:31 -070043 FXJSE_Runtime_Release(m_pIsolate);
dsinclairec3da5b2016-05-25 16:42:05 -070044 m_pIsolate = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080045
46 FXJSE_Finalize();
47 BC_Library_Destory();
Lei Zhang94293682016-01-27 18:27:56 -080048}
49
dsinclairec3da5b2016-05-25 16:42:05 -070050FX_BOOL CPDFXFA_App::Initialize(v8::Isolate* pIsolate) {
Lei Zhang94293682016-01-27 18:27:56 -080051 BC_Library_Init();
52 FXJSE_Initialize();
53
tsepezd5f72612016-06-01 14:01:31 -070054 m_pIsolate = pIsolate ? pIsolate : FXJSE_Runtime_Create_Own();
dsinclairec3da5b2016-05-25 16:42:05 -070055 if (!m_pIsolate)
Lei Zhang94293682016-01-27 18:27:56 -080056 return FALSE;
57
dsinclairdf4bc592016-03-31 20:34:43 -070058 m_pXFAApp = new CXFA_FFApp(this);
thestig4dce6d42016-05-31 05:46:52 -070059 m_pXFAApp->SetDefaultFontMgr(
60 std::unique_ptr<CXFA_DefFontMgr>(new CXFA_DefFontMgr));
tsepezd5f72612016-06-01 14:01:31 -070061
Lei Zhang94293682016-01-27 18:27:56 -080062 return TRUE;
63}
64
65FX_BOOL CPDFXFA_App::AddFormFillEnv(CPDFDoc_Environment* pEnv) {
66 if (!pEnv)
67 return FALSE;
68
69 m_pEnvList.Add(pEnv);
70 return TRUE;
71}
72
73FX_BOOL CPDFXFA_App::RemoveFormFillEnv(CPDFDoc_Environment* pEnv) {
74 if (!pEnv)
75 return FALSE;
76
77 int nFind = m_pEnvList.Find(pEnv);
78 if (nFind != -1) {
79 m_pEnvList.RemoveAt(nFind);
80 return TRUE;
81 }
82
83 return FALSE;
84}
85
86void CPDFXFA_App::GetAppType(CFX_WideString& wsAppType) {
87 wsAppType = m_csAppType;
88}
89
90void CPDFXFA_App::GetAppName(CFX_WideString& wsName) {
91 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
92 if (pEnv) {
93 wsName = pEnv->FFI_GetAppName();
94 }
95}
96
97void CPDFXFA_App::SetAppType(const CFX_WideStringC& wsAppType) {
98 m_csAppType = wsAppType;
99}
100
101void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) {
102 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
103 if (pEnv) {
104 wsLanguage = pEnv->FFI_GetLanguage();
105 }
106}
107
108void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) {
109 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
110 if (pEnv) {
111 wsPlatform = pEnv->FFI_GetPlatform();
112 }
113}
114
115void CPDFXFA_App::GetVariation(CFX_WideString& wsVariation) {
116 wsVariation = JS_STR_VIEWERVARIATION;
117}
118
119void CPDFXFA_App::GetVersion(CFX_WideString& wsVersion) {
120 wsVersion = JS_STR_VIEWERVERSION_XFA;
121}
122
tsepezc3255f52016-03-25 14:52:27 -0700123void CPDFXFA_App::Beep(uint32_t dwType) {
Lei Zhang94293682016-01-27 18:27:56 -0800124 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
125 if (pEnv) {
126 pEnv->JS_appBeep(dwType);
127 }
128}
129
tsepez3f80c862016-05-16 12:03:24 -0700130int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage,
131 const CFX_WideString& wsTitle,
tsepezc3255f52016-03-25 14:52:27 -0700132 uint32_t dwIconType,
133 uint32_t dwButtonType) {
Lei Zhang94293682016-01-27 18:27:56 -0800134 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
135 if (!pEnv)
136 return -1;
137
tsepezc3255f52016-03-25 14:52:27 -0700138 uint32_t iconType = 0;
Lei Zhang94293682016-01-27 18:27:56 -0800139 int iButtonType = 0;
140 switch (dwIconType) {
141 case XFA_MBICON_Error:
142 iconType |= 0;
143 break;
144 case XFA_MBICON_Warning:
145 iconType |= 1;
146 break;
147 case XFA_MBICON_Question:
148 iconType |= 2;
149 break;
150 case XFA_MBICON_Status:
151 iconType |= 3;
152 break;
153 }
154 switch (dwButtonType) {
155 case XFA_MB_OK:
156 iButtonType |= 0;
157 break;
158 case XFA_MB_OKCancel:
159 iButtonType |= 1;
160 break;
161 case XFA_MB_YesNo:
162 iButtonType |= 2;
163 break;
164 case XFA_MB_YesNoCancel:
165 iButtonType |= 3;
166 break;
167 }
tsepez660956f2016-04-06 06:27:29 -0700168 int32_t iRet = pEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
Lei Zhang94293682016-01-27 18:27:56 -0800169 iButtonType, iconType);
170 switch (iRet) {
171 case 1:
172 return XFA_IDOK;
173 case 2:
174 return XFA_IDCancel;
175 case 3:
176 return XFA_IDNo;
177 case 4:
178 return XFA_IDYes;
179 }
180 return XFA_IDYes;
181}
182
tsepez3f80c862016-05-16 12:03:24 -0700183CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion,
184 const CFX_WideString& wsTitle,
185 const CFX_WideString& wsDefaultAnswer,
186 FX_BOOL bMark) {
187 CFX_WideString wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800188 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
189 if (pEnv) {
190 int nLength = 2048;
191 char* pBuff = new char[nLength];
tsepez660956f2016-04-06 06:27:29 -0700192 nLength = pEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
thestig1cd352e2016-06-07 17:53:06 -0700193 wsDefaultAnswer.c_str(), nullptr, bMark,
194 pBuff, nLength);
Lei Zhang94293682016-01-27 18:27:56 -0800195 if (nLength > 0) {
196 nLength = nLength > 2046 ? 2046 : nLength;
197 pBuff[nLength] = 0;
198 pBuff[nLength + 1] = 0;
199 wsAnswer = CFX_WideString::FromUTF16LE(
200 reinterpret_cast<const unsigned short*>(pBuff),
201 nLength / sizeof(unsigned short));
202 }
203 delete[] pBuff;
204 }
tsepez3f80c862016-05-16 12:03:24 -0700205 return wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800206}
207
208int32_t CPDFXFA_App::GetCurDocumentInBatch() {
209 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
210 if (pEnv) {
211 return pEnv->FFI_GetCurDocument();
212 }
213 return 0;
214}
215
216int32_t CPDFXFA_App::GetDocumentCountInBatch() {
217 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
218 if (pEnv) {
219 return pEnv->FFI_GetDocumentCount();
220 }
221
222 return 0;
223}
224
tsepez3f80c862016-05-16 12:03:24 -0700225IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) {
Lei Zhang94293682016-01-27 18:27:56 -0800226 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700227 return pEnv ? pEnv->FFI_DownloadFromURL(wsURL.c_str()) : nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800228}
229
tsepez3f80c862016-05-16 12:03:24 -0700230FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL,
231 const CFX_WideString& wsData,
232 const CFX_WideString& wsContentType,
233 const CFX_WideString& wsEncode,
234 const CFX_WideString& wsHeader,
Lei Zhang94293682016-01-27 18:27:56 -0800235 CFX_WideString& wsResponse) {
236 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700237 if (!pEnv)
238 return FALSE;
239
240 wsResponse = pEnv->FFI_PostRequestURL(wsURL.c_str(), wsData.c_str(),
241 wsContentType.c_str(), wsEncode.c_str(),
242 wsHeader.c_str());
243 return TRUE;
Lei Zhang94293682016-01-27 18:27:56 -0800244}
245
tsepez3f80c862016-05-16 12:03:24 -0700246FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL,
247 const CFX_WideString& wsData,
248 const CFX_WideString& wsEncode) {
Lei Zhang94293682016-01-27 18:27:56 -0800249 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700250 return pEnv &&
251 pEnv->FFI_PutRequestURL(wsURL.c_str(), wsData.c_str(),
252 wsEncode.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800253}
254
255void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
256 switch (iStringID) {
257 case XFA_IDS_ValidateFailed:
dsinclair0e0a86a2016-06-01 18:56:51 -0700258 wsString = L"%s validation failed";
Lei Zhang94293682016-01-27 18:27:56 -0800259 return;
260 case XFA_IDS_CalcOverride:
261 wsString = L"Calculate Override";
262 return;
263 case XFA_IDS_ModifyField:
264 wsString = L"Are you sure you want to modify this field?";
265 return;
266 case XFA_IDS_NotModifyField:
267 wsString = L"You are not allowed to modify this field.";
268 return;
269 case XFA_IDS_AppName:
dsinclair0e0a86a2016-06-01 18:56:51 -0700270 wsString = L"pdfium";
Lei Zhang94293682016-01-27 18:27:56 -0800271 return;
272 case XFA_IDS_Unable_TO_SET:
273 wsString = L"Unable to set ";
274 return;
Lei Zhang94293682016-01-27 18:27:56 -0800275 case XFA_IDS_INVAlID_PROP_SET:
dsinclair0e0a86a2016-06-01 18:56:51 -0700276 wsString = L"Invalid property set operation.";
Lei Zhang94293682016-01-27 18:27:56 -0800277 return;
278 case XFA_IDS_NOT_DEFAUL_VALUE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700279 wsString = L" doesn't have a default property.";
Lei Zhang94293682016-01-27 18:27:56 -0800280 return;
281 case XFA_IDS_UNABLE_SET_LANGUAGE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700282 wsString = L"Unable to set language value.";
Lei Zhang94293682016-01-27 18:27:56 -0800283 return;
284 case XFA_IDS_UNABLE_SET_NUMPAGES:
dsinclair0e0a86a2016-06-01 18:56:51 -0700285 wsString = L"Unable to set numPages value.";
Lei Zhang94293682016-01-27 18:27:56 -0800286 return;
287 case XFA_IDS_UNABLE_SET_PLATFORM:
dsinclair0e0a86a2016-06-01 18:56:51 -0700288 wsString = L"Unable to set platform value.";
Lei Zhang94293682016-01-27 18:27:56 -0800289 return;
290 case XFA_IDS_UNABLE_SET_VARIATION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700291 wsString = L"Unable to set variation value.";
Lei Zhang94293682016-01-27 18:27:56 -0800292 return;
293 case XFA_IDS_UNABLE_SET_VERSION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700294 wsString = L"Unable to set version value.";
Lei Zhang94293682016-01-27 18:27:56 -0800295 return;
296 case XFA_IDS_UNABLE_SET_READY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700297 wsString = L"Unable to set ready value.";
Lei Zhang94293682016-01-27 18:27:56 -0800298 return;
299 case XFA_IDS_COMPILER_ERROR:
dsinclair0e0a86a2016-06-01 18:56:51 -0700300 wsString = L"Compiler error.";
Lei Zhang94293682016-01-27 18:27:56 -0800301 return;
302 case XFA_IDS_DIVIDE_ZERO:
dsinclair0e0a86a2016-06-01 18:56:51 -0700303 wsString = L"Divide by zero.";
Lei Zhang94293682016-01-27 18:27:56 -0800304 return;
305 case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
306 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700307 L"An attempt was made to reference property '%s' of a non-object in "
308 L"SOM expression %s.";
Lei Zhang94293682016-01-27 18:27:56 -0800309 return;
310 case XFA_IDS_INDEX_OUT_OF_BOUNDS:
dsinclair0e0a86a2016-06-01 18:56:51 -0700311 wsString = L"Index value is out of bounds.";
Lei Zhang94293682016-01-27 18:27:56 -0800312 return;
313 case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
dsinclair0e0a86a2016-06-01 18:56:51 -0700314 wsString = L"Incorrect number of parameters calling method '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800315 return;
316 case XFA_IDS_ARGUMENT_MISMATCH:
dsinclair0e0a86a2016-06-01 18:56:51 -0700317 wsString = L"Argument mismatch in property or function argument.";
Lei Zhang94293682016-01-27 18:27:56 -0800318 return;
319 case XFA_IDS_NOT_HAVE_PROPERTY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700320 wsString = L"'%s' doesn't have property '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800321 return;
322 case XFA_IDS_VIOLATE_BOUNDARY:
323 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700324 L"The element [%s] has violated its allowable number of occurrences.";
Lei Zhang94293682016-01-27 18:27:56 -0800325 return;
326 case XFA_IDS_SERVER_DENY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700327 wsString = L"Server does not permit.";
Lei Zhang94293682016-01-27 18:27:56 -0800328 return;
329 case XFA_IDS_ValidateLimit:
dsinclair0e0a86a2016-06-01 18:56:51 -0700330 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800331 L"Message limit exceeded. Remaining %d validation errors not "
dsinclair0e0a86a2016-06-01 18:56:51 -0700332 L"reported.";
Lei Zhang94293682016-01-27 18:27:56 -0800333 return;
334 case XFA_IDS_ValidateNullWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700335 wsString =
336 L"%s cannot be blank. To ignore validations for %s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800337 return;
338 case XFA_IDS_ValidateNullError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700339 wsString = L"%s cannot be blank.";
Lei Zhang94293682016-01-27 18:27:56 -0800340 return;
341 case XFA_IDS_ValidateWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700342 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800343 L"The value you entered for %s is invalid. To ignore validations for "
dsinclair0e0a86a2016-06-01 18:56:51 -0700344 L"%s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800345 return;
346 case XFA_IDS_ValidateError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700347 wsString = L"The value you entered for %s is invalid.";
Lei Zhang94293682016-01-27 18:27:56 -0800348 return;
349 }
350}
351
Lei Zhang94293682016-01-27 18:27:56 -0800352IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
thestig1cd352e2016-06-07 17:53:06 -0700353 CXFA_FWLAdapterTimerMgr* pAdapter = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800354 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
355 if (pEnv)
356 pAdapter = new CXFA_FWLAdapterTimerMgr(pEnv);
357 return pAdapter;
358}