blob: 5fe114a64cca6bd6e33f050deaccfbdc8b02488f [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"
weili625ad662016-06-15 11:21:33 -07008
dsinclair89bdd082016-04-06 10:47:54 -07009#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"
Dan Sinclaira98600a2016-03-21 15:15:56 -040012#include "xfa/fxbarcode/include/BC_Library.h"
dsinclair7222ea62016-04-06 14:33:07 -070013#include "xfa/fxfa/include/xfa_ffapp.h"
14#include "xfa/fxfa/include/xfa_fontmgr.h"
Lei Zhang94293682016-01-27 18:27:56 -080015
thestig1cd352e2016-06-07 17:53:06 -070016CPDFXFA_App* CPDFXFA_App::g_pApp = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080017
18CPDFXFA_App* CPDFXFA_App::GetInstance() {
19 if (!g_pApp) {
20 g_pApp = new CPDFXFA_App();
21 }
22 return g_pApp;
23}
24
25void CPDFXFA_App::ReleaseInstance() {
26 delete g_pApp;
thestig1cd352e2016-06-07 17:53:06 -070027 g_pApp = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080028}
29
30CPDFXFA_App::CPDFXFA_App()
31 : m_bJavaScriptInitialized(FALSE),
thestig1cd352e2016-06-07 17:53:06 -070032 m_pXFAApp(nullptr),
dsinclairec3da5b2016-05-25 16:42:05 -070033 m_pIsolate(nullptr),
tsepezd5f72612016-06-01 14:01:31 -070034 m_csAppType(JS_STR_VIEWERTYPE_STANDARD) {
Lei Zhang94293682016-01-27 18:27:56 -080035 m_pEnvList.RemoveAll();
36}
37
38CPDFXFA_App::~CPDFXFA_App() {
Lei Zhang94293682016-01-27 18:27:56 -080039 delete m_pXFAApp;
thestig1cd352e2016-06-07 17:53:06 -070040 m_pXFAApp = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080041
tsepezd5f72612016-06-01 14:01:31 -070042 FXJSE_Runtime_Release(m_pIsolate);
dsinclairec3da5b2016-05-25 16:42:05 -070043 m_pIsolate = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -080044
45 FXJSE_Finalize();
46 BC_Library_Destory();
Lei Zhang94293682016-01-27 18:27:56 -080047}
48
dsinclairec3da5b2016-05-25 16:42:05 -070049FX_BOOL CPDFXFA_App::Initialize(v8::Isolate* pIsolate) {
Lei Zhang94293682016-01-27 18:27:56 -080050 BC_Library_Init();
51 FXJSE_Initialize();
52
tsepezd5f72612016-06-01 14:01:31 -070053 m_pIsolate = pIsolate ? pIsolate : FXJSE_Runtime_Create_Own();
dsinclairec3da5b2016-05-25 16:42:05 -070054 if (!m_pIsolate)
Lei Zhang94293682016-01-27 18:27:56 -080055 return FALSE;
56
dsinclairdf4bc592016-03-31 20:34:43 -070057 m_pXFAApp = new CXFA_FFApp(this);
thestig4dce6d42016-05-31 05:46:52 -070058 m_pXFAApp->SetDefaultFontMgr(
59 std::unique_ptr<CXFA_DefFontMgr>(new CXFA_DefFontMgr));
tsepezd5f72612016-06-01 14:01:31 -070060
Lei Zhang94293682016-01-27 18:27:56 -080061 return TRUE;
62}
63
64FX_BOOL CPDFXFA_App::AddFormFillEnv(CPDFDoc_Environment* pEnv) {
65 if (!pEnv)
66 return FALSE;
67
68 m_pEnvList.Add(pEnv);
69 return TRUE;
70}
71
72FX_BOOL CPDFXFA_App::RemoveFormFillEnv(CPDFDoc_Environment* pEnv) {
73 if (!pEnv)
74 return FALSE;
75
76 int nFind = m_pEnvList.Find(pEnv);
77 if (nFind != -1) {
78 m_pEnvList.RemoveAt(nFind);
79 return TRUE;
80 }
81
82 return FALSE;
83}
84
85void CPDFXFA_App::GetAppType(CFX_WideString& wsAppType) {
86 wsAppType = m_csAppType;
87}
88
89void CPDFXFA_App::GetAppName(CFX_WideString& wsName) {
90 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
91 if (pEnv) {
92 wsName = pEnv->FFI_GetAppName();
93 }
94}
95
weili625ad662016-06-15 11:21:33 -070096void CPDFXFA_App::GetFoxitAppName(CFX_WideString& wsFoxitName) {
97 wsFoxitName = L"Foxit";
98}
99
Lei Zhang94293682016-01-27 18:27:56 -0800100void CPDFXFA_App::SetAppType(const CFX_WideStringC& wsAppType) {
101 m_csAppType = wsAppType;
102}
103
weili625ad662016-06-15 11:21:33 -0700104void CPDFXFA_App::SetFoxitAppType(const CFX_WideStringC& wsFoxitAppType) {}
105
106void CPDFXFA_App::GetFoxitAppType(CFX_WideString& wsFoxitAppType) {
107 wsFoxitAppType.clear();
108}
109
Lei Zhang94293682016-01-27 18:27:56 -0800110void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) {
111 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
112 if (pEnv) {
113 wsLanguage = pEnv->FFI_GetLanguage();
114 }
115}
116
117void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) {
118 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
119 if (pEnv) {
120 wsPlatform = pEnv->FFI_GetPlatform();
121 }
122}
123
124void CPDFXFA_App::GetVariation(CFX_WideString& wsVariation) {
125 wsVariation = JS_STR_VIEWERVARIATION;
126}
127
128void CPDFXFA_App::GetVersion(CFX_WideString& wsVersion) {
129 wsVersion = JS_STR_VIEWERVERSION_XFA;
130}
131
weili625ad662016-06-15 11:21:33 -0700132void CPDFXFA_App::GetFoxitVersion(CFX_WideString& wsFoxitVersion) {
133 wsFoxitVersion = L"7.0";
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
tsepez3f80c862016-05-16 12:03:24 -0700143int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage,
144 const CFX_WideString& 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 }
tsepez660956f2016-04-06 06:27:29 -0700181 int32_t iRet = pEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
Lei Zhang94293682016-01-27 18:27:56 -0800182 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
tsepez3f80c862016-05-16 12:03:24 -0700196CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion,
197 const CFX_WideString& wsTitle,
198 const CFX_WideString& wsDefaultAnswer,
199 FX_BOOL bMark) {
200 CFX_WideString wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800201 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
202 if (pEnv) {
203 int nLength = 2048;
204 char* pBuff = new char[nLength];
tsepez660956f2016-04-06 06:27:29 -0700205 nLength = pEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
thestig1cd352e2016-06-07 17:53:06 -0700206 wsDefaultAnswer.c_str(), nullptr, bMark,
207 pBuff, nLength);
Lei Zhang94293682016-01-27 18:27:56 -0800208 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 }
tsepez3f80c862016-05-16 12:03:24 -0700218 return wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800219}
220
221int32_t CPDFXFA_App::GetCurDocumentInBatch() {
222 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
223 if (pEnv) {
224 return pEnv->FFI_GetCurDocument();
225 }
226 return 0;
227}
228
229int32_t CPDFXFA_App::GetDocumentCountInBatch() {
230 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
231 if (pEnv) {
232 return pEnv->FFI_GetDocumentCount();
233 }
234
235 return 0;
236}
237
tsepez3f80c862016-05-16 12:03:24 -0700238IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) {
Lei Zhang94293682016-01-27 18:27:56 -0800239 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700240 return pEnv ? pEnv->FFI_DownloadFromURL(wsURL.c_str()) : nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800241}
242
tsepez3f80c862016-05-16 12:03:24 -0700243FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL,
244 const CFX_WideString& wsData,
245 const CFX_WideString& wsContentType,
246 const CFX_WideString& wsEncode,
247 const CFX_WideString& wsHeader,
Lei Zhang94293682016-01-27 18:27:56 -0800248 CFX_WideString& wsResponse) {
249 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700250 if (!pEnv)
251 return FALSE;
252
253 wsResponse = pEnv->FFI_PostRequestURL(wsURL.c_str(), wsData.c_str(),
254 wsContentType.c_str(), wsEncode.c_str(),
255 wsHeader.c_str());
256 return TRUE;
Lei Zhang94293682016-01-27 18:27:56 -0800257}
258
tsepez3f80c862016-05-16 12:03:24 -0700259FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL,
260 const CFX_WideString& wsData,
261 const CFX_WideString& wsEncode) {
Lei Zhang94293682016-01-27 18:27:56 -0800262 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
tsepez3f80c862016-05-16 12:03:24 -0700263 return pEnv &&
264 pEnv->FFI_PutRequestURL(wsURL.c_str(), wsData.c_str(),
265 wsEncode.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800266}
267
268void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
269 switch (iStringID) {
270 case XFA_IDS_ValidateFailed:
dsinclair0e0a86a2016-06-01 18:56:51 -0700271 wsString = L"%s validation failed";
Lei Zhang94293682016-01-27 18:27:56 -0800272 return;
273 case XFA_IDS_CalcOverride:
274 wsString = L"Calculate Override";
275 return;
276 case XFA_IDS_ModifyField:
277 wsString = L"Are you sure you want to modify this field?";
278 return;
279 case XFA_IDS_NotModifyField:
280 wsString = L"You are not allowed to modify this field.";
281 return;
282 case XFA_IDS_AppName:
dsinclair0e0a86a2016-06-01 18:56:51 -0700283 wsString = L"pdfium";
Lei Zhang94293682016-01-27 18:27:56 -0800284 return;
285 case XFA_IDS_Unable_TO_SET:
286 wsString = L"Unable to set ";
287 return;
Lei Zhang94293682016-01-27 18:27:56 -0800288 case XFA_IDS_INVAlID_PROP_SET:
dsinclair0e0a86a2016-06-01 18:56:51 -0700289 wsString = L"Invalid property set operation.";
Lei Zhang94293682016-01-27 18:27:56 -0800290 return;
291 case XFA_IDS_NOT_DEFAUL_VALUE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700292 wsString = L" doesn't have a default property.";
Lei Zhang94293682016-01-27 18:27:56 -0800293 return;
294 case XFA_IDS_UNABLE_SET_LANGUAGE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700295 wsString = L"Unable to set language value.";
Lei Zhang94293682016-01-27 18:27:56 -0800296 return;
297 case XFA_IDS_UNABLE_SET_NUMPAGES:
dsinclair0e0a86a2016-06-01 18:56:51 -0700298 wsString = L"Unable to set numPages value.";
Lei Zhang94293682016-01-27 18:27:56 -0800299 return;
300 case XFA_IDS_UNABLE_SET_PLATFORM:
dsinclair0e0a86a2016-06-01 18:56:51 -0700301 wsString = L"Unable to set platform value.";
Lei Zhang94293682016-01-27 18:27:56 -0800302 return;
303 case XFA_IDS_UNABLE_SET_VARIATION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700304 wsString = L"Unable to set variation value.";
Lei Zhang94293682016-01-27 18:27:56 -0800305 return;
306 case XFA_IDS_UNABLE_SET_VERSION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700307 wsString = L"Unable to set version value.";
Lei Zhang94293682016-01-27 18:27:56 -0800308 return;
309 case XFA_IDS_UNABLE_SET_READY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700310 wsString = L"Unable to set ready value.";
Lei Zhang94293682016-01-27 18:27:56 -0800311 return;
312 case XFA_IDS_COMPILER_ERROR:
dsinclair0e0a86a2016-06-01 18:56:51 -0700313 wsString = L"Compiler error.";
Lei Zhang94293682016-01-27 18:27:56 -0800314 return;
315 case XFA_IDS_DIVIDE_ZERO:
dsinclair0e0a86a2016-06-01 18:56:51 -0700316 wsString = L"Divide by zero.";
Lei Zhang94293682016-01-27 18:27:56 -0800317 return;
318 case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
319 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700320 L"An attempt was made to reference property '%s' of a non-object in "
321 L"SOM expression %s.";
Lei Zhang94293682016-01-27 18:27:56 -0800322 return;
323 case XFA_IDS_INDEX_OUT_OF_BOUNDS:
dsinclair0e0a86a2016-06-01 18:56:51 -0700324 wsString = L"Index value is out of bounds.";
Lei Zhang94293682016-01-27 18:27:56 -0800325 return;
326 case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
dsinclair0e0a86a2016-06-01 18:56:51 -0700327 wsString = L"Incorrect number of parameters calling method '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800328 return;
329 case XFA_IDS_ARGUMENT_MISMATCH:
dsinclair0e0a86a2016-06-01 18:56:51 -0700330 wsString = L"Argument mismatch in property or function argument.";
Lei Zhang94293682016-01-27 18:27:56 -0800331 return;
332 case XFA_IDS_NOT_HAVE_PROPERTY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700333 wsString = L"'%s' doesn't have property '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800334 return;
335 case XFA_IDS_VIOLATE_BOUNDARY:
336 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700337 L"The element [%s] has violated its allowable number of occurrences.";
Lei Zhang94293682016-01-27 18:27:56 -0800338 return;
339 case XFA_IDS_SERVER_DENY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700340 wsString = L"Server does not permit.";
Lei Zhang94293682016-01-27 18:27:56 -0800341 return;
342 case XFA_IDS_ValidateLimit:
dsinclair0e0a86a2016-06-01 18:56:51 -0700343 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800344 L"Message limit exceeded. Remaining %d validation errors not "
dsinclair0e0a86a2016-06-01 18:56:51 -0700345 L"reported.";
Lei Zhang94293682016-01-27 18:27:56 -0800346 return;
347 case XFA_IDS_ValidateNullWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700348 wsString =
349 L"%s cannot be blank. To ignore validations for %s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800350 return;
351 case XFA_IDS_ValidateNullError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700352 wsString = L"%s cannot be blank.";
Lei Zhang94293682016-01-27 18:27:56 -0800353 return;
354 case XFA_IDS_ValidateWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700355 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800356 L"The value you entered for %s is invalid. To ignore validations for "
dsinclair0e0a86a2016-06-01 18:56:51 -0700357 L"%s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800358 return;
359 case XFA_IDS_ValidateError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700360 wsString = L"The value you entered for %s is invalid.";
Lei Zhang94293682016-01-27 18:27:56 -0800361 return;
362 }
363}
364
Lei Zhang94293682016-01-27 18:27:56 -0800365IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
thestig1cd352e2016-06-07 17:53:06 -0700366 CXFA_FWLAdapterTimerMgr* pAdapter = nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800367 CPDFDoc_Environment* pEnv = m_pEnvList.GetAt(0);
368 if (pEnv)
369 pAdapter = new CXFA_FWLAdapterTimerMgr(pEnv);
370 return pAdapter;
371}