OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "fpdfsdk/javascript/app.h" | 7 #include "fpdfsdk/javascript/app.h" |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 BEGIN_JS_STATIC_PROP(CJS_TimerObj) | 26 BEGIN_JS_STATIC_PROP(CJS_TimerObj) |
27 END_JS_STATIC_PROP() | 27 END_JS_STATIC_PROP() |
28 | 28 |
29 BEGIN_JS_STATIC_METHOD(CJS_TimerObj) | 29 BEGIN_JS_STATIC_METHOD(CJS_TimerObj) |
30 END_JS_STATIC_METHOD() | 30 END_JS_STATIC_METHOD() |
31 | 31 |
32 IMPLEMENT_JS_CLASS(CJS_TimerObj, TimerObj) | 32 IMPLEMENT_JS_CLASS(CJS_TimerObj, TimerObj) |
33 | 33 |
34 TimerObj::TimerObj(CJS_Object* pJSObject) | 34 TimerObj::TimerObj(CJS_Object* pJSObject) |
35 : CJS_EmbedObj(pJSObject), m_pTimer(NULL) {} | 35 : CJS_EmbedObj(pJSObject), m_pTimer(nullptr) {} |
36 | 36 |
37 TimerObj::~TimerObj() {} | 37 TimerObj::~TimerObj() {} |
38 | 38 |
39 void TimerObj::SetTimer(CJS_Timer* pTimer) { | 39 void TimerObj::SetTimer(CJS_Timer* pTimer) { |
40 m_pTimer = pTimer; | 40 m_pTimer = pTimer; |
41 } | 41 } |
42 | 42 |
43 CJS_Timer* TimerObj::GetTimer() const { | 43 CJS_Timer* TimerObj::GetTimer() const { |
44 return m_pTimer; | 44 return m_pTimer; |
45 } | 45 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 JS_STATIC_METHOD_ENTRY(setInterval) | 95 JS_STATIC_METHOD_ENTRY(setInterval) |
96 JS_STATIC_METHOD_ENTRY(setTimeOut) | 96 JS_STATIC_METHOD_ENTRY(setTimeOut) |
97 END_JS_STATIC_METHOD() | 97 END_JS_STATIC_METHOD() |
98 | 98 |
99 IMPLEMENT_JS_CLASS(CJS_App, app) | 99 IMPLEMENT_JS_CLASS(CJS_App, app) |
100 | 100 |
101 app::app(CJS_Object* pJSObject) | 101 app::app(CJS_Object* pJSObject) |
102 : CJS_EmbedObj(pJSObject), m_bCalculate(true), m_bRuntimeHighLight(false) {} | 102 : CJS_EmbedObj(pJSObject), m_bCalculate(true), m_bRuntimeHighLight(false) {} |
103 | 103 |
104 app::~app() { | 104 app::~app() { |
105 for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) | |
106 delete m_aTimer[i]; | |
107 | |
108 m_aTimer.RemoveAll(); | |
109 } | 105 } |
110 | 106 |
111 FX_BOOL app::activeDocs(IJS_Context* cc, | 107 FX_BOOL app::activeDocs(IJS_Context* cc, |
112 CJS_PropValue& vp, | 108 CJS_PropValue& vp, |
113 CFX_WideString& sError) { | 109 CFX_WideString& sError) { |
114 if (!vp.IsGetting()) | 110 if (!vp.IsGetting()) |
115 return FALSE; | 111 return FALSE; |
116 | 112 |
117 CJS_Context* pContext = (CJS_Context*)cc; | 113 CJS_Context* pContext = (CJS_Context*)cc; |
118 CPDFDoc_Environment* pApp = pContext->GetReaderApp(); | 114 CPDFDoc_Environment* pApp = pContext->GetReaderApp(); |
119 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); | 115 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); |
120 CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument(); | 116 CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument(); |
121 CJS_Array aDocs(pRuntime); | 117 CJS_Array aDocs(pRuntime); |
122 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument()) { | 118 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument()) { |
123 CJS_Document* pJSDocument = NULL; | 119 CJS_Document* pJSDocument = nullptr; |
124 if (pDoc == pCurDoc) { | 120 if (pDoc == pCurDoc) { |
125 v8::Local<v8::Object> pObj = FXJS_GetThisObj(pRuntime->GetIsolate()); | 121 v8::Local<v8::Object> pObj = FXJS_GetThisObj(pRuntime->GetIsolate()); |
126 if (FXJS_GetObjDefnID(pObj) == CJS_Document::g_nObjDefnID) | 122 if (FXJS_GetObjDefnID(pObj) == CJS_Document::g_nObjDefnID) |
127 pJSDocument = | 123 pJSDocument = |
128 (CJS_Document*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); | 124 (CJS_Document*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); |
129 } else { | 125 } else { |
130 v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj( | 126 v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj( |
131 pRuntime->GetIsolate(), pRuntime, CJS_Document::g_nObjDefnID); | 127 pRuntime->GetIsolate(), pRuntime, CJS_Document::g_nObjDefnID); |
132 pJSDocument = | 128 pJSDocument = |
133 (CJS_Document*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); | 129 (CJS_Document*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 } | 373 } |
378 | 374 |
379 CFX_WideString script = params.size() > 0 ? params[0].ToCFXWideString() : L""; | 375 CFX_WideString script = params.size() > 0 ? params[0].ToCFXWideString() : L""; |
380 if (script.IsEmpty()) { | 376 if (script.IsEmpty()) { |
381 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); | 377 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); |
382 return TRUE; | 378 return TRUE; |
383 } | 379 } |
384 | 380 |
385 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); | 381 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); |
386 uint32_t dwInterval = params.size() > 1 ? params[1].ToInt() : 1000; | 382 uint32_t dwInterval = params.size() > 1 ? params[1].ToInt() : 1000; |
387 | |
388 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); | 383 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); |
389 ASSERT(pApp); | 384 m_Timers.push_back(std::unique_ptr<CJS_Timer>( |
390 CJS_Timer* pTimer = | 385 new CJS_Timer(this, pApp, pRuntime, 0, script, dwInterval, 0))); |
391 new CJS_Timer(this, pApp, pRuntime, 0, script, dwInterval, 0); | |
392 m_aTimer.Add(pTimer); | |
393 | 386 |
394 v8::Local<v8::Object> pRetObj = FXJS_NewFxDynamicObj( | 387 v8::Local<v8::Object> pRetObj = FXJS_NewFxDynamicObj( |
395 pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); | 388 pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); |
396 CJS_TimerObj* pJS_TimerObj = | 389 CJS_TimerObj* pJS_TimerObj = static_cast<CJS_TimerObj*>( |
397 (CJS_TimerObj*)FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj); | 390 FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj)); |
398 TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject(); | 391 TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject()); |
399 pTimerObj->SetTimer(pTimer); | 392 pTimerObj->SetTimer(m_Timers.back().get()); |
400 | 393 |
401 vRet = pRetObj; | 394 vRet = pRetObj; |
402 return TRUE; | 395 return TRUE; |
403 } | 396 } |
404 | 397 |
405 FX_BOOL app::setTimeOut(IJS_Context* cc, | 398 FX_BOOL app::setTimeOut(IJS_Context* cc, |
406 const std::vector<CJS_Value>& params, | 399 const std::vector<CJS_Value>& params, |
407 CJS_Value& vRet, | 400 CJS_Value& vRet, |
408 CFX_WideString& sError) { | 401 CFX_WideString& sError) { |
| 402 CJS_Context* pContext = static_cast<CJS_Context*>(cc); |
409 if (params.size() > 2 || params.size() == 0) { | 403 if (params.size() > 2 || params.size() == 0) { |
410 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); | 404 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); |
411 return FALSE; | 405 return FALSE; |
412 } | 406 } |
413 | 407 |
414 CJS_Context* pContext = (CJS_Context*)cc; | 408 CFX_WideString script = params[0].ToCFXWideString(); |
415 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); | |
416 | |
417 CFX_WideString script = params.size() > 0 ? params[0].ToCFXWideString() : L""; | |
418 if (script.IsEmpty()) { | 409 if (script.IsEmpty()) { |
419 sError = | 410 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); |
420 JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE); | |
421 return TRUE; | 411 return TRUE; |
422 } | 412 } |
423 | 413 |
424 uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt() : 1000; | 414 uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt() : 1000; |
425 | 415 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); |
426 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); | 416 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); |
427 ASSERT(pApp); | 417 m_Timers.push_back(std::unique_ptr<CJS_Timer>( |
428 | 418 new CJS_Timer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut))); |
429 CJS_Timer* pTimer = | |
430 new CJS_Timer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut); | |
431 m_aTimer.Add(pTimer); | |
432 | 419 |
433 v8::Local<v8::Object> pRetObj = FXJS_NewFxDynamicObj( | 420 v8::Local<v8::Object> pRetObj = FXJS_NewFxDynamicObj( |
434 pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); | 421 pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); |
435 CJS_TimerObj* pJS_TimerObj = | 422 |
436 (CJS_TimerObj*)FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj); | 423 CJS_TimerObj* pJS_TimerObj = static_cast<CJS_TimerObj*>( |
437 TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject(); | 424 FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj)); |
438 pTimerObj->SetTimer(pTimer); | 425 |
| 426 TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject()); |
| 427 pTimerObj->SetTimer(m_Timers.back().get()); |
439 | 428 |
440 vRet = pRetObj; | 429 vRet = pRetObj; |
441 return TRUE; | 430 return TRUE; |
442 } | 431 } |
443 | 432 |
444 FX_BOOL app::clearTimeOut(IJS_Context* cc, | 433 FX_BOOL app::clearTimeOut(IJS_Context* cc, |
445 const std::vector<CJS_Value>& params, | 434 const std::vector<CJS_Value>& params, |
446 CJS_Value& vRet, | 435 CJS_Value& vRet, |
447 CFX_WideString& sError) { | 436 CFX_WideString& sError) { |
448 CJS_Context* pContext = (CJS_Context*)cc; | 437 CJS_Context* pContext = (CJS_Context*)cc; |
449 if (params.size() != 1) { | 438 if (params.size() != 1) { |
450 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); | 439 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); |
451 return FALSE; | 440 return FALSE; |
452 } | 441 } |
453 | 442 |
454 if (params[0].GetType() == CJS_Value::VT_fxobject) { | 443 app::ClearTimerCommon(params[0]); |
455 v8::Local<v8::Object> pObj = params[0].ToV8Object(); | |
456 if (FXJS_GetObjDefnID(pObj) == CJS_TimerObj::g_nObjDefnID) { | |
457 if (CJS_Object* pJSObj = params[0].ToCJSObject()) { | |
458 if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject()) { | |
459 if (CJS_Timer* pTimer = pTimerObj->GetTimer()) { | |
460 pTimer->KillJSTimer(); | |
461 | |
462 for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) { | |
463 if (m_aTimer[i] == pTimer) { | |
464 m_aTimer.RemoveAt(i); | |
465 break; | |
466 } | |
467 } | |
468 | |
469 delete pTimer; | |
470 pTimerObj->SetTimer(NULL); | |
471 } | |
472 } | |
473 } | |
474 } | |
475 } | |
476 | |
477 return TRUE; | 444 return TRUE; |
478 } | 445 } |
479 | 446 |
480 FX_BOOL app::clearInterval(IJS_Context* cc, | 447 FX_BOOL app::clearInterval(IJS_Context* cc, |
481 const std::vector<CJS_Value>& params, | 448 const std::vector<CJS_Value>& params, |
482 CJS_Value& vRet, | 449 CJS_Value& vRet, |
483 CFX_WideString& sError) { | 450 CFX_WideString& sError) { |
484 CJS_Context* pContext = (CJS_Context*)cc; | 451 CJS_Context* pContext = (CJS_Context*)cc; |
485 if (params.size() != 1) { | 452 if (params.size() != 1) { |
486 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); | 453 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); |
487 return FALSE; | 454 return FALSE; |
488 } | 455 } |
489 | 456 |
490 if (params[0].GetType() == CJS_Value::VT_fxobject) { | 457 app::ClearTimerCommon(params[0]); |
491 v8::Local<v8::Object> pObj = params[0].ToV8Object(); | 458 return TRUE; |
492 if (FXJS_GetObjDefnID(pObj) == CJS_TimerObj::g_nObjDefnID) { | 459 } |
493 if (CJS_Object* pJSObj = params[0].ToCJSObject()) { | |
494 if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject()) { | |
495 if (CJS_Timer* pTimer = pTimerObj->GetTimer()) { | |
496 pTimer->KillJSTimer(); | |
497 | 460 |
498 for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) { | 461 void app::ClearTimerCommon(const CJS_Value& param) { |
499 if (m_aTimer[i] == pTimer) { | 462 if (param.GetType() != CJS_Value::VT_fxobject) |
500 m_aTimer.RemoveAt(i); | 463 return; |
501 break; | |
502 } | |
503 } | |
504 | 464 |
505 delete pTimer; | 465 v8::Local<v8::Object> pObj = param.ToV8Object(); |
506 pTimerObj->SetTimer(NULL); | 466 if (FXJS_GetObjDefnID(pObj) != CJS_TimerObj::g_nObjDefnID) |
507 } | 467 return; |
508 } | |
509 } | |
510 } | |
511 } | |
512 | 468 |
513 return TRUE; | 469 CJS_Object* pJSObj = param.ToCJSObject(); |
| 470 if (!pJSObj) |
| 471 return; |
| 472 |
| 473 TimerObj* pTimerObj = static_cast<TimerObj*>(pJSObj->GetEmbedObject()); |
| 474 if (!pTimerObj) |
| 475 return; |
| 476 |
| 477 CJS_Timer* pTimer = pTimerObj->GetTimer(); |
| 478 if (!pTimer) |
| 479 return; |
| 480 |
| 481 pTimer->KillJSTimer(); |
| 482 auto iter = std::find_if(m_Timers.begin(), m_Timers.end(), |
| 483 [pTimer](const std::unique_ptr<CJS_Timer>& that) { |
| 484 return pTimer == that.get(); |
| 485 }); |
| 486 |
| 487 if (iter != m_Timers.end()) |
| 488 m_Timers.erase(iter); |
| 489 |
| 490 pTimerObj->SetTimer(nullptr); |
514 } | 491 } |
515 | 492 |
516 FX_BOOL app::execMenuItem(IJS_Context* cc, | 493 FX_BOOL app::execMenuItem(IJS_Context* cc, |
517 const std::vector<CJS_Value>& params, | 494 const std::vector<CJS_Value>& params, |
518 CJS_Value& vRet, | 495 CJS_Value& vRet, |
519 CFX_WideString& sError) { | 496 CFX_WideString& sError) { |
520 return FALSE; | 497 return FALSE; |
521 } | 498 } |
522 | 499 |
523 void app::TimerProc(CJS_Timer* pTimer) { | 500 void app::TimerProc(CJS_Timer* pTimer) { |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 FX_BOOL app::media(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) { | 718 FX_BOOL app::media(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) { |
742 return FALSE; | 719 return FALSE; |
743 } | 720 } |
744 | 721 |
745 FX_BOOL app::execDialog(IJS_Context* cc, | 722 FX_BOOL app::execDialog(IJS_Context* cc, |
746 const std::vector<CJS_Value>& params, | 723 const std::vector<CJS_Value>& params, |
747 CJS_Value& vRet, | 724 CJS_Value& vRet, |
748 CFX_WideString& sError) { | 725 CFX_WideString& sError) { |
749 return TRUE; | 726 return TRUE; |
750 } | 727 } |
OLD | NEW |