Annotation of html5/workers/Overview.html, revision 1.34
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.26 ihickson 22: Draft <!--ZZZ--> 6 August 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.26 ihickson 147: the W3C Recommendation track. <!--ZZZ:--> This specification is the 6
1.25 ihickson 148: August 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.29 ihickson 185: <ul class=toc>
186: <li><a href="#a-background"><span class=secno>1.1.1 </span>A
1.32 ihickson 187: background number-crunching worker</a>
1.31 ihickson 188:
189: <li><a href="#a-worker"><span class=secno>1.1.2 </span>A worker for
190: updating a client-side database</a>
1.33 ihickson 191:
192: <li><a href="#worker"><span class=secno>1.1.3 </span>Worker used for
193: backgroud I/O</a>
1.34 ! ihickson 194:
! 195: <li><a href="#shared"><span class=secno>1.1.4 </span>Shared
! 196: workers</a>
! 197:
! 198: <li><a href="#granting"><span class=secno>1.1.5 </span>Granting
! 199: capabilities</a>
! 200:
! 201: <li><a href="#delegation"><span class=secno>1.1.6
! 202: </span>Delegation</a>
1.29 ihickson 203: </ul>
1.1 ihickson 204:
1.22 ihickson 205: <li><a href="#conformance"><span class=secno>1.2 </span>Conformance
1.1 ihickson 206: requirements</a>
207: <ul class=toc>
1.22 ihickson 208: <li><a href="#dependencies"><span class=secno>1.2.1
1.1 ihickson 209: </span>Dependencies</a>
210: </ul>
211:
1.22 ihickson 212: <li><a href="#terminology"><span class=secno>1.3 </span>Terminology</a>
1.4 ihickson 213: </ul>
214:
1.12 ihickson 215: <li><a href="#infrastructure"><span class=secno>2.
216: </span>Infrastructure</a>
1.4 ihickson 217: <ul class=toc>
1.26 ihickson 218: <li><a href="#the-workerglobalscope"><span class=secno>2.1 </span>The
219: <code>WorkerGlobalScope</code> interface</a>
1.4 ihickson 220:
1.15 ihickson 221: <li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
222: origins of workers</a>
223:
224: <li><a href="#the-queue"><span class=secno>2.3 </span>The queue of
1.4 ihickson 225: events</a>
226:
1.15 ihickson 227: <li><a href="#the-workers"><span class=secno>2.4 </span>The worker's
1.9 ihickson 228: ports</a>
229:
1.15 ihickson 230: <li><a href="#processing"><span class=secno>2.5 </span>Processing
1.4 ihickson 231: model</a>
1.12 ihickson 232:
1.15 ihickson 233: <li><a href="#creating"><span class=secno>2.6 </span>Creating
1.12 ihickson 234: workers</a>
1.1 ihickson 235: </ul>
236:
1.12 ihickson 237: <li><a href="#apis-available"><span class=secno>3. </span>APIs available
238: to workers</a>
1.26 ihickson 239: <ul class=toc>
240: <li><a href="#importing"><span class=secno>3.1 </span>Importing scripts
241: and libraries</a>
242:
243: <li><a href="#apis-defined"><span class=secno>3.2 </span>APIs defined in
244: other specifications</a>
245:
246: <li><a href="#interface"><span class=secno>3.3 </span>Interface objects
247: and constructors</a>
1.34 ! ihickson 248:
! 249: <li><a href="#worker0"><span class=secno>3.4 </span>Worker locations</a>
! 250:
1.26 ihickson 251: </ul>
1.4 ihickson 252:
1.1 ihickson 253: <li class=no-num><a href="#references">References</a>
254:
255: <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
256: </ul>
257: <!--end-toc-->
258:
259: <hr>
260:
261: <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
262:
1.4 ihickson 263: <h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3>
1.1 ihickson 264:
265: <p><em>This section is non-normative.</em>
266:
1.29 ihickson 267: <p>There are a variety of uses that workers can be put to. The following
268: subsections show various examples of this use.
269:
270: <h4 id=a-background><span class=secno>1.1.1 </span>A background
1.32 ihickson 271: number-crunching worker</h4>
1.29 ihickson 272:
273: <p><em>This section is non-normative.</em>
274:
275: <p>The simplest use of workers is for performing a computationally
276: expensive task without interrupting the user interface.
277:
1.32 ihickson 278: <p>In this example, the main document spawns a worker to (naïvely)
1.29 ihickson 279: compute prime numbers, and progressively displays the most recently found
280: prime number.
281:
282: <p>The main page is as follows:
283:
284: <pre><!DOCTYPE HTML>
285: <html>
286: <head>
287: <title>Worker example: Computation</title>
288: </head>
289: <body>
290: <p>The highest prime number discovered so far is: <output id="result"></output></p>
291: <script>
292: var worker = createWorker('worker.js');
293: worker.onmessage = function (event) {
294: document.getElementById('result').textContent = event.message;
295: };
296: </script>
297: </body>
298: </html></pre>
299:
300: <p>The <code title=dom-WorkerFactory-createWorker><a
301: href="#createworker">createWorker()</a></code> method call creates a
302: worker and returns a <code>MessagePort</code> object, which is used to
303: communicate with the worker. That object's <code
304: title=dom-MessagePort-onmessage>onmessage</code> event handler attribute
305: allows the code to receive messages from the worker.
306:
307: <p>The worker itself is as follows:
308:
309: <pre>var n = 1;
310: search: while (true) {
311: n += 1;
312: for (var i = 2; i <= Math.sqrt(n); i += 1)
313: if (n % i == 0)
314: continue search;
315: // found a prime!
316: port.postMessage(n);
317: }</pre>
318:
319: <p>The bulk of this code is simply an unoptimised search for a prime
320: number. To send a message back to the page, the <code
1.30 ihickson 321: title=dom-WorkerGlobalScope-port><a href="#port">port</a></code> variable
322: (defined automatically when the worker is created) is used to post a
323: message when a prime is found.
1.29 ihickson 324:
325: <p><a href="http://www.whatwg.org/demos/workers/primes/page.html">View this
326: example online</a>.
1.4 ihickson 327:
1.31 ihickson 328: <h4 id=a-worker><span class=secno>1.1.2 </span>A worker for updating a
329: client-side database</h4>
330:
331: <p><em>This section is non-normative.</em>
332:
333: <p>In this example, the main document spawns a worker whose only task is to
334: listen for notifications from the server, and, when appropriate, either
335: add or remove data from the client-side database.
336:
337: <p>Since no communication occurs between the worker and the main page, the
338: main page can start the worker by just doing:
339:
340: <pre><script>
341: createWorker('worker.js');
342: </script></pre>
343:
344: <p>The worker itself is as follows:
345:
346: <pre>var server = new WebSocket('ws://whatwg.org/database');
347: var database = utils.openDatabase('demobase', '1.0', 'Demo Database', 10240);
348: server.onmessage = function (event) {
349: // data is in the format "command key value"
350: var data = event.message.split(' ');
351: switch (data[0]) {
352: case '+':
353: database.transaction(function(tx) {
354: tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
355: });
356: case '-':
357: database.transaction(function(tx) {
358: tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
359: });
360: }
361: };</pre>
362:
363: <p>This connects to the server using the <code>WebSocket</code> mechanism
364: and opens the local database (which, we presume, has been created
365: earlier). The worker then just listens for messages from the worker and
366: acts on them as appropriate, forever (or until the main page is closed).
367:
368: <p><a
369: href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
370: this example online</a>. (This example will not actually function, since
371: the server does not actually exist and the database is not created by this
372: sample code.)
373:
1.33 ihickson 374: <h4 id=worker><span class=secno>1.1.3 </span>Worker used for backgroud I/O</h4>
375:
376: <p><em>This section is non-normative.</em>
377:
378: <p>In this example, the main document uses two workers, one for fetching
379: stock updates for at regular intervals, and one for fetching performing
380: search queries that the user requests.
381:
382: <p>The main page is as follows:
383:
384: <pre><!DOCTYPE HTML>
385: <html>
386: <head>
387: <title>Worker example: Stock ticker</title>
388: <script>
389: // TICKER
390: var symbol = 'GOOG'; // default symbol to watch
391: var ticker = createWorker('ticker.js');
392: ticker.postMessage(symbol);
393:
394: // SEARCHER
395: var searcher = createWorker('searcher.js');
396: function search(query) {
397: searcher.postMessage(query);
398: }
399:
400: // SYMBOL SELECTION UI
401: function select(newSymbol) {
402: symbol = newSymbol;
403: ticker.postMessage(symbol);
404: }
405: </script>
406: </head>
407: <body>
408: <p><output id="symbol"></output> <output id="value"></output></p>
409: <script>
410: ticker.onmessage = function (event) {
411: var data = event.message.split(' ');
412: document.getElementById('symbol').textContent = data[0];
413: document.getElementById('value').textContent = data[1];
414: };
415: </script>
416: <p><label>Search: <input type="text" oninput="search(this.value)"></label></p>
417: <ul id="results"></ul>
418: <script>
419: searcher.onmessage = function (event) {
420: var data = event.message.split(' ');
421: var results = document.getElementById('results');
422: while (results.hasChildNodes()) // clear previous results
423: results.removeChild(results.firstChild);
424: for (var i = 0; i < data.length; i += 1) {
425: // add a list item with a button for each result
426: var li = document.createElement('li');
427: var button = document.createElement('button');
428: button.value = data[i];
429: button.type = 'button';
430: button.onclick = function () { select(this.value); };
431: button.textContent = data[i];
432: li.appendChild(button);
433: results.appendChild(li);
434: }
435: };
436: </script>
437: <p>(The data in this example is not real. Try searching for "Google" or "Apple".)</p>
438: </body>
439: </html></pre>
440:
441: <p>Messages are queued until the <code
442: title=handler-MessagePort-onmessage>onmessage</code> handler is set, so
443: even if it takes a while for the message handler to be attached, no data
444: is lost.
445:
446: <p>The two workers use a common library for performing the actual network
447: calls. This library is as follows:
448:
449: <pre>function get(url) {
450: try {
451: var xhr = new XMLHttpRequest();
452: xhr.open('GET', url, false);
453: xhr.send();
454: return xhr.responseText;
1.34 ! ihickson 455: } catch (e) {
1.33 ihickson 456: return ''; // turn all errors into empty results
457: }
458: }</pre>
459:
460: <p>The stock updater worker is as follows:
461:
462: <pre>importScript('io.js');
463: var timer;
464: var symbol;
465: function update() {
466: port.postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
467: timer = setTimeout(update, 10000);
468: }
469: port.onmessage = function (event) {
470: if (timer)
471: clearTimeout(timer);
472: symbol = event.message;
473: update();
474: };</pre>
475:
476: <p>The search query worker is as follows:
477:
478: <pre>importScript('io.js');
479: port.onmessage = function (event) {
480: port.postMessage(get('search.cgi?' + event.message));
481: };</pre>
482:
483: <p><a href="http://www.whatwg.org/demos/workers/stock/page.html">View this
484: example online</a>.
485:
1.34 ! ihickson 486: <h4 id=shared><span class=secno>1.1.4 </span>Shared workers</h4>
! 487:
! 488: <p class=big-isue>...
! 489:
! 490: <h4 id=granting><span class=secno>1.1.5 </span>Granting capabilities</h4>
! 491:
! 492: <p class=big-isue>...
! 493:
! 494: <h4 id=delegation><span class=secno>1.1.6 </span>Delegation</h4>
! 495:
! 496: <p class=big-isue>...
! 497:
1.22 ihickson 498: <h3 id=conformance><span class=secno>1.2 </span>Conformance requirements</h3>
1.1 ihickson 499:
500: <p>All diagrams, examples, and notes in this specification are
501: non-normative, as are all sections explicitly marked non-normative.
502: Everything else in this specification is normative.
503:
504: <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
505: NOT",-->
506: "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
507: normative parts of this document are to be interpreted as described in
508: RFC2119. For readability, these words do not appear in all uppercase
509: letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
510: <!-- XXX but they should be
511: marked up -->
512:
513: <p>Requirements phrased in the imperative as part of algorithms (such as
514: "strip any leading space characters" or "return false and abort these
515: steps") are to be interpreted with the meaning of the key word ("must",
516: "should", "may", etc) used in introducing the algorithm.
517:
518: <p>Some conformance requirements are phrased as requirements on attributes,
519: methods or objects. Such requirements are to be interpreted as
520: requirements on user agents.
521:
522: <p>Conformance requirements phrased as algorithms or specific steps may be
523: implemented in any manner, so long as the end result is equivalent. (In
524: particular, the algorithms defined in this specification are intended to
525: be easy to follow, and not intended to be performant.)
526:
527: <p>The only conformance class defined by this specification is user agents.
528:
529: <p>User agents may impose implementation-specific limits on otherwise
530: unconstrained inputs, e.g. to prevent denial of service attacks, to guard
531: against running out of memory, or to work around platform-specific
532: limitations.
533:
1.22 ihickson 534: <h4 id=dependencies><span class=secno>1.2.1 </span>Dependencies</h4>
1.1 ihickson 535:
536: <p>This specification relies on several other underlying specifications.
537:
538: <dl>
539: <dt>HTML5
540:
541: <dd>
542: <p>Many fundamental concepts from HTML5 are used by this specification.
543: <a href="#refsHTML5">[HTML5]</a></p>
544:
545: <dt>ECMAScript
546:
547: <dd>
548: <p>This specification is intended to be used with JavaScript as the
549: scripting language. <a href="#refsJS">[JS]</a></p>
550:
551: <dt>WebIDL
552:
553: <dd>
554: <p>The IDL blocks in this specification use the semantics of the WebIDL
555: specification. <a href="#refsWebIDL">[WebIDL]</a></p>
556: </dl>
557:
1.22 ihickson 558: <h3 id=terminology><span class=secno>1.3 </span>Terminology</h3>
1.1 ihickson 559:
560: <p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
561: <em>visible</em> might sometimes be used when referring to the way a
562: document is rendered to the user. These terms are not meant to imply a
563: visual medium; they must be considered to apply to other media in
564: equivalent ways.
565:
1.4 ihickson 566: <p>The construction "a <code title="">Foo</code> object", where <code
567: title="">Foo</code> is actually an interface, is sometimes used instead of
568: the more accurate "an object implementing the interface <code
569: title="">Foo</code>".
1.1 ihickson 570:
571: <p>The term DOM is used to refer to the API set made available to scripts
572: in Web applications, and does not necessarily imply the existence of an
573: actual <code>Document</code> object or of any other <code>Node</code>
574: objects as defined in the DOM Core specifications. <a
575: href="#refsDOM3CORE">[DOM3CORE]</a>
576:
577: <p>A DOM attribute is said to be <em>getting</em> when its value is being
578: retrieved (e.g. by author script), and is said to be <em>setting</em> when
579: a new value is assigned to it.
580:
581: <p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means
582: that any attributes returning that object must always return the same
583: object (not a new object each time), and the attributes and methods on
584: that object must operate on the actual underlying data, not a snapshot of
585: the data.
586:
1.12 ihickson 587: <h2 id=infrastructure><span class=secno>2. </span>Infrastructure</h2>
1.4 ihickson 588:
1.26 ihickson 589: <h3 id=the-workerglobalscope><span class=secno>2.1 </span>The <code><a
590: href="#workerglobalscope">WorkerGlobalScope</a></code> interface</h3>
1.4 ihickson 591:
592: <pre
1.26 ihickson 593: class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
594: // core worker features
595: readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
1.34 ! ihickson 596: readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
1.26 ihickson 597: readonly attribute DOMString <a href="#name" title=dom-WorkerGlobalScope-name>name</a>;
598: readonly attribute boolean <a href="#closing" title=dom-WorkerGlobalScope-closing>closing</a>;
599: void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
600:
601: // other APIs
602: readonly attribute WorkerUtils <a href="#utils" title=dom-WorkerGlobalScope-utils>utils</a>;
1.9 ihickson 603:
604: // event handler attributes
1.26 ihickson 605: attribute <span>EventListener</span> <a href="#onconnect" title=handler-WorkerGlobalScope-onconnect>onconnect</a>;
606: attribute <span>EventListener</span> <a href="#onunload" title=handler-WorkerGlobalScope-onunload>onunload</a>;
1.4 ihickson 607: };</pre>
608:
1.26 ihickson 609: <p>The <dfn id=self
610: title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
611: return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
612: object itself.
1.9 ihickson 613:
1.34 ! ihickson 614: <p>The <dfn id=location
! 615: title=dom-WorkerGlobalScope-location><code>location</code></dfn> attribute
! 616: must return the <code><a href="#workerlocation">WorkerLocation</a></code>
! 617: object created for the <code><a
! 618: href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
! 619: worker was created. It represents the <span>absolute URL</span> of the
! 620: script that was used to initialize the worker.
1.26 ihickson 621:
622: <p>The <dfn id=name
623: title=dom-WorkerGlobalScope-name><code>name</code></dfn> attribute must
624: return the value it was assigned when the <code><a
625: href="#workerglobalscope">WorkerGlobalScope</a></code> object was created
626: by the "<a href="#run-a">run a worker</a>" algorithm. If it has a value
627: that isn't the empty string, its value represents the name that can be
628: used to obtain a reference to the worker using the <code
629: title=dom-WorkerFactory-createNamedWorker><a
1.11 ihickson 630: href="#createnamedworker">createNamedWorker()</a></code> method.
1.6 ihickson 631:
1.4 ihickson 632: <p>The <dfn id=closing
1.26 ihickson 633: title=dom-WorkerGlobalScope-closing><code>closing</code></dfn> attribute
634: must return false until it is set to true by one of the algorithms in the
1.19 ihickson 635: processing model section below.
1.4 ihickson 636:
1.9 ihickson 637: <p>The following are the <span>event handler DOM attributes</span> that
638: must be supported by objects implementing the <code><a
1.26 ihickson 639: href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
1.9 ihickson 640:
641: <dl>
1.20 ihickson 642: <dt><dfn id=onconnect
1.26 ihickson 643: title=handler-WorkerGlobalScope-onconnect><code>onconnect</code></dfn>
1.9 ihickson 644:
645: <dd>
646: <p>Must be invoked whenever a <code
1.26 ihickson 647: title=event-WorkerGlobalScope-connect>connect</code> event is targeted
648: at or bubbles through the <code><a
649: href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1.9 ihickson 650:
651: <dt><dfn id=onunload
1.26 ihickson 652: title=handler-WorkerGlobalScope-onunload><code>onunload</code></dfn>
1.9 ihickson 653:
654: <dd>
655: <p>Must be invoked whenever a <code title=event-unload>unload</code>
656: event is targeted at or bubbles through the <code><a
1.26 ihickson 657: href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1.9 ihickson 658: </dl>
659:
1.26 ihickson 660: <p>The <dfn id=utils
661: title=dom-WorkerGlobalScope-utils><code>utils</code></dfn> attribute must
662: return the <code><a href="#workerutils">WorkerUtils</a></code> object
663: created for the <code><a
664: href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
665: worker was created.
666:
1.30 ihickson 667: <p>In addition to the above members, the <a href="#success" title="worker
668: creation succeeded">worker creation success steps</a> add a <dfn id=port
669: title=dom-WorkerGlobalScope-port><code>port</code></dfn> property to the
670: object.
671:
1.15 ihickson 672: <h3 id=base-urls><span class=secno>2.2 </span>Base URLs and origins of
673: workers</h3>
674:
675: <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
1.34 ! ihickson 676: worker is the <span>absolute URL</span> given that the worker's <code
! 677: title=dom-WorkerGlobalScope-location><a
! 678: href="#location">location</a></code> attribute represents.
1.15 ihickson 679:
1.25 ihickson 680: <p>Both the <span>origin</span> and <span>effective script origin</span> of
681: scripts running in workers are the <span>origin</span> of the
1.34 ! ihickson 682: <span>absolute URL</span> given that the worker's <code
! 683: title=dom-WorkerGlobalScope-location><a
! 684: href="#location">location</a></code> attribute represents.
1.15 ihickson 685:
686: <h3 id=the-queue><span class=secno>2.3 </span>The queue of events</h3>
1.4 ihickson 687:
1.26 ihickson 688: <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
689: object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
690: initially empty.
1.4 ihickson 691:
692: <p>An event in the queue can be a DOM event or a timeout callback.
693:
1.8 ihickson 694: <p>All asynchronous callbacks and events that would be called or dispatched
695: in the worker must be added to the worker's queue, with the "<a
696: href="#run-a">run a worker</a>" processing model below taking care of
697: actually calling the callbacks or dispatching the events.
698:
1.26 ihickson 699: <p>Once the <code><a
700: href="#workerglobalscope">WorkerGlobalScope</a></code>'s <code
701: title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1.8 ihickson 702: attribute is set to true, the queue must discard anything else that would
703: be added to it. Effectively, once the <code
1.26 ihickson 704: title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1.8 ihickson 705: attribute is true, timers stop firing, notifications for all pending
706: asynchronous operations are dropped, etc.
707:
1.15 ihickson 708: <h3 id=the-workers><span class=secno>2.4 </span>The worker's ports</h3>
1.9 ihickson 709:
710: <p>Workers communicate with other workers and with <span title="browsing
711: context">browsing contexts</span> through <span title="channel
712: messaging">message channels</span> and their <code>MessagePort</code>
713: objects.
714:
1.26 ihickson 715: <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
716: <var title="">worker</var> has a list of <dfn id=the-workers0>the worker's
1.9 ihickson 717: ports</dfn>, which consists of all the <code>MessagePort</code> objects
718: that are entangled with another port and that have one (but only one) port
1.26 ihickson 719: owned by <var title="">worker</var>. This list includes all the
720: <code>MessagePort</code> objects that are in events pending in the <a
721: href="#queue">queue of events</a>.
1.9 ihickson 722:
1.17 ihickson 723: <hr>
724:
725: <p>A worker is said to be a <dfn id=front-line>front-line worker</dfn> if
726: at least one of the <a href="#the-workers0">the worker's ports</a> has an
1.26 ihickson 727: entangled <code>MessagePort</code> owned by a <code>Window</code> object.
1.17 ihickson 728:
729: <p>A worker is said to be a <dfn id=needed>needed worker</dfn> if either:
730:
731: <ul>
732: <li>it is a <a href="#front-line">front-line worker</a>, or
733:
734: <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1.26 ihickson 735: an entangled <code>MessagePort</code> owned by a <code><a
736: href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
737: itself a <a href="#needed">needed worker</a>.
1.17 ihickson 738: </ul>
739:
740: <hr>
741:
742: <p>A worker is said to be an <dfn id=active>active front-line worker</dfn>
1.28 ihickson 743: if at least one of the <a href="#the-workers0">the worker's ports</a> is
744: entangled with a <code>MessagePort</code> <var title="">p</var> whose
745: owner is a <code>Window</code> object whose <span>active document</span>
746: is the <code>Document</code> that was that <span>browsing context</span>'s
747: <span>active document</span> when that <code>MessagePort</code> <var
748: title="">p</var> was created, and that <code>Document</code> is
749: <span>fully active</span>.
1.17 ihickson 750:
751: <p>A worker is said to be an <dfn id=active0>active needed worker</dfn> if
752: either:
753:
754: <ul>
755: <li>it is an <a href="#active">active front-line worker</a>, or
756:
757: <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1.26 ihickson 758: an entangled <code>MessagePort</code> owned by a <code><a
759: href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
760: itself an <a href="#active0">active needed worker</a>.
1.17 ihickson 761: </ul>
762:
1.15 ihickson 763: <h3 id=processing><span class=secno>2.5 </span>Processing model</h3>
1.4 ihickson 764:
1.6 ihickson 765: <p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var
1.7 ihickson 766: title="">name</var> for a script with <span>URL</span> <var
767: title="">url</var>, a browsing context <var title="">owner browsing
768: context</var> and a <code>Document</code> <var title="">owner
769: document</var>, it must run the following steps in a completely separate
770: and parallel execution environment:
1.4 ihickson 771:
772: <ol>
773: <li>
1.7 ihickson 774: <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
775: <var title="">url</var>.</p>
776:
777: <p>If the attempt fails, then abort these steps and invoke the <a
1.34 ! ihickson 778: href="#worker1" title="worker creation failed">error handling steps</a>
1.7 ihickson 779: defined by the algorithm that called this one.</p>
780:
781: <p>If the attempt succeeds, then let <var title="">script</var> be the
782: resource that was obtained.</p>
783:
784: <p class=note>As with <code>script</code> elements, the MIME type of the
785: script is ignored. Unlike with <code>script</code> elements, there is no
786: way to override the type. It's always assumed to be JavaScript.</p>
787: <!-- XXX people will complain about
788: this. I guess we might want to examine the MIME type... -->
789:
790:
791: <li>
1.26 ihickson 792: <p>Create a new <code><a
793: href="#workerglobalscope">WorkerGlobalScope</a></code> object, <var
794: title="">worker</var>.</p>
1.4 ihickson 795:
796: <li>
1.26 ihickson 797: <p>Set the <code title=dom-WorkerGlobalScope-name><a
1.19 ihickson 798: href="#name">name</a></code> attribute of <var title="">worker</var> to
1.6 ihickson 799: the value of <var title="">name</var>.</p>
800:
801: <li>
1.34 ! ihickson 802: <p>Create a new <code><a href="#workerlocation">WorkerLocation</a></code>
! 803: object for the <code title=dom-WorkerGlobalScope-location><a
! 804: href="#location">location</a></code> attribute of <var
! 805: title="">worker</var>, representing <var title="">url</var>.</p>
! 806:
! 807: <li>
1.26 ihickson 808: <p>Create a new <code><a href="#workerutils">WorkerUtils</a></code>
809: object for the <code title=dom-WorkerGlobalScope-utils><a
1.34 ! ihickson 810: href="#utils">utils</a></code> attribute of <var title="">worker</var>.</p>
1.26 ihickson 811:
812: <li>
1.4 ihickson 813: <p>Let <var title="">script</var>'s <span>script execution context</span>
814: (and thus also <span>global object</span>) be <var
1.19 ihickson 815: title="">worker</var>.</p>
1.4 ihickson 816:
817: <li>
818: <p>Let <var title="">script</var>'s <span>script browsing context</span>
819: be <var title="">owner browsing context</var>.</p>
820:
821: <li>
822: <p>Let <var title="">script</var>'s <span>script document context</span>
823: be <var title="">owner document</var>.</p>
824:
825: <li>
1.9 ihickson 826: <p>Invoke the <a href="#success" title="worker creation
827: succeeded">success steps</a> defined by the algorithm that called this
1.17 ihickson 828: one.</p>
829:
830: <p class=note>This will usually add an event to the <a
1.28 ihickson 831: href="#queue">queue of events</a> and set the <code
1.30 ihickson 832: title=dom-WorkerGlobalScope-port><a href="#port">port</a></code>
833: property on the <var title="">worker</var> object. If it does, that
834: event will have a <code>MessagePort</code> and thus the list of <a
1.17 ihickson 835: href="#the-workers0">the worker's ports</a> will not be empty. If it
1.19 ihickson 836: doesn't, then the next step will set the <var title="">worker</var>
1.26 ihickson 837: object's <code title=dom-WorkerGlobalScope-closing><a
1.17 ihickson 838: href="#closing">closing</a></code> attribute to true.</p>
839:
840: <li>
1.28 ihickson 841: <p><strong>Closing orphan workers</strong>: Start monitoring <var
842: title="">worker</var>, such that as soon as the worker stops being a <a
843: href="#needed">needed worker</a>, the <var title="">worker</var>
844: object's <code title=dom-WorkerGlobalScope-closing><a
1.17 ihickson 845: href="#closing">closing</a></code> attribute is set to true.</p>
1.9 ihickson 846:
847: <li>
1.28 ihickson 848: <p><strong>Suspending workers</strong>: Start monitoring <var
849: title="">worker</var>, such that whenever the <var title="">worker</var>
850: object's <code title=dom-WorkerGlobalScope-closing><a
1.26 ihickson 851: href="#closing">closing</a></code> attribute is false and the worker is
852: not an <a href="#active0">active needed worker</a>, the user agent
853: suspends execution of script in that worker until such time as either
854: the <code title=dom-WorkerGlobalScope-closing><a
855: href="#closing">closing</a></code> attribute switches to true or the
856: worker becomes an <a href="#active0">active needed worker</a>.</p>
1.7 ihickson 857:
858: <li>
859: <p>Run <var title="">script</var> until it either returns, fails to catch
860: an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
861: a worker</a>" algorithm below.</p>
862:
863: <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
864: worker</a>" algorithm, then that same algorithm will cause there to only
865: be a single event in the <a href="#queue">queue of events</a> at the
1.28 ihickson 866: next step, namely the <code title=message-unload>unload</code> event.</p>
1.4 ihickson 867:
868: <li>
1.9 ihickson 869: <p><i>Event loop</i>: Wait until either there is an event in the <a
1.4 ihickson 870: href="#queue">queue of events</a> associated with <var
1.19 ihickson 871: title="">worker</var> or the <var title="">worker</var> object's <code
1.26 ihickson 872: title=dom-WorkerGlobalScope-closing><a
873: href="#closing">closing</a></code> attribute is set to true.</p>
1.4 ihickson 874:
875: <li>
876: <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1.9 ihickson 877: events</a>, if any. The handling of this event or the execution of this
878: callback might get prematurely aborted by the "<a href="#kill-a">kill a
1.8 ihickson 879: worker</a>" algorithm below.</p>
1.4 ihickson 880:
881: <li>
882: <p>If there are any more events in the <a href="#queue">queue of
1.19 ihickson 883: events</a> or if the <var title="">worker</var> object's <code
1.26 ihickson 884: title=dom-WorkerGlobalScope-closing><a
885: href="#closing">closing</a></code> attribute is set to false, then jump
886: back to the step above labeled <i>event loop</i>.</p>
1.4 ihickson 887:
888: <li>
889: <p class=big-issue>timers, intervals, XMLHttpRequests, database
1.28 ihickson 890: transactions, etc, must be killed; ports must be unentangled</p>
1.4 ihickson 891: </ol>
892:
893: <hr>
894:
895: <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run
896: the following steps in parallel with the worker's main loop (the "<a
897: href="#run-a">run a worker</a>" processing model defined above):
898:
899: <ol>
900: <li>
901: <p>Create an <code>Event</code> object with the event name <code
902: title=event-unload>unload</code>, which does not bubble and is not
903: cancelable, and add it to the worker's <code><a
1.26 ihickson 904: href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1.8 ihickson 905: href="#queue">queue of events</a>, targetted at the <code><a
1.26 ihickson 906: href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1.28 ihickson 907: </li>
908: <!-- XXX shouldn't add one if closing is
909: already true, assuming unload has already been added to the queue
910: (?) -->
1.4 ihickson 911:
912: <li>
1.26 ihickson 913: <p>Set the worker's <code><a
914: href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
915: title=dom-WorkerGlobalScope-closing><a
1.8 ihickson 916: href="#closing">closing</a></code> attribute to true.
917:
918: <li>
919: <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
920: a worker</a>" processing model defined above immediately starts running
1.28 ihickson 921: event listeners registered for the <code
922: title=event-unload>unload</code> event, this time should not be zero
923: — the idea is that the <code title=event-unload>unload</code>
924: event can be used to clean up when shutting down unexpectedly.
1.4 ihickson 925:
926: <li>
927: <p>If there are any events in the <a href="#queue">queue of events</a>
928: other than the <code title=event-unload>unload</code> event that this
929: algorithm just added, discard them without dispatching them.
930:
931: <li>
932: <p>If the <code title=event-unload>unload</code> event that this
933: algorithm just added hasn't yet been dispatched, then abort the script
934: currently running in the worker.
935:
936: <li>
937: <p>Wait a user-agent-defined amount of time.
938:
939: <li>
940: <p>Abort the script currently running in the worker (if any script is
941: running, then it will be a handler for the <code
942: title=event-unload>unload</code> event).
943: </ol>
944:
1.19 ihickson 945: <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
946: processing model on a worker at any time, e.g. in response to user
947: requests, in response to CPU quota management, or when a worker stops
948: being a <a href="#needed">needed worker</a> if the worker continues
1.26 ihickson 949: executing even after its <code title=dom-WorkerGlobalScope-closing><a
1.19 ihickson 950: href="#closing">closing</a></code> attribute was set to true.
951:
1.8 ihickson 952: <hr>
953:
954: <p>When a script invokes the <dfn id=close
1.26 ihickson 955: title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
956: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
957: the user agent must run the following steps:
1.8 ihickson 958:
959: <ol>
960: <li>
961: <p>Create an <code>Event</code> object with the event name <code
962: title=event-unload>unload</code>, which does not bubble and is not
963: cancelable, and add it to the <code><a
1.26 ihickson 964: href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1.8 ihickson 965: href="#queue">queue of events</a>, targetted at the <code><a
1.26 ihickson 966: href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1.8 ihickson 967:
968: <li>
1.26 ihickson 969: <p>Set the worker's <code><a
970: href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
971: title=dom-WorkerGlobalScope-closing><a
1.8 ihickson 972: href="#closing">closing</a></code> attribute to true.
973:
974: <li>
975: <p>For each <code>MessagePort</code> object that is entangled with
1.26 ihickson 976: another port and that has one (but only one) port whose owner is the
977: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
978: on which the method was invoked, run the following substeps:</p>
1.8 ihickson 979:
980: <ol>
981: <li>
982: <p>Unentangle the two ports.
983:
984: <li>
985: <p>At the next available opportunity, after any scripts have finished
986: executing<!-- XXX queue -->, <span>fire a simple event</span> called
987: <code title=event-unload>unload</code> at the other port (the one
1.26 ihickson 988: whose owner is not the <code><a
989: href="#workerglobalscope">WorkerGlobalScope</a></code> object on which
990: the <code title=dom-WorkerGlobalScope-close><a
1.8 ihickson 991: href="#close">close()</a></code> method was called).
992: </ol>
993: </ol>
994:
1.15 ihickson 995: <h3 id=creating><span class=secno>2.6 </span>Creating workers</h3>
1.5 ihickson 996:
997: <pre
1.26 ihickson 998: class=idl>[NoInterfaceObject] interface <dfn id=workerfactory>WorkerFactory</dfn> {
999: <span>MessagePort</span> <a href="#createworker" title=dom-WorkerFactory-createWorker>createWorker</a>(in DOMString scriptURL);
1000: <span>MessagePort</span> <a href="#createnamedworker" title=dom-WorkerFactory-createNamedWorker>createNamedWorker</a>(in DOMString name, in DOMString scriptURL);
1.5 ihickson 1001: };</pre>
1002:
1.26 ihickson 1003: <p>Objects that implement the <code>Window</code> and <code><a
1004: href="#workerutils">WorkerUtils</a></code> interfaces must also implement
1005: the <code><a href="#workerfactory">WorkerFactory</a></code> interface.
1.5 ihickson 1006:
1.9 ihickson 1007: <hr>
1008:
1.11 ihickson 1009: <p>When the <dfn id=createworker
1.26 ihickson 1010: title=dom-WorkerFactory-createWorker><code>createWorker(<var
1.11 ihickson 1011: title="">scriptURL</var>)</code></dfn> method is invoked, the user agent
1012: must run the following steps:
1.9 ihickson 1013:
1014: <ol>
1015: <li>
1016: <p><span title="resolve a url">Resolve</span> the <var
1017: title="">scriptURL</var> argument.
1018:
1019: <li>
1020: <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1021:
1022: <li>
1023: <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1024: is not the <span title="same origin">same</span> as the origin of the
1025: script that invoked the method, then throw a <span>security
1026: exception</span>.
1027:
1028: <li>
1029: <p><a href="#create">Create a worker</a> from the resulting
1030: <span>absolute URL</span> whose name is the empty string.
1031:
1032: <li>
1033: <p>Return the <code>MessagePort</code> object returned from the <a
1034: href="#create">create a worker</a> algorithm.
1035: </ol>
1036:
1037: <hr>
1038:
1.11 ihickson 1039: <p>When the <dfn id=createnamedworker
1.26 ihickson 1040: title=dom-WorkerFactory-createNamedWorker><code>createNamedWorker(<var
1.11 ihickson 1041: title="">name</var>, <var title="">scriptURL</var>)</code></dfn> method is
1.9 ihickson 1042: invoked, the user agent must run the following steps:
1043:
1044: <ol>
1045: <li>
1046: <p><span title="resolve a url">Resolve</span> the <var
1047: title="">scriptURL</var> argument.
1.4 ihickson 1048:
1.9 ihickson 1049: <li>
1050: <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1051:
1052: <li>
1053: <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1054: is not the <span title="same origin">same</span> as the origin of the
1055: script that invoked the method, then throw a <span>security
1056: exception</span>.
1057:
1058: <li>
1059: <p>If the <var title="">name</var> argument is the empty string, <a
1060: href="#create">create a worker</a> from the resulting <span>absolute
1061: URL</span>, whose name is the empty string, and return the
1062: <code>MessagePort</code> object returned from the <a
1063: href="#create">create a worker</a> algorithm. Then, abort these steps.
1064:
1065: <li>
1.26 ihickson 1066: <p>If there exists a worker whose <code
1067: title=dom-WorkerGlobalScope-closing><a
1.9 ihickson 1068: href="#closing">closing</a></code> attribute is false, whose <code
1.26 ihickson 1069: title=dom-WorkerGlobalScope-name><a href="#name">name</a></code>
1070: attribute is exactly equal to the <var title="">name</var> argument, and
1.34 ! ihickson 1071: whose <code title=dom-WorkerGlobalScope-location><a
! 1072: href="#location">location</a></code> attribute represents an
! 1073: <span>absolute URL</span> that has the <span>same origin</span> as the
! 1074: resulting <span>absolute URL</span>, then run these substeps:</p>
1.9 ihickson 1075:
1076: <ol>
1077: <li>
1.34 ! ihickson 1078: <p>If that worker's <code title=dom-WorkerGlobalScope-location><a
! 1079: href="#location">location</a></code> attribute represents an
! 1080: <span>absolute URL</span> that is not exactly equal to the resulting
! 1081: <span>absolute URL</span>, then throw a <code>URL_MISMATCH_ERR</code>
! 1082: exception and abort these steps. <span class=big-issue>code 19</span>
1.9 ihickson 1083:
1084: <li>
1085: <p><span>Create a new <code>MessagePort</code> object</span> owned by
1086: the <span>script execution context</span> of the script that invoked
1087: the method.
1088:
1089: <li>
1090: <p>Return that port.
1091:
1092: <li>
1.20 ihickson 1093: <p>Asynchronously, <a href="#connect" title="connect to a
1094: worker">connect</a> to this preexisting worker, with the newly created
1.9 ihickson 1095: port.
1096: </ol>
1097:
1098: <p>Otherwise, <a href="#create">create a worker</a> from the resulting
1099: <span>absolute URL</span>, whose name is the value of the <var
1100: title="">name</var> argument, and return the <code>MessagePort</code>
1101: object returned from the <a href="#create">create a worker</a>
1102: algorithm.</p>
1103: </ol>
1104:
1105: <hr>
1106:
1107: <p>The steps to <dfn id=create>create a worker</dfn> from a
1108: <span>URL</span> <var title="">url</var> and whose name is <var
1109: title="">name</var>, in the context of a method call, are as follows:
1110:
1111: <ol>
1112: <li>
1113: <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1114: <span>script execution context</span> of the script that invoked the
1115: method.
1116:
1117: <li>
1118: <p>Return that port.
1119:
1120: <li>
1121: <p>In a parallel execution context (i.e. a separate thread or process or
1122: equivalent construct), <a href="#run-a">run a worker</a> named <var
1123: title="">name</var> for the script with <span>URL</span> <var
1124: title="">url</var>, with the <span>script browsing context</span> of the
1125: script that invoked the method as the <var title="">owner browsing
1126: context</var> and with the <span>script document context</span> of the
1127: script that invoked the method as the <var title="">owner
1128: document</var>.</p>
1129:
1130: <p>If that algorithm invokes the steps for <dfn id=success title="worker
1.20 ihickson 1131: creation succeeded">success steps</dfn>, then <a href="#connect"
1132: title="connect to a worker">connect</a> to this new worker, with the
1.28 ihickson 1133: newly created port, and if that algorithm returns a second new port,
1.30 ihickson 1134: then add a new property <code title=dom-WorkerGlobalScope-port><a
1135: href="#port">port</a></code> on the new worker's <code><a
1136: href="#workerglobalscope">WorkerGlobalScope</a></code> object, whose
1137: value is the <code>MessagePort</code> object return by the <a
1.28 ihickson 1138: href="#connect">connect to a worker</a> algorithm.</p>
1.9 ihickson 1139:
1.34 ! ihickson 1140: <p>Otherwise, if the <dfn id=worker1>worker creation failed</dfn>, then
1.33 ihickson 1141: at the next available opportunity, after any scripts have finished
1.9 ihickson 1142: executing<!-- XXX queue -->, <span>fire a simple event</span> called
1143: <code title=event-error>error</code> at the newly created port.</p>
1144: </ol>
1145:
1146: <hr>
1147:
1.20 ihickson 1148: <p>The steps to <dfn id=connect>connect to a worker</dfn> given a
1.9 ihickson 1149: <code>MessagePort</code> object <var title="">port</var> are as follows:
1150:
1151: <ol>
1152: <li>
1153: <p>If <var title="">port</var> would have been garbage collected, or if
1.26 ihickson 1154: the <span>active document</span> of the owner of <var
1.9 ihickson 1155: title="">port</var> is no longer the same <code>Document</code> object
1156: as when <var title="">port</var> was created, then do nothing. Abort
1157: these steps. If the worker was just created, it'll get killed
1158: immediately.</p>
1159:
1160: <li>
1161: <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1.26 ihickson 1162: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> of the
1163: worker.
1.9 ihickson 1164:
1165: <li>
1166: <p><span>Entangle</span> this newly created port and the port <var
1167: title="">port</var> that was passed to these steps.
1168:
1169: <li>
1170: <p>At the next available opportunity, after any scripts have finished
1171: executing<!-- XXX queue -->, <span>fire a simple event</span> called
1172: <code title=event-load>load</code> at <var title="">port</var>.
1173:
1174: <li>
1175: <p>Create an event that uses the <code>MessageEvent</code> interface,
1.20 ihickson 1176: with the name <code title=event-connect>connect</code>, which does not
1.9 ihickson 1177: bubble, is cancelable, has no default action, has a <code
1178: title=dom-MessageEvent-data>data</code> attribute whose value is the
1179: empty string and has a <code
1180: title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
1181: value is the newly created port, and add it to the worker's <code><a
1.26 ihickson 1182: href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1.9 ihickson 1183: href="#queue">queue of events</a>, targetted at the <code><a
1.26 ihickson 1184: href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1.28 ihickson 1185:
1186: <li>
1187: <p>Return the newly created port.
1.9 ihickson 1188: </ol>
1.7 ihickson 1189:
1.12 ihickson 1190: <h2 id=apis-available><span class=secno>3. </span>APIs available to workers</h2>
1191:
1.26 ihickson 1192: <pre
1193: class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1.27 ihickson 1194: boolean <a href="#importscript" title=dom-WorkerGlobalScope-importScript>importScript</a>(in DOMString url);
1.26 ihickson 1195: readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
1196: <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
1197: void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
1198: void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
1199: };</pre>
1.18 ihickson 1200:
1.12 ihickson 1201: <p>Objects that implement the <code><a
1.26 ihickson 1202: href="#workerutils">WorkerUtils</a></code> interface must also implement
1.12 ihickson 1203: the following interfaces:
1204:
1205: <ul>
1.26 ihickson 1206: <li>The <code>WindowTimers</code> interface
1.12 ihickson 1207:
1.26 ihickson 1208: <li>The <code>EventTarget</code> interface
1.12 ihickson 1209:
1.26 ihickson 1210: <li>The <code><a href="#workerfactory">WorkerFactory</a></code> interface,
1211: as noted earlier in this specification
1.12 ihickson 1212: </ul>
1213:
1.23 ihickson 1214: <p class=big-issue>Need to define a sync database API.
1215:
1216: <p class=big-issue>May need to define a browser sniffing API (like
1217: window.navigator).</p>
1.18 ihickson 1218: <!-- XXX ApplicationCache -->
1219: <!-- XXX a way to set cookies on the URL for the script -->
1.24 ihickson 1220: <!-- XXX debugging: void log(in DOMString s); // log to console -->
1221: <!-- XXX debugging: onerror -->
1.13 ihickson 1222:
1.18 ihickson 1223: <hr>
1.13 ihickson 1224:
1.18 ihickson 1225: <p>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects,
1226: etc) are not available to workers in this version of this specification.
1.13 ihickson 1227:
1.26 ihickson 1228: <h3 id=importing><span class=secno>3.1 </span>Importing scripts and
1229: libraries</h3>
1230:
1.27 ihickson 1231: <p>When a script invokes the <dfn id=importscript
1232: title=dom-WorkerGlobalScope-importScript><code>importScript(<var
1.26 ihickson 1233: title="">url</var>)</code></dfn> method on a <code><a
1234: href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
1235: agent must run the following steps:
1236:
1237: <ol>
1238: <li>
1239: <p><span title="resolve a url">Resolve</span> the <var title="">url</var>
1240: argument.
1241:
1242: <li>
1243: <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1244:
1245: <li>
1246: <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1247: is not the <span title="same origin">same</span> as the origin of the
1248: script that invoked the method, then throw a <span>security
1249: exception</span>.
1250:
1251: <li>
1252: <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
1253: the resulting <span>absolute URL</span>.</p>
1254:
1255: <p>If the attempt fails, return false and abort these steps.</p>
1256:
1257: <p>If the attempt succeeds, then let <var title="">script</var> be the
1258: resource that was obtained.</p>
1259:
1260: <p class=note>As with the initial script, the script here is always
1261: assumed to be JavaScript, regardless of the MIME type.</p>
1262: <!-- XXX -->
1263:
1264: <li>
1265: <p>Let <var title="">script</var>'s <span>script execution
1266: context</span>, <span>script browsing context</span>, and <span>script
1267: document context</span> be the same as for the script that was executed
1268: by the <a href="#run-a">run a worker</a> processing model for this
1269: worker.</p>
1270:
1271: <li>
1272: <p>Run <var title="">script</var> until it either returns, fails to catch
1273: an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
1274: a worker</a>" algorithm above.</p>
1275:
1276: <p>If an exception was raised or if the script was prematurely aborted,
1277: then abort these steps, letting the exception or aborting continue to be
1278: processed by the script that called the <code
1.27 ihickson 1279: title=dom-WorkerGlobalScope-importScript><a
1280: href="#importscript">importScript()</a></code> method.</p>
1.26 ihickson 1281:
1282: <p>Otherwise, return true.</p>
1283: </ol>
1284:
1285: <h3 id=apis-defined><span class=secno>3.2 </span>APIs defined in other
1286: specifications</h3>
1287:
1.27 ihickson 1288: <p>The <dfn id=localstorage
1.26 ihickson 1289: title=dom-localStorage><code>localStorage</code></dfn>, <dfn
1290: id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
1291: must act as defined for the APIs with the same names on the
1292: <code>Window</code> object in the HTML5 specification, with the exception
1293: that where the API would use the <span>origin</span> of the <span>active
1294: document</span> of the <span>browsing context</span> of the
1295: <code>Window</code> object on which the method was supposedly invoked, it
1296: must instead use the <span>origin</span> of the script that invoked the
1297: method. <a href="#refsHTML5">[HTML5]</a>
1298:
1299: <p>The <dfn id=shownotification
1300: title=dom-showNotification><code>showNotification()</code></dfn> methods
1301: must act as defined for the APIs with the same names on the
1302: <code>Window</code> object in the HTML5 specification. <a
1303: href="#refsHTML5">[HTML5]</a>
1304:
1305: <h3 id=interface><span class=secno>3.3 </span>Interface objects and
1306: constructors</h3>
1307:
1308: <p>There must be no interface objects and constructors available in the
1309: global scope of scripts whose <span>script execution context</span> is a
1310: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1311: except for the following:
1312:
1313: <ul>
1314: <li>
1315: <p><code>XMLHttpRequest</code> and all interface objects and constructors
1316: defined by the XMLHttpRequest specifications, except that the
1317: <span>document response entity body</span> must always be null. <a
1318: href="#refsXHR">[XHR]</a>
1319:
1320: <li>
1321: <p>The <code>WebSocket</code> interface object and constructor.
1322:
1323: <li>
1324: <p>The <code>MessageChannel</code> interface object and constructor.
1325: </ul>
1326:
1.34 ! ihickson 1327: <h3 id=worker0><span class=secno>3.4 </span>Worker locations</h3>
! 1328:
! 1329: <pre
! 1330: class=idl>[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
! 1331: readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
! 1332: readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
! 1333: readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
! 1334: readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
! 1335: readonly attribute DOMString <a href="#port0" title=dom-WorkerLocation-port>port</a>;
! 1336: readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
! 1337: readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
! 1338: readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
! 1339: };</pre>
! 1340:
! 1341: <p>A <code><a href="#workerlocation">WorkerLocation</a></code> object
! 1342: represents an <span>absolute URL</span> set at its creation.
! 1343:
! 1344: <p>The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
! 1345: attribute must return the <span>absolute URL</span> that the object
! 1346: represents.
! 1347:
! 1348: <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface
! 1349: also has the complement of <span>URL decomposition attributes</span>, <dfn
! 1350: id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
! 1351: <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
! 1352: id=port0 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
! 1353: id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
! 1354: <dfn id=pathname
! 1355: title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
! 1356: id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
! 1357: <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
! 1358: must follow the rules given for URL decomposition attributes, with the
! 1359: <span title=concept-uda-input>input</span> being the <span>absolute
! 1360: URL</span> that the object represents (same as the <code
! 1361: title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
! 1362: and the <span title=concept-uda-setter>common setter action</span> being a
! 1363: no-op, since the attributes are defined to be readonly. <a
! 1364: href="#refsHTML5">[HTML5]</a>
! 1365:
1.1 ihickson 1366: <h2 class=no-num id=references>References</h2>
1367:
1368: <p class=big-issue>This section will be written in a future
1369: draft.<!--XXX-->
1370:
1371: <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
1372: <!-- ACKS -->
1373:
1.26 ihickson 1374: <p>Thanks to Aaron Boodman, Jonas Sicking, Maciej Stachowiak, Mike Smith,
1375: and Philip Taylor for their useful and substantial comments.
1.4 ihickson 1376:
1377: <p>Huge thanks to the whole Gears team, who pioneered this technology and
1378: whose experience has been a huge influence on this specification.
Webmaster