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"
10 interface nsIEventTarget
;
13 #include
"mozilla/MemoryReporting.h"
14 #include
"mozilla/TimeStamp.h"
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
27 typedef void (*nsTimerCallbackFunc
) (nsITimer
* aTimer
, void* aClosure
);
30 native MallocSizeOf
(mozilla
::MallocSizeOf
);
31 native nsTimerCallbackFunc
(nsTimerCallbackFunc
);
32 [ref] native TimeDuration
(mozilla
::TimeDuration
);
35 * The callback interface for timers.
39 [function
, scriptable
, uuid(a796816d
-7d47
-4348-9ab8
-c7aeb3216a7d
)]
40 interface nsITimerCallback
: nsISupports
43 * @param aTimer the timer which has expired
45 void notify
(in nsITimer timer
);
49 // Two timer deadlines must differ by less than half the PRIntervalTime domain.
50 #define DELAY_INTERVAL_LIMIT PR_BIT
(8 * sizeof
(PRIntervalTime
) - 1)
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
72 * Type of a timer that fires once only.
74 const short TYPE_ONE_SHOT
= 0;
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;
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;
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
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
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).
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
,
187 in unsigned long aDelay
,
188 in unsigned long aType
,
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
,
204 [const] in TimeDuration aDelay
,
205 in unsigned long aType
,
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
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
246 [noscript
] readonly attribute
unsigned long allowedEarlyFiringMicroseconds
;
248 [notxpcom
, nostdcall
] size_t sizeOfIncludingThis
(in MallocSizeOf aMallocSizeOf
);
252 #include
"nsCOMPtr.h"
254 already_AddRefed
<nsITimer
> NS_NewTimer
();
256 already_AddRefed
<nsITimer
> NS_NewTimer
(nsIEventTarget
* aTarget
);
259 NS_NewTimerWithObserver
(nsITimer
** aTimer
,
260 nsIObserver
* aObserver
,
263 nsIEventTarget
* aTarget
= nullptr
);
264 mozilla
::Result
<nsCOMPtr
<nsITimer
>, nsresult
>
265 NS_NewTimerWithObserver
(nsIObserver
* aObserver
,
268 nsIEventTarget
* aTarget
= nullptr
);
271 NS_NewTimerWithCallback
(nsITimer
** aTimer
,
272 nsITimerCallback
* aCallback
,
275 nsIEventTarget
* aTarget
= nullptr
);
276 mozilla
::Result
<nsCOMPtr
<nsITimer
>, nsresult
>
277 NS_NewTimerWithCallback
(nsITimerCallback
* aCallback
,
280 nsIEventTarget
* aTarget
= nullptr
);
283 NS_NewTimerWithCallback
(nsITimer
** aTimer
,
284 nsITimerCallback
* aCallback
,
285 const mozilla
::TimeDuration
& aDelay
,
287 nsIEventTarget
* aTarget
= nullptr
);
288 mozilla
::Result
<nsCOMPtr
<nsITimer
>, nsresult
>
289 NS_NewTimerWithCallback
(nsITimerCallback
* aCallback
,
290 const mozilla
::TimeDuration
& aDelay
,
292 nsIEventTarget
* aTarget
= nullptr
);
295 NS_NewTimerWithCallback
(nsITimer
** aTimer
,
296 std
::function
<void(nsITimer
*)>&& aCallback
,
299 const char* aNameString
,
300 nsIEventTarget
* aTarget
= nullptr
);
301 mozilla
::Result
<nsCOMPtr
<nsITimer
>, nsresult
>
302 NS_NewTimerWithCallback
(std
::function
<void(nsITimer
*)>&& aCallback
,
305 const char* aNameString
,
306 nsIEventTarget
* aTarget
= nullptr
);
309 NS_NewTimerWithCallback
(nsITimer
** aTimer
,
310 std
::function
<void(nsITimer
*)>&& aCallback
,
311 const mozilla
::TimeDuration
& aDelay
,
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
,
319 const char* aNameString
,
320 nsIEventTarget
* aTarget
= nullptr
);
323 NS_NewTimerWithFuncCallback
(nsITimer
** aTimer
,
324 nsTimerCallbackFunc aCallback
,
328 const char* aNameString
,
329 nsIEventTarget
* aTarget
= nullptr
);
330 mozilla
::Result
<nsCOMPtr
<nsITimer
>, nsresult
>
331 NS_NewTimerWithFuncCallback
(nsTimerCallbackFunc aCallback
,
335 const char* aNameString
,
336 nsIEventTarget
* aTarget
= nullptr
);
339 NS_NewTimerWithFuncCallback
(nsITimer
** aTimer
,
340 nsTimerCallbackFunc aCallback
,
342 const mozilla
::TimeDuration
& aDelay
,
344 const char* aNameString
,
345 nsIEventTarget
* aTarget
= nullptr
);
346 mozilla
::Result
<nsCOMPtr
<nsITimer
>, nsresult
>
347 NS_NewTimerWithFuncCallback
(nsTimerCallbackFunc aCallback
,
349 const mozilla
::TimeDuration
& aDelay
,
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"); \
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
();