Refactor fxjs_v8 and add embeddertests for it.
[pdfium.git] / fpdfsdk / src / javascript / app.cpp
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 "../../../third_party/base/nonstd_unique_ptr.h"
8 #include "../../include/javascript/JavaScript.h"
9 #include "../../include/javascript/IJavaScript.h"
10 #include "../../include/javascript/JS_Define.h"
11 #include "../../include/javascript/JS_Object.h"
12 #include "../../include/javascript/JS_Value.h"
13 #include "../../include/javascript/app.h"
14 #include "../../include/javascript/JS_EventHandler.h"
15 #include "../../include/javascript/resource.h"
16 #include "../../include/javascript/JS_Context.h"
17 #include "../../include/javascript/JS_Runtime.h"
18 #include "../../include/javascript/Document.h"
19
20 static v8::Isolate* GetIsolate(IFXJS_Context* cc) {
21   CJS_Context* pContext = (CJS_Context*)cc;
22   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
23   return pRuntime->GetIsolate();
24 }
25
26 BEGIN_JS_STATIC_CONST(CJS_TimerObj)
27 END_JS_STATIC_CONST()
28
29 BEGIN_JS_STATIC_PROP(CJS_TimerObj)
30 END_JS_STATIC_PROP()
31
32 BEGIN_JS_STATIC_METHOD(CJS_TimerObj)
33 END_JS_STATIC_METHOD()
34
35 IMPLEMENT_JS_CLASS(CJS_TimerObj, TimerObj)
36
37 TimerObj::TimerObj(CJS_Object* pJSObject)
38     : CJS_EmbedObj(pJSObject), m_pTimer(NULL) {}
39
40 TimerObj::~TimerObj() {}
41
42 void TimerObj::SetTimer(CJS_Timer* pTimer) {
43   m_pTimer = pTimer;
44 }
45
46 CJS_Timer* TimerObj::GetTimer() const {
47   return m_pTimer;
48 }
49
50 #define JS_STR_VIEWERTYPE L"pdfium"
51 #define JS_STR_VIEWERVARIATION L"Full"
52 #define JS_STR_PLATFORM L"WIN"
53 #define JS_STR_LANGUANGE L"ENU"
54 #define JS_NUM_VIEWERVERSION 8
55 #define JS_NUM_FORMSVERSION 7
56
57 BEGIN_JS_STATIC_CONST(CJS_App)
58 END_JS_STATIC_CONST()
59
60 BEGIN_JS_STATIC_PROP(CJS_App)
61 JS_STATIC_PROP_ENTRY(activeDocs)
62 JS_STATIC_PROP_ENTRY(calculate)
63 JS_STATIC_PROP_ENTRY(formsVersion)
64 JS_STATIC_PROP_ENTRY(fs)
65 JS_STATIC_PROP_ENTRY(fullscreen)
66 JS_STATIC_PROP_ENTRY(language)
67 JS_STATIC_PROP_ENTRY(media)
68 JS_STATIC_PROP_ENTRY(platform)
69 JS_STATIC_PROP_ENTRY(runtimeHighlight)
70 JS_STATIC_PROP_ENTRY(viewerType)
71 JS_STATIC_PROP_ENTRY(viewerVariation)
72 JS_STATIC_PROP_ENTRY(viewerVersion)
73 END_JS_STATIC_PROP()
74
75 BEGIN_JS_STATIC_METHOD(CJS_App)
76 JS_STATIC_METHOD_ENTRY(alert)
77 JS_STATIC_METHOD_ENTRY(beep)
78 JS_STATIC_METHOD_ENTRY(browseForDoc)
79 JS_STATIC_METHOD_ENTRY(clearInterval)
80 JS_STATIC_METHOD_ENTRY(clearTimeOut)
81 JS_STATIC_METHOD_ENTRY(execDialog)
82 JS_STATIC_METHOD_ENTRY(execMenuItem)
83 JS_STATIC_METHOD_ENTRY(findComponent)
84 JS_STATIC_METHOD_ENTRY(goBack)
85 JS_STATIC_METHOD_ENTRY(goForward)
86 JS_STATIC_METHOD_ENTRY(launchURL)
87 JS_STATIC_METHOD_ENTRY(mailMsg)
88 JS_STATIC_METHOD_ENTRY(newFDF)
89 JS_STATIC_METHOD_ENTRY(newDoc)
90 JS_STATIC_METHOD_ENTRY(openDoc)
91 JS_STATIC_METHOD_ENTRY(openFDF)
92 JS_STATIC_METHOD_ENTRY(popUpMenuEx)
93 JS_STATIC_METHOD_ENTRY(popUpMenu)
94 JS_STATIC_METHOD_ENTRY(response)
95 JS_STATIC_METHOD_ENTRY(setInterval)
96 JS_STATIC_METHOD_ENTRY(setTimeOut)
97 END_JS_STATIC_METHOD()
98
99 IMPLEMENT_JS_CLASS(CJS_App, app)
100
101 app::app(CJS_Object* pJSObject)
102     : CJS_EmbedObj(pJSObject), m_bCalculate(true), m_bRuntimeHighLight(false) {}
103
104 app::~app() {
105   for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++)
106     delete m_aTimer[i];
107
108   m_aTimer.RemoveAll();
109 }
110
111 FX_BOOL app::activeDocs(IFXJS_Context* cc,
112                         CJS_PropValue& vp,
113                         CFX_WideString& sError) {
114   if (!vp.IsGetting())
115     return FALSE;
116
117   CJS_Context* pContext = (CJS_Context*)cc;
118   CPDFDoc_Environment* pApp = pContext->GetReaderApp();
119   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
120   CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument();
121   CJS_Array aDocs(pRuntime->GetIsolate());
122   if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument()) {
123     CJS_Document* pJSDocument = NULL;
124     if (pDoc == pCurDoc) {
125       v8::Local<v8::Object> pObj = JS_GetThisObj(pRuntime->GetIsolate());
126       if (JS_GetObjDefnID(pObj) ==
127           JS_GetObjDefnID(pRuntime->GetIsolate(), L"Document"))
128         pJSDocument =
129             (CJS_Document*)JS_GetPrivate(pRuntime->GetIsolate(), pObj);
130     } else {
131       v8::Local<v8::Object> pObj = JS_NewFxDynamicObj(
132           pRuntime->GetIsolate(), pContext,
133           JS_GetObjDefnID(pRuntime->GetIsolate(), L"Document"));
134       pJSDocument = (CJS_Document*)JS_GetPrivate(pRuntime->GetIsolate(), pObj);
135       ASSERT(pJSDocument != NULL);
136     }
137     aDocs.SetElement(0, CJS_Value(pRuntime->GetIsolate(), pJSDocument));
138   }
139   if (aDocs.GetLength() > 0)
140     vp << aDocs;
141   else
142     vp.SetNull();
143
144   return TRUE;
145 }
146
147 FX_BOOL app::calculate(IFXJS_Context* cc,
148                        CJS_PropValue& vp,
149                        CFX_WideString& sError) {
150   if (vp.IsSetting()) {
151     bool bVP;
152     vp >> bVP;
153     m_bCalculate = (FX_BOOL)bVP;
154
155     CJS_Context* pContext = (CJS_Context*)cc;
156     CPDFDoc_Environment* pApp = pContext->GetReaderApp();
157     CJS_Runtime* pRuntime = pContext->GetJSRuntime();
158     CJS_Array aDocs(pRuntime->GetIsolate());
159     if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
160       pDoc->GetInterForm()->EnableCalculate((FX_BOOL)m_bCalculate);
161   } else {
162     vp << (bool)m_bCalculate;
163   }
164   return TRUE;
165 }
166
167 FX_BOOL app::formsVersion(IFXJS_Context* cc,
168                           CJS_PropValue& vp,
169                           CFX_WideString& sError) {
170   if (vp.IsGetting()) {
171     vp << JS_NUM_FORMSVERSION;
172     return TRUE;
173   }
174
175   return FALSE;
176 }
177
178 FX_BOOL app::viewerType(IFXJS_Context* cc,
179                         CJS_PropValue& vp,
180                         CFX_WideString& sError) {
181   if (vp.IsGetting()) {
182     vp << JS_STR_VIEWERTYPE;
183     return TRUE;
184   }
185
186   return FALSE;
187 }
188
189 FX_BOOL app::viewerVariation(IFXJS_Context* cc,
190                              CJS_PropValue& vp,
191                              CFX_WideString& sError) {
192   if (vp.IsGetting()) {
193     vp << JS_STR_VIEWERVARIATION;
194     return TRUE;
195   }
196
197   return FALSE;
198 }
199
200 FX_BOOL app::viewerVersion(IFXJS_Context* cc,
201                            CJS_PropValue& vp,
202                            CFX_WideString& sError) {
203   if (vp.IsGetting()) {
204     vp << JS_NUM_VIEWERVERSION;
205     return TRUE;
206   }
207
208   return FALSE;
209 }
210
211 FX_BOOL app::platform(IFXJS_Context* cc,
212                       CJS_PropValue& vp,
213                       CFX_WideString& sError) {
214   if (vp.IsGetting()) {
215     vp << JS_STR_PLATFORM;
216     return TRUE;
217   }
218
219   return FALSE;
220 }
221
222 FX_BOOL app::language(IFXJS_Context* cc,
223                       CJS_PropValue& vp,
224                       CFX_WideString& sError) {
225   if (vp.IsGetting()) {
226     vp << JS_STR_LANGUANGE;
227     return TRUE;
228   }
229
230   return FALSE;
231 }
232
233 // creates a new fdf object that contains no data
234 // comment: need reader support
235 // note:
236 // CFDF_Document * CPDFDoc_Environment::NewFDF();
237 FX_BOOL app::newFDF(IFXJS_Context* cc,
238                     const CJS_Parameters& params,
239                     CJS_Value& vRet,
240                     CFX_WideString& sError) {
241   return TRUE;
242 }
243 // opens a specified pdf document and returns its document object
244 // comment:need reader support
245 // note: as defined in js reference, the proto of this function's fourth
246 // parmeters, how old an fdf document while do not show it.
247 // CFDF_Document * CPDFDoc_Environment::OpenFDF(string strPath,bool bUserConv);
248
249 FX_BOOL app::openFDF(IFXJS_Context* cc,
250                      const CJS_Parameters& params,
251                      CJS_Value& vRet,
252                      CFX_WideString& sError) {
253   return TRUE;
254 }
255
256 FX_BOOL app::alert(IFXJS_Context* cc,
257                    const CJS_Parameters& params,
258                    CJS_Value& vRet,
259                    CFX_WideString& sError) {
260   int iSize = params.size();
261   if (iSize < 1)
262     return FALSE;
263
264   CFX_WideString swMsg = L"";
265   CFX_WideString swTitle = L"";
266   int iIcon = 0;
267   int iType = 0;
268
269   v8::Isolate* isolate = GetIsolate(cc);
270
271   if (iSize == 1) {
272     if (params[0].GetType() == VT_object) {
273       v8::Local<v8::Object> pObj = params[0].ToV8Object();
274       {
275         v8::Local<v8::Value> pValue =
276             JS_GetObjectElement(isolate, pObj, L"cMsg");
277         swMsg = CJS_Value(isolate, pValue, VT_unknown).ToCFXWideString();
278
279         pValue = JS_GetObjectElement(isolate, pObj, L"cTitle");
280         swTitle = CJS_Value(isolate, pValue, VT_unknown).ToCFXWideString();
281
282         pValue = JS_GetObjectElement(isolate, pObj, L"nIcon");
283         iIcon = CJS_Value(isolate, pValue, VT_unknown).ToInt();
284
285         pValue = JS_GetObjectElement(isolate, pObj, L"nType");
286         iType = CJS_Value(isolate, pValue, VT_unknown).ToInt();
287       }
288
289       if (swMsg == L"") {
290         CJS_Array carray(isolate);
291         if (params[0].ConvertToArray(carray)) {
292           int iLength = carray.GetLength();
293           CJS_Value* pValue = new CJS_Value(isolate);
294           for (int i = 0; i < iLength; ++i) {
295             carray.GetElement(i, *pValue);
296             swMsg += (*pValue).ToCFXWideString();
297             if (i < iLength - 1)
298               swMsg += L",  ";
299           }
300
301           delete pValue;
302         }
303       }
304
305       if (swTitle == L"")
306         swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
307     } else if (params[0].GetType() == VT_boolean) {
308       FX_BOOL bGet = params[0].ToBool();
309       if (bGet)
310         swMsg = L"true";
311       else
312         swMsg = L"false";
313
314       swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
315     } else {
316       swMsg = params[0].ToCFXWideString();
317       swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
318     }
319   } else {
320     if (params[0].GetType() == VT_boolean) {
321       FX_BOOL bGet = params[0].ToBool();
322       if (bGet)
323         swMsg = L"true";
324       else
325         swMsg = L"false";
326     } else {
327       swMsg = params[0].ToCFXWideString();
328     }
329     swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
330
331     for (int i = 1; i < iSize; i++) {
332       if (i == 1)
333         iIcon = params[i].ToInt();
334       if (i == 2)
335         iType = params[i].ToInt();
336       if (i == 3)
337         swTitle = params[i].ToCFXWideString();
338     }
339   }
340
341   CJS_Context* pContext = (CJS_Context*)cc;
342   ASSERT(pContext != NULL);
343   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
344   ASSERT(pRuntime != NULL);
345   pRuntime->BeginBlock();
346   vRet = MsgBox(pRuntime->GetReaderApp(), JSGetPageView(cc), swMsg.c_str(),
347                 swTitle.c_str(), iType, iIcon);
348   pRuntime->EndBlock();
349
350   return TRUE;
351 }
352
353 FX_BOOL app::beep(IFXJS_Context* cc,
354                   const CJS_Parameters& params,
355                   CJS_Value& vRet,
356                   CFX_WideString& sError) {
357   if (params.size() == 1) {
358     CJS_Context* pContext = (CJS_Context*)cc;
359     CJS_Runtime* pRuntime = pContext->GetJSRuntime();
360     CPDFDoc_Environment* pEnv = pRuntime->GetReaderApp();
361     pEnv->JS_appBeep(params[0].ToInt());
362     return TRUE;
363   }
364
365   sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
366   return FALSE;
367 }
368
369 FX_BOOL app::findComponent(IFXJS_Context* cc,
370                            const CJS_Parameters& params,
371                            CJS_Value& vRet,
372                            CFX_WideString& sError) {
373   return TRUE;
374 }
375
376 FX_BOOL app::popUpMenuEx(IFXJS_Context* cc,
377                          const CJS_Parameters& params,
378                          CJS_Value& vRet,
379                          CFX_WideString& sError) {
380   return FALSE;
381 }
382
383 FX_BOOL app::fs(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
384   return FALSE;
385 }
386
387 FX_BOOL app::setInterval(IFXJS_Context* cc,
388                          const CJS_Parameters& params,
389                          CJS_Value& vRet,
390                          CFX_WideString& sError) {
391   CJS_Context* pContext = (CJS_Context*)cc;
392   if (params.size() > 2 || params.size() == 0) {
393     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
394     return FALSE;
395   }
396
397   CFX_WideString script = params.size() > 0 ? params[0].ToCFXWideString() : L"";
398   if (script.IsEmpty()) {
399     sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
400     return TRUE;
401   }
402
403   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
404   FX_DWORD dwInterval = params.size() > 1 ? params[1].ToInt() : 1000;
405
406   CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
407   ASSERT(pApp);
408   CJS_Timer* pTimer = new CJS_Timer(this, pApp);
409   m_aTimer.Add(pTimer);
410
411   pTimer->SetType(0);
412   pTimer->SetRuntime(pRuntime);
413   pTimer->SetJScript(script);
414   pTimer->SetTimeOut(0);
415   //    pTimer->SetStartTime(GetTickCount());
416   pTimer->SetJSTimer(dwInterval);
417
418   v8::Local<v8::Object> pRetObj =
419       JS_NewFxDynamicObj(pRuntime->GetIsolate(), pContext,
420                          JS_GetObjDefnID(pRuntime->GetIsolate(), L"TimerObj"));
421
422   CJS_TimerObj* pJS_TimerObj =
423       (CJS_TimerObj*)JS_GetPrivate(pRuntime->GetIsolate(), pRetObj);
424   ASSERT(pJS_TimerObj != NULL);
425
426   TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject();
427   ASSERT(pTimerObj != NULL);
428
429   pTimerObj->SetTimer(pTimer);
430
431   vRet = pRetObj;
432
433   return TRUE;
434 }
435
436 FX_BOOL app::setTimeOut(IFXJS_Context* cc,
437                         const CJS_Parameters& params,
438                         CJS_Value& vRet,
439                         CFX_WideString& sError) {
440   if (params.size() > 2 || params.size() == 0) {
441     sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
442     return FALSE;
443   }
444
445   CJS_Context* pContext = (CJS_Context*)cc;
446   ASSERT(pContext != NULL);
447   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
448   ASSERT(pRuntime != NULL);
449
450   CFX_WideString script = params.size() > 0 ? params[0].ToCFXWideString() : L"";
451   if (script.IsEmpty()) {
452     sError =
453         JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE);
454     return TRUE;
455   }
456
457   FX_DWORD dwTimeOut = params.size() > 1 ? params[1].ToInt() : 1000;
458
459   CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
460   ASSERT(pApp);
461
462   CJS_Timer* pTimer = new CJS_Timer(this, pApp);
463   m_aTimer.Add(pTimer);
464
465   pTimer->SetType(1);
466   pTimer->SetRuntime(pRuntime);
467   pTimer->SetJScript(script);
468   pTimer->SetTimeOut(dwTimeOut);
469   pTimer->SetJSTimer(dwTimeOut);
470
471   v8::Local<v8::Object> pRetObj =
472       JS_NewFxDynamicObj(pRuntime->GetIsolate(), pContext,
473                          JS_GetObjDefnID(pRuntime->GetIsolate(), L"TimerObj"));
474
475   CJS_TimerObj* pJS_TimerObj =
476       (CJS_TimerObj*)JS_GetPrivate(pRuntime->GetIsolate(), pRetObj);
477   ASSERT(pJS_TimerObj != NULL);
478
479   TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject();
480   ASSERT(pTimerObj != NULL);
481
482   pTimerObj->SetTimer(pTimer);
483
484   vRet = pRetObj;
485
486   return TRUE;
487 }
488
489 FX_BOOL app::clearTimeOut(IFXJS_Context* cc,
490                           const CJS_Parameters& params,
491                           CJS_Value& vRet,
492                           CFX_WideString& sError) {
493   CJS_Context* pContext = (CJS_Context*)cc;
494   ASSERT(pContext != NULL);
495   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
496   ASSERT(pRuntime != NULL);
497
498   if (params.size() != 1) {
499     sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
500     return FALSE;
501   }
502
503   if (params[0].GetType() == VT_fxobject) {
504     v8::Local<v8::Object> pObj = params[0].ToV8Object();
505     {
506       if (JS_GetObjDefnID(pObj) ==
507           JS_GetObjDefnID(pRuntime->GetIsolate(), L"TimerObj")) {
508         if (CJS_Object* pJSObj = params[0].ToCJSObject()) {
509           if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject()) {
510             if (CJS_Timer* pTimer = pTimerObj->GetTimer()) {
511               pTimer->KillJSTimer();
512
513               for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) {
514                 if (m_aTimer[i] == pTimer) {
515                   m_aTimer.RemoveAt(i);
516                   break;
517                 }
518               }
519
520               delete pTimer;
521               pTimerObj->SetTimer(NULL);
522             }
523           }
524         }
525       }
526     }
527   }
528
529   return TRUE;
530 }
531
532 FX_BOOL app::clearInterval(IFXJS_Context* cc,
533                            const CJS_Parameters& params,
534                            CJS_Value& vRet,
535                            CFX_WideString& sError) {
536   CJS_Context* pContext = (CJS_Context*)cc;
537   ASSERT(pContext != NULL);
538   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
539   ASSERT(pRuntime != NULL);
540
541   if (params.size() != 1) {
542     sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
543     return FALSE;
544   }
545
546   if (params[0].GetType() == VT_fxobject) {
547     v8::Local<v8::Object> pObj = params[0].ToV8Object();
548     {
549       if (JS_GetObjDefnID(pObj) ==
550           JS_GetObjDefnID(pRuntime->GetIsolate(), L"TimerObj")) {
551         if (CJS_Object* pJSObj = params[0].ToCJSObject()) {
552           if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject()) {
553             if (CJS_Timer* pTimer = pTimerObj->GetTimer()) {
554               pTimer->KillJSTimer();
555
556               for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) {
557                 if (m_aTimer[i] == pTimer) {
558                   m_aTimer.RemoveAt(i);
559                   break;
560                 }
561               }
562
563               delete pTimer;
564               pTimerObj->SetTimer(NULL);
565             }
566           }
567         }
568       }
569     }
570   }
571
572   return TRUE;
573 }
574
575 FX_BOOL app::execMenuItem(IFXJS_Context* cc,
576                           const CJS_Parameters& params,
577                           CJS_Value& vRet,
578                           CFX_WideString& sError) {
579   return FALSE;
580 }
581
582 void app::TimerProc(CJS_Timer* pTimer) {
583   ASSERT(pTimer != NULL);
584
585   switch (pTimer->GetType()) {
586     case 0:  // interval
587       RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript());
588       break;
589     case 1:
590       if (pTimer->GetTimeOut() > 0) {
591         RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript());
592         pTimer->KillJSTimer();
593       }
594       break;
595   }
596 }
597
598 void app::RunJsScript(CJS_Runtime* pRuntime, const CFX_WideString& wsScript) {
599   ASSERT(pRuntime != NULL);
600
601   if (!pRuntime->IsBlocking()) {
602     IFXJS_Context* pContext = pRuntime->NewContext();
603     ASSERT(pContext != NULL);
604     pContext->OnExternal_Exec();
605     CFX_WideString wtInfo;
606     pContext->RunScript(wsScript, wtInfo);
607     pRuntime->ReleaseContext(pContext);
608   }
609 }
610
611 FX_BOOL app::goBack(IFXJS_Context* cc,
612                     const CJS_Parameters& params,
613                     CJS_Value& vRet,
614                     CFX_WideString& sError) {
615   // Not supported.
616   return TRUE;
617 }
618
619 FX_BOOL app::goForward(IFXJS_Context* cc,
620                        const CJS_Parameters& params,
621                        CJS_Value& vRet,
622                        CFX_WideString& sError) {
623   // Not supported.
624   return TRUE;
625 }
626
627 FX_BOOL app::mailMsg(IFXJS_Context* cc,
628                      const CJS_Parameters& params,
629                      CJS_Value& vRet,
630                      CFX_WideString& sError) {
631   CJS_Context* pContext = (CJS_Context*)cc;
632   v8::Isolate* isolate = GetIsolate(cc);
633
634   FX_BOOL bUI = TRUE;
635   CFX_WideString cTo = L"";
636   CFX_WideString cCc = L"";
637   CFX_WideString cBcc = L"";
638   CFX_WideString cSubject = L"";
639   CFX_WideString cMsg = L"";
640
641   if (params.size() < 1)
642     return FALSE;
643
644   if (params[0].GetType() == VT_object) {
645     v8::Local<v8::Object> pObj = params[0].ToV8Object();
646
647     v8::Local<v8::Value> pValue = JS_GetObjectElement(isolate, pObj, L"bUI");
648     bUI = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToBool();
649
650     pValue = JS_GetObjectElement(isolate, pObj, L"cTo");
651     cTo = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
652
653     pValue = JS_GetObjectElement(isolate, pObj, L"cCc");
654     cCc = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
655
656     pValue = JS_GetObjectElement(isolate, pObj, L"cBcc");
657     cBcc = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
658
659     pValue = JS_GetObjectElement(isolate, pObj, L"cSubject");
660     cSubject =
661         CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
662
663     pValue = JS_GetObjectElement(isolate, pObj, L"cMsg");
664     cMsg = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
665   } else {
666     if (params.size() < 2)
667       return FALSE;
668
669     bUI = params[0].ToBool();
670     cTo = params[1].ToCFXWideString();
671
672     if (params.size() >= 3)
673       cCc = params[2].ToCFXWideString();
674     if (params.size() >= 4)
675       cBcc = params[3].ToCFXWideString();
676     if (params.size() >= 5)
677       cSubject = params[4].ToCFXWideString();
678     if (params.size() >= 6)
679       cMsg = params[5].ToCFXWideString();
680   }
681
682   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
683   ASSERT(pRuntime != NULL);
684
685   CPDFDoc_Environment* pApp = pContext->GetReaderApp();
686   ASSERT(pApp != NULL);
687
688   pRuntime->BeginBlock();
689   pApp->JS_docmailForm(NULL, 0, bUI, cTo.c_str(), cSubject.c_str(), cCc.c_str(),
690                        cBcc.c_str(), cMsg.c_str());
691   pRuntime->EndBlock();
692
693   return FALSE;
694 }
695
696 FX_BOOL app::launchURL(IFXJS_Context* cc,
697                        const CJS_Parameters& params,
698                        CJS_Value& vRet,
699                        CFX_WideString& sError) {
700   // Unsafe, not supported.
701   return TRUE;
702 }
703
704 FX_BOOL app::runtimeHighlight(IFXJS_Context* cc,
705                               CJS_PropValue& vp,
706                               CFX_WideString& sError) {
707   if (vp.IsSetting()) {
708     vp >> m_bRuntimeHighLight;
709   } else {
710     vp << m_bRuntimeHighLight;
711   }
712
713   return TRUE;
714 }
715
716 FX_BOOL app::fullscreen(IFXJS_Context* cc,
717                         CJS_PropValue& vp,
718                         CFX_WideString& sError) {
719   return FALSE;
720 }
721
722 FX_BOOL app::popUpMenu(IFXJS_Context* cc,
723                        const CJS_Parameters& params,
724                        CJS_Value& vRet,
725                        CFX_WideString& sError) {
726   return FALSE;
727 }
728
729 FX_BOOL app::browseForDoc(IFXJS_Context* cc,
730                           const CJS_Parameters& params,
731                           CJS_Value& vRet,
732                           CFX_WideString& sError) {
733   // Unsafe, not supported.
734   return TRUE;
735 }
736
737 CFX_WideString app::SysPathToPDFPath(const CFX_WideString& sOldPath) {
738   CFX_WideString sRet = L"/";
739
740   for (int i = 0, sz = sOldPath.GetLength(); i < sz; i++) {
741     wchar_t c = sOldPath.GetAt(i);
742     if (c == L':') {
743     } else {
744       if (c == L'\\') {
745         sRet += L"/";
746       } else {
747         sRet += c;
748       }
749     }
750   }
751
752   return sRet;
753 }
754
755 FX_BOOL app::newDoc(IFXJS_Context* cc,
756                     const CJS_Parameters& params,
757                     CJS_Value& vRet,
758                     CFX_WideString& sError) {
759   return FALSE;
760 }
761
762 FX_BOOL app::openDoc(IFXJS_Context* cc,
763                      const CJS_Parameters& params,
764                      CJS_Value& vRet,
765                      CFX_WideString& sError) {
766   return FALSE;
767 }
768
769 FX_BOOL app::response(IFXJS_Context* cc,
770                       const CJS_Parameters& params,
771                       CJS_Value& vRet,
772                       CFX_WideString& sError) {
773   CFX_WideString swQuestion = L"";
774   CFX_WideString swLabel = L"";
775   CFX_WideString swTitle = L"PDF";
776   CFX_WideString swDefault = L"";
777   bool bPassWord = false;
778
779   v8::Isolate* isolate = GetIsolate(cc);
780
781   int iLength = params.size();
782   if (iLength > 0 && params[0].GetType() == VT_object) {
783     v8::Local<v8::Object> pObj = params[0].ToV8Object();
784     v8::Local<v8::Value> pValue =
785         JS_GetObjectElement(isolate, pObj, L"cQuestion");
786     swQuestion =
787         CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
788
789     pValue = JS_GetObjectElement(isolate, pObj, L"cTitle");
790     swTitle =
791         CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
792
793     pValue = JS_GetObjectElement(isolate, pObj, L"cDefault");
794     swDefault =
795         CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
796
797     pValue = JS_GetObjectElement(isolate, pObj, L"cLabel");
798     swLabel =
799         CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToCFXWideString();
800
801     pValue = JS_GetObjectElement(isolate, pObj, L"bPassword");
802     bPassWord = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue)).ToBool();
803   } else {
804     switch (iLength) {
805       case 5:
806         swLabel = params[4].ToCFXWideString();
807       // FALLTHROUGH
808       case 4:
809         bPassWord = params[3].ToBool();
810       // FALLTHROUGH
811       case 3:
812         swDefault = params[2].ToCFXWideString();
813       // FALLTHROUGH
814       case 2:
815         swTitle = params[1].ToCFXWideString();
816       // FALLTHROUGH
817       case 1:
818         swQuestion = params[0].ToCFXWideString();
819       // FALLTHROUGH
820       default:
821         break;
822     }
823   }
824
825   CJS_Context* pContext = (CJS_Context*)cc;
826   ASSERT(pContext != NULL);
827
828   CPDFDoc_Environment* pApp = pContext->GetReaderApp();
829   ASSERT(pApp != NULL);
830
831   const int MAX_INPUT_BYTES = 2048;
832   nonstd::unique_ptr<char[]> pBuff(new char[MAX_INPUT_BYTES + 2]);
833   memset(pBuff.get(), 0, MAX_INPUT_BYTES + 2);
834   int nLengthBytes = pApp->JS_appResponse(
835       swQuestion.c_str(), swTitle.c_str(), swDefault.c_str(), swLabel.c_str(),
836       bPassWord, pBuff.get(), MAX_INPUT_BYTES);
837   if (nLengthBytes <= 0) {
838     vRet.SetNull();
839     return FALSE;
840   }
841   nLengthBytes = std::min(nLengthBytes, MAX_INPUT_BYTES);
842
843   CFX_WideString ret_string = CFX_WideString::FromUTF16LE(
844       (unsigned short*)pBuff.get(), nLengthBytes / sizeof(unsigned short));
845   vRet = ret_string.c_str();
846   return TRUE;
847 }
848
849 FX_BOOL app::media(IFXJS_Context* cc,
850                    CJS_PropValue& vp,
851                    CFX_WideString& sError) {
852   return FALSE;
853 }
854
855 FX_BOOL app::execDialog(IFXJS_Context* cc,
856                         const CJS_Parameters& params,
857                         CJS_Value& vRet,
858                         CFX_WideString& sError) {
859   return TRUE;
860 }