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