OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #ifndef AsyncMethodRunner_h | |
32 #define AsyncMethodRunner_h | |
33 | |
34 #include "platform/PlatformExport.h" | |
35 #include "platform/Timer.h" | |
36 #include "wtf/FastAllocBase.h" | |
37 #include "wtf/Noncopyable.h" | |
38 | |
39 namespace WebCore { | |
40 | |
41 template <typename TargetClass> | |
42 class PLATFORM_EXPORT AsyncMethodRunner { | |
abarth-chromium
2013/10/22 18:46:51
No need for PLATFORM_EXPORT. The class is entirel
tyoshino (SeeGerritForStatus)
2013/10/23 06:02:30
got it. removed
| |
43 WTF_MAKE_NONCOPYABLE(AsyncMethodRunner); | |
44 WTF_MAKE_FAST_ALLOCATED; | |
45 public: | |
46 typedef void (TargetClass::*TargetMethod)(); | |
47 | |
48 AsyncMethodRunner(TargetClass* object, TargetMethod method) | |
49 : m_timer(this, &AsyncMethodRunner<TargetClass>::fired) | |
50 , m_object(object) | |
51 , m_method(method) | |
52 , m_suspended(false) | |
53 , m_runWhenResumed(false) | |
54 { | |
55 } | |
56 | |
57 // Schedules to run the method asynchronously. Do nothing if it's already | |
58 // scheduled. If it's suspended, remember to schedule to run the method when | |
59 // resume() is called. | |
60 void runAsync() | |
61 { | |
62 if (m_suspended) { | |
63 ASSERT(!m_timer.isActive()); | |
64 m_runWhenResumed = true; | |
65 return; | |
66 } | |
67 | |
68 if (!m_timer.isActive()) | |
69 m_timer.startOneShot(0); | |
70 } | |
71 | |
72 // If it's scheduled to run the method, cancel it and remember to schedule | |
73 // it again when resume() is called. Mainly for implementing | |
74 // ActiveDOMObject::suspend(). | |
75 void suspend() | |
76 { | |
77 ASSERT(!m_suspended); | |
78 m_suspended = true; | |
79 | |
80 if (!m_timer.isActive()) | |
81 return; | |
82 | |
83 m_timer.stop(); | |
84 m_runWhenResumed = true; | |
85 } | |
86 | |
87 // Resumes pending method run. | |
88 void resume() | |
89 { | |
90 ASSERT(m_suspended); | |
91 m_suspended = false; | |
92 | |
93 if (!m_runWhenResumed) | |
94 return; | |
95 | |
96 m_runWhenResumed = false; | |
97 m_timer.startOneShot(0); | |
98 } | |
99 | |
100 void stop() | |
101 { | |
102 if (m_suspended) { | |
103 ASSERT(!m_timer.isActive()); | |
104 m_runWhenResumed = false; | |
105 m_suspended = false; | |
106 return; | |
107 } | |
108 | |
109 ASSERT(!m_runWhenResumed); | |
110 if (m_timer.isActive()) | |
111 m_timer.stop(); | |
112 } | |
113 | |
114 private: | |
115 virtual void fired(Timer<AsyncMethodRunner<TargetClass> >*) { (m_object->*m_ method)(); } | |
116 | |
117 Timer<AsyncMethodRunner<TargetClass> > m_timer; | |
118 | |
119 TargetClass* m_object; | |
120 TargetMethod m_method; | |
121 | |
122 bool m_suspended; | |
123 bool m_runWhenResumed; | |
124 }; | |
125 | |
126 } | |
127 | |
128 #endif | |
OLD | NEW |