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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24