OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium 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 #include "pdf/timer.h" |
| 6 |
| 7 #include "ppapi/cpp/core.h" |
| 8 #include "ppapi/cpp/module.h" |
| 9 |
| 10 namespace chrome_pdf { |
| 11 |
| 12 static TestTimerRunner* g_test_timer_runner = nullptr; |
| 13 |
| 14 Timer::Timer(int delay) : delay_(delay), callback_factory_(this) { |
| 15 PostCallback(); |
| 16 } |
| 17 |
| 18 Timer::~Timer() { |
| 19 if (g_test_timer_runner) { |
| 20 g_test_timer_runner->UnregisterTimer(this); |
| 21 } |
| 22 } |
| 23 |
| 24 void Timer::PostCallback() { |
| 25 if (g_test_timer_runner) { |
| 26 g_test_timer_runner->RegisterTimer(this); |
| 27 } else { |
| 28 pp::CompletionCallback callback = |
| 29 callback_factory_.NewCallback(&Timer::TimerProc); |
| 30 pp::Module::Get()->core()->CallOnMainThread(delay_, callback, 0); |
| 31 } |
| 32 } |
| 33 |
| 34 void Timer::TimerProc(int32_t /*result*/) { |
| 35 PostCallback(); |
| 36 OnTimer(); |
| 37 } |
| 38 |
| 39 TestTimerRunner::TestTimerRunner() { |
| 40 PP_DCHECK(!g_test_timer_runner); |
| 41 g_test_timer_runner = this; |
| 42 } |
| 43 |
| 44 TestTimerRunner::~TestTimerRunner() { |
| 45 PP_DCHECK(scheduled_timers_.empty()); |
| 46 PP_DCHECK(g_test_timer_runner == this); |
| 47 g_test_timer_runner = nullptr; |
| 48 } |
| 49 |
| 50 void TestTimerRunner::RunUntilIdle() { |
| 51 while (!scheduled_timers_.empty()) { |
| 52 RunScheduled(); |
| 53 } |
| 54 } |
| 55 |
| 56 void TestTimerRunner::RunScheduled() { |
| 57 auto saved_timers = scheduled_timers_; |
| 58 for (auto& it : saved_timers) { |
| 59 if (scheduled_timers_.count(it)) { |
| 60 it->TimerProc(0); |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 int TestTimerRunner::scheduled_count() const { |
| 66 return static_cast<int>(scheduled_timers_.size()); |
| 67 } |
| 68 |
| 69 void TestTimerRunner::RegisterTimer(Timer* timer) { |
| 70 scheduled_timers_.insert(timer); |
| 71 } |
| 72 |
| 73 void TestTimerRunner::UnregisterTimer(Timer* timer) { |
| 74 scheduled_timers_.erase(timer); |
| 75 } |
| 76 |
| 77 } // namespace chrome_pdf |
OLD | NEW |