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