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