• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
7 #include "fxjs/cjs_app.h"
8 
9 #include <utility>
10 
11 #include "fpdfsdk/cpdfsdk_interactiveform.h"
12 #include "fxjs/cjs_document.h"
13 #include "fxjs/cjs_timerobj.h"
14 #include "fxjs/global_timer.h"
15 #include "fxjs/ijs_event_context.h"
16 #include "fxjs/js_resources.h"
17 #include "third_party/base/ptr_util.h"
18 #include "third_party/base/stl_util.h"
19 
20 #define JS_STR_VIEWERTYPE L"pdfium"
21 #define JS_STR_VIEWERVARIATION L"Full"
22 #define JS_STR_PLATFORM L"WIN"
23 #define JS_STR_LANGUAGE L"ENU"
24 #define JS_NUM_VIEWERVERSION 8
25 #define JS_NUM_VIEWERVERSION_XFA 11
26 #define JS_NUM_FORMSVERSION 7
27 
28 const JSPropertySpec CJS_App::PropertySpecs[] = {
29     {"activeDocs", get_active_docs_static, set_active_docs_static},
30     {"calculate", get_calculate_static, set_calculate_static},
31     {"formsVersion", get_forms_version_static, set_forms_version_static},
32     {"fs", get_fs_static, set_fs_static},
33     {"fullscreen", get_fullscreen_static, set_fullscreen_static},
34     {"language", get_language_static, set_language_static},
35     {"media", get_media_static, set_media_static},
36     {"platform", get_platform_static, set_platform_static},
37     {"runtimeHighlight", get_runtime_highlight_static,
38      set_runtime_highlight_static},
39     {"viewerType", get_viewer_type_static, set_viewer_type_static},
40     {"viewerVariation", get_viewer_variation_static,
41      set_viewer_variation_static},
42     {"viewerVersion", get_viewer_version_static, set_viewer_version_static}};
43 
44 const JSMethodSpec CJS_App::MethodSpecs[] = {
45     {"alert", alert_static},
46     {"beep", beep_static},
47     {"browseForDoc", browseForDoc_static},
48     {"clearInterval", clearInterval_static},
49     {"clearTimeOut", clearTimeOut_static},
50     {"execDialog", execDialog_static},
51     {"execMenuItem", execMenuItem_static},
52     {"findComponent", findComponent_static},
53     {"goBack", goBack_static},
54     {"goForward", goForward_static},
55     {"launchURL", launchURL_static},
56     {"mailMsg", mailMsg_static},
57     {"newFDF", newFDF_static},
58     {"newDoc", newDoc_static},
59     {"openDoc", openDoc_static},
60     {"openFDF", openFDF_static},
61     {"popUpMenuEx", popUpMenuEx_static},
62     {"popUpMenu", popUpMenu_static},
63     {"response", response_static},
64     {"setInterval", setInterval_static},
65     {"setTimeOut", setTimeOut_static}};
66 
67 int CJS_App::ObjDefnID = -1;
68 
69 const char CJS_App::kName[] = "app";
70 
71 // static
GetObjDefnID()72 int CJS_App::GetObjDefnID() {
73   return ObjDefnID;
74 }
75 
76 // static
DefineJSObjects(CFXJS_Engine * pEngine)77 void CJS_App::DefineJSObjects(CFXJS_Engine* pEngine) {
78   ObjDefnID = pEngine->DefineObj(CJS_App::kName, FXJSOBJTYPE_STATIC,
79                                  JSConstructor<CJS_App>, JSDestructor);
80   DefineProps(pEngine, ObjDefnID, PropertySpecs);
81   DefineMethods(pEngine, ObjDefnID, MethodSpecs);
82 }
83 
CJS_App(v8::Local<v8::Object> pObject,CJS_Runtime * pRuntime)84 CJS_App::CJS_App(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
85     : CJS_Object(pObject, pRuntime) {}
86 
87 CJS_App::~CJS_App() = default;
88 
get_active_docs(CJS_Runtime * pRuntime)89 CJS_Result CJS_App::get_active_docs(CJS_Runtime* pRuntime) {
90   v8::Local<v8::Object> pObj = pRuntime->GetThisObj();
91   auto pJSDocument = JSGetObject<CJS_Document>(pObj);
92   if (!pJSDocument)
93     return CJS_Result::Failure(JSMessage::kObjectTypeError);
94   v8::Local<v8::Array> aDocs = pRuntime->NewArray();
95   pRuntime->PutArrayElement(aDocs, 0, pJSDocument->ToV8Object());
96   if (pRuntime->GetArrayLength(aDocs) > 0)
97     return CJS_Result::Success(aDocs);
98 
99   return CJS_Result::Success(pRuntime->NewUndefined());
100 }
101 
set_active_docs(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)102 CJS_Result CJS_App::set_active_docs(CJS_Runtime* pRuntime,
103                                     v8::Local<v8::Value> vp) {
104   return CJS_Result::Failure(JSMessage::kNotSupportedError);
105 }
106 
get_calculate(CJS_Runtime * pRuntime)107 CJS_Result CJS_App::get_calculate(CJS_Runtime* pRuntime) {
108   return CJS_Result::Success(pRuntime->NewBoolean(m_bCalculate));
109 }
110 
set_calculate(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)111 CJS_Result CJS_App::set_calculate(CJS_Runtime* pRuntime,
112                                   v8::Local<v8::Value> vp) {
113   m_bCalculate = pRuntime->ToBoolean(vp);
114   pRuntime->GetFormFillEnv()->GetInteractiveForm()->EnableCalculate(
115       m_bCalculate);
116   return CJS_Result::Success();
117 }
118 
get_forms_version(CJS_Runtime * pRuntime)119 CJS_Result CJS_App::get_forms_version(CJS_Runtime* pRuntime) {
120   return CJS_Result::Success(pRuntime->NewNumber(JS_NUM_FORMSVERSION));
121 }
122 
set_forms_version(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)123 CJS_Result CJS_App::set_forms_version(CJS_Runtime* pRuntime,
124                                       v8::Local<v8::Value> vp) {
125   return CJS_Result::Failure(JSMessage::kNotSupportedError);
126 }
127 
get_viewer_type(CJS_Runtime * pRuntime)128 CJS_Result CJS_App::get_viewer_type(CJS_Runtime* pRuntime) {
129   return CJS_Result::Success(pRuntime->NewString(JS_STR_VIEWERTYPE));
130 }
131 
set_viewer_type(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)132 CJS_Result CJS_App::set_viewer_type(CJS_Runtime* pRuntime,
133                                     v8::Local<v8::Value> vp) {
134   return CJS_Result::Failure(JSMessage::kNotSupportedError);
135 }
136 
get_viewer_variation(CJS_Runtime * pRuntime)137 CJS_Result CJS_App::get_viewer_variation(CJS_Runtime* pRuntime) {
138   return CJS_Result::Success(pRuntime->NewString(JS_STR_VIEWERVARIATION));
139 }
140 
set_viewer_variation(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)141 CJS_Result CJS_App::set_viewer_variation(CJS_Runtime* pRuntime,
142                                          v8::Local<v8::Value> vp) {
143   return CJS_Result::Failure(JSMessage::kNotSupportedError);
144 }
145 
get_viewer_version(CJS_Runtime * pRuntime)146 CJS_Result CJS_App::get_viewer_version(CJS_Runtime* pRuntime) {
147   CPDF_Document::Extension* pContext =
148       pRuntime->GetFormFillEnv()->GetDocExtension();
149   int version = pContext && pContext->ContainsExtensionForm()
150                     ? JS_NUM_VIEWERVERSION_XFA
151                     : JS_NUM_VIEWERVERSION;
152   return CJS_Result::Success(pRuntime->NewNumber(version));
153 }
154 
set_viewer_version(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)155 CJS_Result CJS_App::set_viewer_version(CJS_Runtime* pRuntime,
156                                        v8::Local<v8::Value> vp) {
157   return CJS_Result::Failure(JSMessage::kNotSupportedError);
158 }
159 
get_platform(CJS_Runtime * pRuntime)160 CJS_Result CJS_App::get_platform(CJS_Runtime* pRuntime) {
161   CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
162   if (pFormFillEnv) {
163     WideString platform = pFormFillEnv->GetPlatform();
164     if (!platform.IsEmpty())
165       return CJS_Result::Success(pRuntime->NewString(platform.AsStringView()));
166   }
167   return CJS_Result::Success(pRuntime->NewString(JS_STR_PLATFORM));
168 }
169 
set_platform(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)170 CJS_Result CJS_App::set_platform(CJS_Runtime* pRuntime,
171                                  v8::Local<v8::Value> vp) {
172   return CJS_Result::Failure(JSMessage::kNotSupportedError);
173 }
174 
get_language(CJS_Runtime * pRuntime)175 CJS_Result CJS_App::get_language(CJS_Runtime* pRuntime) {
176   CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
177   if (pFormFillEnv) {
178     WideString language = pFormFillEnv->GetLanguage();
179     if (!language.IsEmpty())
180       return CJS_Result::Success(pRuntime->NewString(language.AsStringView()));
181   }
182   return CJS_Result::Success(pRuntime->NewString(JS_STR_LANGUAGE));
183 }
184 
set_language(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)185 CJS_Result CJS_App::set_language(CJS_Runtime* pRuntime,
186                                  v8::Local<v8::Value> vp) {
187   return CJS_Result::Failure(JSMessage::kNotSupportedError);
188 }
189 
190 // creates a new fdf object that contains no data
191 // comment: need reader support
192 // note:
193 // CFDF_Document * CPDFSDK_FormFillEnvironment::NewFDF();
newFDF(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)194 CJS_Result CJS_App::newFDF(CJS_Runtime* pRuntime,
195                            const std::vector<v8::Local<v8::Value>>& params) {
196   return CJS_Result::Success();
197 }
198 
199 // opens a specified pdf document and returns its document object
200 // comment:need reader support
201 // note: as defined in js reference, the proto of this function's fourth
202 // parmeters, how old an fdf document while do not show it.
203 // CFDF_Document * CPDFSDK_FormFillEnvironment::OpenFDF(string strPath,bool
204 // bUserConv);
205 
openFDF(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)206 CJS_Result CJS_App::openFDF(CJS_Runtime* pRuntime,
207                             const std::vector<v8::Local<v8::Value>>& params) {
208   return CJS_Result::Success();
209 }
210 
alert(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)211 CJS_Result CJS_App::alert(CJS_Runtime* pRuntime,
212                           const std::vector<v8::Local<v8::Value>>& params) {
213   std::vector<v8::Local<v8::Value>> newParams = ExpandKeywordParams(
214       pRuntime, params, 4, "cMsg", "nIcon", "nType", "cTitle");
215 
216   if (!IsExpandedParamKnown(newParams[0]))
217     return CJS_Result::Failure(JSMessage::kParamError);
218 
219   CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
220   if (!pFormFillEnv)
221     return CJS_Result::Success(pRuntime->NewNumber(0));
222 
223   WideString swMsg;
224   if (newParams[0]->IsArray()) {
225     v8::Local<v8::Array> carray = pRuntime->ToArray(newParams[0]);
226     swMsg = L"[";
227     for (size_t i = 0; i < pRuntime->GetArrayLength(carray); ++i) {
228       if (i)
229         swMsg += L", ";
230 
231       swMsg += pRuntime->ToWideString(pRuntime->GetArrayElement(carray, i));
232     }
233     swMsg += L"]";
234   } else {
235     swMsg = pRuntime->ToWideString(newParams[0]);
236   }
237 
238   int iIcon = JSPLATFORM_ALERT_ICON_DEFAULT;
239   if (IsExpandedParamKnown(newParams[1]))
240     iIcon = pRuntime->ToInt32(newParams[1]);
241 
242   int iType = JSPLATFORM_ALERT_BUTTON_DEFAULT;
243   if (IsExpandedParamKnown(newParams[2]))
244     iType = pRuntime->ToInt32(newParams[2]);
245 
246   WideString swTitle;
247   if (IsExpandedParamKnown(newParams[3]))
248     swTitle = pRuntime->ToWideString(newParams[3]);
249   else
250     swTitle = JSGetStringFromID(JSMessage::kAlert);
251 
252   pRuntime->BeginBlock();
253   pFormFillEnv->KillFocusAnnot(0);
254   v8::Local<v8::Value> ret = pRuntime->NewNumber(
255       pFormFillEnv->JS_appAlert(swMsg, swTitle, iType, iIcon));
256   pRuntime->EndBlock();
257 
258   return CJS_Result::Success(ret);
259 }
260 
beep(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)261 CJS_Result CJS_App::beep(CJS_Runtime* pRuntime,
262                          const std::vector<v8::Local<v8::Value>>& params) {
263   if (params.size() != 1)
264     return CJS_Result::Failure(JSMessage::kParamError);
265 
266   int type = JSPLATFORM_BEEP_DEFAULT;
267   if (IsExpandedParamKnown(params[0]))
268     type = pRuntime->ToInt32(params[0]);
269 
270   pRuntime->GetFormFillEnv()->JS_appBeep(type);
271   return CJS_Result::Success();
272 }
273 
findComponent(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)274 CJS_Result CJS_App::findComponent(
275     CJS_Runtime* pRuntime,
276     const std::vector<v8::Local<v8::Value>>& params) {
277   return CJS_Result::Success();
278 }
279 
popUpMenuEx(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)280 CJS_Result CJS_App::popUpMenuEx(
281     CJS_Runtime* pRuntime,
282     const std::vector<v8::Local<v8::Value>>& params) {
283   return CJS_Result::Failure(JSMessage::kNotSupportedError);
284 }
285 
get_fs(CJS_Runtime * pRuntime)286 CJS_Result CJS_App::get_fs(CJS_Runtime* pRuntime) {
287   return CJS_Result::Failure(JSMessage::kNotSupportedError);
288 }
289 
set_fs(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)290 CJS_Result CJS_App::set_fs(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
291   return CJS_Result::Failure(JSMessage::kNotSupportedError);
292 }
293 
setInterval(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)294 CJS_Result CJS_App::setInterval(
295     CJS_Runtime* pRuntime,
296     const std::vector<v8::Local<v8::Value>>& params) {
297   if (params.size() == 0 || params.size() > 2)
298     return CJS_Result::Failure(JSMessage::kParamError);
299 
300   WideString script = pRuntime->ToWideString(params[0]);
301   if (script.IsEmpty())
302     return CJS_Result::Failure(JSMessage::kInvalidInputError);
303 
304   uint32_t dwInterval = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 1000;
305   auto timerRef = pdfium::MakeUnique<GlobalTimer>(
306       this, pRuntime, GlobalTimer::Type::kRepeating, script, dwInterval, 0);
307   GlobalTimer* pTimerRef = timerRef.get();
308   m_Timers.insert(std::move(timerRef));
309 
310   v8::Local<v8::Object> pRetObj = pRuntime->NewFXJSBoundObject(
311       CJS_TimerObj::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
312   if (pRetObj.IsEmpty())
313     return CJS_Result::Failure(JSMessage::kBadObjectError);
314 
315   auto* pJS_TimerObj =
316       static_cast<CJS_TimerObj*>(CFXJS_Engine::GetObjectPrivate(pRetObj));
317 
318   pJS_TimerObj->SetTimer(pTimerRef);
319   return CJS_Result::Success(pRetObj);
320 }
321 
setTimeOut(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)322 CJS_Result CJS_App::setTimeOut(
323     CJS_Runtime* pRuntime,
324     const std::vector<v8::Local<v8::Value>>& params) {
325   if (params.size() == 0 || params.size() > 2)
326     return CJS_Result::Failure(JSMessage::kParamError);
327 
328   WideString script = pRuntime->ToWideString(params[0]);
329   if (script.IsEmpty())
330     return CJS_Result::Failure(JSMessage::kInvalidInputError);
331 
332   uint32_t dwTimeOut = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 1000;
333   auto timerRef = pdfium::MakeUnique<GlobalTimer>(this, pRuntime,
334                                                   GlobalTimer::Type::kOneShot,
335                                                   script, dwTimeOut, dwTimeOut);
336   GlobalTimer* pTimerRef = timerRef.get();
337   m_Timers.insert(std::move(timerRef));
338 
339   v8::Local<v8::Object> pRetObj = pRuntime->NewFXJSBoundObject(
340       CJS_TimerObj::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
341   if (pRetObj.IsEmpty())
342     return CJS_Result::Failure(JSMessage::kBadObjectError);
343 
344   auto* pJS_TimerObj =
345       static_cast<CJS_TimerObj*>(CFXJS_Engine::GetObjectPrivate(pRetObj));
346 
347   pJS_TimerObj->SetTimer(pTimerRef);
348   return CJS_Result::Success(pRetObj);
349 }
350 
clearTimeOut(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)351 CJS_Result CJS_App::clearTimeOut(
352     CJS_Runtime* pRuntime,
353     const std::vector<v8::Local<v8::Value>>& params) {
354   if (params.size() != 1)
355     return CJS_Result::Failure(JSMessage::kParamError);
356 
357   CJS_App::ClearTimerCommon(pRuntime, params[0]);
358   return CJS_Result::Success();
359 }
360 
clearInterval(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)361 CJS_Result CJS_App::clearInterval(
362     CJS_Runtime* pRuntime,
363     const std::vector<v8::Local<v8::Value>>& params) {
364   if (params.size() != 1)
365     return CJS_Result::Failure(JSMessage::kParamError);
366 
367   CJS_App::ClearTimerCommon(pRuntime, params[0]);
368   return CJS_Result::Success();
369 }
370 
ClearTimerCommon(CJS_Runtime * pRuntime,v8::Local<v8::Value> param)371 void CJS_App::ClearTimerCommon(CJS_Runtime* pRuntime,
372                                v8::Local<v8::Value> param) {
373   if (!param->IsObject())
374     return;
375 
376   v8::Local<v8::Object> pObj = pRuntime->ToObject(param);
377   auto pTimer = JSGetObject<CJS_TimerObj>(pObj);
378   if (!pTimer)
379     return;
380 
381   GlobalTimer::Cancel(pTimer->GetTimerID());
382 }
383 
execMenuItem(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)384 CJS_Result CJS_App::execMenuItem(
385     CJS_Runtime* pRuntime,
386     const std::vector<v8::Local<v8::Value>>& params) {
387   return CJS_Result::Failure(JSMessage::kNotSupportedError);
388 }
389 
TimerProc(GlobalTimer * pTimer)390 void CJS_App::TimerProc(GlobalTimer* pTimer) {
391   CJS_Runtime* pRuntime = pTimer->GetRuntime();
392   if (pRuntime && (!pTimer->IsOneShot() || pTimer->GetTimeOut() > 0))
393     RunJsScript(pRuntime, pTimer->GetJScript());
394 }
395 
CancelProc(GlobalTimer * pTimer)396 void CJS_App::CancelProc(GlobalTimer* pTimer) {
397   m_Timers.erase(pdfium::FakeUniquePtr<GlobalTimer>(pTimer));
398 }
399 
RunJsScript(CJS_Runtime * pRuntime,const WideString & wsScript)400 void CJS_App::RunJsScript(CJS_Runtime* pRuntime, const WideString& wsScript) {
401   if (pRuntime->IsBlocking())
402     return;
403 
404   IJS_Runtime::ScopedEventContext pContext(pRuntime);
405   pContext->OnExternal_Exec();
406   pContext->RunScript(wsScript);
407 }
408 
goBack(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)409 CJS_Result CJS_App::goBack(CJS_Runtime* pRuntime,
410                            const std::vector<v8::Local<v8::Value>>& params) {
411   // Not supported, but do not return error.
412   return CJS_Result::Success();
413 }
414 
goForward(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)415 CJS_Result CJS_App::goForward(CJS_Runtime* pRuntime,
416                               const std::vector<v8::Local<v8::Value>>& params) {
417   // Not supported, but do not return error.
418   return CJS_Result::Success();
419 }
420 
mailMsg(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)421 CJS_Result CJS_App::mailMsg(CJS_Runtime* pRuntime,
422                             const std::vector<v8::Local<v8::Value>>& params) {
423   std::vector<v8::Local<v8::Value>> newParams = ExpandKeywordParams(
424       pRuntime, params, 6, "bUI", "cTo", "cCc", "cBcc", "cSubject", "cMsg");
425 
426   if (!IsExpandedParamKnown(newParams[0]))
427     return CJS_Result::Failure(JSMessage::kParamError);
428 
429   bool bUI = pRuntime->ToBoolean(newParams[0]);
430   WideString cTo;
431   if (IsExpandedParamKnown(newParams[1])) {
432     cTo = pRuntime->ToWideString(newParams[1]);
433   } else {
434     // cTo parameter required when UI not invoked.
435     if (!bUI)
436       return CJS_Result::Failure(JSMessage::kParamError);
437   }
438 
439   WideString cCc;
440   if (IsExpandedParamKnown(newParams[2]))
441     cCc = pRuntime->ToWideString(newParams[2]);
442 
443   WideString cBcc;
444   if (IsExpandedParamKnown(newParams[3]))
445     cBcc = pRuntime->ToWideString(newParams[3]);
446 
447   WideString cSubject;
448   if (IsExpandedParamKnown(newParams[4]))
449     cSubject = pRuntime->ToWideString(newParams[4]);
450 
451   WideString cMsg;
452   if (IsExpandedParamKnown(newParams[5]))
453     cMsg = pRuntime->ToWideString(newParams[5]);
454 
455   pRuntime->BeginBlock();
456   pRuntime->GetFormFillEnv()->JS_docmailForm(nullptr, 0, bUI, cTo, cSubject,
457                                              cCc, cBcc, cMsg);
458   pRuntime->EndBlock();
459   return CJS_Result::Success();
460 }
461 
launchURL(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)462 CJS_Result CJS_App::launchURL(CJS_Runtime* pRuntime,
463                               const std::vector<v8::Local<v8::Value>>& params) {
464   // Unsafe, not supported, but do not return error.
465   return CJS_Result::Success();
466 }
467 
get_runtime_highlight(CJS_Runtime * pRuntime)468 CJS_Result CJS_App::get_runtime_highlight(CJS_Runtime* pRuntime) {
469   return CJS_Result::Success(pRuntime->NewBoolean(m_bRuntimeHighLight));
470 }
471 
set_runtime_highlight(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)472 CJS_Result CJS_App::set_runtime_highlight(CJS_Runtime* pRuntime,
473                                           v8::Local<v8::Value> vp) {
474   m_bRuntimeHighLight = pRuntime->ToBoolean(vp);
475   return CJS_Result::Success();
476 }
477 
get_fullscreen(CJS_Runtime * pRuntime)478 CJS_Result CJS_App::get_fullscreen(CJS_Runtime* pRuntime) {
479   return CJS_Result::Failure(JSMessage::kNotSupportedError);
480 }
481 
set_fullscreen(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)482 CJS_Result CJS_App::set_fullscreen(CJS_Runtime* pRuntime,
483                                    v8::Local<v8::Value> vp) {
484   return CJS_Result::Failure(JSMessage::kNotSupportedError);
485 }
486 
popUpMenu(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)487 CJS_Result CJS_App::popUpMenu(CJS_Runtime* pRuntime,
488                               const std::vector<v8::Local<v8::Value>>& params) {
489   return CJS_Result::Failure(JSMessage::kNotSupportedError);
490 }
491 
browseForDoc(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)492 CJS_Result CJS_App::browseForDoc(
493     CJS_Runtime* pRuntime,
494     const std::vector<v8::Local<v8::Value>>& params) {
495   // Unsafe, not supported, but do not return an error.
496   return CJS_Result::Success();
497 }
498 
SysPathToPDFPath(const WideString & sOldPath)499 WideString CJS_App::SysPathToPDFPath(const WideString& sOldPath) {
500   WideString sRet = L"/";
501   for (const wchar_t& c : sOldPath) {
502     if (c != L':')
503       sRet += (c == L'\\') ? L'/' : c;
504   }
505   return sRet;
506 }
507 
newDoc(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)508 CJS_Result CJS_App::newDoc(CJS_Runtime* pRuntime,
509                            const std::vector<v8::Local<v8::Value>>& params) {
510   return CJS_Result::Failure(JSMessage::kNotSupportedError);
511 }
512 
openDoc(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)513 CJS_Result CJS_App::openDoc(CJS_Runtime* pRuntime,
514                             const std::vector<v8::Local<v8::Value>>& params) {
515   return CJS_Result::Failure(JSMessage::kNotSupportedError);
516 }
517 
response(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)518 CJS_Result CJS_App::response(CJS_Runtime* pRuntime,
519                              const std::vector<v8::Local<v8::Value>>& params) {
520   std::vector<v8::Local<v8::Value>> newParams =
521       ExpandKeywordParams(pRuntime, params, 5, "cQuestion", "cTitle",
522                           "cDefault", "bPassword", "cLabel");
523 
524   if (!IsExpandedParamKnown(newParams[0]))
525     return CJS_Result::Failure(JSMessage::kParamError);
526 
527   WideString swQuestion = pRuntime->ToWideString(newParams[0]);
528   WideString swTitle = L"PDF";
529   if (IsExpandedParamKnown(newParams[1]))
530     swTitle = pRuntime->ToWideString(newParams[1]);
531 
532   WideString swDefault;
533   if (IsExpandedParamKnown(newParams[2]))
534     swDefault = pRuntime->ToWideString(newParams[2]);
535 
536   bool bPassword = false;
537   if (IsExpandedParamKnown(newParams[3]))
538     bPassword = pRuntime->ToBoolean(newParams[3]);
539 
540   WideString swLabel;
541   if (IsExpandedParamKnown(newParams[4]))
542     swLabel = pRuntime->ToWideString(newParams[4]);
543 
544   const int MAX_INPUT_BYTES = 2048;
545   std::vector<uint8_t> pBuff(MAX_INPUT_BYTES + 2);
546   int nLengthBytes = pRuntime->GetFormFillEnv()->JS_appResponse(
547       swQuestion, swTitle, swDefault, swLabel, bPassword, pBuff.data(),
548       MAX_INPUT_BYTES);
549 
550   if (nLengthBytes < 0 || nLengthBytes > MAX_INPUT_BYTES)
551     return CJS_Result::Failure(JSMessage::kParamTooLongError);
552 
553   return CJS_Result::Success(pRuntime->NewString(
554       WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
555                               nLengthBytes / sizeof(uint16_t))
556           .AsStringView()));
557 }
558 
get_media(CJS_Runtime * pRuntime)559 CJS_Result CJS_App::get_media(CJS_Runtime* pRuntime) {
560   return CJS_Result::Failure(JSMessage::kNotSupportedError);
561 }
562 
set_media(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)563 CJS_Result CJS_App::set_media(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
564   return CJS_Result::Failure(JSMessage::kNotSupportedError);
565 }
566 
execDialog(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)567 CJS_Result CJS_App::execDialog(
568     CJS_Runtime* pRuntime,
569     const std::vector<v8::Local<v8::Value>>& params) {
570   return CJS_Result::Success();
571 }
572