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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24