Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: fpdfsdk/javascript/app.cpp

Issue 2251333002: Use a map for app::m_Timers (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Review feedback Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/javascript/app.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 502
503 CFX_WideString script = 503 CFX_WideString script =
504 params.size() > 0 ? params[0].ToCFXWideString(pRuntime) : L""; 504 params.size() > 0 ? params[0].ToCFXWideString(pRuntime) : L"";
505 if (script.IsEmpty()) { 505 if (script.IsEmpty()) {
506 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); 506 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
507 return TRUE; 507 return TRUE;
508 } 508 }
509 509
510 uint32_t dwInterval = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000; 510 uint32_t dwInterval = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000;
511 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); 511 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
512 m_Timers.push_back(std::unique_ptr<GlobalTimer>( 512
513 new GlobalTimer(this, pApp, pRuntime, 0, script, dwInterval, 0))); 513 std::unique_ptr<GlobalTimer> timer(
514 new GlobalTimer(this, pApp, pRuntime, 0, script, dwInterval, 0));
515 GlobalTimer* timerRef = timer.get();
516 m_Timers[timerRef] = std::move(timer);
514 517
515 v8::Local<v8::Object> pRetObj = 518 v8::Local<v8::Object> pRetObj =
516 pRuntime->NewFxDynamicObj(CJS_TimerObj::g_nObjDefnID); 519 pRuntime->NewFxDynamicObj(CJS_TimerObj::g_nObjDefnID);
517 CJS_TimerObj* pJS_TimerObj = 520 CJS_TimerObj* pJS_TimerObj =
518 static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj)); 521 static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj));
519 TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject()); 522 TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject());
520 pTimerObj->SetTimer(m_Timers.back().get()); 523 pTimerObj->SetTimer(timerRef);
521 524
522 vRet = CJS_Value(pRuntime, pRetObj); 525 vRet = CJS_Value(pRuntime, pRetObj);
523 return TRUE; 526 return TRUE;
524 } 527 }
525 528
526 FX_BOOL app::setTimeOut(IJS_Context* cc, 529 FX_BOOL app::setTimeOut(IJS_Context* cc,
527 const std::vector<CJS_Value>& params, 530 const std::vector<CJS_Value>& params,
528 CJS_Value& vRet, 531 CJS_Value& vRet,
529 CFX_WideString& sError) { 532 CFX_WideString& sError) {
530 CJS_Context* pContext = static_cast<CJS_Context*>(cc); 533 CJS_Context* pContext = static_cast<CJS_Context*>(cc);
531 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); 534 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
532 535
533 if (params.size() > 2 || params.size() == 0) { 536 if (params.size() > 2 || params.size() == 0) {
534 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); 537 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
535 return FALSE; 538 return FALSE;
536 } 539 }
537 540
538 CFX_WideString script = params[0].ToCFXWideString(pRuntime); 541 CFX_WideString script = params[0].ToCFXWideString(pRuntime);
539 if (script.IsEmpty()) { 542 if (script.IsEmpty()) {
540 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); 543 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
541 return TRUE; 544 return TRUE;
542 } 545 }
543 546
544 uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000; 547 uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000;
545 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); 548 CPDFDoc_Environment* pApp = pRuntime->GetReaderApp();
546 m_Timers.push_back(std::unique_ptr<GlobalTimer>( 549
547 new GlobalTimer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut))); 550 std::unique_ptr<GlobalTimer> timer(
551 new GlobalTimer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut));
552 GlobalTimer* timerRef = timer.get();
553 m_Timers[timerRef] = std::move(timer);
Tom Sepez 2016/08/18 17:17:43 This feels strange, mapping a pointer to itself, W
dsinclair 2016/08/18 17:20:21 If this was a set I'd have the same issue as befor
548 554
549 v8::Local<v8::Object> pRetObj = 555 v8::Local<v8::Object> pRetObj =
550 pRuntime->NewFxDynamicObj(CJS_TimerObj::g_nObjDefnID); 556 pRuntime->NewFxDynamicObj(CJS_TimerObj::g_nObjDefnID);
551 557
552 CJS_TimerObj* pJS_TimerObj = 558 CJS_TimerObj* pJS_TimerObj =
553 static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj)); 559 static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj));
554 560
555 TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject()); 561 TimerObj* pTimerObj = static_cast<TimerObj*>(pJS_TimerObj->GetEmbedObject());
556 pTimerObj->SetTimer(m_Timers.back().get()); 562 pTimerObj->SetTimer(timerRef);
557 563
558 vRet = CJS_Value(pRuntime, pRetObj); 564 vRet = CJS_Value(pRuntime, pRetObj);
559 return TRUE; 565 return TRUE;
560 } 566 }
561 567
562 FX_BOOL app::clearTimeOut(IJS_Context* cc, 568 FX_BOOL app::clearTimeOut(IJS_Context* cc,
563 const std::vector<CJS_Value>& params, 569 const std::vector<CJS_Value>& params,
564 CJS_Value& vRet, 570 CJS_Value& vRet,
565 CFX_WideString& sError) { 571 CFX_WideString& sError) {
566 CJS_Context* pContext = (CJS_Context*)cc; 572 CJS_Context* pContext = (CJS_Context*)cc;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 return FALSE; 619 return FALSE;
614 } 620 }
615 621
616 void app::TimerProc(GlobalTimer* pTimer) { 622 void app::TimerProc(GlobalTimer* pTimer) {
617 CJS_Runtime* pRuntime = pTimer->GetRuntime(); 623 CJS_Runtime* pRuntime = pTimer->GetRuntime();
618 if (pRuntime && (!pTimer->IsOneShot() || pTimer->GetTimeOut() > 0)) 624 if (pRuntime && (!pTimer->IsOneShot() || pTimer->GetTimeOut() > 0))
619 RunJsScript(pRuntime, pTimer->GetJScript()); 625 RunJsScript(pRuntime, pTimer->GetJScript());
620 } 626 }
621 627
622 void app::CancelProc(GlobalTimer* pTimer) { 628 void app::CancelProc(GlobalTimer* pTimer) {
623 auto iter = std::find_if(m_Timers.begin(), m_Timers.end(), 629 m_Timers.erase(pTimer);
624 [pTimer](const std::unique_ptr<GlobalTimer>& that) {
625 return pTimer == that.get();
626 });
627
628 if (iter != m_Timers.end())
629 m_Timers.erase(iter);
630 } 630 }
631 631
632 void app::RunJsScript(CJS_Runtime* pRuntime, const CFX_WideString& wsScript) { 632 void app::RunJsScript(CJS_Runtime* pRuntime, const CFX_WideString& wsScript) {
633 if (!pRuntime->IsBlocking()) { 633 if (!pRuntime->IsBlocking()) {
634 IJS_Context* pContext = pRuntime->NewContext(); 634 IJS_Context* pContext = pRuntime->NewContext();
635 pContext->OnExternal_Exec(); 635 pContext->OnExternal_Exec();
636 CFX_WideString wtInfo; 636 CFX_WideString wtInfo;
637 pContext->RunScript(wsScript, &wtInfo); 637 pContext->RunScript(wsScript, &wtInfo);
638 pRuntime->ReleaseContext(pContext); 638 pRuntime->ReleaseContext(pContext);
639 } 639 }
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 FX_BOOL app::media(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) { 834 FX_BOOL app::media(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
835 return FALSE; 835 return FALSE;
836 } 836 }
837 837
838 FX_BOOL app::execDialog(IJS_Context* cc, 838 FX_BOOL app::execDialog(IJS_Context* cc,
839 const std::vector<CJS_Value>& params, 839 const std::vector<CJS_Value>& params,
840 CJS_Value& vRet, 840 CJS_Value& vRet,
841 CFX_WideString& sError) { 841 CFX_WideString& sError) {
842 return TRUE; 842 return TRUE;
843 } 843 }
OLDNEW
« no previous file with comments | « fpdfsdk/javascript/app.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698