Bug 1946787 - Avoid creating redundant GradientCache::OnMaxEntriesBreached tasks...
[gecko.git] / xpcom / threads / nsITimer.idl
blobd150722d14e49fec755017c2185c2c3073c21feb
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsISupports.idl"
7 #include "nsINamed.idl"
9 interface nsIObserver;
10 interface nsIEventTarget;
12 %{C++
13 #include "mozilla/MemoryReporting.h"
14 #include "mozilla/TimeStamp.h"
15 #include <functional>
17 /**
18 * The signature of the timer callback function passed to
19 * initWithNamedFuncCallback and similar functions. This is the function that
20 * will get called when the timer expires if the timer is initialized via
21 * initWithNamedFuncCallback.
23 * @param aTimer the timer which has expired
24 * @param aClosure opaque parameter passed to initWithNamedFuncCallback
26 class nsITimer;
27 typedef void (*nsTimerCallbackFunc) (nsITimer* aTimer, void* aClosure);
30 native MallocSizeOf(mozilla::MallocSizeOf);
31 native nsTimerCallbackFunc(nsTimerCallbackFunc);
32 [ref] native TimeDuration(mozilla::TimeDuration);
34 /**
35 * The callback interface for timers.
37 interface nsITimer;
39 [function, scriptable, uuid(a796816d-7d47-4348-9ab8-c7aeb3216a7d)]
40 interface nsITimerCallback : nsISupports
42 /**
43 * @param aTimer the timer which has expired
45 void notify(in nsITimer timer);
48 %{C++
49 // Two timer deadlines must differ by less than half the PRIntervalTime domain.
50 #define DELAY_INTERVAL_LIMIT PR_BIT(8 * sizeof(PRIntervalTime) - 1)
53 /**
54 * nsITimer instances must be initialized by calling one of the "init" methods
55 * documented below. You may also re-initialize (using one of the init()
56 * methods) an existing instance to avoid the overhead of destroying and
57 * creating a timer. It is not necessary to cancel the timer in that case.
59 * By default a timer will fire on the thread that created it. Set the .target
60 * attribute to fire on a different thread. Once you have set a timer's .target
61 * and called one of its init functions, any further interactions with the timer
62 * (calling cancel(), changing member fields, etc) should only be done by the
63 * target thread, or races may occur with bad results like timers firing after
64 * they've been canceled, and/or not firing after re-initiatization.
66 [scriptable, builtinclass, uuid(3de4b105-363c-482c-a409-baac83a01bfc)]
67 interface nsITimer : nsISupports
69 /* Timer types */
71 /**
72 * Type of a timer that fires once only.
74 const short TYPE_ONE_SHOT = 0;
76 /**
77 * After firing, a TYPE_REPEATING_SLACK timer is stopped and not restarted
78 * until its callback completes. Specified timer period will be at least
79 * the time between when processing for last firing the callback completes
80 * and when the next firing occurs.
82 * This is the preferable repeating type for most situations.
84 const short TYPE_REPEATING_SLACK = 1;
86 /**
87 * TYPE_REPEATING_PRECISE is just a synonym for
88 * TYPE_REPEATING_PRECISE_CAN_SKIP. They used to be distinct, but the old
89 * TYPE_REPEATING_PRECISE kind was similar to TYPE_REPEATING_PRECISE_CAN_SKIP
90 * while also being less useful. So the distinction was removed.
92 const short TYPE_REPEATING_PRECISE = 2;
94 /**
95 * A TYPE_REPEATING_PRECISE_CAN_SKIP repeating timer aims to have constant
96 * period between firings. The processing time for each timer callback will
97 * not influence the timer period. If the callback finishes after the next
98 * firing(s) should have happened (either because the callback took a long
99 * time, or the callback was called extremely late), that firing(s) is
100 * skipped, but the following sequence of firing times will not be altered.
101 * This timer type guarantees that it will not queue up new events to fire
102 * the callback until the previous callback event finishes firing. This is
103 * the only non-slack timer available.
105 const short TYPE_REPEATING_PRECISE_CAN_SKIP = 3;
108 * Same as TYPE_REPEATING_SLACK with the exception that idle events
109 * won't yield to timers with this type. Use this when you want an
110 * idle callback to be scheduled to run even though this timer is
111 * about to fire.
113 const short TYPE_REPEATING_SLACK_LOW_PRIORITY = 4;
116 * Same as TYPE_ONE_SHOT with the exception that idle events won't
117 * yield to timers with this type. Use this when you want an idle
118 * callback to be scheduled to run even though this timer is about
119 * to fire.
121 const short TYPE_ONE_SHOT_LOW_PRIORITY = 5;
124 * Initialize a timer that will fire after the said delay.
125 * A user must keep a reference to this timer till it is
126 * is no longer needed or has been cancelled.
128 * @param aObserver the callback object that observes the
129 * ``timer-callback'' topic with the subject being
130 * the timer itself when the timer fires:
132 * observe(nsISupports aSubject, => nsITimer
133 * string aTopic, => ``timer-callback''
134 * wstring data => null
136 * @param aDelayInMs delay in milliseconds for timer to fire
137 * @param aType timer type per TYPE* consts defined above
139 void init(in nsIObserver aObserver, in unsigned long aDelayInMs,
140 in unsigned long aType);
144 * Initialize a timer to fire after the given millisecond interval.
145 * This version takes a callback object.
147 * @param aFunc nsITimerCallback interface to call when timer expires
148 * @param aDelayInMs The millisecond interval
149 * @param aType Timer type per TYPE* consts defined above
151 void initWithCallback(in nsITimerCallback aCallback,
152 in unsigned long aDelayInMs,
153 in unsigned long aType);
156 * Initialize a timer to fire after the high resolution TimeDuration.
157 * This version takes a callback object.
159 * @param aFunc nsITimerCallback interface to call when timer expires
160 * @param aDelay The high resolution interval
161 * @param aType Timer type per TYPE* consts defined above
163 [noscript] void initHighResolutionWithCallback(in nsITimerCallback aCallback,
164 [const] in TimeDuration aDelay,
165 in unsigned long aType);
168 * Cancel the timer. This method works on all types, not just on repeating
169 * timers -- you might want to cancel a TYPE_ONE_SHOT timer, and even reuse
170 * it by re-initializing it (to avoid object destruction and creation costs
171 * by conserving one timer instance).
173 void cancel();
176 * Initialize a timer to fire after the given millisecond interval.
177 * This version takes a named function callback.
179 * @param aFunc The function to invoke
180 * @param aClosure An opaque pointer to pass to that function
181 * @param aDelay The millisecond interval
182 * @param aType Timer type per TYPE* consts defined above
183 * @param aName The timer's name
185 [noscript] void initWithNamedFuncCallback(in nsTimerCallbackFunc aCallback,
186 in voidPtr aClosure,
187 in unsigned long aDelay,
188 in unsigned long aType,
189 in string aName);
192 * Initialize a timer to fire after the high resolution TimeDuration.
193 * This version takes a named function callback.
195 * @param aFunc The function to invoke
196 * @param aClosure An opaque pointer to pass to that function
197 * @param aDelay The high resolution interval
198 * @param aType Timer type per TYPE* consts defined above
199 * @param aName The timer's name
201 [noscript] void initHighResolutionWithNamedFuncCallback(
202 in nsTimerCallbackFunc aCallback,
203 in voidPtr aClosure,
204 [const] in TimeDuration aDelay,
205 in unsigned long aType,
206 in string aName);
209 * The millisecond delay of the timeout.
211 * NOTE: Re-setting the delay on a one-shot timer that has already fired
212 * doesn't restart the timer. Call one of the init() methods to restart
213 * a one-shot timer.
215 attribute unsigned long delay;
218 * The timer type - one of the above TYPE_* constants.
220 attribute unsigned long type;
223 * The opaque pointer passed to initWithNamedFuncCallback.
225 [noscript] readonly attribute voidPtr closure;
228 * The nsITimerCallback object passed to initWithCallback.
230 readonly attribute nsITimerCallback callback;
233 * The nsIEventTarget where the callback will be dispatched. Note that this
234 * target may only be set before the call to one of the init methods above.
236 * By default the target is the thread that created the timer.
238 attribute nsIEventTarget target;
240 readonly attribute ACString name;
243 * The number of microseconds this nsITimer implementation can possibly
244 * fire early.
246 [noscript] readonly attribute unsigned long allowedEarlyFiringMicroseconds;
248 [notxpcom, nostdcall] size_t sizeOfIncludingThis(in MallocSizeOf aMallocSizeOf);
251 %{C++
252 #include "nsCOMPtr.h"
254 already_AddRefed<nsITimer> NS_NewTimer();
256 already_AddRefed<nsITimer> NS_NewTimer(nsIEventTarget* aTarget);
258 nsresult
259 NS_NewTimerWithObserver(nsITimer** aTimer,
260 nsIObserver* aObserver,
261 uint32_t aDelay,
262 uint32_t aType,
263 nsIEventTarget* aTarget = nullptr);
264 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
265 NS_NewTimerWithObserver(nsIObserver* aObserver,
266 uint32_t aDelay,
267 uint32_t aType,
268 nsIEventTarget* aTarget = nullptr);
270 nsresult
271 NS_NewTimerWithCallback(nsITimer** aTimer,
272 nsITimerCallback* aCallback,
273 uint32_t aDelay,
274 uint32_t aType,
275 nsIEventTarget* aTarget = nullptr);
276 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
277 NS_NewTimerWithCallback(nsITimerCallback* aCallback,
278 uint32_t aDelay,
279 uint32_t aType,
280 nsIEventTarget* aTarget = nullptr);
282 nsresult
283 NS_NewTimerWithCallback(nsITimer** aTimer,
284 nsITimerCallback* aCallback,
285 const mozilla::TimeDuration& aDelay,
286 uint32_t aType,
287 nsIEventTarget* aTarget = nullptr);
288 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
289 NS_NewTimerWithCallback(nsITimerCallback* aCallback,
290 const mozilla::TimeDuration& aDelay,
291 uint32_t aType,
292 nsIEventTarget* aTarget = nullptr);
294 nsresult
295 NS_NewTimerWithCallback(nsITimer** aTimer,
296 std::function<void(nsITimer*)>&& aCallback,
297 uint32_t aDelay,
298 uint32_t aType,
299 const char* aNameString,
300 nsIEventTarget* aTarget = nullptr);
301 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
302 NS_NewTimerWithCallback(std::function<void(nsITimer*)>&& aCallback,
303 uint32_t aDelay,
304 uint32_t aType,
305 const char* aNameString,
306 nsIEventTarget* aTarget = nullptr);
308 nsresult
309 NS_NewTimerWithCallback(nsITimer** aTimer,
310 std::function<void(nsITimer*)>&& aCallback,
311 const mozilla::TimeDuration& aDelay,
312 uint32_t aType,
313 const char* aNameString,
314 nsIEventTarget* aTarget = nullptr);
315 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
316 NS_NewTimerWithCallback(std::function<void(nsITimer*)>&& aCallback,
317 const mozilla::TimeDuration& aDelay,
318 uint32_t aType,
319 const char* aNameString,
320 nsIEventTarget* aTarget = nullptr);
322 nsresult
323 NS_NewTimerWithFuncCallback(nsITimer** aTimer,
324 nsTimerCallbackFunc aCallback,
325 void* aClosure,
326 uint32_t aDelay,
327 uint32_t aType,
328 const char* aNameString,
329 nsIEventTarget* aTarget = nullptr);
330 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
331 NS_NewTimerWithFuncCallback(nsTimerCallbackFunc aCallback,
332 void* aClosure,
333 uint32_t aDelay,
334 uint32_t aType,
335 const char* aNameString,
336 nsIEventTarget* aTarget = nullptr);
338 nsresult
339 NS_NewTimerWithFuncCallback(nsITimer** aTimer,
340 nsTimerCallbackFunc aCallback,
341 void* aClosure,
342 const mozilla::TimeDuration& aDelay,
343 uint32_t aType,
344 const char* aNameString,
345 nsIEventTarget* aTarget = nullptr);
346 mozilla::Result<nsCOMPtr<nsITimer>, nsresult>
347 NS_NewTimerWithFuncCallback(nsTimerCallbackFunc aCallback,
348 void* aClosure,
349 const mozilla::TimeDuration& aDelay,
350 uint32_t aType,
351 const char* aNameString,
352 nsIEventTarget* aTarget = nullptr);
354 #define NS_TIMER_CALLBACK_TOPIC "timer-callback"
356 #ifndef RELEASE_OR_BETA
357 #undef NS_DECL_NSITIMERCALLBACK
358 #define NS_DECL_NSITIMERCALLBACK \
359 NS_IMETHOD Notify(nsITimer *timer) override; \
360 inline void _ensure_GetName_exists(void) { \
361 static_assert(std::is_convertible<decltype(this), nsINamed*>::value, \
362 "nsITimerCallback implementations must also implement nsINamed"); \
364 #endif
367 [scriptable, builtinclass, uuid(5482506d-1d21-4d08-b01c-95c87e1295ad)]
368 interface nsITimerManager : nsISupports
371 * Returns a read-only list of nsITimer objects, implementing only the name,
372 * delay and type attribute getters.
373 * This is meant to be used for tests, to verify that no timer is leftover
374 * at the end of a test. */
375 Array<nsITimer> getTimers();