Annotation of html5/workers/Overview.html, revision 1.9
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.9 ! ihickson 22: Draft <!--ZZZ-->18 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.9 ! ihickson 147: the W3C Recommendation track. <!--ZZZ:--> This specification is the 18
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:
1.9 ! ihickson 207: <li><a href="#the-workers"><span class=secno>2.3 </span>The worker's
! 208: ports</a>
! 209:
! 210: <li><a href="#processing"><span class=secno>2.4 </span>Processing
1.4 ihickson 211: model</a>
1.1 ihickson 212: </ul>
213:
1.4 ihickson 214: <li><a href="#creating"><span class=secno>3. </span>Creating workers</a>
1.5 ihickson 215: <ul class=toc>
216: <li><a href="#the-windowworkercreators"><span class=secno>3.1 </span>The
217: <code>WindowWorkerCreators</code> interface</a>
218: </ul>
1.4 ihickson 219:
1.1 ihickson 220: <li class=no-num><a href="#references">References</a>
221:
222: <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
223: </ul>
224: <!--end-toc-->
225:
226: <hr>
227:
228: <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
229:
1.4 ihickson 230: <h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3>
1.1 ihickson 231:
232: <p><em>This section is non-normative.</em>
233:
1.4 ihickson 234: <p class=big-issue>This section is missing.
235:
1.3 ihickson 236: <h3 id=requirements><span class=secno>1.2 </span>Requirements</h3>
1.1 ihickson 237:
238: <p><em>This section is non-normative.</em>
239:
1.3 ihickson 240: <p>This specification aims to address the following use cases and
241: requirements:
242:
243: <ul>
244: <li>Background workers: A Web application needs to keep its data
245: synchronised with the server, both sending updates to the server and
246: receiving updates from the server, including handling buffering of
247: updates for when the application goes offline. The code to do this would
248: ideally be independent of the UI code.
249:
250: <li>URLs: Workers should be spawned from URLs, not from strings, since
251: script rarely has access to its own source.
252:
253: <li>Message queuing: Messages sent to a worker before the worker has
254: initialised should not be lost.
255:
256: <li>Workers should have access to timers.
257:
258: <li>Workers should have access to the network.
259:
260: <li>Workers should be able to use libraries.
261:
262: <li>Implementations should not have to expose <code>Node</code> or
263: <code>Document</code> objects to workers.
264:
265: <li>Workers should not share anything with the outside world. The objects
266: representing the worker in the worker itself and in the context that
267: created the worker should be different, for instance.
268:
269: <li>Shared workers: Multiple instances of the same Web application would
270: want to keep just one connection back to the server.
271:
272: <li>Capabilities granting: It should be possible for code running in one
273: iframe to negotiate a connection to another iframe, with that connection
274: granting certain rights (e.g. adding to an address book but not reading
275: from it).
276:
277: <li>Delegation: It should be possible for one worker to spawn another
278: worker and efficiently delagate a request to that worker, without the
279: caller being aware of the delagate and without the original worker having
280: to proxy all the messages.
281:
282: <li>Workers whose parents are not longer useful should be killed. Workers
283: should be able to detect this is about to happen and exit gracefully.
284: </ul>
285:
1.4 ihickson 286: <h3 id=conformance><span class=secno>1.3 </span>Conformance requirements</h3>
1.1 ihickson 287:
288: <p>All diagrams, examples, and notes in this specification are
289: non-normative, as are all sections explicitly marked non-normative.
290: Everything else in this specification is normative.
291:
292: <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
293: NOT",-->
294: "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
295: normative parts of this document are to be interpreted as described in
296: RFC2119. For readability, these words do not appear in all uppercase
297: letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
298: <!-- XXX but they should be
299: marked up -->
300:
301: <p>Requirements phrased in the imperative as part of algorithms (such as
302: "strip any leading space characters" or "return false and abort these
303: steps") are to be interpreted with the meaning of the key word ("must",
304: "should", "may", etc) used in introducing the algorithm.
305:
306: <p>Some conformance requirements are phrased as requirements on attributes,
307: methods or objects. Such requirements are to be interpreted as
308: requirements on user agents.
309:
310: <p>Conformance requirements phrased as algorithms or specific steps may be
311: implemented in any manner, so long as the end result is equivalent. (In
312: particular, the algorithms defined in this specification are intended to
313: be easy to follow, and not intended to be performant.)
314:
315: <p>The only conformance class defined by this specification is user agents.
316:
317: <p>User agents may impose implementation-specific limits on otherwise
318: unconstrained inputs, e.g. to prevent denial of service attacks, to guard
319: against running out of memory, or to work around platform-specific
320: limitations.
321:
1.4 ihickson 322: <h4 id=dependencies><span class=secno>1.3.1 </span>Dependencies</h4>
1.1 ihickson 323:
324: <p>This specification relies on several other underlying specifications.
325:
326: <dl>
327: <dt>HTML5
328:
329: <dd>
330: <p>Many fundamental concepts from HTML5 are used by this specification.
331: <a href="#refsHTML5">[HTML5]</a></p>
332:
333: <dt>ECMAScript
334:
335: <dd>
336: <p>This specification is intended to be used with JavaScript as the
337: scripting language. <a href="#refsJS">[JS]</a></p>
338:
339: <dt>WebIDL
340:
341: <dd>
342: <p>The IDL blocks in this specification use the semantics of the WebIDL
343: specification. <a href="#refsWebIDL">[WebIDL]</a></p>
344: </dl>
345:
1.4 ihickson 346: <h3 id=terminology><span class=secno>1.4 </span>Terminology</h3>
1.1 ihickson 347:
348: <p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
349: <em>visible</em> might sometimes be used when referring to the way a
350: document is rendered to the user. These terms are not meant to imply a
351: visual medium; they must be considered to apply to other media in
352: equivalent ways.
353:
1.4 ihickson 354: <p>The construction "a <code title="">Foo</code> object", where <code
355: title="">Foo</code> is actually an interface, is sometimes used instead of
356: the more accurate "an object implementing the interface <code
357: title="">Foo</code>".
1.1 ihickson 358:
359: <p>The term DOM is used to refer to the API set made available to scripts
360: in Web applications, and does not necessarily imply the existence of an
361: actual <code>Document</code> object or of any other <code>Node</code>
362: objects as defined in the DOM Core specifications. <a
363: href="#refsDOM3CORE">[DOM3CORE]</a>
364:
365: <p>A DOM attribute is said to be <em>getting</em> when its value is being
366: retrieved (e.g. by author script), and is said to be <em>setting</em> when
367: a new value is assigned to it.
368:
369: <p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means
370: that any attributes returning that object must always return the same
371: object (not a new object each time), and the attributes and methods on
372: that object must operate on the actual underlying data, not a snapshot of
373: the data.
374:
1.4 ihickson 375: <h2 id=inside><span class=secno>2. </span>Inside workers</h2>
376:
377: <h3 id=the-windowworker><span class=secno>2.1 </span>The <code><a
378: href="#windowworker">WindowWorker</a></code> interface</h3>
379:
380: <pre
381: class=idl>[NoInterfaceObject] interface <dfn id=windowworker>WindowWorker</dfn> {
1.9 ! ihickson 382: readonly attribute DOMString <a href="#url" title=dom-windowworker-URL>URL</a>;
! 383: readonly attribute DOMString <a href="#name" title=dom-windowworker-name>name</a>;
1.4 ihickson 384: readonly attribute boolean <a href="#closing" title=dom-windowworker-closing>closing</a>;
1.8 ihickson 385: void <a href="#close" title=dom-windowworker-close>close</a>();
1.9 ! ihickson 386:
! 387: // event handler attributes
! 388: attribute <span>EventListener</span> <a href="#onattach" title=handler-windowworker-onattach>onattach</a>;
! 389: attribute <span>EventListener</span> <a href="#onunload" title=handler-windowworker-onunload>onunload</a>;
1.4 ihickson 390: };</pre>
391:
392: <p>Objects that implement the <code><a
393: href="#windowworker">WindowWorker</a></code> interface must also implement
394: the <code>Window</code> interface (and thus also the
1.5 ihickson 395: <code>WindowTimers</code> and <code><a
396: href="#windowworkercreators">WindowWorkerCreators</a></code> interfaces)
397: and the <code>EventTarget</code> interface.
1.4 ihickson 398:
1.9 ! ihickson 399: <p>The <dfn id=url title=dom-windowworker-URL><code>URL</code></dfn>
! 400: attribute must return the value it was assigned when the <code><a
! 401: href="#windowworker">WindowWorker</a></code> object was created by the "<a
! 402: href="#run-a">run a worker</a>" algorithm. It gives the <span>absolute
! 403: URL</span> of the script that was used to initialize the worker.
! 404:
1.6 ihickson 405: <p>The <dfn id=name title=dom-windowworker-name><code>name</code></dfn>
406: attribute must return the value it was assigned when the <code><a
407: href="#windowworker">WindowWorker</a></code> object was created by the "<a
1.9 ! ihickson 408: href="#run-a">run a worker</a>" algorithm. If it has a value that isn't
! 409: the empty string, its value represents the name that can be used to obtain
! 410: a reference to the worker using the <code
! 411: title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker()</code>
! 412: method.
1.6 ihickson 413:
1.4 ihickson 414: <p>The <dfn id=closing
415: title=dom-windowworker-closing><code>closing</code></dfn> attribute must
416: return false until the "<a href="#kill-a">kill a worker</a>" processing
417: model defined below sets it to false.
418:
1.9 ! ihickson 419: <p>The following are the <span>event handler DOM attributes</span> that
! 420: must be supported by objects implementing the <code><a
! 421: href="#windowworker">WindowWorker</a></code> interface:
! 422:
! 423: <dl>
! 424: <dt><dfn id=onattach
! 425: title=handler-windowworker-onattach><code>onattach</code></dfn>
! 426:
! 427: <dd>
! 428: <p>Must be invoked whenever a <code
! 429: title=event-windowworker-attach>attach</code> event is targeted at or
! 430: bubbles through the <code><a
! 431: href="#windowworker">WindowWorker</a></code> object.
! 432:
! 433: <dt><dfn id=onunload
! 434: title=handler-windowworker-onunload><code>onunload</code></dfn>
! 435:
! 436: <dd>
! 437: <p>Must be invoked whenever a <code title=event-unload>unload</code>
! 438: event is targeted at or bubbles through the <code><a
! 439: href="#windowworker">WindowWorker</a></code> object.
! 440: </dl>
! 441:
1.4 ihickson 442: <h3 id=the-queue><span class=secno>2.2 </span>The queue of events</h3>
443:
444: <p>Each <code><a href="#windowworker">WindowWorker</a></code> object is
445: asssociated with a <dfn id=queue>queue of events</dfn>, which is initially
446: empty.
447:
448: <p>An event in the queue can be a DOM event or a timeout callback.
449:
1.8 ihickson 450: <p>All asynchronous callbacks and events that would be called or dispatched
451: in the worker must be added to the worker's queue, with the "<a
452: href="#run-a">run a worker</a>" processing model below taking care of
453: actually calling the callbacks or dispatching the events.
454:
455: <p>Once the <code><a href="#windowworker">WindowWorker</a></code>'s <code
456: title=dom-windowworker-closing><a href="#closing">closing</a></code>
457: attribute is set to true, the queue must discard anything else that would
458: be added to it. Effectively, once the <code
459: title=dom-windowworker-closing><a href="#closing">closing</a></code>
460: attribute is true, timers stop firing, notifications for all pending
461: asynchronous operations are dropped, etc.
462:
1.9 ! ihickson 463: <h3 id=the-workers><span class=secno>2.3 </span>The worker's ports</h3>
! 464:
! 465: <p>Workers communicate with other workers and with <span title="browsing
! 466: context">browsing contexts</span> through <span title="channel
! 467: messaging">message channels</span> and their <code>MessagePort</code>
! 468: objects.
! 469:
! 470: <p>Each <code><a href="#windowworker">WindowWorker</a></code> <var
! 471: title="">worker</var> has a list of <dfn id=the-workers0>the worker's
! 472: ports</dfn>, which consists of all the <code>MessagePort</code> objects
! 473: that are entangled with another port and that have one (but only one) port
! 474: whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is <var
! 475: title="">worker</var>. This list includes all the <code>MessagePort</code>
! 476: objects that are in events pending in the <a href="#queue">queue of
! 477: events</a>.
! 478:
! 479: <h3 id=processing><span class=secno>2.4 </span>Processing model</h3>
1.4 ihickson 480:
1.6 ihickson 481: <p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var
1.7 ihickson 482: title="">name</var> for a script with <span>URL</span> <var
483: title="">url</var>, a browsing context <var title="">owner browsing
484: context</var> and a <code>Document</code> <var title="">owner
485: document</var>, it must run the following steps in a completely separate
486: and parallel execution environment:
1.4 ihickson 487:
488: <ol>
489: <li>
1.7 ihickson 490: <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
491: <var title="">url</var>.</p>
492:
493: <p>If the attempt fails, then abort these steps and invoke the <a
1.9 ! ihickson 494: href="#worker" title="worker creation failed">error handling steps</a>
1.7 ihickson 495: defined by the algorithm that called this one.</p>
496:
497: <p>If the attempt succeeds, then let <var title="">script</var> be the
498: resource that was obtained.</p>
499:
500: <p class=note>As with <code>script</code> elements, the MIME type of the
501: script is ignored. Unlike with <code>script</code> elements, there is no
502: way to override the type. It's always assumed to be JavaScript.</p>
503: <!-- XXX people will complain about
504: this. I guess we might want to examine the MIME type... -->
505:
506:
507: <li>
1.4 ihickson 508: <p>Create a new <code><a href="#windowworker">WindowWorker</a></code>
509: object, <var title="">window</var>.</p>
510:
511: <li>
1.9 ! ihickson 512: <p>Set the <code title=dom-windowworker-URL><a href="#url">URL</a></code>
! 513: attribute of <var title="">window</var> to the value of <var
! 514: title="">url</var>.</p>
! 515:
! 516: <li>
1.6 ihickson 517: <p>Set the <code title=dom-windowworker-name><a
518: href="#name">name</a></code> attribute of <var title="">window</var> to
519: the value of <var title="">name</var>.</p>
520:
521: <li>
1.4 ihickson 522: <p>Let <var title="">script</var>'s <span>script execution context</span>
523: (and thus also <span>global object</span>) be <var
524: title="">window</var>.</p>
525:
526: <li>
527: <p>Let <var title="">script</var>'s <span>script browsing context</span>
528: be <var title="">owner browsing context</var>.</p>
529:
530: <li>
531: <p>Let <var title="">script</var>'s <span>script document context</span>
532: be <var title="">owner document</var>.</p>
533:
534: <li>
1.9 ! ihickson 535: <p>Invoke the <a href="#success" title="worker creation
! 536: succeeded">success steps</a> defined by the algorithm that called this
! 537: one. (This will add an event to the <a href="#queue">queue of
! 538: events</a>.)</p>
! 539:
! 540: <li>
! 541: <p>Start monitoring <var title="">worker</var>, such that whenever the
! 542: <var title="">window</var> object's <code
! 543: title=dom-windowworker-closing><a href="#closing">closing</a></code>
! 544: attribute is false and all <a href="#the-workers0">the worker's
! 545: ports</a> have their <code title=dom-MessagePort-active>active</code>
! 546: attributes set to false, the user agent suspends execution of script in
! 547: that worker until such time as either the <code
! 548: title=dom-windowworker-closing><a href="#closing">closing</a></code>
! 549: attribute switches to true or one of the <code>MessagePort</code>
! 550: objects in the list of <a href="#the-workers0">the worker's ports</a>
! 551: has an <code title=dom-MessagePort-active>active</code> attribute with
! 552: the value true.</p>
! 553:
! 554: <li>
! 555: <p>Start monitoring <var title="">worker</var>, such that as soon as the
! 556: list of <a href="#the-workers0">the worker's ports</a> becomes empty,
! 557: the <var title="">window</var> object's <code
! 558: title=dom-windowworker-closing><a href="#closing">closing</a></code>
! 559: attribute is set to true.</p>
1.7 ihickson 560:
561: <li>
562: <p>Run <var title="">script</var> until it either returns, fails to catch
563: an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
564: a worker</a>" algorithm below.</p>
565:
566: <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
567: worker</a>" algorithm, then that same algorithm will cause there to only
568: be a single event in the <a href="#queue">queue of events</a> at the
1.9 ! ihickson 569: next step, namely the <code title=message-unload>unload</code> event.
! 570: However, if the event is ignored then it will become true as soon as
! 571: that port is garbage collected.</p>
1.4 ihickson 572:
573: <li>
1.9 ! ihickson 574: <p><i>Event loop</i>: Wait until either there is an event in the <a
1.4 ihickson 575: href="#queue">queue of events</a> associated with <var
1.9 ! ihickson 576: title="">window</var> or the <var title="">window</var> object's <code
! 577: title=dom-windowworker-closing><a href="#closing">closing</a></code>
! 578: attribute is set to true.</p>
1.4 ihickson 579:
580: <li>
581: <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1.9 ! ihickson 582: events</a>, if any. The handling of this event or the execution of this
! 583: callback might get prematurely aborted by the "<a href="#kill-a">kill a
1.8 ihickson 584: worker</a>" algorithm below.</p>
1.4 ihickson 585:
586: <li>
587: <p>If there are any more events in the <a href="#queue">queue of
588: events</a> or if the <var title="">window</var> object's <code
589: title=dom-windowworker-closing><a href="#closing">closing</a></code>
590: attribute is set to false, then jump back to the step above labeled
591: <i>event loop</i>.</p>
592:
593: <li>
594: <p class=big-issue>timers, intervals, XMLHttpRequests, database
595: transactions, etc, must be killed; ports must be deactivated and
1.8 ihickson 596: unentangled (do not send unload)</p>
1.4 ihickson 597: </ol>
598:
599: <hr>
600:
601: <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run
602: the following steps in parallel with the worker's main loop (the "<a
603: href="#run-a">run a worker</a>" processing model defined above):
604:
605: <ol>
606: <li>
607: <p>Create an <code>Event</code> object with the event name <code
608: title=event-unload>unload</code>, which does not bubble and is not
609: cancelable, and add it to the worker's <code><a
610: href="#windowworker">WindowWorker</a></code> object's <a
1.8 ihickson 611: href="#queue">queue of events</a>, targetted at the <code><a
612: href="#windowworker">WindowWorker</a></code> object itself.
1.4 ihickson 613:
614: <li>
1.8 ihickson 615: <p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code>
616: object's <code title=dom-windowworker-closing><a
617: href="#closing">closing</a></code> attribute to true.
618:
619: <li>
620: <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
621: a worker</a>" processing model defined above immediately starts running
622: event listeners registered for <code title=event-unload>unload</code>
623: event, this time should not be zero — the idea is that the <code
624: title=event-unload>unload</code> event can be used to clean up when
625: shutting down unexpectedly.
1.4 ihickson 626:
627: <li>
628: <p>If there are any events in the <a href="#queue">queue of events</a>
629: other than the <code title=event-unload>unload</code> event that this
630: algorithm just added, discard them without dispatching them.
631:
632: <li>
633: <p>If the <code title=event-unload>unload</code> event that this
634: algorithm just added hasn't yet been dispatched, then abort the script
635: currently running in the worker.
636:
637: <li>
638: <p>Wait a user-agent-defined amount of time.
639:
640: <li>
641: <p>Abort the script currently running in the worker (if any script is
642: running, then it will be a handler for the <code
643: title=event-unload>unload</code> event).
644: </ol>
645:
1.8 ihickson 646: <hr>
647:
648: <p>When a script invokes the <dfn id=close
649: title=dom-windowworker-close><code>close()</code></dfn> method on a
650: <code><a href="#windowworker">WindowWorker</a></code> object, the user
651: agent must run the following steps:
652:
653: <ol>
654: <li>
655: <p>Create an <code>Event</code> object with the event name <code
656: title=event-unload>unload</code>, which does not bubble and is not
657: cancelable, and add it to the <code><a
658: href="#windowworker">WindowWorker</a></code> object's <a
659: href="#queue">queue of events</a>, targetted at the <code><a
660: href="#windowworker">WindowWorker</a></code> object itself.
661:
662: <li>
663: <p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code>
664: object's <code title=dom-windowworker-closing><a
665: href="#closing">closing</a></code> attribute to true.
666:
667: <li>
668: <p>For each <code>MessagePort</code> object that is entangled with
669: another port and that has one (but only one) port whose <code
1.9 ! ihickson 670: title=dom-MessagePort-ownerWindow>ownerWindow</code> is the <code><a
1.8 ihickson 671: href="#windowworker">WindowWorker</a></code> object on which the method
672: was invoked, run the following substeps:</p>
673:
674: <ol>
675: <li>
676: <p>Unentangle the two ports.
677:
678: <li>
1.9 ! ihickson 679: <p>Set both ports' <code title=dom-MessagePort-active>active</code>
1.8 ihickson 680: attribute to false.
681:
682: <li>
683: <p>At the next available opportunity, after any scripts have finished
684: executing<!-- XXX queue -->, <span>fire a simple event</span> called
685: <code title=event-unload>unload</code> at the other port (the one
1.9 ! ihickson 686: whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is
1.8 ihickson 687: not the <code><a href="#windowworker">WindowWorker</a></code> object
688: on which the <code title=dom-windowworker-close><a
689: href="#close">close()</a></code> method was called).
690: </ol>
691: </ol>
692:
1.4 ihickson 693: <h2 id=creating><span class=secno>3. </span>Creating workers</h2>
694:
1.5 ihickson 695: <h3 id=the-windowworkercreators><span class=secno>3.1 </span>The <code><a
696: href="#windowworkercreators">WindowWorkerCreators</a></code> interface</h3>
697:
698: <pre
699: class=idl>[NoInterfaceObject] interface <dfn id=windowworkercreators>WindowWorkerCreators</dfn> {
1.9 ! ihickson 700: <span>MessagePort</span> <span title=dom-WindowWorkerCreators-createWorker>createWorker</span>(in DOMString scriptURL);
! 701: <span>MessagePort</span> <span title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker</span>(in DOMString name, in DOMString scriptURL);
1.5 ihickson 702: };</pre>
703:
704: <p>Objects that implement the <code>Window</code> interface must also
705: implement the <code><a
706: href="#windowworkercreators">WindowWorkerCreators</a></code> interface.
707:
1.9 ! ihickson 708: <hr>
! 709:
! 710: <p>When the <code
! 711: title=dom-WindowWorkerCreators-createWorker>createWorker(<var
! 712: title="">scriptURL</var>)</code> method is invoked, the user agent must
! 713: run the following steps:
! 714:
! 715: <ol>
! 716: <li>
! 717: <p><span title="resolve a url">Resolve</span> the <var
! 718: title="">scriptURL</var> argument.
! 719:
! 720: <li>
! 721: <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
! 722:
! 723: <li>
! 724: <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
! 725: is not the <span title="same origin">same</span> as the origin of the
! 726: script that invoked the method, then throw a <span>security
! 727: exception</span>.
! 728:
! 729: <li>
! 730: <p><a href="#create">Create a worker</a> from the resulting
! 731: <span>absolute URL</span> whose name is the empty string.
! 732:
! 733: <li>
! 734: <p>Return the <code>MessagePort</code> object returned from the <a
! 735: href="#create">create a worker</a> algorithm.
! 736: </ol>
! 737:
! 738: <hr>
! 739:
! 740: <p>When the <code
! 741: title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker(<var
! 742: title="">name</var>, <var title="">scriptURL</var>)</code> method is
! 743: invoked, the user agent must run the following steps:
! 744:
! 745: <ol>
! 746: <li>
! 747: <p><span title="resolve a url">Resolve</span> the <var
! 748: title="">scriptURL</var> argument.
1.4 ihickson 749:
1.9 ! ihickson 750: <li>
! 751: <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
! 752:
! 753: <li>
! 754: <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
! 755: is not the <span title="same origin">same</span> as the origin of the
! 756: script that invoked the method, then throw a <span>security
! 757: exception</span>.
! 758:
! 759: <li>
! 760: <p>If the <var title="">name</var> argument is the empty string, <a
! 761: href="#create">create a worker</a> from the resulting <span>absolute
! 762: URL</span>, whose name is the empty string, and return the
! 763: <code>MessagePort</code> object returned from the <a
! 764: href="#create">create a worker</a> algorithm. Then, abort these steps.
! 765:
! 766: <li>
! 767: <p>If there exists a worker whose <code title=dom-windowworker-closing><a
! 768: href="#closing">closing</a></code> attribute is false, whose <code
! 769: title=dom-windowworker-name><a href="#name">name</a></code> attribute is
! 770: exactly equal to the <var title="">name</var> argument, and whose <code
! 771: title=dom-windowworker-URL><a href="#url">URL</a></code> attribute has
! 772: the <span>same origin</span> as the resulting <span>absolute URL</span>,
! 773: then run these substeps:</p>
! 774:
! 775: <ol>
! 776: <li>
! 777: <p>If that worker's <code title=dom-windowworker-URL><a
! 778: href="#url">URL</a></code> attribute is not exactly equal to the
! 779: resulting <span>absolute URL</span>, then throw a
! 780: <code>URL_MISMATCH_ERR</code> exception and abort these steps. <span
! 781: class=big-issue>code 19</span>
! 782:
! 783: <li>
! 784: <p><span>Create a new <code>MessagePort</code> object</span> owned by
! 785: the <span>script execution context</span> of the script that invoked
! 786: the method.
! 787:
! 788: <li>
! 789: <p>Return that port.
! 790:
! 791: <li>
! 792: <p>Asynchronously, <a href="#attach" title="attach to a
! 793: worker">attach</a> to this preexisting worker, with the newly created
! 794: port.
! 795: </ol>
! 796:
! 797: <p>Otherwise, <a href="#create">create a worker</a> from the resulting
! 798: <span>absolute URL</span>, whose name is the value of the <var
! 799: title="">name</var> argument, and return the <code>MessagePort</code>
! 800: object returned from the <a href="#create">create a worker</a>
! 801: algorithm.</p>
! 802: </ol>
! 803:
! 804: <hr>
! 805:
! 806: <p>The steps to <dfn id=create>create a worker</dfn> from a
! 807: <span>URL</span> <var title="">url</var> and whose name is <var
! 808: title="">name</var>, in the context of a method call, are as follows:
! 809:
! 810: <ol>
! 811: <li>
! 812: <p><span>Create a new <code>MessagePort</code> object</span> owned by the
! 813: <span>script execution context</span> of the script that invoked the
! 814: method.
! 815:
! 816: <li>
! 817: <p>Return that port.
! 818:
! 819: <li>
! 820: <p>In a parallel execution context (i.e. a separate thread or process or
! 821: equivalent construct), <a href="#run-a">run a worker</a> named <var
! 822: title="">name</var> for the script with <span>URL</span> <var
! 823: title="">url</var>, with the <span>script browsing context</span> of the
! 824: script that invoked the method as the <var title="">owner browsing
! 825: context</var> and with the <span>script document context</span> of the
! 826: script that invoked the method as the <var title="">owner
! 827: document</var>.</p>
! 828:
! 829: <p>If that algorithm invokes the steps for <dfn id=success title="worker
! 830: creation succeeded">success steps</dfn>, then <a href="#attach"
! 831: title="attach to a worker">attach</a> to this new worker, with the newly
! 832: created port.</p>
! 833:
! 834: <p>Otherwise, if the <dfn id=worker>worker creation failed</dfn>, then at
! 835: the next available opportunity, after any scripts have finished
! 836: executing<!-- XXX queue -->, <span>fire a simple event</span> called
! 837: <code title=event-error>error</code> at the newly created port.</p>
! 838: </ol>
! 839:
! 840: <hr>
! 841:
! 842: <p>The steps to <dfn id=attach>attach to a worker</dfn> given a
! 843: <code>MessagePort</code> object <var title="">port</var> are as follows:
! 844:
! 845: <ol>
! 846: <li>
! 847: <p>If <var title="">port</var> would have been garbage collected, or if
! 848: the <span>active document</span> of the <code
! 849: title=dom-MessagePort-ownerWindow>ownerWindow</code> of <var
! 850: title="">port</var> is no longer the same <code>Document</code> object
! 851: as when <var title="">port</var> was created, then do nothing. Abort
! 852: these steps. If the worker was just created, it'll get killed
! 853: immediately.</p>
! 854:
! 855: <li>
! 856: <p><span>Create a new <code>MessagePort</code> object</span> owned by the
! 857: <code><a href="#windowworker">WindowWorker</a></code> of the worker.
! 858:
! 859: <li>
! 860: <p><span>Entangle</span> this newly created port and the port <var
! 861: title="">port</var> that was passed to these steps.
! 862:
! 863: <li>
! 864: <p>Set the <code title=dom-MessagePort-active>active</code> attribute of
! 865: both ports to true.
! 866:
! 867: <li>
! 868: <p>At the next available opportunity, after any scripts have finished
! 869: executing<!-- XXX queue -->, <span>fire a simple event</span> called
! 870: <code title=event-load>load</code> at <var title="">port</var>.
! 871:
! 872: <li>
! 873: <p>Create an event that uses the <code>MessageEvent</code> interface,
! 874: with the name <code title=event-attach>attach</code>, which does not
! 875: bubble, is cancelable, has no default action, has a <code
! 876: title=dom-MessageEvent-data>data</code> attribute whose value is the
! 877: empty string and has a <code
! 878: title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
! 879: value is the newly created port, and add it to the worker's <code><a
! 880: href="#windowworker">WindowWorker</a></code> object's <a
! 881: href="#queue">queue of events</a>, targetted at the <code><a
! 882: href="#windowworker">WindowWorker</a></code> object itself.
! 883: </ol>
1.7 ihickson 884:
1.1 ihickson 885: <h2 class=no-num id=references>References</h2>
886:
887: <p class=big-issue>This section will be written in a future
888: draft.<!--XXX-->
889:
890: <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
891: <!-- ACKS -->
892:
893: <p>Thanks to Maciej Stachowiak and Mike Smith for their useful and
894: substantial comments.
1.4 ihickson 895:
896: <p>Huge thanks to the whole Gears team, who pioneered this technology and
897: whose experience has been a huge influence on this specification.
Webmaster