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