blob: e91bdf169975d4eaa1b551481300d24ac7fa0a74 [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
17CPDFXFA_App* CPDFXFA_App::g_pApp = NULL;
18
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;
28 g_pApp = NULL;
29}
30
31CPDFXFA_App::CPDFXFA_App()
32 : m_bJavaScriptInitialized(FALSE),
33 m_pXFAApp(NULL),
Lei Zhang94293682016-01-27 18:27:56 -080034 m_hJSERuntime(NULL),
jinming_wang48661582016-02-04 09:41:56 +080035 m_csAppType(JS_STR_VIEWERTYPE_STANDARD),
36 m_bOwnedRuntime(false) {
Lei Zhang94293682016-01-27 18:27:56 -080037 m_pEnvList.RemoveAll();
38}
39
40CPDFXFA_App::~CPDFXFA_App() {
Lei Zhang94293682016-01-27 18:27:56 -080041 delete m_pXFAApp;
42 m_pXFAApp = NULL;
43
44#ifdef PDF_ENABLE_XFA
jinming_wang48661582016-02-04 09:41:56 +080045 FXJSE_Runtime_Release(m_hJSERuntime, m_bOwnedRuntime);
Lei Zhang94293682016-01-27 18:27:56 -080046 m_hJSERuntime = NULL;
47
48 FXJSE_Finalize();
49 BC_Library_Destory();
50#endif
51}
52
jinming_wang48661582016-02-04 09:41:56 +080053FX_BOOL CPDFXFA_App::Initialize(FXJSE_HRUNTIME hRuntime) {
Lei Zhang94293682016-01-27 18:27:56 -080054#ifdef PDF_ENABLE_XFA
55 BC_Library_Init();
56 FXJSE_Initialize();
57
jinming_wang48661582016-02-04 09:41:56 +080058 m_bOwnedRuntime = !hRuntime;
59 m_hJSERuntime = hRuntime ? hRuntime : FXJSE_Runtime_Create();
Lei Zhang94293682016-01-27 18:27:56 -080060 if (!m_hJSERuntime)
61 return FALSE;
62
dsinclairdf4bc592016-03-31 20:34:43 -070063 m_pXFAApp = new CXFA_FFApp(this);
64 m_pXFAApp->SetDefaultFontMgr(new CXFA_DefFontMgr);
Lei Zhang94293682016-01-27 18:27:56 -080065#endif
66 return TRUE;
67}
68
69FX_BOOL CPDFXFA_App::AddFormFillEnv(CPDFDoc_Environment* pEnv) {
70 if (!pEnv)
71 return FALSE;
72
73 m_pEnvList.Add(pEnv);
74 return TRUE;
75}
76
77FX_BOOL CPDFXFA_App::RemoveFormFillEnv(CPDFDoc_Environment* pEnv) {
78 if (!pEnv)
79 return FALSE;
80
81 int nFind = m_pEnvList.Find(pEnv);
82 if (nFind != -1) {
83 m_pEnvList.RemoveAt(nFind);
84 return TRUE;
85 }
86
87 return FALSE;
88}
89
90void CPDFXFA_App::GetAppType(CFX_WideString& wsAppType) {
91 wsAppType = m_csAppType;
92}
93
94void CPDFXFA_App::GetAppName(CFX_WideString& wsName) {
95 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
96 if (pEnv) {
97 wsName = pEnv->FFI_GetAppName();
98 }
99}
100
101void CPDFXFA_App::SetAppType(const CFX_WideStringC& wsAppType) {
102 m_csAppType = wsAppType;
103}
104
105void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) {
106 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
107 if (pEnv) {
108 wsLanguage = pEnv->FFI_GetLanguage();
109 }
110}
111
112void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) {
113 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
114 if (pEnv) {
115 wsPlatform = pEnv->FFI_GetPlatform();
116 }
117}
118
119void CPDFXFA_App::GetVariation(CFX_WideString& wsVariation) {
120 wsVariation = JS_STR_VIEWERVARIATION;
121}
122
123void CPDFXFA_App::GetVersion(CFX_WideString& wsVersion) {
124 wsVersion = JS_STR_VIEWERVERSION_XFA;
125}
126
tsepezc3255f52016-03-25 14:52:27 -0700127void CPDFXFA_App::Beep(uint32_t dwType) {
Lei Zhang94293682016-01-27 18:27:56 -0800128 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
129 if (pEnv) {
130 pEnv->JS_appBeep(dwType);
131 }
132}
133
tsepez3f80c862016-05-16 12:03:24 -0700134int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage,
135 const CFX_WideString& wsTitle,
tsepezc3255f52016-03-25 14:52:27 -0700136 uint32_t dwIconType,
137 uint32_t dwButtonType) {
Lei Zhang94293682016-01-27 18:27:56 -0800138 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
139 if (!pEnv)
140 return -1;
141
tsepezc3255f52016-03-25 14:52:27 -0700142 uint32_t iconType = 0;
Lei Zhang94293682016-01-27 18:27:56 -0800143 int iButtonType = 0;
144 switch (dwIconType) {
145 case XFA_MBICON_Error:
146 iconType |= 0;
147 break;
148 case XFA_MBICON_Warning:
149 iconType |= 1;
150 break;
151 case XFA_MBICON_Question:
152 iconType |= 2;
153 break;
154 case XFA_MBICON_Status:
155 iconType |= 3;
156 break;
157 }
158 switch (dwButtonType) {
159 case XFA_MB_OK:
160 iButtonType |= 0;
161 break;
162 case XFA_MB_OKCancel:
163 iButtonType |= 1;
164 break;
165 case XFA_MB_YesNo:
166 iButtonType |= 2;
167 break;
168 case XFA_MB_YesNoCancel:
169 iButtonType |= 3;
170 break;
171 }
tsepez660956f2016-04-06 06:27:29 -0700172 int32_t iRet = pEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
Lei Zhang94293682016-01-27 18:27:56 -0800173 iButtonType, iconType);
174 switch (iRet) {
175 case 1:
176 return XFA_IDOK;
177 case 2:
178 return XFA_IDCancel;
179 case 3:
180 return XFA_IDNo;
181 case 4:
182 return XFA_IDYes;
183 }
184 return XFA_IDYes;
185}
186
tsepez3f80c862016-05-16 12:03:24 -0700187CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion,
188 const CFX_WideString& wsTitle,
189 const CFX_WideString& wsDefaultAnswer,
190 FX_BOOL bMark) {
191 CFX_WideString wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800192 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
193 if (pEnv) {
194 int nLength = 2048;
195 char* pBuff = new char[nLength];
tsepez660956f2016-04-06 06:27:29 -0700196 nLength = pEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
197 wsDefaultAnswer.c_str(), NULL, bMark, pBuff,
198 nLength);
Lei Zhang94293682016-01-27 18:27:56 -0800199 if (nLength > 0) {
200 nLength = nLength > 2046 ? 2046 : nLength;
201 pBuff[nLength] = 0;
202 pBuff[nLength + 1] = 0;
203 wsAnswer = CFX_WideString::FromUTF16LE(
204 reinterpret_cast<const unsigned short*>(pBuff),
205 nLength / sizeof(unsigned short));
206 }
207 delete[] pBuff;
208 }
tsepez3f80c862016-05-16 12:03:24 -0700209 return wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800210}
211
212int32_t CPDFXFA_App::GetCurDocumentInBatch() {
213 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
214 if (pEnv) {
215 return pEnv->FFI_GetCurDocument();
216 }
217 return 0;
218}
219
220int32_t CPDFXFA_App::GetDocumentCountInBatch() {
221 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
222 if (pEnv) {
223 return pEnv->FFI_GetDocumentCount();
224 }
225
226 return 0;
227}
228
tsepez3f80c862016-05-16 12:03:24 -0700229IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) {
Lei Zhang94293682016-01-27 18:27:56 -0800230 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700231 return pEnv ? pEnv->FFI_DownloadFromURL(wsURL.c_str()) : nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800232}
233
tsepez3f80c862016-05-16 12:03:24 -0700234FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL,
235 const CFX_WideString& wsData,
236 const CFX_WideString& wsContentType,
237 const CFX_WideString& wsEncode,
238 const CFX_WideString& wsHeader,
Lei Zhang94293682016-01-27 18:27:56 -0800239 CFX_WideString& wsResponse) {
240 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700241 if (!pEnv)
242 return FALSE;
243
244 wsResponse = pEnv->FFI_PostRequestURL(wsURL.c_str(), wsData.c_str(),
245 wsContentType.c_str(), wsEncode.c_str(),
246 wsHeader.c_str());
247 return TRUE;
Lei Zhang94293682016-01-27 18:27:56 -0800248}
249
tsepez3f80c862016-05-16 12:03:24 -0700250FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL,
251 const CFX_WideString& wsData,
252 const CFX_WideString& wsEncode) {
Lei Zhang94293682016-01-27 18:27:56 -0800253 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700254 return pEnv &&
255 pEnv->FFI_PutRequestURL(wsURL.c_str(), wsData.c_str(),
256 wsEncode.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800257}
258
259void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
260 switch (iStringID) {
261 case XFA_IDS_ValidateFailed:
262 wsString = L"%s validate failed";
263 return;
264 case XFA_IDS_CalcOverride:
265 wsString = L"Calculate Override";
266 return;
267 case XFA_IDS_ModifyField:
268 wsString = L"Are you sure you want to modify this field?";
269 return;
270 case XFA_IDS_NotModifyField:
271 wsString = L"You are not allowed to modify this field.";
272 return;
273 case XFA_IDS_AppName:
274 wsString = L"Foxit";
275 return;
276 case XFA_IDS_ImageFilter:
277 wsString =
278 L"Image "
279 L"Files(*.bmp;*.jpg;*.png;*.gif;*.tif)|*.bmp;*.jpg;*.png;*.gif;*.tif|"
280 L"All Files(*.*)|*.*||";
281 return;
282 case XFA_IDS_UNKNOW_CATCHED:
283 wsString = L"unknown error is catched!";
284 return;
285 case XFA_IDS_Unable_TO_SET:
286 wsString = L"Unable to set ";
287 return;
288 case XFA_IDS_VALUE_EXCALMATORY:
289 wsString = L" value!";
290 return;
291 case XFA_IDS_INVALID_ENUM_VALUE:
292 wsString = L"Invalid enumerated value: ";
293 return;
294 case XFA_IDS_UNSUPPORT_METHOD:
295 wsString = L"unsupport %s method.";
296 return;
297 case XFA_IDS_UNSUPPORT_PROP:
298 wsString = L"unsupport %s property.";
299 return;
300 case XFA_IDS_INVAlID_PROP_SET:
301 wsString = L"Invalid property set operation;";
302 return;
303 case XFA_IDS_NOT_DEFAUL_VALUE:
304 wsString = L" doesn't have a default property";
305 return;
306 case XFA_IDS_UNABLE_SET_LANGUAGE:
307 wsString = L"Unable to set language value!";
308 return;
309 case XFA_IDS_UNABLE_SET_NUMPAGES:
310 wsString = L"Unable to set numPages value!";
311 return;
312 case XFA_IDS_UNABLE_SET_PLATFORM:
313 wsString = L"Unable to set platform value!";
314 return;
315 case XFA_IDS_UNABLE_SET_VALIDATIONENABLE:
316 wsString = L"Unable to set validationsEnabled value!";
317 return;
318 case XFA_IDS_UNABLE_SET_VARIATION:
319 wsString = L"Unable to set variation value!";
320 return;
321 case XFA_IDS_UNABLE_SET_VERSION:
322 wsString = L"Unable to set version value!";
323 return;
324 case XFA_IDS_UNABLE_SET_READY:
325 wsString = L"Unable to set ready value!";
326 return;
327 case XFA_IDS_NUMBER_OF_OCCUR:
328 wsString =
329 L"The element [%s] has violated its allowable number of occurrences";
330 return;
331 case XFA_IDS_UNABLE_SET_CLASS_NAME:
332 wsString = L"Unable to set className value!";
333 return;
334 case XFA_IDS_UNABLE_SET_LENGTH_VALUE:
335 wsString = L"Unable to set length value!";
336 return;
337 case XFA_IDS_UNSUPPORT_CHAR:
338 wsString = L"unsupported char '%c'";
339 return;
340 case XFA_IDS_BAD_SUFFIX:
341 wsString = L"bad suffix on number";
342 return;
343 case XFA_IDS_EXPECTED_IDENT:
344 wsString = L"expected identifier instead of '%s'";
345 return;
346 case XFA_IDS_EXPECTED_STRING:
347 wsString = L"expected '%s' instead of '%s'";
348 return;
349 case XFA_IDS_INVALIDATE_CHAR:
350 wsString = L"invalidate char '%c'";
351 return;
352 case XFA_IDS_REDEFINITION:
353 wsString = L"'%s' redefinition ";
354 return;
355 case XFA_IDS_INVALIDATE_TOKEN:
356 wsString = L"invalidate token '%s'";
357 return;
358 case XFA_IDS_INVALIDATE_EXPRESSION:
359 wsString = L"invalidate expression '%s'";
360 return;
361 case XFA_IDS_UNDEFINE_IDENTIFIER:
362 wsString = L"undefined identifier '%s'";
363 return;
364 case XFA_IDS_INVALIDATE_LEFTVALUE:
365 wsString = L"invalidate left-value '%s'";
366 return;
367 case XFA_IDS_COMPILER_ERROR:
368 wsString = L"compiler error";
369 return;
370 case XFA_IDS_CANNOT_MODIFY_VALUE:
371 wsString = L"can't modify the '%s' value";
372 return;
373 case XFA_IDS_ERROR_PARAMETERS:
374 wsString = L"function '%s' has not %d parameters";
375 return;
376 case XFA_IDS_EXPECT_ENDIF:
377 wsString = L"expected 'endif' instead of '%s'";
378 return;
379 case XFA_IDS_UNEXPECTED_EXPRESSION:
380 wsString = L"unexpected expression '%s'";
381 return;
382 case XFA_IDS_CONDITION_IS_NULL:
383 wsString = L"condition is null";
384 return;
385 case XFA_IDS_ILLEGALBREAK:
386 wsString = L"illegal break";
387 return;
388 case XFA_IDS_ILLEGALCONTINUE:
389 wsString = L"illegal continue";
390 return;
391 case XFA_IDS_EXPECTED_OPERATOR:
392 wsString = L"expected operator '%s' instead of '%s'";
393 return;
394 case XFA_IDS_DIVIDE_ZERO:
395 wsString = L"divide by zero";
396 return;
397 case XFA_IDS_CANNOT_COVERT_OBJECT:
398 wsString = L"%s.%s can not covert to object";
399 return;
400 case XFA_IDS_NOT_FOUND_CONTAINER:
401 wsString = L"can not found container '%s'";
402 return;
403 case XFA_IDS_NOT_FOUND_PROPERTY:
404 wsString = L"can not found property '%s'";
405 return;
406 case XFA_IDS_NOT_FOUND_METHOD:
407 wsString = L"can not found method '%s'";
408 return;
409 case XFA_IDS_NOT_FOUND_CONST:
410 wsString = L"can not found const '%s'";
411 return;
412 case XFA_IDS_NOT_ASSIGN_OBJECT:
413 wsString = L"can not direct assign value to object";
414 return;
415 case XFA_IDS_IVALIDATE_INSTRUCTION:
416 wsString = L"invalidate instruction";
417 return;
418 case XFA_IDS_EXPECT_NUMBER:
419 wsString = L"expected number instead of '%s'";
420 return;
421 case XFA_IDS_VALIDATE_OUT_ARRAY:
422 wsString = L"validate access index '%s' out of array";
423 return;
424 case XFA_IDS_CANNOT_ASSIGN_IDENT:
425 wsString = L"can not assign to %s";
426 return;
427 case XFA_IDS_NOT_FOUNT_FUNCTION:
428 wsString = L"can not found '%s' function";
429 return;
430 case XFA_IDS_NOT_ARRAY:
431 wsString = L"'%s' doesn't an array";
432 return;
433 case XFA_IDS_OUT_ARRAY:
434 wsString = L"out of range of '%s' array";
435 return;
436 case XFA_IDS_NOT_SUPPORT_CALC:
437 wsString = L"'%s' operator can not support array calculate";
438 return;
439 case XFA_IDS_ARGUMENT_NOT_ARRAY:
440 wsString = L"'%s' function's %d argument can not be array";
441 return;
442 case XFA_IDS_ARGUMENT_EXPECT_CONTAINER:
443 wsString = L"'%s' argument expected a container";
444 return;
445 case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
446 wsString =
447 L"an attempt was made to reference property '%s' of a non-object in "
448 L"SOM expression %s";
449 return;
450 case XFA_IDS_FUNCTION_IS_BUILDIN:
451 wsString = L"function '%s' is buildin";
452 return;
453 case XFA_IDS_ERROR_MSG:
454 wsString = L"%s : %s";
455 return;
456 case XFA_IDS_INDEX_OUT_OF_BOUNDS:
457 wsString = L"Index value is out of bounds";
458 return;
459 case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
460 wsString = L"Incorrect number of parameters calling method '%s'";
461 return;
462 case XFA_IDS_ARGUMENT_MISMATCH:
463 wsString = L"Argument mismatch in property or function argument";
464 return;
465 case XFA_IDS_INVALID_ENUMERATE:
466 wsString = L"Invalid enumerated value: %s";
467 return;
468 case XFA_IDS_INVALID_APPEND:
469 wsString =
470 L"Invalid append operation: %s cannot have a child element of %s";
471 return;
472 case XFA_IDS_SOM_EXPECTED_LIST:
473 wsString =
474 L"SOM expression returned list when single result was expected";
475 return;
476 case XFA_IDS_NOT_HAVE_PROPERTY:
477 wsString = L"'%s' doesn't have property '%s'";
478 return;
479 case XFA_IDS_INVALID_NODE_TYPE:
480 wsString = L"Invalid node type : '%s'";
481 return;
482 case XFA_IDS_VIOLATE_BOUNDARY:
483 wsString =
484 L"The element [%s] has violated its allowable number of occurrences";
485 return;
486 case XFA_IDS_SERVER_DENY:
487 wsString = L"Server does not permit";
488 return;
489 case XFA_IDS_ValidateLimit:
490 wsString = FX_WSTRC(
491 L"Message limit exceeded. Remaining %d validation errors not "
492 L"reported.");
493 return;
494 case XFA_IDS_ValidateNullWarning:
495 wsString = FX_WSTRC(
496 L"%s cannot be left blank. To ignore validations for %s, click "
497 L"Ignore.");
498 return;
499 case XFA_IDS_ValidateNullError:
500 wsString = FX_WSTRC(L"%s cannot be left blank.");
501 return;
502 case XFA_IDS_ValidateWarning:
503 wsString = FX_WSTRC(
504 L"The value you entered for %s is invalid. To ignore validations for "
505 L"%s, click Ignore.");
506 return;
507 case XFA_IDS_ValidateError:
508 wsString = FX_WSTRC(L"The value you entered for %s is invalid.");
509 return;
510 }
511}
512
Lei Zhang94293682016-01-27 18:27:56 -0800513IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
514 CXFA_FWLAdapterTimerMgr* pAdapter = NULL;
515 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
516 if (pEnv)
517 pAdapter = new CXFA_FWLAdapterTimerMgr(pEnv);
518 return pAdapter;
519}