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