OLD | NEW |
| (Empty) |
1 // Copyright 2011 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 "config.h" | |
6 | |
7 #include "cc/timer.h" | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/logging.h" | |
11 #include "cc/thread.h" | |
12 | |
13 namespace cc { | |
14 | |
15 class TimerTask : public Thread::Task { | |
16 public: | |
17 explicit TimerTask(Timer* timer) | |
18 : Thread::Task(0) | |
19 , m_timer(timer) | |
20 { | |
21 } | |
22 | |
23 virtual ~TimerTask() | |
24 { | |
25 if (!m_timer) | |
26 return; | |
27 | |
28 DCHECK(m_timer->m_task == this); | |
29 m_timer->stop(); | |
30 } | |
31 | |
32 virtual void performTask() OVERRIDE | |
33 { | |
34 if (!m_timer) | |
35 return; | |
36 | |
37 TimerClient* client = m_timer->m_client; | |
38 | |
39 m_timer->stop(); | |
40 if (client) | |
41 client->onTimerFired(); | |
42 } | |
43 | |
44 private: | |
45 friend class Timer; | |
46 | |
47 Timer* m_timer; // null if cancelled | |
48 }; | |
49 | |
50 Timer::Timer(Thread* thread, TimerClient* client) | |
51 : m_client(client) | |
52 , m_thread(thread) | |
53 , m_task(0) | |
54 { | |
55 } | |
56 | |
57 Timer::~Timer() | |
58 { | |
59 stop(); | |
60 } | |
61 | |
62 void Timer::startOneShot(double intervalSeconds) | |
63 { | |
64 stop(); | |
65 | |
66 m_task = new TimerTask(this); | |
67 | |
68 // The thread expects delays in milliseconds. | |
69 m_thread->postDelayedTask(adoptPtr(m_task), intervalSeconds * 1000.0); | |
70 } | |
71 | |
72 void Timer::stop() | |
73 { | |
74 if (!m_task) | |
75 return; | |
76 | |
77 m_task->m_timer = 0; | |
78 m_task = 0; | |
79 } | |
80 | |
81 } // namespace cc | |
OLD | NEW |