blob: 569e3141866eabc87b02aec76e1163ea055849ff [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"
dsinclairf3736bc2016-10-13 12:07:42 -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
dsinclairf3736bc2016-10-13 12:07:42 -070019CPDFXFA_App::CPDFXFA_App() : m_pIsolate(nullptr) {
20 m_bOwnsIsolate = FXJS_GetIsolate(&m_pIsolate);
Lei Zhang94293682016-01-27 18:27:56 -080021
dsinclairf3736bc2016-10-13 12:07:42 -070022 m_pXFAApp = pdfium::MakeUnique<CXFA_FFApp>(this);
23 m_pXFAApp->SetDefaultFontMgr(pdfium::MakeUnique<CXFA_DefFontMgr>());
dsinclairb685e642016-10-13 10:57:01 -070024}
25
26CPDFXFA_App::~CPDFXFA_App() {
dsinclairf3736bc2016-10-13 12:07:42 -070027 if (m_bOwnsIsolate)
28 m_pIsolate->Dispose();
dsinclairb685e642016-10-13 10:57:01 -070029}
Lei Zhang94293682016-01-27 18:27:56 -080030
Lei Zhang94293682016-01-27 18:27:56 -080031void CPDFXFA_App::GetAppName(CFX_WideString& wsName) {
dsinclairf3736bc2016-10-13 12:07:42 -070032 if (m_pFormFillEnv)
33 wsName = m_pFormFillEnv->FFI_GetAppName();
Lei Zhang94293682016-01-27 18:27:56 -080034}
35
Lei Zhang94293682016-01-27 18:27:56 -080036void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) {
dsinclairf3736bc2016-10-13 12:07:42 -070037 if (m_pFormFillEnv)
38 wsLanguage = m_pFormFillEnv->GetLanguage();
Lei Zhang94293682016-01-27 18:27:56 -080039}
40
41void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) {
dsinclairf3736bc2016-10-13 12:07:42 -070042 if (m_pFormFillEnv) {
43 wsPlatform = m_pFormFillEnv->GetPlatform();
Lei Zhang94293682016-01-27 18:27:56 -080044 }
45}
46
tsepezc3255f52016-03-25 14:52:27 -070047void CPDFXFA_App::Beep(uint32_t dwType) {
dsinclairf3736bc2016-10-13 12:07:42 -070048 if (m_pFormFillEnv)
49 m_pFormFillEnv->JS_appBeep(dwType);
Lei Zhang94293682016-01-27 18:27:56 -080050}
51
tsepez3f80c862016-05-16 12:03:24 -070052int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage,
53 const CFX_WideString& wsTitle,
tsepezc3255f52016-03-25 14:52:27 -070054 uint32_t dwIconType,
55 uint32_t dwButtonType) {
dsinclairf3736bc2016-10-13 12:07:42 -070056 if (!m_pFormFillEnv)
Lei Zhang94293682016-01-27 18:27:56 -080057 return -1;
58
tsepezc3255f52016-03-25 14:52:27 -070059 uint32_t iconType = 0;
Lei Zhang94293682016-01-27 18:27:56 -080060 int iButtonType = 0;
61 switch (dwIconType) {
62 case XFA_MBICON_Error:
63 iconType |= 0;
64 break;
65 case XFA_MBICON_Warning:
66 iconType |= 1;
67 break;
68 case XFA_MBICON_Question:
69 iconType |= 2;
70 break;
71 case XFA_MBICON_Status:
72 iconType |= 3;
73 break;
74 }
75 switch (dwButtonType) {
76 case XFA_MB_OK:
77 iButtonType |= 0;
78 break;
79 case XFA_MB_OKCancel:
80 iButtonType |= 1;
81 break;
82 case XFA_MB_YesNo:
83 iButtonType |= 2;
84 break;
85 case XFA_MB_YesNoCancel:
86 iButtonType |= 3;
87 break;
88 }
dsinclairf3736bc2016-10-13 12:07:42 -070089 int32_t iRet = m_pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
90 iButtonType, iconType);
Lei Zhang94293682016-01-27 18:27:56 -080091 switch (iRet) {
92 case 1:
93 return XFA_IDOK;
94 case 2:
95 return XFA_IDCancel;
96 case 3:
97 return XFA_IDNo;
98 case 4:
99 return XFA_IDYes;
100 }
101 return XFA_IDYes;
102}
103
tsepez3f80c862016-05-16 12:03:24 -0700104CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion,
105 const CFX_WideString& wsTitle,
106 const CFX_WideString& wsDefaultAnswer,
107 FX_BOOL bMark) {
108 CFX_WideString wsAnswer;
dsinclairf3736bc2016-10-13 12:07:42 -0700109 if (!m_pFormFillEnv)
110 return wsAnswer;
111
112 int nLength = 2048;
113 char* pBuff = new char[nLength];
114 nLength = m_pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
dsinclair8779fa82016-10-12 12:05:44 -0700115 wsDefaultAnswer.c_str(), nullptr,
116 bMark, pBuff, nLength);
dsinclairf3736bc2016-10-13 12:07:42 -0700117 if (nLength > 0) {
118 nLength = nLength > 2046 ? 2046 : nLength;
119 pBuff[nLength] = 0;
120 pBuff[nLength + 1] = 0;
121 wsAnswer = CFX_WideString::FromUTF16LE(
122 reinterpret_cast<const unsigned short*>(pBuff),
123 nLength / sizeof(unsigned short));
Lei Zhang94293682016-01-27 18:27:56 -0800124 }
dsinclairf3736bc2016-10-13 12:07:42 -0700125 delete[] pBuff;
tsepez3f80c862016-05-16 12:03:24 -0700126 return wsAnswer;
Lei Zhang94293682016-01-27 18:27:56 -0800127}
128
tsepez3f80c862016-05-16 12:03:24 -0700129IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) {
dsinclairf3736bc2016-10-13 12:07:42 -0700130 return m_pFormFillEnv ? m_pFormFillEnv->DownloadFromURL(wsURL.c_str())
131 : nullptr;
Lei Zhang94293682016-01-27 18:27:56 -0800132}
133
tsepez3f80c862016-05-16 12:03:24 -0700134FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL,
135 const CFX_WideString& wsData,
136 const CFX_WideString& wsContentType,
137 const CFX_WideString& wsEncode,
138 const CFX_WideString& wsHeader,
Lei Zhang94293682016-01-27 18:27:56 -0800139 CFX_WideString& wsResponse) {
dsinclairf3736bc2016-10-13 12:07:42 -0700140 if (!m_pFormFillEnv)
tsepez3f80c862016-05-16 12:03:24 -0700141 return FALSE;
142
dsinclairf3736bc2016-10-13 12:07:42 -0700143 wsResponse = m_pFormFillEnv->PostRequestURL(
144 wsURL.c_str(), wsData.c_str(), wsContentType.c_str(), wsEncode.c_str(),
145 wsHeader.c_str());
tsepez3f80c862016-05-16 12:03:24 -0700146 return TRUE;
Lei Zhang94293682016-01-27 18:27:56 -0800147}
148
tsepez3f80c862016-05-16 12:03:24 -0700149FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL,
150 const CFX_WideString& wsData,
151 const CFX_WideString& wsEncode) {
dsinclairf3736bc2016-10-13 12:07:42 -0700152 return m_pFormFillEnv &&
153 m_pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(),
154 wsEncode.c_str());
Lei Zhang94293682016-01-27 18:27:56 -0800155}
156
157void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
158 switch (iStringID) {
159 case XFA_IDS_ValidateFailed:
dsinclair0e0a86a2016-06-01 18:56:51 -0700160 wsString = L"%s validation failed";
Lei Zhang94293682016-01-27 18:27:56 -0800161 return;
162 case XFA_IDS_CalcOverride:
163 wsString = L"Calculate Override";
164 return;
165 case XFA_IDS_ModifyField:
166 wsString = L"Are you sure you want to modify this field?";
167 return;
168 case XFA_IDS_NotModifyField:
169 wsString = L"You are not allowed to modify this field.";
170 return;
171 case XFA_IDS_AppName:
dsinclair0e0a86a2016-06-01 18:56:51 -0700172 wsString = L"pdfium";
Lei Zhang94293682016-01-27 18:27:56 -0800173 return;
174 case XFA_IDS_Unable_TO_SET:
175 wsString = L"Unable to set ";
176 return;
Lei Zhang94293682016-01-27 18:27:56 -0800177 case XFA_IDS_INVAlID_PROP_SET:
dsinclair0e0a86a2016-06-01 18:56:51 -0700178 wsString = L"Invalid property set operation.";
Lei Zhang94293682016-01-27 18:27:56 -0800179 return;
180 case XFA_IDS_NOT_DEFAUL_VALUE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700181 wsString = L" doesn't have a default property.";
Lei Zhang94293682016-01-27 18:27:56 -0800182 return;
183 case XFA_IDS_UNABLE_SET_LANGUAGE:
dsinclair0e0a86a2016-06-01 18:56:51 -0700184 wsString = L"Unable to set language value.";
Lei Zhang94293682016-01-27 18:27:56 -0800185 return;
186 case XFA_IDS_UNABLE_SET_NUMPAGES:
dsinclair0e0a86a2016-06-01 18:56:51 -0700187 wsString = L"Unable to set numPages value.";
Lei Zhang94293682016-01-27 18:27:56 -0800188 return;
189 case XFA_IDS_UNABLE_SET_PLATFORM:
dsinclair0e0a86a2016-06-01 18:56:51 -0700190 wsString = L"Unable to set platform value.";
Lei Zhang94293682016-01-27 18:27:56 -0800191 return;
192 case XFA_IDS_UNABLE_SET_VARIATION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700193 wsString = L"Unable to set variation value.";
Lei Zhang94293682016-01-27 18:27:56 -0800194 return;
195 case XFA_IDS_UNABLE_SET_VERSION:
dsinclair0e0a86a2016-06-01 18:56:51 -0700196 wsString = L"Unable to set version value.";
Lei Zhang94293682016-01-27 18:27:56 -0800197 return;
198 case XFA_IDS_UNABLE_SET_READY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700199 wsString = L"Unable to set ready value.";
Lei Zhang94293682016-01-27 18:27:56 -0800200 return;
201 case XFA_IDS_COMPILER_ERROR:
dsinclair0e0a86a2016-06-01 18:56:51 -0700202 wsString = L"Compiler error.";
Lei Zhang94293682016-01-27 18:27:56 -0800203 return;
204 case XFA_IDS_DIVIDE_ZERO:
dsinclair0e0a86a2016-06-01 18:56:51 -0700205 wsString = L"Divide by zero.";
Lei Zhang94293682016-01-27 18:27:56 -0800206 return;
207 case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
208 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700209 L"An attempt was made to reference property '%s' of a non-object in "
210 L"SOM expression %s.";
Lei Zhang94293682016-01-27 18:27:56 -0800211 return;
212 case XFA_IDS_INDEX_OUT_OF_BOUNDS:
dsinclair0e0a86a2016-06-01 18:56:51 -0700213 wsString = L"Index value is out of bounds.";
Lei Zhang94293682016-01-27 18:27:56 -0800214 return;
215 case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
dsinclair0e0a86a2016-06-01 18:56:51 -0700216 wsString = L"Incorrect number of parameters calling method '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800217 return;
218 case XFA_IDS_ARGUMENT_MISMATCH:
dsinclair0e0a86a2016-06-01 18:56:51 -0700219 wsString = L"Argument mismatch in property or function argument.";
Lei Zhang94293682016-01-27 18:27:56 -0800220 return;
221 case XFA_IDS_NOT_HAVE_PROPERTY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700222 wsString = L"'%s' doesn't have property '%s'.";
Lei Zhang94293682016-01-27 18:27:56 -0800223 return;
224 case XFA_IDS_VIOLATE_BOUNDARY:
225 wsString =
dsinclair0e0a86a2016-06-01 18:56:51 -0700226 L"The element [%s] has violated its allowable number of occurrences.";
Lei Zhang94293682016-01-27 18:27:56 -0800227 return;
228 case XFA_IDS_SERVER_DENY:
dsinclair0e0a86a2016-06-01 18:56:51 -0700229 wsString = L"Server does not permit.";
Lei Zhang94293682016-01-27 18:27:56 -0800230 return;
231 case XFA_IDS_ValidateLimit:
dsinclair0e0a86a2016-06-01 18:56:51 -0700232 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800233 L"Message limit exceeded. Remaining %d validation errors not "
dsinclair0e0a86a2016-06-01 18:56:51 -0700234 L"reported.";
Lei Zhang94293682016-01-27 18:27:56 -0800235 return;
236 case XFA_IDS_ValidateNullWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700237 wsString =
238 L"%s cannot be blank. To ignore validations for %s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800239 return;
240 case XFA_IDS_ValidateNullError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700241 wsString = L"%s cannot be blank.";
Lei Zhang94293682016-01-27 18:27:56 -0800242 return;
243 case XFA_IDS_ValidateWarning:
dsinclair0e0a86a2016-06-01 18:56:51 -0700244 wsString =
Lei Zhang94293682016-01-27 18:27:56 -0800245 L"The value you entered for %s is invalid. To ignore validations for "
dsinclair0e0a86a2016-06-01 18:56:51 -0700246 L"%s, click Ignore.";
Lei Zhang94293682016-01-27 18:27:56 -0800247 return;
248 case XFA_IDS_ValidateError:
dsinclair0e0a86a2016-06-01 18:56:51 -0700249 wsString = L"The value you entered for %s is invalid.";
Lei Zhang94293682016-01-27 18:27:56 -0800250 return;
251 }
252}
253
Lei Zhang94293682016-01-27 18:27:56 -0800254IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
thestig1cd352e2016-06-07 17:53:06 -0700255 CXFA_FWLAdapterTimerMgr* pAdapter = nullptr;
dsinclairf3736bc2016-10-13 12:07:42 -0700256 if (m_pFormFillEnv)
257 pAdapter = new CXFA_FWLAdapterTimerMgr(m_pFormFillEnv);
Lei Zhang94293682016-01-27 18:27:56 -0800258 return pAdapter;
259}