2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef AsyncMethodRunner_h
32 #define AsyncMethodRunner_h
34 #include "platform/Timer.h"
35 #include "wtf/FastAllocBase.h"
36 #include "wtf/Noncopyable.h"
40 template <typename TargetClass
>
41 class AsyncMethodRunner final
{
42 WTF_MAKE_NONCOPYABLE(AsyncMethodRunner
);
43 WTF_MAKE_FAST_ALLOCATED(AsyncMethodRunner
);
45 typedef void (TargetClass::*TargetMethod
)();
47 AsyncMethodRunner(TargetClass
* object
, TargetMethod method
)
48 : m_timer(this, &AsyncMethodRunner
<TargetClass
>::fired
)
52 , m_runWhenResumed(false)
56 // Schedules to run the method asynchronously. Do nothing if it's already
57 // scheduled. If it's suspended, remember to schedule to run the method when
58 // resume() is called.
62 ASSERT(!m_timer
.isActive());
63 m_runWhenResumed
= true;
67 // FIXME: runAsync should take a TraceLocation and pass it to timer here.
68 if (!m_timer
.isActive())
69 m_timer
.startOneShot(0, FROM_HERE
);
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().
81 if (!m_timer
.isActive())
85 m_runWhenResumed
= true;
88 // Resumes pending method run.
95 if (!m_runWhenResumed
)
98 m_runWhenResumed
= false;
99 // FIXME: resume should take a TraceLocation and pass it to timer here.
100 m_timer
.startOneShot(0, FROM_HERE
);
106 ASSERT(!m_timer
.isActive());
107 m_runWhenResumed
= false;
112 ASSERT(!m_runWhenResumed
);
113 if (m_timer
.isActive())
117 bool isActive() const
119 return m_timer
.isActive();
123 void fired(Timer
<AsyncMethodRunner
<TargetClass
>>*)
125 // TODO(Oilpan): when AsyncMethodRunner is on the heap, this check becomes
126 // redundant; handled directly by Timer<> instead.
127 if (!TimerIsObjectAliveTrait
<TargetClass
>::isHeapObjectAlive(m_object
))
129 (m_object
->*m_method
)();
132 Timer
<AsyncMethodRunner
<TargetClass
>> m_timer
;
134 // TODO(Oilpan): AsyncMethodRunner should be moved to the heap and m_object should be traced.
135 // This raw pointer is safe as long as AsyncMethodRunner<X> is held by the X itself
136 // (That's the case in the current code base).
137 GC_PLUGIN_IGNORE("363031")
138 TargetClass
* m_object
;
139 TargetMethod m_method
;
142 bool m_runWhenResumed
;