summaryrefslogtreecommitdiff
path: root/fpdfsdk/fpdfxfa/cpdfxfa_app.cpp
blob: 61694d0a289a30dd94f9c424554261cdbc3ea407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "fpdfsdk/fpdfxfa/cpdfxfa_app.h"

#include <memory>

#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
#include "fpdfsdk/fsdk_define.h"
#include "third_party/base/ptr_util.h"
#include "xfa/fxbarcode/BC_Library.h"
#include "xfa/fxfa/xfa_ffapp.h"
#include "xfa/fxfa/xfa_fontmgr.h"

namespace {

CPDFXFA_App* g_pApp = nullptr;

}  // namespace

CPDFXFA_App* CPDFXFA_App::GetInstance() {
  if (!g_pApp) {
    g_pApp = new CPDFXFA_App();
  }
  return g_pApp;
}

void CPDFXFA_App::ReleaseInstance() {
  delete g_pApp;
  g_pApp = nullptr;
}

CPDFXFA_App::CPDFXFA_App()
    : m_bJavaScriptInitialized(FALSE), m_pIsolate(nullptr) {
  m_pFormFillEnvList.RemoveAll();
}

CPDFXFA_App::~CPDFXFA_App() {
  FXJSE_Runtime_Release(m_pIsolate);
  m_pIsolate = nullptr;

  FXJSE_Finalize();
  BC_Library_Destory();
}

FX_BOOL CPDFXFA_App::Initialize(v8::Isolate* pIsolate) {
  BC_Library_Init();
  FXJSE_Initialize();

  m_pIsolate = pIsolate ? pIsolate : FXJSE_Runtime_Create_Own();
  if (!m_pIsolate)
    return FALSE;

  m_pXFAApp = pdfium::MakeUnique<CXFA_FFApp>(this);
  m_pXFAApp->SetDefaultFontMgr(
      std::unique_ptr<CXFA_DefFontMgr>(new CXFA_DefFontMgr));

  return TRUE;
}

FX_BOOL CPDFXFA_App::AddFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
  if (!pFormFillEnv)
    return FALSE;

  m_pFormFillEnvList.Add(pFormFillEnv);
  return TRUE;
}

FX_BOOL CPDFXFA_App::RemoveFormFillEnv(
    CPDFSDK_FormFillEnvironment* pFormFillEnv) {
  if (!pFormFillEnv)
    return FALSE;

  int nFind = m_pFormFillEnvList.Find(pFormFillEnv);
  if (nFind != -1) {
    m_pFormFillEnvList.RemoveAt(nFind);
    return TRUE;
  }

  return FALSE;
}

void CPDFXFA_App::GetAppName(CFX_WideString& wsName) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (pFormFillEnv)
    wsName = pFormFillEnv->FFI_GetAppName();
}

void CPDFXFA_App::GetLanguage(CFX_WideString& wsLanguage) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (pFormFillEnv)
    wsLanguage = pFormFillEnv->GetLanguage();
}

void CPDFXFA_App::GetPlatform(CFX_WideString& wsPlatform) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (pFormFillEnv) {
    wsPlatform = pFormFillEnv->GetPlatform();
  }
}

void CPDFXFA_App::Beep(uint32_t dwType) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (pFormFillEnv)
    pFormFillEnv->JS_appBeep(dwType);
}

int32_t CPDFXFA_App::MsgBox(const CFX_WideString& wsMessage,
                            const CFX_WideString& wsTitle,
                            uint32_t dwIconType,
                            uint32_t dwButtonType) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (!pFormFillEnv)
    return -1;

  uint32_t iconType = 0;
  int iButtonType = 0;
  switch (dwIconType) {
    case XFA_MBICON_Error:
      iconType |= 0;
      break;
    case XFA_MBICON_Warning:
      iconType |= 1;
      break;
    case XFA_MBICON_Question:
      iconType |= 2;
      break;
    case XFA_MBICON_Status:
      iconType |= 3;
      break;
  }
  switch (dwButtonType) {
    case XFA_MB_OK:
      iButtonType |= 0;
      break;
    case XFA_MB_OKCancel:
      iButtonType |= 1;
      break;
    case XFA_MB_YesNo:
      iButtonType |= 2;
      break;
    case XFA_MB_YesNoCancel:
      iButtonType |= 3;
      break;
  }
  int32_t iRet = pFormFillEnv->JS_appAlert(wsMessage.c_str(), wsTitle.c_str(),
                                           iButtonType, iconType);
  switch (iRet) {
    case 1:
      return XFA_IDOK;
    case 2:
      return XFA_IDCancel;
    case 3:
      return XFA_IDNo;
    case 4:
      return XFA_IDYes;
  }
  return XFA_IDYes;
}

CFX_WideString CPDFXFA_App::Response(const CFX_WideString& wsQuestion,
                                     const CFX_WideString& wsTitle,
                                     const CFX_WideString& wsDefaultAnswer,
                                     FX_BOOL bMark) {
  CFX_WideString wsAnswer;
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (pFormFillEnv) {
    int nLength = 2048;
    char* pBuff = new char[nLength];
    nLength = pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
                                           wsDefaultAnswer.c_str(), nullptr,
                                           bMark, pBuff, nLength);
    if (nLength > 0) {
      nLength = nLength > 2046 ? 2046 : nLength;
      pBuff[nLength] = 0;
      pBuff[nLength + 1] = 0;
      wsAnswer = CFX_WideString::FromUTF16LE(
          reinterpret_cast<const unsigned short*>(pBuff),
          nLength / sizeof(unsigned short));
    }
    delete[] pBuff;
  }
  return wsAnswer;
}

IFX_FileRead* CPDFXFA_App::DownloadURL(const CFX_WideString& wsURL) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  return pFormFillEnv ? pFormFillEnv->DownloadFromURL(wsURL.c_str()) : nullptr;
}

FX_BOOL CPDFXFA_App::PostRequestURL(const CFX_WideString& wsURL,
                                    const CFX_WideString& wsData,
                                    const CFX_WideString& wsContentType,
                                    const CFX_WideString& wsEncode,
                                    const CFX_WideString& wsHeader,
                                    CFX_WideString& wsResponse) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (!pFormFillEnv)
    return FALSE;

  wsResponse = pFormFillEnv->PostRequestURL(wsURL.c_str(), wsData.c_str(),
                                            wsContentType.c_str(),
                                            wsEncode.c_str(), wsHeader.c_str());
  return TRUE;
}

FX_BOOL CPDFXFA_App::PutRequestURL(const CFX_WideString& wsURL,
                                   const CFX_WideString& wsData,
                                   const CFX_WideString& wsEncode) {
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  return pFormFillEnv &&
         pFormFillEnv->PutRequestURL(wsURL.c_str(), wsData.c_str(),
                                     wsEncode.c_str());
}

void CPDFXFA_App::LoadString(int32_t iStringID, CFX_WideString& wsString) {
  switch (iStringID) {
    case XFA_IDS_ValidateFailed:
      wsString = L"%s validation failed";
      return;
    case XFA_IDS_CalcOverride:
      wsString = L"Calculate Override";
      return;
    case XFA_IDS_ModifyField:
      wsString = L"Are you sure you want to modify this field?";
      return;
    case XFA_IDS_NotModifyField:
      wsString = L"You are not allowed to modify this field.";
      return;
    case XFA_IDS_AppName:
      wsString = L"pdfium";
      return;
    case XFA_IDS_Unable_TO_SET:
      wsString = L"Unable to set ";
      return;
    case XFA_IDS_INVAlID_PROP_SET:
      wsString = L"Invalid property set operation.";
      return;
    case XFA_IDS_NOT_DEFAUL_VALUE:
      wsString = L" doesn't have a default property.";
      return;
    case XFA_IDS_UNABLE_SET_LANGUAGE:
      wsString = L"Unable to set language value.";
      return;
    case XFA_IDS_UNABLE_SET_NUMPAGES:
      wsString = L"Unable to set numPages value.";
      return;
    case XFA_IDS_UNABLE_SET_PLATFORM:
      wsString = L"Unable to set platform value.";
      return;
    case XFA_IDS_UNABLE_SET_VARIATION:
      wsString = L"Unable to set variation value.";
      return;
    case XFA_IDS_UNABLE_SET_VERSION:
      wsString = L"Unable to set version value.";
      return;
    case XFA_IDS_UNABLE_SET_READY:
      wsString = L"Unable to set ready value.";
      return;
    case XFA_IDS_COMPILER_ERROR:
      wsString = L"Compiler error.";
      return;
    case XFA_IDS_DIVIDE_ZERO:
      wsString = L"Divide by zero.";
      return;
    case XFA_IDS_ACCESS_PROPERTY_IN_NOT_OBJECT:
      wsString =
          L"An attempt was made to reference property '%s' of a non-object in "
          L"SOM expression %s.";
      return;
    case XFA_IDS_INDEX_OUT_OF_BOUNDS:
      wsString = L"Index value is out of bounds.";
      return;
    case XFA_IDS_INCORRECT_NUMBER_OF_METHOD:
      wsString = L"Incorrect number of parameters calling method '%s'.";
      return;
    case XFA_IDS_ARGUMENT_MISMATCH:
      wsString = L"Argument mismatch in property or function argument.";
      return;
    case XFA_IDS_NOT_HAVE_PROPERTY:
      wsString = L"'%s' doesn't have property '%s'.";
      return;
    case XFA_IDS_VIOLATE_BOUNDARY:
      wsString =
          L"The element [%s] has violated its allowable number of occurrences.";
      return;
    case XFA_IDS_SERVER_DENY:
      wsString = L"Server does not permit.";
      return;
    case XFA_IDS_ValidateLimit:
      wsString =
          L"Message limit exceeded. Remaining %d validation errors not "
          L"reported.";
      return;
    case XFA_IDS_ValidateNullWarning:
      wsString =
          L"%s cannot be blank. To ignore validations for %s, click Ignore.";
      return;
    case XFA_IDS_ValidateNullError:
      wsString = L"%s cannot be blank.";
      return;
    case XFA_IDS_ValidateWarning:
      wsString =
          L"The value you entered for %s is invalid. To ignore validations for "
          L"%s, click Ignore.";
      return;
    case XFA_IDS_ValidateError:
      wsString = L"The value you entered for %s is invalid.";
      return;
  }
}

IFWL_AdapterTimerMgr* CPDFXFA_App::GetTimerMgr() {
  CXFA_FWLAdapterTimerMgr* pAdapter = nullptr;
  CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pFormFillEnvList.GetAt(0);
  if (pFormFillEnv)
    pAdapter = new CXFA_FWLAdapterTimerMgr(pFormFillEnv);
  return pAdapter;
}