/[suikacvs]/markup/html/html5/spec-ja/.workers-spec.en.html
Suika

Contents of /markup/html/html5/spec-ja/.workers-spec.en.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.192 - (hide annotations) (download) (as text)
Mon Feb 15 06:25:24 2010 UTC (16 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.191: +5 -5 lines
File MIME type: text/html
make

1 wakaba 1.190 <!DOCTYPE html><html lang=en-US-x-hixie><meta charset=ascii><title>Web Workers</title><link href=/style/specification rel=stylesheet><link href=/images/icon rel=icon><body class=cfc>
2    
3     <header class=head><p><a class=logo href=http://www.whatwg.org/ rel=home><img alt=WHATWG src=/images/logo></a></p>
4     <hgroup><h1>Web Workers</h1>
5 wakaba 1.192 <h2 class="no-num no-toc">Draft Recommendation &mdash; 14 February 2010</h2>
6 wakaba 1.190 </hgroup><p>You can take part in this work. <a href=http://www.whatwg.org/mailing-list>Join the working group's discussion list.</a></p>
7     <p><strong>Web designers!</strong> We have a <a href=http://blog.whatwg.org/faq/>FAQ</a>, a <a href=http://forums.whatwg.org/>forum</a>, and a <a href=http://www.whatwg.org/mailing-list#help>help mailing list</a> for you!</p>
8     <dl><dt>This version:</dt>
9     <dd><a href=http://www.whatwg.org/specs/web-workers/current-work/>http://whatwg.org/ww</a></dd>
10     <dt>Version history:</dt>
11     <dd>Twitter messages (non-editorial changes only): <a href=http://twitter.com/WHATWG>http://twitter.com/WHATWG</a></dd>
12     <dd>Commit-Watchers mailing list: <a href=http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org>http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a></dd>
13     <dd>Interactive Web interface: <a href=http://html5.org/tools/web-workers-tracker>http://html5.org/tools/web-workers-tracker</a></dd>
14     <dd>Subversion interface: <a href=http://svn.whatwg.org/webworkers/>http://svn.whatwg.org/webworkers/</a></dd>
15     <dt>Issues:</dt>
16     <dd>To send feedback: <a href=http://www.whatwg.org/mailing-list>whatwg@whatwg.org</a></dd>
17     <dd>To view and vote on feedback: <a href=http://www.whatwg.org/issues/>http://www.whatwg.org/issues/</a></dd>
18     <dt>Editor:</dt>
19     <dd>Ian Hickson, Google, ian@hixie.ch</dd>
20     </dl><p class=copyright>&copy; Copyright 2004-2008 Apple Computer, Inc.,
21     Mozilla Foundation, and Opera Software ASA.</p>
22     <p class=copyright>You are granted a license to use, reproduce
23     and create derivative works of this document.</p>
24     </header><hr><h2 class="no-num no-toc" id=abstract>Abstract</h2>
25    
26     <p>This specification defines an API that allows Web application
27     authors to spawn background workers running scripts in parallel to
28     their main page. This allows for thread-like operation with
29     message-passing as the coordination mechanism.</p>
30    
31    
32     <h2 class="no-num no-toc" id=status>Status of this document</h2>
33    
34     <p><strong>This is a work in progress!</strong> This document is
35     changing on a daily if not hourly basis in response to comments and
36     as a general part of its development process. Comments are very
37     welcome, please send them to <a href=mailto:whatwg@whatwg.org>whatwg@whatwg.org</a>. Thank
38     you.</p>
39    
40     <p>The current focus is in developing a first draft proposal.</p>
41    
42     <p>Implementors should be aware that this specification is not
43     stable. <strong>Implementors who are not taking part in the
44     discussions are likely to find the specification changing out from
45     under them in incompatible ways.</strong> Vendors interested in
46     implementing this specification before it eventually reaches the
47     call for implementations should join the <a href=/mailing-list>WHATWG mailing list</a> and take part in the
48     discussions.</p>
49    
50     <p>This specification is also being produced by the <a href=http://www.w3.org/2008/webapps/>W3C Web Apps WG</a>. The two
51     specifications are identical from the table of contents onwards.</p>
52    
53    
54    
55     <h2 class="no-num no-toc" id=contents>Table of contents</h2>
56    
57     <!--begin-toc-->
58     <ol class=toc>
59     <li><a href=#introduction><span class=secno>1 </span>Introduction</a>
60     <ol>
61     <li><a href=#scope><span class=secno>1.1 </span>Scope</a></li>
62     <li><a href=#tutorial><span class=secno>1.2 </span>Tutorial</a>
63     <ol>
64     <li><a href=#a-background-number-crunching-worker><span class=secno>1.2.1 </span>A background number-crunching worker</a></li>
65     <li><a href=#a-worker-for-updating-a-client-side-database><span class=secno>1.2.2 </span>A worker for updating a client-side database</a></li>
66     <li><a href=#worker-used-for-background-i/o><span class=secno>1.2.3 </span>Worker used for background I/O</a></li>
67     <li><a href=#shared-workers><span class=secno>1.2.4 </span>Shared workers</a></li>
68     <li><a href=#delegation><span class=secno>1.2.5 </span>Delegation</a></li>
69     <li><a href=#providing-libraries><span class=secno>1.2.6 </span>Providing libraries</a></ol></ol></li>
70     <li><a href=#conformance-requirements><span class=secno>2 </span>Conformance requirements</a>
71     <ol>
72     <li><a href=#dependencies><span class=secno>2.1 </span>Dependencies</a></ol></li>
73     <li><a href=#terminology><span class=secno>3 </span>Terminology</a></li>
74     <li><a href=#infrastructure><span class=secno>4 </span>Infrastructure</a>
75     <ol>
76     <li><a href=#the-global-scope><span class=secno>4.1 </span>The global scope</a>
77     <ol>
78     <li><a href=#the-workerglobalscope-abstract-interface><span class=secno>4.1.1 </span>The <code>WorkerGlobalScope</code> abstract interface</a></li>
79     <li><a href=#dedicated-workers-and-the-dedicatedworkerglobalscope-interface><span class=secno>4.1.2 </span>Dedicated workers and the <code>DedicatedWorkerGlobalScope</code> interface</a></li>
80     <li><a href=#shared-workers-and-the-sharedworkerglobalscope-inteface><span class=secno>4.1.3 </span>Shared workers and the <code>SharedWorkerGlobalScope</code> inteface</a></ol></li>
81     <li><a href=#origins-of-workers><span class=secno>4.2 </span>Origins of workers</a></li>
82     <li><a href=#the-event-loop><span class=secno>4.3 </span>The event loop</a></li>
83     <li><a href="#the-worker's-lifetime"><span class=secno>4.4 </span>The worker's lifetime</a></li>
84     <li><a href=#processing-model><span class=secno>4.5 </span>Processing model</a></li>
85     <li><a href=#runtime-script-errors><span class=secno>4.6 </span>Runtime script errors</a></li>
86     <li><a href=#creating-workers><span class=secno>4.7 </span>Creating workers</a>
87     <ol>
88     <li><a href=#the-abstractworker-abstract-interface><span class=secno>4.7.1 </span>The <code>AbstractWorker</code> abstract interface</a></li>
89     <li><a href=#dedicated-workers-and-the-worker-interface><span class=secno>4.7.2 </span>Dedicated workers and the <code>Worker</code> interface</a></li>
90     <li><a href=#shared-workers-and-the-sharedworker-interface><span class=secno>4.7.3 </span>Shared workers and the <code>SharedWorker</code> interface</a></ol></ol></li>
91     <li><a href=#apis-available-to-workers><span class=secno>5 </span>APIs available to workers</a>
92     <ol>
93     <li><a href=#importing-scripts-and-libraries><span class=secno>5.1 </span>Importing scripts and libraries</a></li>
94     <li><a href=#the-workernavigator-object><span class=secno>5.2 </span>The <code>WorkerNavigator</code> object</a></li>
95     <li><a href=#apis-defined-in-other-specifications><span class=secno>5.3 </span>APIs defined in other specifications</a></li>
96     <li><a href=#interface-objects-and-constructors><span class=secno>5.4 </span>Interface objects and constructors</a></li>
97     <li><a href=#worker-locations><span class=secno>5.5 </span>Worker locations</a></ol></li>
98     <li><a class=no-num href=#references>References</a></li>
99     <li><a class=no-num href=#acknowledgements>Acknowledgements</a></ol>
100     <!--end-toc-->
101     <hr><h2 id=introduction><span class=secno>1 </span>Introduction</h2>
102    
103     <h3 id=scope><span class=secno>1.1 </span>Scope</h3>
104    
105     <p><i>This section is non-normative.</i></p>
106    
107     <p>This specification defines an API for running scripts in the
108     background independently of any user interface scripts.</p>
109    
110     <p>This allows for long-running scripts that are not interrupted by
111     scripts that respond to clicks or other user interactions, and
112     allows long tasks to be executed without yielding to keep the page
113     responsive.</p>
114    
115     <p>Workers (as these background scripts are called herein) are
116     relatively heavy-weight, and are not intended to be used in large
117     numbers. For example, it would be inappropriate to launch one worker
118     for each pixel of a four megapixel image. The examples below show
119     some appropriate uses of workers.</p>
120    
121     <p>Generally, workers are expected to be long-lived, have a high
122     start-up performance cost, and a high per-instance memory cost.</p>
123    
124    
125     <h3 id=tutorial><span class=secno>1.2 </span>Tutorial</h3>
126    
127     <p><i>This section is non-normative.</i></p>
128    
129     <p>There are a variety of uses that workers can be put to. The
130     following subsections show various examples of this use.</p>
131    
132     <h4 id=a-background-number-crunching-worker><span class=secno>1.2.1 </span>A background number-crunching worker</h4>
133    
134     <p><i>This section is non-normative.</i></p>
135    
136     <p>The simplest use of workers is for performing a computationally
137     expensive task without interrupting the user interface.</p>
138    
139     <p>In this example, the main document spawns a worker to
140     (na&iuml;vely) compute prime numbers, and progressively displays the
141     most recently found prime number.</p>
142    
143     <p>The main page is as follows:</p>
144    
145     <pre>&lt;!DOCTYPE HTML&gt;
146     &lt;html&gt;
147     &lt;head&gt;
148     &lt;title&gt;Worker example: One-core computation&lt;/title&gt;
149     &lt;/head&gt;
150     &lt;body&gt;
151     &lt;p&gt;The highest prime number discovered so far is: &lt;output id="result"&gt;&lt;/output&gt;&lt;/p&gt;
152     &lt;script&gt;
153     var worker = new Worker('worker.js');
154     worker.onmessage = function (event) {
155     document.getElementById('result').textContent = event.data;
156     };
157     &lt;/script&gt;
158     &lt;/body&gt;
159     &lt;/html&gt;</pre>
160    
161     <p>The <code title=dom-Worker><a href=#dom-worker>Worker()</a></code> constructor call
162     creates a worker and returns a <code><a href=#worker>Worker</a></code> object
163     representing that worker, which is used to communicate with the
164     worker. That object's <code title=handler-Worker-onmessage><a href=#handler-worker-onmessage>onmessage</a></code> event handler allows the code to receive messages from the worker.</p>
165    
166     <p>The worker itself is as follows:</p>
167    
168     <pre>var n = 1;
169     search: while (true) {
170     n += 1;
171     for (var i = 2; i &lt;= Math.sqrt(n); i += 1)
172     if (n % i == 0)
173     continue search;
174     // found a prime!
175     postMessage(n);
176     }</pre>
177    
178     <p>The bulk of this code is simply an unoptimized search for a prime
179     number. To send a message back to the page, the <code title=dom-DedicatedWorkerGlobalScope-postMessage><a href=#dom-dedicatedworkerglobalscope-postmessage>postMessage()</a></code>
180     method is used to post a message when a prime is found.</p>
181    
182     <p><a href=http://www.whatwg.org/demos/workers/primes/page.html>View this example online</a>.</p>
183    
184    
185    
186     <h4 id=a-worker-for-updating-a-client-side-database><span class=secno>1.2.2 </span>A worker for updating a client-side database</h4>
187    
188     <p><i>This section is non-normative.</i></p>
189    
190     <p>In this example, the main document spawns a worker whose only
191     task is to listen for notifications from the server, and, when
192     appropriate, either add or remove data from the client-side
193     database.</p>
194    
195     <p>Since no communication occurs between the worker and the main
196     page, the main page can start the worker by just doing:</p>
197    
198     <pre>&lt;script&gt;
199     new Worker('worker.js');
200     &lt;/script&gt;</pre>
201    
202     <p>The worker itself is as follows:</p>
203    
204     <pre>var server = new WebSocket('ws://whatwg.org/database');
205     var database = openDatabase('demobase', '1.0', 'Demo Database', 10240);
206     server.onmessage = function (event) {
207     // data is in the format "command key value"
208     var data = event.data.split(' ');
209     switch (data[0]) {
210     case '+':
211     database.transaction(function(tx) {
212     tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
213     });
214     case '-':
215     database.transaction(function(tx) {
216     tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
217     });
218     }
219     };</pre>
220    
221     <p>This connects to the server using the <code>WebSocket</code>
222     mechanism and opens the local database (which, we presume, has been
223     created earlier). The worker then just listens for messages from the
224     server and acts on them as appropriate, forever (or until the main
225     page is closed).</p>
226    
227     <p><a href=http://www.whatwg.org/demos/workers/database-updater/page.html>View
228     this example online</a>. (This example will not actually function,
229     since the server does not actually exist and the database is not
230     created by this sample code.)</p>
231    
232    
233    
234     <h4 id=worker-used-for-background-i/o><span class=secno>1.2.3 </span>Worker used for background I/O</h4>
235    
236     <p><i>This section is non-normative.</i></p>
237    
238     <p>In this example, the main document uses two workers, one for
239     fetching stock updates for at regular intervals, and one for
240     fetching performing search queries that the user requests.</p>
241    
242     <p>The main page is as follows:</p>
243    
244     <pre>&lt;!DOCTYPE HTML&gt;
245     &lt;html&gt;
246     &lt;head&gt;
247     &lt;title&gt;Worker example: Stock ticker&lt;/title&gt;
248     &lt;script&gt;
249     // TICKER
250     var symbol = 'GOOG'; // default symbol to watch
251     var ticker = new Worker('ticker.js');
252    
253     // SEARCHER
254     var searcher = new Worker('searcher.js');
255     function search(query) {
256     searcher.postMessage(query);
257     }
258    
259     // SYMBOL SELECTION UI
260     function select(newSymbol) {
261     symbol = newSymbol;
262     ticker.postMessage(symbol);
263     }
264     &lt;/script&gt;
265     &lt;/head&gt;
266     &lt;body onload="search('')"&gt;
267     &lt;p&gt;&lt;output id="symbol"&gt;&lt;/output&gt; &lt;output id="value"&gt;&lt;/output&gt;&lt;/p&gt;
268     &lt;script&gt;
269     ticker.onmessage = function (event) {
270     var data = event.data.split(' ');
271     document.getElementById('symbol').textContent = data[0];
272     document.getElementById('value').textContent = data[1];
273     };
274     ticker.postMessage(symbol);
275     &lt;/script&gt;
276     &lt;p&gt;&lt;label&gt;Search: &lt;input type="text" autofocus oninput="search(this.value)"&gt;&lt;/label&gt;&lt;/p&gt;
277     &lt;ul id="results"&gt;&lt;/ul&gt;
278     &lt;script&gt;
279     searcher.onmessage = function (event) {
280     var data = event.data.split(' ');
281     var results = document.getElementById('results');
282     while (results.hasChildNodes()) // clear previous results
283     results.removeChild(results.firstChild);
284     for (var i = 0; i &lt; data.length; i += 1) {
285     // add a list item with a button for each result
286     var li = document.createElement('li');
287     var button = document.createElement('button');
288     button.value = data[i];
289     button.type = 'button';
290     button.onclick = function () { select(this.value); };
291     button.textContent = data[i];
292     li.appendChild(button);
293     results.appendChild(li);
294     }
295     };
296     &lt;/script&gt;
297     &lt;p&gt;(The data in this example is not real. Try searching for "Google" or "Apple".)&lt;/p&gt;
298     &lt;/body&gt;
299     &lt;/html&gt;</pre>
300    
301     <p>The two workers use a common library for performing the actual
302     network calls. This library is as follows:</p>
303    
304     <pre>function get(url) {
305     try {
306     var xhr = new XMLHttpRequest();
307     xhr.open('GET', url, false);
308     xhr.send();
309     return xhr.responseText;
310     } catch (e) {
311     return ''; // turn all errors into empty results
312     }
313     }</pre>
314    
315     <p>The stock updater worker is as follows:</p>
316    
317     <pre>importScripts('io.js');
318     var timer;
319     var symbol;
320     function update() {
321     postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
322     timer = setTimeout(update, 10000);
323     }
324     onmessage = function (event) {
325     if (timer)
326     clearTimeout(timer);
327     symbol = event.data;
328     update();
329     };</pre>
330    
331     <p>The search query worker is as follows:</p>
332    
333     <pre>importScripts('io.js');
334     onmessage = function (event) {
335     postMessage(get('search.cgi?' + event.data));
336     };</pre>
337    
338     <p><a href=http://www.whatwg.org/demos/workers/stocks/page.html>View this example online</a>.</p>
339    
340    
341     <h4 id=shared-workers><span class=secno>1.2.4 </span>Shared workers</h4>
342    
343     <p><i>This section is non-normative.</i></p>
344    
345     <p>In this example, multiple windows (viewers) can be opened that
346     are all viewing the same map. All the windows share the same map
347     information, with a single worker coordinating all the viewers. Each
348     viewer can move around independently, but if they set any data on
349     the map, all the viewers are updated.</p>
350    
351     <p>The main page isn't interesting, it merely provides a way to open
352     the viewers:</p>
353    
354     <pre>&lt;!DOCTYPE HTML&gt;
355     &lt;html&gt;
356     &lt;head&gt;
357     &lt;title&gt;Workers example: Multiviewer&lt;/title&gt;
358     &lt;script&gt;
359     function openViewer() {
360     window.open('viewer.html');
361     }
362     &lt;/script&gt;
363     &lt;/head&gt;
364     &lt;body&gt;
365     &lt;p&gt;&lt;button type=button onclick="openViewer()"&gt;Open a new
366     viewer&lt;/button&gt;&lt;/p&gt;
367     &lt;p&gt;Each viewer opens in a new window. You can have as many viewers
368     as you like, they all view the same data.&lt;/p&gt;
369     &lt;/body&gt;
370     &lt;/html&gt;</pre>
371    
372     <p>The viewer is more involved:</p>
373    
374     <pre>&lt;!DOCTYPE HTML&gt;
375     &lt;html&gt;
376     &lt;head&gt;
377     &lt;title&gt;Workers example: Multiviewer viewer&lt;/title&gt;
378     &lt;script&gt;
379     var worker = new SharedWorker('worker.js', 'core');
380    
381     // CONFIGURATION
382     function configure(event) {
383     if (event.data.substr(0, 4) != 'cfg ') return;
384     var name = event.data.substr(4).split(' ', 1);
385     // update display to mention our name is name
386     document.getElementsByTagName('h1')[0].textContent += ' ' + name;
387     // no longer need this listener
388     worker.port.removeEventListener('message', configure, false);
389     }
390     worker.port.addEventListener('message', configure, false);
391    
392     // MAP
393     function paintMap(event) {
394     if (event.data.substr(0, 4) != 'map ') return;
395     var data = event.data.substr(4).split(',');
396     // display tiles data[0] .. data[8]
397     var canvas = document.getElementById('map');
398     var context = canvas.getContext('2d');
399     for (var y = 0; y &lt; 3; y += 1) {
400     for (var x = 0; x &lt; 3; x += 1) {
401     var tile = data[y * 3 + x];
402     if (tile == '0')
403     context.fillStyle = 'green';
404     else
405     context.fillStyle = 'maroon';
406     fillRect(x * 50, y * 50, 50, 50);
407     }
408     }
409     }
410     worker.port.addEventListener('message', paintMap, false);
411    
412     // PUBLIC CHAT
413     function updatePublicChat(event) {
414     if (event.data.substr(0, 4) != 'txt ') return;
415     var name = event.data.substr(4).split(' ', 1);
416     var message = event.data.substr(4 + length(name) + 1);
417     // display "&lt;name&gt; message" in public chat
418     var dialog = document.getElementById('public');
419     var dt = document.createElement('dt');
420     dt.textContent = name;
421     dialog.appendChild(dt);
422     var dd = document.createElement('dd');
423     dd.textContent = message;
424     dialog.appendChild(dd);
425     }
426     worker.port.addEventListener('message', updatePublicChat, false);
427    
428     // PRIVATE CHAT
429     function startPrivateChat(event) {
430     if (event.data.substr(0, 4) != 'msg ') return;
431     var name = event.data.substr(4).split(' ', 1);
432     var port = event.ports[0];
433     // display a private chat UI
434     var ul = document.getElementById('private');
435     var li = document.createElement('li');
436     var h3 = document.createElement('h3');
437     h3.textContent = 'Private chat with ' + name;
438     li.appendChild(h3);
439     var dialog = document.createElement('dialog');
440     var addMessage = function(name, message) {
441     var dt = document.createElement('dt');
442     dt.textContent = name;
443     dialog.appendChild(dt);
444     var dd = document.createElement('dd');
445     dd.textContent = message;
446     dialog.appendChild(dd);
447     };
448     port.onmessage = function (event) {
449     addMessage(name, event.data);
450     };
451     li.appendChild(dialog);
452     var form = document.createElement('form');
453     var p = document.createElement('p');
454     var input = document.createElement('input');
455     input.size = 50;
456     p.appendChild(input);
457     p.appendChild(document.createTextNode(' '));
458     var button = document.createElement('button');
459     button.textContent = 'Post';
460     p.appendChild(button);
461     form.onsubmit = function () {
462     port.postMessage(input.value);
463     addMessage('me', input.value);
464     input.value = '';
465     return false;
466     };
467     form.appendChild(p);
468     li.appendChild(form);
469     }
470     worker.port.addEventListener('message', startPrivateChat, false);
471    
472     worker.port.start();
473     &lt;/script&gt;
474     &lt;/head&gt;
475     &lt;body&gt;
476     &lt;h1&gt;Viewer&lt;/h1&gt;
477     &lt;h2&gt;Map&lt;/h2&gt;
478     &lt;p&gt;&lt;canvas id="map" height=150 width=150&gt;&lt;/canvas&gt;&lt;/p&gt;
479     &lt;p&gt;
480     &lt;button type=button onclick="worker.port.postMessage('mov left')"&gt;Left&lt;/button&gt;
481     &lt;button type=button onclick="worker.port.postMessage('mov up')"&gt;Up&lt;/button&gt;
482     &lt;button type=button onclick="worker.port.postMessage('mov down')"&gt;Down&lt;/button&gt;
483     &lt;button type=button onclick="worker.port.postMessage('mov right')"&gt;Right&lt;/button&gt;
484     &lt;button type=button onclick="worker.port.postMessage('set 0')"&gt;Set 0&lt;/button&gt;
485     &lt;button type=button onclick="worker.port.postMessage('set 1')"&gt;Set 1&lt;/button&gt;
486     &lt;/p&gt;
487     &lt;h2&gt;Public Chat&lt;/h2&gt;
488     &lt;dialog id="public"&gt;&lt;/dialog&gt;
489     &lt;form onsubmit="worker.port.postMessage('txt ' + message.value); message.value = ''; return false;"&gt;
490     &lt;p&gt;
491     &lt;input type="text" name="message" size="50"&gt;
492     &lt;button&gt;Post&lt;/button&gt;
493     &lt;/p&gt;
494     &lt;/form&gt;
495     &lt;h2&gt;Private Chat&lt;/h2&gt;
496     &lt;ul id="private"&gt;&lt;/ul&gt;
497     &lt;/body&gt;
498     &lt;/html&gt;
499     </pre>
500    
501     <p>There are several key things worth noting about the way the
502     viewer is written.</p>
503    
504     <p><strong>Multiple listeners</strong>. Instead of a single message
505     processing function, the code here attaches multiple event
506     listeners, each one performing a quick check to see if it is
507     relevant for the message. In this example it doesn't make much
508     difference, but if multiple authors wanted to collaborate using a
509     single port to communicate with a worker, it would allow for
510     independent code instead of changes having to all be made to a
511     single event handling function.</p>
512    
513     <p>Registering event listeners in this way also allows you to
514     unregister specific listeners when you are done with them, as is
515     done with the <code title="">configure()</code> method in this
516     example.</p>
517    
518     <p>Finally, the worker:</p>
519    
520     <pre>
521     var nextName = 0;
522     function getNextName() {
523     // this could use more friendly names
524     // but for now just return a number
525     return nextName++;
526     }
527    
528     var map = [
529     [0, 0, 0, 0, 0, 0, 0],
530     [1, 1, 0, 1, 0, 1, 1],
531     [0, 1, 0, 1, 0, 0, 0],
532     [0, 1, 0, 1, 0, 1, 1],
533     [0, 0, 0, 1, 0, 0, 0],
534     [1, 0, 0, 1, 1, 1, 1],
535     [1, 1, 0, 1, 1, 0, 1],
536     ];
537    
538     function wrapX(x) {
539     if (x &lt; 0) return wrapX(x + map[0].length);
540     if (x &gt;= map[0].length) return wrapX(x - map[0].length);
541     return x;
542     }
543    
544     function wrapY(y) {
545     if (y &lt; 0) return wrapY(y + map.length);
546     if (y &gt;= map[0].length) return wrapY(y - map.length);
547     return y;
548     }
549    
550     function sendMapData(callback) {
551     var data = '';
552     for (var y = viewer.y-1; y &lt;= viewer.y+1; y += 1) {
553     for (var x = viewer.x-1; x &lt;= viewer.x+1; x += 1) {
554     if (data != '')
555     data += ',';
556     data += map[y][x];
557     }
558     }
559     callback('map ' + data);
560     }
561    
562     var viewers = {};
563     onconnect = function (event) {
564     event.ports[0]._name = getNextName();
565     event.ports[0]._data = { port: event.port, x: 0, y: 0, };
566     viewers[event.ports[0]._name] = event.port._data;
567     event.ports[0].postMessage('cfg ' + name);
568     event.ports[0].onmessage = getMessage;
569     sendMapData(event.ports[0].postMessage);
570     };
571    
572     function getMessage(event) {
573     switch (event.data.substr(0, 4)) {
574     case 'mov ':
575     var direction = event.data.substr(4);
576     var dx = 0;
577     var dy = 0;
578     switch (direction) {
579     case 'up': dy = -1; break;
580     case 'down': dy = 1; break;
581     case 'left': dx = -1; break;
582     case 'right': dx = 1; break;
583     }
584     event.target._data.x = wrapX(event.target._data.x + dx);
585     event.target._data.y = wrapY(event.target._data.y + dy);
586     sendMapData(event.target.postMessage);
587     break;
588     case 'set ':
589     var value = event.data.substr(4);
590     map[event.target._data.y][event.target._data.x] = value;
591     for (var viewer in viewers)
592     sendMapData(viewers[viewer].port.postMessage);
593     break;
594     case 'txt ':
595     var name = event.target._name;
596     var message = event.data.substr(4);
597     for (var viewer in viewers)
598     viewers[viewer].port.postMessage('txt ' + name + ' ' + message);
599     break;
600     case 'msg ':
601     var party1 = event._data;
602     var party2 = viewers[event.data.substr(4).split(' ', 1)];
603     if (party2) {
604     var channel = new MessageChannel();
605     party1.port.postMessage('msg ' + party2.name, [channel.port1]);
606     party2.port.postMessage('msg ' + party1.name, [channel.port2]);
607     }
608     break;
609     }
610     }</pre>
611    
612     <p><strong>Connecting to multiple pages</strong>. The script uses
613     the <code title=handler-SharedWorkerGlobalScope-onconnect><a href=#handler-sharedworkerglobalscope-onconnect>onconnect</a></code>
614     event listener to listen for multiple connections.</p>
615    
616     <p><strong>Direct channels</strong>. When the worker receives a
617     "msg" message from one viewer naming another viewer, it sets up a
618     direct connection between the two, so that the two viewers can
619     communicate directly without the worker having to proxy all the
620     messages.</p>
621    
622     <p><a href=http://www.whatwg.org/demos/workers/multiviewer/page.html>View this example online</a>.</p>
623    
624    
625     <h4 id=delegation><span class=secno>1.2.5 </span>Delegation</h4>
626    
627     <p><i>This section is non-normative.</i></p>
628    
629     <p>With multicore CPUs becoming prevalent, one way to obtain better
630     performance is to split computationally expensive tasks amongst
631     multiple workers. In this example, a computationally expensive task
632     that is to be performed for every number from 1 to 10,000,000 is
633     farmed out to ten subworkers.</p>
634    
635     <p>The main page is as follows, it just reports the result:</p>
636    
637     <pre>&lt;!DOCTYPE HTML&gt;
638     &lt;html&gt;
639     &lt;head&gt;
640     &lt;title&gt;Worker example: Multicore computation&lt;/title&gt;
641     &lt;/head&gt;
642     &lt;body&gt;
643     &lt;p&gt;Result: &lt;output id="result"&gt;&lt;/output&gt;&lt;/p&gt;
644     &lt;script&gt;
645     var worker = new Worker('worker.js');
646     worker.onmessage = function (event) {
647     document.getElementById('result').textContent = event.data;
648     };
649     &lt;/script&gt;
650     &lt;/body&gt;
651     &lt;/html&gt;</pre>
652    
653     <p>The worker itself is as follows:</p>
654    
655     <pre>// settings
656     var num_workers = 10;
657     var items_per_worker = 1000000;
658    
659     // start the workers
660     var result = 0;
661     var pending_workers = num_workers;
662     for (var i = 0; i &lt; num_workers; i += 1) {
663     var worker = new Worker('core.js');
664     worker.postMessage(i * items_per_worker);
665     worker.postMessage((i+1) * items_per_worker);
666     worker.onmessage = storeResult;
667     }
668    
669     // handle the results
670     function storeResult(event) {
671     result += 1*event.data;
672     pending_workers -= 1;
673     if (pending_workers &lt;= 0)
674     postMessage(result); // finished!
675     }</pre>
676    
677     <p>It consists of a loop to start the subworkers, and then a handler
678     that waits for all the subworkers to respond.</p>
679    
680     <p>The subworkers are implemented as follows:</p>
681    
682     <pre>var start;
683     onmessage = getStart;
684     function getStart(event) {
685     start = 1*event.data;
686     onmessage = getEnd;
687     }
688    
689     var end;
690     function getEnd(event) {
691     end = 1*event.data;
692     onmessage = null;
693     work();
694     }
695    
696     function work() {
697     var result = 0;
698     for (var i = start; i &lt; end; i += 1) {
699     // perform some complex calculation here
700     result += 1;
701     }
702     postMessage(result);
703     close();
704     }</pre>
705    
706     <p>They receive two numbers in two events, perform the computation
707     for the range of numbers thus specified, and then report the result
708     back to the parent.</p>
709    
710     <p><a href=http://www.whatwg.org/demos/workers/multicore/page.html>View this example online</a>.</p>
711    
712    
713     <h4 id=providing-libraries><span class=secno>1.2.6 </span>Providing libraries</h4>
714    
715     <p><i>This section is non-normative.</i></p>
716    
717     <p>Suppose that a cryptography library is made available that
718     provides three tasks:</p>
719    
720     <dl><dt>Generate a public/private key pair</dt>
721    
722     <dd>Takes a port, on which it will send two messages, first the
723     public key and then the private key.</dd>
724    
725     <dt>Given a plaintext and a public key, return the corresponding cyphertext</dt>
726    
727     <dd>Takes a port, to which any number of messages can be sent, the
728     first giving the public key, and the remainder giving the
729     plaintext, each of which is encrypted and then sent on that same
730     channel as the cyphertext. The user can close the port when it is
731     done encrypting content.</dd>
732    
733     <dt>Given a cyphertext and a private key, return the corresponding plaintext</dt>
734    
735     <dd>Takes a port, to which any number of messages can be sent, the
736     first giving the private key, and the remainder giving the
737     cyphertext, each of which is decrypted and then sent on that same
738     channel as the plaintext. The user can close the port when it is
739     done decrypting content.</dd>
740    
741     </dl><p>The library itself is as follows:</p>
742    
743     <pre>function handleMessage(e) {
744     if (e.data == "genkeys")
745     genkeys(e.ports[0]);
746     else if (e.data == "encrypt")
747     encrypt(e.ports[0]);
748     else if (e.data == "decrypt")
749     decrypt(e.ports[0]);
750     }
751    
752     function genkeys(p) {
753     var keys = _generateKeyPair();
754     p.postMessage(keys[0]);
755     p.postMessage(keys[1]);
756     }
757    
758     function encrypt(p) {
759     var key, state = 0;
760     p.onmessage = function (e) {
761     if (state == 0) {
762     key = e.data;
763     state = 1;
764     } else {
765     p.postMessage(_encrypt(key, e.data));
766     }
767     };
768     }
769    
770     function decrypt(p) {
771     var key, state = 0;
772     p.onmessage = function (e) {
773     if (state == 0) {
774     key = e.data;
775     state = 1;
776     } else {
777     p.postMessage(_decrypt(key, e.data));
778     }
779     };
780     }
781    
782     // support being used as a shared worker as well as a dedicated worker
783     if ('onmessage' in this) // dedicated worker
784     onmessage = handleMessage;
785     else // shared worker
786     onconnect = function (e) { e.port.onmessage = handleMessage; }
787    
788    
789     // the "crypto" functions:
790    
791     function _generateKeyPair() {
792     return [Math.random(), Math.random()];
793     }
794    
795     function _encrypt(k, s) {
796     return 'encrypted-' + k + ' ' + s;
797     }
798    
799     function _decrypt(k, s) {
800     return s.substr(s.indexOf(' ')+1);
801     }</pre>
802    
803     <p>Note that the crypto functions here are just stubs and don't do
804     real cryptography.</p>
805    
806     <p>This library could be used as follows:</p>
807    
808     <pre>&lt;!DOCTYPE HTML&gt;
809     &lt;html&gt;
810     &lt;head&gt;
811     &lt;title&gt;Worker example: Crypto library&lt;/title&gt;
812     &lt;script&gt;
813     var crytoLib = new Worker('libcrypto-v1.js'); // or could use 'libcrypto-v2.js'
814     function getKeys() {
815     var state = 0;
816     cryptoLib.startConversation("genkeys").onmessage = function (e) {
817     if (state == 0)
818     document.getElementById('public').value = e.data;
819     else if (state == 1)
820     document.getElementById('private').value = e.data;
821     state += 1;
822     };
823     }
824     function enc() {
825     var port = cryptoLib.startConversation("encrypt");
826     port.postMessage(document.getElementById('public').value);
827     port.postMessage(document.getElementById('input').value);
828     port.onmessage = function (e) {
829     document.getElementById('input').value = e.data;
830     port.close();
831     };
832     }
833     function dec() {
834     var port = cryptoLib.startConversation("decrypt");
835     port.postMessage(document.getElementById('private').value);
836     port.postMessage(document.getElementById('input').value);
837     port.onmessage = function (e) {
838     document.getElementById('input').value = e.data;
839     port.close();
840     };
841     }
842     &lt;/script&gt;
843     &lt;style&gt;
844     textarea { display: block; }
845     &lt;/style&gt;
846     &lt;/head&gt;
847     &lt;body onload="getKeys()"&gt;
848     &lt;fieldset&gt;
849     &lt;legend&gt;Keys&lt;/legend&gt;
850     &lt;p&gt;&lt;label&gt;Public Key: &lt;textarea id="public"&gt;&lt;/textarea&gt;&lt;/label&gt;&lt;/p&gt;
851     &lt;p&gt;&lt;label&gt;Private Key: &lt;textarea id="private"&gt;&lt;/textarea&gt;&lt;/label&gt;&lt;/p&gt;
852     &lt;/fieldset&gt;
853     &lt;p&gt;&lt;label&gt;Input: &lt;textarea id="input"&gt;&lt;/textarea&gt;&lt;/label&gt;&lt;/p&gt;
854     &lt;p&gt;&lt;button onclick="enc()"&gt;Encrypt&lt;/button&gt; &lt;button onclick="dec()"&gt;Decrypt&lt;/button&gt;&lt;/p&gt;
855     &lt;/body&gt;
856     &lt;/html&gt;</pre>
857    
858     <p>A later version of the API, though, might want to offload all the
859     crypto work onto subworkers. This could be done as follows:</p>
860    
861     <pre>function handleMessage(e) {
862     if (e.data == "genkeys")
863     genkeys(e.ports[0]);
864     else if (e.data == "encrypt")
865     encrypt(e.ports[0]);
866     else if (e.data == "decrypt")
867     decrypt(e.ports[0]);
868     }
869    
870     function genkeys(p) {
871     var generator = new Worker('libcrypto-v2-generator.js');
872     generator.postMessage('', [p]);
873     }
874    
875     function encrypt(p) {
876     p.onmessage = function (e) {
877     var key = e.data;
878     var encryptor = new Worker('libcrypto-v2-encryptor.js');
879     encryptor.postMessage(key, [p]);
880     };
881     }
882    
883     function encrypt(p) {
884     p.onmessage = function (e) {
885     var key = e.data;
886     var decryptor = new Worker('libcrypto-v2-decryptor.js');
887     decryptor.postMessage(key, [p]);
888     };
889     }
890    
891     // support being used as a shared worker as well as a dedicated worker
892     if ('onmessage' in this) // dedicated worker
893     onmessage = handleMessage;
894     else // shared worker
895     onconnect = function (e) { e.ports[0].onmessage = handleMessage };
896     </pre>
897    
898     <p>The little subworkers would then be as follows.</p>
899    
900     <p>For generating key pairs:</p>
901    
902     <pre>onmessage = function (e) {
903     var k = _generateKeyPair();
904     e.ports[0].postMessage(k[0]);
905     e.ports[0].postMessage(k[1]);
906     close();
907     }
908    
909     function _generateKeyPair() {
910     return [Math.random(), Math.random()];
911     }</pre>
912    
913     <p>For encrypting:</p>
914    
915     <pre>onmessage = function (e) {
916     var key = e.data;
917     e.ports[0].onmessage = function (e) {
918     var s = e.data;
919     postMessage(_encrypt(key, s));
920     }
921     }
922    
923     function _encrypt(k, s) {
924     return 'encrypted-' + k + ' ' + s;
925     }</pre>
926    
927     <p>For decrypting:</p>
928    
929     <pre>onmessage = function (e) {
930     var key = e.data;
931     e.ports[0].onmessage = function (e) {
932     var s = e.data;
933     postMessage(_decrypt(key, s));
934     }
935     }
936    
937     function _decrypt(k, s) {
938     return s.substr(s.indexOf(' ')+1);
939     }</pre>
940    
941     <p>Notice how the users of the API don't have to even know that this
942     is happening &mdash; the API hasn't changed; the library can
943     delegate to subworkers without changing its API, even though it is
944     accepting data using message channels.</p>
945    
946     <p><a href=http://www.whatwg.org/demos/workers/crypto/page.html>View this example online</a>.</p>
947    
948    
949    
950    
951     <h2 id=conformance-requirements><span class=secno>2 </span>Conformance requirements</h2>
952    
953     <p>All diagrams, examples, and notes in this specification are
954     non-normative, as are all sections explicitly marked non-normative.
955     Everything else in this specification is normative.</p>
956    
957     <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
958     NOT",--> "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
959     "OPTIONAL" in the normative parts of this document are to be
960     interpreted as described in RFC2119. For readability, these words do
961     not appear in all uppercase letters in this specification. <a href=#refsRFC2119>[RFC2119]</a></p>
962    
963     <p>Requirements phrased in the imperative as part of algorithms
964     (such as "strip any leading space characters" or "return false and
965     abort these steps") are to be interpreted with the meaning of the
966     key word ("must", "should", "may", etc) used in introducing the
967     algorithm.</p>
968    
969     <p>Some conformance requirements are phrased as requirements on
970     attributes, methods or objects. Such requirements are to be
971     interpreted as requirements on user agents.</p>
972    
973     <p>Conformance requirements phrased as algorithms or specific steps
974     may be implemented in any manner, so long as the end result is
975     equivalent. (In particular, the algorithms defined in this
976     specification are intended to be easy to follow, and not intended to
977     be performant.)</p>
978    
979     <p>The only conformance class defined by this specification is user
980     agents.</p>
981    
982     <p>User agents may impose implementation-specific limits on
983     otherwise unconstrained inputs, e.g. to prevent denial of service
984     attacks, to guard against running out of memory, or to work around
985     platform-specific limitations.</p>
986 wakaba 1.191
987     <p>When support for a feature is disabled (e.g. as an emergency
988     measure to mitigate a security problem, or to aid in development, or
989     for performance reasons), user agents must act as if they had no
990     support for the feature whatsoever, and as if the feature was not
991     mentioned in this specification. For example, if a particular
992     feature is accessed via an attribute in a Web IDL interface, the
993     attribute itself would be omitted from the objects that implement
994     that interface &mdash; leaving the attribute on the object but
995     making it return null or throw an exception is insufficient.</p>
996 wakaba 1.190
997    
998     <h3 id=dependencies><span class=secno>2.1 </span>Dependencies</h3>
999    
1000     <p>This specification relies on several other underlying
1001     specifications.</p>
1002    
1003     <dl><dt>HTML</dt>
1004    
1005     <dd>
1006    
1007     <p>Many fundamental concepts from HTML are used by this
1008     specification. <a href=#refsHTML>[HTML]</a></p>
1009    
1010     </dd>
1011    
1012     <dt>WebIDL</dt>
1013    
1014     <dd>
1015    
1016     <p>The IDL blocks in this specification use the semantics of the
1017     WebIDL specification. <a href=#refsWEBIDL>[WEBIDL]</a></p>
1018    
1019     </dd>
1020    
1021     </dl><h2 id=terminology><span class=secno>3 </span>Terminology</h2>
1022    
1023     <p>The construction "a <code title="">Foo</code> object", where
1024     <code title="">Foo</code> is actually an interface, is sometimes
1025     used instead of the more accurate "an object implementing the
1026     interface <code title="">Foo</code>".</p>
1027    
1028     <p>The term DOM is used to refer to the API set made available to
1029     scripts in Web applications, and does not necessarily imply the
1030     existence of an actual <code>Document</code> object or of any other
1031     <code>Node</code> objects as defined in the DOM Core
1032     specifications. <a href=#refsDOMCORE>[DOMCORE]</a></p>
1033    
1034     <p>An IDL attribute is said to be <em>getting</em> when its value is
1035     being retrieved (e.g. by author script), and is said to be
1036     <em>setting</em> when a new value is assigned to it.</p>
1037    
1038     <p>The term "JavaScript" is used to refer to ECMA262, rather than
1039     the official term ECMAScript, since the term JavaScript is more
1040     widely known. <a href=#refsECMA262>[ECMA262]</a></p>
1041    
1042    
1043     <h2 id=infrastructure><span class=secno>4 </span>Infrastructure</h2>
1044    
1045     <p>There are two kinds of workers; dedicated workers, and shared
1046     workers. Dedicated workers, once created, and are linked to their
1047     creator; but message ports can be used to communicate from a
1048     dedicated worker to multiple other browsing contexts or
1049     workers. Shared workers, on the other hand, are named, and once
1050     created any script running in the same <span>origin</span> can
1051     obtain a reference to that worker and communicate with it.</p>
1052    
1053    
1054     <h3 id=the-global-scope><span class=secno>4.1 </span>The global scope</h3>
1055    
1056     <p>The global scope is the "inside" of a worker.</p>
1057    
1058     <h4 id=the-workerglobalscope-abstract-interface><span class=secno>4.1.1 </span>The <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> abstract interface</h4>
1059    
1060     <pre class=idl>interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
1061     readonly attribute <a href=#workerglobalscope>WorkerGlobalScope</a> <a href=#dom-workerglobalscope-self title=dom-WorkerGlobalScope-self>self</a>;
1062     readonly attribute <a href=#workerlocation>WorkerLocation</a> <a href=#dom-workerglobalscope-location title=dom-WorkerGlobalScope-location>location</a>;
1063    
1064     void <a href=#dom-workerglobalscope-close title=dom-WorkerGlobalScope-close>close</a>();
1065     <!-- v2-onclose attribute <span>Function</span> <span title="handler-WorkerGlobalScope-onclose">onclose</span>;
1066     --> attribute <span>Function</span> <a href=#handler-workerglobalscope-onerror title=handler-WorkerGlobalScope-onerror>onerror</a>;
1067     };
1068     <a href=#workerglobalscope>WorkerGlobalScope</a> implements <a href=#workerutils>WorkerUtils</a>;
1069     <a href=#workerglobalscope>WorkerGlobalScope</a> implements <span>EventTarget</span>;</pre>
1070    
1071     <p>The <dfn id=dom-workerglobalscope-self title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute
1072     must return the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object itself.</p>
1073    
1074     <p>The <dfn id=dom-workerglobalscope-location title=dom-WorkerGlobalScope-location><code>location</code></dfn>
1075     attribute must return the <code><a href=#workerlocation>WorkerLocation</a></code> object created
1076     for the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object when the worker was
1077     created. It represents the <span>absolute URL</span> of the script
1078     that was used to initialize the worker, after any redirects.</p>
1079    
1080     <hr><p>When a script invokes the <dfn id=dom-workerglobalscope-close title=dom-WorkerGlobalScope-close><code>close()</code></dfn>
1081     method on a <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object, the user agent
1082     must run the following steps (atomically):</p>
1083    
1084     <ol><li><p>Discard any <span title=concept-task>tasks</span> that
1085     have been added to the <span>event loop</span>'s <span title="task
1086     queue">task queues</span>.</p>
1087    
1088     <!-- v2-onclose
1089     <li><p><span>Queue a task</span> to <span>fire a simple
1090     event</span> named <code title="event-close">close</code> at the
1091     <code>WorkerGlobalScope</code> object.</p></li>
1092     -->
1093    
1094     <li><p>Set the worker's <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object's
1095     <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag to
1096     true. (This prevents any further tasks from being queued.)</li>
1097    
1098     <li><p>Disentangle all the ports in the list of <a href="#the-worker's-ports">the worker's
1099     ports</a>.</li>
1100    
1101     </ol><p>The following are the <span>event handlers</span> (and their
1102     corresponding <span title="event handler event type">event handler
1103     event types</span>) that must be supported, as IDL attributes, by
1104     objects implementing the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code>
1105     interface:</p>
1106    
1107     <table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1108     <tbody><!-- v2-onclose <tr><td><dfn title="handler-WorkerGlobalScope-onclose"><code>onclose</code></dfn> <td> <code title="event-close">close</code> --><tr><td><dfn id=handler-workerglobalscope-onerror title=handler-WorkerGlobalScope-onerror><code>onerror</code></dfn> <td> <code title=event-error>error</code>
1109     </table><hr><p>The <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> interface must not exist if
1110     the interface's <span>relevant namespace object</span> is a
1111     <code>Window</code> object. <a href=#refsWEBIDL>[WEBIDL]</a></p>
1112    
1113    
1114    
1115     <h4 id=dedicated-workers-and-the-dedicatedworkerglobalscope-interface><span class=secno>4.1.2 </span>Dedicated workers and the <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> interface</h4>
1116    
1117     <pre class=idl>[Supplemental, NoInterfaceObject]
1118     interface <dfn id=dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</dfn> : <a href=#workerglobalscope>WorkerGlobalScope</a> {
1119     void <a href=#dom-dedicatedworkerglobalscope-postmessage title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in any message, in optional <span>MessagePortArray</span> ports);<!--
1120     <span>MessagePort</span> <span title="dom-DedicatedWorkerGlobalScope-startConversation">startConversation</span>(in any message);-->
1121     attribute <span>Function</span> <a href=#handler-dedicatedworkerglobalscope-onmessage title=handler-DedicatedWorkerGlobalScope-onmessage>onmessage</a>;
1122     };</pre>
1123    
1124     <p><code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> objects act as if they
1125     had an implicit <code>MessagePort</code> associated with them. This
1126     port is part of a channel that is set up when the worker is created,
1127     but it is not exposed. This object must never be garbage collected
1128     before the <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> object.</p>
1129    
1130     <p>All messages received by that port must immediately be retargeted
1131     at the <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> object.</p>
1132    
1133     <p>The <dfn id=dom-dedicatedworkerglobalscope-postmessage title=dom-DedicatedWorkerGlobalScope-postMessage><code>postMessage()</code></dfn><!--
1134     and <dfn
1135     title="dom-DedicatedWorkerGlobalScope-startConversation"><code>startConversation()</code></dfn>-->
1136     method<!--s (startConversation)--> on
1137     <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> objects must act as if, when
1138     invoked, it<!--/they (startConversation)--> immediately invoked the
1139     method of the same name on the port, with the same arguments, and
1140     returned the same return value.</p>
1141    
1142     <p>The following are the <span>event handlers</span> (and their
1143     corresponding <span title="event handler event type">event handler
1144     event types</span>) that must be supported, as IDL attributes, by
1145     objects implementing the <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code>
1146     interface:</p>
1147    
1148     <table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1149     <tbody><tr><td><dfn id=handler-dedicatedworkerglobalscope-onmessage title=handler-DedicatedWorkerGlobalScope-onmessage><code>onmessage</code></dfn> <td> <code title=event-message>message</code>
1150     </table><p>For the purposes of the <span>application cache</span> networking
1151     model, a dedicated worker is an extension of the <span>cache
1152     host</span> from which it was created.</p>
1153    
1154    
1155    
1156     <h4 id=shared-workers-and-the-sharedworkerglobalscope-inteface><span class=secno>4.1.3 </span>Shared workers and the <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> inteface</h4>
1157    
1158     <pre class=idl>[Supplemental, NoInterfaceObject]
1159     interface <dfn id=sharedworkerglobalscope>SharedWorkerGlobalScope</dfn> : <a href=#workerglobalscope>WorkerGlobalScope</a> {
1160     readonly attribute DOMString <a href=#dom-sharedworkerglobalscope-name title=dom-SharedWorkerGlobalScope-name>name</a>;
1161     readonly attribute <span>ApplicationCache</span> <span title=dom-SharedWorkerGlobalScope-applicationCache>applicationCache</span>;
1162     attribute <span>Function</span> <a href=#handler-sharedworkerglobalscope-onconnect title=handler-SharedWorkerGlobalScope-onconnect>onconnect</a>;
1163     };</pre>
1164    
1165     <p>Shared workers receive message ports through <code title=event-WorkerGlobalScope-connect>connect</code> events on
1166     their global object for each connection.</p>
1167    
1168     <p>The <dfn id=dom-sharedworkerglobalscope-name title=dom-SharedWorkerGlobalScope-name><code>name</code></dfn>
1169     attribute must return the value it was assigned when the
1170     <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object was created by the
1171     "<a href=#run-a-worker>run a worker</a>" algorithm. Its value represents the name
1172     that can be used to obtain a reference to the worker using the
1173     <code><a href=#sharedworker>SharedWorker</a></code> constructor.</p>
1174    
1175     <p>The following are the <span>event handlers</span> (and their
1176     corresponding <span title="event handler event type">event handler
1177     event types</span>) that must be supported, as IDL attributes, by
1178     objects implementing the <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code>
1179     interface:</p>
1180    
1181     <table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1182     <tbody><tr><td><dfn id=handler-sharedworkerglobalscope-onconnect title=handler-SharedWorkerGlobalScope-onconnect><code>onconnect</code></dfn> <td> <code title=event-connect>connect</code>
1183     </table><p>For the purposes of the <span>application cache</span> networking
1184     model, a shared worker is its own <span>cache host</span>. The
1185     <a href=#run-a-worker>run a worker</a> algorithm takes care of associating the
1186     worker with an <span>application cache</span>.</p>
1187    
1188     <p class=note>The <code title=dom-SharedWorkerGlobalScope-applicationCache>applicationCache</code>
1189     attribute returns the <code>ApplicationCache</code> object for the
1190     worker.</p><!-- normative conf criteria is in the appcache section
1191     -->
1192    
1193    
1194     <h3 id=origins-of-workers><span class=secno>4.2 </span>Origins of workers</h3>
1195    
1196     <p>Both the <span>origin</span> and <span>effective script
1197     origin</span> of scripts running in workers are the
1198     <span>origin</span> of the <span>absolute URL</span> given in that
1199     the worker's <code title=dom-WorkerGlobalScope-location><a href=#dom-workerglobalscope-location>location</a></code> attribute
1200     represents.</p>
1201    
1202    
1203    
1204     <h3 id=the-event-loop><span class=secno>4.3 </span>The event loop</h3>
1205    
1206     <p>Each <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object has an <span>event
1207     loop</span> distinct from those defined for <span title="unit of
1208     related similar-origin browsing contexts">units of related
1209     similar-origin browsing contexts</span>. This <span>event
1210     loop</span> has no associated <span>browsing context</span>, and its
1211     <span title="task queue">task queues</span> only have events,
1212     callbacks, and networking activity as <span title=concept-task>tasks</span>. The processing model of these
1213     <span title="event loop">event loops</span> is defined below in the
1214     <a href=#run-a-worker>run a worker</a> algorithm.</p>
1215    
1216     <p>Each <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object also has a <dfn id=dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</dfn> flag, which must
1217     initially be false, but which can get set to true by the algorithms
1218     in the processing model section below.</p>
1219    
1220     <p>Once the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code>'s <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag is set to
1221     true, the <span>event loop</span>'s <span title="task queue">task
1222     queues</span> must discard any further <span title=concept-task>tasks</span> that would be added to them (tasks
1223     already on the queue are unaffected except where otherwise
1224     specified). Effectively, once the <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag is true,
1225     timers stop firing, notifications for all pending asynchronous
1226     operations are dropped, etc.</p>
1227    
1228    
1229    
1230     <h3 id="the-worker's-lifetime"><span class=secno>4.4 </span>The worker's lifetime</h3>
1231    
1232     <p>Workers communicate with other workers and with <span title="browsing context">browsing contexts</span> through <span title="channel messaging">message channels</span> and their
1233     <code>MessagePort</code> objects.</p>
1234    
1235     <p>Each <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> <var title="">worker global
1236     scope</var> has a list of <dfn id="the-worker's-ports">the worker's ports</dfn>, which
1237     consists of all the <code>MessagePort</code> objects that are
1238     entangled with another port and that have one (but only one) port
1239     owned by <var title="">worker global scope</var>. This list includes
1240     <!--all the <code>MessagePort</code> objects that are in events
1241     pending in the <span>event loop</span>, as well as (commented out
1242     because in practice it makes no difference either way as far as I
1243     can tell, and it would be hard to strictly implement since these
1244     ports might not yet be across the thread boundary)--> the implicit
1245     <code>MessagePort</code> in the case of <a href=#dedicatedworkerglobalscope title=DedicatedWorkerGlobalScope>dedicated workers</a>.</p>
1246    
1247     <p>Each <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> also has a list of <dfn id="the-worker's-workers">the
1248     worker's workers</dfn>. Initially this list is empty; it is
1249     populated when the worker creates or obtains further workers.</p>
1250    
1251     <p>Finally, each <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> also has a list of
1252     <dfn id="the-worker's-documents">the worker's <code>Document</code>s</dfn>. Initially this list
1253     is empty; it is populated when the worker is created.</p>
1254    
1255     <p>Whenever a <code>Document</code> <var title="">d</var> is <dfn id="add-a-document-to-the-worker's-documents" title="add a document to the worker's documents">added to the
1256     worker's <code>Document</code>s</dfn>, the user agent must, for each
1257     worker in the list of <a href="#the-worker's-workers">the worker's workers</a> whose list
1258     of <a href="#the-worker's-documents">the worker's <code>Document</code>s</a> does not contain
1259     <var title="">d</var>, <a href="#add-a-document-to-the-worker's-documents" title="add a document to the worker's
1260     documents">add <var title="">d</var> to <var title="">q</var>'s
1261     <code>WorkerGlobalScope</code> owner's list of <span>the worker's
1262     <code>Document</code>s</span></a>.</p> <!-- suggestions welcome
1263     on making this sentence into understandable English -->
1264    
1265     <p>Whenever a <code>Document</code> object is <span title="discard a
1266     Document">discarded</span>, it must be removed from the list of
1267     <a href="#the-worker's-documents">the worker's <code>Document</code>s</a> of each worker
1268     whose list contains that <code>Document</code>.</p>
1269    
1270     <p>Given a <span>script's global object</span> <var title="">o</var>
1271     when creating or obtaining a worker, the <dfn id=list-of-relevant-document-objects-to-add>list of relevant
1272     <code>Document</code> objects to add</dfn> depends on the type of
1273     <var title="">o</var>. If <var title="">o</var> is a
1274     <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object (i.e. if we are creating a
1275     nested worker), then the relevant <code>Document</code>s are the
1276     <code>Document</code>s that are in <var title="">o</var>'s own list
1277     of <a href="#the-worker's-documents">the worker's <code>Document</code>s</a>. Otherwise, <var title="">o</var> is a <code>Window</code> object, and the relevant
1278     <code>Document</code> is just the <code>Document</code> that is the
1279     <span>active document</span> of the <code>Window</code> object <var title="">o</var>.</p>
1280    
1281     <hr><p>A worker is said to be a <dfn id=permissible-worker>permissible worker</dfn> if its
1282     list of <a href="#the-worker's-documents">the worker's <code>Document</code>s</a> is not
1283     empty.</p>
1284    
1285     <p>A worker is said to be a <dfn id=protected-worker>protected worker</dfn> if it is a
1286     <a href=#permissible-worker>permissible worker</a> and either it has outstanding
1287     timers, database transactions, or network connections, or its list
1288     of <a href="#the-worker's-ports">the worker's ports</a> is not empty, or its
1289     <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> is actually a
1290     <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object (i.e. the worker is a
1291     shared worker).</p>
1292    
1293     <p>A worker is said to be an <dfn id=active-needed-worker>active needed worker</dfn> if any
1294     of the <code>Document</code> objects in <a href="#the-worker's-documents">the worker's
1295     <code>Document</code>s</a> are <span>fully active</span>.</p>
1296    
1297     <p>A worker is said to be a <dfn id=suspendable-worker>suspendable worker</dfn> if it is
1298     not an <a href=#active-needed-worker>active needed worker</a> but it is a
1299     <a href=#permissible-worker>permissible worker</a>.</p>
1300    
1301    
1302     <h3 id=processing-model><span class=secno>4.5 </span>Processing model</h3>
1303    
1304     <p>When a user agent is to <dfn id=run-a-worker>run a worker</dfn> for a script with
1305     <span>URL</span> <var title="">url</var>, a browsing context <var title="">owner browsing context</var>, an origin <var title="">owner
1306     origin</var>, and with global scope <var title="">worker global
1307     scope</var>, it must run the following steps:</p>
1308    
1309     <ol><li>
1310    
1311     <p>Create a completely separate and parallel execution environment
1312     (i.e. a separate thread or process or equivalent construct), and
1313     run the rest of these steps asynchronously in that context.</p>
1314    
1315     </li>
1316    
1317     <li><p>If <var title="">worker global scope</var> is actually a
1318     <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object (i.e. the worker is a
1319     shared worker), and there are any <span title="relevant application
1320     cache">relevant application caches</span> that are identified by a
1321     manifest URL with the <span>same origin</span> as <var title="">url</var> and that have <var title="">url</var> as one of
1322     their entries, <em>not</em> excluding entries marked as <span title=concept-appcache-foreign>foreign</span>, then associate the
1323     <var title="">worker global scope</var> with the <span title=concept-appcache-selection>most appropriate application
1324     cache</span> of those that match.</li>
1325    
1326     <li>
1327    
1328     <p>Attempt to <span>fetch</span> the resource identified by <var title="">url</var>, from the <var title="">owner origin</var>.</p>
1329     <!-- not http-origin privacy sensitive -->
1330    
1331     <p>If the attempt fails, or if the attempt involves any redirects
1332     to URIs that do not have the <span>same origin</span> as <var title="">url</var> (even if the final URI is at the <span>same
1333     origin</span> as the original <var title="">url</var>), then for
1334     each <code><a href=#worker>Worker</a></code> or <code><a href=#sharedworker>SharedWorker</a></code> object
1335     associated with <var title="">worker global scope</var>,
1336     <span>queue a task</span> to <span>fire a simple event</span>
1337     named <code title=event-error>error</code> at that
1338     object. Abort these steps.</p>
1339    
1340     <p>If the attempt succeeds, then convert the script resource to
1341     Unicode by assuming it was encoded as UTF-8, to obtain its <var title="">source</var>.</p>
1342    
1343     <p>Let <var title="">language</var> be JavaScript.</p>
1344    
1345     <p class=note>As with <code>script</code> elements, the MIME
1346     type of the script is ignored. Unlike with <code>script</code>
1347     elements, there is no way to override the type. It's always
1348     assumed to be JavaScript.</p>
1349    
1350     </li>
1351    
1352     <li>
1353    
1354     <p>A new <span title=concept-script>script</span> is now
1355     created, as follows.</p>
1356    
1357     <p>Create a new <span>script execution environment</span>
1358     set up as appropriate for the scripting language <var title="">language</var>.</p>
1359    
1360     <p>Parse/compile/initialize <var title="">source</var> using that
1361     <span>script execution environment</span>, as appropriate for <var title="">language</var>, and thus obtain a <span>list of code
1362     entry-points</span>; set the <i>initial code entry-point</i> to
1363     the entry-point for any executable code to be immediately run.</p>
1364    
1365     <p>Set the <span>script's global object</span> to <var title="">worker global scope</var>.</p>
1366    
1367     <p>Set the <span>script's browsing context</span> to <var title="">owner browsing context</var>.</p>
1368    
1369     <p>Set the <span>script's URL character encoding</span> to
1370     UTF-8. (This is just used for encoding non-ASCII characters in the
1371     query component of URLs.)</p>
1372    
1373     <p>Set the <span>script's base URL</span> to <var title="">url</var>.</p>
1374    
1375     </li>
1376    
1377     <li>
1378    
1379     <p><strong>Closing orphan workers</strong>: Start monitoring the
1380     worker such that no sooner than it stops being either a
1381     <a href=#protected-worker>protected worker</a> or a <a href=#suspendable-worker>suspendable
1382     worker</a>, and no later than it stops being a
1383     <a href=#permissible-worker>permissible worker</a>, <var title="">worker global
1384     scope</var>'s <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag is set
1385     to true<!-- v2-onclose and <span title="queue a task">a task is
1386     queued</span> to <span>fire a simple event</span> named <code
1387     title="event-close">close</code> at <var title="">worker global
1388     scope</var>-->.</p>
1389    
1390     </li>
1391    
1392     <li>
1393    
1394     <p><strong>Suspending workers</strong>: Start monitoring the
1395     worker, such that whenever <var title="">worker global
1396     scope</var>'s <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag is false
1397     and the worker is a <a href=#suspendable-worker>suspendable worker</a>, the user
1398     agent suspends execution of script in that worker until such time
1399     as either the <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag switches
1400     to true or the worker stops being a <a href=#suspendable-worker>suspendable
1401     worker</a>.</p>
1402    
1403     </li>
1404    
1405     <li>
1406    
1407     <p><span title="jump to a code entry-point">Jump</span> to the
1408     <span title=concept-script>script</span>'s <i>initial code
1409     entry-point</i>, and let that run until it either returns, fails
1410     to catch an exception, or gets prematurely aborted by the
1411     "<a href=#kill-a-worker>kill a worker</a>" or "<a href=#terminate-a-worker>terminate a worker</a>"
1412     algorithms defined below.</p>
1413    
1414     <!-- v2-onclose
1415     <p class="note">If the script gets aborted by the "<span>kill a
1416     worker</span>" algorithm, then that same algorithm will cause
1417     there to only be a single <span title="concept-task">task</span>
1418     in the <span>event loop</span> at the next step, namely the task
1419     for the <code title="message-close">close</code> event. The
1420     "<span>terminate a worker</span>" algorithm removes all the
1421     events.</p>
1422     -->
1423    
1424     </li>
1425    
1426     <li><p>If <var title="">worker global scope</var> is actually a
1427     <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> object (i.e. the worker is
1428     a dedicated worker), then enable the <span>port message
1429     queue</span> of the worker's implicit port.</li>
1430    
1431     <li>
1432    
1433     <p><i title="">Event loop</i>: Wait until either there is a <span title=concept-task>task</span> in one of the <span>event
1434     loop</span>'s <span title="task queue">task queues</span> or <var title="">worker global scope</var>'s <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag is set
1435     to true.</p>
1436    
1437     </li>
1438    
1439     <li>
1440    
1441     <p>Run the oldest task on one of the <span>event loop</span>'s
1442     <span title="task queue">task queues</span>, if any. The user
1443     agent may pick any <span>task queue</span>.</p>
1444    
1445     <p class=note>The handling of events or the execution of
1446     callbacks might get prematurely aborted by the "<a href=#kill-a-worker>kill a
1447     worker</a>" or "<a href=#terminate-a-worker>terminate a worker</a>" algorithms
1448     defined below.</p>
1449    
1450     </li>
1451    
1452     <li>
1453    
1454     <p>Remove the task run in the previous step, if any, from its
1455     <span>task queue</span>.</p>
1456    
1457     </li>
1458    
1459     <li>
1460    
1461     <p>If there are any more events in the <span>event loop</span>'s
1462     <span title="task queue">task queues</span> or if <var title="">worker global scope</var>'s <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag is set
1463     to false, then jump back to the step above labeled <i>event
1464     loop</i>.</p>
1465    
1466     </li>
1467    
1468     <li>
1469    
1470     <p>If there are any outstanding transactions that have callbacks
1471     that involve <span title=concept-script>scripts</span> whose
1472     <span title="script's global object">global object</span> is the
1473     <var title="">worker global scope</var>, roll them back (without
1474     invoking any of the callbacks).</p>
1475    
1476     </li>
1477    
1478     <li>
1479    
1480     <p>Empty the <var title="">worker global scope</var>'s <span>list
1481     of active timeouts</span> and its <span>list of active
1482     intervals</span>.</p>
1483    
1484     </li>
1485    
1486     <!-- v2-onclose
1487     <li>
1488    
1489     <p>For each <code>Worker</code> or <code>SharedWorker</code>
1490     object associated with <var title="">worker global scope</var>,
1491     <span>queue a task</span> to <span>fire a simple event</span>
1492     named <code title="event-close">close</code> at that object.</p>
1493    
1494     </li>
1495     -->
1496     </ol><hr><p>When a user agent is to <dfn id=kill-a-worker>kill a worker</dfn> it must
1497     run the following steps in parallel with the worker's main loop (the
1498     "<a href=#run-a-worker>run a worker</a>" processing model defined above):</p>
1499    
1500     <ol><!-- v2-onclose
1501     <li><p>If the worker's <code>WorkerGlobalScope</code> object's
1502     <span title="dom-WorkerGlobalScope-closing">closing</span> flag is
1503     false, <span>queue a task</span> to <span>fire a simple
1504     event</span> named <code title="event-close">close</code> at the
1505     worker's <code>WorkerGlobalScope</code> object.</p></li>
1506     --><li><p>Set the worker's <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object's <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag to
1507     true.</li>
1508    
1509     <!-- v2-onclose
1510     <li><p>Wait a user-agent-defined amount of time. If the "<span>run
1511     a worker</span>" processing model defined above immediately starts
1512     running event listeners registered for the <code
1513     title="event-close">close</code> event, this time should not be
1514     zero &mdash; the idea is that the <code
1515     title="event-close">close</code> event can be used to clean up
1516     when shutting down unexpectedly.</p></li>
1517     -->
1518    
1519     <li><p>If there are any <span title=concept-task>tasks</span>
1520     queued in the <span>event loop</span>'s <span title="task
1521     queue">task queues</span><!-- v2-onclose other than the <code
1522     title="event-close">close</code> event that this algorithm just
1523     added-->, discard them without processing them.</li>
1524    
1525     <!-- v2-onclose
1526     <li><p>If the <code title="event-close">close</code> event that
1527     this algorithm just queued hasn't yet been dispatched, then abort
1528     the script currently running in the worker.</p></li>
1529     -->
1530    
1531     <li><p>Wait a user-agent-defined amount of time.</li>
1532    
1533     <li><p>Abort the script currently running in the worker<!--
1534     v2-onclose (if any script is running, then it will be a handler for
1535     the <code title="event-close">close</code> event)-->.</li>
1536    
1537     </ol><p>User agents may invoke the "<a href=#kill-a-worker>kill a worker</a>"
1538     processing model on a worker at any time, e.g. in response to user
1539     requests, in response to CPU quota management, or when a worker
1540     stops being an <a href=#active-needed-worker>active needed worker</a> if the worker
1541     continues executing even after its <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag was
1542     set to true.</p>
1543    
1544     <hr><p>When a user agent is to <dfn id=terminate-a-worker>terminate a worker</dfn> it must run
1545     the following steps in parallel with the worker's main loop (the
1546     "<a href=#run-a-worker>run a worker</a>" processing model defined above):</p>
1547    
1548     <ol><li><p>Set the worker's <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object's
1549     <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag to
1550     true.</li>
1551    
1552     <li><p>If there are any <span title=concept-task>tasks</span>
1553     queued in the <span>event loop</span>'s <span title="task
1554     queue">task queues</span>, discard them without processing
1555     them.</li>
1556    
1557     <li><p>Abort the script currently running in the worker.</li>
1558    
1559     <li><p>If the worker's <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object is
1560     actually a <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code> object (i.e. the
1561     worker is a dedicated worker), then empty the <span>port message
1562     queue</span> of the port that the worker's implicit port is
1563     entangled with.</li>
1564    
1565     </ol><hr><p>The <span>task source</span> for the tasks mentioned above is the
1566     <span>DOM manipulation task source</span>.</p>
1567    
1568    
1569     <h3 id=runtime-script-errors><span class=secno>4.6 </span>Runtime script errors</h3>
1570    
1571     <p>Whenever an uncaught runtime script error occurs in one of the
1572     worker's scripts, if the error did not occur while handling a
1573     previous script error, the user agent must <span>report the
1574     error</span> using the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object's <code title=handler-WorkerGlobalScope-onerror><a href=#handler-workerglobalscope-onerror>onerror</a></code>
1575     attribute.
1576    
1577     <a href=#refsHTML>[HTML]</a>
1578    
1579     </p>
1580    
1581     <p>For shared workers, if the error is still <i title=concept-error-nothandled>not handled</i> afterwards, or if
1582     the error occurred while handling a previous script error, the error
1583     may be reported to the user.
1584    
1585     <a href=#refsHTML>[HTML]</a>
1586    
1587     </p>
1588    
1589     <p>For dedicated workers, if the error is still <i title=concept-error-nothandled>not handled</i> afterwards, or if
1590     the error occurred while handling a previous script error, the user
1591     agent must <span>queue a task</span> to <a href=#fire-a-worker-error-event>fire a worker error
1592     event</a> at the <code><a href=#worker>Worker</a></code> object associated with the
1593     worker.</p>
1594    
1595     <p>When the user agent is to <dfn id=fire-a-worker-error-event>fire a worker error event</dfn> at
1596     a <code><a href=#worker>Worker</a></code> object, it must dispatch an event that uses
1597     the <code><a href=#errorevent>ErrorEvent</a></code> interface, with the name <code title=event-error>error</code>, that doesn't bubble and is
1598     cancelable, with its <code title=dom-ErrorEvent-message><a href=#dom-errorevent-message>message</a></code>, <code title=dom-ErrorEvent-filename><a href=#dom-errorevent-filename>filename</a></code>, and <code title=dom-ErrorEvent-lineno><a href=#dom-errorevent-lineno>lineno</a></code> attributes set
1599     appropriately. The default action of this event depends on whether
1600     the <code><a href=#worker>Worker</a></code> object is itself in a worker. If it is, and
1601     that worker is also a dedicated worker, then the user agent must
1602     again <span>queue a task</span> to <a href=#fire-a-worker-error-event>fire a worker error
1603     event</a> at the <code><a href=#worker>Worker</a></code> object associated with
1604     <em>that</em> worker. Otherwise, then the error may be reported
1605     to the user.</p>
1606    
1607     <p>The <span>task source</span> for the tasks mentioned above is the
1608     <span>DOM manipulation task source</span>.</p>
1609    
1610     <hr><pre class=idl>interface <dfn id=errorevent>ErrorEvent</dfn> : Event {
1611     readonly attribute DOMString <a href=#dom-errorevent-message title=dom-ErrorEvent-message>message</a>;
1612     readonly attribute DOMString <a href=#dom-errorevent-filename title=dom-ErrorEvent-filename>filename</a>;
1613     readonly attribute unsigned long <a href=#dom-errorevent-lineno title=dom-ErrorEvent-lineno>lineno</a>;
1614     void <a href=#dom-errorevent-initerrorevent title=dom-ErrorEvent-initErrorEvent>initErrorEvent</a>(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in DOMString messageArg, in DOMString filenameArg, in unsigned long linenoArg);
1615     };</pre>
1616    
1617     <p>The <dfn id=dom-errorevent-initerrorevent title=dom-ErrorEvent-initErrorEvent><code>initErrorEvent()</code></dfn>
1618     method must initialize the event in a manner analogous to the
1619     similarly-named method in the DOM Events interfaces. <a href=#refsDOMEVENTS>[DOMEVENTS]</a></p>
1620    
1621     <p>The <dfn id=dom-errorevent-message title=dom-ErrorEvent-message><code>message</code></dfn>
1622     attribute represents the error message.</p>
1623    
1624     <p>The <dfn id=dom-errorevent-filename title=dom-ErrorEvent-filename><code>filename</code></dfn>
1625     attribute represents the <span>absolute URL</span> of the script in
1626     which the error originally occurred.</p>
1627    
1628     <p>The <dfn id=dom-errorevent-lineno title=dom-ErrorEvent-lineno><code>lineno</code></dfn>
1629     attribute represents the line number where the error occurred in the
1630     script.</p>
1631    
1632    
1633    
1634     <h3 id=creating-workers><span class=secno>4.7 </span>Creating workers</h3>
1635    
1636     <h4 id=the-abstractworker-abstract-interface><span class=secno>4.7.1 </span>The <code><a href=#abstractworker>AbstractWorker</a></code> abstract interface</h4>
1637    
1638     <pre class=idl>[Supplemental, NoInterfaceObject]
1639     interface <dfn id=abstractworker>AbstractWorker</dfn> {
1640     attribute <span>Function</span> <a href=#handler-abstractworker-onerror title=handler-AbstractWorker-onerror>onerror</a>;
1641     <!-- v2-onclose attribute <span>Function</span> <span title="handler-AbstractWorker-onclose">onclose</span>; -->
1642     };
1643     <a href=#abstractworker>AbstractWorker</a> implements <span>EventTarget</span>;</pre>
1644    
1645     <p>The following are the <span>event handlers</span> (and their
1646     corresponding <span title="event handler event type">event handler
1647     event types</span>) that must be supported, as IDL attributes, by
1648     objects implementing the <code><a href=#abstractworker>AbstractWorker</a></code> interface:</p>
1649    
1650     <table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1651     <tbody><tr><td><dfn id=handler-abstractworker-onerror title=handler-AbstractWorker-onerror><code>onerror</code></dfn> <td> <code title=event-error>error</code>
1652     <!-- v2-onclose <tr><td><dfn title="handler-AbstractWorker-onclose"><code>onclose</code></dfn> <td> <code title="event-close">close</code> -->
1653     </table><h4 id=dedicated-workers-and-the-worker-interface><span class=secno>4.7.2 </span>Dedicated workers and the <code><a href=#worker>Worker</a></code> interface</h4>
1654    
1655     <pre class=idl>[<a href=#dom-worker title=dom-Worker>Constructor</a>(in DOMString scriptURL)]
1656     interface <dfn id=worker>Worker</dfn> : <a href=#abstractworker>AbstractWorker</a> {
1657     void <a href=#dom-worker-terminate title=dom-Worker-terminate>terminate</a>();
1658    
1659     void <a href=#dom-worker-postmessage title=dom-Worker-postMessage>postMessage</a>(in any message, in optional <span>MessagePortArray</span> ports);<!--
1660     <span>MessagePort</span> <span title="dom-Worker-startConversation">startConversation</span>(in any message);-->
1661     attribute <span>Function</span> <a href=#handler-worker-onmessage title=handler-Worker-onmessage>onmessage</a>;
1662     };</pre>
1663    
1664     <p>The <dfn id=dom-worker-terminate title=dom-Worker-terminate><code>terminate()</code></dfn> method,
1665     when invoked, must cause the "<a href=#terminate-a-worker>terminate a worker</a>"
1666     algorithm to be run on the worker with with the object is
1667     associated.</p>
1668    
1669     <p><code><a href=#worker>Worker</a></code> objects act as if they had an implicit
1670     <code>MessagePort</code> associated with them. This port is part of
1671     a channel that is set up when the worker is created, but it is not
1672     exposed. This object must never be garbage collected before the
1673     <code><a href=#worker>Worker</a></code> object.</p>
1674    
1675     <p>All messages received by that port must immediately be retargeted
1676     at the <code><a href=#worker>Worker</a></code> object.</p>
1677    
1678     <p>The <dfn id=dom-worker-postmessage title=dom-Worker-postMessage><code>postMessage()</code></dfn><!--
1679     and <dfn
1680     title="dom-Worker-startConversation"><code>startConversation()</code></dfn>-->
1681     method<!--s (startConversation)--> on <code><a href=#worker>Worker</a></code> objects
1682     must act as if, when invoked, it<!--/they (startConversation)-->
1683     immediately invoked the method of the same name on the port, with
1684     the same arguments, and returned the same return value.</p>
1685    
1686     <p>The following are the <span>event handlers</span> (and their
1687     corresponding <span title="event handler event type">event handler
1688     event types</span>) that must be supported, as IDL attributes, by
1689     objects implementing the <code><a href=#worker>Worker</a></code> interface:</p>
1690    
1691     <table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1692     <tbody><tr><td><dfn id=handler-worker-onmessage title=handler-Worker-onmessage><code>onmessage</code></dfn> <td> <code title=event-message>message</code>
1693     </table><hr><p>When the <dfn id=dom-worker title=dom-Worker><code>Worker(<var title="">scriptURL</var>)</code></dfn> constructor is invoked, the
1694     user agent must run the following steps:</p>
1695    
1696     <ol><li><p><span title="resolve a url">Resolve</span> the <var title="">scriptURL</var> argument relative to the <span>entry
1697     script</span>'s <span title="script's base URL">base URL</span>,
1698     when the method is invoked.</li>
1699    
1700     <li><p>If this fails, throw a <code>SYNTAX_ERR</code>
1701     exception.</li>
1702    
1703     <li>
1704    
1705     <p>If the <span>origin</span> of the resulting <span>absolute
1706     URL</span> is not the <span title="same origin">same</span> as the
1707     origin of the <span>entry script</span>, then throw a
1708     <code>SECURITY_ERR</code> exception.</p>
1709    
1710     <p class=note>Thus, scripts must be external files with the same
1711     scheme as the original page: you can't load a script from a <code title="">data:</code> URL or <code title="">javascript:</code>
1712     URL, and a <code>https:</code> page couldn't start workers using
1713     scripts with <code>http:</code> URLs.</p>
1714    
1715     </li>
1716    
1717     <li><p>Create a new <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code>
1718     object. Let <var title="">worker global scope</var> be this new
1719     object.</li>
1720    
1721     <li><p>Create a new <code><a href=#worker>Worker</a></code> object, associated with
1722     <var title="">worker global scope</var>. Let <var title="">worker</var> be this new object.</li>
1723    
1724     <li><p><span>Create a new <code>MessagePort</code> object</span>
1725     owned by the <span title="script's global object">global
1726     object</span> of the <span title=concept-script>script</span> that
1727     invoked the constructor. Let this be the <var title="">outside
1728     port</var>.</li>
1729    
1730     <li><p>Associate the <var title="">outside port</var> with <var title="">worker</var>.</li>
1731    
1732     <li><p><span>Create a new <code>MessagePort</code> object</span>
1733     owned by <var title="">worker global scope</var>. Let <var title="">inside port</var> be this new object.</li>
1734    
1735     <li><p>Associate <var title="">inside port</var> with <var title="">worker global scope</var>.</li>
1736    
1737     <li><p><span>Entangle</span> <var title="">outside port</var> and
1738     <var title="">inside port</var>.</li>
1739    
1740     <li><p>Return <var title="">worker</var>, and run the following
1741     steps asynchronously.</li>
1742    
1743     <!-- (this is done by the "run a worker" algorithm)
1744     <li><p>Enable <var title="">inside port</var>'s <span>port message
1745     queue</span>.</p></li>
1746     -->
1747    
1748     <li><p>Enable <var title="">outside port</var>'s <span>port message
1749     queue</span>.</li>
1750    
1751     <li>
1752    
1753     <p>Let <var title="">docs</var> be the <a href=#list-of-relevant-document-objects-to-add>list of relevant
1754     <code>Document</code> objects to add</a> given the <span title="script's global object">global object</span> of the <span title=concept-script>script</span> that invoked the
1755     constructor.</p>
1756    
1757     </li>
1758    
1759     <li>
1760    
1761     <p><a href="#add-a-document-to-the-worker's-documents" title="add a document to the worker's documents">Add to
1762     <var title="">worker global scope</var>'s list of <span>the
1763     worker's <code>Document</code>s</span></a> the
1764     <code>Document</code> objects in <var title="">docs</var>.</p>
1765    
1766     </li>
1767    
1768     <li>
1769    
1770     <p>If the <span title="script's global object">global object</span>
1771     of the <span title=concept-script>script</span> that invoked the
1772     constructor is a <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object (i.e. we
1773     are creating a nested worker), add <var title="">worker global
1774     scope</var> to the list of <a href="#the-worker's-workers">the worker's workers</a> of the
1775     <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object that is the <span title="script's global object">global object</span> of the <span title=concept-script>script</span> that invoked the
1776     constructor.</p>
1777    
1778     </li>
1779    
1780     <li>
1781    
1782     <p><a href=#run-a-worker>Run a worker</a> for the resulting <span>absolute
1783     URL</span>, with the <span>script's browsing context</span> of the
1784     script that invoked the method as the <var title="">owner browsing
1785     context</var>, with the <span>origin</span> of the <span>entry
1786     script</span> as the <var title="">owner origin</var>, and with
1787     <var title="">worker global scope</var> as the global scope.</p>
1788    
1789     </li>
1790    
1791     </ol><p>This constructor must be visible when the <span>script's global
1792     object</span> is either a <code>Window</code> object or an object
1793     implementing the <code><a href=#workerutils>WorkerUtils</a></code> interface.</p>
1794    
1795    
1796     <h4 id=shared-workers-and-the-sharedworker-interface><span class=secno>4.7.3 </span>Shared workers and the <code><a href=#sharedworker>SharedWorker</a></code> interface</h4>
1797    
1798     <pre class=idl>[<a href=#dom-sharedworker title=dom-SharedWorker>Constructor</a>(in DOMString scriptURL, in optional DOMString name)]
1799     interface <dfn id=sharedworker>SharedWorker</dfn> : <a href=#abstractworker>AbstractWorker</a> {
1800     readonly attribute <span>MessagePort</span> <a href=#dom-sharedworker-port title=dom-SharedWorker-port>port</a>;
1801     };</pre>
1802    
1803     <p>The <dfn id=dom-sharedworker-port title=dom-SharedWorker-port><code>port</code></dfn>
1804     attribute must return the value it was assigned by the object's
1805     constructor. It represents the <code>MessagePort</code> for
1806     communicating with the shared worker.</p>
1807    
1808     <p>When the <dfn id=dom-sharedworker title=dom-SharedWorker><code>SharedWorker(<var title="">scriptURL</var>, <var title="">name</var>)</code></dfn>
1809     constructor is invoked, the user agent must run the following
1810     steps:</p>
1811    
1812     <ol><li><p><span title="resolve a url">Resolve</span> the <var title="">scriptURL</var> argument.</li>
1813    
1814     <li><p>If this fails, throw a <code>SYNTAX_ERR</code>
1815     exception.</li>
1816    
1817     <li><p>Otherwise, let <var title="">scriptURL</var> be the
1818     resulting <span>absolute URL</span>.</li>
1819    
1820     <li><p>Let <var title="">name</var> be the value of the second
1821     argument, or the empty string if the second argument was
1822     omitted.</li>
1823    
1824     <li>
1825    
1826     <p>If the <span>origin</span> of <var title="">scriptURL</var> is
1827     not the <span title="same origin">same</span> as the origin of the
1828     <span>entry script</span>, then throw a <code>SECURITY_ERR</code>
1829     exception.</p>
1830    
1831     <p class=note>Thus, scripts must be external files with the same
1832     scheme as the original page: you can't load a script from a <code title="">data:</code> URL or <code title="">javascript:</code>
1833     URL, and a <code>https:</code> page couldn't start workers using
1834     scripts with <code>http:</code> URLs.</p>
1835    
1836     </li>
1837    
1838     <li>
1839    
1840     <p>Let <var title="">docs</var> be the <a href=#list-of-relevant-document-objects-to-add>list of relevant
1841     <code>Document</code> objects to add</a> given the <span title="script's global object">global object</span> of the <span title=concept-script>script</span> that invoked the
1842     constructor.</p>
1843    
1844     </li>
1845    
1846     <li>
1847    
1848     <p>Execute the following substeps atomically:</p>
1849    
1850     <ol><li><p>Create a new <code><a href=#sharedworker>SharedWorker</a></code> object, which will
1851     shortly be associated with a <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code>
1852     object. Let this <code><a href=#sharedworker>SharedWorker</a></code> object be <var title="">worker</var>.</li>
1853    
1854     <li><p><span>Create a new <code>MessagePort</code> object</span>
1855     owned by the <span title="script's global object">global
1856     object</span> of the script that invoked the method. Let this be
1857     the <var title="">outside port</var>.</li>
1858    
1859     <li><p>Assign <var title="">outside port</var> to the <code title=dom-SharedWorker-port><a href=#dom-sharedworker-port>port</a></code> attribute of <var title="">worker</var>.</li>
1860    
1861     <li><p>Let <var title="">worker global scope</var> be
1862     null.</li>
1863    
1864     <li><p>If <var title="">name</var> is not the empty string and
1865     there exists a <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object whose
1866     <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a> flag
1867     is false, whose <code title=dom-WorkerGlobalScope-name>name</code> attribute is
1868     exactly equal to <var title="">name</var>, and whose <code title=dom-WorkerGlobalScope-location><a href=#dom-workerglobalscope-location>location</a></code> attribute
1869     represents an <span>absolute URL</span> with the <span>same
1870     origin</span> as <var title="">scriptURL</var>, then let <var title="">worker global scope</var> be that
1871     <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object.</li>
1872    
1873     <li><p>Otherwise, if <var title="">name</var> is the empty string
1874     and there exists a <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object
1875     whose <a href=#dom-workerglobalscope-closing title=dom-WorkerGlobalScope-closing>closing</a>
1876     flag is false, and whose <code title=dom-WorkerGlobalScope-location><a href=#dom-workerglobalscope-location>location</a></code> attribute
1877     is exactly equal to <var title="">scriptURL</var>, then let <var title="">worker global scope</var> be that
1878     <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code> object.</li>
1879    
1880     <li>
1881    
1882     <p>If <var title="">worker global scope</var> is not null, then
1883     run these steps:</p>
1884    
1885     <ol><li><p>If <var title="">worker global scope</var>'s <code title=dom-WorkerGlobalScope-location><a href=#dom-workerglobalscope-location>location</a></code>
1886     attribute represents an <span>absolute URL</span> that is not
1887     exactly equal to <var title="">scriptURL</var>, then throw a
1888     <code>URL_MISMATCH_ERR</code> exception and abort all these
1889     steps.</li>
1890    
1891     <li><p>Associate <var title="">worker</var> with <var title="">worker global scope</var>.</li>
1892    
1893     <li><p><span>Create a new <code>MessagePort</code>
1894     object</span> owned by <var title="">worker global
1895     scope</var>. Let this be the <var title="">inside
1896     port</var>.</li>
1897    
1898     <li><p><span>Entangle</span> <var title="">outside port</var>
1899     and <var title="">inside port</var>.</li>
1900    
1901     <li><p>Return <var title="">worker</var> and perform the next
1902     step asynchronously.</li>
1903    
1904     <li><p>Create an event that uses the <code>MessageEvent</code>
1905     interface, with the name <code title=event-connect>connect</code>, which does not bubble, is
1906     not cancelable, has no default action, has a <code title=dom-MessageEvent-data>data</code> attribute whose value
1907     is the empty string and has a <code title=dom-MessageEvent-ports>ports</code> attribute whose
1908     value is an array containing only the newly created port, and
1909     <span>queue a task</span> to dispatch the event at <var title="">worker global scope</var>.</li>
1910    
1911     <li>
1912    
1913     <p><a href="#add-a-document-to-the-worker's-documents" title="add a document to the worker's documents">Add to
1914     <var title="">worker global scope</var>'s list of <span>the
1915     worker's <code>Document</code>s</span></a> the
1916     <code>Document</code> objects in <var title="">docs</var>.</p>
1917    
1918     </li>
1919    
1920     <li>
1921    
1922     <p>If the <span title="script's global object">global
1923     object</span> of the <span title=concept-script>script</span>
1924     that invoked the constructor is a
1925     <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object, add <var title="">worker global scope</var> to the list of <a href="#the-worker's-workers">the
1926     worker's workers</a> of the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code>
1927     object that is the <span title="script's global object">global
1928     object</span> of the <span title=concept-script>script</span>
1929     that invoked the constructor.</p>
1930    
1931     </li>
1932    
1933     <li><p>Abort all these steps.</li>
1934    
1935     </ol></li>
1936    
1937     <li><p>Create a new <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code>
1938     object. Let <var title="">worker global scope</var> be this new
1939     object.</li>
1940    
1941     <li><p>Associate <var title="">worker</var> with <var title="">worker global scope</var>.</li>
1942    
1943     <li><p>Set the <code title=dom-SharedWorkerGlobalScope-name><a href=#dom-sharedworkerglobalscope-name>name</a></code> attribute of
1944     <var title="">worker global scope</var> to <var title="">name</var>.</li>
1945    
1946     <li><p><span>Create a new <code>MessagePort</code> object</span>
1947     owned by <var title="">worker global scope</var>. Let <var title="">inside port</var> be this new object.</li>
1948    
1949     <li><p><span>Entangle</span> <var title="">outside port</var> and
1950     <var title="">inside port</var>.</li>
1951    
1952     </ol></li>
1953    
1954     <li><p>Return <var title="">worker</var> and perform the remaining
1955     steps asynchronously.</li>
1956    
1957     <li><p>Create an event that uses the <code>MessageEvent</code>
1958     interface, with the name <code title=event-connect>connect</code>, which does not bubble, is not
1959     cancelable, has no default action, has a <code title=dom-MessageEvent-data>data</code> attribute whose value is
1960     the empty string and has a <code title=dom-MessageEvent-ports>ports</code> attribute whose value
1961     is an array containing only the newly created port, and <span>queue
1962     a task</span> to dispatch the event at <var title="">worker global
1963     scope</var>.</li>
1964    
1965     <li>
1966    
1967     <p><a href="#add-a-document-to-the-worker's-documents" title="add a document to the worker's documents">Add to
1968     <var title="">worker global scope</var>'s list of <span>the
1969     worker's <code>Document</code>s</span></a> the
1970     <code>Document</code> objects in <var title="">docs</var>.</p>
1971    
1972     </li>
1973    
1974     <li>
1975    
1976     <p>If the <span title="script's global object">global object</span>
1977     of the <span title=concept-script>script</span> that invoked the
1978     constructor is a <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object, add <var title="">worker global scope</var> to the list of <a href="#the-worker's-workers">the
1979     worker's workers</a> of the <code><a href=#workerglobalscope>WorkerGlobalScope</a></code>
1980     object that is the <span title="script's global object">global
1981     object</span> of the <span title=concept-script>script</span>
1982     that invoked the constructor.</p>
1983    
1984     </li>
1985    
1986     <li>
1987    
1988     <p><a href=#run-a-worker>Run a worker</a> for <var title="">scriptURL</var>,
1989     with the <span>script's browsing context</span> of the script that
1990     invoked the method as the <var title="">owner browsing
1991     context</var>, with the <span>origin</span> of the <span>entry
1992     script</span> as the <var title="">owner origin</var>, and with
1993     <var title="">worker global scope</var> as the global scope.</p>
1994    
1995     </li>
1996    
1997     </ol><p>This constructor must be visible when the <span>script's global
1998     object</span> is either a <code>Window</code> object or an object
1999     implementing the <code><a href=#workerutils>WorkerUtils</a></code> interface.</p>
2000    
2001     <p>The <span>task source</span> for the tasks mentioned above is the
2002     <span>DOM manipulation task source</span>.</p>
2003    
2004    
2005    
2006     <h2 id=apis-available-to-workers><span class=secno>5 </span>APIs available to workers</h2>
2007    
2008     <pre class=idl>[Supplemental, NoInterfaceObject]
2009     interface <dfn id=workerutils>WorkerUtils</dfn> {
2010     void <a href=#dom-workerglobalscope-importscripts title=dom-WorkerGlobalScope-importScripts>importScripts</a>(in DOMString... urls);
2011     readonly attribute <a href=#workernavigator>WorkerNavigator</a> <a href=#dom-worker-navigator title=dom-worker-navigator>navigator</a>;
2012     };
2013     <a href=#workerutils>WorkerUtils</a> implements <span>WindowTimers</span>;</pre>
2014    
2015     <p>The DOM APIs (<code>Node</code> objects, <code>Document</code>
2016     objects, etc) are not available to workers in this version of this
2017     specification.</p>
2018    
2019    
2020     <h3 id=importing-scripts-and-libraries><span class=secno>5.1 </span>Importing scripts and libraries</h3>
2021    
2022     <p>When a script invokes the <dfn id=dom-workerglobalscope-importscripts title=dom-WorkerGlobalScope-importScripts><code>importScripts(<var title="">urls</var>)</code></dfn> method on a
2023     <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object, the user agent must run the
2024     following steps:</p>
2025    
2026     <ol><li><p>If there are no arguments, return without doing
2027     anything. Abort these steps.</li>
2028    
2029     <li><p><span title="resolve a url">Resolve</span> each
2030     argument.</li>
2031    
2032     <li><p>If any fail, throw a <code>SYNTAX_ERR</code>
2033     exception.</li>
2034    
2035     <!--
2036     <li><p>If any of the resulting <span title="absolute URL">absolute
2037     URLs</span> have an <span>origin</span> that is not the <span
2038     title="same origin">same</span> as the origin of the script that
2039     invoked the method, then throw a <code>SECURITY_ERR</code>
2040     exception.</p></li>
2041     -->
2042    
2043     <li>
2044    
2045     <p>Attempt to <span>fetch</span> each resource identified by the
2046     resulting <span title="absolute URL">absolute URLs</span>, from
2047     the <span>entry script</span>'s <span>origin</span>.</p> <!-- not
2048     http-origin privacy sensitive -->
2049    
2050     </li>
2051    
2052     <li>
2053    
2054     <p>For each argument in turn, in the order given, starting with
2055     the first one, run these substeps:</p>
2056    
2057     <ol><li>
2058    
2059     <p>Wait for the fetching attempt for the corresponding resource
2060     to complete.</p>
2061    
2062     <p>If the fetching attempt failed, throw a
2063     <code>NETWORK_ERR</code> exception and abort all these
2064     steps.</p>
2065    
2066     <p>If the attempt succeeds, then convert the script resource to
2067     Unicode by assuming it was encoded as UTF-8, to obtain its <var title="">source</var>.</p>
2068    
2069     <p>Let <var title="">language</var> be JavaScript.</p>
2070    
2071     <p class=note>As with the worker's script, the script here is
2072     always assumed to be JavaScript, regardless of the MIME
2073     type.</p>
2074    
2075     </li>
2076    
2077     <li>
2078    
2079     <p><span>Create a script</span>, using <var title="">source</var> as the script source and <var title="">language</var> as the scripting language, using the
2080     same global object, browsing context, URL character encoding,
2081     base URL, and script group as the <span title=concept-script>script</span> that was created by the
2082     worker's <a href=#run-a-worker>run a worker</a> algorithm.</p>
2083    
2084     <p>Let the newly created <span title=concept-script>script</span> run until it either
2085     returns, fails to parse, fails to catch an exception, or gets
2086     prematurely aborted by the "<a href=#kill-a-worker>kill a worker</a>" or
2087     "<a href=#terminate-a-worker>terminate a worker</a>" algorithms defined above.</p>
2088    
2089     <p>If it failed to parse, then throw an ECMAScript
2090     <code>SyntaxError</code> exception and abort all these
2091     steps. <a href=#refsECMA262>[ECMA262]</a></p>
2092    
2093     <p>If an exception was raised or if the script was prematurely
2094     aborted, then abort all these steps, letting the exception or
2095     aborting continue to be processed by the script that called the
2096     <code title=dom-WorkerGlobalScope-importScripts><a href=#dom-workerglobalscope-importscripts>importScripts()</a></code>
2097     method.</p>
2098    
2099     <p>If the "<a href=#kill-a-worker>kill a worker</a>" or "<a href=#terminate-a-worker>terminate a
2100     worker</a>" algorithms abort the script then abort all these
2101     steps.</p>
2102    
2103     </li>
2104    
2105     </ol></li>
2106    
2107     </ol><h3 id=the-workernavigator-object><span class=secno>5.2 </span>The <code><a href=#workernavigator>WorkerNavigator</a></code> object</h3>
2108    
2109     <p>The <dfn id=dom-worker-navigator title=dom-worker-navigator><code>navigator</code></dfn> attribute
2110     of the <code><a href=#workerutils>WorkerUtils</a></code> interface must return an instance of
2111     the <code><a href=#workernavigator>WorkerNavigator</a></code> interface, which represents the
2112     identity and state of the user agent (the client):</p>
2113    
2114     <pre class=idl>interface <dfn id=workernavigator>WorkerNavigator</dfn> {};
2115     <a href=#workernavigator>WorkerNavigator</a> implements <span>NavigatorID</span>;
2116     <a href=#workernavigator>WorkerNavigator</a> implements <span>NavigatorOnLine</span>;</pre>
2117    
2118     <p>Objects implementing the <code><a href=#workernavigator>WorkerNavigator</a></code> interface
2119     also implement the <code>NavigatorID</code> and
2120     <code>NavigatorOnLine</code> interfaces.
2121    
2122     <a href=#refsHTML>[HTML]</a>
2123    
2124     </p>
2125    
2126     <p>This <code><a href=#workernavigator>WorkerNavigator</a></code> interface must not exist if the
2127     interface's <span>relevant namespace object</span> is a
2128     <code>Window</code> object. <a href=#refsWEBIDL>[WEBIDL]</a></p>
2129    
2130    
2131    
2132     <h3 id=apis-defined-in-other-specifications><span class=secno>5.3 </span>APIs defined in other specifications</h3>
2133    
2134     <p>The <code title=dom-opendatabase>openDatabase()</code> and
2135     <code title=dom-opendatabase-sync>openDatabaseSync()</code>
2136     methods are defined in the Web SQL Database specification. <a href=#refsWEBSQL>[WEBSQL]</a></p>
2137    
2138    
2139    
2140     <h3 id=interface-objects-and-constructors><span class=secno>5.4 </span>Interface objects and constructors</h3>
2141    
2142     <p>There must be no interface objects and constructors available in
2143     the global scope of scripts whose <span>script's global
2144     object</span> is a <code><a href=#workerglobalscope>WorkerGlobalScope</a></code> object except for
2145     the following:</p>
2146    
2147     <ul><li><p><code>XMLHttpRequest</code> and all interface objects and
2148     constructors defined by the XMLHttpRequest specifications, except
2149     that the <span>document response entity body</span> must always be
2150     null. The <span><code>XMLHttpRequest</code> base URL</span> is the
2151     <span>script's base URL</span>; the
2152     <span><code>XMLHttpRequest</code> origin</span> is the script's
2153     <span>origin</span>. <a href=#refsXHR>[XHR]</a></li>
2154    
2155     <li><p>The interface objects and constructors defined by this
2156     specification.</li>
2157    
2158     <li><p>Constructors defined by specifications that explicitly say
2159     that they should be visible when the <span>script's global
2160     object</span> is a <code><a href=#dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</a></code>, a
2161     <code><a href=#sharedworkerglobalscope>SharedWorkerGlobalScope</a></code>, or an object implementing the
2162     <code><a href=#workerutils>WorkerUtils</a></code> interface; the interfaces of any objects
2163     with such constructors; and the interfaces of any objects made
2164     accessible through APIs exposed by those constructors or made
2165     accessible through interfaces to be implemented by any objects that
2166     are themselves accessible to scripts whose <span>script's global
2167     object</span> implements the <code><a href=#workerutils>WorkerUtils</a></code>
2168     interface.</li>
2169    
2170     </ul><p class=note>These requirements do not override the requirements
2171     defined by the Web IDL specification, in particular concerning the
2172     visibility of interfaces annotated with the <code title="">[NoInterfaceObject]</code> extended attribute.</p>
2173    
2174    
2175     <h3 id=worker-locations><span class=secno>5.5 </span>Worker locations</h3>
2176    
2177     <pre class=idl>interface <dfn id=workerlocation>WorkerLocation</dfn> {
2178     readonly attribute DOMString <a href=#dom-workerlocation-href title=dom-WorkerLocation-href>href</a>;
2179     readonly attribute DOMString <a href=#dom-workerlocation-protocol title=dom-WorkerLocation-protocol>protocol</a>;
2180     readonly attribute DOMString <a href=#dom-workerlocation-host title=dom-WorkerLocation-host>host</a>;
2181     readonly attribute DOMString <a href=#dom-workerlocation-hostname title=dom-WorkerLocation-hostname>hostname</a>;
2182     readonly attribute DOMString <a href=#dom-workerlocation-port title=dom-WorkerLocation-port>port</a>;
2183     readonly attribute DOMString <a href=#dom-workerlocation-pathname title=dom-WorkerLocation-pathname>pathname</a>;
2184     readonly attribute DOMString <a href=#dom-workerlocation-search title=dom-WorkerLocation-search>search</a>;
2185     readonly attribute DOMString <a href=#dom-workerlocation-hash title=dom-WorkerLocation-hash>hash</a>;
2186     };</pre>
2187    
2188     <p>A <code><a href=#workerlocation>WorkerLocation</a></code> object represents an <span>absolute
2189     URL</span> set at its creation.</p>
2190    
2191     <p>The <dfn id=dom-workerlocation-href title=dom-WorkerLocation-href><code>href</code></dfn>
2192     attribute must return the <span>absolute URL</span> that the object
2193     represents.</p>
2194    
2195     <p>The <code><a href=#workerlocation>WorkerLocation</a></code> interface also has the complement
2196     of <span>URL decomposition IDL attributes</span>, <dfn id=dom-workerlocation-protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
2197     <dfn id=dom-workerlocation-host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn id=dom-workerlocation-port title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn id=dom-workerlocation-hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
2198     <dfn id=dom-workerlocation-pathname title=dom-WorkerLocation-pathname><code>pathname</code></dfn>,
2199     <dfn id=dom-workerlocation-search title=dom-WorkerLocation-search><code>search</code></dfn>,
2200     and <dfn id=dom-workerlocation-hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These must
2201     follow the rules given for URL decomposition IDL attributes, with the
2202     <span title=concept-uda-input>input</span> being the
2203     <span>absolute URL</span> that the object represents (same as the
2204     <code title=dom-WorkerLocation-href><a href=#dom-workerlocation-href>href</a></code> attribute), and
2205     the <span title=concept-uda-setter>common setter action</span>
2206     being a no-op, since the attributes are defined to be readonly.
2207    
2208     <a href=#refsHTML>[HTML]</a>
2209    
2210     </p>
2211    
2212     <p>The <code><a href=#workerlocation>WorkerLocation</a></code> interface must not exist if the
2213     interface's <span>relevant namespace object</span> is a
2214     <code>Window</code> object. <a href=#refsWEBIDL>[WEBIDL]</a></p>
2215    
2216    
2217    
2218     <h2 class=no-num id=references>References</h2><!--REFS-->
2219    
2220     <p>All references are normative unless marked "Non-normative".</p>
2221    
2222     <!-- Dates are only included for standards older than the Web,
2223     because the newer ones keep changing. -->
2224    
2225     <dl><dt id=refsDOMCORE>[DOMCORE]</dt>
2226     <dd><cite><a href=http://www.w3.org/TR/DOM-Level-3-Core/>Document
2227     Object Model (DOM) Level 3 Core Specification</a></cite>, A. Le
2228     Hors, P. Le Hegaret, L. Wood, G. Nicol, J. Robie, M. Champion,
2229     S. Byrnes. W3C.</dd>
2230     <!--
2231     <dd><cite><a href="http://simon.html5.org/specs/web-dom-core">Web
2232     DOM Core</a></cite>, S. Pieters. W3C.</dd>
2233     -->
2234    
2235     <dt id=refsDOMEVENTS>[DOMEVENTS]</dt>
2236     <!--
2237     <dd><cite><a
2238     href="http://www.w3.org/TR/DOM-Level-3-Events/">Document Object
2239     Model (DOM) Level 3 Events Specification</a></cite>,
2240     B. H&ouml;hrmann, P. Le Hegaret, T. Pixley. W3C.</dd>
2241     -->
2242     <dd><cite><a href=http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html>Document
2243     Object Model (DOM) Level 3 Events Specification</a></cite>,
2244     D. Schepers. W3C.</dd>
2245    
2246     <dt id=refsECMA262>[ECMA262]</dt>
2247     <dd><cite><a href=http://www.ecma-international.org/publications/standards/Ecma-262.htm>ECMAScript
2248     Language Specification</a></cite>. ECMA.</dd>
2249    
2250     <dt id=refsHTML>[HTML]</dt>
2251     <dd><cite><a href=http://www.whatwg.org/specs/web-apps/current-work/>HTML</a></cite>,
2252     I. Hickson. WHATWG.</dd>
2253    
2254     <dt id=refsRFC2119>[RFC2119]</dt>
2255     <dd><cite><a href=http://www.ietf.org/rfc/rfc2119.txt>Key words for use in
2256     RFCs to Indicate Requirement Levels</a></cite>, S. Bradner. IETF.</dd>
2257    
2258     <dt id=refsWEBIDL>[WEBIDL]</dt>
2259     <!--
2260     <dd><cite><a href="http://www.w3.org/TR/WebIDL/">Web
2261     IDL</a></cite>, C. McCormack. W3C.</dd>
2262     -->
2263     <dd><cite><a href=http://dev.w3.org/2006/webapi/WebIDL/>Web
2264     IDL</a></cite>, C. McCormack. W3C.</dd>
2265 wakaba 1.192
2266     <dt id=refsWEBSQL>[WEBSQL]</dt>
2267     <dd><cite><a href=http://dev.w3.org/html5/webdatabase/>Web SQL
2268     Database</a></cite>, I. Hickson. W3C.</dd>
2269 wakaba 1.190
2270     <dt id=refsXHR>[XHR]</dt>
2271     <!--
2272     <dd><cite><a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest
2273     Object</a></cite>, A. van Kesteren. W3C.</dd>
2274     -->
2275     <dd><cite><a href=http://dev.w3.org/2006/webapi/XMLHttpRequest-2/><code>XMLHttpRequest</code></a></cite>,
2276     A. van Kesteren. W3C.</dd>
2277    
2278     </dl><h2 class=no-num id=acknowledgements>Acknowledgements</h2> <!-- ACKS -->
2279    
2280     <p>Thanks to
2281    
2282     Aaron Boodman,
2283     &#1040;&#1083;&#1077;&#1082;&#1089;&#1077;&#1081; &#1055;&#1088;&#1086;&#1089;&#1082;&#1091;&#1088;&#1103;&#1082;&#1086;&#1074; (Alexey Proskuryakov),
2284     Anne van Kesteren,
2285     Ben Turner,
2286     Dmitry Titov,
2287     Drew Wilson,
2288     Jeremy Orlow,
2289     Jonas Sicking,
2290     Justin James,
2291     Kevin Hakanson,
2292     Maciej Stachowiak,
2293     Michael Nordman,
2294     Mike Smith,
2295    
2296     and
2297    
2298     Philip Taylor
2299    
2300     for their useful and substantial comments.</p>
2301    
2302     <p>Huge thanks to the whole Gears team, who pioneered this
2303     technology and whose experience has been a huge influence on this
2304     specification.</p>
2305    
2306    

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24