Remove a few unused variables, functions, and member variables.
[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 #ifdef FOXIT_CHROME_BUILD
484         return FALSE;
485 #else
486         CJS_Context* pContext = (CJS_Context*)cc;
487         ASSERT(pContext != NULL);
488         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
489         ASSERT(pRuntime != NULL);
490
491         if (vp.IsGetting())
492         {
493                 return TRUE;
494         }
495         else
496         {
497                 return TRUE;
498         }
499 #endif
500 }
501
502 FX_BOOL app::setInterval(OBJ_METHOD_PARAMS)
503 {
504         if (params.size() > 2 || params.size() == 0) 
505         {
506                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
507                 return FALSE;
508         }
509         
510         CJS_Context* pContext = (CJS_Context*)cc;
511         ASSERT(pContext != NULL);
512         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
513         ASSERT(pRuntime != NULL);
514
515         CFX_WideString script = params.size() > 0 ?  (FX_LPCWSTR)(params[0].operator CFX_WideString()) : (FX_LPCWSTR)L"";
516         if (script.IsEmpty()) 
517         {
518                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE);
519                 return TRUE;
520         }
521
522         FX_DWORD dwInterval = params.size() > 1 ? (int)params[1] : 1000;
523         
524         CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
525         ASSERT(pApp);
526         CJS_Timer* pTimer = new CJS_Timer(this, pApp);
527         m_aTimer.Add(pTimer);
528
529         pTimer->SetType(0);
530         pTimer->SetRuntime(pRuntime);
531         pTimer->SetJScript(script);
532         pTimer->SetTimeOut(0);
533 //      pTimer->SetStartTime(GetTickCount());
534         pTimer->SetJSTimer(dwInterval);
535         
536         JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"TimerObj"));
537         
538         CJS_TimerObj* pJS_TimerObj = (CJS_TimerObj*)JS_GetPrivate(pRuntime->GetIsolate(),pRetObj);
539         ASSERT(pJS_TimerObj != NULL);
540         
541         TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject();
542         ASSERT(pTimerObj != NULL);
543         
544         pTimerObj->SetTimer(pTimer); 
545         
546         vRet = pRetObj;
547         
548         return TRUE;
549 }
550
551 FX_BOOL app::setTimeOut(OBJ_METHOD_PARAMS)
552 {
553         if (params.size() > 2 || params.size() == 0)
554         {
555                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
556                 return FALSE;
557         }
558         
559         CJS_Context* pContext = (CJS_Context*)cc;
560         ASSERT(pContext != NULL);
561         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
562         ASSERT(pRuntime != NULL);
563         
564         CFX_WideString script = params.size() > 0 ?  (FX_LPCWSTR)(params[0].operator CFX_WideString()) : (FX_LPCWSTR)L"";
565         if (script.IsEmpty()) 
566         {
567                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE);
568                 return TRUE;
569         }
570         
571         FX_DWORD dwTimeOut = params.size() > 1 ? (int)params[1] : 1000;
572         
573         CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
574         ASSERT(pApp);
575         CJS_Timer* pTimer = new CJS_Timer(this, pApp);
576         m_aTimer.Add(pTimer);
577         
578         pTimer->SetType(1);
579         pTimer->SetRuntime(pRuntime);
580         pTimer->SetJScript(script);
581         pTimer->SetTimeOut(dwTimeOut);
582 //      pTimer->SetStartTime(GetTickCount());
583 //      pTimer->SetJSTimer(1000);
584         pTimer->SetJSTimer(dwTimeOut);
585         
586         JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"TimerObj"));
587 //      ASSERT(pRetObj != NULL);
588         
589         CJS_TimerObj* pJS_TimerObj = (CJS_TimerObj*)JS_GetPrivate(pRuntime->GetIsolate(),pRetObj);
590         ASSERT(pJS_TimerObj != NULL);
591         
592         TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject();
593         ASSERT(pTimerObj != NULL);
594         
595         pTimerObj->SetTimer(pTimer); 
596         
597         vRet = pRetObj;
598         
599         return TRUE;
600 }
601
602 FX_BOOL app::clearTimeOut(OBJ_METHOD_PARAMS)
603 {
604         CJS_Context* pContext = (CJS_Context*)cc;
605         ASSERT(pContext != NULL);
606         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
607         ASSERT(pRuntime != NULL);
608         
609         if (params.size() != 1)
610         {
611                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
612                 return FALSE;
613         }
614         
615         if (params[0].GetType() == VT_fxobject)
616         {
617                 JSFXObject pObj = (JSFXObject)params[0];
618                 {
619                         if (JS_GetObjDefnID(pObj) == JS_GetObjDefnID(*pRuntime, L"TimerObj"))
620                         {
621                                 if (CJS_Object* pJSObj = (CJS_Object*)params[0])
622                                 {
623                                         if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject())
624                                         {
625                                                 if (CJS_Timer* pTimer = pTimerObj->GetTimer())
626                                                 {
627                                                         pTimer->KillJSTimer();
628                                                         
629                                                         for (int i=0,sz=m_aTimer.GetSize(); i<sz; i++)
630                                                         {
631                                                                 if (m_aTimer[i] == pTimer)
632                                                                 {
633                                                                         m_aTimer.RemoveAt(i);
634                                                                         break;
635                                                                 }
636                                                         }
637                                                         
638                                                         delete pTimer;
639                                                         pTimerObj->SetTimer(NULL);
640                                                 }
641                                         }
642                                 }
643                         }
644                 }
645         }
646         
647         return TRUE;
648 }
649
650 FX_BOOL app::clearInterval(OBJ_METHOD_PARAMS)
651 {
652         CJS_Context* pContext = (CJS_Context*)cc;
653         ASSERT(pContext != NULL);
654         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
655         ASSERT(pRuntime != NULL);
656
657         if (params.size() != 1)
658         {
659                 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);  
660                 return FALSE;
661         }
662         
663         if (params[0].GetType() == VT_fxobject)
664         {
665                 JSFXObject pObj = (JSFXObject)params[0];
666                 {
667                         if (JS_GetObjDefnID(pObj) == JS_GetObjDefnID(*pRuntime, L"TimerObj"))
668                         {
669                                 if (CJS_Object* pJSObj = (CJS_Object*)params[0])
670                                 {
671                                         if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject())
672                                         {
673                                                 if (CJS_Timer* pTimer = pTimerObj->GetTimer())
674                                                 {
675                                                         pTimer->KillJSTimer();
676                                                         
677                                                         for (int i=0,sz=m_aTimer.GetSize(); i<sz; i++)
678                                                         {
679                                                                 if (m_aTimer[i] == pTimer)
680                                                                 {
681                                                                         m_aTimer.RemoveAt(i);
682                                                                         break;
683                                                                 }
684                                                         }
685                                                         
686                                                         delete pTimer;
687                                                         pTimerObj->SetTimer(NULL);
688                                                 }
689                                         }
690                                 }
691                         }
692                 }
693         }
694         
695         return TRUE;
696 }
697
698 FX_BOOL app::execMenuItem(OBJ_METHOD_PARAMS)
699 {       
700         return FALSE;
701 }
702
703 void app::TimerProc(CJS_Timer* pTimer)
704 {
705         ASSERT(pTimer != NULL);
706
707         switch (pTimer->GetType())
708         {
709         case 0: //interval
710                 RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript());
711                 break;
712         case 1:
713                 if (pTimer->GetTimeOut() > 0)
714                 {
715                         RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript());
716                         pTimer->KillJSTimer();
717                 }
718                 break;
719         }
720         
721 }
722
723 void app::RunJsScript(CJS_Runtime* pRuntime,const CFX_WideString& wsScript)
724 {
725         ASSERT(pRuntime != NULL);
726
727         if (!pRuntime->IsBlocking())
728         {
729                 IFXJS_Context* pContext = pRuntime->NewContext();
730                 ASSERT(pContext != NULL);
731                 pContext->OnExternal_Exec();
732                 CFX_WideString wtInfo;
733                 pContext->RunScript(wsScript,wtInfo);
734                 pRuntime->ReleaseContext(pContext);
735         }
736 }
737
738 FX_BOOL app::goBack(OBJ_METHOD_PARAMS)
739 {
740
741
742         
743         
744         
745         
746         return TRUE;
747 }
748
749 FX_BOOL app::goForward(OBJ_METHOD_PARAMS)
750 {       
751
752
753
754
755
756
757         return TRUE;
758 }
759
760 FX_BOOL app::mailMsg(OBJ_METHOD_PARAMS)
761 {
762         CJS_Context* pContext = (CJS_Context*)cc;
763         ASSERT(pContext != NULL);
764
765         v8::Isolate* isolate = GetIsolate(cc);
766
767         FX_BOOL bUI = TRUE;
768         CFX_WideString cTo = L"";
769         CFX_WideString cCc = L"";
770         CFX_WideString cBcc = L"";
771         CFX_WideString cSubject = L"";
772         CFX_WideString cMsg = L"";
773         if(params.size() < 2)
774                 return FALSE;
775
776         bUI = params.size()>=1?(int)params[0]:TRUE;
777         cTo = params.size()>=2?(const wchar_t*)(FX_LPCWSTR)params[1].operator CFX_WideString():L"";
778         cCc = params.size()>=3?(const wchar_t*)(FX_LPCWSTR)params[2].operator CFX_WideString():L"";
779         cBcc = params.size()>=4?(const wchar_t*)(FX_LPCWSTR)params[3].operator CFX_WideString():L"";
780         cSubject = params.size()>=5?(const wchar_t*)(FX_LPCWSTR)params[4].operator CFX_WideString():L"";
781         cMsg = params.size()>=6?(const wchar_t*)(FX_LPCWSTR)params[5].operator CFX_WideString():L"";            
782
783
784         if (params[0].GetType() == VT_object)
785         {
786                 JSObject pObj = (JSObject)params[0];
787
788                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj, L"bUI");
789                         bUI = (int)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
790
791                 pValue = JS_GetObjectElement(isolate, pObj, L"cTo");
792                         cTo = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
793
794                 pValue = JS_GetObjectElement(isolate,pObj, L"cCc");
795                         cCc = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
796
797                 pValue = JS_GetObjectElement(isolate,pObj, L"cBcc");
798                         cBcc = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
799
800                 pValue = JS_GetObjectElement(isolate,pObj, L"cSubject");
801                         cSubject = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
802
803                 pValue = JS_GetObjectElement(isolate,pObj, L"cMsg");
804                         cMsg = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
805         }
806         
807         
808
809         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
810         ASSERT(pRuntime != NULL);
811
812         CPDFDoc_Environment* pApp = pContext->GetReaderApp();
813         ASSERT(pApp != NULL);
814
815         pRuntime->BeginBlock();
816         pApp->JS_docmailForm(NULL, 0, bUI, (FX_LPCWSTR)cTo, (FX_LPCWSTR)cSubject, (FX_LPCWSTR)cCc, (FX_LPCWSTR)cBcc, (FX_LPCWSTR)cMsg);
817         ///////////////////////////////////////////////////////////////////////////////////////////////
818         pRuntime->EndBlock();
819
820         //return bRet;
821         return FALSE;
822 }
823
824 FX_BOOL app::launchURL(OBJ_METHOD_PARAMS)
825 {
826         if (IsSafeMode(cc)) return TRUE;
827
828         CJS_Context* pContext = (CJS_Context*)cc;
829         ASSERT(pContext != NULL);
830
831         
832
833
834         CFX_WideString swURL = params[0].operator CFX_WideString();
835
836         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
837         ASSERT(pRuntime != NULL);
838
839         pRuntime->BeginBlock();
840 //      FX_BOOL bRet = pApp->OpenURL(swURL);
841         pRuntime->EndBlock();
842
843 //      return bRet;
844         return FALSE;
845 }
846
847 FX_BOOL app::runtimeHighlight(OBJ_PROP_PARAMS)
848 {
849         if (vp.IsSetting())
850         {
851                 vp>>m_bRuntimeHighLight;
852         }
853         else
854         {
855                 vp<<m_bRuntimeHighLight;
856         }
857
858         return TRUE;
859 }
860
861 FX_BOOL app::fullscreen(OBJ_PROP_PARAMS)
862 {
863         return FALSE;
864 }
865
866 FX_BOOL app::popUpMenu(OBJ_METHOD_PARAMS)
867 {
868         return FALSE;
869 }
870
871
872 FX_BOOL app::browseForDoc(OBJ_METHOD_PARAMS)
873 {
874         //This method may trigger a "file save" dialog,while enable user to save contents of the document.
875         //Such action is considered to be unsafe.
876         if (IsSafeMode(cc)) return TRUE;
877
878         v8::Isolate* isolate = GetIsolate(cc);
879
880         bool bSave = false;
881         CFX_ByteString cFilenameInit = CFX_ByteString();
882         CFX_ByteString cFSInit = CFX_ByteString();
883
884         if(params.size()>0 && (params[0].GetType() == VT_object))
885         {
886                 JSObject pObj = (JSObject )params[0];
887
888                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj,L"bSave");
889                         bSave = (bool)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
890                 
891                 pValue = JS_GetObjectElement(isolate, pObj,L"cFilenameInit");
892                 {
893                         CJS_Value t = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue));
894                         cFilenameInit = t.operator CFX_ByteString();
895                 }
896                 
897                 pValue = JS_GetObjectElement(isolate,pObj,L"cFSInit");
898                 {
899                         CJS_Value t = CJS_Value(isolate, pValue, GET_VALUE_TYPE(pValue));
900                         cFSInit = t.operator CFX_ByteString();
901                 }
902         }
903         else
904         {
905                 if(params.size() >= 1)
906                 {
907                         bSave = (bool)params[0];
908                 }
909                 if(params.size() >= 2)
910                 {
911                         CJS_Value t = params[1];
912                         cFilenameInit = t.operator CFX_ByteString();
913                 }
914                 if(params.size() >= 3)
915                 {
916                         CJS_Value t = params[2];
917                         cFSInit = t.operator CFX_ByteString();
918                 }
919         }
920         CJS_Context* pContext = (CJS_Context *)cc;
921         ASSERT(pContext != NULL);
922         
923         CPDFDoc_Environment* pApp = pContext->GetReaderApp();
924         ASSERT(pApp != NULL);
925
926         CJS_Runtime* pRuntime = pContext->GetJSRuntime();
927         ASSERT(pRuntime != NULL);
928
929         CFX_WideString wsFileNameInit = CFX_WideString::FromLocal(cFilenameInit);
930         CFX_WideString wsFSInit = CFX_WideString::FromLocal(cFSInit);
931         CFX_WideString wsFilePath = pApp->JS_appbrowseForDoc(bSave, wsFileNameInit);
932         if(wsFilePath.IsEmpty())
933                 return FALSE;
934
935         JSFXObject pRetObj = JS_NewFxDynamicObj(*pRuntime, pContext, -1);
936
937         JS_PutObjectString(isolate,pRetObj, L"cPath", SysPathToPDFPath(wsFilePath));    
938         JS_PutObjectString(isolate,pRetObj, L"cURL", SysPathToPDFPath(wsFilePath));
939
940         if (!cFSInit.IsEmpty())
941         {
942                 JS_PutObjectString(isolate,pRetObj, L"cFS", CFX_WideString::FromLocal(cFSInit.GetBuffer(cFSInit.GetLength())));
943         }
944         else
945         {
946                 JS_PutObjectString(isolate,pRetObj, L"cFS", CFX_WideString::FromLocal("DOS"));
947         }
948         
949         vRet =  pRetObj;
950
951         return TRUE;
952 }
953
954 CFX_WideString app::SysPathToPDFPath(const CFX_WideString& sOldPath)
955 {
956         CFX_WideString sRet = L"/";
957         
958         for (int i=0,sz=sOldPath.GetLength(); i<sz; i++)
959         {
960                 wchar_t c = sOldPath.GetAt(i);
961                 if (c == L':')
962                 {
963                 }
964                 else
965                 {
966                         if (c == L'\\')
967                         {
968                                 sRet += L"/";
969                         }
970                         else
971                         {
972                                 sRet += c;
973                         }
974                 }
975         }
976         
977         return sRet;
978 }
979
980 CFX_WideString app::PDFPathToSysPath(const CFX_WideString& sOldPath)
981 {
982         //strLPath = "D:\temporay.fdf";
983         CFX_WideString strOPath = sOldPath;
984         strOPath.TrimLeft();
985         strOPath.TrimRight();
986         
987         if (strOPath.GetAt(0) == L'/' && strOPath.GetAt(2) == L'/')
988         {
989                 wchar_t c_Drive = strOPath.GetAt(1);
990                 if ((c_Drive >= L'a' && c_Drive <= L'z' )||( c_Drive >= L'A' && c_Drive <= L'Z'))
991                 {
992                         strOPath.Replace((FX_LPCWSTR)L"/",(FX_LPCWSTR)L"\\");
993                         //strOPath.SetAt(0,'');
994                         strOPath.Insert(2,':');
995                         strOPath.Delete(0);
996                 }
997         }
998         
999         return strOPath;
1000 }
1001
1002 CFX_WideString app::RelativePathToSysPath(const CFX_WideString& sOldPath, const CFX_WideString& sFilePath)
1003 {
1004 //      if (!PathIsRelative(sOldPath)) return sOldPath;
1005         
1006         int nSplit = 0;
1007         for (int i=sFilePath.GetLength()-1; i>=0; i--)
1008         {
1009                 if (sFilePath[i] == '\\' || sFilePath[i] == '/')
1010                 {
1011                         nSplit = i;
1012                         break;
1013                 }
1014         }
1015         
1016         return sFilePath.Left(nSplit+1) + sOldPath;
1017 }
1018
1019 FX_BOOL app::newDoc(OBJ_METHOD_PARAMS)
1020 {
1021         return FALSE;
1022 }
1023
1024 FX_BOOL app::openDoc(OBJ_METHOD_PARAMS)
1025 {
1026         return FALSE;
1027 }
1028
1029 FX_BOOL app::response(OBJ_METHOD_PARAMS)
1030 {
1031         CFX_WideString swQuestion = L"";
1032         CFX_WideString swLabel = L"";
1033 #ifndef FOXIT_CHROME_BUILD
1034         CFX_WideString swTitle = L"Foxit";
1035 #else
1036         CFX_WideString swTitle = L"PDF";
1037 #endif
1038         CFX_WideString swDefault = L"";
1039         bool bPassWord = false;
1040
1041         v8::Isolate* isolate = GetIsolate(cc);
1042
1043         int iLength = params.size();
1044         if (iLength > 0 && params[0].GetType() == VT_object)
1045         {
1046                 JSObject pObj = (JSObject )params[0];
1047                 v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate,pObj,L"cQuestion");
1048                         swQuestion = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1049
1050                 pValue = JS_GetObjectElement(isolate,pObj,L"cTitle");
1051                         swTitle = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1052
1053                 pValue = JS_GetObjectElement(isolate,pObj,L"cDefault");
1054                         swDefault = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1055
1056                 pValue = JS_GetObjectElement(isolate,pObj,L"cLabel");
1057                         swLabel = CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue)).operator CFX_WideString();
1058
1059                 pValue = JS_GetObjectElement(isolate,pObj,L"bPassword");
1060                         bPassWord = (bool)CJS_Value(isolate,pValue,GET_VALUE_TYPE(pValue));
1061         }
1062         else
1063         {
1064                 switch(iLength)
1065                 {
1066                 case 1:
1067                         swQuestion = params[0];
1068                         break;
1069                 case 2:
1070                         swQuestion = params[0];
1071                         swTitle = params[1];
1072                         break;
1073                 case 3:
1074                         swQuestion = params[0];
1075                         swTitle = params[1];
1076                         swDefault = params[2];
1077                         break;
1078                 case 4:
1079                         swQuestion = params[0];
1080                         swTitle = params[1];
1081                         swDefault = params[2];
1082                         bPassWord = params[3];
1083                         break;
1084                 case 5:
1085                         swQuestion = params[0];
1086                         swTitle = params[1];
1087                         swDefault = params[2];
1088                         bPassWord = params[3];
1089                         swLabel = params[4];
1090                         break;
1091                 default:
1092                         break;
1093                 }
1094         }
1095
1096         CJS_Context* pContext = (CJS_Context *)cc;
1097         ASSERT(pContext != NULL);
1098
1099         CPDFDoc_Environment* pApp = pContext->GetReaderApp();
1100         ASSERT(pApp != NULL);
1101
1102         const int MAX_INPUT_BYTES = 2048;
1103         char* pBuff = new char[MAX_INPUT_BYTES + 2];
1104         if (!pBuff)
1105                 return FALSE;
1106
1107         memset(pBuff, 0, MAX_INPUT_BYTES + 2);
1108         int nLengthBytes = pApp->JS_appResponse(swQuestion, swTitle, swDefault, swLabel, bPassWord, pBuff, MAX_INPUT_BYTES);
1109         if (nLengthBytes <= 0)
1110         {
1111                 vRet.SetNull();
1112                 delete[] pBuff;
1113                 return FALSE;
1114         }
1115         if (nLengthBytes > MAX_INPUT_BYTES)
1116                 nLengthBytes = MAX_INPUT_BYTES;
1117
1118         vRet = CFX_WideString::FromUTF16LE((unsigned short*)pBuff, nLengthBytes);
1119         delete[] pBuff;
1120         return TRUE;
1121 }
1122
1123 FX_BOOL app::media(OBJ_PROP_PARAMS)
1124 {
1125         return FALSE;
1126 }
1127
1128 FX_BOOL app::execDialog(OBJ_METHOD_PARAMS)
1129 {
1130         return TRUE;
1131 }
1132