/[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.18 - (hide annotations) (download) (as text)
Tue Dec 16 06:18:35 2008 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.17: +168 -197 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.18 <h2 class="no-num no-toc" id=draft>Draft Recommendation &mdash; 16
20     December 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 wakaba 1.18 <li><a href="#the-event"><span class=secno>2.3 </span>The event loop</a>
163    
164 wakaba 1.3
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 wakaba 1.18 letters in this specification. <a href="#refsRFC2119">[RFC2119]</a>
1077 wakaba 1.1
1078     <p>Requirements phrased in the imperative as part of algorithms (such as
1079     "strip any leading space characters" or "return false and abort these
1080     steps") are to be interpreted with the meaning of the key word ("must",
1081     "should", "may", etc) used in introducing the algorithm.
1082    
1083     <p>Some conformance requirements are phrased as requirements on attributes,
1084     methods or objects. Such requirements are to be interpreted as
1085     requirements on user agents.
1086    
1087     <p>Conformance requirements phrased as algorithms or specific steps may be
1088     implemented in any manner, so long as the end result is equivalent. (In
1089     particular, the algorithms defined in this specification are intended to
1090     be easy to follow, and not intended to be performant.)
1091    
1092     <p>The only conformance class defined by this specification is user agents.
1093    
1094     <p>User agents may impose implementation-specific limits on otherwise
1095     unconstrained inputs, e.g. to prevent denial of service attacks, to guard
1096     against running out of memory, or to work around platform-specific
1097     limitations.
1098    
1099 wakaba 1.14 <h4 id=dependencies><span class=secno>1.3.1 </span>Dependencies</h4>
1100 wakaba 1.1
1101     <p>This specification relies on several other underlying specifications.
1102    
1103     <dl>
1104     <dt>HTML5
1105    
1106     <dd>
1107     <p>Many fundamental concepts from HTML5 are used by this specification.
1108     <a href="#refsHTML5">[HTML5]</a></p>
1109    
1110     <dt>ECMAScript
1111    
1112     <dd>
1113     <p>This specification is intended to be used with JavaScript as the
1114     scripting language. <a href="#refsJS">[JS]</a></p>
1115    
1116     <dt>WebIDL
1117    
1118     <dd>
1119     <p>The IDL blocks in this specification use the semantics of the WebIDL
1120     specification. <a href="#refsWebIDL">[WebIDL]</a></p>
1121     </dl>
1122    
1123 wakaba 1.14 <h3 id=terminology><span class=secno>1.4 </span>Terminology</h3>
1124 wakaba 1.1
1125 wakaba 1.3 <p>The construction "a <code title="">Foo</code> object", where <code
1126     title="">Foo</code> is actually an interface, is sometimes used instead of
1127     the more accurate "an object implementing the interface <code
1128     title="">Foo</code>".
1129 wakaba 1.1
1130     <p>The term DOM is used to refer to the API set made available to scripts
1131     in Web applications, and does not necessarily imply the existence of an
1132     actual <code>Document</code> object or of any other <code>Node</code>
1133     objects as defined in the DOM Core specifications. <a
1134     href="#refsDOM3CORE">[DOM3CORE]</a>
1135    
1136     <p>A DOM attribute is said to be <em>getting</em> when its value is being
1137     retrieved (e.g. by author script), and is said to be <em>setting</em> when
1138     a new value is assigned to it.
1139    
1140 wakaba 1.5 <h2 id=infrastructure><span class=secno>2. </span>Infrastructure</h2>
1141 wakaba 1.3
1142 wakaba 1.12 <p>There are two kinds of workers; dedicated workers, and shared workers.
1143     Dedicated workers, once created, and are linked to their creator; but
1144     message ports can be used to communicate from a dedicated worker to
1145 wakaba 1.14 multiple other browsing contexts or workers. Shared workers, on the other
1146 wakaba 1.12 hand, are named, and once created any script running in the same
1147     <span>origin</span> can obtain a reference to that worker and communicate
1148     with it.
1149    
1150     <h3 id=the-global><span class=secno>2.1 </span>The global scope</h3>
1151    
1152     <p>The global scope is the "inside" of a worker.
1153    
1154     <h4 id=the-workerglobalscope><span class=secno>2.1.1 </span>The <code><a
1155     href="#workerglobalscope">WorkerGlobalScope</a></code> abstract interface</h4>
1156 wakaba 1.3
1157     <pre
1158 wakaba 1.7 class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
1159     readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
1160 wakaba 1.8 readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
1161 wakaba 1.12 // also implements everything on <a href="#workerutils">WorkerUtils</a>
1162    
1163 wakaba 1.7 void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
1164 wakaba 1.12 attribute <span>EventListener</span> <a href="#onclose" title=handler-WorkerGlobalScope-onclose>onclose</a>;
1165 wakaba 1.3 };</pre>
1166    
1167 wakaba 1.8 <p>Objects implementing the <code><a
1168 wakaba 1.14 href="#workerglobalscope">WorkerGlobalScope</a></code> interface must also
1169     implement the <code>EventTarget</code> interface.
1170 wakaba 1.8
1171 wakaba 1.7 <p>The <dfn id=self
1172     title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
1173     return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1174     object itself.
1175 wakaba 1.3
1176 wakaba 1.8 <p>The <dfn id=location
1177     title=dom-WorkerGlobalScope-location><code>location</code></dfn> attribute
1178     must return the <code><a href="#workerlocation">WorkerLocation</a></code>
1179     object created for the <code><a
1180     href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
1181     worker was created. It represents the <span>absolute URL</span> of the
1182     script that was used to initialize the worker.
1183 wakaba 1.7
1184 wakaba 1.12 <hr>
1185    
1186     <p>When a script invokes the <dfn id=close
1187     title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
1188     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
1189     the user agent must run the following steps:
1190    
1191     <ol>
1192     <li>
1193 wakaba 1.18 <p><span>Queue a task</span> to <span>fire a simple event</span> called
1194     <code title=event-close>close</code> at the <code><a
1195     href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1196 wakaba 1.12
1197     <li>
1198     <p>Set the worker's <code><a
1199     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1200     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1201     true.
1202    
1203     <li>
1204     <p>For each <code>MessagePort</code> object that is entangled with
1205     another port and that has one (but only one) port whose owner is the
1206     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1207     on which the method was invoked (this would include, for instance, the
1208 wakaba 1.18 implicit port in used for dedicated workers), unentangle the two ports.
1209 wakaba 1.12 </ol>
1210 wakaba 1.3
1211 wakaba 1.4 <p>The following are the <span>event handler DOM attributes</span> that
1212     must be supported by objects implementing the <code><a
1213 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
1214 wakaba 1.4
1215     <dl>
1216 wakaba 1.12 <dt><dfn id=onclose
1217     title=handler-WorkerGlobalScope-onclose><code>onclose</code></dfn>
1218 wakaba 1.4
1219     <dd>
1220 wakaba 1.12 <p>Must be invoked whenever a <code title=event-close>close</code> event
1221     is targeted at or bubbles through the <code><a
1222 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1223 wakaba 1.12 </dl>
1224 wakaba 1.4
1225 wakaba 1.12 <h4 id=dedicated><span class=secno>2.1.2 </span>Dedicated workers and the
1226     <code><a
1227     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1228     interface</h4>
1229    
1230     <pre
1231     class=idl>[NoInterfaceObject] interface <dfn id=dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1232 wakaba 1.13 void <a href="#postmessage" title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in DOMString message);
1233 wakaba 1.14 void <a href="#postmessage" title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in DOMString message, in <span>MessagePort</span> messagePort);<!--
1234     <span>MessagePort</span> <span title="dom-DedicatedWorkerGlobalScope-startConversation">startConversation</span>(in DOMString message);-->
1235 wakaba 1.12 attribute <span>EventListener</span> <a href="#onmessage" title=handler-DedicatedWorkerGlobalScope-onmessage>onmessage</a>;
1236     };</pre>
1237    
1238     <p><code><a
1239     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1240     objects act as if they had an implicit <code>MessagePort</code> associated
1241     with them. This port is part of a channel that is set up when the worker
1242     is created, but it is not exposed. This object must never be garbage
1243     collected before the <code><a
1244     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1245     object.
1246    
1247     <p>All messages received by that port must immediately be retargetted at
1248     the <code><a
1249     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1250     object.
1251    
1252     <p>The <dfn id=postmessage
1253 wakaba 1.14 title=dom-DedicatedWorkerGlobalScope-postMessage><code>postMessage()</code></dfn><!--
1254     and <dfn
1255     title="dom-DedicatedWorkerGlobalScope-startConversation"><code>startConversation()</code></dfn>-->
1256     method<!--s (startConversation)--> on <code><a
1257 wakaba 1.12 href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1258 wakaba 1.14 objects must act as if, when invoked, it<!--/they (startConversation)-->
1259     immediately invoked the method of the same name on the port, with the same
1260     arguments, and returned the same return value.
1261 wakaba 1.12
1262     <p>The following are the <span>event handler DOM attributes</span> that
1263     must be supported by objects implementing the <code><a
1264     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1265     interface:
1266    
1267     <dl>
1268     <dt><dfn id=onmessage
1269     title=handler-DedicatedWorkerGlobalScope-onmessage><code>onmessage</code></dfn>
1270 wakaba 1.4
1271     <dd>
1272 wakaba 1.12 <p>Must be invoked whenever a <code
1273     title=event-DedicatedWorkerGlobalScope-message>message</code> event is
1274     targeted at or bubbles through the <code><a
1275     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1276     object.
1277 wakaba 1.4 </dl>
1278    
1279 wakaba 1.12 <h4 id=shared0><span class=secno>2.1.3 </span>Shared workers and the
1280     <code><a
1281     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1282     inteface</h4>
1283    
1284     <pre
1285     class=idl>[NoInterfaceObject] interface <dfn id=sharedworkerglobalscope>SharedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1286     readonly attribute DOMString <a href="#name" title=dom-SharedWorkerGlobalScope-name>name</a>;
1287     attribute <span>EventListener</span> <a href="#onconnect" title=handler-SharedWorkerGlobalScope-onconnect>onconnect</a>;
1288     };</pre>
1289    
1290     <p>Shared workers receive message ports through <code
1291     title=event-WorkerGlobalScope-connect>connect</code> events on their
1292     global object for each connection.
1293    
1294     <p>The <dfn id=name
1295     title=dom-SharedWorkerGlobalScope-name><code>name</code></dfn> attribute
1296     must return the value it was assigned when the <code><a
1297     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object
1298     was created by the "<a href="#run-a">run a worker</a>" algorithm. Its
1299     value represents the name that can be used to obtain a reference to the
1300     worker using the <code><a href="#sharedworker">SharedWorker</a></code>
1301     constructor.
1302    
1303     <p>The following are the <span>event handler DOM attributes</span> that
1304     must be supported by objects implementing the <code><a
1305     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1306     interface:
1307    
1308     <dl>
1309     <dt><dfn id=onconnect
1310     title=handler-SharedWorkerGlobalScope-onconnect><code>onconnect</code></dfn>
1311    
1312     <dd>
1313     <p>Must be invoked whenever a <code
1314     title=event-SharedWorkerGlobalScope-connect>connect</code> event is
1315     targeted at or bubbles through the <code><a
1316     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1317     object.
1318     </dl>
1319 wakaba 1.8
1320 wakaba 1.6 <h3 id=base-urls><span class=secno>2.2 </span>Base URLs and origins of
1321     workers</h3>
1322    
1323 wakaba 1.7 <p>Both the <span>origin</span> and <span>effective script origin</span> of
1324     scripts running in workers are the <span>origin</span> of the
1325 wakaba 1.18 <span>absolute URL</span> given in that the worker's <code
1326 wakaba 1.8 title=dom-WorkerGlobalScope-location><a
1327     href="#location">location</a></code> attribute represents.
1328 wakaba 1.6
1329 wakaba 1.18 <h3 id=the-event><span class=secno>2.3 </span>The event loop</h3>
1330 wakaba 1.3
1331 wakaba 1.7 <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1332 wakaba 1.18 object is asssociated with a <span>event loop</span>. This <span>event
1333     loop</span> has no associated <span>browsing context</span>, and its <span
1334     title="task queue">task queues</span> only have events, callbacks, and
1335     networking activity as <span title=concept-task>tasks</span>. The
1336     processing model of these <span title="event loop">event loops</span> is
1337     defined below in the <a href="#run-a">run a worker</a> algorithm.
1338 wakaba 1.4
1339 wakaba 1.12 <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1340     object also has a <dfn id=closing
1341     title=dom-WorkerGlobalScope-closing>closing</dfn> flag, which must
1342     initially be false, but which can get set to true by the algorithms in the
1343     processing model section below.
1344    
1345 wakaba 1.7 <p>Once the <code><a
1346 wakaba 1.12 href="#workerglobalscope">WorkerGlobalScope</a></code>'s <a
1347     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1348 wakaba 1.18 set to true, the <span>event loop</span>'s <span title="task queue">task
1349     queues</span> must discard any further <span
1350     title=concept-task>tasks</span> that would be added to them (tasks already
1351     on the queue are unaffected unless otherwise specified). Effectively, once
1352     the <a href="#closing" title=dom-WorkerGlobalScope-closing>closing</a>
1353     flag is true, timers stop firing, notifications for all pending
1354     asynchronous operations are dropped, etc.
1355 wakaba 1.4
1356 wakaba 1.6 <h3 id=the-workers><span class=secno>2.4 </span>The worker's ports</h3>
1357 wakaba 1.4
1358     <p>Workers communicate with other workers and with <span title="browsing
1359     context">browsing contexts</span> through <span title="channel
1360     messaging">message channels</span> and their <code>MessagePort</code>
1361     objects.
1362    
1363 wakaba 1.7 <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1364 wakaba 1.12 <var title="">worker global scope</var> has a list of <dfn
1365     id=the-workers0>the worker's ports</dfn>, which consists of all the
1366     <code>MessagePort</code> objects that are entangled with another port and
1367     that have one (but only one) port owned by <var title="">worker global
1368 wakaba 1.15 scope</var>. This list includes
1369     <!--all the <code>MessagePort</code> objects that are in events
1370 wakaba 1.18 pending in the <span>event loop</span>, as well as (commented out
1371     because in practice it makes no difference either way as far as I
1372     can tell, and it would be hard to strictly implement since these
1373 wakaba 1.15 ports might not yet be across the thread boundary)-->
1374     the implicit <code>MessagePort</code> in the case of <a
1375 wakaba 1.12 href="#dedicatedworkerglobalscope"
1376     title=DedicatedWorkerGlobalScope>dedicated workers</a>.
1377 wakaba 1.4
1378 wakaba 1.6 <hr>
1379    
1380 wakaba 1.8 <p>A worker is said to be a <dfn id=permissible>permissible worker</dfn> if
1381     either:
1382    
1383     <ul>
1384     <li>at some point past or present a <code>MessagePort</code> owned by the
1385     worker was entangled with a <code>MessagePort</code> <var
1386     title="">p</var> whose owner is a <code>Window</code> object whose
1387     <span>active document</span> is the <code>Document</code> that was that
1388     <span>browsing context</span>'s <span>active document</span> when <var
1389     title="">p</var> was created, and that <code>Document</code> is
1390     <span>fully active</span>, or
1391    
1392     <li>at some point past or present a <code>MessagePort</code> owned by the
1393     worker was entangled with a <code>MessagePort</code> owned by another
1394     worker that is currently a <a href="#permissible">permissible worker</a>.
1395     </ul>
1396    
1397     <hr>
1398 wakaba 1.6
1399 wakaba 1.8 <p>A worker is said to be a <dfn id=protected>protected worker</dfn> if
1400     either:
1401 wakaba 1.6
1402     <ul>
1403 wakaba 1.8 <li>it has outstanding timers, database transactions, or network
1404 wakaba 1.14 connections, and is a <a href="#permissible">permissible worker</a>, or
1405 wakaba 1.8
1406     <li>there is a <a href="#protected">protected worker</a> that at some
1407     point past or present owned a <code>MessagePort</code> that was entangled
1408     with a <code>MessagePort</code> owned by this worker.
1409     </ul>
1410    
1411     <hr>
1412    
1413     <p>A worker is said to be an <dfn id=active>active needed worker</dfn> if
1414     either:
1415    
1416     <ul>
1417     <li>the worker is a <a href="#protected">protected worker</a>, or
1418    
1419     <li>at least one of the <a href="#the-workers0">the worker's ports</a> is
1420     entangled with a <code>MessagePort</code> <var title="">p</var> whose
1421     owner is a <code>Window</code> object whose <span>active document</span>
1422     is the <code>Document</code> that was that <span>browsing
1423     context</span>'s <span>active document</span> when that
1424     <code>MessagePort</code> <var title="">p</var> was created, and that
1425     <code>Document</code> is <span>fully active</span>, or
1426 wakaba 1.6
1427     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1428 wakaba 1.7 an entangled <code>MessagePort</code> owned by a <code><a
1429     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1430 wakaba 1.8 itself an <a href="#active">active needed worker</a>.
1431 wakaba 1.6 </ul>
1432    
1433     <hr>
1434    
1435 wakaba 1.8 <p>A worker is said to be a <dfn id=suspendable>suspendable worker</dfn> if
1436     it is not an <a href="#active">active needed worker</a> but either:
1437 wakaba 1.6
1438     <ul>
1439 wakaba 1.8 <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1440     an entangled <code>MessagePort</code> owned by a <code>Window</code>
1441     object, or
1442 wakaba 1.6
1443     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1444 wakaba 1.7 an entangled <code>MessagePort</code> owned by a <code><a
1445     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1446 wakaba 1.8 itself a <span>needed worker</span>.
1447 wakaba 1.6 </ul>
1448    
1449     <h3 id=processing><span class=secno>2.5 </span>Processing model</h3>
1450 wakaba 1.3
1451 wakaba 1.12 <p>When a user agent is to <dfn id=run-a>run a worker</dfn> for a script
1452     with <span>URL</span> <var title="">url</var>, a browsing context <var
1453 wakaba 1.18 title="">owner browsing context</var>, and with global scope <var
1454     title="">worker global scope</var>, it must run the following steps:
1455 wakaba 1.3
1456     <ol>
1457     <li>
1458 wakaba 1.13 <p>Create a completely separate and parallel execution environment (i.e.
1459     a separate thread or process or equivalent construct), and run the rest
1460     of these steps asychronously in that context.</p>
1461    
1462     <li>
1463 wakaba 1.18 <p>Attempt to <span>fetch</span> the resource identified by <var
1464     title="">url</var>.</p>
1465 wakaba 1.4
1466 wakaba 1.18 <p>If the attempt fails, then for each <code><a
1467     href="#worker1">Worker</a></code> or <code><a
1468     href="#sharedworker">SharedWorker</a></code> object associated with <var
1469     title="">worker global scope</var>, <span>queue a task</span> to
1470     <span>fire a simple event</span> called <code
1471     title=event-error>error</code> at that object. Abort these steps.</p>
1472    
1473     <p>If the attempt succeeds, then let <var title="">source</var> be the
1474     text of the resource that was obtained.</p>
1475     <!-- XXX do we need
1476     to define character encoding decoding here? -->
1477     <p>Let <var title="">language</var> be JavaScript.</p>
1478 wakaba 1.4
1479     <p class=note>As with <code>script</code> elements, the MIME type of the
1480     script is ignored. Unlike with <code>script</code> elements, there is no
1481     way to override the type. It's always assumed to be JavaScript.</p>
1482     <!-- XXX people will complain about
1483     this. I guess we might want to examine the MIME type... -->
1484    
1485    
1486     <li>
1487 wakaba 1.18 <p>A new <span title=concept-script>script</span> is now created, as
1488     follows.</p>
1489 wakaba 1.8
1490 wakaba 1.18 <p>Create a new <span>script execution environment</span> set up as
1491     appropriate for the scripting language <var title="">language</var>.</p>
1492    
1493     <p>Parse/compile/initialize <var title="">source</var> using that
1494     <span>script execution environment</span>, as appropriate for <var
1495     title="">language</var>, and thus obtain a <span>list of code
1496     entry-points</span>; set the <i>initial code entry-point</i> to the
1497     entry-point for any executable code to be immediately run.</p>
1498    
1499     <p>Set the <span>script's global object</span> to <var title="">worker
1500 wakaba 1.12 global scope</var>.</p>
1501 wakaba 1.3
1502 wakaba 1.18 <p>Set the <span>script's browsing context</span> to <var title="">owner
1503     browsing context</var>.</p>
1504    
1505     <p>Set the <span>script's character encoding</span> to UTF-8. (This is
1506     just used for encoding non-ASCII characters in the query component of
1507     URLs.)</p>
1508    
1509     <p>Set the <span>script's base URL</span> to <var title="">url</var>.</p>
1510 wakaba 1.3
1511 wakaba 1.18 <p>Create a new <span>script group</span> and add the <span
1512     title=concept-script>script</span> to it.</p>
1513 wakaba 1.3
1514     <li>
1515 wakaba 1.12 <p><strong>Closing orphan workers</strong>: Start monitoring the worker
1516     such that as soon as it stops being either an <a href="#active">active
1517     needed worker</a> or a <a href="#suspendable">suspendable worker</a>,
1518 wakaba 1.18 <var title="">worker global scope</var>'s <a href="#closing"
1519 wakaba 1.12 title=dom-WorkerGlobalScope-closing>closing</a> flag is set to true and
1520 wakaba 1.18 <span title="queue a task">a task is queued</span> to <span>fire a
1521     simple event</span> called <code title=event-close>close</code> at <var
1522     title="">worker global scope</var>.</p>
1523 wakaba 1.4
1524     <li>
1525 wakaba 1.12 <p><strong>Suspending workers</strong>: Start monitoring the worker, such
1526 wakaba 1.18 that whenever <var title="">worker global scope</var>'s <a
1527 wakaba 1.12 href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1528     false and the worker is a <a href="#suspendable">suspendable worker</a>,
1529     the user agent suspends execution of script in that worker until such
1530     time as either the <a href="#closing"
1531     title=dom-WorkerGlobalScope-closing>closing</a> flag switches to true or
1532     the worker stops being a <a href="#suspendable">suspendable worker</a>.</p>
1533 wakaba 1.4
1534     <li>
1535 wakaba 1.18
1536     <li>
1537     <p><span title="jump to a code entry-point">Jump</span> to the <span
1538     title=concept-script>script</span>'s <i>initial code entry-point</i>,
1539     and let that run until it either returns, fails to catch an exception,
1540     or gets prematurely aborted by the "<a href="#kill-a">kill a worker</a>"
1541     or "<a href="#terminate">terminate a worker</a>" algorithms defined
1542     below.</p>
1543 wakaba 1.4
1544     <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
1545     worker</a>" algorithm, then that same algorithm will cause there to only
1546 wakaba 1.18 be a single <span title=concept-task>task</span> in the <span>event
1547     loop</span> at the next step, namely the task for the <code
1548     title=message-close>close</code> event. The "<a
1549     href="#terminate">terminate a worker</a>" algorithm removes all the
1550 wakaba 1.13 events.</p>
1551 wakaba 1.4
1552     <li>
1553 wakaba 1.18 <p><i title="">Event loop</i>: Wait until either there is a <span
1554     title=concept-task>task</span> in one of the <span>event loop</span>'s
1555     <span title="task queue">task queues</span> or <var title="">worker
1556     global scope</var>'s <a href="#closing"
1557     title=dom-WorkerGlobalScope-closing>closing</a> flag is set to true.</p>
1558    
1559     <li>
1560     <p>Run the oldest task on one of the <span>event loop</span>'s <span
1561     title="task queue">task queues</span>, if any. The user agent may pick
1562     any <span>task queue</span>.</p>
1563    
1564     <p class=note>The handling of events or the execution of callbacks might
1565     get prematurely aborted by the "<a href="#kill-a">kill a worker</a>" or
1566     "<a href="#terminate">terminate a worker</a>" algorithms defined below.</p>
1567 wakaba 1.3
1568     <li>
1569 wakaba 1.18 <p>Remove the task run in the previous step, if any, from its <span>task
1570     queue</span>.</p>
1571 wakaba 1.3
1572     <li>
1573 wakaba 1.18 <p>If there are any more events in the <span>event loop</span>'s <span
1574     title="task queue">task queues</span> or if <var title="">worker global
1575     scope</var>'s <a href="#closing"
1576     title=dom-WorkerGlobalScope-closing>closing</a> flag is set to false,
1577     then jump back to the step above labeled <i>event loop</i>.</p>
1578 wakaba 1.3
1579     <li>
1580 wakaba 1.18 <p>Freeze the <span>script group</span>.</p>
1581    
1582     <p class=note>This kills timers, database transactions, etc.</p>
1583 wakaba 1.9
1584     <li>
1585 wakaba 1.18 <p>For each <code><a href="#worker1">Worker</a></code> or <code><a
1586     href="#sharedworker">SharedWorker</a></code> object associated with <var
1587     title="">worker global scope</var>, <span>queue a task</span> to
1588     <span>fire a simple event</span> called <code
1589     title=event-close>close</code> at that object.</p>
1590 wakaba 1.3 </ol>
1591    
1592     <hr>
1593    
1594 wakaba 1.12 <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn> it must run
1595 wakaba 1.3 the following steps in parallel with the worker's main loop (the "<a
1596     href="#run-a">run a worker</a>" processing model defined above):
1597    
1598     <ol>
1599     <li>
1600 wakaba 1.18 <p>If the worker's <code><a
1601 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1602 wakaba 1.18 href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1603     false, <span>queue a task</span> to <span>fire a simple event</span>
1604     called <code title=event-close>close</code> at the worker's <code><a
1605     href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1606 wakaba 1.4
1607     <li>
1608 wakaba 1.7 <p>Set the worker's <code><a
1609 wakaba 1.12 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1610     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1611     true.
1612 wakaba 1.3
1613     <li>
1614 wakaba 1.4 <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
1615     a worker</a>" processing model defined above immediately starts running
1616 wakaba 1.12 event listeners registered for the <code title=event-close>close</code>
1617     event, this time should not be zero &mdash; the idea is that the <code
1618     title=event-close>close</code> event can be used to clean up when
1619     shutting down unexpectedly.
1620 wakaba 1.3
1621     <li>
1622 wakaba 1.18 <p>If there are any <span title=concept-task>tasks</span> queued in the
1623     <span>event loop</span>'s <span title="task queue">task queues</span>
1624 wakaba 1.12 other than the <code title=event-close>close</code> event that this
1625 wakaba 1.18 algorithm just added, discard them without processing them.
1626 wakaba 1.3
1627     <li>
1628 wakaba 1.12 <p>If the <code title=event-close>close</code> event that this algorithm
1629 wakaba 1.18 just queued hasn't yet been dispatched, then abort the script currently
1630 wakaba 1.12 running in the worker.
1631 wakaba 1.3
1632     <li>
1633     <p>Wait a user-agent-defined amount of time.
1634    
1635     <li>
1636     <p>Abort the script currently running in the worker (if any script is
1637     running, then it will be a handler for the <code
1638 wakaba 1.12 title=event-close>close</code> event).
1639 wakaba 1.3 </ol>
1640    
1641 wakaba 1.6 <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
1642     processing model on a worker at any time, e.g. in response to user
1643     requests, in response to CPU quota management, or when a worker stops
1644 wakaba 1.8 being an <a href="#active">active needed worker</a> if the worker
1645 wakaba 1.12 continues executing even after its <a href="#closing"
1646     title=dom-WorkerGlobalScope-closing>closing</a> flag was set to true.
1647 wakaba 1.6
1648     <hr>
1649    
1650 wakaba 1.13 <p>When a user agent is to <dfn id=terminate>terminate a worker</dfn> it
1651     must run the following steps in parallel with the worker's main loop (the
1652     "<a href="#run-a">run a worker</a>" processing model defined above):
1653 wakaba 1.4
1654     <ol>
1655     <li>
1656 wakaba 1.12 <p>Set the worker's <code><a
1657 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1658 wakaba 1.12 href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1659     true.
1660 wakaba 1.4
1661     <li>
1662 wakaba 1.18 <p>If there are any <span title=concept-task>tasks</span> queued in the
1663     <span>event loop</span>'s <span title="task queue">task queues</span>,
1664     discard them without processing them.
1665 wakaba 1.4
1666     <li>
1667 wakaba 1.12 <p>Abort the script currently running in the worker.
1668 wakaba 1.4 </ol>
1669    
1670 wakaba 1.6 <h3 id=creating><span class=secno>2.6 </span>Creating workers</h3>
1671 wakaba 1.3
1672 wakaba 1.12 <h4 id=the-abstractworker><span class=secno>2.6.1 </span>The <code><a
1673     href="#abstractworker">AbstractWorker</a></code> abstract interface</h4>
1674    
1675 wakaba 1.3 <pre
1676 wakaba 1.12 class=idl>[NoInterfaceObject] interface <dfn id=abstractworker>AbstractWorker</dfn> {
1677     attribute <span>EventListener</span> <a href="#onerror" title=handler-AbstractWorker-onerror>onerror</a>;
1678     attribute <span>EventListener</span> <a href="#onclose0" title=handler-AbstractWorker-onclose>onclose</a>;
1679     };</pre>
1680    
1681 wakaba 1.14 <p>Objects implementing the <code><a
1682     href="#abstractworker">AbstractWorker</a></code> interface must also
1683     implement the <code>EventTarget</code> interface.
1684    
1685 wakaba 1.12 <p>The following are the <span>event handler DOM attributes</span> that
1686     must be supported by objects implementing the <code><a
1687     href="#abstractworker">AbstractWorker</a></code> interface:
1688    
1689     <dl>
1690     <dt><dfn id=onerror
1691     title=handler-AbstractWorker-onerror><code>onerror</code></dfn>
1692    
1693     <dd>
1694     <p>Must be invoked whenever an <code title=event-error>error</code> event
1695     is targeted at or bubbles through the <code><a
1696     href="#abstractworker">AbstractWorker</a></code> object.
1697    
1698     <dt><dfn id=onclose0
1699     title=handler-AbstractWorker-onclose><code>onclose</code></dfn>
1700    
1701     <dd>
1702     <p>Must be invoked whenever an <code title=event-close>close</code> event
1703     is targeted at or bubbles through the <code><a
1704     href="#abstractworker">AbstractWorker</a></code> object.
1705     </dl>
1706 wakaba 1.9
1707 wakaba 1.12 <h4 id=dedicated0><span class=secno>2.6.2 </span>Dedicated workers and the
1708     <code><a href="#worker1">Worker</a></code> interface</h4>
1709 wakaba 1.9
1710 wakaba 1.12 <pre class=idl>[NoInterfaceObject,
1711     <a href="#worker2" title=dom-Worker>Constructor</a>(in DOMString scriptURL)]
1712     interface <dfn id=worker1>Worker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1713 wakaba 1.14 void <a href="#terminate0" title=dom-Worker-terminate>terminate</a>();
1714 wakaba 1.12
1715 wakaba 1.13 void <a href="#postmessage0" title=dom-Worker-postMessage>postMessage</a>(in DOMString message);
1716 wakaba 1.14 void <a href="#postmessage0" title=dom-Worker-postMessage>postMessage</a>(in DOMString message, in <span>MessagePort</span> messagePort);<!--
1717     <span>MessagePort</span> <span title="dom-Worker-startConversation">startConversation</span>(in DOMString message);-->
1718 wakaba 1.12 attribute <span>EventListener</span> <a href="#onmessage0" title=handler-Worker-onmessage>onmessage</a>;
1719 wakaba 1.3 };</pre>
1720    
1721 wakaba 1.14 <p>The <dfn id=terminate0
1722     title=dom-Worker-terminate><code>terminate()</code></dfn> method, when
1723     invoked, must cause the "<a href="#terminate">terminate a worker</a>"
1724     algorithm to be run on the worker with with the object is associated.
1725 wakaba 1.12
1726     <p><code><a href="#worker1">Worker</a></code> objects act as if they had an
1727     implicit <code>MessagePort</code> associated with them. This port is part
1728     of a channel that is set up when the worker is created, but it is not
1729     exposed. This object must never be garbage collected before the <code><a
1730     href="#worker1">Worker</a></code> object.
1731    
1732     <p>All messages received by that port must immediately be retargetted at
1733     the <code><a href="#worker1">Worker</a></code> object.
1734    
1735     <p>The <dfn id=postmessage0
1736 wakaba 1.14 title=dom-Worker-postMessage><code>postMessage()</code></dfn><!--
1737     and <dfn
1738     title="dom-Worker-startConversation"><code>startConversation()</code></dfn>-->
1739     method<!--s (startConversation)--> on <code><a
1740     href="#worker1">Worker</a></code> objects must act as if, when invoked,
1741     it<!--/they (startConversation)--> immediately invoked the method of the
1742     same name on the port, with the same arguments, and returned the same
1743     return value.
1744 wakaba 1.12
1745     <p>The following are the <span>event handler DOM attributes</span> that
1746     must be supported by objects implementing the <code><a
1747     href="#worker1">Worker</a></code> interface:
1748    
1749     <dl>
1750     <dt><dfn id=onmessage0
1751     title=handler-Worker-onmessage><code>onmessage</code></dfn>
1752    
1753     <dd>
1754     <p>Must be invoked whenever a <code
1755     title=event-Worker-message>message</code> event is targeted at or
1756     bubbles through the <code><a href="#worker1">Worker</a></code> object.
1757     </dl>
1758 wakaba 1.4
1759     <hr>
1760    
1761 wakaba 1.12 <p>When the <dfn id=worker2 title=dom-Worker><code>Worker(<var
1762     title="">scriptURL</var>)</code></dfn> constructor is invoked, the user
1763     agent must run the following steps:
1764 wakaba 1.4
1765     <ol>
1766 wakaba 1.5 <li>
1767     <p><span title="resolve a url">Resolve</span> the <var
1768     title="">scriptURL</var> argument.
1769    
1770     <li>
1771     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1772    
1773     <li>
1774     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1775     is not the <span title="same origin">same</span> as the origin of the
1776 wakaba 1.12 script that invoked the constructor, then throw a <span>security
1777 wakaba 1.5 exception</span>.
1778    
1779     <li>
1780 wakaba 1.12 <p><span>Create a new <code><a
1781     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1782 wakaba 1.18 object</span>. Let <var title="">worker global scope</var> be this new
1783     object.
1784 wakaba 1.12
1785     <li>
1786     <p>Create a new <code><a href="#worker1">Worker</a></code> object,
1787 wakaba 1.18 associated with <var title="">worker global scope</var>. Let <var
1788     title="">worker</var> be this new object.
1789 wakaba 1.5
1790     <li>
1791 wakaba 1.12 <p><span>Create a <code>MessagePort</code> object</span> owned by the
1792     <span>script execution context</span> of the script that invoked the
1793     method. Let this be the <var title="">outside port</var>.
1794    
1795     <li>
1796     <p>Associate the <var title="">outside port</var> with <var
1797     title="">worker</var>.
1798    
1799     <li>
1800 wakaba 1.18 <p><span>Create a <code>MessagePort</code> object</span> owned by <var
1801     title="">worker global scope</var>. Let <var title="">inside port</var>
1802     be this new object.
1803 wakaba 1.12
1804     <li>
1805 wakaba 1.18 <p>Associate <var title="">inside port</var> with <var title="">worker
1806     global scope</var>.
1807 wakaba 1.12
1808     <li>
1809     <p><span>Entangle</span> <var title="">outside port</var> and <var
1810     title="">inside port</var>.
1811    
1812     <li>
1813     <p>Return <var title="">worker</var>, and run the following steps
1814     asynchronously.
1815    
1816     <li>
1817     <p>Open <var title="">inside port</var>'s <span>port message
1818     queue</span>.
1819    
1820     <li>
1821 wakaba 1.13 <p>Open <var title="">outside port</var>'s <span>port message
1822     queue</span>.
1823    
1824     <li>
1825 wakaba 1.18 <p><a href="#run-a">Run a worker</a> for the resulting <span>absolute
1826     URL</span>, with the <span>script browsing context</span> of the script
1827     that invoked the method as the <var title="">owner browsing
1828     context</var>, and with <var title="">worker global scope</var> as the
1829     global scope.</p>
1830 wakaba 1.4 </ol>
1831    
1832 wakaba 1.12 <h4 id=shared1><span class=secno>2.6.3 </span>Shared workers and the
1833     <code><a href="#sharedworker">SharedWorker</a></code> interface</h4>
1834 wakaba 1.4
1835 wakaba 1.12 <pre class=idl>[NoInterfaceObject,
1836     <a href="#sharedworker0" title=dom-SharedWorker>Constructor</a>(in DOMString scriptURL, in DOMString name)]
1837     interface <dfn id=sharedworker>SharedWorker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1838     readonly attribute <code>MessagePort</code> <a href="#port" title=dom-SharedWorker-port>port</a>;
1839     };</pre>
1840    
1841     <p>The <dfn id=port title=dom-SharedWorker-port><code>port</code></dfn>
1842     attribute must return the value it was assigned by the object's
1843     constructor. It represents the <code>MessagePort</code> for communicating
1844     with the shared worker.
1845    
1846     <p>When the <dfn id=sharedworker0
1847     title=dom-SharedWorker><code>SharedWorker(<var title="">scriptURL</var>,
1848     <var title="">name</var>)</code></dfn> constructor is invoked, the user
1849     agent must run the following steps:
1850 wakaba 1.4
1851     <ol>
1852 wakaba 1.5 <li>
1853     <p><span title="resolve a url">Resolve</span> the <var
1854     title="">scriptURL</var> argument.
1855    
1856     <li>
1857     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1858    
1859     <li>
1860     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1861     is not the <span title="same origin">same</span> as the origin of the
1862 wakaba 1.12 script that invoked the constructor, then throw a <span>security
1863 wakaba 1.5 exception</span>.
1864    
1865     <li>
1866 wakaba 1.12 <p>Execute the following substeps atomically:</p>
1867 wakaba 1.5
1868     <ol>
1869     <li>
1870 wakaba 1.12 <p>Create a new <code><a href="#sharedworker">SharedWorker</a></code>
1871     object, which will shortly be associated with a <code><a
1872     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1873     object. Let this <code><a href="#sharedworker">SharedWorker</a></code>
1874     object be <var title="">worker</var>.
1875 wakaba 1.5
1876     <li>
1877 wakaba 1.12 <p><span>Create a <code>MessagePort</code> object</span> owned by the
1878     <span>script execution context</span> of the script that invoked the
1879     method. Let this be the <var title="">outside port</var>.
1880 wakaba 1.5
1881     <li>
1882 wakaba 1.12 <p>Assign <var title="">outside port</var> to the <code
1883     title=dom-SharedWorker-port><a href="#port">port</a></code> attribute
1884     of <var title="">worker</var>.
1885 wakaba 1.9
1886     <li>
1887 wakaba 1.12 <p>If there exists a <code><a
1888     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1889     object whose <a href="#closing"
1890     title=dom-WorkerGlobalScope-closing>closing</a> flag is false, whose
1891     <code title=dom-WorkerGlobalScope-name>name</code> attribute is
1892     exactly equal to the <var title="">name</var> argument, and whose
1893     <code title=dom-WorkerGlobalScope-location><a
1894     href="#location">location</a></code> attribute represents an
1895     <span>absolute URL</span> that has the <span>same origin</span> as the
1896     resulting <span>absolute URL</span>, then run these substeps:</p>
1897    
1898     <ol>
1899     <li>
1900     <p>Let <var title="">worker global scope</var> be that <code><a
1901     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1902     object.
1903    
1904     <li>
1905     <p>If <var title="">worker global scope</var>'s <code
1906     title=dom-WorkerGlobalScope-location><a
1907     href="#location">location</a></code> attribute represents an
1908     <span>absolute URL</span> that is not exactly equal to the resulting
1909     <span>absolute URL</span>, then throw a
1910     <code>URL_MISMATCH_ERR</code> exception and abort all these steps.
1911     <span class=big-issue>code 21</span>
1912    
1913     <li>
1914     <p>Associate <var title="">worker</var> with <var title="">worker
1915     global scope</var>.
1916    
1917     <li>
1918 wakaba 1.18 <p><span>Create a <code>MessagePort</code> object</span> owned by
1919 wakaba 1.12 <var title="">worker global scope</var>. Let this be the <var
1920     title="">inside port</var>.
1921    
1922     <li>
1923     <p><span>Entangle</span> <var title="">outside port</var> and <var
1924     title="">inside port</var>.
1925    
1926     <li>
1927     <p>Return <var title="">worker</var> and perform the next step
1928     asynchronously.
1929    
1930     <li>
1931     <p>Create an event that uses the <code>MessageEvent</code> interface,
1932     with the name <code title=event-connect>connect</code>, which does
1933     not bubble, is cancelable, has no default action, has a <code
1934     title=dom-MessageEvent-data>data</code> attribute whose value is the
1935     empty string and has a <code
1936     title=dom-MessageEvent-messagePort>messagePort</code> attribute
1937 wakaba 1.18 whose value is the newly created port, and <span>queue a task</span>
1938     to dispatch the event at <var title="">worker global scope</var>.
1939 wakaba 1.12
1940     <li>
1941     <p>Abort all these steps.
1942     </ol>
1943 wakaba 1.5
1944     <li>
1945 wakaba 1.12 <p><span>Create a new <code><a
1946     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1947 wakaba 1.18 object</span>. Let <var title="">worker global scope</var> be this new
1948     object.
1949 wakaba 1.5
1950 wakaba 1.12 <li>
1951     <p>Associate <var title="">worker</var> with <var title="">worker
1952     global scope</var>.
1953 wakaba 1.4
1954 wakaba 1.12 <li>
1955     <p>Set the <code title=dom-SharedWorkerGlobalScope-name><a
1956     href="#name">name</a></code> attribute of <var title="">worker global
1957 wakaba 1.18 scope</var> to <var title="">name</var>.
1958 wakaba 1.4
1959 wakaba 1.12 <li>
1960 wakaba 1.18 <p><span>Create a <code>MessagePort</code> object</span> owned by <var
1961     title="">worker global scope</var>. Let <var title="">inside
1962     port</var> be this new object.
1963 wakaba 1.4
1964 wakaba 1.12 <li>
1965     <p><span>Entangle</span> <var title="">outside port</var> and <var
1966     title="">inside port</var>.
1967     </ol>
1968 wakaba 1.5
1969     <li>
1970 wakaba 1.12 <p>Return <var title="">worker</var> and perform the next step
1971     asynchronously.
1972 wakaba 1.5
1973     <li>
1974     <p>Create an event that uses the <code>MessageEvent</code> interface,
1975 wakaba 1.6 with the name <code title=event-connect>connect</code>, which does not
1976 wakaba 1.5 bubble, is cancelable, has no default action, has a <code
1977     title=dom-MessageEvent-data>data</code> attribute whose value is the
1978     empty string and has a <code
1979     title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
1980 wakaba 1.18 value is the newly created port, and <span>queue a task</span> to
1981     dispatch the event at <var title="">worker global scope</var>.
1982 wakaba 1.8
1983     <li>
1984 wakaba 1.18 <p><a href="#run-a">Run a worker</a> for the resulting <span>absolute
1985     URL</span>, with the <span>script browsing context</span> of the script
1986     that invoked the method as the <var title="">owner browsing
1987     context</var>, and with <var title="">worker global scope</var> as the
1988     global scope.</p>
1989 wakaba 1.4 </ol>
1990 wakaba 1.3
1991 wakaba 1.5 <h2 id=apis-available><span class=secno>3. </span>APIs available to workers</h2>
1992    
1993 wakaba 1.7 <pre
1994     class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1995 wakaba 1.9 void <a href="#importscripts" title=dom-WorkerGlobalScope-importScripts>importScripts</a>([Variadic] in DOMString urls);
1996 wakaba 1.7 readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
1997 wakaba 1.14 readonly attribute <a href="#navigator0">Navigator</a> <a href="#navigator" title=dom-navigator>navigator</a>;
1998 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);
1999     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
2000     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
2001     };</pre>
2002 wakaba 1.6
2003 wakaba 1.5 <p>Objects that implement the <code><a
2004 wakaba 1.9 href="#workerglobalscope">WorkerGlobalScope</a></code> interface must also
2005     implement the <code><a href="#workerutils">WorkerUtils</a></code>
2006     interface.
2007 wakaba 1.3
2008 wakaba 1.14 <p>Objects that implement the <code><a
2009     href="#workerutils">WorkerUtils</a></code> interface must also implement
2010     the <code>WindowTimers</code> interface. (This interface provides the
2011     <code title="">setTimeout()</code> method and its friends.)
2012 wakaba 1.6
2013 wakaba 1.14 <p class=big-issue>Need to define a sync database API.</p>
2014 wakaba 1.6 <!-- XXX ApplicationCache -->
2015     <!-- XXX a way to set cookies on the URL for the script -->
2016     <!-- XXX debugging: void log(in DOMString s); // log to console -->
2017     <!-- XXX debugging: onerror -->
2018    
2019     <hr>
2020    
2021     <p>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects,
2022     etc) are not available to workers in this version of this specification.
2023    
2024 wakaba 1.7 <h3 id=importing><span class=secno>3.1 </span>Importing scripts and
2025     libraries</h3>
2026    
2027 wakaba 1.9 <p>When a script invokes the <dfn id=importscripts
2028     title=dom-WorkerGlobalScope-importScripts><code>importScripts(<var
2029     title="">urls</var>)</code></dfn> method on a <code><a
2030 wakaba 1.7 href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
2031     agent must run the following steps:
2032    
2033     <ol>
2034     <li>
2035 wakaba 1.9 <p>If there are no arguments, return without doing anything. Abort these
2036     steps.
2037    
2038     <li>
2039     <p><span title="resolve a url">Resolve</span> each argument.
2040 wakaba 1.7
2041     <li>
2042 wakaba 1.9 <p>If any fail, throw a <code>SYNTAX_ERR</code> exception.
2043 wakaba 1.14 </li>
2044 wakaba 1.17 <!--
2045     <li><p>If any of the resulting <span title="absolute URL">absolute
2046     URLs</span> have an <span>origin</span> that is not the <span
2047     title="same origin">same</span> as the origin of the script that
2048     invoked the method, then throw a <span>security
2049     exception</span>.</p></li>
2050     -->
2051 wakaba 1.7
2052     <li>
2053 wakaba 1.9 <p>Attempt to <span>fetch</span> each resource identified by the
2054     resulting <span title="absolute URLs">absolute URL</span>.</p>
2055 wakaba 1.7
2056     <li>
2057 wakaba 1.9 <p>For each argument in turn, in the order given, starting with the first
2058     one, run these substeps:</p>
2059 wakaba 1.7
2060 wakaba 1.9 <ol>
2061     <li>
2062     <p>Wait for the fetching attempt for the corresponding resource to
2063     complete.</p>
2064 wakaba 1.7
2065 wakaba 1.9 <p>If the fetching attempt failed, throw a <code>NETWORK_ERR</code>
2066 wakaba 1.18 exception and abort all these steps.</p>
2067 wakaba 1.7
2068 wakaba 1.9 <p>If the fetching attempt succeeded, then let <var
2069 wakaba 1.18 title="">source</var> be the text of the resource that was obtained,
2070     and let <var title="">language</var> be JavaScript.</p>
2071 wakaba 1.7
2072 wakaba 1.9 <p class=note>As with the worker's script, the script here is always
2073     assumed to be JavaScript, regardless of the MIME type.</p>
2074     <!-- XXX -->
2075 wakaba 1.7
2076 wakaba 1.9 <li>
2077 wakaba 1.18 <p><span>Create a script</span>, using <var title="">source</var> as
2078     the script source and <var title="">language</var> as the scripting
2079     language, using the same global object, browsing context, character
2080     encoding, base URL, and script group as the <span
2081     title=concept-script>script</span> that was created by the worker's <a
2082     href="#run-a">run a worker</a> algorithm.</p>
2083    
2084     <p>Let the newly created <span title=concept-script>script</span> run
2085     until it either returns, fails to parse, fails to catch an exception,
2086     or gets prematurely aborted by the "<a href="#kill-a">kill a
2087     worker</a>" or "<a href="#terminate">terminate a worker</a>"
2088     algorithms defined above.</p>
2089 wakaba 1.14
2090     <p>If it failed to parse, then throw a
2091     <code>SyntaxError</code><!-- XXX ref? --> exception and abort all
2092     these steps.</p>
2093 wakaba 1.9
2094     <p>If an exception was raised or if the script was prematurely aborted,
2095     then abort all these steps, letting the exception or aborting continue
2096     to be processed by the script that called the <code
2097     title=dom-WorkerGlobalScope-importScripts><a
2098     href="#importscripts">importScripts()</a></code> method.</p>
2099 wakaba 1.7
2100 wakaba 1.13 <p>If the "<a href="#kill-a">kill a worker</a>" or "<a
2101 wakaba 1.14 href="#terminate">terminate a worker</a>" algorithms abort the script
2102 wakaba 1.13 then abort all these steps.</p>
2103 wakaba 1.9 </ol>
2104 wakaba 1.7 </ol>
2105    
2106 wakaba 1.14 <h3 id=the-navigator><span class=secno>3.2 </span>The <code><a
2107     href="#navigator0">Navigator</a></code> object</h3>
2108    
2109     <p>The <dfn id=navigator title=dom-navigator><code>navigator</code></dfn>
2110     attribute of the <code><a href="#workerutils">WorkerUtils</a></code>
2111     interface must return an instance of the <code><a
2112     href="#navigator0">Navigator</a></code> interface, which represents the
2113     identity and state of the user agent (the client):
2114    
2115     <pre
2116     class=idl>[NoInterfaceObject] interface <dfn id=navigator0>Navigator</dfn> {
2117     // objects implementing this interface also implement the interfaces listed below
2118     };</pre>
2119    
2120     <p>Objects implementing the <code><a
2121     href="#navigator0">Navigator</a></code> interface must also implement the
2122     <span>NavigatorID</span> and <span>NavigatorOnLine</span> interfaces
2123     specified in the HTML5 specification. <a href="#refsHTML5">[HTML5]</a>
2124    
2125     <p class=note>The <code><a href="#navigator0">Navigator</a></code>
2126     interface defined in this specification is different than the one defined
2127     in the HTML5 specification.
2128    
2129     <h3 id=apis-defined><span class=secno>3.3 </span>APIs defined in other
2130 wakaba 1.7 specifications</h3>
2131    
2132     <p>The <dfn id=localstorage
2133     title=dom-localStorage><code>localStorage</code></dfn>, <dfn
2134     id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
2135     must act as defined for the APIs with the same names on the
2136     <code>Window</code> object in the HTML5 specification, with the exception
2137     that where the API would use the <span>origin</span> of the <span>active
2138     document</span> of the <span>browsing context</span> of the
2139     <code>Window</code> object on which the method was supposedly invoked, it
2140     must instead use the <span>origin</span> of the script that invoked the
2141     method. <a href="#refsHTML5">[HTML5]</a>
2142    
2143     <p>The <dfn id=shownotification
2144     title=dom-showNotification><code>showNotification()</code></dfn> methods
2145     must act as defined for the APIs with the same names on the
2146     <code>Window</code> object in the HTML5 specification. <a
2147     href="#refsHTML5">[HTML5]</a>
2148    
2149 wakaba 1.14 <h3 id=interface><span class=secno>3.4 </span>Interface objects and
2150 wakaba 1.7 constructors</h3>
2151    
2152     <p>There must be no interface objects and constructors available in the
2153     global scope of scripts whose <span>script execution context</span> is a
2154     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
2155     except for the following:
2156    
2157     <ul>
2158     <li>
2159     <p><code>XMLHttpRequest</code> and all interface objects and constructors
2160     defined by the XMLHttpRequest specifications, except that the
2161     <span>document response entity body</span> must always be null. <a
2162     href="#refsXHR">[XHR]</a>
2163    
2164     <li>
2165     <p>The <code>WebSocket</code> interface object and constructor.
2166    
2167     <li>
2168     <p>The <code>MessageChannel</code> interface object and constructor.
2169 wakaba 1.14
2170     <li>
2171     <p>The <code title=dom-Worker><a href="#worker2">Worker()</a></code> and
2172     <code title=dom-SharedWorker><a href="#sharedworker0">SharedWorker(<var
2173     title="">url</var>)</a></code> constructors.
2174 wakaba 1.7 </ul>
2175 wakaba 1.8
2176 wakaba 1.14 <h3 id=worker0><span class=secno>3.5 </span>Worker locations</h3>
2177 wakaba 1.8
2178     <pre
2179     class=idl>[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
2180     readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
2181     readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
2182     readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
2183     readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
2184 wakaba 1.12 readonly attribute DOMString <a href="#port0" title=dom-WorkerLocation-port>port</a>;
2185 wakaba 1.8 readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
2186     readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
2187     readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
2188     };</pre>
2189    
2190     <p>A <code><a href="#workerlocation">WorkerLocation</a></code> object
2191     represents an <span>absolute URL</span> set at its creation.
2192    
2193     <p>The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
2194     attribute must return the <span>absolute URL</span> that the object
2195     represents.
2196    
2197     <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface
2198     also has the complement of <span>URL decomposition attributes</span>, <dfn
2199     id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
2200     <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
2201 wakaba 1.12 id=port0 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
2202 wakaba 1.8 id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
2203     <dfn id=pathname
2204     title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
2205     id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
2206     <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
2207     must follow the rules given for URL decomposition attributes, with the
2208     <span title=concept-uda-input>input</span> being the <span>absolute
2209     URL</span> that the object represents (same as the <code
2210     title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
2211     and the <span title=concept-uda-setter>common setter action</span> being a
2212     no-op, since the attributes are defined to be readonly. <a
2213     href="#refsHTML5">[HTML5]</a>
2214 wakaba 1.7
2215 wakaba 1.1 <h2 class=no-num id=references>References</h2>
2216    
2217     <p class=big-issue>This section will be written in a future
2218     draft.<!--XXX-->
2219    
2220     <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
2221     <!-- ACKS -->
2222    
2223 wakaba 1.14 <p>Thanks to Aaron Boodman, Dmitry Titov, Jonas Sicking, Justin James,
2224     Kevin Hakanson, Maciej Stachowiak, Michael Nordman, Mike Smith, and Philip
2225     Taylor for their useful and substantial comments.
2226 wakaba 1.3
2227     <p>Huge thanks to the whole Gears team, who pioneered this technology and
2228     whose experience has been a huge influence on this specification.

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24