blob: b543d52315be010853c17a0f6dbd4f9dbd83349d [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
dsinclair4d29e782016-10-04 14:02:47 -07007#include "fpdfsdk/fpdfxfa/cpdfxfa_app.h"
weili625ad662016-06-15 11:21:33 -07008
thestigd4c34f22016-09-28 17:04:51 -07009#include <memory>
10
dsinclair735606d2016-10-05 15:47:02 -070011#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair4d29e782016-10-04 14:02:47 -070012#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
dsinclair114e46a2016-09-29 17:18:21 -070013#include "fpdfsdk/fsdk_define.h"
dsinclairabefb792016-10-13 10:48:22 -070014#include "fxjs/fxjs_v8.h"
tsepez36eb4bd2016-10-03 15:24:27 -070015#include "third_party/base/ptr_util.h"
dsinclair5b493092016-09-29 20:20:24 -070016#include "xfa/fxfa/xfa_ffapp.h"
17#include "xfa/fxfa/xfa_fontmgr.h"
Lei Zhang94293682016-01-27 18:27:56 -080018
dsinclairabefb792016-10-13 10:48:22 -070019CPDFXFA_App::CPDFXFA_App() : m_pIsolate(nullptr) {
20 // TODO(dsinclair): How do we 'release' the isolate?
21 FXJS_GetIsolate(&m_pIsolate);
Lei Zhang94293682016-01-27 18:27:56 -080022
tsepez36eb4bd2016-10-03 15:24:27 -070023 m_pXFAApp = pdfium::MakeUnique<CXFA_FFApp>(this);
dsinclairabefb792016-10-13 10:48:22 -070024 m_pXFAApp->SetDefaultFontMgr(pdfium::MakeUnique<CXFA_DefFontMgr>());
Lei Zhang94293682016-01-27 18:27:56 -080025}
26
dsinclairabefb792016-10-13 10:48:22 -070027CPDFXFA_App::~CPDFXFA_App() {}
Lei Zhang94293682016-01-27 18:27:56 -080028
Lei Zhang94293682016-01-27 18:27:56 -080029void CPDFXFA_App::GetAppName(CFX_WideString& wsName) {
dsinclairabefb792016-10-13 10:48:22 -070030 if (m_pFormFillEnv)
31 wsName = m_pFormFillEnv->FFI_GetAppName();
Lei Zhang94293682016-01-27 18:27:56 -080032}
33
Lei Zhang94293682016-01-27 18:27:56 -080034void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) {
dsinclairabefb792016-10-13 10:48:22 -070035 if (m_pFormFillEnv)
36 wsLanguage = m_pFormFillEnv->GetLanguage();
Lei Zhang94293682016-01-27 18:27:56 -080037}
38
39void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) {
dsinclairabefb792016-10-13 10:48:22 -070040 if (m_pFormFillEnv) {
41 wsPlatform = m_pFormFillEnv->GetPlatform();
Lei Zhang94293682016-01-27 18:27:56 -080042 }
43}
44
tsepezc3255f52016-03-25 14:52:27 -070045void CPDFXFA_App::Beep(uint32_t dwType) {
dsinclairabefb792016-10-13 10:48:22 -070046 if (m_pFormFillEnv)
47 m_pFormFillEnv->JS_appBeep(dwType);
Lei Zhang94293682016-01-27 18:27:56 -080048}
49
tsepez3f80c862016-05-16 12:03:24 -070050int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage,
51 const CFX_WideString& wsTitle,
tsepezc3255f52016-03-25 14:52:27 -070052 uint32_t dwIconType,
53 uint32_t dwButtonType) {
dsinclairabefb792016-10-13 10:48:22 -070054 if (!m_pFormFillEnv)
Lei Zhang94293682016-01-27 18:27:56 -080055 return -1;
56
tsepezc3255f52016-03-25 14:52:27 -070057 uint32_t iconType = 0;
Lei Zhang94293682016-01-27 18:27:56 -080058 int iButtonType = 0;
59 switch (dwIconType) {
60 case XFA_MBICON_Error:
61 iconType |= 0;
62 break;
63 case XFA_MBICON_Warning:
64 iconType |= 1;
65 break;
66 case XFA_MBICON_Question:
67 iconType |= 2;
68 break;
69 case XFA_MBICON_Status:
70 iconType |= 3;
71 break;
72 }
73 switch (dwButtonType) {
74 case XFA_MB_OK:
75 iButtonType |= 0;
76 break;
77 case XFA_MB_OKCancel:
78 iButtonType |= 1;
79 break;
80 case XFA_MB_YesNo:
81 iButtonType |= 2;
82 break;
83 case XFA_MB_YesNoCancel:
84 iButtonType |= 3;
85 break;
86 }
dsinclairabefb792016-10-13 10:48:22 -070087 int32_t iRet = m_pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
88 iButtonType, iconType);
Lei Zhang94293682016-01-27 18:27:56 -080089 switch (iRet) {
90 case 1:
91 return XFA_IDOK;
92 case 2:
93 return XFA_IDCancel;
94 case 3:
95 return XFA_IDNo;
96 case 4:
97 return XFA_IDYes;
98 }
99 return XFA_IDYes;
100}
101
tsepez3f80c862016-05-16 12:03:24 -0700102CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion,
103 const CFX_WideString& wsTitle,
104 const CFX_WideString& wsDefaultAnswer,
105 FX_BOOL bMark) {
106 CFX_WideString wsAnswer;
dsinclairabefb792016-10-13 10:48:22 -0700107 if (!m_pFormFillEnv)
108 return wsAnswer;
109
110 int nLength = 2048;
111 char* pBuff = new char[nLength];
112 nLength = m_pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
dsinclair8779fa82016-10-12 12:05:44 -0700113 wsDefaultAnswer.c_str(), nullptr,
114 bMark, pBuff, nLength);
dsinclairabefb792016-10-13 10:48:22 -0700115 if (nLength > 0) {
116 nLength = nLength > 2046 ? 2046 : nLength;
117 pBuff[nLength] = 0;
118 pBuff[nLength + 1] = 0;
119 wsAnswer = CFX_WideString::FromUTF16LE(
120 reinterpret_cast<const unsigned short*>(pBuff),
121 nLength / sizeof(unsigned short));
Lei Zhang94293682016-01-27 18:27:56 -0800122 }
dsinclairabefb792016-10-13 10:48:22 -0700123 delete[] pBuff;
tsepez3f80c862016-05-16 12:03:24 -0700124 return wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800125}
126
tsepez3f80c862016-05-16 12:03:24 -0700127IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) {
dsinclairabefb792016-10-13 10:48:22 -0700128 return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL.c_str())
129 : nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800130}
131
tsepez3f80c862016-05-16 12:03:24 -0700132FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL,
133 const CFX_WideString& wsData,
134 const CFX_WideString& wsContentType,
135 const CFX_WideString& wsEncode,
136 const CFX_WideString& wsHeader,
Lei Zhang94293682016-01-27 18:27:56 -0800137 CFX_WideString& wsResponse) {
dsinclairabefb792016-10-13 10:48:22 -0700138 if (!m_pFormFillEnv)
tsepez3f80c862016-05-16 12:03:24 -0700139 return FALSE;
140
dsinclairabefb792016-10-13 10:48:22 -0700141 wsResponse = m_pFormFillEnv->PostRequestURL(
142 wsURL.c_str(), wsData.c_str(), wsContentType.c_str(), wsEncode.c_str(),
143 wsHeader.c_str());
tsepez3f80c862016-05-16 12:03:24 -0700144 return TRUE;
Lei Zhang94293682016-01-27 18:27:56 -0800145}
146
tsepez3f80c862016-05-16 12:03:24 -0700147FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL,
148 const CFX_WideString& wsData,
149 const CFX_WideString& wsEncode) {
dsinclairabefb792016-10-13 10:48:22 -0700150 return m_pFormFillEnv &&
151 m_pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(),
152 wsEncode.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800153}
154
155void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
156 switch (iStringID) {
157 case XFA_IDS_ValidateFailed:
dsinclair0e0a86a2016-06-01 18:56:51 -0700158 wsString = L"%s validation failed";
Lei Zhang94293682016-01-27 18:27:56 -0800159 return;
160 case XFA_IDS_CalcOverride:
161 wsString = L"Calculate Override";
162 return;
163 case XFA_IDS_ModifyField:
164 wsString = L"Are you sure you want to modify this field?";
165 return;
166 case XFA_IDS_NotModifyField:
167 wsString = L"You are not allowed to modify this field.";
168 return;
169 case XFA_IDS_AppName:
dsinclair0e0a86a2016-06-01 18:56:51 -0700170 wsString = L"pdfium";
Lei Zhang94293682016-01-27 18:27:56 -0800171 return;
172 case XFA_IDS_Unable_TO_SET:
173 wsString = L"Unable to set ";
174 return;
Lei Zhang94293682016-01-27 18:27:56 -0800175 case XFA_IDS_INVAlID_PROP_SET:
dsinclair0e0a86a2016-06-01 18:56:51 -0700176 wsString = L"Invalid property set operation.";
Lei Zhang94293682016-01-27 18:27:56 -0800177 return;
178 case XFA_IDS_NOT_DEFAUL_VALUE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700179 wsString = L" doesn't have a default property.";
Lei Zhang94293682016-01-27 18:27:56 -0800180 return;
181 case XFA_IDS_UNABLE_SET_LANGUAGE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700182 wsString = L"Unable to set language value.";
Lei Zhang94293682016-01-27 18:27:56 -0800183 return;
184 case XFA_IDS_UNABLE_SET_NUMPAGES:
dsinclair0e0a86a2016-06-01 18:56:51 -0700185 wsString = L"Unable to set numPages value.";
Lei Zhang94293682016-01-27 18:27:56 -0800186 return;
187 case XFA_IDS_UNABLE_SET_PLATFORM:
dsinclair0e0a86a2016-06-01 18:56:51 -0700188 wsString = L"Unable to set platform value.";
Lei Zhang94293682016-01-27 18:27:56 -0800189 return;
190 case XFA_IDS_UNABLE_SET_VARIATION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700191 wsString = L"Unable to set variation value.";
Lei Zhang94293682016-01-27 18:27:56 -0800192 return;
193 case XFA_IDS_UNABLE_SET_VERSION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700194 wsString = L"Unable to set version value.";
Lei Zhang94293682016-01-27 18:27:56 -0800195 return;
196 case XFA_IDS_UNABLE_SET_READY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700197 wsString = L"Unable to set ready value.";
Lei Zhang94293682016-01-27 18:27:56 -0800198 return;
199 case XFA_IDS_COMPILER_ERROR:
dsinclair0e0a86a2016-06-01 18:56:51 -0700200 wsString = L"Compiler error.";
Lei Zhang94293682016-01-27 18:27:56 -0800201 return;
202 case XFA_IDS_DIVIDE_ZERO:
dsinclair0e0a86a2016-06-01 18:56:51 -0700203 wsString = L"Divide by zero.";
Lei Zhang94293682016-01-27 18:27:56 -0800204 return;
205 case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
206 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700207 L"An attempt was made to reference property '%s' of a non-object in "
208 L"SOM expression %s.";
Lei Zhang94293682016-01-27 18:27:56 -0800209 return;
210 case XFA_IDS_INDEX_OUT_OF_BOUNDS:
dsinclair0e0a86a2016-06-01 18:56:51 -0700211 wsString = L"Index value is out of bounds.";
Lei Zhang94293682016-01-27 18:27:56 -0800212 return;
213 case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
dsinclair0e0a86a2016-06-01 18:56:51 -0700214 wsString = L"Incorrect number of parameters calling method '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800215 return;
216 case XFA_IDS_ARGUMENT_MISMATCH:
dsinclair0e0a86a2016-06-01 18:56:51 -0700217 wsString = L"Argument mismatch in property or function argument.";
Lei Zhang94293682016-01-27 18:27:56 -0800218 return;
219 case XFA_IDS_NOT_HAVE_PROPERTY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700220 wsString = L"'%s' doesn't have property '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800221 return;
222 case XFA_IDS_VIOLATE_BOUNDARY:
223 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700224 L"The element [%s] has violated its allowable number of occurrences.";
Lei Zhang94293682016-01-27 18:27:56 -0800225 return;
226 case XFA_IDS_SERVER_DENY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700227 wsString = L"Server does not permit.";
Lei Zhang94293682016-01-27 18:27:56 -0800228 return;
229 case XFA_IDS_ValidateLimit:
dsinclair0e0a86a2016-06-01 18:56:51 -0700230 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800231 L"Message limit exceeded. Remaining %d validation errors not "
dsinclair0e0a86a2016-06-01 18:56:51 -0700232 L"reported.";
Lei Zhang94293682016-01-27 18:27:56 -0800233 return;
234 case XFA_IDS_ValidateNullWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700235 wsString =
236 L"%s cannot be blank. To ignore validations for %s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800237 return;
238 case XFA_IDS_ValidateNullError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700239 wsString = L"%s cannot be blank.";
Lei Zhang94293682016-01-27 18:27:56 -0800240 return;
241 case XFA_IDS_ValidateWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700242 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800243 L"The value you entered for %s is invalid. To ignore validations for "
dsinclair0e0a86a2016-06-01 18:56:51 -0700244 L"%s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800245 return;
246 case XFA_IDS_ValidateError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700247 wsString = L"The value you entered for %s is invalid.";
Lei Zhang94293682016-01-27 18:27:56 -0800248 return;
249 }
250}
251
Lei Zhang94293682016-01-27 18:27:56 -0800252IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
thestig1cd352e2016-06-07 17:53:06 -0700253 CXFA_FWLAdapterTimerMgr* pAdapter = nullptr;
dsinclairabefb792016-10-13 10:48:22 -0700254 if (m_pFormFillEnv)
255 pAdapter = new CXFA_FWLAdapterTimerMgr(m_pFormFillEnv);
Lei Zhang94293682016-01-27 18:27:56 -0800256 return pAdapter;
257}