Simplify PDFium by removing code that's not used in the open source repo.
[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 "../../include/javascript/JavaScript.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
20 static v8::Isolate* GetIsolate(IFXJS_Context* cc)
21 {
22         CJS_Context* pContext = (CJS_Context *)cc;
23         ASSERT(pContext != NULL);
24
25         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
26         ASSERT(pRuntime != NULL);
27
28         return pRuntime->GetIsolate();
29 }
30
31 /* ---------------------------- TimerObj ---------------------------- */
32
33 BEGIN_JS_STATIC_CONST(CJS_TimerObj)
34 END_JS_STATIC_CONST()
35
36 BEGIN_JS_STATIC_PROP(CJS_TimerObj)
37 END_JS_STATIC_PROP()
38
39 BEGIN_JS_STATIC_METHOD(CJS_TimerObj)
40 END_JS_STATIC_METHOD()
41
42 IMPLEMENT_JS_CLASS(CJS_TimerObj, TimerObj)
43
44 TimerObj::TimerObj(CJS_Object* pJSObject)
45 : CJS_EmbedObj(pJSObject),
46 m_pTimer(NULL)
47 {
48         
49 }
50
51 TimerObj::~TimerObj()
52 {
53 }
54
55 void TimerObj::SetTimer(CJS_Timer* pTimer)
56 {
57         m_pTimer = pTimer;
58 }
59
60 CJS_Timer* TimerObj::GetTimer() const
61 {
62         return m_pTimer;
63 }
64
65 #define JS_STR_VIEWERTYPE_READER                L"Reader"
66 #define JS_STR_VIEWERTYPE_STANDARD              L"Exchange"
67 #define JS_STR_VIEWERVARIATION                  L"Full"
68 #define JS_STR_PLATFORM                                 L"WIN"
69 #define JS_STR_LANGUANGE                                L"ENU"
70 #define JS_STR_VIEWERVERSION                    8
71 #define JS_NUM_FORMSVERSION                             7
72
73 #define JS_FILEPATH_MAXLEN                              2000
74
75 /* ---------------------------- app ---------------------------- */
76
77 BEGIN_JS_STATIC_CONST(CJS_App)
78 END_JS_STATIC_CONST()
79
80 BEGIN_JS_STATIC_PROP(CJS_App)
81         JS_STATIC_PROP_ENTRY(activeDocs)
82         JS_STATIC_PROP_ENTRY(calculate)
83         JS_STATIC_PROP_ENTRY(formsVersion)
84         JS_STATIC_PROP_ENTRY(fs)
85         JS_STATIC_PROP_ENTRY(fullscreen)
86         JS_STATIC_PROP_ENTRY(language)
87         JS_STATIC_PROP_ENTRY(media)
88         JS_STATIC_PROP_ENTRY(platform)
89         JS_STATIC_PROP_ENTRY(runtimeHighlight)
90         JS_STATIC_PROP_ENTRY(viewerType)
91         JS_STATIC_PROP_ENTRY(viewerVariation)
92         JS_STATIC_PROP_ENTRY(viewerVersion)     
93 END_JS_STATIC_PROP()
94
95 BEGIN_JS_STATIC_METHOD(CJS_App)
96         JS_STATIC_METHOD_ENTRY(alert, 6)
97         JS_STATIC_METHOD_ENTRY(beep, 1)
98         JS_STATIC_METHOD_ENTRY(browseForDoc, 0)
99         JS_STATIC_METHOD_ENTRY(clearInterval, 1)
100         JS_STATIC_METHOD_ENTRY(clearTimeOut, 1)
101         JS_STATIC_METHOD_ENTRY(execDialog, 3)
102         JS_STATIC_METHOD_ENTRY(execMenuItem,  1)
103         JS_STATIC_METHOD_ENTRY(findComponent, 1)
104         JS_STATIC_METHOD_ENTRY(goBack, 0)
105         JS_STATIC_METHOD_ENTRY(goForward, 0)
106         JS_STATIC_METHOD_ENTRY(launchURL, 0)
107         JS_STATIC_METHOD_ENTRY(mailMsg, 0)      
108         JS_STATIC_METHOD_ENTRY(newFDF, 0)
109         JS_STATIC_METHOD_ENTRY(newDoc, 0)
110         JS_STATIC_METHOD_ENTRY(openDoc, 0)
111         JS_STATIC_METHOD_ENTRY(openFDF, 5)
112         JS_STATIC_METHOD_ENTRY(popUpMenuEx, 0)
113         JS_STATIC_METHOD_ENTRY(popUpMenu, 0)
114         JS_STATIC_METHOD_ENTRY(response, 0)
115         JS_STATIC_METHOD_ENTRY(setInterval, 2)
116         JS_STATIC_METHOD_ENTRY(setTimeOut, 2)
117 END_JS_STATIC_METHOD()
118
119 IMPLEMENT_JS_CLASS(CJS_App,app)
120
121 app::app(CJS_Object * pJSObject) : CJS_EmbedObj(pJSObject) ,
122         m_bCalculate(true),
123         m_bRuntimeHighLight(false)
124 //      m_pMenuHead(NULL)
125 {
126 }
127
128 app::~app(void)
129 {
130         for (int i=0,sz=m_aTimer.GetSize(); i<sz; i++)
131                 delete m_aTimer[i];
132
133         m_aTimer.RemoveAll();
134 }
135
136 FX_BOOL app::activeDocs(OBJ_PROP_PARAMS)
137 {
138         if (vp.IsGetting())
139         {
140
141                 CJS_Context* pContext = (CJS_Context *)cc;
142                 ASSERT(pContext != NULL);
143                 
144                 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
145                 ASSERT(pApp != NULL);
146
147                 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
148                 ASSERT(pRuntime != NULL);
149                 
150                 CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument();
151                 
152                 CJS_Array aDocs(pRuntime->GetIsolate());
153 //              int iNumDocs = pApp->CountDocuments();
154                 
155 //              for(int iIndex = 0; iIndex<iNumDocs; iIndex++)
156 //              {
157                         CPDFSDK_Document* pDoc = pApp->GetCurrentDoc();
158                         if (pDoc)
159                         {
160                                 CJS_Document * pJSDocument = NULL;
161                                 
162                                 if (pDoc == pCurDoc)
163                                 {
164                                         JSFXObject pObj = JS_GetThisObj(*pRuntime);
165                                         
166                                         if (JS_GetObjDefnID(pObj) == JS_GetObjDefnID(*pRuntime, L"Document"))
167                                         {
168                                                 pJSDocument = (CJS_Document*)JS_GetPrivate(pRuntime->GetIsolate(),pObj);
169                                         }
170                                 }
171                                 else
172                                 {
173                                         JSFXObject pObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime,L"Document"));
174                                         pJSDocument = (CJS_Document*)JS_GetPrivate(pRuntime->GetIsolate(),pObj);
175                                         ASSERT(pJSDocument != NULL);
176                                         
177                                         
178                                         //                      pDocument->AttachDoc(pDoc);
179                                 }
180                                 
181                                 aDocs.SetElement(0,CJS_Value(pRuntime->GetIsolate(),pJSDocument));
182                         }
183         //              }
184                 
185                 if (aDocs.GetLength() > 0)
186                         vp << aDocs;
187                 else
188                         vp.SetNull();
189                 return TRUE;
190         }
191         return FALSE;
192 }
193
194 FX_BOOL app::calculate(OBJ_PROP_PARAMS)
195 {
196         if (vp.IsSetting())
197         {
198                 bool bVP;
199                 vp >> bVP;
200                 m_bCalculate = (FX_BOOL)bVP;
201
202                 CJS_Context* pContext = (CJS_Context*)cc;
203                 ASSERT(pContext != NULL);
204                 
205                 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
206                 ASSERT(pApp != NULL);
207                 
208                 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
209                 ASSERT(pRuntime != NULL);
210
211                 CJS_Array aDocs(pRuntime->GetIsolate());
212 //              int iNumDocs = pApp->CountDocuments();
213 //              
214 //              for (int iIndex = 0;iIndex < iNumDocs; iIndex++)
215 //              {
216                         if (CPDFSDK_Document* pDoc = pApp->GetCurrentDoc())
217                         {
218                                 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDoc->GetInterForm();
219                                 ASSERT(pInterForm != NULL);
220                                 pInterForm->EnableCalculate((FX_BOOL)m_bCalculate);
221                         }                       
222 //              }
223         }
224         else
225         {
226                 vp << (bool)m_bCalculate;
227         }
228         
229         return TRUE;
230 }
231
232 FX_BOOL app::formsVersion(OBJ_PROP_PARAMS)
233 {
234         if (vp.IsGetting())
235         {
236                 vp << JS_NUM_FORMSVERSION;
237                 return TRUE;
238         }
239         
240         return FALSE;
241 }
242
243 FX_BOOL app::viewerType(OBJ_PROP_PARAMS)
244 {
245
246
247         
248         
249         
250
251         if (vp.IsGetting())
252         {
253 //              if (pApp->GetAppName() == PHANTOM)
254 //                      vp << JS_STR_VIEWERTYPE_STANDARD;
255 //              else
256 //                      vp << JS_STR_VIEWERTYPE_READER;
257                 vp << L"unknown";
258
259                 //vp << pApp->GetAppTitle();
260                 return TRUE;
261         }
262         
263         return FALSE;
264 }
265
266 FX_BOOL app::viewerVariation(OBJ_PROP_PARAMS)
267 {
268         if (vp.IsGetting())
269         {
270                 vp << JS_STR_VIEWERVARIATION;
271                 return TRUE;
272         }
273         
274         return FALSE;
275 }
276
277 FX_BOOL app::viewerVersion(OBJ_PROP_PARAMS)
278 {
279         if (vp.IsGetting())
280         {
281                 vp << JS_STR_VIEWERVERSION;
282                 return TRUE;
283         }
284         
285         return FALSE;
286 }
287
288 FX_BOOL app::platform(OBJ_PROP_PARAMS)
289 {
290         if (vp.IsGetting())
291         {
292                 vp << JS_STR_PLATFORM;
293                 return TRUE;
294         }
295         
296         return FALSE;
297 }
298
299 FX_BOOL app::language(OBJ_PROP_PARAMS)
300 {
301         if (vp.IsGetting())
302         {
303                 vp << JS_STR_LANGUANGE;
304                 return TRUE;
305         }
306         
307         return FALSE;
308 }
309
310 //creates a new fdf object that contains no data
311 //comment: need reader support
312 //note:
313 //CFDF_Document * CPDFDoc_Environment::NewFDF();
314 FX_BOOL app::newFDF(OBJ_METHOD_PARAMS)
315 {
316         return TRUE;
317 }
318 //opens a specified pdf document and returns its document object
319 //comment:need reader support
320 //note: as defined in js reference, the proto of this function's fourth parmeters, how old an fdf document while do not show it.
321 //CFDF_Document * CPDFDoc_Environment::OpenFDF(string strPath,bool bUserConv);
322
323 FX_BOOL app::openFDF(OBJ_METHOD_PARAMS)
324 {
325         return TRUE;
326 }
327
328 FX_BOOL app::alert(OBJ_METHOD_PARAMS)
329 {
330         int iSize = params.size();
331         if (iSize < 1)
332                 return FALSE;
333
334         CFX_WideString swMsg = L"";
335         CFX_WideString swTitle = L"";
336         int iIcon = 0;
337         int iType = 0;
338
339         v8::Isolate* isolate = GetIsolate(cc);
340
341         if (iSize == 1)
342         {
343                 if (params[0].GetType() == VT_object)
344                 {
345                         JSObject pObj = params[0];
346                         {
347                                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate, pObj, L"cMsg");
348                                 swMsg = CJS_Value(isolate,pValue,VT_unknown).operator CFX_WideString();
349
350                                 pValue = JS_GetObjectElement(isolate,pObj,L"cTitle");
351                                 swTitle = CJS_Value(isolate, pValue,VT_unknown).operator CFX_WideString();
352
353                                 pValue = JS_GetObjectElement(isolate,pObj,L"nIcon");
354                                 iIcon = (int)CJS_Value(isolate,pValue,VT_unknown);
355
356                                 pValue = JS_GetObjectElement(isolate,pObj,L"nType");
357                                 iType = (int)CJS_Value(isolate,pValue,VT_unknown);
358                         }
359
360                         if (swMsg == L"")
361                         {
362                                 CJS_Array carray(isolate);
363                                 if (params[0].ConvertToArray(carray))
364                                 {
365                                         int iLenth = carray.GetLength();
366                                         CJS_Value* pValue = new CJS_Value(isolate);
367 //                                      if (iLenth == 1)
368 //                                              pValue = new CJS_Value(isolate);
369 //                                      else if (iLenth > 1)
370 //                                              pValue = new CJS_Value[iLenth];
371
372                                         for(int i = 0; i < iLenth; i++)
373                                         {
374                                                 carray.GetElement(i, *pValue);
375                                                 swMsg += (*pValue).operator CFX_WideString();
376                                                 if (i < iLenth - 1)
377                                                         swMsg += L",  ";
378                                         }
379
380                                         if(pValue) delete pValue;
381 //                                      if ((iLenth > 1) && pValue)
382 //                                      {
383 //                                              delete[]pValue;
384 //                                              pValue = NULL;
385 //                                      }
386 //                                      else if ((iLenth == 1) && pValue)
387 //                                      {
388 //                                              delete pValue;
389 //                                              pValue = NULL;
390 //                                      }
391                                 }
392                         }
393
394                         if (swTitle == L"")
395                                 swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
396                 }
397                 else if (params[0].GetType() == VT_boolean)
398                 {
399                         FX_BOOL bGet = (FX_BOOL)params[0];
400                         if (bGet)
401                                 swMsg = L"true";
402                         else
403                                 swMsg = L"false";
404
405                         swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
406                 }
407                 else
408                 {
409                         swMsg = params[0];
410                         swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
411                 }
412         }
413         else
414         {
415                 if (params[0].GetType() == VT_boolean)
416                 {
417                         FX_BOOL bGet = (FX_BOOL)params[0];
418                         if (bGet)
419                                 swMsg = L"true";
420                         else
421                                 swMsg = L"false";
422                 }
423                 else
424                 {
425                         swMsg = params[0];
426                 }
427                 swTitle = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSALERT);
428                 
429                 for(int i = 1;i<iSize;i++)
430                 {
431                         if (i == 1)
432                                 iIcon = int(params[i]);
433                         if (i == 2)
434                                 iType = int(params[i]);
435                         if (i == 3)
436                                 swTitle = params[i];                    
437                 }
438         }
439
440
441         CJS_Context* pContext = (CJS_Context*)cc;
442         ASSERT(pContext != NULL);
443         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
444         ASSERT(pRuntime != NULL);
445         pRuntime->BeginBlock();
446         vRet = MsgBox(pRuntime->GetReaderApp(), JSGetPageView(cc),swMsg,swTitle,iType,iIcon);
447         pRuntime->EndBlock();
448         
449         return TRUE;
450 }
451
452
453 FX_BOOL app::beep(OBJ_METHOD_PARAMS)
454 {
455         if (params.size() == 1)
456         {
457                 CJS_Context* pContext = (CJS_Context*)cc;
458                 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
459                 CPDFDoc_Environment * pEnv = pRuntime->GetReaderApp();
460                 pEnv->JS_appBeep((int)params[0]);
461
462                 return TRUE;
463         }
464         else
465         {
466                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
467                 return FALSE;
468         }
469 }
470
471 FX_BOOL app::findComponent(OBJ_METHOD_PARAMS)
472 {
473         return TRUE;
474 }
475
476 FX_BOOL app::popUpMenuEx(OBJ_METHOD_PARAMS)
477 {       
478         return FALSE;
479 }
480
481 FX_BOOL app::fs(OBJ_PROP_PARAMS)
482 {
483         return FALSE;
484 }
485
486 FX_BOOL app::setInterval(OBJ_METHOD_PARAMS)
487 {
488         if (params.size() > 2 || params.size() == 0) 
489         {
490                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
491                 return FALSE;
492         }
493         
494         CJS_Context* pContext = (CJS_Context*)cc;
495         ASSERT(pContext != NULL);
496         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
497         ASSERT(pRuntime != NULL);
498
499         CFX_WideString script = params.size() > 0 ?  (FX_LPCWSTR)(params[0].operator CFX_WideString()) : L"";
500         if (script.IsEmpty()) 
501         {
502                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE);
503                 return TRUE;
504         }
505
506         FX_DWORD dwInterval = params.size() > 1 ? (int)params[1] : 1000;
507         
508         CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
509         ASSERT(pApp);
510         CJS_Timer* pTimer = new CJS_Timer(this, pApp);
511         m_aTimer.Add(pTimer);
512
513         pTimer->SetType(0);
514         pTimer->SetRuntime(pRuntime);
515         pTimer->SetJScript(script);
516         pTimer->SetTimeOut(0);
517 //      pTimer->SetStartTime(GetTickCount());
518         pTimer->SetJSTimer(dwInterval);
519         
520         JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"TimerObj"));
521         
522         CJS_TimerObj* pJS_TimerObj = (CJS_TimerObj*)JS_GetPrivate(pRuntime->GetIsolate(),pRetObj);
523         ASSERT(pJS_TimerObj != NULL);
524         
525         TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject();
526         ASSERT(pTimerObj != NULL);
527         
528         pTimerObj->SetTimer(pTimer); 
529         
530         vRet = pRetObj;
531         
532         return TRUE;
533 }
534
535 FX_BOOL app::setTimeOut(OBJ_METHOD_PARAMS)
536 {
537         if (params.size() > 2 || params.size() == 0)
538         {
539                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
540                 return FALSE;
541         }
542         
543         CJS_Context* pContext = (CJS_Context*)cc;
544         ASSERT(pContext != NULL);
545         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
546         ASSERT(pRuntime != NULL);
547         
548         CFX_WideString script = params.size() > 0 ?  (FX_LPCWSTR)(params[0].operator CFX_WideString()) : L"";
549         if (script.IsEmpty()) 
550         {
551                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE);
552                 return TRUE;
553         }
554         
555         FX_DWORD dwTimeOut = params.size() > 1 ? (int)params[1] : 1000;
556         
557         CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
558         ASSERT(pApp);
559         CJS_Timer* pTimer = new CJS_Timer(this, pApp);
560         m_aTimer.Add(pTimer);
561         
562         pTimer->SetType(1);
563         pTimer->SetRuntime(pRuntime);
564         pTimer->SetJScript(script);
565         pTimer->SetTimeOut(dwTimeOut);
566 //      pTimer->SetStartTime(GetTickCount());
567 //      pTimer->SetJSTimer(1000);
568         pTimer->SetJSTimer(dwTimeOut);
569         
570         JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"TimerObj"));
571 //      ASSERT(pRetObj != NULL);
572         
573         CJS_TimerObj* pJS_TimerObj = (CJS_TimerObj*)JS_GetPrivate(pRuntime->GetIsolate(),pRetObj);
574         ASSERT(pJS_TimerObj != NULL);
575         
576         TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject();
577         ASSERT(pTimerObj != NULL);
578         
579         pTimerObj->SetTimer(pTimer); 
580         
581         vRet = pRetObj;
582         
583         return TRUE;
584 }
585
586 FX_BOOL app::clearTimeOut(OBJ_METHOD_PARAMS)
587 {
588         CJS_Context* pContext = (CJS_Context*)cc;
589         ASSERT(pContext != NULL);
590         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
591         ASSERT(pRuntime != NULL);
592         
593         if (params.size() != 1)
594         {
595                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
596                 return FALSE;
597         }
598         
599         if (params[0].GetType() == VT_fxobject)
600         {
601                 JSFXObject pObj = (JSFXObject)params[0];
602                 {
603                         if (JS_GetObjDefnID(pObj) == JS_GetObjDefnID(*pRuntime, L"TimerObj"))
604                         {
605                                 if (CJS_Object* pJSObj = (CJS_Object*)params[0])
606                                 {
607                                         if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject())
608                                         {
609                                                 if (CJS_Timer* pTimer = pTimerObj->GetTimer())
610                                                 {
611                                                         pTimer->KillJSTimer();
612                                                         
613                                                         for (int i=0,sz=m_aTimer.GetSize(); i<sz; i++)
614                                                         {
615                                                                 if (m_aTimer[i] == pTimer)
616                                                                 {
617                                                                         m_aTimer.RemoveAt(i);
618                                                                         break;
619                                                                 }
620                                                         }
621                                                         
622                                                         delete pTimer;
623                                                         pTimerObj->SetTimer(NULL);
624                                                 }
625                                         }
626                                 }
627                         }
628                 }
629         }
630         
631         return TRUE;
632 }
633
634 FX_BOOL app::clearInterval(OBJ_METHOD_PARAMS)
635 {
636         CJS_Context* pContext = (CJS_Context*)cc;
637         ASSERT(pContext != NULL);
638         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
639         ASSERT(pRuntime != NULL);
640
641         if (params.size() != 1)
642         {
643                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
644                 return FALSE;
645         }
646         
647         if (params[0].GetType() == VT_fxobject)
648         {
649                 JSFXObject pObj = (JSFXObject)params[0];
650                 {
651                         if (JS_GetObjDefnID(pObj) == JS_GetObjDefnID(*pRuntime, L"TimerObj"))
652                         {
653                                 if (CJS_Object* pJSObj = (CJS_Object*)params[0])
654                                 {
655                                         if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject())
656                                         {
657                                                 if (CJS_Timer* pTimer = pTimerObj->GetTimer())
658                                                 {
659                                                         pTimer->KillJSTimer();
660                                                         
661                                                         for (int i=0,sz=m_aTimer.GetSize(); i<sz; i++)
662                                                         {
663                                                                 if (m_aTimer[i] == pTimer)
664                                                                 {
665                                                                         m_aTimer.RemoveAt(i);
666                                                                         break;
667                                                                 }
668                                                         }
669                                                         
670                                                         delete pTimer;
671                                                         pTimerObj->SetTimer(NULL);
672                                                 }
673                                         }
674                                 }
675                         }
676                 }
677         }
678         
679         return TRUE;
680 }
681
682 FX_BOOL app::execMenuItem(OBJ_METHOD_PARAMS)
683 {       
684         return FALSE;
685 }
686
687 void app::TimerProc(CJS_Timer* pTimer)
688 {
689         ASSERT(pTimer != NULL);
690
691         switch (pTimer->GetType())
692         {
693         case 0: //interval
694                 RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript());
695                 break;
696         case 1:
697                 if (pTimer->GetTimeOut() > 0)
698                 {
699                         RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript());
700                         pTimer->KillJSTimer();
701                 }
702                 break;
703         }
704         
705 }
706
707 void app::RunJsScript(CJS_Runtime* pRuntime,const CFX_WideString& wsScript)
708 {
709         ASSERT(pRuntime != NULL);
710
711         if (!pRuntime->IsBlocking())
712         {
713                 IFXJS_Context* pContext = pRuntime->NewContext();
714                 ASSERT(pContext != NULL);
715                 pContext->OnExternal_Exec();
716                 CFX_WideString wtInfo;
717                 pContext->RunScript(wsScript,wtInfo);
718                 pRuntime->ReleaseContext(pContext);
719         }
720 }
721
722 FX_BOOL app::goBack(OBJ_METHOD_PARAMS)
723 {
724
725
726         
727         
728         
729         
730         return TRUE;
731 }
732
733 FX_BOOL app::goForward(OBJ_METHOD_PARAMS)
734 {       
735
736
737
738
739
740
741         return TRUE;
742 }
743
744 FX_BOOL app::mailMsg(OBJ_METHOD_PARAMS)
745 {
746         CJS_Context* pContext = (CJS_Context*)cc;
747         ASSERT(pContext != NULL);
748
749         v8::Isolate* isolate = GetIsolate(cc);
750
751         FX_BOOL bUI = TRUE;
752         CFX_WideString cTo = L"";
753         CFX_WideString cCc = L"";
754         CFX_WideString cBcc = L"";
755         CFX_WideString cSubject = L"";
756         CFX_WideString cMsg = L"";
757         if(params.size() < 2)
758                 return FALSE;
759
760         bUI = params.size()>=1?(int)params[0]:TRUE;
761         cTo = params.size()>=2?(const wchar_t*)(FX_LPCWSTR)params[1].operator CFX_WideString():L"";
762         cCc = params.size()>=3?(const wchar_t*)(FX_LPCWSTR)params[2].operator CFX_WideString():L"";
763         cBcc = params.size()>=4?(const wchar_t*)(FX_LPCWSTR)params[3].operator CFX_WideString():L"";
764         cSubject = params.size()>=5?(const wchar_t*)(FX_LPCWSTR)params[4].operator CFX_WideString():L"";
765         cMsg = params.size()>=6?(const wchar_t*)(FX_LPCWSTR)params[5].operator CFX_WideString():L"";            
766
767
768         if (params[0].GetType() == VT_object)
769         {
770                 JSObject pObj = (JSObject)params[0];
771
772                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj, L"bUI");
773                         bUI = (int)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
774
775                 pValue = JS_GetObjectElement(isolate, pObj, L"cTo");
776                         cTo = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
777
778                 pValue = JS_GetObjectElement(isolate,pObj, L"cCc");
779                         cCc = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
780
781                 pValue = JS_GetObjectElement(isolate,pObj, L"cBcc");
782                         cBcc = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
783
784                 pValue = JS_GetObjectElement(isolate,pObj, L"cSubject");
785                         cSubject = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
786
787                 pValue = JS_GetObjectElement(isolate,pObj, L"cMsg");
788                         cMsg = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
789         }
790         
791         
792
793         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
794         ASSERT(pRuntime != NULL);
795
796         CPDFDoc_Environment* pApp = pContext->GetReaderApp();
797         ASSERT(pApp != NULL);
798
799         pRuntime->BeginBlock();
800         pApp->JS_docmailForm(NULL, 0, bUI, cTo.c_str(), cSubject.c_str(), cCc.c_str(), cBcc.c_str(), cMsg.c_str());
801         ///////////////////////////////////////////////////////////////////////////////////////////////
802         pRuntime->EndBlock();
803
804         //return bRet;
805         return FALSE;
806 }
807
808 FX_BOOL app::launchURL(OBJ_METHOD_PARAMS)
809 {
810         if (IsSafeMode(cc)) return TRUE;
811
812         CJS_Context* pContext = (CJS_Context*)cc;
813         ASSERT(pContext != NULL);
814
815         
816
817
818         CFX_WideString swURL = params[0].operator CFX_WideString();
819
820         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
821         ASSERT(pRuntime != NULL);
822
823         pRuntime->BeginBlock();
824 //      FX_BOOL bRet = pApp->OpenURL(swURL);
825         pRuntime->EndBlock();
826
827 //      return bRet;
828         return FALSE;
829 }
830
831 FX_BOOL app::runtimeHighlight(OBJ_PROP_PARAMS)
832 {
833         if (vp.IsSetting())
834         {
835                 vp>>m_bRuntimeHighLight;
836         }
837         else
838         {
839                 vp<<m_bRuntimeHighLight;
840         }
841
842         return TRUE;
843 }
844
845 FX_BOOL app::fullscreen(OBJ_PROP_PARAMS)
846 {
847         return FALSE;
848 }
849
850 FX_BOOL app::popUpMenu(OBJ_METHOD_PARAMS)
851 {
852         return FALSE;
853 }
854
855
856 FX_BOOL app::browseForDoc(OBJ_METHOD_PARAMS)
857 {
858         //This method may trigger a "file save" dialog,while enable user to save contents of the document.
859         //Such action is considered to be unsafe.
860         if (IsSafeMode(cc)) return TRUE;
861
862         v8::Isolate* isolate = GetIsolate(cc);
863
864         bool bSave = false;
865         CFX_ByteString cFilenameInit = CFX_ByteString();
866         CFX_ByteString cFSInit = CFX_ByteString();
867
868         if(params.size()>0 && (params[0].GetType() == VT_object))
869         {
870                 JSObject pObj = (JSObject )params[0];
871
872                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj,L"bSave");
873                         bSave = (bool)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
874                 
875                 pValue = JS_GetObjectElement(isolate, pObj,L"cFilenameInit");
876                 {
877                         CJS_Value t = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue));
878                         cFilenameInit = t.operator CFX_ByteString();
879                 }
880                 
881                 pValue = JS_GetObjectElement(isolate,pObj,L"cFSInit");
882                 {
883                         CJS_Value t = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue));
884                         cFSInit = t.operator CFX_ByteString();
885                 }
886         }
887         else
888         {
889                 if(params.size() >= 1)
890                 {
891                         bSave = (bool)params[0];
892                 }
893                 if(params.size() >= 2)
894                 {
895                         CJS_Value t = params[1];
896                         cFilenameInit = t.operator CFX_ByteString();
897                 }
898                 if(params.size() >= 3)
899                 {
900                         CJS_Value t = params[2];
901                         cFSInit = t.operator CFX_ByteString();
902                 }
903         }
904         CJS_Context* pContext = (CJS_Context *)cc;
905         ASSERT(pContext != NULL);
906         
907         CPDFDoc_Environment* pApp = pContext->GetReaderApp();
908         ASSERT(pApp != NULL);
909
910         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
911         ASSERT(pRuntime != NULL);
912
913         CFX_WideString wsFileNameInit = CFX_WideString::FromLocal(cFilenameInit);
914         CFX_WideString wsFSInit = CFX_WideString::FromLocal(cFSInit);
915         CFX_WideString wsFilePath = pApp->JS_appbrowseForDoc(bSave, wsFileNameInit);
916         if(wsFilePath.IsEmpty())
917                 return FALSE;
918
919         JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, -1);
920
921         JS_PutObjectString(isolate,pRetObj, L"cPath", SysPathToPDFPath(wsFilePath));    
922         JS_PutObjectString(isolate,pRetObj, L"cURL", SysPathToPDFPath(wsFilePath));
923
924         if (!cFSInit.IsEmpty())
925         {
926                 JS_PutObjectString(isolate,pRetObj, L"cFS", CFX_WideString::FromLocal(cFSInit.GetBuffer(cFSInit.GetLength())));
927         }
928         else
929         {
930                 JS_PutObjectString(isolate,pRetObj, L"cFS", CFX_WideString::FromLocal("DOS"));
931         }
932         
933         vRet =  pRetObj;
934
935         return TRUE;
936 }
937
938 CFX_WideString app::SysPathToPDFPath(const CFX_WideString& sOldPath)
939 {
940         CFX_WideString sRet = L"/";
941         
942         for (int i=0,sz=sOldPath.GetLength(); i<sz; i++)
943         {
944                 wchar_t c = sOldPath.GetAt(i);
945                 if (c == L':')
946                 {
947                 }
948                 else
949                 {
950                         if (c == L'\\')
951                         {
952                                 sRet += L"/";
953                         }
954                         else
955                         {
956                                 sRet += c;
957                         }
958                 }
959         }
960         
961         return sRet;
962 }
963
964 CFX_WideString app::PDFPathToSysPath(const CFX_WideString& sOldPath)
965 {
966         //strLPath = "D:\temporay.fdf";
967         CFX_WideString strOPath = sOldPath;
968         strOPath.TrimLeft();
969         strOPath.TrimRight();
970         
971         if (strOPath.GetAt(0) == L'/' && strOPath.GetAt(2) == L'/')
972         {
973                 wchar_t c_Drive = strOPath.GetAt(1);
974                 if ((c_Drive >= L'a' && c_Drive <= L'z' )||( c_Drive >= L'A' && c_Drive <= L'Z'))
975                 {
976                         strOPath.Replace(L"/",L"\\");
977                         //strOPath.SetAt(0,'');
978                         strOPath.Insert(2,':');
979                         strOPath.Delete(0);
980                 }
981         }
982         
983         return strOPath;
984 }
985
986 CFX_WideString app::RelativePathToSysPath(const CFX_WideString& sOldPath, const CFX_WideString& sFilePath)
987 {
988 //      if (!PathIsRelative(sOldPath)) return sOldPath;
989         
990         int nSplit = 0;
991         for (int i=sFilePath.GetLength()-1; i>=0; i--)
992         {
993                 if (sFilePath[i] == '\\' || sFilePath[i] == '/')
994                 {
995                         nSplit = i;
996                         break;
997                 }
998         }
999         
1000         return sFilePath.Left(nSplit+1) + sOldPath;
1001 }
1002
1003 FX_BOOL app::newDoc(OBJ_METHOD_PARAMS)
1004 {
1005         return FALSE;
1006 }
1007
1008 FX_BOOL app::openDoc(OBJ_METHOD_PARAMS)
1009 {
1010         return FALSE;
1011 }
1012
1013 FX_BOOL app::response(OBJ_METHOD_PARAMS)
1014 {
1015         CFX_WideString swQuestion = L"";
1016         CFX_WideString swLabel = L"";
1017         CFX_WideString swTitle = L"PDF";
1018         CFX_WideString swDefault = L"";
1019         bool bPassWord = false;
1020
1021         v8::Isolate* isolate = GetIsolate(cc);
1022
1023         int iLength = params.size();
1024         if (iLength > 0 && params[0].GetType() == VT_object)
1025         {
1026                 JSObject pObj = (JSObject )params[0];
1027                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj,L"cQuestion");
1028                 swQuestion = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1029
1030                 pValue = JS_GetObjectElement(isolate,pObj,L"cTitle");
1031                 swTitle = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1032
1033                 pValue = JS_GetObjectElement(isolate,pObj,L"cDefault");
1034                 swDefault = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1035
1036                 pValue = JS_GetObjectElement(isolate,pObj,L"cLabel");
1037                 swLabel = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1038
1039                 pValue = JS_GetObjectElement(isolate,pObj,L"bPassword");
1040                 bPassWord = (bool)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
1041         }
1042         else
1043         {
1044                 switch(iLength)
1045                 {
1046                 case 5:
1047                         swLabel = params[4];
1048                         // FALLTHROUGH
1049                 case 4:
1050                         bPassWord = params[3];
1051                         // FALLTHROUGH
1052                 case 3:
1053                         swDefault = params[2];
1054                         // FALLTHROUGH
1055                 case 2:
1056                         swTitle = params[1];
1057                         // FALLTHROUGH
1058                 case 1:
1059                         swQuestion = params[0];
1060                         // FALLTHROUGH
1061                 default:
1062                         break;
1063                 }
1064         }
1065
1066         CJS_Context* pContext = (CJS_Context *)cc;
1067         ASSERT(pContext != NULL);
1068
1069         CPDFDoc_Environment* pApp = pContext->GetReaderApp();
1070         ASSERT(pApp != NULL);
1071
1072         const int MAX_INPUT_BYTES = 2048;
1073         char* pBuff = new char[MAX_INPUT_BYTES + 2];
1074         if (!pBuff)
1075                 return FALSE;
1076
1077         memset(pBuff, 0, MAX_INPUT_BYTES + 2);
1078         int nLengthBytes = pApp->JS_appResponse(swQuestion, swTitle, swDefault, swLabel, bPassWord, pBuff, MAX_INPUT_BYTES);
1079         if (nLengthBytes <= 0)
1080         {
1081                 vRet.SetNull();
1082                 delete[] pBuff;
1083                 return FALSE;
1084         }
1085         if (nLengthBytes > MAX_INPUT_BYTES)
1086                 nLengthBytes = MAX_INPUT_BYTES;
1087
1088         vRet = CFX_WideString::FromUTF16LE((unsigned short*)pBuff, nLengthBytes / sizeof(unsigned short));
1089         delete[] pBuff;
1090         return TRUE;
1091 }
1092
1093 FX_BOOL app::media(OBJ_PROP_PARAMS)
1094 {
1095         return FALSE;
1096 }
1097
1098 FX_BOOL app::execDialog(OBJ_METHOD_PARAMS)
1099 {
1100         return TRUE;
1101 }
1102