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