forked from sysapps/web-alarms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
578 lines (556 loc) · 31.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<!DOCTYPE html><html lang="en"><head>
<meta charset="utf-8">
<title>
Task Scheduler API Specification
</title>
<style type="text/css">
pre.idl { border:solid thin; background:#eee; color:#000; padding:0.5em }
pre.idl :link, pre.idl :visited { color:inherit; background:transparent }
pre code { color:inherit; background:transparent }
div.example { margin-left:1em; padding-left:1em; border-left:double; color:#222; background:#fcfcfc }
.note { margin-left:2em; font-weight:bold; font-style:italic; color:#008000 }
p.note::before { content:"Note: " }
.XXX { padding:.5em; border:solid #f00 }
p.XXX::before { content:"Issue: " }
dl.switch { padding-left:2em }
dl.switch > dt { text-indent:-1.5em }
dl.switch > dt:before { content:'\21AA'; padding:0 0.5em 0 0; display:inline-block; width:1em; text-align:right; line-height:0.5em }
dl.domintro { color: green; margin: 2em 0 2em 2em; padding: 0.5em 1em; border: none; background: #DDFFDD; }
dl.domintro dt, dl.domintro dt * { color: black; text-decoration: none; }
dl.domintro dd { margin: 0.5em 0 1em 2em; padding: 0; }
dl.domintro dd p { margin: 0.5em 0; }
dl.domintro:before { display: table; margin: -1em -0.5em -0.5em auto; width: auto; content: 'This box is non-normative. Implementation requirements are given below this box.'; color: red; border: solid 2px; background: white; padding: 0 0.25em; }
em.ct { text-transform:lowercase; font-variant:small-caps; font-style:normal }
dfn { font-weight:bold; font-style:normal }
code { color:orangered }
code :link, code :visited { color:inherit }
hr:not(.top) { display:block; background:none; border:none; padding:0; margin:2em 0; height:auto }
table { border-collapse:collapse; border-style:hidden hidden none hidden }
table thead { border-bottom:solid }
table tbody th:first-child { border-left:solid }
table td, table th { border-left:solid; border-right:solid; border-bottom:solid thin; vertical-align:top; padding:0.2em }
</style>
<link href="http://www.w3.org/StyleSheets/TR/W3C-ED" rel="stylesheet" type="text/css">
</head>
<body class="draft">
<div class="head">
<h1>
Task Scheduler API Specification
</h1>
<h2 class="no-num no-toc" id="editor-draft-—-13-october-2014">
Editor Draft — 13 October 2014
</h2>
<dl>
<dt>
This version:
</dt>
<dd>
<!--begin-link--><a href="http://www.w3.org/2012/sysapps/web-alarms/">http://www.w3.org/2012/sysapps/web-alarms/</a><!--end-link-->
</dd>
<dt class="dontpublish">
Participate:
</dt>
<dd class="dontpublish">
<a href="mailto:public-sysapps@w3.org?subject=%5Bweb-alarms%5D%20">public-sysapps@w3.org</a> (<a href="http://lists.w3.org/Archives/Public/public-sysapps/">archives</a>)
</dd>
<dd class="dontpublish">
<a href="https://github.com/sysapps/web-alarms/issues/new">File a bug</a>
</dd>
<dt>
Latest published version:
</dt>
<dd>
<!--begin-link--><a href="http://www.w3.org/TR/web-alarms/">http://www.w3.org/TR/web-alarms/</a><!--end-link-->
</dd>
<dt>
Latest editor's draft:
</dt>
<dd>
<!--begin-link--><a href="http://www.w3.org/2012/sysapps/web-alarms/">http://www.w3.org/2012/sysapps/web-alarms/</a><!--end-link-->
</dd>
<dt>
Previous versions:
</dt>
<dd>
<!--begin-link--><a href="http://www.w3.org/TR/2013/WD-web-alarms-20130205/ ">http://www.w3.org/TR/2013/WD-web-alarms-20130205/ </a><!--end-link-->
</dd>
<dt>
Editors:
</dt>
<dd class="vcard">
<span class="fn">Mahesh Kulkarni</span>, <span class="org"><a href="http://www.samsung.com/sec">Samsung Electronics,
Co., Ltd</a></span>, <span class="email"><a href="mailto:mahesh.kk@samsung.com">mahesh.kk@samsung.com</a></span>
</dd>
<dt>
Former Editors:
</dt>
<dd class="vcard">
<span class="fn">Christophe Dumez</span>, representing <span class="org"><a href="http://www.intel.com/">Intel</a></span> and <span class="org"><a href="http://www.samsung.com/sec">Samsung Electronics</a></span> (Until January 2013 and mid-August 2014, respectively)
</dd>
</dl>
<div class="w3conly">
<!--begin-copyright-->
<p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> © 2014 <a href="http://www.w3.org/"><abbr title="World Wide Web Consortium">W3C</abbr></a><sup>®</sup> (<a href="http://www.csail.mit.edu/"><abbr title="Massachusetts Institute of Technology">MIT</abbr></a>, <a href="http://www.ercim.eu/"><abbr title="European Research Consortium for Informatics and Mathematics">ERCIM</abbr></a>, <a href="http://www.keio.ac.jp/">Keio</a>, <a href="http://ev.buaa.edu.cn/">Beihang</a>), All Rights Reserved. W3C <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>, <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a> and <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document use</a> rules apply.</p>
<!--end-copyright-->
</div>
</div>
<h2 class="no-num no-toc" id="specabstract">
Abstract
</h2>
<p>
This specification defines an API to schedule a task at a specified time. When the indicated time is reached, the
application that scheduled the task will be notified via a functional event on a service worker. A task event will be delivered to a service worker, regardless
of whether the application is active on user agent. Applications such as an alarm clock or an
auto-updater may utilize this API to perform certain action at a specified time.
</p>
<h2 class="no-num no-toc" id="toc">
Table of Contents
</h2>
<!--begin-toc-->
<ol class="toc">
<li><a href="#introduction"><span class="secno">1 </span>
Introduction
</a></li>
<li><a href="#conformance"><span class="secno">2 </span>
Conformance
</a></li>
<li><a href="#terminology"><span class="secno">3 </span>
Terminology
</a></li>
<li><a href="#requirements"><span class="secno">4 </span>
Requirements
</a></li>
<li><a href="#task-scheduler-api"><span class="secno">5 </span>
Task Scheduler API
</a>
<ol class="toc">
<li><a href="#interface-serviceworkerregistration"><span class="secno">5.1 </span>
Interface <code title="">ServiceWorkerRegistration</code>
</a></li>
<li><a href="#interface-taskscheduler"><span class="secno">5.2 </span>
Interface <code title="">TaskScheduler</code>
</a></li>
<li><a href="#interface-scheduledtask"><span class="secno">5.3 </span>
<span>Interface <code title="">ScheduledTask</code></span>
</a></ol></li>
<li><a href="#events"><span class="secno">6 </span>
Events
</a>
<ol class="toc">
<li><a href="#event-handler"><span class="secno">6.1 </span>
Event Handler
</a></li>
<li><a href="#the-taskevent-interface"><span class="secno">6.2 </span>
The <code title="task-system-message">TaskEvent</code> Interface
</a></li>
<li><a href="#firing-task-event-to-service-worker"><span class="secno">6.3 </span>
Firing <code>task</code> event to service worker
</a></ol></li>
<li><a class="no-num" href="#references">
References
</a></li>
<li><a class="no-num" href="#acknowledgments">
Acknowledgments
</a></ol>
<!--end-toc-->
<h2 id="introduction"><span class="secno">1 </span>
Introduction
</h2>
<p>
<em>This section is non-normative.</em>
</p>
<div class="example">
<p>
Example use of the ScheduledTask API for adding, getting and removing and listening for the alarm clock use cases:
</p>
<p>
How to set an alarm 10 minutes from now?
</p>
<pre> // https://example.com/serviceworker.js
this.ontask = function(task) {
alert(task.data.message);
console.log("Task scheduled at: " + new Date(task.time));
// From here on we can write the data to IndexedDB, send it to any open windows,
// display a notification, etc.
}
// https://example.com/webapp.js
function onTaskAdded(task) {
console.log("Task successfully scheduled.");
}
function onError(error) {
alert("Sorry, couldn't set the alarm: " + error);
}
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.taskScheduler.add(Date.now() + (10 * 60000), {
message: "It's been 10 minutes, your soup is ready!"
}).then(onTaskAdded, onError);
});
</pre>
<p>
How to get all the scheduled tasks whose time is in the future?
</p>
<pre> navigator.serviceWorker.getRegistration().then(function(registration) {
registration.taskScheduler.getPendingTasks().then(function(tasks) {
alert("There are " + tasks.length + " tasks set.");
}, function(error) {
alert("An error occurred getting the scheduled tasks.");
});
}, function(error) {
alert("An error occurred getting the scheduled tasks.");
});
</pre>
<p>
How to remove a scheduled task?
</p>
<pre> navigator.serviceWorker.getRegistration().then(function(registration) {
var request = registration.taskScheduler.remove(id).then(function() {
alert("Task removed");
}, function(error) {
alert("Sorry, can't remove the task.");
});
}, function(error) {
alert("An error occurred getting the scheduled tasks.");
});
</pre>
</div>
<h2 id="conformance"><span class="secno">2 </span>
Conformance
</h2>
<p>
This specification defines conformance criteria for a single product: the <dfn id="ua">user agent</dfn> that implements
the interfaces that it contains.
</p>
<p>
Implementations that use ECMAScript to implement the APIs defined in this specification MUST implement them in a manner
consistent with the <a href="http://www.w3.org/TR/WebIDL/#ecmascript-binding">ECMAScript Bindings</a> defined in the
<cite>Web IDL specification</cite> <a href="#refsWEBIDL">[WEBIDL]</a>, as this specification uses that
specification and terminology.
</p>
<h2 id="terminology"><span class="secno">3 </span>
Terminology
</h2>
<p>
A <dfn id="json-serializable-object">JSON-serializable object</dfn> is an <a href="http://dev.w3.org/2006/webapi/WebIDL/#idl-object"><code>object</code></a> that when <a href="http://dev.w3.org/2006/webapi/WebIDL/#idl-stringifiers">serialized</a> or <a href="http://dev.w3.org/2006/webapi/WebIDL/#idl-serializers">stringified</a> conforms to the JSON Grammar as defined in
<a href="#refsECMASCRIPT">[ECMASCRIPT]</a>.
</p>
<p>
The <dfn id="eventhandler"><code><a href="http://dev.w3.org/html5/spec/webappapis.html#eventhandler">EventHandler</a></code></dfn>
interface represents a callback used for handling events as defined in <a href="#refsHTML5">[HTML5]</a>.
</p>
<p>
The <dfn id="promise"><code><a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects">Promise</a></code></dfn> interface provides
asynchronous access to the result of an operation that is ongoing, has yet to start, or has completed, as defined in
<a href="#refsECMASCRIPT6">[ECMASCRIPT6]</a>.
</p>
<p>
The concepts <dfn id="queue-a-task"><a href="http://dev.w3.org/html5/spec/webappapis.html#queue-a-task">queue a task</a></dfn>,
<dfn id="event-handler-idl-attribute"><a href="http://www.w3.org/html/wg/drafts/html/master/webappapis.html#event-handler-idl-attributes">event handler
IDL attribute</a></dfn> and <dfn id="fire-a-simple-event"><a href="http://dev.w3.org/html5/spec/webappapis.html#fire-a-simple-event">fire a
simple event</a></dfn> are defined in <a href="#refsHTML5">[HTML5]</a>.
</p>
<p>
The concepts <dfn id="event"><a href="https://dom.spec.whatwg.org/#concept-event">event</a></dfn> and
<dfn id="fire-an-event-named-e"><a href="https://dom.spec.whatwg.org/#concept-event-fire">fire an event named e</a></dfn>are defined in <a href="#refsDOM">[DOM]</a>.
</p>
<p>
The terms <dfn id="event-handler-0"><a href="http://dev.w3.org/html5/spec/webappapis.html#event-handlers">event handler</a></dfn> and
<dfn id="event-handler-event-types"><a href="http://dev.w3.org/html5/spec/webappapis.html#event-handler-event-type">event handler event
types</a></dfn> are defined in <a href="#refsHTML5">[HTML5]</a>.
</p>
<p>
<dfn id="service-worker"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#dfn-service-worker">Service worker</a></dfn>, <dfn id="service-worker-registration"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#dfn-service-worker-registration">service worker registration</a></dfn>, <dfn id="serviceworker"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-interface">ServiceWorker</a></dfn>, <dfn id="serviceworkerregistration"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-registration-interface">ServiceWorkerRegistration</a></dfn>,
<dfn id="serviceworkerglobalscope"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-global-scope-interface">ServiceWorkerGlobalScope</a></dfn>, <dfn id="extendableevent"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#extendable-event-interface">ExtendableEvent</a></dfn>, and <dfn id="handle-functional-event"><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#handle-functional-event-algorithm">Handle Functional Event</a></dfn> are defined in
<a href="#refsSERVICE-WORKERS">[SERVICE-WORKERS]</a>.
</p>
<h2 id="requirements"><span class="secno">4 </span>
Requirements
</h2>
<p>
Below is a summary of requirements associated with this API:
</p>
<ol>
<li>An application <em class="ct">must</em> only be able to access its own scheduled tasks.
</li>
<li>A scheduled task identifier <em class="ct">must</em> be unique within the application origin.
</li>
<li>A scheduled task <em class="ct">must</em> persist if the system is restarted.
</li>
<li>A scheduled task <em class="ct">must</em> actively wake the system if the scheduled time is reached while sleeping.
</li>
<li>A scheduled task that was missed (e.g. because the device was off or the clock jumped past it) should be fired as
soon as possible.
</li>
<li>A scheduled task and its associated data <em class="ct">must</em> be removed when the application's service worker registration is uninstalled.
</li>
</ol>
<h2 id="task-scheduler-api"><span class="secno">5 </span>
Task Scheduler API
</h2>
<p>
<em>This section is non-normative.</em>
</p>
<p>
The task scheduler supports the following features:
</p>
<ul>
<li>Web applications can schedule multiple tasks and get a returned ID for each of them.
</li>
<li>Each <code><a href="#scheduledtask">ScheduledTask</a></code> has a unique identifier that can be used to specify and remove the scheduled task.
</li>
<li>Web applications can pass a <a href="#json-serializable-object">JSON-serializable object</a> to describe more details about each task setting.
</li>
<li>When a scheduled time is reached, an <code title="task-system-message">task</code> event is
sent to the application.
</li>
<li>ScheduledTask API actually does more than <code>setTimeout()</code> because it can actively <em>wake</em> the
system from sleeping and scheduled task are not lost when closing the application or restarting the system.
</li>
</ul>
<h3 id="interface-serviceworkerregistration"><span class="secno">5.1 </span>
Interface <code title="">ServiceWorkerRegistration</code>
</h3>
<p>
The Service Worker specification defines a <code><a href="#serviceworkerregistration">ServiceWorkerRegistration</a></code> interface
<a href="#refsSERVICE-WORKERS">[SERVICE-WORKERS]</a>, which this specification extends.
</p>
<pre class="idl">partial interface <a href="#serviceworkerregistration">ServiceWorkerRegistration</a> {
readonly attribute <a href="#taskscheduler">TaskScheduler</a> <a href="#serviceworkerregistration-taskscheduler" title="ServiceWorkerRegistration-taskScheduler">taskScheduler</a>;
}</pre>
<p>
The <dfn id="serviceworkerregistration-taskscheduler" title="ServiceWorkerRegistration-taskScheduler"><code>taskScheduler</code></dfn> attribute provides the developer access to a
<code><a href="#taskscheduler">TaskScheduler</a></code>.
</p>
<h3 id="interface-taskscheduler"><span class="secno">5.2 </span>
Interface <code title="">TaskScheduler</code>
</h3>
<p>
The <code><a href="#taskscheduler">TaskScheduler</a></code> interface exposes methods to get, set or remove scheduled tasks. ScheduledTasks are
application specific, so there is no way to see the tasks scheduled by other applications nor to modify them.
Developers should set an <a href="#ontask-event-handler">ontask</a> event handler in the associated <a href="#service-worker">service worker</a> to listen for the <code title="task-system-message">task</code> event when scheduled tasks should be executed.
</p>
<pre class="idl">interface <dfn id="taskscheduler">TaskScheduler</dfn> {
<a href="#promise">Promise</a> <a href="#taskscheduler-getpendingtasks" title="TaskScheduler-getPendingTasks">getPendingTasks</a>();
<a href="#promise">Promise</a> <a href="#taskscheduler-add" title="TaskScheduler-add">add</a>(DOMTimeStamp <var title="">time</var>, optional any <var title="">data</var>);
<a href="#promise">Promise</a> <a href="#taskscheduler-remove" title="TaskScheduler-remove">remove</a>(DOMString <var title="">taskId</var>);
};</pre>
<p>
When invoked, the <dfn id="taskscheduler-getpendingtasks" title="TaskScheduler-getPendingTasks"><code>getPendingTasks()</code></dfn> method <em class="ct">must</em> run the following steps:
</p>
<ol>
<li>Make a request to the system to retrieve the tasks that were registered by the current application and whose
scheduled time is in the future.
</li>
<li>Let <em><a href="#promise">promise</a></em> be a new <code><a href="#promise">Promise</a></code> object and <em>resolver</em> its associated resolver.
</li>
<li>Return <em><a href="#promise">promise</a></em> and run the remaining steps asynchronously.
</li>
<li>If an error occurs, run these substeps and then terminate these steps:
<ol>
<li>Let <em>error</em> be a new <code>DOMException</code> exception whose <code>name</code> is the same as the error
returned.
</li>
<li>Run <em>resolver</em>'s internal <em>reject</em> algorithm with <em>error</em> as <code>value</code>.
</li>
</ol>
</li>
<li>When the operation completes successfully, run these substeps:
<ol>
<li>Let <em>tasks</em> be a new array containing the <code><a href="#scheduledtask">ScheduledTask</a></code> objects that were retrieved.
</li>
<li>Run <em>resolver</em>'s intenal <code>fulfill</code> algorithm with <em>tasks</em> as <code>value</code>.
</li>
</ol>
</li>
</ol>
<p>
When invoked, the <dfn id="taskscheduler-add" title="TaskScheduler-add"><code>add(<var>time</var>[, <var>data</var>])</code></dfn> method
<em class="ct">must</em> run the following steps:
</p>
<ol>
<li>Make a request to the system to schedule a new task for the current application that will trigger at the given
<code>time</code> (number of milliseconds since the epoch). If the <code>time</code> argument is in the past, the task
will be executed as soon as possible, asynchronously. The system <em class="ct">must</em> associate the
<span>JSON-serializable <code>data</code> with the task if provided.</span>
</li>
<li>
<span>Let <em><a href="#promise">promise</a></em> be a new <code><a href="#promise">Promise</a></code> object and <em>resolver</em> its associated resolver.</span>
</li>
<li>
<span>Return <em><a href="#promise">promise</a></em> and run the remaining steps asynchronously.</span>
</li>
<li>
<span>If an error occurs, run these substeps and then terminate these steps:</span>
<ol>
<li>
<span>Let <em>error</em> be a new <code>DOMException</code> exception whose <code>name</code> is
<code>"QuotaExceededError"</code> if the <code>data</code> argument exceeds an implementation-dependent size limit, or
whose <code>name</code> is the same as the error returned otherwise.</span>
</li>
<li>
<span>Run <em>resolver</em>'s internal <em>reject</em> algorithm with <em>error</em> as <code>value</code>.</span>
</li>
</ol>
</li>
<li>
<span>When the operation completes successfully, run these substeps:</span>
<ol>
<li>
<span>Let <em>task</em> be a new <code><a href="#scheduledtask">ScheduledTask</a></code> object.</span>
</li>
<li>
<span>Set <em>task</em>'s <code>id</code> attribute to the unique identifier returned by the system for the newly
registered task.</span>
</li>
<li>
<span>Set <em>task</em>'s <code>time</code> attribute to the <code>time</code> argument.</span>
</li>
<li>
<span>Set <em>task</em>'s <code>data</code> attribute to the <code>data</code> argument, if provided.</span>
</li>
<li>
<span>Run <em>resolver</em>'s internal <code>fulfill</code> algorithm with <em>task</em> as <code>value</code>.</span>
</li>
</ol>
</li>
</ol>
<p>
<span>When invoked, the <dfn id="taskscheduler-remove" title="TaskScheduler-remove"><code>remove(<var>taskId</var>)</code></dfn> method
<em class="ct">must</em> run the following steps:</span>
</p>
<ol>
<li>
<span>Make a request to the system to unregister the task with the given unique <code>taskId</code> identifier.</span>
</li>
<li>
<span>Let <em><a href="#promise">promise</a></em> be a new <code><a href="#promise">Promise</a></code> object and <em>resolver</em> its associated resolver.</span>
</li>
<li>
<span>Return <em><a href="#promise">promise</a></em> and run the remaining steps asynchronously.</span>
</li>
<li>
<span>If an error occurs, run these substeps and then terminate these steps:</span>
<ol>
<li>
<span>Let <em>error</em> be a new <code>DOMException</code> exception whose <code>name</code> is the same as the error
returned.</span>
</li>
<li>
<span>Run <em>resolver</em>'s internal <em>reject</em> algorithm with <em>error</em> as <code>value</code>.</span>
</li>
</ol>
</li>
<li>
<span>When the operation completes successfully, run these substeps:</span>
<ol>
<li>
<span>Let <em>removed</em> be a boolean value.</span>
</li>
<li>
<span>Set <em>removed</em> to <code>true</code> if the task was removed, and to <code>false</code> if there was no task
with the given identifier.</span>
</li>
<li>
<span>Run <em>resolver</em>'s intenal <code>fulfill</code> algorithm with <em>removed</em> as
<code>value</code>.</span>
</li>
</ol>
</li>
</ol>
<h3 id="interface-scheduledtask"><span class="secno">5.3 </span>
<span>Interface <code title="">ScheduledTask</code></span>
</h3>
<p>
<span>The <code><a href="#scheduledtask">ScheduledTask</a></code> interface captures the properties of a scheduled task.</span>
</p>
<pre class="idl"><span>interface <dfn id="scheduledtask">ScheduledTask</dfn> {
readonly attribute DOMString <a href="#scheduledtask-id" title="ScheduledTask-id">id</a>;
readonly attribute DOMTimeStamp <a href="#scheduledtask-time" title="ScheduledTask-time">time</a>;
readonly attribute any <a href="#scheduledtask-data" title="ScheduledTask-data">data</a>;
};</span></pre>
<p>
The <dfn id="scheduledtask-id" title="ScheduledTask-id"><code>id</code></dfn> attribute returns an identifier for the given
<code><a href="#scheduledtask">ScheduledTask</a></code> object that is unique within the origin. An implementation <em class="ct">must</em> maintain
this identifier when a <code><a href="#scheduledtask">ScheduledTask</a></code> is added.
</p>
<p>
The <dfn id="scheduledtask-time" title="ScheduledTask-time"><code>time</code></dfn> attribute is the time at which this task is scheduled to
fire, in milliseconds past the epoch (e.g. Date.now() + n). Due to performance, the task may be delayed past this time.
</p>
<p>
The <dfn id="scheduledtask-data" title="ScheduledTask-data"><code>data</code></dfn> attribute optionally represents the <span>JSON-serializable
data</span> associated with the task.
</p>
<h2 id="events"><span class="secno">6 </span>
Events
</h2>
<p>
The Service Worker specification defines a <code><a href="#serviceworkerglobalscope">ServiceWorkerGlobalScope</a></code> interface
<a href="#refsSERVICE-WORKERS">[SERVICE-WORKERS]</a>, which this specification extends.
</p>
<pre class="idl">partial interface <a class="idlInterfaceID" href="#serviceworkerglobalscope">ServiceWorkerGlobalScope</a> {
attribute EventHandler <span title="task-system-message"><a href="#ontask-event-handler">ontask</a></span>;
};</pre>
<h3 id="event-handler"><span class="secno">6.1 </span>
Event Handler
</h3>
The following is the <a href="https://html.spec.whatwg.org/multipage/webappapis.html#event-handlers">event handler</a> (and its corresponding <a href="https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-event-type">event handler event type</a>) that must be supported as attribute by the <code><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-global-scope-interface">ServiceWorkerGlobalScope</a></code> object.
<table>
<thead>
<tr>
<th><a href="http://www.whatwg.org/specs/web-apps/current-work/#event-handlers">event handler</a></th>
<th><a href="http://www.whatwg.org/specs/web-apps/current-work/#event-handler-event-type">event handler event type</a></th>
</tr>
</thead>
<tbody>
<tr>
<td><dfn id="ontask-event-handler"><code>ontask</code></dfn></td>
<td><code>task</code></td>
</tr>
</tbody>
</table>
<h3 id="the-taskevent-interface"><span class="secno">6.2 </span>
The <code title="task-system-message">TaskEvent</code> Interface
</h3>
<p> The <code><a href="#taskevent">TaskEvent</a></code> interface represents a scheduled task. </p>
<pre class="idl">interface <dfn id="taskevent">TaskEvent</dfn> : <a href="#extendableevent">ExtendableEvent</a> {
readonly attribute <a href="#scheduledtask" title="ScheduledTask">ScheduledTask</a> <a href="#scheduledtask" title="ScheduledTask">task</a>;
};</pre>
<p>
<h3 id="firing-task-event-to-service-worker"><span class="secno">6.3 </span>
Firing <code>task</code> event to service worker
</h3>
A <code>task</code> <a href="#event">event</a> is fired when a scheduled task should be
executed. The scheduled task is originated from the system and will wake up a <a href="#service-worker">service worker</a> if it is not currently running.
<p></p>
<p>When the scheduled task <em>task</em> went off by the system, the user agent must (unless otherwise specified) run these steps:</p>
<ol>
<li>Let <em>callback</em> be an algorithm that when invoked with a <em>global</em>, <a href="#fire-a-service-worker-task-event">fires a service worker task event</a> named <code>task</code> given <em>task</em> on <em>global</em>.</li>
<li>Then run <a href="#handle-functional-event">Handle Functional Event</a> with <em>task</em>'s <a href="#service-worker-registration">service worker registration</a> and <em>callback</em>.</li>
</ol>
<p>To <dfn id="fire-a-service-worker-task-event">fire a service worker task event named e</dfn> given <em>task</em>, <a href="#fire-an-event-named-e">fire an event named e</a> with an <a href="#event">event</a> using the <code><a href="#taskevent">TaskEvent</a></code> interface whose <code>task</code> attribute is initialized to a new <code>ScheduledEvent</code> object representing <em>task</em>.
</p>
<h2 class="no-num" id="references">
References
</h2>
<div id="anolis-references"><dl><dt id="refsB2G-ALARM">[B2G-ALARM]
<dd><cite><a href="https://wiki.mozilla.org/WebAPI/AlarmAPI">B2G Alarm API Specification</a></cite>, Mounir Lamouri, Kan-Ru Chen and Jonas Sicking. Mozilla.
<dt id="refsDOM">[DOM]
<dd><cite><a href="http://dom.spec.whatwg.org/">DOM</a></cite>, Anne van Kesteren, Aryeh Gregor and Ms2ger. WHATWG.
<dt id="refsECMASCRIPT">[ECMASCRIPT]
<dd><cite><a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript Language Specification</a></cite>. ECMA.
<dt id="refsECMASCRIPT6">[ECMASCRIPT6]
<dd><cite><a href="https://people.mozilla.org/~jorendorff/es6-draft.html">ECMAScript Language Specification (6th edition, draft)</a></cite>. ECMA.
<dt id="refsHTML5">[HTML5]
<dd><cite><a href="http://dev.w3.org/html5/spec/">HTML5</a></cite>, Ian Hickson. W3C.
<dt id="refsSERVICE-WORKERS">[SERVICE-WORKERS]
<dd><cite><a href="https://slightlyoff.github.io/ServiceWorker/spec/service_worker/">Service Workers</a></cite>, Alex Russell and Jungkee Song. W3C.
<dt id="refsWEBIDL">[WEBIDL]
<dd><cite><a href="http://dev.w3.org/2006/webapi/WebIDL/">Web IDL</a></cite>, Cameron McCormack. W3C.
</dl></div>
<h2 class="no-num" id="acknowledgments">
Acknowledgments
</h2>
<p>
We would like to thank Kan-Ru Chen, Mounir Lamouri, Gene Lian and Jonas Sicking for their work on the API design, as
well as the WebAPI/B2G teams at Mozilla <a href="#refsB2G-ALARM">[B2G-ALARM]</a>.
</p>