Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / Timer.h
blob5ee63b819918f5f35418da2afa457343177d791e
1 /*
2 * Copyright (C) 2006 Apple Computer, 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef Timer_h
27 #define Timer_h
29 #include "platform/PlatformExport.h"
30 #include "platform/heap/Handle.h"
31 #include "public/platform/WebTaskRunner.h"
32 #include "public/platform/WebTraceLocation.h"
33 #include "wtf/AddressSanitizer.h"
34 #include "wtf/Noncopyable.h"
35 #include "wtf/Threading.h"
36 #include "wtf/Vector.h"
38 namespace blink {
40 // Time intervals are all in seconds.
42 class PLATFORM_EXPORT TimerBase {
43 WTF_MAKE_NONCOPYABLE(TimerBase);
44 public:
45 TimerBase();
46 virtual ~TimerBase();
48 void start(double nextFireInterval, double repeatInterval, const WebTraceLocation&);
50 void startRepeating(double repeatInterval, const WebTraceLocation& caller)
52 start(repeatInterval, repeatInterval, caller);
54 void startOneShot(double interval, const WebTraceLocation& caller)
56 start(interval, 0, caller);
59 void stop();
60 bool isActive() const;
61 const WebTraceLocation& location() const { return m_location; }
63 double nextFireInterval() const;
64 double nextUnalignedFireInterval() const;
65 double repeatInterval() const { return m_repeatInterval; }
67 void augmentRepeatInterval(double delta) {
68 double now = monotonicallyIncreasingTime();
69 setNextFireTime(now, m_nextFireTime - now + delta);
70 m_repeatInterval += delta;
73 void didChangeAlignmentInterval(double now);
75 struct PLATFORM_EXPORT Comparator {
76 bool operator()(const TimerBase* a, const TimerBase* b) const;
79 private:
80 virtual void fired() = 0;
82 NO_LAZY_SWEEP_SANITIZE_ADDRESS
83 virtual bool canFire() const { return true; }
85 virtual double alignedFireTime(double fireTime) const { return fireTime; }
87 void setNextFireTime(double now, double delay);
89 void runInternal();
91 class CancellableTimerTask final : public WebTaskRunner::Task {
92 WTF_MAKE_NONCOPYABLE(CancellableTimerTask);
93 public:
94 explicit CancellableTimerTask(TimerBase* timer) : m_timer(timer) { }
96 ~CancellableTimerTask() override
98 if (m_timer)
99 m_timer->m_cancellableTimerTask = nullptr;
102 NO_LAZY_SWEEP_SANITIZE_ADDRESS
103 void run() override
105 if (m_timer) {
106 m_timer->m_cancellableTimerTask = nullptr;
107 m_timer->runInternal();
108 m_timer = nullptr;
112 void cancel()
114 m_timer = nullptr;
117 private:
118 TimerBase* m_timer; // NOT OWNED
121 double m_nextFireTime; // 0 if inactive
122 double m_unalignedNextFireTime; // m_nextFireTime not considering alignment interval
123 double m_repeatInterval; // 0 if not repeating
124 WebTraceLocation m_location;
125 CancellableTimerTask* m_cancellableTimerTask; // NOT OWNED
126 WebScheduler* m_webScheduler; // Not owned.
128 #if ENABLE(ASSERT)
129 ThreadIdentifier m_thread;
130 #endif
132 friend class ThreadTimers;
133 friend class TimerHeapLessThanFunction;
134 friend class TimerHeapReference;
137 template<typename T, bool = IsGarbageCollectedType<T>::value>
138 class TimerIsObjectAliveTrait {
139 public:
140 static bool isHeapObjectAlive(T*) { return true; }
143 template<typename T>
144 class TimerIsObjectAliveTrait<T, true> {
145 public:
146 static bool isHeapObjectAlive(T* objectPointer)
148 return !Heap::willObjectBeLazilySwept(objectPointer);
152 template <typename TimerFiredClass>
153 class Timer : public TimerBase {
154 public:
155 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
157 Timer(TimerFiredClass* o, TimerFiredFunction f)
158 : m_object(o), m_function(f)
162 protected:
163 void fired() override
165 (m_object->*m_function)(this);
168 NO_LAZY_SWEEP_SANITIZE_ADDRESS
169 bool canFire() const override
171 // Oilpan: if a timer fires while Oilpan heaps are being lazily
172 // swept, it is not safe to proceed if the object is about to
173 // be swept (and this timer will be stopped while doing so.)
174 return TimerIsObjectAliveTrait<TimerFiredClass>::isHeapObjectAlive(m_object);
177 private:
178 // FIXME: Oilpan: TimerBase should be moved to the heap and m_object should be traced.
179 // This raw pointer is safe as long as Timer<X> is held by the X itself (That's the case
180 // in the current code base).
181 GC_PLUGIN_IGNORE("363031")
182 TimerFiredClass* m_object;
183 TimerFiredFunction m_function;
186 NO_LAZY_SWEEP_SANITIZE_ADDRESS
187 inline bool TimerBase::isActive() const
189 ASSERT(m_thread == currentThread());
190 return m_cancellableTimerTask;
193 } // namespace blink
195 #endif // Timer_h