/[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.9 - (hide annotations) (download) (as text)
Fri Aug 8 06:18:38 2008 UTC (17 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.8: +190 -113 lines
File MIME type: text/html
*** empty log message ***

1 wakaba 1.1
2     <html lang=en-US-x-hixie>
3     <head>
4     <title>Web Workers</title>
5     <link href="/style/specification" rel=stylesheet>
6     <link href="/images/icon" rel=icon>
7    
8 wakaba 1.6 <style>
9     p > span:not([title=""]):not([class="big-issue"]), li > span:not([title=""]):not([class="big-issue"]) { border-bottom: solid #99CC99; }
10     </style>
11    
12 wakaba 1.1 <body class=draft>
13     <div class=head>
14     <p><a class=logo href="http://www.whatwg.org/" rel=home><img alt=WHATWG
15     src="/images/logo"></a></p>
16    
17     <h1 id=web-workers>Web Workers</h1>
18    
19 wakaba 1.9 <h2 class="no-num no-toc" id=draft>Draft Recommendation &mdash; 8 August
20 wakaba 1.1 2008</h2>
21    
22     <p>You can take part in this work. <a
23     href="http://www.whatwg.org/mailing-list">Join the working group's
24     discussion list.</a></p>
25    
26     <p><strong>Web designers!</strong> We have a <a
27     href="http://blog.whatwg.org/faq/">FAQ</a>, a <a
28     href="http://forums.whatwg.org/">forum</a>, and a <a
29     href="http://www.whatwg.org/mailing-list#help">help mailing list</a> for
30     you!</p>
31    
32     <dl>
33     <dt>This version:
34    
35     <dd><a
36 wakaba 1.5 href="http://www.whatwg.org/specs/web-workers/current-work/">http://whatwg.org/ww</a>
37 wakaba 1.1
38     <dt>Version history:
39    
40     <dd>Twitter messages (non-editorial changes only): <a
41     href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a>
42    
43     <dd>Commit-Watchers mailing list: <a
44     href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a>
45    
46     <dd>Interactive Web interface: <a
47     href="http://html5.org/tools/web-workers-tracker">http://html5.org/tools/web-workers-tracker</a>
48    
49     <dd>Subversion interface: <a
50     href="http://svn.whatwg.org/webworkers/">http://svn.whatwg.org/webworkers/</a>
51    
52     <dt>Issues:
53    
54     <dd>To send feedback: <a
55     href="http://www.whatwg.org/mailing-list">whatwg@whatwg.org</a>
56    
57     <dd>To view and vote on feedback: <a
58     href="http://www.whatwg.org/issues/">http://www.whatwg.org/issues/</a>
59    
60     <dt>Editor:
61    
62     <dd>Ian Hickson, Google, ian@hixie.ch
63     </dl>
64    
65     <p class=copyright>&copy; Copyright 2004-2008 Apple Computer, Inc.,
66     Mozilla Foundation, and Opera Software ASA.</p>
67    
68     <p class=copyright>You are granted a license to use, reproduce and create
69     derivative works of this document.</p>
70     </div>
71    
72     <hr>
73    
74     <h2 class="no-num no-toc" id=abstract>Abstract</h2>
75    
76     <p>This specification defines an API that allows Web application authors to
77     spawn background workers running scripts in parallel to their main page.
78     This allows for thread-like operation with message-passing as the
79     coordination mechanism.
80    
81     <h2 class="no-num no-toc" id=status>Status of this document</h2>
82    
83     <p><strong>This is a work in progress!</strong> This document is changing
84     on a daily if not hourly basis in response to comments and as a general
85     part of its development process. Comments are very welcome, please send
86     them to <a href="mailto:whatwg@whatwg.org">whatwg@whatwg.org</a>. Thank
87     you.
88    
89     <p>The current focus is in developing a first draft proposal.
90    
91     <p>Implementors should be aware that this specification is not stable.
92     <strong>Implementors who are not taking part in the discussions are likely
93     to find the specification changing out from under them in incompatible
94     ways.</strong> Vendors interested in implementing this specification
95     before it eventually reaches the call for implementations should join the
96     <a href="/mailing-list">WHATWG mailing list</a> and take part in the
97     discussions.
98    
99     <p>This specification is also being produced by the <a
100     href="http://www.w3.org/html/wg">W3C HTML WG</a>. The two specifications
101     are identical from the table of contents onwards.
102    
103     <h2 class="no-num no-toc" id=contents>Table of contents</h2>
104     <!--begin-toc-->
105    
106     <ul class=toc>
107     <li><a href="#introduction"><span class=secno>1. </span>Introduction</a>
108     <ul class=toc>
109 wakaba 1.3 <li><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a>
110 wakaba 1.8 <ul class=toc>
111     <li><a href="#a-background"><span class=secno>1.1.1 </span>A
112     background number-crunching worker</a>
113    
114     <li><a href="#a-worker"><span class=secno>1.1.2 </span>A worker for
115     updating a client-side database</a>
116    
117     <li><a href="#worker"><span class=secno>1.1.3 </span>Worker used for
118     backgroud I/O</a>
119    
120     <li><a href="#shared"><span class=secno>1.1.4 </span>Shared
121     workers</a>
122    
123     <li><a href="#delegation"><span class=secno>1.1.5
124     </span>Delegation</a>
125    
126     <li><a href="#granting"><span class=secno>1.1.6 </span>Granting
127     capabilities</a>
128     </ul>
129 wakaba 1.1
130 wakaba 1.6 <li><a href="#conformance"><span class=secno>1.2 </span>Conformance
131 wakaba 1.1 requirements</a>
132     <ul class=toc>
133 wakaba 1.6 <li><a href="#dependencies"><span class=secno>1.2.1
134 wakaba 1.1 </span>Dependencies</a>
135     </ul>
136    
137 wakaba 1.6 <li><a href="#terminology"><span class=secno>1.3 </span>Terminology</a>
138 wakaba 1.3 </ul>
139    
140 wakaba 1.5 <li><a href="#infrastructure"><span class=secno>2.
141     </span>Infrastructure</a>
142 wakaba 1.3 <ul class=toc>
143 wakaba 1.7 <li><a href="#the-workerglobalscope"><span class=secno>2.1 </span>The
144     <code>WorkerGlobalScope</code> interface</a>
145 wakaba 1.3
146 wakaba 1.6 <li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
147     origins of workers</a>
148    
149     <li><a href="#the-queue"><span class=secno>2.3 </span>The queue of
150 wakaba 1.3 events</a>
151    
152 wakaba 1.6 <li><a href="#the-workers"><span class=secno>2.4 </span>The worker's
153 wakaba 1.4 ports</a>
154    
155 wakaba 1.6 <li><a href="#processing"><span class=secno>2.5 </span>Processing
156 wakaba 1.3 model</a>
157 wakaba 1.5
158 wakaba 1.6 <li><a href="#creating"><span class=secno>2.6 </span>Creating
159 wakaba 1.5 workers</a>
160 wakaba 1.3 </ul>
161    
162 wakaba 1.5 <li><a href="#apis-available"><span class=secno>3. </span>APIs available
163     to workers</a>
164 wakaba 1.7 <ul class=toc>
165     <li><a href="#importing"><span class=secno>3.1 </span>Importing scripts
166     and libraries</a>
167    
168     <li><a href="#apis-defined"><span class=secno>3.2 </span>APIs defined in
169     other specifications</a>
170    
171     <li><a href="#interface"><span class=secno>3.3 </span>Interface objects
172     and constructors</a>
173 wakaba 1.8
174     <li><a href="#worker0"><span class=secno>3.4 </span>Worker locations</a>
175    
176 wakaba 1.7 </ul>
177 wakaba 1.1
178     <li class=no-num><a href="#references">References</a>
179    
180     <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
181     </ul>
182     <!--end-toc-->
183    
184     <hr>
185    
186     <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
187    
188 wakaba 1.3 <h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3>
189 wakaba 1.1
190     <p><em>This section is non-normative.</em>
191    
192 wakaba 1.8 <p>There are a variety of uses that workers can be put to. The following
193     subsections show various examples of this use.
194    
195     <h4 id=a-background><span class=secno>1.1.1 </span>A background
196     number-crunching worker</h4>
197    
198     <p><em>This section is non-normative.</em>
199    
200     <p>The simplest use of workers is for performing a computationally
201     expensive task without interrupting the user interface.
202    
203     <p>In this example, the main document spawns a worker to (na&iuml;vely)
204     compute prime numbers, and progressively displays the most recently found
205     prime number.
206    
207     <p>The main page is as follows:
208    
209     <pre>&lt;!DOCTYPE HTML>
210     &lt;html>
211     &lt;head>
212     &lt;title>Worker example: One-core computation&lt;/title>
213     &lt;/head>
214     &lt;body>
215     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
216     &lt;script>
217     var worker = createWorker('worker.js');
218 wakaba 1.9 worker.port.onmessage = function (event) {
219 wakaba 1.8 document.getElementById('result').textContent = event.message;
220     };
221     &lt;/script>
222     &lt;/body>
223     &lt;/html></pre>
224    
225     <p>The <code title=dom-WorkerFactory-createWorker><a
226     href="#createworker">createWorker()</a></code> method call creates a
227 wakaba 1.9 worker and returns a <code><a href="#worker1">Worker</a></code> object
228     representing that worker and containing a <code>MessagePort</code> object,
229     which is used to communicate with the worker. That object's <code
230 wakaba 1.8 title=dom-MessagePort-onmessage>onmessage</code> event handler attribute
231     allows the code to receive messages from the worker.
232    
233     <p>The worker itself is as follows:
234    
235     <pre>var n = 1;
236     search: while (true) {
237     n += 1;
238     for (var i = 2; i &lt;= Math.sqrt(n); i += 1)
239     if (n % i == 0)
240     continue search;
241     // found a prime!
242     port.postMessage(n);
243     }</pre>
244    
245     <p>The bulk of this code is simply an unoptimised search for a prime
246     number. To send a message back to the page, the <code
247     title=dom-WorkerGlobalScope-port><a href="#port">port</a></code> variable
248     (defined automatically when the worker is created) is used to post a
249     message when a prime is found.
250    
251     <p><a href="http://www.whatwg.org/demos/workers/primes/page.html">View this
252     example online</a>.
253    
254     <h4 id=a-worker><span class=secno>1.1.2 </span>A worker for updating a
255     client-side database</h4>
256    
257     <p><em>This section is non-normative.</em>
258    
259     <p>In this example, the main document spawns a worker whose only task is to
260     listen for notifications from the server, and, when appropriate, either
261     add or remove data from the client-side database.
262    
263     <p>Since no communication occurs between the worker and the main page, the
264     main page can start the worker by just doing:
265    
266     <pre>&lt;script>
267     createWorker('worker.js');
268     &lt;/script></pre>
269    
270     <p>The worker itself is as follows:
271    
272     <pre>var server = new WebSocket('ws://whatwg.org/database');
273 wakaba 1.9 var database = openDatabase('demobase', '1.0', 'Demo Database', 10240);
274 wakaba 1.8 server.onmessage = function (event) {
275     // data is in the format "command key value"
276     var data = event.message.split(' ');
277     switch (data[0]) {
278     case '+':
279     database.transaction(function(tx) {
280     tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
281     });
282     case '-':
283     database.transaction(function(tx) {
284     tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
285     });
286     }
287     };</pre>
288    
289     <p>This connects to the server using the <code>WebSocket</code> mechanism
290     and opens the local database (which, we presume, has been created
291     earlier). The worker then just listens for messages from the worker and
292     acts on them as appropriate, forever (or until the main page is closed).
293    
294     <p><a
295     href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
296     this example online</a>. (This example will not actually function, since
297     the server does not actually exist and the database is not created by this
298     sample code.)
299    
300     <h4 id=worker><span class=secno>1.1.3 </span>Worker used for backgroud I/O</h4>
301    
302     <p><em>This section is non-normative.</em>
303    
304     <p>In this example, the main document uses two workers, one for fetching
305     stock updates for at regular intervals, and one for fetching performing
306     search queries that the user requests.
307    
308     <p>The main page is as follows:
309    
310     <pre>&lt;!DOCTYPE HTML>
311     &lt;html>
312     &lt;head>
313     &lt;title>Worker example: Stock ticker&lt;/title>
314     &lt;script>
315     // TICKER
316     var symbol = 'GOOG'; // default symbol to watch
317     var ticker = createWorker('ticker.js');
318 wakaba 1.9 ticker.port.postMessage(symbol);
319 wakaba 1.8
320     // SEARCHER
321     var searcher = createWorker('searcher.js');
322     function search(query) {
323 wakaba 1.9 searcher.port.postMessage(query);
324 wakaba 1.8 }
325    
326     // SYMBOL SELECTION UI
327     function select(newSymbol) {
328     symbol = newSymbol;
329 wakaba 1.9 ticker.port.postMessage(symbol);
330 wakaba 1.8 }
331     &lt;/script>
332     &lt;/head>
333     &lt;body>
334     &lt;p>&lt;output id="symbol">&lt;/output> &lt;output id="value">&lt;/output>&lt;/p>
335     &lt;script>
336 wakaba 1.9 ticker.port.onmessage = function (event) {
337 wakaba 1.8 var data = event.message.split(' ');
338     document.getElementById('symbol').textContent = data[0];
339     document.getElementById('value').textContent = data[1];
340     };
341     &lt;/script>
342     &lt;p>&lt;label>Search: &lt;input type="text" oninput="search(this.value)">&lt;/label>&lt;/p>
343     &lt;ul id="results">&lt;/ul>
344     &lt;script>
345 wakaba 1.9 searcher.port.onmessage = function (event) {
346 wakaba 1.8 var data = event.message.split(' ');
347     var results = document.getElementById('results');
348     while (results.hasChildNodes()) // clear previous results
349     results.removeChild(results.firstChild);
350     for (var i = 0; i &lt; data.length; i += 1) {
351     // add a list item with a button for each result
352     var li = document.createElement('li');
353     var button = document.createElement('button');
354     button.value = data[i];
355     button.type = 'button';
356     button.onclick = function () { select(this.value); };
357     button.textContent = data[i];
358     li.appendChild(button);
359     results.appendChild(li);
360     }
361     };
362     &lt;/script>
363     &lt;p>(The data in this example is not real. Try searching for "Google" or "Apple".)&lt;/p>
364     &lt;/body>
365     &lt;/html></pre>
366    
367     <p>Messages are queued until the <code
368     title=handler-MessagePort-onmessage>onmessage</code> handler is set, so
369     even if it takes a while for the message handler to be attached, no data
370     is lost.
371    
372     <p>The two workers use a common library for performing the actual network
373     calls. This library is as follows:
374    
375     <pre>function get(url) {
376     try {
377     var xhr = new XMLHttpRequest();
378     xhr.open('GET', url, false);
379     xhr.send();
380     return xhr.responseText;
381     } catch (e) {
382     return ''; // turn all errors into empty results
383     }
384     }</pre>
385    
386     <p>The stock updater worker is as follows:
387    
388 wakaba 1.9 <pre>importScripts('io.js');
389 wakaba 1.8 var timer;
390     var symbol;
391     function update() {
392     port.postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
393     timer = setTimeout(update, 10000);
394     }
395     port.onmessage = function (event) {
396     if (timer)
397     clearTimeout(timer);
398     symbol = event.message;
399     update();
400     };</pre>
401    
402     <p>The search query worker is as follows:
403    
404 wakaba 1.9 <pre>importScripts('io.js');
405 wakaba 1.8 port.onmessage = function (event) {
406     port.postMessage(get('search.cgi?' + event.message));
407     };</pre>
408    
409     <p><a href="http://www.whatwg.org/demos/workers/stocks/page.html">View this
410     example online</a>.
411    
412     <h4 id=shared><span class=secno>1.1.4 </span>Shared workers</h4>
413    
414     <p><em>This section is non-normative.</em>
415    
416     <p>In this example, multiple windows (viewers) can be opened that are all
417     viewing the same map. All the windows share the same map information, with
418     a single worker coordinating all the viewers. Each viewer can move around
419     idependently, but if they set any data on the map, all the viewers are
420     updated.
421    
422     <p>The main page isn't interesting, it merely provides a way to open the
423     viewers:
424    
425     <pre>&lt;!DOCTYPE HTML>
426     &lt;html>
427     &lt;head>
428     &lt;title>Workers example: Multiviewer&lt;/title>
429     &lt;script>
430     function openViewer() {
431     window.open('viewer.html');
432     }
433     &lt;/script>
434     &lt;/head>
435     &lt;body>
436     &lt;p>&lt;button type=button onclick="openViewer()">Open a new
437     viewer&lt;/button>&lt;/p>
438     &lt;p>Each viewer opens in a new window. You can have as many viewers
439     as you like, they all view the same data.&lt;/p>
440     &lt;/body>
441     &lt;/html></pre>
442    
443     <p>The viewer is more involved:
444    
445     <pre>&lt;!DOCTYPE HTML>
446     &lt;html>
447     &lt;head>
448     &lt;title>Workers example: Multiviewer viewer&lt;/title>
449     &lt;script>
450     var worker = createNamedWorker('worker.js', 'core');
451    
452     // CONFIGURATION
453     function configure(event) {
454     if (event.message.substr(0, 4) != 'cfg ') return;
455     var name = event.message.substr(4).split(' ', 1);
456     // update display to mention our name is name
457     document.getElementsByTagName('h1')[0].textContent += ' ' + name;
458     // no longer need this listener
459 wakaba 1.9 worker.port.removeEventListener('message', configure, false);
460 wakaba 1.8 }
461 wakaba 1.9 worker.port.addEventListener('message', configure, false);
462 wakaba 1.8
463     // MAP
464     function paintMap(event) {
465     if (event.message.substr(0, 4) != 'map ') return;
466     var data = event.message.substr(4).split(',');
467     // display tiles data[0] .. data[8]
468     var canvas = document.getElementById('map');
469     var context = canvas.getContext('2d');
470     for (var y = 0; y &lt; 3; y += 1) {
471     for (var x = 0; x &lt; 3; x += 1) {
472     var tile = data[y * 3 + x];
473     if (tile == '0')
474     context.fillStyle = 'green';
475     else
476     context.fillStyle = 'maroon';
477     fillRect(x * 50, y * 50, 50, 50);
478     }
479     }
480     }
481 wakaba 1.9 worker.port.addEventListener('message', paintMap, false);
482 wakaba 1.8
483     // PUBLIC CHAT
484     function updatePublicChat(event) {
485     if (event.message.substr(0, 4) != 'txt ') return;
486     var name = event.message.substr(4).split(' ', 1);
487     var message = event.message.substr(4 + length(name) + 1);
488     // display "&lt;name> message" in public chat
489     var dialog = document.getElementById('public');
490     var dt = document.createElement('dt');
491     dt.textContent = name;
492     dialog.appendChild(dt);
493     var dd = document.createElement('dd');
494     dd.textContent = message;
495     dialog.appendChild(dd);
496     }
497 wakaba 1.9 worker.port.addEventListener('message', updatePublicChat, false);
498 wakaba 1.8
499     // PRIVATE CHAT
500     function startPrivateChat(event) {
501     if (event.message.substr(0, 4) != 'msg ') return;
502     var name = event.message.substr(4).split(' ', 1);
503     var port = event.port;
504     // display a private chat UI
505     var ul = document.getElementById('private');
506     var li = document.createElement('li');
507     var h3 = document.createElement('h3');
508     h3.textContent = 'Private chat with ' + name;
509     li.appendChild(h3);
510     var dialog = document.createElement('dialog');
511     var addMessage = function(name, message) {
512     var dt = document.createElement('dt');
513     dt.textContent = name;
514     dialog.appendChild(dt);
515     var dd = document.createElement('dd');
516     dd.textContent = message;
517     dialog.appendChild(dd);
518     };
519     port.onmessage = function (event) {
520     addMessage(name, event.message);
521     };
522     li.appendChild(dialog);
523     var form = document.createElement('form');
524     var p = document.createElement('p');
525     var input = document.createElement('input');
526     input.size = 50;
527     p.appendChild(input);
528     p.appendChild(document.createTextNode(' '));
529     var button = document.createElement('button');
530     button.textContent = 'Post';
531     p.appendChild(button);
532     form.onsubmit = function () {
533     port.postMessage(input.value);
534     addMessage('me', input.value);
535     input.value = '';
536     return false;
537     };
538     form.appendChild(p);
539     li.appendChild(form);
540     }
541 wakaba 1.9 worker.port.addEventListener('message', startPrivateChat, false);
542 wakaba 1.8
543     function init() {
544 wakaba 1.9 // begin receiving messages (messages are queued until you either
545     // set worker.port.onmessage or call worker.port.start())
546     worker.port.start();
547 wakaba 1.8 }
548     &lt;/script>
549     &lt;/head>
550     &lt;body onload="init()">
551     &lt;h1>Viewer&lt;/h1>
552     &lt;h2>Map&lt;/h2>
553     &lt;p>&lt;canvas id="map" height=150 width=150>&lt;/canvas>&lt;/p>
554     &lt;p>
555 wakaba 1.9 &lt;button type=button onclick="worker.port.postMessage('mov left')">Left&lt;/button>
556     &lt;button type=button onclick="worker.port.postMessage('mov up')">Up&lt;/button>
557     &lt;button type=button onclick="worker.port.postMessage('mov down')">Down&lt;/button>
558     &lt;button type=button onclick="worker.port.postMessage('mov right')">Right&lt;/button>
559     &lt;button type=button onclick="worker.port.postMessage('set 0')">Set 0&lt;/button>
560     &lt;button type=button onclick="worker.port.postMessage('set 1')">Set 1&lt;/button>
561 wakaba 1.8 &lt;/p>
562     &lt;h2>Public Chat&lt;/h2>
563     &lt;dialog id="public">&lt;/dialog>
564 wakaba 1.9 &lt;form onsubmit="worker.port.postMessage('txt ' + message.value); message.value = ''; return false;">
565 wakaba 1.8 &lt;p>
566     &lt;input type="text" name="message" size="50">
567     &lt;button>Post&lt;/button>
568     &lt;/p>
569     &lt;/form>
570     &lt;h2>Private Chat&lt;/h2>
571     &lt;ul id="private">&lt;/ul>
572     &lt;/body>
573 wakaba 1.9 &lt;/html>
574     </pre>
575 wakaba 1.8
576     <p>There are several key things worth noting about the way the viewer is
577     written.
578    
579     <p><strong>Multiple listeners</strong>. Instead of a single message
580     processing function, the code here attaches multiple event listeners, each
581     one performing a quick check to see if it is relevant for the message. In
582     this example it doesn't make much difference, but if multiple authors
583     wanted to collaborate using a single port to communicate with a worker, it
584     would allow for independent code instead of changes having to all be made
585     to a single event handling function.
586    
587     <p>Because event listeners are registered using <code
588     title="">addEventListener()</code> instead of <code
589     title=handler-MessagePort-onmessage>onmessge</code>, the events remain
590     queued until the <code title=dom-MessagePort-start>start()</code> method
591     is called on the message port, in the <code title="">init()</code>
592     function called from the <code title=handler-onload>onload</code> handler.
593    
594     <p>Registering event listeners in this way also allows you to unregister
595     specific listeners when you are done with them, as is done with the <code
596     title="">configure()</code> method in this example.
597    
598     <p>Finally, the worker:
599    
600     <pre>
601     var nextName = 0;
602     function getNextName() {
603     // this could use more friendly names
604     // but for now just return a number
605     return nextName++;
606     }
607    
608     var map = [
609     [0, 0, 0, 0, 0, 0, 0],
610     [1, 1, 0, 1, 0, 1, 1],
611     [0, 1, 0, 1, 0, 0, 0],
612     [0, 1, 0, 1, 0, 1, 1],
613     [0, 0, 0, 1, 0, 0, 0],
614     [1, 0, 0, 1, 1, 1, 1],
615     [1, 1, 0, 1, 1, 0, 1],
616     ];
617    
618     function wrapX(x) {
619     if (x &lt; 0) return wrapX(x + map[0].length);
620     if (x >= map[0].length) return wrapX(x - map[0].length);
621     return x;
622     }
623    
624     function wrapY(y) {
625     if (y &lt; 0) return wrapY(y + map.length);
626     if (y >= map[0].length) return wrapY(y - map.length);
627     return y;
628     }
629    
630     function sendMapData(viewer) {
631     var data = '';
632     for (var y = viewer.y-1; y &lt;= viewer.y+1; y += 1) {
633     for (var x = viewer.x-1; x &lt;= viewer.x+1; x += 1) {
634     if (data != '')
635     data += ',';
636     data += map[y][x];
637     }
638     }
639     viewer.port.postMessage('map ' + data);
640     }
641    
642     var viewers = {};
643     onconnect = function (event) {
644     event.port._name = getNextName();
645     event.port._data = { port: event.port, x: 0, y: 0, };
646     viewers[event.port._name] = event.port._data;
647     event.port.postMessage('cfg ' + name);
648     event.port.onmessage = getMessage;
649     sendMapData(event.port._data);
650     };
651    
652     function getMessage(event) {
653     switch (event.message.substr(0, 4)) {
654     case 'mov ':
655     var direction = event.message.substr(4);
656     var dx = 0;
657     var dy = 0;
658     switch (direction) {
659     case 'up': dy = -1; break;
660     case 'down': dy = 1; break;
661     case 'left': dx = -1; break;
662     case 'right': dx = 1; break;
663     }
664     event.target._data.x = wrapX(event.target._data.x + dx);
665     event.target._data.y = wrapY(event.target._data.y + dy);
666     sendMapData(event.target._data);
667     break;
668     case 'set ':
669     var value = event.message.substr(4);
670     map[event.target._data.y][event.target._data.x] = value;
671     for (var viewer in viewers)
672     sendMapData(viewers[viewer]);
673     break;
674     case 'txt ':
675     var name = event.target._name;
676     var message = event.message.substr(4);
677     for (var viewer in viewers)
678     viewers[viewer].port.postMessage('txt ' + name + ' ' + message);
679     break;
680     case 'msg ':
681     var party1 = event._data;
682     var party2 = viewers[event.message.substr(4).split(' ', 1)];
683     if (party2) {
684     var channel = new MessageChannel();
685     party1.port.postMessage('msg ' + party2.name, channel.port1);
686     party2.port.postMessage('msg ' + party1.name, channel.port2);
687     }
688     break;
689     }
690     }</pre>
691    
692     <p><strong>Connecting to multiple pages</strong>. Instead of using the
693     <code title=dom-WorkerGlobalScope-port><a href="#port">port</a></code>
694     global variable in the worker, the script uses the <code
695     title=handler-WorkerGlobalScope-onconnect><a
696     href="#onconnect">onconnect</a></code> event listener to listen for
697     multiple connections.
698    
699     <p><strong>Direct channels</strong>. When the worker receives a "msg"
700     message from one viewer naming another viewer, it sets up a direct
701     connection between the two, so that the two viewers can communicate
702     directly without the worker having to proxy all the messages.
703    
704     <p><a href="http://www.whatwg.org/demos/workers/multiviewer/page.html">View
705     this example online</a>.
706    
707     <h4 id=delegation><span class=secno>1.1.5 </span>Delegation</h4>
708    
709     <p><em>This section is non-normative.</em>
710    
711     <p>With multicore CPUs becoming prevalent, one way to obtain better
712     performance is to split computationally expensive tasks amongst multiple
713     workers. In this example, a computationally expensive task that is to be
714     performed for every number from 1 to 10,000,000 is farmed out to ten
715     subworkers.
716    
717     <p>The main page is as follows, it just reports the result:
718    
719     <pre>&lt;!DOCTYPE HTML>
720     &lt;html>
721     &lt;head>
722     &lt;title>Worker example: One-core computation&lt;/title>
723     &lt;/head>
724     &lt;body>
725     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
726     &lt;script>
727     var worker = createWorker('worker.js');
728 wakaba 1.9 worker.port.onmessage = function (event) {
729 wakaba 1.8 document.getElementById('result').textContent = event.message;
730     };
731     &lt;/script>
732     &lt;/body>
733     &lt;/html></pre>
734    
735     <p>The worker itself is as follows:
736    
737     <pre>// settings
738     var num_workers = 10;
739     var items_per_worker = 1000000;
740    
741     // start the workers
742     var result = 0;
743     var pending_workers = num_workers;
744     for (var i = 0; i &lt; num_workers; i += 1) {
745     var worker = createWorker('core.js');
746 wakaba 1.9 worker.port.postMessage(i * items_per_worker);
747     worker.port.postMessage((i+1) * items_per_worker);
748     worker.port.onmessage = storeResult;
749 wakaba 1.8 }
750    
751     // handle the results
752     function storeResult(event) {
753     result += 1*event.message;
754     pending_workers -= 1;
755     if (pending_workers &lt;= 0)
756     port.postMessage(result); // finished!
757     }</pre>
758    
759     <p>It consists of a loop to start the subworkers, and then a handler that
760     waits for all the subworkers to respond.
761    
762     <p>The subworkers are implemented as follows:
763    
764     <pre>var start;
765     onmessage = getStart;
766     function getStart(event) {
767     start = 1*event.message;
768     onmessage = getEnd;
769     }
770    
771     var end;
772     function getEnd(event) {
773     end = 1*event.message;
774     onmessage = null;
775     do();
776     }
777    
778     function do() {
779     var result = 0;
780     for (var i = start; i &lt; end; i += 1) {
781     // perform some complex calculation here
782     result += 1;
783     }
784     port.postMessage(result);
785     }</pre>
786    
787     <p>They receive two numbers in two events, perform the computation for the
788     range of numbers thus specified, and then report the result back to the
789     parent.
790    
791     <p><a href="http://www.whatwg.org/demos/workers/multicore/page.html">View
792     this example online</a>.
793    
794     <h4 id=granting><span class=secno>1.1.6 </span>Granting capabilities</h4>
795    
796     <p><em>This section is non-normative.</em>
797    
798     <p class=big-issue>...
799 wakaba 1.3
800 wakaba 1.6 <h3 id=conformance><span class=secno>1.2 </span>Conformance requirements</h3>
801 wakaba 1.1
802     <p>All diagrams, examples, and notes in this specification are
803     non-normative, as are all sections explicitly marked non-normative.
804     Everything else in this specification is normative.
805    
806     <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
807     NOT",-->
808     "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
809     normative parts of this document are to be interpreted as described in
810     RFC2119. For readability, these words do not appear in all uppercase
811     letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
812     <!-- XXX but they should be
813     marked up -->
814    
815     <p>Requirements phrased in the imperative as part of algorithms (such as
816     "strip any leading space characters" or "return false and abort these
817     steps") are to be interpreted with the meaning of the key word ("must",
818     "should", "may", etc) used in introducing the algorithm.
819    
820     <p>Some conformance requirements are phrased as requirements on attributes,
821     methods or objects. Such requirements are to be interpreted as
822     requirements on user agents.
823    
824     <p>Conformance requirements phrased as algorithms or specific steps may be
825     implemented in any manner, so long as the end result is equivalent. (In
826     particular, the algorithms defined in this specification are intended to
827     be easy to follow, and not intended to be performant.)
828    
829     <p>The only conformance class defined by this specification is user agents.
830    
831     <p>User agents may impose implementation-specific limits on otherwise
832     unconstrained inputs, e.g. to prevent denial of service attacks, to guard
833     against running out of memory, or to work around platform-specific
834     limitations.
835    
836 wakaba 1.6 <h4 id=dependencies><span class=secno>1.2.1 </span>Dependencies</h4>
837 wakaba 1.1
838     <p>This specification relies on several other underlying specifications.
839    
840     <dl>
841     <dt>HTML5
842    
843     <dd>
844     <p>Many fundamental concepts from HTML5 are used by this specification.
845     <a href="#refsHTML5">[HTML5]</a></p>
846    
847     <dt>ECMAScript
848    
849     <dd>
850     <p>This specification is intended to be used with JavaScript as the
851     scripting language. <a href="#refsJS">[JS]</a></p>
852    
853     <dt>WebIDL
854    
855     <dd>
856     <p>The IDL blocks in this specification use the semantics of the WebIDL
857     specification. <a href="#refsWebIDL">[WebIDL]</a></p>
858     </dl>
859    
860 wakaba 1.6 <h3 id=terminology><span class=secno>1.3 </span>Terminology</h3>
861 wakaba 1.1
862     <p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
863     <em>visible</em> might sometimes be used when referring to the way a
864     document is rendered to the user. These terms are not meant to imply a
865     visual medium; they must be considered to apply to other media in
866     equivalent ways.
867    
868 wakaba 1.3 <p>The construction "a <code title="">Foo</code> object", where <code
869     title="">Foo</code> is actually an interface, is sometimes used instead of
870     the more accurate "an object implementing the interface <code
871     title="">Foo</code>".
872 wakaba 1.1
873     <p>The term DOM is used to refer to the API set made available to scripts
874     in Web applications, and does not necessarily imply the existence of an
875     actual <code>Document</code> object or of any other <code>Node</code>
876     objects as defined in the DOM Core specifications. <a
877     href="#refsDOM3CORE">[DOM3CORE]</a>
878    
879     <p>A DOM attribute is said to be <em>getting</em> when its value is being
880     retrieved (e.g. by author script), and is said to be <em>setting</em> when
881     a new value is assigned to it.
882    
883     <p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means
884     that any attributes returning that object must always return the same
885     object (not a new object each time), and the attributes and methods on
886     that object must operate on the actual underlying data, not a snapshot of
887     the data.
888    
889 wakaba 1.5 <h2 id=infrastructure><span class=secno>2. </span>Infrastructure</h2>
890 wakaba 1.3
891 wakaba 1.7 <h3 id=the-workerglobalscope><span class=secno>2.1 </span>The <code><a
892     href="#workerglobalscope">WorkerGlobalScope</a></code> interface</h3>
893 wakaba 1.3
894     <pre
895 wakaba 1.7 class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
896     // core worker features
897     readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
898 wakaba 1.8 readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
899 wakaba 1.7 readonly attribute DOMString <a href="#name" title=dom-WorkerGlobalScope-name>name</a>;
900     readonly attribute boolean <a href="#closing" title=dom-WorkerGlobalScope-closing>closing</a>;
901     void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
902    
903 wakaba 1.4 // event handler attributes
904 wakaba 1.7 attribute <span>EventListener</span> <a href="#onconnect" title=handler-WorkerGlobalScope-onconnect>onconnect</a>;
905     attribute <span>EventListener</span> <a href="#onunload" title=handler-WorkerGlobalScope-onunload>onunload</a>;
906 wakaba 1.3 };</pre>
907    
908 wakaba 1.8 <p>Objects implementing the <code><a
909     href="#workerglobalscope">WorkerGlobalScope</a></code> interface must also
910     implement the <code>EventTarget</code> interface.
911    
912 wakaba 1.7 <p>The <dfn id=self
913     title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
914     return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
915     object itself.
916 wakaba 1.3
917 wakaba 1.8 <p>The <dfn id=location
918     title=dom-WorkerGlobalScope-location><code>location</code></dfn> attribute
919     must return the <code><a href="#workerlocation">WorkerLocation</a></code>
920     object created for the <code><a
921     href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
922     worker was created. It represents the <span>absolute URL</span> of the
923     script that was used to initialize the worker.
924 wakaba 1.7
925     <p>The <dfn id=name
926     title=dom-WorkerGlobalScope-name><code>name</code></dfn> attribute must
927     return the value it was assigned when the <code><a
928     href="#workerglobalscope">WorkerGlobalScope</a></code> object was created
929     by the "<a href="#run-a">run a worker</a>" algorithm. If it has a value
930     that isn't the empty string, its value represents the name that can be
931     used to obtain a reference to the worker using the <code
932 wakaba 1.9 title=dom-WorkerFactory-createSharedWorker><a
933     href="#createsharedworker">createSharedWorker()</a></code> method.
934 wakaba 1.3
935     <p>The <dfn id=closing
936 wakaba 1.7 title=dom-WorkerGlobalScope-closing><code>closing</code></dfn> attribute
937     must return false until it is set to true by one of the algorithms in the
938 wakaba 1.6 processing model section below.
939 wakaba 1.3
940 wakaba 1.4 <p>The following are the <span>event handler DOM attributes</span> that
941     must be supported by objects implementing the <code><a
942 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
943 wakaba 1.4
944     <dl>
945 wakaba 1.6 <dt><dfn id=onconnect
946 wakaba 1.7 title=handler-WorkerGlobalScope-onconnect><code>onconnect</code></dfn>
947 wakaba 1.4
948     <dd>
949     <p>Must be invoked whenever a <code
950 wakaba 1.7 title=event-WorkerGlobalScope-connect>connect</code> event is targeted
951     at or bubbles through the <code><a
952     href="#workerglobalscope">WorkerGlobalScope</a></code> object.
953 wakaba 1.4
954     <dt><dfn id=onunload
955 wakaba 1.7 title=handler-WorkerGlobalScope-onunload><code>onunload</code></dfn>
956 wakaba 1.4
957     <dd>
958     <p>Must be invoked whenever a <code title=event-unload>unload</code>
959     event is targeted at or bubbles through the <code><a
960 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object.
961 wakaba 1.4 </dl>
962    
963 wakaba 1.8 <p>In addition to the above members, the <a href="#success" title="worker
964     creation succeeded">worker creation success steps</a> add a <dfn id=port
965     title=dom-WorkerGlobalScope-port><code>port</code></dfn> property to the
966     object.
967    
968 wakaba 1.6 <h3 id=base-urls><span class=secno>2.2 </span>Base URLs and origins of
969     workers</h3>
970    
971     <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
972 wakaba 1.8 worker is the <span>absolute URL</span> given that the worker's <code
973     title=dom-WorkerGlobalScope-location><a
974     href="#location">location</a></code> attribute represents.
975 wakaba 1.6
976 wakaba 1.7 <p>Both the <span>origin</span> and <span>effective script origin</span> of
977     scripts running in workers are the <span>origin</span> of the
978 wakaba 1.8 <span>absolute URL</span> given that the worker's <code
979     title=dom-WorkerGlobalScope-location><a
980     href="#location">location</a></code> attribute represents.
981 wakaba 1.6
982     <h3 id=the-queue><span class=secno>2.3 </span>The queue of events</h3>
983 wakaba 1.3
984 wakaba 1.7 <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
985     object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
986     initially empty.
987 wakaba 1.3
988     <p>An event in the queue can be a DOM event or a timeout callback.
989    
990 wakaba 1.4 <p>All asynchronous callbacks and events that would be called or dispatched
991     in the worker must be added to the worker's queue, with the "<a
992     href="#run-a">run a worker</a>" processing model below taking care of
993     actually calling the callbacks or dispatching the events.
994    
995 wakaba 1.7 <p>Once the <code><a
996     href="#workerglobalscope">WorkerGlobalScope</a></code>'s <code
997     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
998 wakaba 1.4 attribute is set to true, the queue must discard anything else that would
999     be added to it. Effectively, once the <code
1000 wakaba 1.7 title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1001 wakaba 1.4 attribute is true, timers stop firing, notifications for all pending
1002     asynchronous operations are dropped, etc.
1003    
1004 wakaba 1.6 <h3 id=the-workers><span class=secno>2.4 </span>The worker's ports</h3>
1005 wakaba 1.4
1006     <p>Workers communicate with other workers and with <span title="browsing
1007     context">browsing contexts</span> through <span title="channel
1008     messaging">message channels</span> and their <code>MessagePort</code>
1009     objects.
1010    
1011 wakaba 1.7 <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1012     <var title="">worker</var> has a list of <dfn id=the-workers0>the worker's
1013 wakaba 1.4 ports</dfn>, which consists of all the <code>MessagePort</code> objects
1014     that are entangled with another port and that have one (but only one) port
1015 wakaba 1.7 owned by <var title="">worker</var>. This list includes all the
1016     <code>MessagePort</code> objects that are in events pending in the <a
1017     href="#queue">queue of events</a>.
1018 wakaba 1.4
1019 wakaba 1.6 <hr>
1020    
1021 wakaba 1.8 <p>A worker is said to be a <dfn id=permissible>permissible worker</dfn> if
1022     either:
1023    
1024     <ul>
1025     <li>at some point past or present a <code>MessagePort</code> owned by the
1026     worker was entangled with a <code>MessagePort</code> <var
1027     title="">p</var> whose owner is a <code>Window</code> object whose
1028     <span>active document</span> is the <code>Document</code> that was that
1029     <span>browsing context</span>'s <span>active document</span> when <var
1030     title="">p</var> was created, and that <code>Document</code> is
1031     <span>fully active</span>, or
1032    
1033     <li>at some point past or present a <code>MessagePort</code> owned by the
1034     worker was entangled with a <code>MessagePort</code> owned by another
1035     worker that is currently a <a href="#permissible">permissible worker</a>.
1036     </ul>
1037    
1038     <hr>
1039 wakaba 1.6
1040 wakaba 1.8 <p>A worker is said to be a <dfn id=protected>protected worker</dfn> if
1041     either:
1042 wakaba 1.6
1043     <ul>
1044 wakaba 1.8 <li>it has outstanding timers, database transactions, or network
1045     connections, and is a <a href="#permissible">permissible worker</a>, or:
1046    
1047     <li>there is a <a href="#protected">protected worker</a> that at some
1048     point past or present owned a <code>MessagePort</code> that was entangled
1049     with a <code>MessagePort</code> owned by this worker.
1050     </ul>
1051    
1052     <hr>
1053    
1054     <p>A worker is said to be an <dfn id=active>active needed worker</dfn> if
1055     either:
1056    
1057     <ul>
1058     <li>the worker is a <a href="#protected">protected worker</a>, or
1059    
1060     <li>at least one of the <a href="#the-workers0">the worker's ports</a> is
1061     entangled with a <code>MessagePort</code> <var title="">p</var> whose
1062     owner is a <code>Window</code> object whose <span>active document</span>
1063     is the <code>Document</code> that was that <span>browsing
1064     context</span>'s <span>active document</span> when that
1065     <code>MessagePort</code> <var title="">p</var> was created, and that
1066     <code>Document</code> is <span>fully active</span>, or
1067 wakaba 1.6
1068     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1069 wakaba 1.7 an entangled <code>MessagePort</code> owned by a <code><a
1070     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1071 wakaba 1.8 itself an <a href="#active">active needed worker</a>.
1072 wakaba 1.6 </ul>
1073    
1074     <hr>
1075    
1076 wakaba 1.8 <p>A worker is said to be a <dfn id=suspendable>suspendable worker</dfn> if
1077     it is not an <a href="#active">active needed worker</a> but either:
1078 wakaba 1.6
1079     <ul>
1080 wakaba 1.8 <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1081     an entangled <code>MessagePort</code> owned by a <code>Window</code>
1082     object, or
1083 wakaba 1.6
1084     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1085 wakaba 1.7 an entangled <code>MessagePort</code> owned by a <code><a
1086     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1087 wakaba 1.8 itself a <span>needed worker</span>.
1088 wakaba 1.6 </ul>
1089    
1090     <h3 id=processing><span class=secno>2.5 </span>Processing model</h3>
1091 wakaba 1.3
1092     <p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var
1093 wakaba 1.4 title="">name</var> for a script with <span>URL</span> <var
1094     title="">url</var>, a browsing context <var title="">owner browsing
1095     context</var> and a <code>Document</code> <var title="">owner
1096     document</var>, it must run the following steps in a completely separate
1097     and parallel execution environment:
1098 wakaba 1.3
1099     <ol>
1100     <li>
1101 wakaba 1.4 <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
1102     <var title="">url</var>.</p>
1103    
1104     <p>If the attempt fails, then abort these steps and invoke the <a
1105 wakaba 1.9 href="#worker2" title="worker creation failed">error handling steps</a>
1106 wakaba 1.4 defined by the algorithm that called this one.</p>
1107    
1108     <p>If the attempt succeeds, then let <var title="">script</var> be the
1109     resource that was obtained.</p>
1110    
1111     <p class=note>As with <code>script</code> elements, the MIME type of the
1112     script is ignored. Unlike with <code>script</code> elements, there is no
1113     way to override the type. It's always assumed to be JavaScript.</p>
1114     <!-- XXX people will complain about
1115     this. I guess we might want to examine the MIME type... -->
1116    
1117    
1118     <li>
1119 wakaba 1.7 <p>Create a new <code><a
1120     href="#workerglobalscope">WorkerGlobalScope</a></code> object, <var
1121     title="">worker</var>.</p>
1122 wakaba 1.3
1123     <li>
1124 wakaba 1.7 <p>Set the <code title=dom-WorkerGlobalScope-name><a
1125 wakaba 1.6 href="#name">name</a></code> attribute of <var title="">worker</var> to
1126 wakaba 1.3 the value of <var title="">name</var>.</p>
1127    
1128     <li>
1129 wakaba 1.8 <p>Create a new <code><a href="#workerlocation">WorkerLocation</a></code>
1130     object for the <code title=dom-WorkerGlobalScope-location><a
1131     href="#location">location</a></code> attribute of <var
1132     title="">worker</var>, representing <var title="">url</var>.</p>
1133    
1134     <li>
1135 wakaba 1.3 <p>Let <var title="">script</var>'s <span>script execution context</span>
1136     (and thus also <span>global object</span>) be <var
1137 wakaba 1.6 title="">worker</var>.</p>
1138 wakaba 1.3
1139     <li>
1140     <p>Let <var title="">script</var>'s <span>script browsing context</span>
1141     be <var title="">owner browsing context</var>.</p>
1142    
1143     <li>
1144     <p>Let <var title="">script</var>'s <span>script document context</span>
1145     be <var title="">owner document</var>.</p>
1146    
1147     <li>
1148 wakaba 1.5 <p>Invoke the <a href="#success" title="worker creation
1149     succeeded">success steps</a> defined by the algorithm that called this
1150 wakaba 1.6 one.</p>
1151    
1152     <p class=note>This will usually add an event to the <a
1153 wakaba 1.8 href="#queue">queue of events</a> and set the <code
1154     title=dom-WorkerGlobalScope-port><a href="#port">port</a></code>
1155     property on the <var title="">worker</var> object. If it does, that
1156     event will have a <code>MessagePort</code> and thus the list of <a
1157 wakaba 1.6 href="#the-workers0">the worker's ports</a> will not be empty. If it
1158     doesn't, then the next step will set the <var title="">worker</var>
1159 wakaba 1.7 object's <code title=dom-WorkerGlobalScope-closing><a
1160 wakaba 1.6 href="#closing">closing</a></code> attribute to true.</p>
1161    
1162     <li>
1163 wakaba 1.8 <p><strong>Closing orphan workers</strong>: Start monitoring <var
1164     title="">worker</var>, such that as soon as the worker stops being
1165     either an <a href="#active">active needed worker</a> or a <a
1166     href="#suspendable">suspendable worker</a>, the <var
1167 wakaba 1.7 title="">worker</var> object's <code
1168     title=dom-WorkerGlobalScope-closing><a
1169 wakaba 1.9 href="#closing">closing</a></code> attribute is set to true and an event
1170     named <code title=event-unload>unload</code>, which uses the
1171     <code>Event</code> object, which does not bubble, and which is not
1172     cancelable, is added it to the worker's <code><a
1173     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1174     href="#queue">queue of events</a>, targetted at the <code><a
1175     href="#workerglobalscope">WorkerGlobalScope</a></code> object.</p>
1176 wakaba 1.4
1177     <li>
1178 wakaba 1.8 <p><strong>Suspending workers</strong>: Start monitoring <var
1179     title="">worker</var>, such that whenever the <var title="">worker</var>
1180     object's <code title=dom-WorkerGlobalScope-closing><a
1181     href="#closing">closing</a></code> attribute is false and the worker is
1182     a <a href="#suspendable">suspendable worker</a>, the user agent suspends
1183     execution of script in that worker until such time as either the <code
1184 wakaba 1.7 title=dom-WorkerGlobalScope-closing><a
1185     href="#closing">closing</a></code> attribute switches to true or the
1186 wakaba 1.8 worker stops being a <a href="#suspendable">suspendable worker</a>.</p>
1187 wakaba 1.4
1188     <li>
1189     <p>Run <var title="">script</var> until it either returns, fails to catch
1190     an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
1191     a worker</a>" algorithm below.</p>
1192    
1193     <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
1194     worker</a>" algorithm, then that same algorithm will cause there to only
1195     be a single event in the <a href="#queue">queue of events</a> at the
1196 wakaba 1.8 next step, namely the <code title=message-unload>unload</code> event.</p>
1197 wakaba 1.4
1198     <li>
1199     <p><i>Event loop</i>: Wait until either there is an event in the <a
1200 wakaba 1.3 href="#queue">queue of events</a> associated with <var
1201 wakaba 1.6 title="">worker</var> or the <var title="">worker</var> object's <code
1202 wakaba 1.7 title=dom-WorkerGlobalScope-closing><a
1203     href="#closing">closing</a></code> attribute is set to true.</p>
1204 wakaba 1.3
1205     <li>
1206     <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1207 wakaba 1.4 events</a>, if any. The handling of this event or the execution of this
1208     callback might get prematurely aborted by the "<a href="#kill-a">kill a
1209     worker</a>" algorithm below.</p>
1210 wakaba 1.3
1211     <li>
1212     <p>If there are any more events in the <a href="#queue">queue of
1213 wakaba 1.6 events</a> or if the <var title="">worker</var> object's <code
1214 wakaba 1.7 title=dom-WorkerGlobalScope-closing><a
1215     href="#closing">closing</a></code> attribute is set to false, then jump
1216     back to the step above labeled <i>event loop</i>.</p>
1217 wakaba 1.3
1218     <li>
1219     <p class=big-issue>timers, intervals, XMLHttpRequests, database
1220 wakaba 1.8 transactions, etc, must be killed; ports must be unentangled</p>
1221 wakaba 1.9
1222     <li>
1223     <p>At the next available opportunity, after any scripts have finished
1224     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1225     <code title=event-unload>unload</code> at any <code><a
1226     href="#worker1">Worker</a></code> objects associated with this worker.</p>
1227 wakaba 1.3 </ol>
1228    
1229     <hr>
1230    
1231     <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run
1232     the following steps in parallel with the worker's main loop (the "<a
1233     href="#run-a">run a worker</a>" processing model defined above):
1234    
1235     <ol>
1236     <li>
1237     <p>Create an <code>Event</code> object with the event name <code
1238     title=event-unload>unload</code>, which does not bubble and is not
1239     cancelable, and add it to the worker's <code><a
1240 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1241 wakaba 1.4 href="#queue">queue of events</a>, targetted at the <code><a
1242 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1243 wakaba 1.8 </li>
1244     <!-- XXX shouldn't add one if closing is
1245     already true, assuming unload has already been added to the queue
1246     (?) -->
1247 wakaba 1.4
1248     <li>
1249 wakaba 1.7 <p>Set the worker's <code><a
1250     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
1251     title=dom-WorkerGlobalScope-closing><a
1252 wakaba 1.4 href="#closing">closing</a></code> attribute to true.
1253 wakaba 1.3
1254     <li>
1255 wakaba 1.4 <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
1256     a worker</a>" processing model defined above immediately starts running
1257 wakaba 1.8 event listeners registered for the <code
1258     title=event-unload>unload</code> event, this time should not be zero
1259     &mdash; the idea is that the <code title=event-unload>unload</code>
1260     event can be used to clean up when shutting down unexpectedly.
1261 wakaba 1.3
1262     <li>
1263     <p>If there are any events in the <a href="#queue">queue of events</a>
1264     other than the <code title=event-unload>unload</code> event that this
1265     algorithm just added, discard them without dispatching them.
1266    
1267     <li>
1268     <p>If the <code title=event-unload>unload</code> event that this
1269     algorithm just added hasn't yet been dispatched, then abort the script
1270     currently running in the worker.
1271    
1272     <li>
1273     <p>Wait a user-agent-defined amount of time.
1274    
1275     <li>
1276     <p>Abort the script currently running in the worker (if any script is
1277     running, then it will be a handler for the <code
1278     title=event-unload>unload</code> event).
1279     </ol>
1280    
1281 wakaba 1.6 <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
1282     processing model on a worker at any time, e.g. in response to user
1283     requests, in response to CPU quota management, or when a worker stops
1284 wakaba 1.8 being an <a href="#active">active needed worker</a> if the worker
1285     continues executing even after its <code
1286     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1287     attribute was set to true.
1288 wakaba 1.6
1289     <hr>
1290    
1291 wakaba 1.4 <p>When a script invokes the <dfn id=close
1292 wakaba 1.7 title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
1293     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
1294     the user agent must run the following steps:
1295 wakaba 1.4
1296     <ol>
1297     <li>
1298     <p>Create an <code>Event</code> object with the event name <code
1299     title=event-unload>unload</code>, which does not bubble and is not
1300     cancelable, and add it to the <code><a
1301 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1302 wakaba 1.4 href="#queue">queue of events</a>, targetted at the <code><a
1303 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1304 wakaba 1.4
1305     <li>
1306 wakaba 1.7 <p>Set the worker's <code><a
1307     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
1308     title=dom-WorkerGlobalScope-closing><a
1309 wakaba 1.4 href="#closing">closing</a></code> attribute to true.
1310    
1311     <li>
1312     <p>For each <code>MessagePort</code> object that is entangled with
1313 wakaba 1.7 another port and that has one (but only one) port whose owner is the
1314     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1315     on which the method was invoked, run the following substeps:</p>
1316 wakaba 1.4
1317     <ol>
1318     <li>
1319     <p>Unentangle the two ports.
1320    
1321     <li>
1322     <p>At the next available opportunity, after any scripts have finished
1323     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1324     <code title=event-unload>unload</code> at the other port (the one
1325 wakaba 1.7 whose owner is not the <code><a
1326     href="#workerglobalscope">WorkerGlobalScope</a></code> object on which
1327     the <code title=dom-WorkerGlobalScope-close><a
1328 wakaba 1.4 href="#close">close()</a></code> method was called).
1329     </ol>
1330     </ol>
1331    
1332 wakaba 1.6 <h3 id=creating><span class=secno>2.6 </span>Creating workers</h3>
1333 wakaba 1.3
1334     <pre
1335 wakaba 1.7 class=idl>[NoInterfaceObject] interface <dfn id=workerfactory>WorkerFactory</dfn> {
1336 wakaba 1.9 <a href="#worker1">Worker</a> <a href="#createworker" title=dom-WorkerFactory-createWorker>createWorker</a>(in DOMString scriptURL);
1337     <a href="#worker1">Worker</a> <a href="#createsharedworker" title=dom-WorkerFactory-createSharedWorker>createSharedWorker</a>(in DOMString name, in DOMString scriptURL);
1338     };
1339    
1340     interface <dfn id=worker1>Worker</dfn> {
1341     <span>MessagePort</span> <a href="#port0" title=dom-Worker-port>port</a>();
1342    
1343     // event handler attributes
1344     attribute <span>EventListener</span> <a href="#onload" title=handler-Worker-onload>onload</a>;
1345     attribute <span>EventListener</span> <a href="#onerror" title=handler-Worker-onerror>onerror</a>;
1346     attribute <span>EventListener</span> <a href="#onunload0" title=handler-Worker-onunload>onunload</a>;
1347 wakaba 1.3 };</pre>
1348    
1349 wakaba 1.7 <p>Objects that implement the <code>Window</code> and <code><a
1350 wakaba 1.9 href="#workerglobalscope">WorkerGlobalScope</a></code> interfaces must
1351     also implement the <code><a href="#workerfactory">WorkerFactory</a></code>
1352     interface.
1353 wakaba 1.4
1354     <hr>
1355    
1356 wakaba 1.5 <p>When the <dfn id=createworker
1357 wakaba 1.7 title=dom-WorkerFactory-createWorker><code>createWorker(<var
1358 wakaba 1.5 title="">scriptURL</var>)</code></dfn> method is invoked, the user agent
1359     must run the following steps:
1360 wakaba 1.4
1361     <ol>
1362 wakaba 1.5 <li>
1363     <p><span title="resolve a url">Resolve</span> the <var
1364     title="">scriptURL</var> argument.
1365    
1366     <li>
1367     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1368    
1369     <li>
1370     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1371     is not the <span title="same origin">same</span> as the origin of the
1372     script that invoked the method, then throw a <span>security
1373     exception</span>.
1374    
1375     <li>
1376     <p><a href="#create">Create a worker</a> from the resulting
1377     <span>absolute URL</span> whose name is the empty string.
1378    
1379     <li>
1380 wakaba 1.9 <p>Return the <code><a href="#worker1">Worker</a></code> object returned
1381     from the <a href="#create">create a worker</a> algorithm.
1382 wakaba 1.4 </ol>
1383    
1384     <hr>
1385    
1386 wakaba 1.9 <p>When the <dfn id=createsharedworker
1387     title=dom-WorkerFactory-createSharedWorker><code>createSharedWorker(<var
1388 wakaba 1.5 title="">name</var>, <var title="">scriptURL</var>)</code></dfn> method is
1389     invoked, the user agent must run the following steps:
1390 wakaba 1.4
1391     <ol>
1392 wakaba 1.5 <li>
1393     <p><span title="resolve a url">Resolve</span> the <var
1394     title="">scriptURL</var> argument.
1395    
1396     <li>
1397     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1398    
1399     <li>
1400     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1401     is not the <span title="same origin">same</span> as the origin of the
1402     script that invoked the method, then throw a <span>security
1403     exception</span>.
1404    
1405     <li>
1406     <p>If the <var title="">name</var> argument is the empty string, <a
1407     href="#create">create a worker</a> from the resulting <span>absolute
1408 wakaba 1.9 URL</span>, whose name is the empty string, and return the <code><a
1409     href="#worker1">Worker</a></code> object returned from the <a
1410 wakaba 1.5 href="#create">create a worker</a> algorithm. Then, abort these steps.
1411    
1412     <li>
1413 wakaba 1.7 <p>If there exists a worker whose <code
1414     title=dom-WorkerGlobalScope-closing><a
1415 wakaba 1.5 href="#closing">closing</a></code> attribute is false, whose <code
1416 wakaba 1.7 title=dom-WorkerGlobalScope-name><a href="#name">name</a></code>
1417     attribute is exactly equal to the <var title="">name</var> argument, and
1418 wakaba 1.8 whose <code title=dom-WorkerGlobalScope-location><a
1419     href="#location">location</a></code> attribute represents an
1420     <span>absolute URL</span> that has the <span>same origin</span> as the
1421     resulting <span>absolute URL</span>, then run these substeps:</p>
1422 wakaba 1.5
1423     <ol>
1424     <li>
1425 wakaba 1.8 <p>If that worker's <code title=dom-WorkerGlobalScope-location><a
1426     href="#location">location</a></code> attribute represents an
1427     <span>absolute URL</span> that is not exactly equal to the resulting
1428     <span>absolute URL</span>, then throw a <code>URL_MISMATCH_ERR</code>
1429     exception and abort these steps. <span class=big-issue>code 19</span>
1430 wakaba 1.5
1431     <li>
1432     <p><span>Create a new <code>MessagePort</code> object</span> owned by
1433     the <span>script execution context</span> of the script that invoked
1434     the method.
1435    
1436     <li>
1437 wakaba 1.9 <p><span>Create a new <code><a href="#worker1">Worker</a></code>
1438     object</span> associated with that worker, and assign the newly
1439     created port to its <code title=dom-Worker-port><a
1440     href="#port0">port</a></code> attribute.
1441    
1442     <li>
1443     <p>Return the newly created <code><a href="#worker1">Worker</a></code>
1444     object.
1445 wakaba 1.5
1446     <li>
1447 wakaba 1.6 <p>Asynchronously, <a href="#connect" title="connect to a
1448     worker">connect</a> to this preexisting worker, with the newly created
1449 wakaba 1.9 <code>MessagePort</code> and <code><a
1450     href="#worker1">Worker</a></code> objects.
1451 wakaba 1.5 </ol>
1452    
1453     <p>Otherwise, <a href="#create">create a worker</a> from the resulting
1454     <span>absolute URL</span>, whose name is the value of the <var
1455 wakaba 1.9 title="">name</var> argument, and return the <code><a
1456     href="#worker1">Worker</a></code> object returned from the <a
1457     href="#create">create a worker</a> algorithm.</p>
1458 wakaba 1.4 </ol>
1459    
1460     <hr>
1461    
1462 wakaba 1.5 <p>The steps to <dfn id=create>create a worker</dfn> from a
1463     <span>URL</span> <var title="">url</var> and whose name is <var
1464     title="">name</var>, in the context of a method call, are as follows:
1465 wakaba 1.4
1466     <ol>
1467 wakaba 1.5 <li>
1468     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1469     <span>script execution context</span> of the script that invoked the
1470     method.
1471    
1472     <li>
1473 wakaba 1.9 <p><span>Create a new <code><a href="#worker1">Worker</a></code>
1474     object</span>, associated with the worker that will be created below,
1475     and assign the newly created port to its <code title=dom-Worker-port><a
1476     href="#port0">port</a></code> attribute.
1477    
1478     <li>
1479     <p>Return that <code><a href="#worker1">Worker</a></code> object.
1480 wakaba 1.5
1481     <li>
1482     <p>In a parallel execution context (i.e. a separate thread or process or
1483     equivalent construct), <a href="#run-a">run a worker</a> named <var
1484     title="">name</var> for the script with <span>URL</span> <var
1485     title="">url</var>, with the <span>script browsing context</span> of the
1486     script that invoked the method as the <var title="">owner browsing
1487     context</var> and with the <span>script document context</span> of the
1488     script that invoked the method as the <var title="">owner
1489     document</var>.</p>
1490    
1491     <p>If that algorithm invokes the steps for <dfn id=success title="worker
1492 wakaba 1.6 creation succeeded">success steps</dfn>, then <a href="#connect"
1493     title="connect to a worker">connect</a> to this new worker, with the
1494 wakaba 1.8 newly created port, and if that algorithm returns a second new port,
1495     then add a new property <code title=dom-WorkerGlobalScope-port><a
1496     href="#port">port</a></code> on the new worker's <code><a
1497     href="#workerglobalscope">WorkerGlobalScope</a></code> object, whose
1498 wakaba 1.9 value is the <code>MessagePort</code> object returned by the <a
1499 wakaba 1.8 href="#connect">connect to a worker</a> algorithm.</p>
1500 wakaba 1.5
1501 wakaba 1.9 <p>Otherwise, if the <dfn id=worker2>worker creation failed</dfn>, then
1502 wakaba 1.8 at the next available opportunity, after any scripts have finished
1503 wakaba 1.5 executing<!-- XXX queue -->, <span>fire a simple event</span> called
1504 wakaba 1.9 <code title=event-error>error</code> at the newly created <code><a
1505     href="#worker1">Worker</a></code> object.</p>
1506 wakaba 1.4 </ol>
1507    
1508     <hr>
1509    
1510 wakaba 1.6 <p>The steps to <dfn id=connect>connect to a worker</dfn> given a
1511 wakaba 1.9 <code>MessagePort</code> object <var title="">port</var> and a <code><a
1512     href="#worker1">Worker</a></code> object <var title="">worker</var> are as
1513     follows:
1514 wakaba 1.4
1515     <ol>
1516 wakaba 1.5 <li>
1517     <p>If <var title="">port</var> would have been garbage collected, or if
1518 wakaba 1.7 the <span>active document</span> of the owner of <var
1519 wakaba 1.5 title="">port</var> is no longer the same <code>Document</code> object
1520     as when <var title="">port</var> was created, then do nothing. Abort
1521     these steps. If the worker was just created, it'll get killed
1522     immediately.</p>
1523    
1524     <li>
1525     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1526 wakaba 1.7 <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> of the
1527     worker.
1528 wakaba 1.5
1529     <li>
1530     <p><span>Entangle</span> this newly created port and the port <var
1531     title="">port</var> that was passed to these steps.
1532    
1533     <li>
1534     <p>At the next available opportunity, after any scripts have finished
1535     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1536 wakaba 1.9 <code title=event-load>load</code> at <var title="">worker</var>.
1537 wakaba 1.5
1538     <li>
1539     <p>Create an event that uses the <code>MessageEvent</code> interface,
1540 wakaba 1.6 with the name <code title=event-connect>connect</code>, which does not
1541 wakaba 1.5 bubble, is cancelable, has no default action, has a <code
1542     title=dom-MessageEvent-data>data</code> attribute whose value is the
1543     empty string and has a <code
1544     title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
1545     value is the newly created port, and add it to the worker's <code><a
1546 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1547 wakaba 1.5 href="#queue">queue of events</a>, targetted at the <code><a
1548 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1549 wakaba 1.8
1550     <li>
1551     <p>Return the newly created port.
1552 wakaba 1.4 </ol>
1553 wakaba 1.3
1554 wakaba 1.9 <hr>
1555    
1556     <p>The <dfn id=port0 title=dom-Worker-port><code>port</code></dfn>
1557     attribute on a <code><a href="#worker1">Worker</a></code> object must
1558     return the value it was assigned when the <code><a
1559     href="#worker1">Worker</a></code> object was created by the methods on the
1560     <code><a href="#workerfactory">WorkerFactory</a></code> interface.
1561    
1562     <p>The following are the <span>event handler DOM attributes</span> that
1563     must be supported by objects implementing the <code><a
1564     href="#worker1">Worker</a></code> interface:
1565    
1566     <dl>
1567     <dt><dfn id=onload title=handler-Worker-onload><code>onload</code></dfn>
1568    
1569     <dd>
1570     <p>Must be invoked whenever a <code title=event-load>load</code> event is
1571     targeted at or bubbles through the <code><a
1572     href="#worker1">Worker</a></code> object.
1573    
1574     <dt><dfn id=onerror
1575     title=handler-Worker-onerror><code>onerror</code></dfn>
1576    
1577     <dd>
1578     <p>Must be invoked whenever an <code title=event-error>error</code> event
1579     is targeted at or bubbles through the <code><a
1580     href="#worker1">Worker</a></code> object.
1581    
1582     <dt><dfn id=onunload0
1583     title=handler-Worker-onunload><code>onunload</code></dfn>
1584    
1585     <dd>
1586     <p>Must be invoked whenever an <code title=event-unload>unload</code>
1587     event is targeted at or bubbles through the <code><a
1588     href="#worker1">Worker</a></code> object.
1589     </dl>
1590    
1591 wakaba 1.5 <h2 id=apis-available><span class=secno>3. </span>APIs available to workers</h2>
1592    
1593 wakaba 1.7 <pre
1594     class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1595 wakaba 1.9 void <a href="#importscripts" title=dom-WorkerGlobalScope-importScripts>importScripts</a>([Variadic] in DOMString urls);
1596 wakaba 1.7 readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
1597     <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
1598     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
1599     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
1600     };</pre>
1601 wakaba 1.6
1602 wakaba 1.5 <p>Objects that implement the <code><a
1603 wakaba 1.9 href="#workerglobalscope">WorkerGlobalScope</a></code> interface must also
1604     implement the <code><a href="#workerutils">WorkerUtils</a></code>
1605     interface.
1606 wakaba 1.3
1607 wakaba 1.6 <p class=big-issue>Need to define a sync database API.
1608    
1609     <p class=big-issue>May need to define a browser sniffing API (like
1610     window.navigator).</p>
1611     <!-- XXX ApplicationCache -->
1612     <!-- XXX a way to set cookies on the URL for the script -->
1613     <!-- XXX debugging: void log(in DOMString s); // log to console -->
1614     <!-- XXX debugging: onerror -->
1615    
1616     <hr>
1617    
1618     <p>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects,
1619     etc) are not available to workers in this version of this specification.
1620    
1621 wakaba 1.7 <h3 id=importing><span class=secno>3.1 </span>Importing scripts and
1622     libraries</h3>
1623    
1624 wakaba 1.9 <p>When a script invokes the <dfn id=importscripts
1625     title=dom-WorkerGlobalScope-importScripts><code>importScripts(<var
1626     title="">urls</var>)</code></dfn> method on a <code><a
1627 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
1628     agent must run the following steps:
1629    
1630     <ol>
1631     <li>
1632 wakaba 1.9 <p>If there are no arguments, return without doing anything. Abort these
1633     steps.
1634    
1635     <li>
1636     <p><span title="resolve a url">Resolve</span> each argument.
1637 wakaba 1.7
1638     <li>
1639 wakaba 1.9 <p>If any fail, throw a <code>SYNTAX_ERR</code> exception.
1640    
1641     <li>
1642     <p>If any of the resulting <span title="absolute URL">absolute
1643     URLs</span> have an <span>origin</span> that is not the <span
1644     title="same origin">same</span> as the origin of the script that invoked
1645     the method, then throw a <span>security exception</span>.
1646 wakaba 1.7
1647     <li>
1648 wakaba 1.9 <p>Attempt to <span>fetch</span> each resource identified by the
1649     resulting <span title="absolute URLs">absolute URL</span>.</p>
1650 wakaba 1.7
1651     <li>
1652 wakaba 1.9 <p>For each argument in turn, in the order given, starting with the first
1653     one, run these substeps:</p>
1654 wakaba 1.7
1655 wakaba 1.9 <ol>
1656     <li>
1657     <p>Wait for the fetching attempt for the corresponding resource to
1658     complete.</p>
1659 wakaba 1.7
1660 wakaba 1.9 <p>If the fetching attempt failed, throw a <code>NETWORK_ERR</code>
1661     exception and abort all these steps.</p>
1662 wakaba 1.7
1663 wakaba 1.9 <p>If the fetching attempt succeeded, then let <var
1664     title="">script</var> be the resource that was obtained.</p>
1665 wakaba 1.7
1666 wakaba 1.9 <p class=note>As with the worker's script, the script here is always
1667     assumed to be JavaScript, regardless of the MIME type.</p>
1668     <!-- XXX -->
1669 wakaba 1.7
1670 wakaba 1.9 <li>
1671     <p>Let <var title="">script</var>'s <span>script execution
1672     context</span>, <span>script browsing context</span>, and <span>script
1673     document context</span> be the same as for the script that was
1674     executed by the <a href="#run-a">run a worker</a> processing model for
1675     this worker.</p>
1676 wakaba 1.7
1677 wakaba 1.9 <li>
1678     <p>Run <var title="">script</var> until it either returns, fails to
1679     catch an exception, or gets prematurely aborted by the "<a
1680     href="#kill-a">kill a worker</a>" algorithm above.</p>
1681    
1682     <p>If an exception was raised or if the script was prematurely aborted,
1683     then abort all these steps, letting the exception or aborting continue
1684     to be processed by the script that called the <code
1685     title=dom-WorkerGlobalScope-importScripts><a
1686     href="#importscripts">importScripts()</a></code> method.</p>
1687 wakaba 1.7
1688 wakaba 1.9 <p>If the "<a href="#kill-a">kill a worker</a>" algorithm aborts the
1689     script then abort all these steps.</p>
1690     </ol>
1691 wakaba 1.7 </ol>
1692    
1693     <h3 id=apis-defined><span class=secno>3.2 </span>APIs defined in other
1694     specifications</h3>
1695    
1696     <p>The <dfn id=localstorage
1697     title=dom-localStorage><code>localStorage</code></dfn>, <dfn
1698     id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
1699     must act as defined for the APIs with the same names on the
1700     <code>Window</code> object in the HTML5 specification, with the exception
1701     that where the API would use the <span>origin</span> of the <span>active
1702     document</span> of the <span>browsing context</span> of the
1703     <code>Window</code> object on which the method was supposedly invoked, it
1704     must instead use the <span>origin</span> of the script that invoked the
1705     method. <a href="#refsHTML5">[HTML5]</a>
1706    
1707     <p>The <dfn id=shownotification
1708     title=dom-showNotification><code>showNotification()</code></dfn> methods
1709     must act as defined for the APIs with the same names on the
1710     <code>Window</code> object in the HTML5 specification. <a
1711     href="#refsHTML5">[HTML5]</a>
1712    
1713     <h3 id=interface><span class=secno>3.3 </span>Interface objects and
1714     constructors</h3>
1715    
1716     <p>There must be no interface objects and constructors available in the
1717     global scope of scripts whose <span>script execution context</span> is a
1718     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1719     except for the following:
1720    
1721     <ul>
1722     <li>
1723     <p><code>XMLHttpRequest</code> and all interface objects and constructors
1724     defined by the XMLHttpRequest specifications, except that the
1725     <span>document response entity body</span> must always be null. <a
1726     href="#refsXHR">[XHR]</a>
1727    
1728     <li>
1729     <p>The <code>WebSocket</code> interface object and constructor.
1730    
1731     <li>
1732     <p>The <code>MessageChannel</code> interface object and constructor.
1733     </ul>
1734 wakaba 1.8
1735     <h3 id=worker0><span class=secno>3.4 </span>Worker locations</h3>
1736    
1737     <pre
1738     class=idl>[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
1739     readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
1740     readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
1741     readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
1742     readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
1743 wakaba 1.9 readonly attribute DOMString <a href="#port1" title=dom-WorkerLocation-port>port</a>;
1744 wakaba 1.8 readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
1745     readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
1746     readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
1747     };</pre>
1748    
1749     <p>A <code><a href="#workerlocation">WorkerLocation</a></code> object
1750     represents an <span>absolute URL</span> set at its creation.
1751    
1752     <p>The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
1753     attribute must return the <span>absolute URL</span> that the object
1754     represents.
1755    
1756     <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface
1757     also has the complement of <span>URL decomposition attributes</span>, <dfn
1758     id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
1759     <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
1760 wakaba 1.9 id=port1 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
1761 wakaba 1.8 id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
1762     <dfn id=pathname
1763     title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
1764     id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
1765     <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
1766     must follow the rules given for URL decomposition attributes, with the
1767     <span title=concept-uda-input>input</span> being the <span>absolute
1768     URL</span> that the object represents (same as the <code
1769     title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
1770     and the <span title=concept-uda-setter>common setter action</span> being a
1771     no-op, since the attributes are defined to be readonly. <a
1772     href="#refsHTML5">[HTML5]</a>
1773 wakaba 1.7
1774 wakaba 1.1 <h2 class=no-num id=references>References</h2>
1775    
1776     <p class=big-issue>This section will be written in a future
1777     draft.<!--XXX-->
1778    
1779     <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
1780     <!-- ACKS -->
1781    
1782 wakaba 1.7 <p>Thanks to Aaron Boodman, Jonas Sicking, Maciej Stachowiak, Mike Smith,
1783     and Philip Taylor for their useful and substantial comments.
1784 wakaba 1.3
1785     <p>Huge thanks to the whole Gears team, who pioneered this technology and
1786     whose experience has been a huge influence on this specification.

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24