/[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.16 - (hide annotations) (download) (as text)
Thu Nov 20 06:18:02 2008 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.15: +5 -5 lines
File MIME type: text/html
make

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24