7#include "fxjs/cjs_app.h"
14#include "core/fxcrt/fixed_size_data_vector.h"
15#include "core/fxcrt/stl_util.h"
16#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
17#include "fpdfsdk/cpdfsdk_interactiveform.h"
18#include "fxjs/cjs_document.h"
19#include "fxjs/cjs_timerobj.h"
20#include "fxjs/global_timer.h"
21#include "fxjs/ijs_event_context.h"
22#include "fxjs/js_resources.h"
23#include "third_party/base/containers/span.h"
24#include "v8/include/v8-container.h"
28constexpr wchar_t kStrViewerType[] = L"pdfium";
29constexpr wchar_t kStrViewerVariation[] = L"Full";
30constexpr wchar_t kStrPlatform[] = L"WIN";
31constexpr wchar_t kStrLanguage[] = L"ENU";
32constexpr int kNumViewerVersion = 8;
33constexpr int kNumViewerVersionXfa = 11;
34constexpr int kNumFormsVersion = 7;
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}};
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}};
77uint32_t CJS_App::ObjDefnID = 0;
79const char CJS_App::kName[] =
"app";
89 JSConstructor<CJS_App>, JSDestructor);
94CJS_App::
CJS_App(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
99CJS_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);
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);
112CJS_Result CJS_App::set_active_docs(CJS_Runtime* pRuntime,
113 v8::Local<v8::Value> vp) {
117CJS_Result CJS_App::get_calculate(CJS_Runtime* pRuntime) {
121CJS_Result CJS_App::set_calculate(CJS_Runtime* pRuntime,
122 v8::Local<v8::Value> vp) {
123 m_bCalculate = pRuntime->ToBoolean(vp);
129CJS_Result CJS_App::get_forms_version(CJS_Runtime* pRuntime) {
133CJS_Result CJS_App::set_forms_version(CJS_Runtime* pRuntime,
134 v8::Local<v8::Value> vp) {
138CJS_Result CJS_App::get_viewer_type(CJS_Runtime* pRuntime) {
142CJS_Result CJS_App::set_viewer_type(CJS_Runtime* pRuntime,
143 v8::Local<v8::Value> vp) {
147CJS_Result CJS_App::get_viewer_variation(CJS_Runtime* pRuntime) {
151CJS_Result CJS_App::set_viewer_variation(CJS_Runtime* pRuntime,
152 v8::Local<v8::Value> vp) {
156CJS_Result CJS_App::get_viewer_version(CJS_Runtime* pRuntime) {
160 ? kNumViewerVersionXfa
165CJS_Result CJS_App::set_viewer_version(CJS_Runtime* pRuntime,
166 v8::Local<v8::Value> vp) {
170CJS_Result CJS_App::get_platform(CJS_Runtime* pRuntime) {
173 WideString platform = pFormFillEnv->GetPlatform();
180CJS_Result CJS_App::set_platform(CJS_Runtime* pRuntime,
181 v8::Local<v8::Value> vp) {
185CJS_Result CJS_App::get_language(CJS_Runtime* pRuntime) {
188 WideString language = pFormFillEnv->GetLanguage();
195CJS_Result CJS_App::set_language(CJS_Runtime* pRuntime,
196 v8::Local<v8::Value> vp) {
204CJS_Result CJS_App::newFDF(CJS_Runtime* pRuntime,
205 pdfium::span<v8::Local<v8::Value>> params) {
216CJS_Result CJS_App::openFDF(CJS_Runtime* pRuntime,
217 pdfium::span<v8::Local<v8::Value>> params) {
221CJS_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");
226 if (!IsExpandedParamKnown(newParams[0]))
234 if (newParams[0]->IsArray()) {
235 v8::Local<v8::Array> carray = pRuntime->ToArray(newParams[0]);
237 for (size_t i = 0; i < pRuntime->GetArrayLength(carray); ++i) {
241 swMsg += pRuntime->ToWideString(pRuntime->GetArrayElement(carray, i));
245 swMsg = pRuntime->ToWideString(newParams[0]);
249 if (IsExpandedParamKnown(newParams[1]))
250 iIcon = pRuntime->ToInt32(newParams[1]);
253 if (IsExpandedParamKnown(newParams[2]))
254 iType = pRuntime->ToInt32(newParams[2]);
257 if (IsExpandedParamKnown(newParams[3]))
258 swTitle = pRuntime->ToWideString(newParams[3]);
264 v8::Local<v8::Value> ret = pRuntime->NewNumber(
265 pFormFillEnv->JS_appAlert(swMsg, swTitle, iType, iIcon));
268 return CJS_Result::Success(ret);
271CJS_Result CJS_App::beep(CJS_Runtime* pRuntime,
272 pdfium::span<v8::Local<v8::Value>> params) {
273 if (params.size() != 1)
277 if (IsExpandedParamKnown(params[0]))
278 type = pRuntime->ToInt32(params[0]);
284CJS_Result CJS_App::findComponent(CJS_Runtime* pRuntime,
285 pdfium::span<v8::Local<v8::Value>> params) {
289CJS_Result CJS_App::popUpMenuEx(CJS_Runtime* pRuntime,
290 pdfium::span<v8::Local<v8::Value>> params) {
294CJS_Result CJS_App::get_fs(CJS_Runtime* pRuntime) {
298CJS_Result CJS_App::set_fs(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
302CJS_Result CJS_App::setInterval(CJS_Runtime* pRuntime,
303 pdfium::span<v8::Local<v8::Value>> params) {
304 if (params.size() == 0 || params.size() > 2)
307 WideString script = pRuntime->ToWideString(params[0]);
311 uint32_t dwInterval = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 1000;
315 m_Timers.insert(std::move(timerRef));
317 v8::Local<v8::Object> pRetObj = pRuntime->NewFXJSBoundObject(
318 CJS_TimerObj::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
319 if (pRetObj.IsEmpty())
322 auto* pJS_TimerObj =
static_cast<CJS_TimerObj*>(
323 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pRetObj));
325 pJS_TimerObj->SetTimer(pTimerRef);
326 return CJS_Result::Success(pRetObj);
329CJS_Result CJS_App::setTimeOut(CJS_Runtime* pRuntime,
330 pdfium::span<v8::Local<v8::Value>> params) {
331 if (params.size() == 0 || params.size() > 2)
334 WideString script = pRuntime->ToWideString(params[0]);
338 uint32_t dwTimeOut = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 1000;
341 script, dwTimeOut, dwTimeOut);
343 m_Timers.insert(std::move(timerRef));
345 v8::Local<v8::Object> pRetObj = pRuntime->NewFXJSBoundObject(
346 CJS_TimerObj::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
347 if (pRetObj.IsEmpty())
350 auto* pJS_TimerObj =
static_cast<CJS_TimerObj*>(
351 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pRetObj));
353 pJS_TimerObj->SetTimer(pTimerRef);
354 return CJS_Result::Success(pRetObj);
357CJS_Result CJS_App::clearTimeOut(CJS_Runtime* pRuntime,
358 pdfium::span<v8::Local<v8::Value>> params) {
359 if (params.size() != 1)
362 CJS_App::ClearTimerCommon(pRuntime, params[0]);
366CJS_Result CJS_App::clearInterval(CJS_Runtime* pRuntime,
367 pdfium::span<v8::Local<v8::Value>> params) {
368 if (params.size() != 1)
371 CJS_App::ClearTimerCommon(pRuntime, params[0]);
375void CJS_App::ClearTimerCommon(CJS_Runtime* pRuntime,
376 v8::Local<v8::Value> param) {
377 if (!param->IsObject())
380 v8::Local<v8::Object> pObj = pRuntime->ToObject(param);
381 auto pTimer = JSGetObject<CJS_TimerObj>(pRuntime->GetIsolate(), pObj);
388CJS_Result CJS_App::execMenuItem(CJS_Runtime* pRuntime,
389 pdfium::span<v8::Local<v8::Value>> params) {
400 m_Timers.erase(fxcrt::MakeFakeUniquePtr(pTimer));
403void CJS_App::RunJsScript(CJS_Runtime* pRuntime,
const WideString& wsScript) {
409 pContext->RunScript(wsScript);
412CJS_Result CJS_App::goBack(CJS_Runtime* pRuntime,
413 pdfium::span<v8::Local<v8::Value>> params) {
418CJS_Result CJS_App::goForward(CJS_Runtime* pRuntime,
419 pdfium::span<v8::Local<v8::Value>> params) {
424CJS_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");
429 if (!IsExpandedParamKnown(newParams[0]))
432 bool bUI = pRuntime->ToBoolean(newParams[0]);
434 if (IsExpandedParamKnown(newParams[1])) {
435 cTo = pRuntime->ToWideString(newParams[1]);
443 if (IsExpandedParamKnown(newParams[2]))
444 cCc = pRuntime->ToWideString(newParams[2]);
447 if (IsExpandedParamKnown(newParams[3]))
448 cBcc = pRuntime->ToWideString(newParams[3]);
451 if (IsExpandedParamKnown(newParams[4]))
452 cSubject = pRuntime->ToWideString(newParams[4]);
455 if (IsExpandedParamKnown(newParams[5]))
456 cMsg = pRuntime->ToWideString(newParams[5]);
460 cTo, cSubject, cCc, cBcc, cMsg);
465CJS_Result CJS_App::launchURL(CJS_Runtime* pRuntime,
466 pdfium::span<v8::Local<v8::Value>> params) {
471CJS_Result CJS_App::get_runtime_highlight(CJS_Runtime* pRuntime) {
475CJS_Result CJS_App::set_runtime_highlight(CJS_Runtime* pRuntime,
476 v8::Local<v8::Value> vp) {
477 m_bRuntimeHighLight = pRuntime->ToBoolean(vp);
481CJS_Result CJS_App::get_fullscreen(CJS_Runtime* pRuntime) {
485CJS_Result CJS_App::set_fullscreen(CJS_Runtime* pRuntime,
486 v8::Local<v8::Value> vp) {
490CJS_Result CJS_App::popUpMenu(CJS_Runtime* pRuntime,
491 pdfium::span<v8::Local<v8::Value>> params) {
495CJS_Result CJS_App::browseForDoc(CJS_Runtime* pRuntime,
496 pdfium::span<v8::Local<v8::Value>> params) {
502 WideString sRet = L"/";
503 for (
const wchar_t& c : sOldPath) {
505 sRet
+= (c == L'\\') ? L'/' : c;
510CJS_Result CJS_App::newDoc(CJS_Runtime* pRuntime,
511 pdfium::span<v8::Local<v8::Value>> params) {
515CJS_Result CJS_App::openDoc(CJS_Runtime* pRuntime,
516 pdfium::span<v8::Local<v8::Value>> params) {
520CJS_Result CJS_App::response(CJS_Runtime* pRuntime,
521 pdfium::span<v8::Local<v8::Value>> params) {
522 v8::LocalVector<v8::Value> newParams =
523 ExpandKeywordParams(pRuntime, params, 5,
"cQuestion",
"cTitle",
524 "cDefault",
"bPassword",
"cLabel");
526 if (!IsExpandedParamKnown(newParams[0]))
529 WideString swQuestion = pRuntime->ToWideString(newParams[0]);
530 WideString swTitle = L"PDF";
531 if (IsExpandedParamKnown(newParams[1]))
532 swTitle = pRuntime->ToWideString(newParams[1]);
534 WideString swDefault;
535 if (IsExpandedParamKnown(newParams[2]))
536 swDefault = pRuntime->ToWideString(newParams[2]);
538 bool bPassword =
false;
539 if (IsExpandedParamKnown(newParams[3]))
540 bPassword = pRuntime->ToBoolean(newParams[3]);
543 if (IsExpandedParamKnown(newParams[4]))
544 swLabel = pRuntime->ToWideString(newParams[4]);
546 constexpr int kMaxWideChars = 1024;
547 constexpr int kMaxBytes = kMaxWideChars *
sizeof(uint16_t);
548 auto buffer = FixedSizeDataVector<uint8_t>::Zeroed(kMaxBytes);
550 swQuestion, swTitle, swDefault, swLabel, bPassword, buffer.span());
552 if (byte_length < 0 || byte_length > kMaxBytes)
559CJS_Result CJS_App::get_media(CJS_Runtime* pRuntime) {
563CJS_Result CJS_App::set_media(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
567CJS_Result CJS_App::execDialog(CJS_Runtime* pRuntime,
568 pdfium::span<v8::Local<v8::Value>> params) {
static void DefineJSObjects(CFXJS_Engine *pEngine)
static WideString SysPathToPDFPath(const WideString &sOldPath)
void CancelProc(GlobalTimer *pTimer)
static uint32_t GetObjDefnID()
void TimerProc(GlobalTimer *pTimer)
CJS_App(v8::Local< v8::Object > pObject, CJS_Runtime *pRuntime)
static void DefineProps(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSPropertySpec > consts)
static void DefineMethods(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSMethodSpec > consts)
static CJS_Result Success()
static CJS_Result Failure(JSMessage id)
CPDFSDK_FormFillEnvironment * GetFormFillEnv() const override
virtual bool ContainsExtensionForm() const =0
uint32_t GetTimeOut() const
CJS_Runtime * GetRuntime() const
WideString GetJScript() const
static void Cancel(int32_t nTimerID)
virtual void OnExternal_Exec()=0
IJS_EventContext * operator->() const
WideString & operator=(WideString &&that) noexcept
WideString & operator+=(const wchar_t *str)
WideString & operator+=(wchar_t ch)
static WideString FromUTF16LE(pdfium::span< const uint8_t > data)
WideString & operator=(const wchar_t *str)
WideString JSGetStringFromID(JSMessage msg)