/[suikacvs]/markup/html/html5/spec-ja/workers-non-normative.ja.html.u8
Suika

Contents of /markup/html/html5/spec-ja/workers-non-normative.ja.html.u8

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (hide annotations) (download)
Mon Aug 11 06:16:28 2008 UTC (17 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.25: +5 -3 lines
*** empty log message ***

1 wakaba 1.1
2     <html lang=en-US-x-hixie>
3     <head>
4     <title>Web Workers</title>
5     <link href="http://www.whatwg.org/style/specification" rel=stylesheet>
6     <!-- Japanese-translation-only style rules -->
7     <style>
8 wakaba 1.14 em, i {
9     font-style: normal;
10 wakaba 1.1 text-decoration: underline;
11     }
12 wakaba 1.14
13     .note, .warning {
14     font-style: normal;
15     }
16     p.note:before {
17     content: '注意: ';
18     }
19     p.warning:before {
20     content: '\26A0 警告! ';
21     }
22    
23 wakaba 1.1 ins.ja-note {
24     background-color: #EEFFFF;
25     color: black;
26     }
27 wakaba 1.16 span.ja-translation {
28     border-style: none !important;
29     }
30 wakaba 1.18
31     /* From Web Workers <style>, modified for Japanese texts */
32     p > span.ja-translation > span:not([title=""]):not([class="big-issue"]),
33     li > span.ja-translation > span:not([title=""]):not([class="big-issue"]) {
34     border-bottom: solid #99CC99;
35     }
36    
37 wakaba 1.1 </style>
38     <link href="http://www.whatwg.org/images/icon" rel=icon>
39    
40 wakaba 1.16 <style>
41     p > span:not([title=""]):not([class="big-issue"]), li > span:not([title=""]):not([class="big-issue"]) { border-bottom: solid #99CC99; }
42     </style>
43    
44 wakaba 1.1 <body class=draft>
45     <div class=head>
46     <p><span class=ja-translation lang=ja><a class=logo href="http://www.whatwg.org/" rel=home><img lang=en alt=WHATWG
47     src="http://www.whatwg.org/images/logo"></a></span></p>
48    
49     <h1 id=web-workers>Web Workers</h1>
50    
51     <h2 class="no-num no-toc" id=draft><span class=ja-translation lang=ja>勧告案<ins class=ja-note>の非公式和訳</ins> &mdash;
52 wakaba 1.26 <ins class=ja-note>(原文出版日)</ins> 2008年8月10日</span></h2>
53 wakaba 1.1
54     <p><span class=ja-translation lang=ja>みなさんにも作業にご参加いただけます。 <a
55     href="http://www.whatwg.org/mailing-list">作業部会の議論用メイリング・リストに参加してください。</a></span></p>
56    
57     <p><span class=ja-translation lang=ja><strong>Web デザイナー!</strong> みなさんのための <a
58     href="http://blog.whatwg.org/faq/" lang=en>FAQ</a>、<a
59     href="http://forums.whatwg.org/">掲示板</a>、<a
60     href="http://www.whatwg.org/mailing-list#help">ヘルプ用メイリング・リスト</a>があります!
61    
62     </span><p>
63    
64     <!-- Japanese-translation-only notes -->
65 wakaba 1.3 <ins class=ja-note style="display: block" lang=ja>
66 wakaba 1.1 <p>この文書は、 WHATWG HTML5 仕様書の日本語訳です。
67     HTML5 の正式な仕様書は英語版だけであり、
68     この日本語訳はあくまで参考としてご利用ください。
69    
70 wakaba 1.3 <p lang=en>This document is a Japanese translation of
71 wakaba 1.1 the WHATWG HTML5 specification. Note that the only normative specification
72     for the HTML5 is the original English version and this Japanese
73     translation is non‐normative.
74 wakaba 1.3
75     <p>日本語訳は <a href="edit/" rel=edit>Web 界面</a>を使って編集できます。
76     編集結果はおよそ3時間おきに反映されます。
77     また、原文の変更には毎日自動的に追随しています。
78    
79 wakaba 1.8 <p lang=en>The Japanese text can be edited using the <a href="edit/" rel=edit>Web
80 wakaba 1.3 interface</a>. Any modifications will be reflected to this translation
81     in about three hours. Note that any changes to the original specification
82     will automatically be incorporated to this translation every day.
83 wakaba 1.1 </ins>
84    
85     <p><span><!-- dummy --></span></p>
86    
87     <dl>
88     <dt><span class=ja-translation lang=ja><ins class=ja-note>原文の</ins>この版</span><dd><a
89 wakaba 1.13 href="http://www.whatwg.org/specs/web-workers/current-work/">http://whatwg.org/ww</a>
90 wakaba 1.1
91     <dt><span class=ja-translation lang=ja><ins class=ja-note>原文の</ins>改版履歴:</span><dd><span class=ja-translation lang=ja><span lang=en>Twitter</span> メッセージ (編集上の変更以外): <a
92 wakaba 1.20 href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a></span><dd><span class=ja-translation lang=ja>Commit-Watchers メイリング・リスト: <a
93     href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a></span><dd><span class=ja-translation lang=ja>対話的 <span lang=en>Web</span> 界面: <a href="http://html5.org/tools/web-workers-tracker">http://html5.org/tools/web-workers-tracker</a></span><dd><span class=ja-translation lang=ja><span lang=en>Subversion</span> 界面: <a href="http://svn.whatwg.org/webworkers/">http://svn.whatwg.org/webworkers/</a></span><dt><span class=ja-translation lang=ja>問題:</span><dd><span class=ja-translation lang=ja>フィードバックの送信先: <a
94 wakaba 1.1 href="http://www.whatwg.org/mailing-list">[email protected]</a></span><dd><span class=ja-translation lang=ja>フィードバックの閲覧・投票: <a
95     href="http://www.whatwg.org/issues/">http://www.whatwg.org/issues/</a></span><dt><span class=ja-translation lang=ja>編集者:</span><dd>Ian Hickson, Google, [email protected]
96     </dl>
97    
98     <p class=copyright>&copy; Copyright 2004-2008 Apple Computer, Inc.,
99     Mozilla Foundation, and Opera Software ASA.</p>
100    
101     <p class=copyright>You are granted a license to use, reproduce and create
102     derivative works of this document.</p>
103     </div>
104    
105     <hr>
106    
107     <h2 class="no-num no-toc" id=abstract><span class=ja-translation lang=ja>概要</span></h2>
108    
109 wakaba 1.14 <p><span class=ja-translation lang=ja>この仕様書は、 Web アプリケーションで、主たる頁とは並列に、
110     背景労働者スクリプトを走らせるための API を定義します。
111     これにより、メッセージ・パッシングによるスレッド的操作を協調機構として利用できるようになります。</span><h2 class="no-num no-toc" id=status><span class=ja-translation lang=ja>この文書の状態</span></h2>
112 wakaba 1.1
113     <p><span class=ja-translation lang=ja><strong>この文書は作業中です!</strong>
114     この文書は、コメントへの対応や通常の開発の過程として、
115     毎時毎分とは言い過ぎですが毎日のペースで変更されています。
116     コメントは非常に歓迎していますので、
117     <a href="mailto:[email protected]">[email protected]</a>
118     へお送りください。よろしくお願いします。</span><p><span class=ja-translation lang=ja>現在の焦点は、第1案の開発です。</span><p><span class=ja-translation lang=ja>実装者は、この仕様書が安定していないことに注意するべきです。
119     <strong>実装者は議論に参加しておかないと、
120     仕様がその実装とは非互換に変更されてしまうかもしれません。</strong>
121     この仕様書がいずれ実装呼び掛けの段階に達するのよりも前にこの仕様を実装したい販売者は、
122 wakaba 1.7 <a href="http://www.whatwg.org/mailing-list">WHATWG メイリング・リスト</a>に参加し、
123 wakaba 1.1 議論に加わるべきです。</span><p><span class=ja-translation lang=ja>この仕様書は <a
124     href="http://www.w3.org/html/wg">W3C HTML WG</a>
125     によっても出版されています。2つの仕様書の目次以降は同一です。</span><h2 class="no-num no-toc" id=contents><span class=ja-translation lang=ja>目次
126     </span></h2>
127     <!--begin-toc-->
128    
129     <ul class=toc>
130     <li><a href="#introduction"><span class=secno>1. </span>Introduction</a>
131     <ul class=toc>
132 wakaba 1.9 <li><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a>
133 wakaba 1.23 <ul class=toc>
134     <li><a href="#a-background"><span class=secno>1.1.1 </span>A
135     background number-crunching worker</a>
136    
137     <li><a href="#a-worker"><span class=secno>1.1.2 </span>A worker for
138     updating a client-side database</a>
139    
140     <li><a href="#worker"><span class=secno>1.1.3 </span>Worker used for
141     backgroud I/O</a>
142    
143     <li><a href="#shared"><span class=secno>1.1.4 </span>Shared
144     workers</a>
145    
146     <li><a href="#delegation"><span class=secno>1.1.5
147     </span>Delegation</a>
148    
149     <li><a href="#granting"><span class=secno>1.1.6 </span>Granting
150     capabilities</a>
151     </ul>
152 wakaba 1.1
153 wakaba 1.16 <li><a href="#conformance"><span class=secno>1.2 </span>Conformance
154 wakaba 1.1 requirements</a>
155     <ul class=toc>
156 wakaba 1.16 <li><a href="#dependencies"><span class=secno>1.2.1
157 wakaba 1.1 </span>Dependencies</a>
158     </ul>
159    
160 wakaba 1.16 <li><a href="#terminology"><span class=secno>1.3 </span>Terminology</a>
161 wakaba 1.9 </ul>
162    
163 wakaba 1.13 <li><a href="#infrastructure"><span class=secno>2.
164     </span>Infrastructure</a>
165 wakaba 1.9 <ul class=toc>
166 wakaba 1.22 <li><a href="#the-workerglobalscope"><span class=secno>2.1 </span>The
167     <code>WorkerGlobalScope</code> interface</a>
168 wakaba 1.9
169 wakaba 1.16 <li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
170     origins of workers</a>
171    
172     <li><a href="#the-queue"><span class=secno>2.3 </span>The queue of
173 wakaba 1.9 events</a>
174    
175 wakaba 1.16 <li><a href="#the-workers"><span class=secno>2.4 </span>The worker's
176 wakaba 1.11 ports</a>
177    
178 wakaba 1.16 <li><a href="#processing"><span class=secno>2.5 </span>Processing
179 wakaba 1.9 model</a>
180 wakaba 1.13
181 wakaba 1.16 <li><a href="#creating"><span class=secno>2.6 </span>Creating
182 wakaba 1.13 workers</a>
183 wakaba 1.9 </ul>
184    
185 wakaba 1.13 <li><a href="#apis-available"><span class=secno>3. </span>APIs available
186     to workers</a>
187 wakaba 1.22 <ul class=toc>
188     <li><a href="#importing"><span class=secno>3.1 </span>Importing scripts
189     and libraries</a>
190    
191     <li><a href="#apis-defined"><span class=secno>3.2 </span>APIs defined in
192     other specifications</a>
193    
194     <li><a href="#interface"><span class=secno>3.3 </span>Interface objects
195     and constructors</a>
196 wakaba 1.23
197     <li><a href="#worker0"><span class=secno>3.4 </span>Worker locations</a>
198    
199 wakaba 1.22 </ul>
200 wakaba 1.1
201     <li class=no-num><a href="#references">References</a>
202    
203     <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
204     </ul>
205     <!--end-toc-->
206    
207     <hr>
208    
209 wakaba 1.3 <h2 id=introduction><span class=ja-translation lang=ja><span class=secno>1. </span>はじめに</span></h2>
210 wakaba 1.1
211 wakaba 1.12 <h3 id=tutorial><span class=ja-translation lang=ja><span class=secno>1.1 </span>解説</span></h3>
212 wakaba 1.1
213 wakaba 1.23 <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>There are a variety of uses that workers can be put to. The following
214     subsections show various examples of this use.
215    
216     <h4 id=a-background><span class=secno>1.1.1 </span>A background
217     number-crunching worker</h4>
218    
219     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p><span class=ja-translation lang=ja>simplest use of workers is for performing a computationally expensive task without interrupting the user 界面。
220     </span><p>In this example, the main document spawns a worker to (na&iuml;vely)
221     compute prime numbers, and progressively displays the most recently found
222     prime number.
223    
224     <p>The main page is as follows:
225    
226     <pre>&lt;!DOCTYPE HTML>
227     &lt;html>
228     &lt;head>
229     &lt;title>Worker example: One-core computation&lt;/title>
230     &lt;/head>
231     &lt;body>
232     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
233     &lt;script>
234     var worker = createWorker('worker.js');
235 wakaba 1.25 worker.port.onmessage = function (event) {
236 wakaba 1.23 document.getElementById('result').textContent = event.message;
237     };
238     &lt;/script>
239     &lt;/body>
240     &lt;/html></pre>
241    
242     <p>The <code title=dom-WorkerFactory-createWorker><a
243     href="#createworker">createWorker()</a></code> method call creates a
244 wakaba 1.25 worker and returns a <code><a href="#worker1">Worker</a></code> object
245     representing that worker and containing a <code>MessagePort</code> object,
246     which is used to communicate with the worker. That object's <code
247 wakaba 1.23 title=dom-MessagePort-onmessage>onmessage</code> event handler attribute
248     allows the code to receive messages from the worker.
249    
250     <p>The worker itself is as follows:
251    
252     <pre>var n = 1;
253     search: while (true) {
254     n += 1;
255     for (var i = 2; i &lt;= Math.sqrt(n); i += 1)
256     if (n % i == 0)
257     continue search;
258     // found a prime!
259     port.postMessage(n);
260     }</pre>
261    
262     <p>The bulk of this code is simply an unoptimised search for a prime
263     number. To send a message back to the page, the <code
264     title=dom-WorkerGlobalScope-port><a href="#port">port</a></code> variable
265     (defined automatically when the worker is created) is used to post a
266     message when a prime is found.
267    
268     <p><a href="http://www.whatwg.org/demos/workers/primes/page.html">View this
269     example online</a>.
270    
271     <h4 id=a-worker><span class=secno>1.1.2 </span>A worker for updating a
272     client-side database</h4>
273    
274     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>In this example, the main document spawns a worker whose only task is to
275     listen for notifications from the server, and, when appropriate, either
276     add or remove data from the client-side database.
277    
278     <p>Since no communication occurs between the worker and the main page, the
279     main page can start the worker by just doing:
280    
281     <pre>&lt;script>
282     createWorker('worker.js');
283     &lt;/script></pre>
284    
285     <p>The worker itself is as follows:
286    
287     <pre>var server = new WebSocket('ws://whatwg.org/database');
288 wakaba 1.25 var database = openDatabase('demobase', '1.0', 'Demo Database', 10240);
289 wakaba 1.23 server.onmessage = function (event) {
290     // data is in the format "command key value"
291     var data = event.message.split(' ');
292     switch (data[0]) {
293     case '+':
294     database.transaction(function(tx) {
295     tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
296     });
297     case '-':
298     database.transaction(function(tx) {
299     tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
300     });
301     }
302     };</pre>
303    
304     <p>This connects to the server using the <code>WebSocket</code> mechanism
305     and opens the local database (which, we presume, has been created
306     earlier). The worker then just listens for messages from the worker and
307     acts on them as appropriate, forever (or until the main page is closed).
308    
309     <p><a
310     href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
311     this example online</a>. (This example will not actually function, since
312     the server does not actually exist and the database is not created by this
313     sample code.)
314    
315     <h4 id=worker><span class=secno>1.1.3 </span>Worker used for backgroud I/O</h4>
316    
317     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>In this example, the main document uses two workers, one for fetching
318     stock updates for at regular intervals, and one for fetching performing
319     search queries that the user requests.
320    
321     <p>The main page is as follows:
322    
323     <pre>&lt;!DOCTYPE HTML>
324     &lt;html>
325     &lt;head>
326     &lt;title>Worker example: Stock ticker&lt;/title>
327     &lt;script>
328     // TICKER
329     var symbol = 'GOOG'; // default symbol to watch
330     var ticker = createWorker('ticker.js');
331 wakaba 1.25 ticker.port.postMessage(symbol);
332 wakaba 1.23
333     // SEARCHER
334     var searcher = createWorker('searcher.js');
335     function search(query) {
336 wakaba 1.25 searcher.port.postMessage(query);
337 wakaba 1.23 }
338    
339     // SYMBOL SELECTION UI
340     function select(newSymbol) {
341     symbol = newSymbol;
342 wakaba 1.25 ticker.port.postMessage(symbol);
343 wakaba 1.23 }
344     &lt;/script>
345     &lt;/head>
346     &lt;body>
347     &lt;p>&lt;output id="symbol">&lt;/output> &lt;output id="value">&lt;/output>&lt;/p>
348     &lt;script>
349 wakaba 1.25 ticker.port.onmessage = function (event) {
350 wakaba 1.23 var data = event.message.split(' ');
351     document.getElementById('symbol').textContent = data[0];
352     document.getElementById('value').textContent = data[1];
353     };
354     &lt;/script>
355     &lt;p>&lt;label>Search: &lt;input type="text" oninput="search(this.value)">&lt;/label>&lt;/p>
356     &lt;ul id="results">&lt;/ul>
357     &lt;script>
358 wakaba 1.25 searcher.port.onmessage = function (event) {
359 wakaba 1.23 var data = event.message.split(' ');
360     var results = document.getElementById('results');
361     while (results.hasChildNodes()) // clear previous results
362     results.removeChild(results.firstChild);
363     for (var i = 0; i &lt; data.length; i += 1) {
364     // add a list item with a button for each result
365     var li = document.createElement('li');
366     var button = document.createElement('button');
367     button.value = data[i];
368     button.type = 'button';
369     button.onclick = function () { select(this.value); };
370     button.textContent = data[i];
371     li.appendChild(button);
372     results.appendChild(li);
373     }
374     };
375     &lt;/script>
376     &lt;p>(The data in this example is not real. Try searching for "Google" or "Apple".)&lt;/p>
377     &lt;/body>
378     &lt;/html></pre>
379    
380     <p>Messages are queued until the <code
381     title=handler-MessagePort-onmessage>onmessage</code> handler is set, so
382     even if it takes a while for the message handler to be attached, no data
383     is lost.
384    
385     <p>The two workers use a common library for performing the actual network
386     calls. This library is as follows:
387    
388     <pre>function get(url) {
389     try {
390     var xhr = new XMLHttpRequest();
391     xhr.open('GET', url, false);
392     xhr.send();
393     return xhr.responseText;
394     } catch (e) {
395     return ''; // turn all errors into empty results
396     }
397     }</pre>
398    
399     <p>The stock updater worker is as follows:
400    
401 wakaba 1.25 <pre>importScripts('io.js');
402 wakaba 1.23 var timer;
403     var symbol;
404     function update() {
405     port.postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
406     timer = setTimeout(update, 10000);
407     }
408     port.onmessage = function (event) {
409     if (timer)
410     clearTimeout(timer);
411     symbol = event.message;
412     update();
413     };</pre>
414    
415     <p>The search query worker is as follows:
416    
417 wakaba 1.25 <pre>importScripts('io.js');
418 wakaba 1.23 port.onmessage = function (event) {
419     port.postMessage(get('search.cgi?' + event.message));
420     };</pre>
421    
422     <p><a href="http://www.whatwg.org/demos/workers/stocks/page.html">View this
423     example online</a>.
424    
425     <h4 id=shared><span class=secno>1.1.4 </span>Shared workers</h4>
426    
427     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>In this example, multiple windows (viewers) can be opened that are all
428     viewing the same map. All the windows share the same map information, with
429     a single worker coordinating all the viewers. Each viewer can move around
430     idependently, but if they set any data on the map, all the viewers are
431     updated.
432    
433     <p>The main page isn't interesting, it merely provides a way to open the
434     viewers:
435    
436     <pre>&lt;!DOCTYPE HTML>
437     &lt;html>
438     &lt;head>
439     &lt;title>Workers example: Multiviewer&lt;/title>
440     &lt;script>
441     function openViewer() {
442     window.open('viewer.html');
443     }
444     &lt;/script>
445     &lt;/head>
446     &lt;body>
447     &lt;p>&lt;button type=button onclick="openViewer()">Open a new
448     viewer&lt;/button>&lt;/p>
449     &lt;p>Each viewer opens in a new window. You can have as many viewers
450     as you like, they all view the same data.&lt;/p>
451     &lt;/body>
452     &lt;/html></pre>
453    
454     <p>The viewer is more involved:
455    
456     <pre>&lt;!DOCTYPE HTML>
457     &lt;html>
458     &lt;head>
459     &lt;title>Workers example: Multiviewer viewer&lt;/title>
460     &lt;script>
461     var worker = createNamedWorker('worker.js', 'core');
462    
463     // CONFIGURATION
464     function configure(event) {
465     if (event.message.substr(0, 4) != 'cfg ') return;
466     var name = event.message.substr(4).split(' ', 1);
467     // update display to mention our name is name
468     document.getElementsByTagName('h1')[0].textContent += ' ' + name;
469     // no longer need this listener
470 wakaba 1.25 worker.port.removeEventListener('message', configure, false);
471 wakaba 1.23 }
472 wakaba 1.25 worker.port.addEventListener('message', configure, false);
473 wakaba 1.23
474     // MAP
475     function paintMap(event) {
476     if (event.message.substr(0, 4) != 'map ') return;
477     var data = event.message.substr(4).split(',');
478     // display tiles data[0] .. data[8]
479     var canvas = document.getElementById('map');
480     var context = canvas.getContext('2d');
481     for (var y = 0; y &lt; 3; y += 1) {
482     for (var x = 0; x &lt; 3; x += 1) {
483     var tile = data[y * 3 + x];
484     if (tile == '0')
485     context.fillStyle = 'green';
486     else
487     context.fillStyle = 'maroon';
488     fillRect(x * 50, y * 50, 50, 50);
489     }
490     }
491     }
492 wakaba 1.25 worker.port.addEventListener('message', paintMap, false);
493 wakaba 1.23
494     // PUBLIC CHAT
495     function updatePublicChat(event) {
496     if (event.message.substr(0, 4) != 'txt ') return;
497     var name = event.message.substr(4).split(' ', 1);
498     var message = event.message.substr(4 + length(name) + 1);
499     // display "&lt;name> message" in public chat
500     var dialog = document.getElementById('public');
501     var dt = document.createElement('dt');
502     dt.textContent = name;
503     dialog.appendChild(dt);
504     var dd = document.createElement('dd');
505     dd.textContent = message;
506     dialog.appendChild(dd);
507     }
508 wakaba 1.25 worker.port.addEventListener('message', updatePublicChat, false);
509 wakaba 1.23
510     // PRIVATE CHAT
511     function startPrivateChat(event) {
512     if (event.message.substr(0, 4) != 'msg ') return;
513     var name = event.message.substr(4).split(' ', 1);
514     var port = event.port;
515     // display a private chat UI
516     var ul = document.getElementById('private');
517     var li = document.createElement('li');
518     var h3 = document.createElement('h3');
519     h3.textContent = 'Private chat with ' + name;
520     li.appendChild(h3);
521     var dialog = document.createElement('dialog');
522     var addMessage = function(name, message) {
523     var dt = document.createElement('dt');
524     dt.textContent = name;
525     dialog.appendChild(dt);
526     var dd = document.createElement('dd');
527     dd.textContent = message;
528     dialog.appendChild(dd);
529     };
530     port.onmessage = function (event) {
531     addMessage(name, event.message);
532     };
533     li.appendChild(dialog);
534     var form = document.createElement('form');
535     var p = document.createElement('p');
536     var input = document.createElement('input');
537     input.size = 50;
538     p.appendChild(input);
539     p.appendChild(document.createTextNode(' '));
540     var button = document.createElement('button');
541     button.textContent = 'Post';
542     p.appendChild(button);
543     form.onsubmit = function () {
544     port.postMessage(input.value);
545     addMessage('me', input.value);
546     input.value = '';
547     return false;
548     };
549     form.appendChild(p);
550     li.appendChild(form);
551     }
552 wakaba 1.25 worker.port.addEventListener('message', startPrivateChat, false);
553 wakaba 1.23
554     function init() {
555 wakaba 1.25 // begin receiving messages (messages are queued until you either
556     // set worker.port.onmessage or call worker.port.start())
557     worker.port.start();
558 wakaba 1.23 }
559     &lt;/script>
560     &lt;/head>
561     &lt;body onload="init()">
562     &lt;h1>Viewer&lt;/h1>
563     &lt;h2>Map&lt;/h2>
564     &lt;p>&lt;canvas id="map" height=150 width=150>&lt;/canvas>&lt;/p>
565     &lt;p>
566 wakaba 1.25 &lt;button type=button onclick="worker.port.postMessage('mov left')">Left&lt;/button>
567     &lt;button type=button onclick="worker.port.postMessage('mov up')">Up&lt;/button>
568     &lt;button type=button onclick="worker.port.postMessage('mov down')">Down&lt;/button>
569     &lt;button type=button onclick="worker.port.postMessage('mov right')">Right&lt;/button>
570     &lt;button type=button onclick="worker.port.postMessage('set 0')">Set 0&lt;/button>
571     &lt;button type=button onclick="worker.port.postMessage('set 1')">Set 1&lt;/button>
572 wakaba 1.23 &lt;/p>
573     &lt;h2>Public Chat&lt;/h2>
574     &lt;dialog id="public">&lt;/dialog>
575 wakaba 1.25 &lt;form onsubmit="worker.port.postMessage('txt ' + message.value); message.value = ''; return false;">
576 wakaba 1.23 &lt;p>
577     &lt;input type="text" name="message" size="50">
578     &lt;button>Post&lt;/button>
579     &lt;/p>
580     &lt;/form>
581     &lt;h2>Private Chat&lt;/h2>
582     &lt;ul id="private">&lt;/ul>
583     &lt;/body>
584 wakaba 1.25 &lt;/html>
585     </pre>
586 wakaba 1.23
587     <p>There are several key things worth noting about the way the viewer is
588     written.
589    
590     <p><strong>Multiple listeners</strong>. Instead of a single message
591     processing function, the code here attaches multiple event listeners, each
592     one performing a quick check to see if it is relevant for the message. In
593     this example it doesn't make much difference, but if multiple authors
594     wanted to collaborate using a single port to communicate with a worker, it
595     would allow for independent code instead of changes having to all be made
596     to a single event handling function.
597    
598     <p>Because event listeners are registered using <code
599     title="">addEventListener()</code> instead of <code
600     title=handler-MessagePort-onmessage>onmessge</code>, the events remain
601     queued until the <code title=dom-MessagePort-start>start()</code> method
602     is called on the message port, in the <code title="">init()</code>
603     function called from the <code title=handler-onload>onload</code> handler.
604    
605     <p>Registering event listeners in this way also allows you to unregister
606     specific listeners when you are done with them, as is done with the <code
607     title="">configure()</code> method in this example.
608    
609     <p>Finally, the worker:
610    
611     <pre>
612     var nextName = 0;
613     function getNextName() {
614     // this could use more friendly names
615     // but for now just return a number
616     return nextName++;
617     }
618    
619     var map = [
620     [0, 0, 0, 0, 0, 0, 0],
621     [1, 1, 0, 1, 0, 1, 1],
622     [0, 1, 0, 1, 0, 0, 0],
623     [0, 1, 0, 1, 0, 1, 1],
624     [0, 0, 0, 1, 0, 0, 0],
625     [1, 0, 0, 1, 1, 1, 1],
626     [1, 1, 0, 1, 1, 0, 1],
627     ];
628    
629     function wrapX(x) {
630     if (x &lt; 0) return wrapX(x + map[0].length);
631     if (x >= map[0].length) return wrapX(x - map[0].length);
632     return x;
633     }
634    
635     function wrapY(y) {
636     if (y &lt; 0) return wrapY(y + map.length);
637     if (y >= map[0].length) return wrapY(y - map.length);
638     return y;
639     }
640    
641     function sendMapData(viewer) {
642     var data = '';
643     for (var y = viewer.y-1; y &lt;= viewer.y+1; y += 1) {
644     for (var x = viewer.x-1; x &lt;= viewer.x+1; x += 1) {
645     if (data != '')
646     data += ',';
647     data += map[y][x];
648     }
649     }
650     viewer.port.postMessage('map ' + data);
651     }
652    
653     var viewers = {};
654     onconnect = function (event) {
655     event.port._name = getNextName();
656     event.port._data = { port: event.port, x: 0, y: 0, };
657     viewers[event.port._name] = event.port._data;
658     event.port.postMessage('cfg ' + name);
659     event.port.onmessage = getMessage;
660     sendMapData(event.port._data);
661     };
662    
663     function getMessage(event) {
664     switch (event.message.substr(0, 4)) {
665     case 'mov ':
666     var direction = event.message.substr(4);
667     var dx = 0;
668     var dy = 0;
669     switch (direction) {
670     case 'up': dy = -1; break;
671     case 'down': dy = 1; break;
672     case 'left': dx = -1; break;
673     case 'right': dx = 1; break;
674     }
675     event.target._data.x = wrapX(event.target._data.x + dx);
676     event.target._data.y = wrapY(event.target._data.y + dy);
677     sendMapData(event.target._data);
678     break;
679     case 'set ':
680     var value = event.message.substr(4);
681     map[event.target._data.y][event.target._data.x] = value;
682     for (var viewer in viewers)
683     sendMapData(viewers[viewer]);
684     break;
685     case 'txt ':
686     var name = event.target._name;
687     var message = event.message.substr(4);
688     for (var viewer in viewers)
689     viewers[viewer].port.postMessage('txt ' + name + ' ' + message);
690     break;
691     case 'msg ':
692     var party1 = event._data;
693     var party2 = viewers[event.message.substr(4).split(' ', 1)];
694     if (party2) {
695     var channel = new MessageChannel();
696     party1.port.postMessage('msg ' + party2.name, channel.port1);
697     party2.port.postMessage('msg ' + party1.name, channel.port2);
698     }
699     break;
700     }
701     }</pre>
702    
703     <p><strong>Connecting to multiple pages</strong>. Instead of using the
704     <code title=dom-WorkerGlobalScope-port><a href="#port">port</a></code>
705     global variable in the worker, the script uses the <code
706     title=handler-WorkerGlobalScope-onconnect><a
707     href="#onconnect">onconnect</a></code> event listener to listen for
708     multiple connections.
709    
710     <p><strong>Direct channels</strong>. When the worker receives a "msg"
711     message from one viewer naming another viewer, it sets up a direct
712     connection between the two, so that the two viewers can communicate
713     directly without the worker having to proxy all the messages.
714    
715     <p><a href="http://www.whatwg.org/demos/workers/multiviewer/page.html">View
716     this example online</a>.
717    
718     <h4 id=delegation><span class=secno>1.1.5 </span>Delegation</h4>
719    
720     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>With multicore CPUs becoming prevalent, one way to obtain better
721     performance is to split computationally expensive tasks amongst multiple
722     workers. In this example, a computationally expensive task that is to be
723     performed for every number from 1 to 10,000,000 is farmed out to ten
724     subworkers.
725    
726     <p>The main page is as follows, it just reports the result:
727    
728     <pre>&lt;!DOCTYPE HTML>
729     &lt;html>
730     &lt;head>
731     &lt;title>Worker example: One-core computation&lt;/title>
732     &lt;/head>
733     &lt;body>
734     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
735     &lt;script>
736     var worker = createWorker('worker.js');
737 wakaba 1.25 worker.port.onmessage = function (event) {
738 wakaba 1.23 document.getElementById('result').textContent = event.message;
739     };
740     &lt;/script>
741     &lt;/body>
742     &lt;/html></pre>
743    
744     <p>The worker itself is as follows:
745    
746     <pre>// settings
747     var num_workers = 10;
748     var items_per_worker = 1000000;
749    
750     // start the workers
751     var result = 0;
752     var pending_workers = num_workers;
753     for (var i = 0; i &lt; num_workers; i += 1) {
754     var worker = createWorker('core.js');
755 wakaba 1.25 worker.port.postMessage(i * items_per_worker);
756     worker.port.postMessage((i+1) * items_per_worker);
757     worker.port.onmessage = storeResult;
758 wakaba 1.23 }
759    
760     // handle the results
761     function storeResult(event) {
762     result += 1*event.message;
763     pending_workers -= 1;
764     if (pending_workers &lt;= 0)
765     port.postMessage(result); // finished!
766     }</pre>
767    
768     <p>It consists of a loop to start the subworkers, and then a handler that
769     waits for all the subworkers to respond.
770    
771     <p>The subworkers are implemented as follows:
772    
773     <pre>var start;
774     onmessage = getStart;
775     function getStart(event) {
776     start = 1*event.message;
777     onmessage = getEnd;
778     }
779    
780     var end;
781     function getEnd(event) {
782     end = 1*event.message;
783     onmessage = null;
784     do();
785     }
786    
787     function do() {
788     var result = 0;
789     for (var i = start; i &lt; end; i += 1) {
790     // perform some complex calculation here
791     result += 1;
792     }
793     port.postMessage(result);
794     }</pre>
795    
796     <p>They receive two numbers in two events, perform the computation for the
797     range of numbers thus specified, and then report the result back to the
798     parent.
799    
800     <p><a href="http://www.whatwg.org/demos/workers/multicore/page.html">View
801     this example online</a>.
802    
803     <h4 id=granting><span class=secno>1.1.6 </span>Granting capabilities</h4>
804    
805     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class=big-issue>...
806 wakaba 1.9
807 wakaba 1.16 <h3 id=conformance><span class=ja-translation lang=ja><span class=secno>1.2 </span>適合性要件</span></h3>
808 wakaba 1.1
809 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書のすべての図、例示、注意書き、ならびに規定の一部ではないと明記したすべての節は、
810 wakaba 1.1 規定の一部ではありません。
811 wakaba 1.24 この仕様書のそれ以外の部分はすべて規定の一部です。</span><p><span class=ja-translation lang=ja>この文書の規定の部分にあるキーワード「<em class=rfc2119 title="MUST">しなければなりません</em>」、
812 wakaba 1.1 「<em class=rfc2119 title="MUST NOT">してはなりません</em>」、
813 wakaba 1.24 「<em class=rfc2119 title="REQUIRED">必須です</em>」、<!--"SHALL", "SHALL
814     NOT",-->「<em class=rfc2119 title="SHOULD">するべきです</em>」、
815 wakaba 1.1 「<em class=rfc2119 title="SHOULD NOT">するべきではありません</em>」、
816     「<em class=rfc2119 title="RECOMMENDED">推奨します</em>」、「<em class=rfc2119 title="MAY">して構いません</em>」、
817     「<em class=rfc2119 title="OPTIONAL">任意です</em>」は、 RFC 2119 で説明されているように解釈します。
818 wakaba 1.24 この仕様書<ins class=ja-note>の原文</ins>では、
819     可読性の観点から、
820     これらの語<ins class=ja-note>の原語</ins>のすべて大文字による表記は行っていません。
821 wakaba 1.1 <a href="#refsRFC2119">[RFC2119]</a></span></p>
822     <!-- XXX but they should be
823     marked up -->
824    
825     <p><span class=ja-translation lang=ja>算法の一部として<ins class=ja-note>動作の指示の形で (原文では</ins>命令形で<ins class=ja-note>) </ins>記述された要件
826     (例えば「先頭の間隔文字をすべて除去します」や「偽を返し、これらの段階を停止します」)
827 wakaba 1.24 は、算法を紹介するに当たって使われたキーワード (<em class=rfc2119 title="MUST">しなければなりません</em>、
828 wakaba 1.1 <em class=rfc2119 title="SHOULD">するべきです</em>、<em class=rfc2119 title="MAY">して構いません</em>など) の意味により解釈します。</span><p><span class=ja-translation lang=ja>いくつかの適合性要件は属性やメソッドやオブジェクトの要件として説明されています。
829     そのような要件は、利用者エージェントに関する要件として解釈します。</span><p><span class=ja-translation lang=ja>算法や特定の段階として説明されている適合性要件は、
830     最終的な結果が等価である限り、任意の方法で実装して<em class=rfc2119 title="MAY">構いません</em>。
831     (特に、この仕様書で定義されている算法は追うのを簡単にしており、
832 wakaba 1.24 高性能であることを目指してはいません。)</span><p><span class=ja-translation lang=ja>この仕様書で定義する唯一の適合性の級は利用者エージェントです。</span><p><span class=ja-translation lang=ja>利用者エージェントは、サービス拒否攻撃を防ぐため、
833 wakaba 1.1 記憶容量を使い尽くすことを防ぐため、
834     あるいは環境特有の制限に対応するためなどの目的で、
835 wakaba 1.16 別段の制約のない入力に対して実装特有の制限を設けて<em class=rfc2119 title="MAY">構いません</em>。</span><h4 id=dependencies><span class=ja-translation lang=ja><span class=secno>1.2.1 </span>依存性</span></h4>
836 wakaba 1.1
837 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書は、他のいくつかの下位の仕様書に依存しています。</span><dl>
838 wakaba 1.1 <dt>HTML5
839    
840     <dd>
841 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書では、 HTML5 の多くの基礎的な概念を使用します。
842 wakaba 1.1 <a href="#refsHTML5">[HTML5]</a></span></p>
843    
844     <dt>ECMAScript
845    
846     <dd>
847 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書は、スクリプト言語として JavaScript
848 wakaba 1.1 を使うことを想定しています。 <a href="#refsJS">[JS]</a></span></p>
849    
850     <dt>WebIDL
851    
852     <dd>
853 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書の IDL ブロックは、WebIDL 仕様書における意味を用います。 <a href="#refsWebIDL">[WebIDL]</a></span></p>
854 wakaba 1.1 </dl>
855    
856 wakaba 1.16 <h3 id=terminology><span class=ja-translation lang=ja><span class=secno>1.3 </span>用語</span></h3>
857 wakaba 1.1
858 wakaba 1.24 <p><span class=ja-translation lang=ja>簡単のため、文書が利用者に対してレンダリングされる方法に言及する時に<em>示す</em>、
859     <em>表示する</em>、<em>可視</em>などの用語を使うことがあります。
860     このような用語は特に視覚媒体を暗示しているわけではありません。
861     他の媒体であってもこれらの用語が等価な方法で適用されるものと考えなければ<em class=rfc2119 title="MUST">なりません</em>。</span><p><span class=ja-translation lang=ja>「<code title="">Foo</code> オブジェクト」という構文 (<code title="">Foo</code>
862 wakaba 1.10 は実際には界面) を時々使いますが、より正確な「界面 <code title="">Foo</code>
863     を実装しているオブジェクト」の代わりに用いています。</span><p><span class=ja-translation lang=ja>DOM という語は、 Web アプリケーションでスクリプトに利用可能とされている API
864 wakaba 1.1 の集合を指して用い、必ずしも DOM 中核仕様書で定義されている実際の
865     <code>Document</code> オブジェクトその他の <code>Node</code>
866     オブジェクトの存在を暗示してはいません。<a
867     href="#refsDOM3CORE">[DOM3CORE]</a></span><p><span class=ja-translation lang=ja>DOM 属性は、その値が (例えば著者のスクリプトにより) 取り出される時<em>取得される</em>といい、
868     新しい値がその属性に割り当てられる時<em>設定される</em>といいます。</span><p><span class=ja-translation lang=ja>DOM オブジェクトが<dfn id=live>生きている</dfn>という場合、
869     そのオブジェクトを返す属性は常に同じオブジェクトを返さなければ<em class=rfc2119 title="MUST">ならない</em>
870     (毎回新しいオブジェクトを返すのではなく) 上に、
871     そのオブジェクトの属性やメソッドがそのオブジェクトの裏にある実際のデータのスナップ写真ではなく、
872 wakaba 1.21 データそのものに対して操作されなければ<em class=rfc2119 title="MUST">ならない</em>ことを意味します。
873    
874    
875    
876    
877    
878    
879     </span><h2 id=infrastructure><span class=ja-translation lang=ja><span class=secno>2. </span>基盤</span></h2>
880 wakaba 1.9
881 wakaba 1.22 <h3 id=the-workerglobalscope><span class=ja-translation lang=ja><span class=secno>2.1 </span><code><a href="#workerglobalscope">WorkerGlobalScope</a></code> 界面</span></h3>
882 wakaba 1.9
883     <pre
884 wakaba 1.22 class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
885     // core worker features
886     readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
887 wakaba 1.23 readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
888 wakaba 1.22 readonly attribute DOMString <a href="#name" title=dom-WorkerGlobalScope-name>name</a>;
889     readonly attribute boolean <a href="#closing" title=dom-WorkerGlobalScope-closing>closing</a>;
890     void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
891    
892 wakaba 1.11 // event handler attributes
893 wakaba 1.22 attribute <span>EventListener</span> <a href="#onconnect" title=handler-WorkerGlobalScope-onconnect>onconnect</a>;
894     attribute <span>EventListener</span> <a href="#onunload" title=handler-WorkerGlobalScope-onunload>onunload</a>;
895 wakaba 1.9 };</pre>
896    
897 wakaba 1.23 <p>Objects implementing the <code><a
898     href="#workerglobalscope">WorkerGlobalScope</a></code> interface must also
899     implement the <code>EventTarget</code> interface.
900    
901 wakaba 1.22 <p>The <dfn id=self
902     title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
903     return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
904     object itself.
905    
906 wakaba 1.23 <p>The <dfn id=location
907     title=dom-WorkerGlobalScope-location><code>location</code></dfn> attribute
908     must return the <code><a href="#workerlocation">WorkerLocation</a></code>
909     object created for the <code><a
910     href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
911     worker was created. It represents the <span>absolute URL</span> of the
912     script that was used to initialize the worker.
913 wakaba 1.22
914     <p>The <dfn id=name
915     title=dom-WorkerGlobalScope-name><code>name</code></dfn> attribute must
916     return the value it was assigned when the <code><a
917     href="#workerglobalscope">WorkerGlobalScope</a></code> object was created
918     by the "<a href="#run-a">run a worker</a>" algorithm. If it has a value
919     that isn't the empty string, its value represents the name that can be
920     used to obtain a reference to the worker using the <code
921 wakaba 1.25 title=dom-WorkerFactory-createSharedWorker><a
922     href="#createsharedworker">createSharedWorker()</a></code> method.
923 wakaba 1.22
924     <p>The <dfn id=closing
925     title=dom-WorkerGlobalScope-closing><code>closing</code></dfn> attribute
926     must return false until it is set to true by one of the algorithms in the
927 wakaba 1.16 processing model section below.
928 wakaba 1.9
929 wakaba 1.22 <p>The following are the <span>event handler DOM attributes</span> that
930     must be supported by objects implementing the <code><a
931     href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
932    
933     <dl>
934 wakaba 1.16 <dt><dfn id=onconnect
935 wakaba 1.22 title=handler-WorkerGlobalScope-onconnect><code>onconnect</code></dfn>
936 wakaba 1.11
937     <dd>
938 wakaba 1.22 <p><span class=ja-translation lang=ja><code title=event-WorkerGlobalScope-connect>connect</code> 事象が当該 <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
939 wakaba 1.12 呼び出されなければなりません。</span><dt><dfn id=onunload
940 wakaba 1.22 title=handler-WorkerGlobalScope-onunload><code>onunload</code></dfn>
941 wakaba 1.11
942     <dd>
943 wakaba 1.22 <p><span class=ja-translation lang=ja><code title=event-unload>unload</code> 事象が当該 <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
944 wakaba 1.12 呼び出されなければなりません。</span></dl>
945 wakaba 1.11
946 wakaba 1.23 <p>In addition to the above members, the <a href="#success" title="worker
947     creation succeeded">worker creation success steps</a> add a <dfn id=port
948     title=dom-WorkerGlobalScope-port><code>port</code></dfn> property to the
949     object.
950    
951 wakaba 1.18 <h3 id=base-urls><span class=ja-translation lang=ja><span class=secno>2.2 </span>労働者の基底 URL と起源</span></h3>
952 wakaba 1.16
953 wakaba 1.22 <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
954 wakaba 1.23 worker is the <span>absolute URL</span> given that the worker's <code
955     title=dom-WorkerGlobalScope-location><a
956     href="#location">location</a></code> attribute represents.
957 wakaba 1.22
958     <p>Both the <span>origin</span> and <span>effective script origin</span> of
959     scripts running in workers are the <span>origin</span> of the
960 wakaba 1.23 <span>absolute URL</span> given that the worker's <code
961     title=dom-WorkerGlobalScope-location><a
962     href="#location">location</a></code> attribute represents.
963 wakaba 1.22
964     <h3 id=the-queue><span class=ja-translation lang=ja><span class=secno>2.3 </span>事象の待ち行列</span></h3>
965    
966     <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
967     object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
968     initially empty.
969 wakaba 1.9
970 wakaba 1.22 <p><span class=ja-translation lang=ja>待ち行列中の事象は、 DOM 事象か小休止呼び戻しのいずれかです。</span><p><span class=ja-translation lang=ja>労働者中で呼び出しまたは発送されるすべての非同期の呼び戻しと事象は、
971 wakaba 1.17 労働者の待ち行列に追加されなければなりません。
972 wakaba 1.12 ただし、実際の呼び戻しの呼び出しや事象の発送は、後述の「<a
973 wakaba 1.22 href="#run-a">労働者を走らせる</a>」処理モデルに従い処理されます。</span><p>Once the <code><a
974     href="#workerglobalscope">WorkerGlobalScope</a></code>'s <code
975     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
976     attribute is set to true, the queue must discard anything else that would
977     be added to it. Effectively, once the <code
978     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
979     attribute is true, timers stop firing, notifications for all pending
980     asynchronous operations are dropped, etc.
981    
982     <h3 id=the-workers><span class=ja-translation lang=ja><span class=secno>2.4 </span>労働者のポート</span></h3>
983 wakaba 1.11
984 wakaba 1.14 <p><span class=ja-translation lang=ja>労働者は、メッセージ通信路とその <code>MessagePort</code>
985 wakaba 1.22 オブジェクトを通じて、他の労働者や閲覧文脈と通信します。</span><p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
986     <var title="">worker</var> has a list of <dfn id=the-workers0>the worker's
987     ports</dfn>, which consists of all the <code>MessagePort</code> objects
988     that are entangled with another port and that have one (but only one) port
989     owned by <var title="">worker</var>. This list includes all the
990     <code>MessagePort</code> objects that are in events pending in the <a
991     href="#queue">queue of events</a>.
992 wakaba 1.16
993     <hr>
994    
995 wakaba 1.23 <p>A worker is said to be a <dfn id=permissible>permissible worker</dfn> if
996     either:
997    
998     <ul>
999     <li>at some point past or present a <code>MessagePort</code> owned by the
1000     worker was entangled with a <code>MessagePort</code> <var
1001     title="">p</var> whose owner is a <code>Window</code> object whose
1002     <span>active document</span> is the <code>Document</code> that was that
1003     <span>browsing context</span>'s <span>active document</span> when <var
1004     title="">p</var> was created, and that <code>Document</code> is
1005     <span>fully active</span>, or
1006    
1007     <li>at some point past or present a <code>MessagePort</code> owned by the
1008     worker was entangled with a <code>MessagePort</code> owned by another
1009     worker that is currently a <a href="#permissible">permissible worker</a>.
1010     </ul>
1011    
1012     <hr>
1013    
1014     <p>A worker is said to be a <dfn id=protected>protected worker</dfn> if
1015     either:
1016    
1017     <ul>
1018     <li>it has outstanding timers, database transactions, or network
1019     connections, and is a <a href="#permissible">permissible worker</a>, or:
1020    
1021     <li>there is a <a href="#protected">protected worker</a> that at some
1022     point past or present owned a <code>MessagePort</code> that was entangled
1023     with a <code>MessagePort</code> owned by this worker.
1024     </ul>
1025    
1026     <hr>
1027    
1028     <p>A worker is said to be an <dfn id=active>active needed worker</dfn> if
1029     either:
1030    
1031     <ul>
1032     <li>the worker is a <a href="#protected">protected worker</a>, or
1033    
1034     <li>at least one of the <a href="#the-workers0">the worker's ports</a> is
1035     entangled with a <code>MessagePort</code> <var title="">p</var> whose
1036     owner is a <code>Window</code> object whose <span>active document</span>
1037     is the <code>Document</code> that was that <span>browsing
1038     context</span>'s <span>active document</span> when that
1039     <code>MessagePort</code> <var title="">p</var> was created, and that
1040     <code>Document</code> is <span>fully active</span>, or
1041 wakaba 1.22
1042 wakaba 1.23 <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1043 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1044     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1045 wakaba 1.23 itself an <a href="#active">active needed worker</a>.
1046 wakaba 1.22 </ul>
1047    
1048     <hr>
1049    
1050 wakaba 1.23 <p>A worker is said to be a <dfn id=suspendable>suspendable worker</dfn> if
1051     it is not an <a href="#active">active needed worker</a> but either:
1052 wakaba 1.22
1053 wakaba 1.23 <ul>
1054     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1055     an entangled <code>MessagePort</code> owned by a <code>Window</code>
1056     object, or
1057    
1058     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1059 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1060     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1061 wakaba 1.23 itself a <span>needed worker</span>.
1062 wakaba 1.22 </ul>
1063 wakaba 1.16
1064     <h3 id=processing><span class=ja-translation lang=ja><span class=secno>2.5 </span>処理モデル</span></h3>
1065 wakaba 1.12
1066     <p><span class=ja-translation lang=ja>利用者エージェントが、<span>URL</span> <var
1067     title="">url</var>、閲覧文脈 <var title="">owner browsing
1068     context</var>、<code>Document</code> <var title="">owner
1069     document</var> のスクリプトに対する、 <var
1070 wakaba 1.14 title="">name</var> と名付けられた<dfn id=run-a>労働者を走らせる</dfn>、
1071 wakaba 1.12 という時には、利用者エージェントは、次の段階を、完全に分離された並列実行環境で実行しなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1072     <li>
1073     <p><span class=ja-translation lang=ja><var title="">url</var> で識別される資源を取って来ようと試みます。</span></p>
1074    
1075 wakaba 1.23 <p>If the attempt fails, then abort these steps and invoke the <a
1076 wakaba 1.25 href="#worker2" title="worker creation failed">error handling steps</a>
1077 wakaba 1.23 defined by the algorithm that called this one.</p>
1078 wakaba 1.12
1079     <p><span class=ja-translation lang=ja>この試みが成功した場合、 <var title="">script</var>
1080     を得られた資源とします。</span></p>
1081    
1082     <p class=note><span class=ja-translation lang=ja><code>script</code> 要素と同様に、スクリプトの MIME 型は無視します。
1083     <code>script</code> 要素とは異なり、
1084     型を上書きする方法はありません。スクリプトは常に JavaScript
1085     と仮定します。</span></p>
1086 wakaba 1.11 <!-- XXX people will complain about
1087     this. I guess we might want to examine the MIME type... -->
1088    
1089    
1090     <li>
1091 wakaba 1.22 <p>Create a new <code><a
1092     href="#workerglobalscope">WorkerGlobalScope</a></code> object, <var
1093     title="">worker</var>.</p>
1094    
1095     <li>
1096     <p>Set the <code title=dom-WorkerGlobalScope-name><a
1097     href="#name">name</a></code> attribute of <var title="">worker</var> to
1098     the value of <var title="">name</var>.</p>
1099 wakaba 1.13
1100     <li>
1101 wakaba 1.23 <p>Create a new <code><a href="#workerlocation">WorkerLocation</a></code>
1102     object for the <code title=dom-WorkerGlobalScope-location><a
1103     href="#location">location</a></code> attribute of <var
1104     title="">worker</var>, representing <var title="">url</var>.</p>
1105    
1106     <li>
1107 wakaba 1.18 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト実行文脈を (ひいては大域オブジェクトをも)
1108     <var
1109     title="">worker</var> とします。</span></p>
1110 wakaba 1.9
1111     <li>
1112 wakaba 1.12 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト閲覧文脈を <var title="">owner browsing context</var>
1113     とします。</span></p>
1114 wakaba 1.9
1115     <li>
1116 wakaba 1.12 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト文書文脈を <var title="">owner document</var>
1117     とします。</span></p>
1118 wakaba 1.9
1119     <li>
1120 wakaba 1.18 <p><span class=ja-translation lang=ja>この段階を呼び出す算法が定義する<a href="#success" title="worker creation
1121     succeeded">成功段階</a>を呼び出します。</span></p>
1122    
1123 wakaba 1.22 <p class=note>This will usually add an event to the <a
1124 wakaba 1.23 href="#queue">queue of events</a> and set the <code
1125     title=dom-WorkerGlobalScope-port><a href="#port">port</a></code>
1126     property on the <var title="">worker</var> object. If it does, that
1127     event will have a <code>MessagePort</code> and thus the list of <a
1128 wakaba 1.22 href="#the-workers0">the worker's ports</a> will not be empty. If it
1129     doesn't, then the next step will set the <var title="">worker</var>
1130     object's <code title=dom-WorkerGlobalScope-closing><a
1131     href="#closing">closing</a></code> attribute to true.</p>
1132    
1133     <li>
1134 wakaba 1.23 <p><strong>Closing orphan workers</strong>: Start monitoring <var
1135     title="">worker</var>, such that as soon as the worker stops being
1136     either an <a href="#active">active needed worker</a> or a <a
1137     href="#suspendable">suspendable worker</a>, the <var
1138 wakaba 1.22 title="">worker</var> object's <code
1139     title=dom-WorkerGlobalScope-closing><a
1140 wakaba 1.25 href="#closing">closing</a></code> attribute is set to true and an event
1141     named <code title=event-unload>unload</code>, which uses the
1142     <code>Event</code> object, which does not bubble, and which is not
1143     cancelable, is added it to the worker's <code><a
1144     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1145     href="#queue">queue of events</a>, targetted at the <code><a
1146     href="#workerglobalscope">WorkerGlobalScope</a></code> object.</p>
1147 wakaba 1.22
1148     <li>
1149 wakaba 1.23 <p><strong>Suspending workers</strong>: Start monitoring <var
1150     title="">worker</var>, such that whenever the <var title="">worker</var>
1151     object's <code title=dom-WorkerGlobalScope-closing><a
1152     href="#closing">closing</a></code> attribute is false and the worker is
1153     a <a href="#suspendable">suspendable worker</a>, the user agent suspends
1154     execution of script in that worker until such time as either the <code
1155 wakaba 1.22 title=dom-WorkerGlobalScope-closing><a
1156     href="#closing">closing</a></code> attribute switches to true or the
1157 wakaba 1.23 worker stops being a <a href="#suspendable">suspendable worker</a>.</p>
1158 wakaba 1.11
1159     <li>
1160 wakaba 1.14 <p><span class=ja-translation lang=ja><var title="">script</var> を、返るか、例外の捕獲に失敗するか、後述の「<a href="#kill-a">労働者を殺す</a>」算法により早々に停止させられるかのいずれかまで走らせます。</span></p>
1161 wakaba 1.11
1162 wakaba 1.23 <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
1163     worker</a>" algorithm, then that same algorithm will cause there to only
1164     be a single event in the <a href="#queue">queue of events</a> at the
1165     next step, namely the <code title=message-unload>unload</code> event.</p>
1166 wakaba 1.9
1167     <li>
1168 wakaba 1.22 <p><i>Event loop</i>: Wait until either there is an event in the <a
1169     href="#queue">queue of events</a> associated with <var
1170     title="">worker</var> or the <var title="">worker</var> object's <code
1171     title=dom-WorkerGlobalScope-closing><a
1172     href="#closing">closing</a></code> attribute is set to true.</p>
1173 wakaba 1.9
1174     <li>
1175 wakaba 1.14 <p><span class=ja-translation lang=ja><a href="#queue">事象の待ち行列</a>中で最古の事象か呼び戻しがあれば、
1176     これを発送します。この事象の取扱いや呼び戻しの実行は、後述の「<a href="#kill-a">労働者を殺す</a>」
1177     算法により、早々に停止させられるかもしれません。</span></p>
1178 wakaba 1.9
1179     <li>
1180 wakaba 1.22 <p>If there are any more events in the <a href="#queue">queue of
1181     events</a> or if the <var title="">worker</var> object's <code
1182     title=dom-WorkerGlobalScope-closing><a
1183     href="#closing">closing</a></code> attribute is set to false, then jump
1184     back to the step above labeled <i>event loop</i>.</p>
1185 wakaba 1.9
1186     <li>
1187 wakaba 1.23 <p class=big-issue>timers, intervals, XMLHttpRequests, database
1188     transactions, etc, must be killed; ports must be unentangled</p>
1189 wakaba 1.25
1190     <li>
1191     <p>At the next available opportunity, after any scripts have finished
1192     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1193     <code title=event-unload>unload</code> at any <code><a
1194     href="#worker1">Worker</a></code> objects associated with this worker.</p>
1195 wakaba 1.9 </ol>
1196    
1197     <hr>
1198    
1199 wakaba 1.14 <p><span class=ja-translation lang=ja>利用者エージェントが<dfn id=kill-a>労働者を殺す</dfn>という時には、
1200     利用者エージェントは、次の段階を労働者の主たるループ (先に定義した「<a
1201     href="#run-a">労働者を走らせる</a>」処理モデル) と並列に走らせなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1202     <li>
1203 wakaba 1.22 <p>Create an <code>Event</code> object with the event name <code
1204     title=event-unload>unload</code>, which does not bubble and is not
1205     cancelable, and add it to the worker's <code><a
1206     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1207     href="#queue">queue of events</a>, targetted at the <code><a
1208     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1209 wakaba 1.23 </li>
1210     <!-- XXX shouldn't add one if closing is
1211     already true, assuming unload has already been added to the queue
1212     (?) -->
1213 wakaba 1.22
1214     <li>
1215     <p>Set the worker's <code><a
1216     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
1217     title=dom-WorkerGlobalScope-closing><a
1218     href="#closing">closing</a></code> attribute to true.
1219    
1220     <li>
1221 wakaba 1.23 <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
1222     a worker</a>" processing model defined above immediately starts running
1223     event listeners registered for the <code
1224     title=event-unload>unload</code> event, this time should not be zero
1225     &mdash; the idea is that the <code title=event-unload>unload</code>
1226     event can be used to clean up when shutting down unexpectedly.
1227    
1228     <li>
1229 wakaba 1.14 <p><span class=ja-translation lang=ja><a href="#queue">事象の待ち行列</a>中にこの算法が追加した
1230     <code title=event-unload>unload</code>
1231     事象以外の事象が存在する場合には、それらを発送せずに捨てます。</span><li>
1232     <p><span class=ja-translation lang=ja>この算法が追加した <code title=event-unload>unload</code>
1233     事象がまだ発送されていなければ、現在労働者中で走っているスクリプトを停止します。</span><li>
1234     <p><span class=ja-translation lang=ja>利用者エージェント定義の時間、待ちます。</span><li>
1235     <p><span class=ja-translation lang=ja>現在労働者中で走っているスクリプト (スクリプトが走っているとすると、
1236     これは <code
1237     title=event-unload>unload</code>
1238     事象の取扱器です。) を停止します。</span></ol>
1239 wakaba 1.9
1240 wakaba 1.22 <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
1241     processing model on a worker at any time, e.g. in response to user
1242     requests, in response to CPU quota management, or when a worker stops
1243 wakaba 1.23 being an <a href="#active">active needed worker</a> if the worker
1244     continues executing even after its <code
1245     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1246     attribute was set to true.
1247 wakaba 1.16
1248 wakaba 1.11 <hr>
1249    
1250 wakaba 1.22 <p>When a script invokes the <dfn id=close
1251     title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
1252     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
1253     the user agent must run the following steps:
1254    
1255     <ol>
1256     <li>
1257     <p>Create an <code>Event</code> object with the event name <code
1258     title=event-unload>unload</code>, which does not bubble and is not
1259     cancelable, and add it to the <code><a
1260     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1261     href="#queue">queue of events</a>, targetted at the <code><a
1262     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1263    
1264     <li>
1265     <p>Set the worker's <code><a
1266     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
1267     title=dom-WorkerGlobalScope-closing><a
1268     href="#closing">closing</a></code> attribute to true.
1269    
1270     <li>
1271     <p>For each <code>MessagePort</code> object that is entangled with
1272     another port and that has one (but only one) port whose owner is the
1273     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1274     on which the method was invoked, run the following substeps:</p>
1275 wakaba 1.11
1276     <ol>
1277     <li>
1278 wakaba 1.15 <p><span class=ja-translation lang=ja>2つのポートをほどきます。</span><li>
1279 wakaba 1.22 <p>At the next available opportunity, after any scripts have finished
1280     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1281     <code title=event-unload>unload</code> at the other port (the one
1282     whose owner is not the <code><a
1283     href="#workerglobalscope">WorkerGlobalScope</a></code> object on which
1284     the <code title=dom-WorkerGlobalScope-close><a
1285     href="#close">close()</a></code> method was called).
1286     </ol>
1287 wakaba 1.11 </ol>
1288    
1289 wakaba 1.16 <h3 id=creating><span class=ja-translation lang=ja><span class=secno>2.6 </span>労働者の作成</span></h3>
1290 wakaba 1.9
1291     <pre
1292 wakaba 1.22 class=idl>[NoInterfaceObject] interface <dfn id=workerfactory>WorkerFactory</dfn> {
1293 wakaba 1.25 <a href="#worker1">Worker</a> <a href="#createworker" title=dom-WorkerFactory-createWorker>createWorker</a>(in DOMString scriptURL);
1294     <a href="#worker1">Worker</a> <a href="#createsharedworker" title=dom-WorkerFactory-createSharedWorker>createSharedWorker</a>(in DOMString name, in DOMString scriptURL);
1295     };
1296    
1297     interface <dfn id=worker1>Worker</dfn> {
1298     <span>MessagePort</span> <a href="#port0" title=dom-Worker-port>port</a>();
1299    
1300     // event handler attributes
1301     attribute <span>EventListener</span> <a href="#onload" title=handler-Worker-onload>onload</a>;
1302     attribute <span>EventListener</span> <a href="#onerror" title=handler-Worker-onerror>onerror</a>;
1303     attribute <span>EventListener</span> <a href="#onunload0" title=handler-Worker-onunload>onunload</a>;
1304 wakaba 1.9 };</pre>
1305    
1306 wakaba 1.22 <p>Objects that implement the <code>Window</code> and <code><a
1307 wakaba 1.25 href="#workerglobalscope">WorkerGlobalScope</a></code> interfaces must
1308     also implement the <code><a href="#workerfactory">WorkerFactory</a></code>
1309     interface.
1310 wakaba 1.22
1311     <hr>
1312 wakaba 1.13
1313 wakaba 1.22 <p><span class=ja-translation lang=ja>利用者エージェントは、 <dfn id=createworker title=dom-WorkerFactory-createWorker><code>createWorker(<var title="">scriptURL</var>)</code></dfn> メソッドが呼び出された時、
1314 wakaba 1.15 次の段階を走らせなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1315 wakaba 1.13 <li>
1316 wakaba 1.15 <p><span class=ja-translation lang=ja><var
1317     title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1318     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1319     例外を投げます。</span><li>
1320     <p><span class=ja-translation lang=ja>得られた絶対 URL の起源がメソッドを呼び出したスクリプトの起源と<span title="same origin">同じ</span>でない場合、
1321     保安性例外を投げます。</span><li>
1322     <p><span class=ja-translation lang=ja>得られた絶対 URL から、名前が空文字列である<a href="#create">労働者を作成</a>します。</span><li>
1323 wakaba 1.25 <p>Return the <code><a href="#worker1">Worker</a></code> object returned
1324     from the <a href="#create">create a worker</a> algorithm.
1325     </ol>
1326 wakaba 1.11
1327     <hr>
1328    
1329 wakaba 1.25 <p><span class=ja-translation lang=ja>利用者エージェントは、 <dfn id=createsharedworker title=dom-WorkerFactory-createSharedWorker><code>createSharedWorker(<var title="">name</var>, <var title="">scriptURL</var>)</code></dfn> メソッドが呼び出された時、
1330 wakaba 1.15 次の段階を走らせなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1331 wakaba 1.13 <li>
1332 wakaba 1.15 <p><span class=ja-translation lang=ja><var
1333     title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1334     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1335     例外を投げます。</span><li>
1336     <p><span class=ja-translation lang=ja>得られた絶対 URL の起源がメソッドを呼び出したスクリプトの起源と<span title="same origin">同じ</span>でない場合、
1337     保安性例外を投げます。</span><li>
1338 wakaba 1.25 <p>If the <var title="">name</var> argument is the empty string, <a
1339     href="#create">create a worker</a> from the resulting <span>absolute
1340     URL</span>, whose name is the empty string, and return the <code><a
1341     href="#worker1">Worker</a></code> object returned from the <a
1342     href="#create">create a worker</a> algorithm. Then, abort these steps.
1343    
1344     <li>
1345 wakaba 1.22 <p>If there exists a worker whose <code
1346     title=dom-WorkerGlobalScope-closing><a
1347     href="#closing">closing</a></code> attribute is false, whose <code
1348     title=dom-WorkerGlobalScope-name><a href="#name">name</a></code>
1349     attribute is exactly equal to the <var title="">name</var> argument, and
1350 wakaba 1.23 whose <code title=dom-WorkerGlobalScope-location><a
1351     href="#location">location</a></code> attribute represents an
1352     <span>absolute URL</span> that has the <span>same origin</span> as the
1353     resulting <span>absolute URL</span>, then run these substeps:</p>
1354 wakaba 1.13
1355     <ol>
1356     <li>
1357 wakaba 1.23 <p>If that worker's <code title=dom-WorkerGlobalScope-location><a
1358     href="#location">location</a></code> attribute represents an
1359     <span>absolute URL</span> that is not exactly equal to the resulting
1360     <span>absolute URL</span>, then throw a <code>URL_MISMATCH_ERR</code>
1361 wakaba 1.26 exception and abort these steps. <span class=big-issue>code 21</span>
1362 wakaba 1.22
1363     <li>
1364 wakaba 1.15 <p><span class=ja-translation lang=ja>メソッドを呼び出したスクリプトのスクリプト実行文脈が所有する新しい <code>MessagePort</code>
1365     オブジェクトを作成します。</span><li>
1366 wakaba 1.25 <p><span>Create a new <code><a href="#worker1">Worker</a></code>
1367     object</span> associated with that worker, and assign the newly
1368     created port to its <code title=dom-Worker-port><a
1369     href="#port0">port</a></code> attribute.
1370    
1371     <li>
1372     <p>Return the newly created <code><a href="#worker1">Worker</a></code>
1373     object.
1374    
1375     <li>
1376     <p>Asynchronously, <a href="#connect" title="connect to a
1377     worker">connect</a> to this preexisting worker, with the newly created
1378     <code>MessagePort</code> and <code><a
1379     href="#worker1">Worker</a></code> objects.
1380     </ol>
1381    
1382     <p>Otherwise, <a href="#create">create a worker</a> from the resulting
1383     <span>absolute URL</span>, whose name is the value of the <var
1384     title="">name</var> argument, and return the <code><a
1385     href="#worker1">Worker</a></code> object returned from the <a
1386     href="#create">create a worker</a> algorithm.</p>
1387 wakaba 1.11 </ol>
1388    
1389     <hr>
1390    
1391 wakaba 1.15 <p><span class=ja-translation lang=ja>メソッド呼び出しの文脈において、 URL <var title="">url</var> から名前 <var
1392     title="">name</var> の<dfn id=create>労働者を作成</dfn>する段階は、次の通りです。</span><ol>
1393     <li>
1394     <p><span class=ja-translation lang=ja>メソッドを呼び出したスクリプトのスクリプト実行文脈が所有する新しい <code>MessagePort</code>
1395     オブジェクトを作成します。</span><li>
1396 wakaba 1.25 <p><span>Create a new <code><a href="#worker1">Worker</a></code>
1397     object</span>, associated with the worker that will be created below,
1398     and assign the newly created port to its <code title=dom-Worker-port><a
1399     href="#port0">port</a></code> attribute.
1400    
1401     <li>
1402     <p>Return that <code><a href="#worker1">Worker</a></code> object.
1403    
1404     <li>
1405 wakaba 1.15 <p><span class=ja-translation lang=ja>並列実行文脈において (つまり、別のスレッドやプロセスや同様の構造において)、 <var title="">owner browsing
1406     context</var> をメソッドを呼び出したスクリプトのスクリプト閲覧文脈、 <var title="">owner
1407     document</var> をメソッドを呼び出したスクリプトのスクリプト文書文脈として、
1408     URL
1409     <var
1410     title="">url</var> のスクリプトのための <var
1411     title="">name</var> という名前の<a href="#run-a">労働者を走らせます</a>。</span></p>
1412    
1413 wakaba 1.23 <p>If that algorithm invokes the steps for <dfn id=success title="worker
1414     creation succeeded">success steps</dfn>, then <a href="#connect"
1415     title="connect to a worker">connect</a> to this new worker, with the
1416     newly created port, and if that algorithm returns a second new port,
1417     then add a new property <code title=dom-WorkerGlobalScope-port><a
1418     href="#port">port</a></code> on the new worker's <code><a
1419     href="#workerglobalscope">WorkerGlobalScope</a></code> object, whose
1420 wakaba 1.25 value is the <code>MessagePort</code> object returned by the <a
1421 wakaba 1.23 href="#connect">connect to a worker</a> algorithm.</p>
1422    
1423 wakaba 1.25 <p>Otherwise, if the <dfn id=worker2>worker creation failed</dfn>, then
1424 wakaba 1.23 at the next available opportunity, after any scripts have finished
1425     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1426 wakaba 1.25 <code title=event-error>error</code> at the newly created <code><a
1427     href="#worker1">Worker</a></code> object.</p>
1428 wakaba 1.11 </ol>
1429    
1430     <hr>
1431    
1432 wakaba 1.25 <p>The steps to <dfn id=connect>connect to a worker</dfn> given a
1433     <code>MessagePort</code> object <var title="">port</var> and a <code><a
1434     href="#worker1">Worker</a></code> object <var title="">worker</var> are as
1435     follows:
1436    
1437     <ol>
1438 wakaba 1.13 <li>
1439 wakaba 1.22 <p>If <var title="">port</var> would have been garbage collected, or if
1440     the <span>active document</span> of the owner of <var
1441     title="">port</var> is no longer the same <code>Document</code> object
1442     as when <var title="">port</var> was created, then do nothing. Abort
1443     these steps. If the worker was just created, it'll get killed
1444     immediately.</p>
1445    
1446     <li>
1447     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1448     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> of the
1449     worker.
1450 wakaba 1.13
1451     <li>
1452 wakaba 1.15 <p><span class=ja-translation lang=ja>この新しく作成されたポートとこれらの段階に渡されたポート <var
1453     title="">port</var> を絡めます。</span><li>
1454 wakaba 1.25 <p>At the next available opportunity, after any scripts have finished
1455     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1456     <code title=event-load>load</code> at <var title="">worker</var>.
1457    
1458     <li>
1459 wakaba 1.22 <p>Create an event that uses the <code>MessageEvent</code> interface,
1460     with the name <code title=event-connect>connect</code>, which does not
1461     bubble, is cancelable, has no default action, has a <code
1462     title=dom-MessageEvent-data>data</code> attribute whose value is the
1463     empty string and has a <code
1464     title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
1465     value is the newly created port, and add it to the worker's <code><a
1466     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1467     href="#queue">queue of events</a>, targetted at the <code><a
1468     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1469 wakaba 1.23
1470     <li>
1471     <p>Return the newly created port.
1472 wakaba 1.22 </ol>
1473 wakaba 1.15
1474 wakaba 1.25 <hr>
1475    
1476     <p><span class=ja-translation lang=ja><dfn id=port0 title=dom-Worker-port><code>port</code></dfn> attribute on a <code><a href="#worker1">Worker</a></code> object must return the value it was assigned when the <code><a href="#worker1">Worker</a></code> object was created by the methods on the <code><a href="#workerfactory">WorkerFactory</a></code> 界面。
1477     </span><p>The following are the <span>event handler DOM attributes</span> that
1478     must be supported by objects implementing the <code><a
1479     href="#worker1">Worker</a></code> interface:
1480    
1481     <dl>
1482     <dt><dfn id=onload title=handler-Worker-onload><code>onload</code></dfn>
1483    
1484     <dd>
1485     <p><span class=ja-translation lang=ja><code title=event-load>load</code> 事象が当該 <code><a href="#worker1">Worker</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1486     呼び出されなければなりません。</span><dt><dfn id=onerror
1487     title=handler-Worker-onerror><code>onerror</code></dfn>
1488    
1489     <dd>
1490     <p>Must be invoked whenever an <code title=event-error>error</code> event
1491     is targeted at or bubbles through the <code><a
1492     href="#worker1">Worker</a></code> object.
1493    
1494     <dt><dfn id=onunload0
1495     title=handler-Worker-onunload><code>onunload</code></dfn>
1496    
1497     <dd>
1498     <p>Must be invoked whenever an <code title=event-unload>unload</code>
1499     event is targeted at or bubbles through the <code><a
1500     href="#worker1">Worker</a></code> object.
1501     </dl>
1502    
1503 wakaba 1.15 <h2 id=apis-available><span class=ja-translation lang=ja><span class=secno>3. </span>労働者が利用可能な API</span></h2>
1504    
1505 wakaba 1.22 <pre
1506     class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1507 wakaba 1.25 void <a href="#importscripts" title=dom-WorkerGlobalScope-importScripts>importScripts</a>([Variadic] in DOMString urls);
1508 wakaba 1.22 readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
1509     <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
1510     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
1511     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
1512     };</pre>
1513 wakaba 1.16
1514 wakaba 1.25 <p><span class=ja-translation lang=ja><code><a href="#workerglobalscope">WorkerGlobalScope</a></code> 界面を実装するオブジェクトは、 <code><a href="#workerutils">WorkerUtils</a></code> 界面も実装しなければ<em class=rfc2119 title="MUST">なりません</em>。</span><p class=big-issue>Need to define a sync database API.
1515 wakaba 1.16
1516     <p class=big-issue>May need to define a browser sniffing API (like
1517     window.navigator).</p>
1518     <!-- XXX ApplicationCache -->
1519     <!-- XXX a way to set cookies on the URL for the script -->
1520     <!-- XXX debugging: void log(in DOMString s); // log to console -->
1521     <!-- XXX debugging: onerror -->
1522    
1523     <hr>
1524    
1525 wakaba 1.19 <p><span class=ja-translation lang=ja>DOM API (<code>Node</code> オブジェクト、 <code>Document</code>
1526 wakaba 1.22 オブジェクト、その他) は、この版のこの仕様書では労働者に利用可能ではありません。</span><h3 id=importing><span class=secno>3.1 </span>Importing scripts and
1527     libraries</h3>
1528    
1529 wakaba 1.25 <p>When a script invokes the <dfn id=importscripts
1530     title=dom-WorkerGlobalScope-importScripts><code>importScripts(<var
1531     title="">urls</var>)</code></dfn> method on a <code><a
1532 wakaba 1.22 href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
1533     agent must run the following steps:
1534    
1535     <ol>
1536     <li>
1537 wakaba 1.25 <p>If there are no arguments, return without doing anything. Abort these
1538     steps.
1539    
1540     <li>
1541     <p><span title="resolve a url">Resolve</span> each argument.
1542 wakaba 1.22
1543 wakaba 1.25 <li>
1544     <p>If any fail, throw a <code>SYNTAX_ERR</code> exception.
1545 wakaba 1.22
1546 wakaba 1.25 <li>
1547     <p>If any of the resulting <span title="absolute URL">absolute
1548     URLs</span> have an <span>origin</span> that is not the <span
1549     title="same origin">same</span> as the origin of the script that invoked
1550     the method, then throw a <span>security exception</span>.
1551 wakaba 1.22
1552 wakaba 1.25 <li>
1553     <p>Attempt to <span>fetch</span> each resource identified by the
1554     resulting <span title="absolute URLs">absolute URL</span>.</p>
1555 wakaba 1.22
1556     <li>
1557 wakaba 1.25 <p>For each argument in turn, in the order given, starting with the first
1558     one, run these substeps:</p>
1559    
1560     <ol>
1561     <li>
1562     <p>Wait for the fetching attempt for the corresponding resource to
1563     complete.</p>
1564    
1565     <p>If the fetching attempt failed, throw a <code>NETWORK_ERR</code>
1566 wakaba 1.26 exception and abort all these steps. <a
1567     href="#refsXHR">[XHR]</a><!-- XXX get DOM Core updated instead
1568     --></p>
1569 wakaba 1.25
1570     <p>If the fetching attempt succeeded, then let <var
1571     title="">script</var> be the resource that was obtained.</p>
1572    
1573     <p class=note>As with the worker's script, the script here is always
1574     assumed to be JavaScript, regardless of the MIME type.</p>
1575     <!-- XXX -->
1576    
1577     <li>
1578     <p><span class=ja-translation lang=ja><var title="">script</var> の<span>スクリプト実行文脈</span>、
1579 wakaba 1.22 <span>スクリプト閲覧文脈</span>、<span>スクリプト文書文脈</span>を、
1580     この労働者についての<a href="#run-a">労働者を走らせる</a>処理モデルで実行されたスクリプトについてのものと同じとします。</span></p>
1581    
1582 wakaba 1.25 <li>
1583     <p>Run <var title="">script</var> until it either returns, fails to
1584     catch an exception, or gets prematurely aborted by the "<a
1585     href="#kill-a">kill a worker</a>" algorithm above.</p>
1586    
1587     <p>If an exception was raised or if the script was prematurely aborted,
1588     then abort all these steps, letting the exception or aborting continue
1589     to be processed by the script that called the <code
1590     title=dom-WorkerGlobalScope-importScripts><a
1591     href="#importscripts">importScripts()</a></code> method.</p>
1592 wakaba 1.22
1593 wakaba 1.25 <p>If the "<a href="#kill-a">kill a worker</a>" algorithm aborts the
1594     script then abort all these steps.</p>
1595     </ol>
1596 wakaba 1.22 </ol>
1597    
1598     <h3 id=apis-defined><span class=secno>3.2 </span>APIs defined in other
1599     specifications</h3>
1600    
1601     <p>The <dfn id=localstorage
1602     title=dom-localStorage><code>localStorage</code></dfn>, <dfn
1603     id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
1604     must act as defined for the APIs with the same names on the
1605     <code>Window</code> object in the HTML5 specification, with the exception
1606     that where the API would use the <span>origin</span> of the <span>active
1607     document</span> of the <span>browsing context</span> of the
1608     <code>Window</code> object on which the method was supposedly invoked, it
1609     must instead use the <span>origin</span> of the script that invoked the
1610     method. <a href="#refsHTML5">[HTML5]</a>
1611    
1612     <p>The <dfn id=shownotification
1613     title=dom-showNotification><code>showNotification()</code></dfn> methods
1614     must act as defined for the APIs with the same names on the
1615     <code>Window</code> object in the HTML5 specification. <a
1616     href="#refsHTML5">[HTML5]</a>
1617    
1618     <h3 id=interface><span class=secno>3.3 </span>Interface objects and
1619     constructors</h3>
1620    
1621     <p>There must be no interface objects and constructors available in the
1622     global scope of scripts whose <span>script execution context</span> is a
1623     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1624     except for the following:
1625    
1626     <ul>
1627     <li>
1628     <p><span class=ja-translation lang=ja>XMLHttpRequest 仕様書で定義されている
1629     <code>XMLHttpRequest</code> とすべての界面オブジェクトと構築器、
1630     ただし<span>文書応答実体本体</span>は常に null でなければ<em class=rfc2119 title="MUST">なりません</em>。 <a
1631     href="#refsXHR">[XHR]</a></span><li>
1632     <p><span class=ja-translation lang=ja><code>WebSocket</code> 界面オブジェクトと構築器</span><li>
1633     <p><span class=ja-translation lang=ja><code>MessageChannel</code> 界面オブジェクトと構築器</span></ul>
1634 wakaba 1.23
1635     <h3 id=worker0><span class=secno>3.4 </span>Worker locations</h3>
1636    
1637     <pre
1638     class=idl>[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
1639     readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
1640     readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
1641     readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
1642     readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
1643 wakaba 1.25 readonly attribute DOMString <a href="#port1" title=dom-WorkerLocation-port>port</a>;
1644 wakaba 1.23 readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
1645     readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
1646     readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
1647     };</pre>
1648    
1649     <p>A <code><a href="#workerlocation">WorkerLocation</a></code> object
1650     represents an <span>absolute URL</span> set at its creation.
1651    
1652     <p>The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
1653     attribute must return the <span>absolute URL</span> that the object
1654     represents.
1655    
1656     <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface
1657     also has the complement of <span>URL decomposition attributes</span>, <dfn
1658     id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
1659     <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
1660 wakaba 1.25 id=port1 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
1661 wakaba 1.23 id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
1662     <dfn id=pathname
1663     title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
1664     id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
1665     <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
1666     must follow the rules given for URL decomposition attributes, with the
1667     <span title=concept-uda-input>input</span> being the <span>absolute
1668     URL</span> that the object represents (same as the <code
1669     title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
1670     and the <span title=concept-uda-setter>common setter action</span> being a
1671     no-op, since the attributes are defined to be readonly. <a
1672     href="#refsHTML5">[HTML5]</a>
1673 wakaba 1.22
1674     <h2 class=no-num id=references><span class=ja-translation lang=ja>参考文献</span></h2>
1675 wakaba 1.1
1676     <p class=big-issue><span class=ja-translation lang=ja>この章は将来の仕様案で執筆します。</span><h2 class=no-num id=acknowledgements><span class=ja-translation lang=ja>謝辞</span></h2>
1677     <!-- ACKS -->
1678    
1679 wakaba 1.22 <p><span class=ja-translation lang=ja>Aaron Boodman, Jonas Sicking, Maciej Stachowiak, Mike Smith, Philip Taylor の有用かつ重要なコメントに感謝します。</span><p><span class=ja-translation lang=ja>Gears チームの方々にも非常に感謝します。
1680 wakaba 1.14 Gears チームの方々はこの技術を開拓し、その経験はこの仕様書にも多大な影響を与えています。
1681     </span>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24