Annotation of html5/workers/Overview.html, revision 1.8
1.1 ihickson 1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
2: <!-- when publishing, change bits marked ZZZ -->
3:
4: <html lang=en-US-x-Hixie>
5: <head>
6: <title>Web Workers</title>
7: <link href="http://www.w3.org/StyleSheets/TR/%57%33%43-ED" rel=stylesheet
8: type="text/css">
9: <!-- ZZZ ED vs WD -->
10:
11: <body>
12: <div class=head>
13: <p><a href="http://www.w3.org/"><img alt=W3C height=48
14: src="http://www.w3.org/Icons/w3c_home" width=72></a></p>
15:
16: <h1 id=web-workers>Web Workers</h1>
17:
18: <h2 class="no-num no-toc" id=an-accompaniment>An accompaniment
19: specification for HTML5</h2>
20:
21: <h2 class="no-num no-toc" id=editors><!-- "W3C Working Draft" --> Editor's
1.8 ! ihickson 22: Draft <!--ZZZ-->17 July 2008</h2>
1.1 ihickson 23:
24: <dl><!-- ZZZ: update the month/day
25: <dt>This Version:</dt>
26: <dd><a href="http://www.w3.org/TR/2008/WD-workers-20080101/">http://www.w3.org/TR/2008/WD-workers-20080101/</a></dd>
1.2 ihickson 27: <dt>Latest Published Version:</dt>
28: <dd><a href="http://www.w3.org/TR/workers/">http://www.w3.org/TR/workers/</a></dd>
1.1 ihickson 29: :ZZZ -->
30:
31: <dt>Latest Editor's Draft:
32:
33: <dd><a
34: href="http://dev.w3.org/html5/workers/">http://dev.w3.org/html5/workers/</a></dd>
1.2 ihickson 35: <!-- ZZZ: add the new version after it has shipped
36: <dt>Previous Versions:</dt>
37: <dd><a href="http://www.w3.org/TR/2008/WD-workers-20080101/">http://www.w3.org/TR/2008/WD-workers-20080101/</a>
38: :ZZZ -->
1.1 ihickson 39:
40: <dt>Editors:
41:
42: <dd><a href="mailto:ian@hixie.ch">Ian Hickson</a>, Google, Inc.
43: </dl>
44:
45: <p class=copyright><a
46: href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
47: © 2008 <a href="http://www.w3.org/"><abbr title="World Wide Web
48: Consortium">W3C</abbr></a><sup>®</sup> (<a
49: href="http://www.csail.mit.edu/"><abbr title="Massachusetts Institute of
50: Technology">MIT</abbr></a>, <a href="http://www.ercim.org/"><abbr
51: title="European Research Consortium for Informatics and
52: Mathematics">ERCIM</abbr></a>, <a
53: href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C <a
54: href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
55: <a
56: href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
57: and <a
58: href="http://www.w3.org/Consortium/Legal/copyright-documents">document
59: use</a> rules apply.</p>
60: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
61:
62: <p class="alt copyright">The <a
63: href="http://www.whatwg.org/specs/web-workers/current-work/">WHATWG
64: version</a> of this specification is available under a more permissive
65: license.</p>
66: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
67: </div>
68:
69: <hr>
70:
71: <h2 class="no-num no-toc" id=abstract>Abstract</h2>
72:
73: <p>This specification defines an API that allows Web application authors to
74: spawn background workers running scripts in parallel to their main page.
75: This allows for thread-like operation with message-passing as the
76: coordination mechanism.
77:
78: <h2 class="no-num no-toc" id=status>Status of this document</h2>
79: <!-- intro boilerplate (required) -->
80:
81: <p><em>This section describes the status of this document at the time of
82: its publication. Other documents may supersede this document. A list of
83: current W3C publications and the most recently formally published revision
84: of this technical report can be found in the <a
85: href="http://www.w3.org/TR/">W3C technical reports index</a> at
86: http://www.w3.org/TR/.</em></p>
87: <!-- where to send feedback (required) -->
88:
89: <p>If you wish to make comments regarding this document, please send them
90: to <a
91: href="mailto:public-html-comments@w3.org">public-html-comments@w3.org</a>
92: (<a
93: href="mailto:public-html-comments-request@w3.org?subject=subscribe">subscribe</a>,
94: <a
95: href="http://lists.w3.org/Archives/Public/public-html-comments/">archives</a>)
96: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING SENTENCE TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
97: or <a href="mailto:whatwg@whatwg.org">whatwg@whatwg.org</a> (<a
98: href="http://lists.whatwg.org/listinfo.cgi/whatwg-whatwg.org">subscribe</a>,
99: <a
100: href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/">archives</a>).
101: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING SENTENCE TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
102: All feedback is welcome.</p>
103: <!-- stability (required) -->
104:
105: <p>Implementors should be aware that this specification is not stable.
106: <strong>Implementors who are not taking part in the discussions are likely
107: to find the specification changing out from under them in incompatible
108: ways.</strong> Vendors interested in implementing this specification
109: before it eventually reaches the Candidate Recommendation stage should
110: join the aforementioned mailing lists and take part in the discussions.</p>
111: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
112: <!-- version history or list of changes (required) -->
113:
114: <p>The latest stable version of the editor's draft of this specification is
115: always available on <a
116: href="http://dev.w3.org/html5/workers/Overview.html">the W3C CVS
117: server</a> and in the <a href="http://svn.whatwg.org/webworkers/">WHATWG
118: Subversion repository</a>. The latest editor's working copy (which may
119: contain unfinished text in the process of being prepared) is available <a
120: href="http://www.whatwg.org/specs/web-workers/current-work/">on the WHATWG
121: site</a>. Detailed change history can be obtained from the following
122: locations:</p>
123: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
124: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING LIST TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
125:
126: <ul>
127: <li>Twitter messages (non-editorial changes only): <a
128: href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a>
129:
130: <li>Interactive Web interface: <a
131: href="http://html5.org/tools/web-workers-tracker">http://html5.org/tools/web-workers-tracker</a>
132:
133: <li>Commit-Watchers mailing list: <a
134: href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a>
135:
136: <li>Subversion interface: <a
137: href="http://svn.whatwg.org/webworkers/">http://svn.whatwg.org/webworkers/</a>
138:
139: <li>CVS log: <a
140: href="http://dev.w3.org/cvsweb/html5/workers/Overview.html">http://dev.w3.org/cvsweb/html5/workers/Overview.html</a>
141: </ul>
142: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING LIST TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
143: <!-- status of document, group responsible (required) -->
144:
145: <p>The W3C <a href="http://www.w3.org/html/wg/">HTML Working Group</a> is
146: the W3C working group responsible for this specification's progress along
1.8 ! ihickson 147: the W3C Recommendation track. <!--ZZZ:--> This specification is the 17
1.3 ihickson 148: July 2008 <!--ZZZ "Working Draft"-->Editor's Draft. <!--:ZZZ--></p>
1.1 ihickson 149: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
150: <!-- relationship to other work (required) -->
151:
152: <p>This specification is also being produced by the <a
153: href="http://www.whatwg.org/">WHATWG</a>. The two specifications are
154: identical from the table of contents onwards.</p>
155: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
156: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
157: <!-- context and rationale (required) -->
158:
159: <p>This specification is intended to specify a part of the Web platform
160: closely related to HTML5. It is defined in a separate document primarily
161: to ease the cognitive load on reviewers.</p>
162: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
163: <!-- required patent boilerplate -->
164:
165: <p>This document was produced by a group operating under the <a
166: href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February
167: 2004 W3C Patent Policy</a>. W3C maintains a <a
168: href="http://www.w3.org/2004/01/pp-impl/40318/status"
169: rel=disclosure>public list of any patent disclosures</a> made in
170: connection with the deliverables of the group; that page also includes
171: instructions for disclosing a patent. An individual who has actual
172: knowledge of a patent which the individual believes contains <a
173: href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
174: Claim(s)</a> must disclose the information in accordance with <a
175: href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
176: 6 of the W3C Patent Policy</a>.
177:
178: <h2 class="no-num no-toc" id=contents>Table of contents</h2>
179: <!--begin-toc-->
180:
181: <ul class=toc>
182: <li><a href="#introduction"><span class=secno>1. </span>Introduction</a>
183: <ul class=toc>
1.4 ihickson 184: <li><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a>
1.1 ihickson 185:
1.3 ihickson 186: <li><a href="#requirements"><span class=secno>1.2
187: </span>Requirements</a>
1.1 ihickson 188:
1.4 ihickson 189: <li><a href="#conformance"><span class=secno>1.3 </span>Conformance
1.1 ihickson 190: requirements</a>
191: <ul class=toc>
1.4 ihickson 192: <li><a href="#dependencies"><span class=secno>1.3.1
1.1 ihickson 193: </span>Dependencies</a>
194: </ul>
195:
1.4 ihickson 196: <li><a href="#terminology"><span class=secno>1.4 </span>Terminology</a>
197: </ul>
198:
199: <li><a href="#inside"><span class=secno>2. </span>Inside workers</a>
200: <ul class=toc>
201: <li><a href="#the-windowworker"><span class=secno>2.1 </span>The
202: <code>WindowWorker</code> interface</a>
203:
204: <li><a href="#the-queue"><span class=secno>2.2 </span>The queue of
205: events</a>
206:
207: <li><a href="#processing"><span class=secno>2.3 </span>Processing
208: model</a>
1.1 ihickson 209: </ul>
210:
1.4 ihickson 211: <li><a href="#creating"><span class=secno>3. </span>Creating workers</a>
1.5 ihickson 212: <ul class=toc>
213: <li><a href="#the-windowworkercreators"><span class=secno>3.1 </span>The
214: <code>WindowWorkerCreators</code> interface</a>
215: </ul>
1.4 ihickson 216:
1.1 ihickson 217: <li class=no-num><a href="#references">References</a>
218:
219: <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
220: </ul>
221: <!--end-toc-->
222:
223: <hr>
224:
225: <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
226:
1.4 ihickson 227: <h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3>
1.1 ihickson 228:
229: <p><em>This section is non-normative.</em>
230:
1.4 ihickson 231: <p class=big-issue>This section is missing.
232:
1.3 ihickson 233: <h3 id=requirements><span class=secno>1.2 </span>Requirements</h3>
1.1 ihickson 234:
235: <p><em>This section is non-normative.</em>
236:
1.3 ihickson 237: <p>This specification aims to address the following use cases and
238: requirements:
239:
240: <ul>
241: <li>Background workers: A Web application needs to keep its data
242: synchronised with the server, both sending updates to the server and
243: receiving updates from the server, including handling buffering of
244: updates for when the application goes offline. The code to do this would
245: ideally be independent of the UI code.
246:
247: <li>URLs: Workers should be spawned from URLs, not from strings, since
248: script rarely has access to its own source.
249:
250: <li>Message queuing: Messages sent to a worker before the worker has
251: initialised should not be lost.
252:
253: <li>Workers should have access to timers.
254:
255: <li>Workers should have access to the network.
256:
257: <li>Workers should be able to use libraries.
258:
259: <li>Implementations should not have to expose <code>Node</code> or
260: <code>Document</code> objects to workers.
261:
262: <li>Workers should not share anything with the outside world. The objects
263: representing the worker in the worker itself and in the context that
264: created the worker should be different, for instance.
265:
266: <li>Shared workers: Multiple instances of the same Web application would
267: want to keep just one connection back to the server.
268:
269: <li>Capabilities granting: It should be possible for code running in one
270: iframe to negotiate a connection to another iframe, with that connection
271: granting certain rights (e.g. adding to an address book but not reading
272: from it).
273:
274: <li>Delegation: It should be possible for one worker to spawn another
275: worker and efficiently delagate a request to that worker, without the
276: caller being aware of the delagate and without the original worker having
277: to proxy all the messages.
278:
279: <li>Workers whose parents are not longer useful should be killed. Workers
280: should be able to detect this is about to happen and exit gracefully.
281: </ul>
282:
1.4 ihickson 283: <h3 id=conformance><span class=secno>1.3 </span>Conformance requirements</h3>
1.1 ihickson 284:
285: <p>All diagrams, examples, and notes in this specification are
286: non-normative, as are all sections explicitly marked non-normative.
287: Everything else in this specification is normative.
288:
289: <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
290: NOT",-->
291: "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
292: normative parts of this document are to be interpreted as described in
293: RFC2119. For readability, these words do not appear in all uppercase
294: letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
295: <!-- XXX but they should be
296: marked up -->
297:
298: <p>Requirements phrased in the imperative as part of algorithms (such as
299: "strip any leading space characters" or "return false and abort these
300: steps") are to be interpreted with the meaning of the key word ("must",
301: "should", "may", etc) used in introducing the algorithm.
302:
303: <p>Some conformance requirements are phrased as requirements on attributes,
304: methods or objects. Such requirements are to be interpreted as
305: requirements on user agents.
306:
307: <p>Conformance requirements phrased as algorithms or specific steps may be
308: implemented in any manner, so long as the end result is equivalent. (In
309: particular, the algorithms defined in this specification are intended to
310: be easy to follow, and not intended to be performant.)
311:
312: <p>The only conformance class defined by this specification is user agents.
313:
314: <p>User agents may impose implementation-specific limits on otherwise
315: unconstrained inputs, e.g. to prevent denial of service attacks, to guard
316: against running out of memory, or to work around platform-specific
317: limitations.
318:
1.4 ihickson 319: <h4 id=dependencies><span class=secno>1.3.1 </span>Dependencies</h4>
1.1 ihickson 320:
321: <p>This specification relies on several other underlying specifications.
322:
323: <dl>
324: <dt>HTML5
325:
326: <dd>
327: <p>Many fundamental concepts from HTML5 are used by this specification.
328: <a href="#refsHTML5">[HTML5]</a></p>
329:
330: <dt>ECMAScript
331:
332: <dd>
333: <p>This specification is intended to be used with JavaScript as the
334: scripting language. <a href="#refsJS">[JS]</a></p>
335:
336: <dt>WebIDL
337:
338: <dd>
339: <p>The IDL blocks in this specification use the semantics of the WebIDL
340: specification. <a href="#refsWebIDL">[WebIDL]</a></p>
341: </dl>
342:
1.4 ihickson 343: <h3 id=terminology><span class=secno>1.4 </span>Terminology</h3>
1.1 ihickson 344:
345: <p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
346: <em>visible</em> might sometimes be used when referring to the way a
347: document is rendered to the user. These terms are not meant to imply a
348: visual medium; they must be considered to apply to other media in
349: equivalent ways.
350:
1.4 ihickson 351: <p>The construction "a <code title="">Foo</code> object", where <code
352: title="">Foo</code> is actually an interface, is sometimes used instead of
353: the more accurate "an object implementing the interface <code
354: title="">Foo</code>".
1.1 ihickson 355:
356: <p>The term DOM is used to refer to the API set made available to scripts
357: in Web applications, and does not necessarily imply the existence of an
358: actual <code>Document</code> object or of any other <code>Node</code>
359: objects as defined in the DOM Core specifications. <a
360: href="#refsDOM3CORE">[DOM3CORE]</a>
361:
362: <p>A DOM attribute is said to be <em>getting</em> when its value is being
363: retrieved (e.g. by author script), and is said to be <em>setting</em> when
364: a new value is assigned to it.
365:
366: <p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means
367: that any attributes returning that object must always return the same
368: object (not a new object each time), and the attributes and methods on
369: that object must operate on the actual underlying data, not a snapshot of
370: the data.
371:
1.4 ihickson 372: <h2 id=inside><span class=secno>2. </span>Inside workers</h2>
373:
374: <h3 id=the-windowworker><span class=secno>2.1 </span>The <code><a
375: href="#windowworker">WindowWorker</a></code> interface</h3>
376:
377: <pre
378: class=idl>[NoInterfaceObject] interface <dfn id=windowworker>WindowWorker</dfn> {
1.6 ihickson 379: readonly attribute boolean <a href="#name" title=dom-windowworker-name>name</a>;
1.4 ihickson 380: readonly attribute boolean <a href="#closing" title=dom-windowworker-closing>closing</a>;
1.8 ! ihickson 381: void <a href="#close" title=dom-windowworker-close>close</a>();
1.4 ihickson 382: };</pre>
383:
384: <p>Objects that implement the <code><a
385: href="#windowworker">WindowWorker</a></code> interface must also implement
386: the <code>Window</code> interface (and thus also the
1.5 ihickson 387: <code>WindowTimers</code> and <code><a
388: href="#windowworkercreators">WindowWorkerCreators</a></code> interfaces)
389: and the <code>EventTarget</code> interface.
1.4 ihickson 390:
1.6 ihickson 391: <p>The <dfn id=name title=dom-windowworker-name><code>name</code></dfn>
392: attribute must return the value it was assigned when the <code><a
393: href="#windowworker">WindowWorker</a></code> object was created by the "<a
394: href="#run-a">run a worker</a>" algorithm.
395:
1.4 ihickson 396: <p>The <dfn id=closing
397: title=dom-windowworker-closing><code>closing</code></dfn> attribute must
398: return false until the "<a href="#kill-a">kill a worker</a>" processing
399: model defined below sets it to false.
400:
401: <h3 id=the-queue><span class=secno>2.2 </span>The queue of events</h3>
402:
403: <p>Each <code><a href="#windowworker">WindowWorker</a></code> object is
404: asssociated with a <dfn id=queue>queue of events</dfn>, which is initially
405: empty.
406:
407: <p>An event in the queue can be a DOM event or a timeout callback.
408:
1.8 ! ihickson 409: <p>All asynchronous callbacks and events that would be called or dispatched
! 410: in the worker must be added to the worker's queue, with the "<a
! 411: href="#run-a">run a worker</a>" processing model below taking care of
! 412: actually calling the callbacks or dispatching the events.
! 413:
! 414: <p>Once the <code><a href="#windowworker">WindowWorker</a></code>'s <code
! 415: title=dom-windowworker-closing><a href="#closing">closing</a></code>
! 416: attribute is set to true, the queue must discard anything else that would
! 417: be added to it. Effectively, once the <code
! 418: title=dom-windowworker-closing><a href="#closing">closing</a></code>
! 419: attribute is true, timers stop firing, notifications for all pending
! 420: asynchronous operations are dropped, etc.
! 421:
1.4 ihickson 422: <h3 id=processing><span class=secno>2.3 </span>Processing model</h3>
423:
1.6 ihickson 424: <p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var
1.7 ihickson 425: title="">name</var> for a script with <span>URL</span> <var
426: title="">url</var>, a browsing context <var title="">owner browsing
427: context</var> and a <code>Document</code> <var title="">owner
428: document</var>, it must run the following steps in a completely separate
429: and parallel execution environment:
1.4 ihickson 430:
431: <ol>
432: <li>
1.7 ihickson 433: <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
434: <var title="">url</var>.</p>
435:
436: <p>If the attempt fails, then abort these steps and invoke the <a
437: href="#worker0" title="worker creation failed">error handling steps</a>
438: defined by the algorithm that called this one.</p>
439:
440: <p>If the attempt succeeds, then let <var title="">script</var> be the
441: resource that was obtained.</p>
442:
443: <p class=note>As with <code>script</code> elements, the MIME type of the
444: script is ignored. Unlike with <code>script</code> elements, there is no
445: way to override the type. It's always assumed to be JavaScript.</p>
446: <!-- XXX people will complain about
447: this. I guess we might want to examine the MIME type... -->
448:
449:
450: <li>
1.4 ihickson 451: <p>Create a new <code><a href="#windowworker">WindowWorker</a></code>
452: object, <var title="">window</var>.</p>
453:
454: <li>
1.6 ihickson 455: <p>Set the <code title=dom-windowworker-name><a
456: href="#name">name</a></code> attribute of <var title="">window</var> to
457: the value of <var title="">name</var>.</p>
458:
459: <li>
1.4 ihickson 460: <p>Let <var title="">script</var>'s <span>script execution context</span>
461: (and thus also <span>global object</span>) be <var
462: title="">window</var>.</p>
463:
464: <li>
465: <p>Let <var title="">script</var>'s <span>script browsing context</span>
466: be <var title="">owner browsing context</var>.</p>
467:
468: <li>
469: <p>Let <var title="">script</var>'s <span>script document context</span>
470: be <var title="">owner document</var>.</p>
471:
472: <li>
1.7 ihickson 473: <p>Invoke the <a href="#worker" title="worker creation succeeded">success
474: steps</a> defined by the algorithm that called this one. (This will fire
475: add an event to the <a href="#queue">queue of events</a>.)</p>
476:
477: <li>
478: <p>Run <var title="">script</var> until it either returns, fails to catch
479: an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
480: a worker</a>" algorithm below.</p>
481:
482: <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
483: worker</a>" algorithm, then that same algorithm will cause there to only
484: be a single event in the <a href="#queue">queue of events</a> at the
485: next step, namely the <code title=message-unload>unload</code> event.</p>
1.4 ihickson 486:
487: <li>
488: <p><i>Event loop</i>: Wait until there is an event in the <a
489: href="#queue">queue of events</a> associated with <var
490: title="">window</var>.</p>
491:
492: <li>
493: <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1.8 ! ihickson 494: events</a>. The handling of this event or the execution of this callback
! 495: might get prematurely aborted by the "<a href="#kill-a">kill a
! 496: worker</a>" algorithm below.</p>
1.4 ihickson 497:
498: <li>
499: <p>If there are any more events in the <a href="#queue">queue of
500: events</a> or if the <var title="">window</var> object's <code
501: title=dom-windowworker-closing><a href="#closing">closing</a></code>
502: attribute is set to false, then jump back to the step above labeled
503: <i>event loop</i>.</p>
504:
505: <li>
506: <p class=big-issue>timers, intervals, XMLHttpRequests, database
507: transactions, etc, must be killed; ports must be deactivated and
1.8 ! ihickson 508: unentangled (do not send unload)</p>
1.4 ihickson 509: </ol>
510:
511: <hr>
512:
513: <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run
514: the following steps in parallel with the worker's main loop (the "<a
515: href="#run-a">run a worker</a>" processing model defined above):
516:
517: <ol>
518: <li>
519: <p>Create an <code>Event</code> object with the event name <code
520: title=event-unload>unload</code>, which does not bubble and is not
521: cancelable, and add it to the worker's <code><a
522: href="#windowworker">WindowWorker</a></code> object's <a
1.8 ! ihickson 523: href="#queue">queue of events</a>, targetted at the <code><a
! 524: href="#windowworker">WindowWorker</a></code> object itself.
1.4 ihickson 525:
526: <li>
1.8 ! ihickson 527: <p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code>
! 528: object's <code title=dom-windowworker-closing><a
! 529: href="#closing">closing</a></code> attribute to true.
! 530:
! 531: <li>
! 532: <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
! 533: a worker</a>" processing model defined above immediately starts running
! 534: event listeners registered for <code title=event-unload>unload</code>
! 535: event, this time should not be zero — the idea is that the <code
! 536: title=event-unload>unload</code> event can be used to clean up when
! 537: shutting down unexpectedly.
1.4 ihickson 538:
539: <li>
540: <p>If there are any events in the <a href="#queue">queue of events</a>
541: other than the <code title=event-unload>unload</code> event that this
542: algorithm just added, discard them without dispatching them.
543:
544: <li>
545: <p>If the <code title=event-unload>unload</code> event that this
546: algorithm just added hasn't yet been dispatched, then abort the script
547: currently running in the worker.
548:
549: <li>
550: <p>Wait a user-agent-defined amount of time.
551:
552: <li>
553: <p>Abort the script currently running in the worker (if any script is
554: running, then it will be a handler for the <code
555: title=event-unload>unload</code> event).
556: </ol>
557:
1.8 ! ihickson 558: <hr>
! 559:
! 560: <p>When a script invokes the <dfn id=close
! 561: title=dom-windowworker-close><code>close()</code></dfn> method on a
! 562: <code><a href="#windowworker">WindowWorker</a></code> object, the user
! 563: agent must run the following steps:
! 564:
! 565: <ol>
! 566: <li>
! 567: <p>Create an <code>Event</code> object with the event name <code
! 568: title=event-unload>unload</code>, which does not bubble and is not
! 569: cancelable, and add it to the <code><a
! 570: href="#windowworker">WindowWorker</a></code> object's <a
! 571: href="#queue">queue of events</a>, targetted at the <code><a
! 572: href="#windowworker">WindowWorker</a></code> object itself.
! 573:
! 574: <li>
! 575: <p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code>
! 576: object's <code title=dom-windowworker-closing><a
! 577: href="#closing">closing</a></code> attribute to true.
! 578:
! 579: <li>
! 580: <p>For each <code>MessagePort</code> object that is entangled with
! 581: another port and that has one (but only one) port whose <code
! 582: title=dom-messageport-ownerWindow>ownerWindow</code> is the <code><a
! 583: href="#windowworker">WindowWorker</a></code> object on which the method
! 584: was invoked, run the following substeps:</p>
! 585:
! 586: <ol>
! 587: <li>
! 588: <p>Unentangle the two ports.
! 589:
! 590: <li>
! 591: <p>Set both ports' <code title=dom-messageport-active>active</code>
! 592: attribute to false.
! 593:
! 594: <li>
! 595: <p>At the next available opportunity, after any scripts have finished
! 596: executing<!-- XXX queue -->, <span>fire a simple event</span> called
! 597: <code title=event-unload>unload</code> at the other port (the one
! 598: whose <code title=dom-messageport-ownerWindow>ownerWindow</code> is
! 599: not the <code><a href="#windowworker">WindowWorker</a></code> object
! 600: on which the <code title=dom-windowworker-close><a
! 601: href="#close">close()</a></code> method was called).
! 602: </ol>
! 603: </ol>
! 604:
1.4 ihickson 605: <h2 id=creating><span class=secno>3. </span>Creating workers</h2>
606:
1.5 ihickson 607: <h3 id=the-windowworkercreators><span class=secno>3.1 </span>The <code><a
608: href="#windowworkercreators">WindowWorkerCreators</a></code> interface</h3>
609:
610: <pre
611: class=idl>[NoInterfaceObject] interface <dfn id=windowworkercreators>WindowWorkerCreators</dfn> {
612: <span>MessagePort</span> <span title=dom-WindowWorkerCreators-createWorker>createWorker</span>(in DOMString scriptURI);
613: <span>MessagePort</span> <span title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker</span>(in DOMString name, in DOMString scriptURI);
614: };</pre>
615:
616: <p>Objects that implement the <code>Window</code> interface must also
617: implement the <code><a
618: href="#windowworkercreators">WindowWorkerCreators</a></code> interface.
619:
1.4 ihickson 620: <p class=big-issue>...
621:
1.7 ihickson 622: <p class=big-issue><dfn id=worker>worker creation succeeded</dfn>, <dfn
623: id=worker0>worker creation failed</dfn>
624:
1.1 ihickson 625: <h2 class=no-num id=references>References</h2>
626:
627: <p class=big-issue>This section will be written in a future
628: draft.<!--XXX-->
629:
630: <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
631: <!-- ACKS -->
632:
633: <p>Thanks to Maciej Stachowiak and Mike Smith for their useful and
634: substantial comments.
1.4 ihickson 635:
636: <p>Huge thanks to the whole Gears team, who pioneered this technology and
637: whose experience has been a huge influence on this specification.
Webmaster