OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 20 matching lines...) Expand all Loading... |
31 #ifndef AsyncMethodRunner_h | 31 #ifndef AsyncMethodRunner_h |
32 #define AsyncMethodRunner_h | 32 #define AsyncMethodRunner_h |
33 | 33 |
34 #include "platform/Timer.h" | 34 #include "platform/Timer.h" |
35 #include "wtf/Allocator.h" | 35 #include "wtf/Allocator.h" |
36 #include "wtf/Noncopyable.h" | 36 #include "wtf/Noncopyable.h" |
37 | 37 |
38 namespace blink { | 38 namespace blink { |
39 | 39 |
40 template <typename TargetClass> | 40 template <typename TargetClass> |
41 class AsyncMethodRunner final { | 41 class AsyncMethodRunner final : public GarbageCollectedFinalized<AsyncMethodRunn
er<TargetClass>> { |
42 WTF_MAKE_NONCOPYABLE(AsyncMethodRunner); | 42 WTF_MAKE_NONCOPYABLE(AsyncMethodRunner); |
43 USING_FAST_MALLOC(AsyncMethodRunner); | |
44 public: | 43 public: |
45 typedef void (TargetClass::*TargetMethod)(); | 44 typedef void (TargetClass::*TargetMethod)(); |
46 | 45 |
47 AsyncMethodRunner(TargetClass* object, TargetMethod method) | 46 static AsyncMethodRunner* create(TargetClass* object, TargetMethod method) |
48 : m_timer(this, &AsyncMethodRunner<TargetClass>::fired) | 47 { |
49 , m_object(object) | 48 return new AsyncMethodRunner(object, method); |
50 , m_method(method) | 49 } |
51 , m_suspended(false) | 50 |
52 , m_runWhenResumed(false) | 51 ~AsyncMethodRunner() |
53 { | 52 { |
54 } | 53 } |
55 | 54 |
56 // Schedules to run the method asynchronously. Do nothing if it's already | 55 // 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 | 56 // scheduled. If it's suspended, remember to schedule to run the method when |
58 // resume() is called. | 57 // resume() is called. |
59 void runAsync() | 58 void runAsync() |
60 { | 59 { |
61 if (m_suspended) { | 60 if (m_suspended) { |
62 ASSERT(!m_timer.isActive()); | 61 ASSERT(!m_timer.isActive()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 ASSERT(!m_runWhenResumed); | 111 ASSERT(!m_runWhenResumed); |
113 if (m_timer.isActive()) | 112 if (m_timer.isActive()) |
114 m_timer.stop(); | 113 m_timer.stop(); |
115 } | 114 } |
116 | 115 |
117 bool isActive() const | 116 bool isActive() const |
118 { | 117 { |
119 return m_timer.isActive(); | 118 return m_timer.isActive(); |
120 } | 119 } |
121 | 120 |
| 121 DEFINE_INLINE_TRACE() |
| 122 { |
| 123 #if ENABLE(OILPAN) |
| 124 visitor->trace(m_object); |
| 125 #else |
| 126 TraceIfNeeded<typename PointerFieldStorageTrait<TargetClass>::Type>::tra
ce(visitor, m_object); |
| 127 #endif |
| 128 } |
| 129 |
122 private: | 130 private: |
| 131 AsyncMethodRunner(TargetClass* object, TargetMethod method) |
| 132 : m_timer(this, &AsyncMethodRunner<TargetClass>::fired) |
| 133 , m_object(object) |
| 134 , m_method(method) |
| 135 , m_suspended(false) |
| 136 , m_runWhenResumed(false) |
| 137 { |
| 138 } |
| 139 |
123 void fired(Timer<AsyncMethodRunner<TargetClass>>*) | 140 void fired(Timer<AsyncMethodRunner<TargetClass>>*) |
124 { | 141 { |
125 // TODO(Oilpan): when AsyncMethodRunner is on the heap, this check becom
es | |
126 // redundant; handled directly by Timer<> instead. | |
127 if (!TimerIsObjectAliveTrait<TargetClass>::isHeapObjectAlive(m_object)) | |
128 return; | |
129 (m_object->*m_method)(); | 142 (m_object->*m_method)(); |
130 } | 143 } |
131 | 144 |
132 Timer<AsyncMethodRunner<TargetClass>> m_timer; | 145 Timer<AsyncMethodRunner<TargetClass>> m_timer; |
133 | 146 |
134 // TODO(Oilpan): AsyncMethodRunner should be moved to the heap and m_object
should be traced. | 147 #if ENABLE(OILPAN) |
135 // This raw pointer is safe as long as AsyncMethodRunner<X> is held by the X
itself | 148 Member<TargetClass> m_object; |
136 // (That's the case in the current code base). | 149 #else |
137 GC_PLUGIN_IGNORE("363031") | 150 typename PointerFieldStorageTrait<TargetClass>::Type m_object; |
138 TargetClass* m_object; | 151 #endif |
139 TargetMethod m_method; | 152 TargetMethod m_method; |
140 | 153 |
141 bool m_suspended; | 154 bool m_suspended; |
142 bool m_runWhenResumed; | 155 bool m_runWhenResumed; |
143 }; | 156 }; |
144 | 157 |
145 } | 158 } |
146 | 159 |
147 #endif | 160 #endif |
OLD | NEW |