| Index: pdf/timer.cc
|
| diff --git a/pdf/timer.cc b/pdf/timer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..521a8d682fde41f6b4c91fae8640690c49e159ff
|
| --- /dev/null
|
| +++ b/pdf/timer.cc
|
| @@ -0,0 +1,77 @@
|
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "pdf/timer.h"
|
| +
|
| +#include "ppapi/cpp/core.h"
|
| +#include "ppapi/cpp/module.h"
|
| +
|
| +namespace chrome_pdf {
|
| +
|
| +static TestTimerRunner* g_test_timer_runner = nullptr;
|
| +
|
| +Timer::Timer(int delay) : delay_(delay), callback_factory_(this) {
|
| + PostCallback();
|
| +}
|
| +
|
| +Timer::~Timer() {
|
| + if (g_test_timer_runner) {
|
| + g_test_timer_runner->UnregisterTimer(this);
|
| + }
|
| +}
|
| +
|
| +void Timer::PostCallback() {
|
| + if (g_test_timer_runner) {
|
| + g_test_timer_runner->RegisterTimer(this);
|
| + } else {
|
| + pp::CompletionCallback callback =
|
| + callback_factory_.NewCallback(&Timer::TimerProc);
|
| + pp::Module::Get()->core()->CallOnMainThread(delay_, callback, 0);
|
| + }
|
| +}
|
| +
|
| +void Timer::TimerProc(int32_t /*result*/) {
|
| + PostCallback();
|
| + OnTimer();
|
| +}
|
| +
|
| +TestTimerRunner::TestTimerRunner() {
|
| + PP_DCHECK(!g_test_timer_runner);
|
| + g_test_timer_runner = this;
|
| +}
|
| +
|
| +TestTimerRunner::~TestTimerRunner() {
|
| + PP_DCHECK(scheduled_timers_.empty());
|
| + PP_DCHECK(g_test_timer_runner == this);
|
| + g_test_timer_runner = nullptr;
|
| +}
|
| +
|
| +void TestTimerRunner::RunUntilIdle() {
|
| + while (!scheduled_timers_.empty()) {
|
| + RunScheduled();
|
| + }
|
| +}
|
| +
|
| +void TestTimerRunner::RunScheduled() {
|
| + auto saved_timers = scheduled_timers_;
|
| + for (auto& it : saved_timers) {
|
| + if (scheduled_timers_.count(it)) {
|
| + it->TimerProc(0);
|
| + }
|
| + }
|
| +}
|
| +
|
| +int TestTimerRunner::scheduled_count() const {
|
| + return static_cast<int>(scheduled_timers_.size());
|
| +}
|
| +
|
| +void TestTimerRunner::RegisterTimer(Timer* timer) {
|
| + scheduled_timers_.insert(timer);
|
| +}
|
| +
|
| +void TestTimerRunner::UnregisterTimer(Timer* timer) {
|
| + scheduled_timers_.erase(timer);
|
| +}
|
| +
|
| +} // namespace chrome_pdf
|
|
|