|
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/. */ |
|
5 |
|
6 #include "nsISupports.idl" |
|
7 |
|
8 interface nsIObserver; |
|
9 interface nsIEventTarget; |
|
10 |
|
11 %{C++ |
|
12 /** |
|
13 * The signature of the timer callback function passed to initWithFuncCallback. |
|
14 * This is the function that will get called when the timer expires if the |
|
15 * timer is initialized via initWithFuncCallback. |
|
16 * |
|
17 * @param aTimer the timer which has expired |
|
18 * @param aClosure opaque parameter passed to initWithFuncCallback |
|
19 */ |
|
20 class nsITimer; |
|
21 typedef void (*nsTimerCallbackFunc) (nsITimer *aTimer, void *aClosure); |
|
22 %} |
|
23 |
|
24 native nsTimerCallbackFunc(nsTimerCallbackFunc); |
|
25 |
|
26 /** |
|
27 * The callback interface for timers. |
|
28 */ |
|
29 interface nsITimer; |
|
30 |
|
31 [function, scriptable, uuid(a796816d-7d47-4348-9ab8-c7aeb3216a7d)] |
|
32 interface nsITimerCallback : nsISupports |
|
33 { |
|
34 /** |
|
35 * @param aTimer the timer which has expired |
|
36 */ |
|
37 void notify(in nsITimer timer); |
|
38 }; |
|
39 |
|
40 %{C++ |
|
41 // Two timer deadlines must differ by less than half the PRIntervalTime domain. |
|
42 #define DELAY_INTERVAL_LIMIT PR_BIT(8 * sizeof(PRIntervalTime) - 1) |
|
43 %} |
|
44 |
|
45 /** |
|
46 * nsITimer instances must be initialized by calling one of the "init" methods |
|
47 * documented below. You may also re-initialize (using one of the init() |
|
48 * methods) an existing instance to avoid the overhead of destroying and |
|
49 * creating a timer. It is not necessary to cancel the timer in that case. |
|
50 * |
|
51 * By default a timer will fire on the thread that created it. Set the .target |
|
52 * attribute to fire on a different thread. Once you have set a timer's .target |
|
53 * and called one of its init functions, any further interactions with the timer |
|
54 * (calling cancel(), changing member fields, etc) should only be done by the |
|
55 * target thread, or races may occur with bad results like timers firing after |
|
56 * they've been canceled, and/or not firing after re-initiatization. |
|
57 */ |
|
58 [scriptable, uuid(193fc37a-8aa4-4d29-aa57-1acd87c26b66)] |
|
59 interface nsITimer : nsISupports |
|
60 { |
|
61 /* Timer types */ |
|
62 |
|
63 /** |
|
64 * Type of a timer that fires once only. |
|
65 */ |
|
66 const short TYPE_ONE_SHOT = 0; |
|
67 |
|
68 /** |
|
69 * After firing, a TYPE_REPEATING_SLACK timer is stopped and not restarted |
|
70 * until its callback completes. Specified timer period will be at least |
|
71 * the time between when processing for last firing the callback completes |
|
72 * and when the next firing occurs. |
|
73 * |
|
74 * This is the preferable repeating type for most situations. |
|
75 */ |
|
76 const short TYPE_REPEATING_SLACK = 1; |
|
77 |
|
78 /** |
|
79 * An TYPE_REPEATING_PRECISE repeating timer aims to have constant period |
|
80 * between firings. The processing time for each timer callback should not |
|
81 * influence the timer period. However, if the processing for the last |
|
82 * timer firing could not be completed until just before the next firing |
|
83 * occurs, then you could have two timer notification routines being |
|
84 * executed in quick succession. Furthermore, if your callback processing |
|
85 * time is longer than the timer period, then the timer will post more |
|
86 * notifications while your callback is running. For example, if a |
|
87 * REPEATING_PRECISE timer has a 10ms period and a callback takes 50ms, |
|
88 * then by the time the callback is done there will be 5 events to run the |
|
89 * timer callback in the event queue. Furthermore, the next scheduled time |
|
90 * will always advance by exactly the delay every time the timer fires. |
|
91 * This means that if the clock increments without the timer thread running |
|
92 * (e.g. the computer is asleep) when the timer thread gets to run again it |
|
93 * will post all the events that it "missed" while it wasn't running. Use |
|
94 * this timer type with extreme caution. Chances are, this is not what you |
|
95 * want. |
|
96 */ |
|
97 const short TYPE_REPEATING_PRECISE = 2; |
|
98 |
|
99 /** |
|
100 * A TYPE_REPEATING_PRECISE_CAN_SKIP repeating timer aims to have constant |
|
101 * period between firings. The processing time for each timer callback |
|
102 * should not influence the timer period. However this timer type |
|
103 * guarantees that it will not queue up new events to fire the callback |
|
104 * until the previous callback event finishes firing. If the callback |
|
105 * takes a long time, then the next callback will be scheduled immediately |
|
106 * afterward, but only once, unlike TYPE_REPEATING_PRECISE. If you want a |
|
107 * non-slack timer, you probably want this one. |
|
108 */ |
|
109 const short TYPE_REPEATING_PRECISE_CAN_SKIP = 3; |
|
110 |
|
111 /** |
|
112 * Initialize a timer that will fire after the said delay. |
|
113 * A user must keep a reference to this timer till it is |
|
114 * is no longer needed or has been cancelled. |
|
115 * |
|
116 * @param aObserver the callback object that observes the |
|
117 * ``timer-callback'' topic with the subject being |
|
118 * the timer itself when the timer fires: |
|
119 * |
|
120 * observe(nsISupports aSubject, => nsITimer |
|
121 * string aTopic, => ``timer-callback'' |
|
122 * wstring data => null |
|
123 * |
|
124 * @param aDelay delay in milliseconds for timer to fire |
|
125 * @param aType timer type per TYPE* consts defined above |
|
126 */ |
|
127 void init(in nsIObserver aObserver, in unsigned long aDelay, |
|
128 in unsigned long aType); |
|
129 |
|
130 |
|
131 /** |
|
132 * Initialize a timer to fire after the given millisecond interval. |
|
133 * This version takes a function to call and a closure to pass to |
|
134 * that function. |
|
135 * |
|
136 * @param aFunc The function to invoke |
|
137 * @param aClosure An opaque pointer to pass to that function |
|
138 * @param aDelay The millisecond interval |
|
139 * @param aType Timer type per TYPE* consts defined above |
|
140 */ |
|
141 [noscript] void initWithFuncCallback(in nsTimerCallbackFunc aCallback, |
|
142 in voidPtr aClosure, |
|
143 in unsigned long aDelay, |
|
144 in unsigned long aType); |
|
145 |
|
146 /** |
|
147 * Initialize a timer to fire after the given millisecond interval. |
|
148 * This version takes a function to call. |
|
149 * |
|
150 * @param aFunc nsITimerCallback interface to call when timer expires |
|
151 * @param aDelay The millisecond interval |
|
152 * @param aType Timer type per TYPE* consts defined above |
|
153 */ |
|
154 void initWithCallback(in nsITimerCallback aCallback, |
|
155 in unsigned long aDelay, |
|
156 in unsigned long aType); |
|
157 |
|
158 /** |
|
159 * Cancel the timer. This method works on all types, not just on repeating |
|
160 * timers -- you might want to cancel a TYPE_ONE_SHOT timer, and even reuse |
|
161 * it by re-initializing it (to avoid object destruction and creation costs |
|
162 * by conserving one timer instance). |
|
163 */ |
|
164 void cancel(); |
|
165 |
|
166 /** |
|
167 * The millisecond delay of the timeout. |
|
168 * |
|
169 * NOTE: Re-setting the delay on a one-shot timer that has already fired |
|
170 * doesn't restart the timer. Call one of the init() methods to restart |
|
171 * a one-shot timer. |
|
172 */ |
|
173 attribute unsigned long delay; |
|
174 |
|
175 /** |
|
176 * The timer type - one of the above TYPE_* constants. |
|
177 */ |
|
178 attribute unsigned long type; |
|
179 |
|
180 /** |
|
181 * The opaque pointer pass to initWithFuncCallback. |
|
182 */ |
|
183 [noscript] readonly attribute voidPtr closure; |
|
184 |
|
185 /** |
|
186 * The nsITimerCallback object passed to initWithCallback. |
|
187 */ |
|
188 readonly attribute nsITimerCallback callback; |
|
189 |
|
190 /** |
|
191 * The nsIEventTarget where the callback will be dispatched. Note that this |
|
192 * target may only be set before the call to one of the init methods above. |
|
193 * |
|
194 * By default the target is the thread that created the timer. |
|
195 */ |
|
196 attribute nsIEventTarget target; |
|
197 }; |
|
198 |
|
199 %{C++ |
|
200 #define NS_TIMER_CONTRACTID "@mozilla.org/timer;1" |
|
201 #define NS_TIMER_CALLBACK_TOPIC "timer-callback" |
|
202 %} |
|
203 |