blob: b7a5b15dd9cb6f4eb2c7f7b9392aa44418ab1f94 [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"
dsinclairdf4bc592016-03-31 20:34:43 -070014#include "xfa/include/fxfa/xfa_ffapp.h"
15#include "xfa/include/fxfa/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
134int32_t CPDFXFA_App::MsgBox(const CFX_WideStringC& wsMessage,
135 const CFX_WideStringC& 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
187void CPDFXFA_App::Response(CFX_WideString& wsAnswer,
188 const CFX_WideStringC& wsQuestion,
189 const CFX_WideStringC& wsTitle,
190 const CFX_WideStringC& wsDefaultAnswer,
191 FX_BOOL bMark) {
192 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 }
209}
210
211int32_t CPDFXFA_App::GetCurDocumentInBatch() {
212 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
213 if (pEnv) {
214 return pEnv->FFI_GetCurDocument();
215 }
216 return 0;
217}
218
219int32_t CPDFXFA_App::GetDocumentCountInBatch() {
220 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
221 if (pEnv) {
222 return pEnv->FFI_GetDocumentCount();
223 }
224
225 return 0;
226}
227
228IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideStringC& wsURL) {
229 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
230 if (pEnv) {
tsepez660956f2016-04-06 06:27:29 -0700231 return pEnv->FFI_DownloadFromURL(wsURL.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800232 }
233 return NULL;
234}
235
236FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideStringC& wsURL,
237 const CFX_WideStringC& wsData,
238 const CFX_WideStringC& wsContentType,
239 const CFX_WideStringC& wsEncode,
240 const CFX_WideStringC& wsHeader,
241 CFX_WideString& wsResponse) {
242 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
243 if (pEnv) {
tsepez660956f2016-04-06 06:27:29 -0700244 wsResponse = pEnv->FFI_PostRequestURL(wsURL.c_str(), wsData.c_str(),
245 wsContentType.c_str(),
246 wsEncode.c_str(), wsHeader.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800247 return TRUE;
248 }
249 return FALSE;
250}
251
252FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideStringC& wsURL,
253 const CFX_WideStringC& wsData,
254 const CFX_WideStringC& wsEncode) {
255 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
256 if (pEnv) {
tsepez660956f2016-04-06 06:27:29 -0700257 return pEnv->FFI_PutRequestURL(wsURL.c_str(), wsData.c_str(),
258 wsEncode.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800259 }
260 return FALSE;
261}
262
263void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
264 switch (iStringID) {
265 case XFA_IDS_ValidateFailed:
266 wsString = L"%s validate failed";
267 return;
268 case XFA_IDS_CalcOverride:
269 wsString = L"Calculate Override";
270 return;
271 case XFA_IDS_ModifyField:
272 wsString = L"Are you sure you want to modify this field?";
273 return;
274 case XFA_IDS_NotModifyField:
275 wsString = L"You are not allowed to modify this field.";
276 return;
277 case XFA_IDS_AppName:
278 wsString = L"Foxit";
279 return;
280 case XFA_IDS_ImageFilter:
281 wsString =
282 L"Image "
283 L"Files(*.bmp;*.jpg;*.png;*.gif;*.tif)|*.bmp;*.jpg;*.png;*.gif;*.tif|"
284 L"All Files(*.*)|*.*||";
285 return;
286 case XFA_IDS_UNKNOW_CATCHED:
287 wsString = L"unknown error is catched!";
288 return;
289 case XFA_IDS_Unable_TO_SET:
290 wsString = L"Unable to set ";
291 return;
292 case XFA_IDS_VALUE_EXCALMATORY:
293 wsString = L" value!";
294 return;
295 case XFA_IDS_INVALID_ENUM_VALUE:
296 wsString = L"Invalid enumerated value: ";
297 return;
298 case XFA_IDS_UNSUPPORT_METHOD:
299 wsString = L"unsupport %s method.";
300 return;
301 case XFA_IDS_UNSUPPORT_PROP:
302 wsString = L"unsupport %s property.";
303 return;
304 case XFA_IDS_INVAlID_PROP_SET:
305 wsString = L"Invalid property set operation;";
306 return;
307 case XFA_IDS_NOT_DEFAUL_VALUE:
308 wsString = L" doesn't have a default property";
309 return;
310 case XFA_IDS_UNABLE_SET_LANGUAGE:
311 wsString = L"Unable to set language value!";
312 return;
313 case XFA_IDS_UNABLE_SET_NUMPAGES:
314 wsString = L"Unable to set numPages value!";
315 return;
316 case XFA_IDS_UNABLE_SET_PLATFORM:
317 wsString = L"Unable to set platform value!";
318 return;
319 case XFA_IDS_UNABLE_SET_VALIDATIONENABLE:
320 wsString = L"Unable to set validationsEnabled value!";
321 return;
322 case XFA_IDS_UNABLE_SET_VARIATION:
323 wsString = L"Unable to set variation value!";
324 return;
325 case XFA_IDS_UNABLE_SET_VERSION:
326 wsString = L"Unable to set version value!";
327 return;
328 case XFA_IDS_UNABLE_SET_READY:
329 wsString = L"Unable to set ready value!";
330 return;
331 case XFA_IDS_NUMBER_OF_OCCUR:
332 wsString =
333 L"The element [%s] has violated its allowable number of occurrences";
334 return;
335 case XFA_IDS_UNABLE_SET_CLASS_NAME:
336 wsString = L"Unable to set className value!";
337 return;
338 case XFA_IDS_UNABLE_SET_LENGTH_VALUE:
339 wsString = L"Unable to set length value!";
340 return;
341 case XFA_IDS_UNSUPPORT_CHAR:
342 wsString = L"unsupported char '%c'";
343 return;
344 case XFA_IDS_BAD_SUFFIX:
345 wsString = L"bad suffix on number";
346 return;
347 case XFA_IDS_EXPECTED_IDENT:
348 wsString = L"expected identifier instead of '%s'";
349 return;
350 case XFA_IDS_EXPECTED_STRING:
351 wsString = L"expected '%s' instead of '%s'";
352 return;
353 case XFA_IDS_INVALIDATE_CHAR:
354 wsString = L"invalidate char '%c'";
355 return;
356 case XFA_IDS_REDEFINITION:
357 wsString = L"'%s' redefinition ";
358 return;
359 case XFA_IDS_INVALIDATE_TOKEN:
360 wsString = L"invalidate token '%s'";
361 return;
362 case XFA_IDS_INVALIDATE_EXPRESSION:
363 wsString = L"invalidate expression '%s'";
364 return;
365 case XFA_IDS_UNDEFINE_IDENTIFIER:
366 wsString = L"undefined identifier '%s'";
367 return;
368 case XFA_IDS_INVALIDATE_LEFTVALUE:
369 wsString = L"invalidate left-value '%s'";
370 return;
371 case XFA_IDS_COMPILER_ERROR:
372 wsString = L"compiler error";
373 return;
374 case XFA_IDS_CANNOT_MODIFY_VALUE:
375 wsString = L"can't modify the '%s' value";
376 return;
377 case XFA_IDS_ERROR_PARAMETERS:
378 wsString = L"function '%s' has not %d parameters";
379 return;
380 case XFA_IDS_EXPECT_ENDIF:
381 wsString = L"expected 'endif' instead of '%s'";
382 return;
383 case XFA_IDS_UNEXPECTED_EXPRESSION:
384 wsString = L"unexpected expression '%s'";
385 return;
386 case XFA_IDS_CONDITION_IS_NULL:
387 wsString = L"condition is null";
388 return;
389 case XFA_IDS_ILLEGALBREAK:
390 wsString = L"illegal break";
391 return;
392 case XFA_IDS_ILLEGALCONTINUE:
393 wsString = L"illegal continue";
394 return;
395 case XFA_IDS_EXPECTED_OPERATOR:
396 wsString = L"expected operator '%s' instead of '%s'";
397 return;
398 case XFA_IDS_DIVIDE_ZERO:
399 wsString = L"divide by zero";
400 return;
401 case XFA_IDS_CANNOT_COVERT_OBJECT:
402 wsString = L"%s.%s can not covert to object";
403 return;
404 case XFA_IDS_NOT_FOUND_CONTAINER:
405 wsString = L"can not found container '%s'";
406 return;
407 case XFA_IDS_NOT_FOUND_PROPERTY:
408 wsString = L"can not found property '%s'";
409 return;
410 case XFA_IDS_NOT_FOUND_METHOD:
411 wsString = L"can not found method '%s'";
412 return;
413 case XFA_IDS_NOT_FOUND_CONST:
414 wsString = L"can not found const '%s'";
415 return;
416 case XFA_IDS_NOT_ASSIGN_OBJECT:
417 wsString = L"can not direct assign value to object";
418 return;
419 case XFA_IDS_IVALIDATE_INSTRUCTION:
420 wsString = L"invalidate instruction";
421 return;
422 case XFA_IDS_EXPECT_NUMBER:
423 wsString = L"expected number instead of '%s'";
424 return;
425 case XFA_IDS_VALIDATE_OUT_ARRAY:
426 wsString = L"validate access index '%s' out of array";
427 return;
428 case XFA_IDS_CANNOT_ASSIGN_IDENT:
429 wsString = L"can not assign to %s";
430 return;
431 case XFA_IDS_NOT_FOUNT_FUNCTION:
432 wsString = L"can not found '%s' function";
433 return;
434 case XFA_IDS_NOT_ARRAY:
435 wsString = L"'%s' doesn't an array";
436 return;
437 case XFA_IDS_OUT_ARRAY:
438 wsString = L"out of range of '%s' array";
439 return;
440 case XFA_IDS_NOT_SUPPORT_CALC:
441 wsString = L"'%s' operator can not support array calculate";
442 return;
443 case XFA_IDS_ARGUMENT_NOT_ARRAY:
444 wsString = L"'%s' function's %d argument can not be array";
445 return;
446 case XFA_IDS_ARGUMENT_EXPECT_CONTAINER:
447 wsString = L"'%s' argument expected a container";
448 return;
449 case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
450 wsString =
451 L"an attempt was made to reference property '%s' of a non-object in "
452 L"SOM expression %s";
453 return;
454 case XFA_IDS_FUNCTION_IS_BUILDIN:
455 wsString = L"function '%s' is buildin";
456 return;
457 case XFA_IDS_ERROR_MSG:
458 wsString = L"%s : %s";
459 return;
460 case XFA_IDS_INDEX_OUT_OF_BOUNDS:
461 wsString = L"Index value is out of bounds";
462 return;
463 case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
464 wsString = L"Incorrect number of parameters calling method '%s'";
465 return;
466 case XFA_IDS_ARGUMENT_MISMATCH:
467 wsString = L"Argument mismatch in property or function argument";
468 return;
469 case XFA_IDS_INVALID_ENUMERATE:
470 wsString = L"Invalid enumerated value: %s";
471 return;
472 case XFA_IDS_INVALID_APPEND:
473 wsString =
474 L"Invalid append operation: %s cannot have a child element of %s";
475 return;
476 case XFA_IDS_SOM_EXPECTED_LIST:
477 wsString =
478 L"SOM expression returned list when single result was expected";
479 return;
480 case XFA_IDS_NOT_HAVE_PROPERTY:
481 wsString = L"'%s' doesn't have property '%s'";
482 return;
483 case XFA_IDS_INVALID_NODE_TYPE:
484 wsString = L"Invalid node type : '%s'";
485 return;
486 case XFA_IDS_VIOLATE_BOUNDARY:
487 wsString =
488 L"The element [%s] has violated its allowable number of occurrences";
489 return;
490 case XFA_IDS_SERVER_DENY:
491 wsString = L"Server does not permit";
492 return;
493 case XFA_IDS_ValidateLimit:
494 wsString = FX_WSTRC(
495 L"Message limit exceeded. Remaining %d validation errors not "
496 L"reported.");
497 return;
498 case XFA_IDS_ValidateNullWarning:
499 wsString = FX_WSTRC(
500 L"%s cannot be left blank. To ignore validations for %s, click "
501 L"Ignore.");
502 return;
503 case XFA_IDS_ValidateNullError:
504 wsString = FX_WSTRC(L"%s cannot be left blank.");
505 return;
506 case XFA_IDS_ValidateWarning:
507 wsString = FX_WSTRC(
508 L"The value you entered for %s is invalid. To ignore validations for "
509 L"%s, click Ignore.");
510 return;
511 case XFA_IDS_ValidateError:
512 wsString = FX_WSTRC(L"The value you entered for %s is invalid.");
513 return;
514 }
515}
516
517FX_BOOL CPDFXFA_App::ShowFileDialog(const CFX_WideStringC& wsTitle,
518 const CFX_WideStringC& wsFilter,
519 CFX_WideStringArray& wsPathArr,
520 FX_BOOL bOpen) {
521 return FALSE;
522}
523
524IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
525 CXFA_FWLAdapterTimerMgr* pAdapter = NULL;
526 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
527 if (pEnv)
528 pAdapter = new CXFA_FWLAdapterTimerMgr(pEnv);
529 return pAdapter;
530}