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