/[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.23 - (hide annotations) (download)
Thu Aug 7 06:14:04 2008 UTC (17 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.22: +809 -92 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     <p><span class=ja-translation lang=ja>この仕様書のすべての図、例、注意書きは、
808     規定の一部ではないと明記された節すべてと同様に、
809     規定の一部ではありません。
810     この仕様書のそれ以外のすべては規定の一部です。</span><p><span class=ja-translation lang=ja>この文書の規定の部分にあるキーワード「<em class=rfc2119 title="MUST">しなければなりません</em>」、
811     「<em class=rfc2119 title="MUST NOT">してはなりません</em>」、
812     「<em class=rfc2119 title="REQUIRED">必須です</em>」、「<em class=rfc2119 title="SHOULD">するべきです</em>」、
813     「<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     可読性の観点から、この仕様書<ins class=ja-note>の原文</ins>では、
817     これらの語<ins class=ja-note>の原語</ins>はすべて大文字では表記しません。
818     <a href="#refsRFC2119">[RFC2119]</a></span></p>
819     <!-- XXX but they should be
820     marked up -->
821    
822     <p><span class=ja-translation lang=ja>算法の一部として<ins class=ja-note>動作の指示の形で (原文では</ins>命令形で<ins class=ja-note>) </ins>記述された要件
823     (例えば「先頭の間隔文字をすべて除去します」や「偽を返し、これらの段階を停止します」)
824     は、算法の紹介で使われたキーワード (<em class=rfc2119 title="MUST">しなければなりません</em>、
825     <em class=rfc2119 title="SHOULD">するべきです</em>、<em class=rfc2119 title="MAY">して構いません</em>など) の意味により解釈します。</span><p><span class=ja-translation lang=ja>いくつかの適合性要件は属性やメソッドやオブジェクトの要件として説明されています。
826     そのような要件は、利用者エージェントに関する要件として解釈します。</span><p><span class=ja-translation lang=ja>算法や特定の段階として説明されている適合性要件は、
827     最終的な結果が等価である限り、任意の方法で実装して<em class=rfc2119 title="MAY">構いません</em>。
828     (特に、この仕様書で定義されている算法は追うのを簡単にしており、
829     高性能であることを目指してはいません。)</span><p><span class=ja-translation lang=ja>この仕様書で定義される適合性の級は、利用者エージェントのみです。</span><p><span class=ja-translation lang=ja>利用者エージェントは、サービス拒否攻撃を防ぐため、
830     記憶容量を使い尽くすことを防ぐため、
831     あるいは環境特有の制限に対応するためなどの目的で、
832 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>
833 wakaba 1.1
834     <p><span class=ja-translation lang=ja>この仕様書は他のいくつかの下位仕様書に依存しています。</span><dl>
835     <dt>HTML5
836    
837     <dd>
838     <p><span class=ja-translation lang=ja>HTML5 の多くの基礎的な概念をこの仕様書で使用します。
839     <a href="#refsHTML5">[HTML5]</a></span></p>
840    
841     <dt>ECMAScript
842    
843     <dd>
844     <p><span class=ja-translation lang=ja>この仕様書はスクリプト言語として JavaScript
845     を使うことを想定しています。 <a href="#refsJS">[JS]</a></span></p>
846    
847     <dt>WebIDL
848    
849     <dd>
850 wakaba 1.21 <p><span class=ja-translation lang=ja>この仕様書の IDL ブロックは WebIDL 仕様書における意味を用います。 <a href="#refsWebIDL">[WebIDL]</a></span></p>
851 wakaba 1.1 </dl>
852    
853 wakaba 1.16 <h3 id=terminology><span class=ja-translation lang=ja><span class=secno>1.3 </span>用語</span></h3>
854 wakaba 1.1
855     <p><span class=ja-translation lang=ja>簡単のため、<em>示す</em>、<em>表示する</em>、
856     <em>可視</em>などの用語を文書が利用者に対してレンダリングされる方法に言及する時に使うことがあります。
857     これらの語は視覚媒体を暗示することを意図していません。
858 wakaba 1.10 これらの語は他の媒体にも等価な方法で適用されると考えなければ<em class=rfc2119 title="MUST">なりません</em>。</span><p><span class=ja-translation lang=ja>「<code title="">Foo</code> オブジェクト」という構文 (<code title="">Foo</code>
859     は実際には界面) を時々使いますが、より正確な「界面 <code title="">Foo</code>
860     を実装しているオブジェクト」の代わりに用いています。</span><p><span class=ja-translation lang=ja>DOM という語は、 Web アプリケーションでスクリプトに利用可能とされている API
861 wakaba 1.1 の集合を指して用い、必ずしも DOM 中核仕様書で定義されている実際の
862     <code>Document</code> オブジェクトその他の <code>Node</code>
863     オブジェクトの存在を暗示してはいません。<a
864     href="#refsDOM3CORE">[DOM3CORE]</a></span><p><span class=ja-translation lang=ja>DOM 属性は、その値が (例えば著者のスクリプトにより) 取り出される時<em>取得される</em>といい、
865     新しい値がその属性に割り当てられる時<em>設定される</em>といいます。</span><p><span class=ja-translation lang=ja>DOM オブジェクトが<dfn id=live>生きている</dfn>という場合、
866     そのオブジェクトを返す属性は常に同じオブジェクトを返さなければ<em class=rfc2119 title="MUST">ならない</em>
867     (毎回新しいオブジェクトを返すのではなく) 上に、
868     そのオブジェクトの属性やメソッドがそのオブジェクトの裏にある実際のデータのスナップ写真ではなく、
869 wakaba 1.21 データそのものに対して操作されなければ<em class=rfc2119 title="MUST">ならない</em>ことを意味します。
870    
871    
872    
873    
874    
875    
876     </span><h2 id=infrastructure><span class=ja-translation lang=ja><span class=secno>2. </span>基盤</span></h2>
877 wakaba 1.9
878 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>
879 wakaba 1.9
880     <pre
881 wakaba 1.22 class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
882     // core worker features
883     readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
884 wakaba 1.23 readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
885 wakaba 1.22 readonly attribute DOMString <a href="#name" title=dom-WorkerGlobalScope-name>name</a>;
886     readonly attribute boolean <a href="#closing" title=dom-WorkerGlobalScope-closing>closing</a>;
887     void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
888    
889     // other APIs
890     readonly attribute WorkerUtils <a href="#utils" title=dom-WorkerGlobalScope-utils>utils</a>;
891 wakaba 1.11
892     // 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     title=dom-WorkerFactory-createNamedWorker><a
922     href="#createnamedworker">createNamedWorker()</a></code> method.
923    
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.22 <p>The <dfn id=utils
947     title=dom-WorkerGlobalScope-utils><code>utils</code></dfn> attribute must
948     return the <code><a href="#workerutils">WorkerUtils</a></code> object
949     created for the <code><a
950     href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
951     worker was created.
952    
953 wakaba 1.23 <p>In addition to the above members, the <a href="#success" title="worker
954     creation succeeded">worker creation success steps</a> add a <dfn id=port
955     title=dom-WorkerGlobalScope-port><code>port</code></dfn> property to the
956     object.
957    
958 wakaba 1.18 <h3 id=base-urls><span class=ja-translation lang=ja><span class=secno>2.2 </span>労働者の基底 URL と起源</span></h3>
959 wakaba 1.16
960 wakaba 1.22 <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
961 wakaba 1.23 worker is the <span>absolute URL</span> given that the worker's <code
962     title=dom-WorkerGlobalScope-location><a
963     href="#location">location</a></code> attribute represents.
964 wakaba 1.22
965     <p>Both the <span>origin</span> and <span>effective script origin</span> of
966     scripts running in workers are the <span>origin</span> of the
967 wakaba 1.23 <span>absolute URL</span> given that the worker's <code
968     title=dom-WorkerGlobalScope-location><a
969     href="#location">location</a></code> attribute represents.
970 wakaba 1.22
971     <h3 id=the-queue><span class=ja-translation lang=ja><span class=secno>2.3 </span>事象の待ち行列</span></h3>
972    
973     <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
974     object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
975     initially empty.
976 wakaba 1.9
977 wakaba 1.22 <p><span class=ja-translation lang=ja>待ち行列中の事象は、 DOM 事象か小休止呼び戻しのいずれかです。</span><p><span class=ja-translation lang=ja>労働者中で呼び出しまたは発送されるすべての非同期の呼び戻しと事象は、
978 wakaba 1.17 労働者の待ち行列に追加されなければなりません。
979 wakaba 1.12 ただし、実際の呼び戻しの呼び出しや事象の発送は、後述の「<a
980 wakaba 1.22 href="#run-a">労働者を走らせる</a>」処理モデルに従い処理されます。</span><p>Once the <code><a
981     href="#workerglobalscope">WorkerGlobalScope</a></code>'s <code
982     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
983     attribute is set to true, the queue must discard anything else that would
984     be added to it. Effectively, once the <code
985     title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
986     attribute is true, timers stop firing, notifications for all pending
987     asynchronous operations are dropped, etc.
988    
989     <h3 id=the-workers><span class=ja-translation lang=ja><span class=secno>2.4 </span>労働者のポート</span></h3>
990 wakaba 1.11
991 wakaba 1.14 <p><span class=ja-translation lang=ja>労働者は、メッセージ通信路とその <code>MessagePort</code>
992 wakaba 1.22 オブジェクトを通じて、他の労働者や閲覧文脈と通信します。</span><p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
993     <var title="">worker</var> has a list of <dfn id=the-workers0>the worker's
994     ports</dfn>, which consists of all the <code>MessagePort</code> objects
995     that are entangled with another port and that have one (but only one) port
996     owned by <var title="">worker</var>. This list includes all the
997     <code>MessagePort</code> objects that are in events pending in the <a
998     href="#queue">queue of events</a>.
999 wakaba 1.16
1000     <hr>
1001    
1002 wakaba 1.23 <p>A worker is said to be a <dfn id=permissible>permissible worker</dfn> if
1003     either:
1004    
1005     <ul>
1006     <li>at some point past or present a <code>MessagePort</code> owned by the
1007     worker was entangled with a <code>MessagePort</code> <var
1008     title="">p</var> whose owner is a <code>Window</code> object whose
1009     <span>active document</span> is the <code>Document</code> that was that
1010     <span>browsing context</span>'s <span>active document</span> when <var
1011     title="">p</var> was created, and that <code>Document</code> is
1012     <span>fully active</span>, or
1013    
1014     <li>at some point past or present a <code>MessagePort</code> owned by the
1015     worker was entangled with a <code>MessagePort</code> owned by another
1016     worker that is currently a <a href="#permissible">permissible worker</a>.
1017     </ul>
1018    
1019     <hr>
1020    
1021     <p>A worker is said to be a <dfn id=protected>protected worker</dfn> if
1022     either:
1023    
1024     <ul>
1025     <li>it has outstanding timers, database transactions, or network
1026     connections, and is a <a href="#permissible">permissible worker</a>, or:
1027    
1028     <li>there is a <a href="#protected">protected worker</a> that at some
1029     point past or present owned a <code>MessagePort</code> that was entangled
1030     with a <code>MessagePort</code> owned by this worker.
1031     </ul>
1032    
1033     <hr>
1034    
1035     <p>A worker is said to be an <dfn id=active>active needed worker</dfn> if
1036     either:
1037    
1038     <ul>
1039     <li>the worker is a <a href="#protected">protected worker</a>, or
1040    
1041     <li>at least one of the <a href="#the-workers0">the worker's ports</a> is
1042     entangled with a <code>MessagePort</code> <var title="">p</var> whose
1043     owner is a <code>Window</code> object whose <span>active document</span>
1044     is the <code>Document</code> that was that <span>browsing
1045     context</span>'s <span>active document</span> when that
1046     <code>MessagePort</code> <var title="">p</var> was created, and that
1047     <code>Document</code> is <span>fully active</span>, or
1048 wakaba 1.22
1049 wakaba 1.23 <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1050 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1051     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1052 wakaba 1.23 itself an <a href="#active">active needed worker</a>.
1053 wakaba 1.22 </ul>
1054    
1055     <hr>
1056    
1057 wakaba 1.23 <p>A worker is said to be a <dfn id=suspendable>suspendable worker</dfn> if
1058     it is not an <a href="#active">active needed worker</a> but either:
1059 wakaba 1.22
1060 wakaba 1.23 <ul>
1061     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1062     an entangled <code>MessagePort</code> owned by a <code>Window</code>
1063     object, or
1064    
1065     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1066 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1067     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1068 wakaba 1.23 itself a <span>needed worker</span>.
1069 wakaba 1.22 </ul>
1070 wakaba 1.16
1071     <h3 id=processing><span class=ja-translation lang=ja><span class=secno>2.5 </span>処理モデル</span></h3>
1072 wakaba 1.12
1073     <p><span class=ja-translation lang=ja>利用者エージェントが、<span>URL</span> <var
1074     title="">url</var>、閲覧文脈 <var title="">owner browsing
1075     context</var>、<code>Document</code> <var title="">owner
1076     document</var> のスクリプトに対する、 <var
1077 wakaba 1.14 title="">name</var> と名付けられた<dfn id=run-a>労働者を走らせる</dfn>、
1078 wakaba 1.12 という時には、利用者エージェントは、次の段階を、完全に分離された並列実行環境で実行しなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1079     <li>
1080     <p><span class=ja-translation lang=ja><var title="">url</var> で識別される資源を取って来ようと試みます。</span></p>
1081    
1082 wakaba 1.23 <p>If the attempt fails, then abort these steps and invoke the <a
1083     href="#worker1" title="worker creation failed">error handling steps</a>
1084     defined by the algorithm that called this one.</p>
1085 wakaba 1.12
1086     <p><span class=ja-translation lang=ja>この試みが成功した場合、 <var title="">script</var>
1087     を得られた資源とします。</span></p>
1088    
1089     <p class=note><span class=ja-translation lang=ja><code>script</code> 要素と同様に、スクリプトの MIME 型は無視します。
1090     <code>script</code> 要素とは異なり、
1091     型を上書きする方法はありません。スクリプトは常に JavaScript
1092     と仮定します。</span></p>
1093 wakaba 1.11 <!-- XXX people will complain about
1094     this. I guess we might want to examine the MIME type... -->
1095    
1096    
1097     <li>
1098 wakaba 1.22 <p>Create a new <code><a
1099     href="#workerglobalscope">WorkerGlobalScope</a></code> object, <var
1100     title="">worker</var>.</p>
1101    
1102     <li>
1103     <p>Set the <code title=dom-WorkerGlobalScope-name><a
1104     href="#name">name</a></code> attribute of <var title="">worker</var> to
1105     the value of <var title="">name</var>.</p>
1106 wakaba 1.13
1107     <li>
1108 wakaba 1.23 <p>Create a new <code><a href="#workerlocation">WorkerLocation</a></code>
1109     object for the <code title=dom-WorkerGlobalScope-location><a
1110     href="#location">location</a></code> attribute of <var
1111     title="">worker</var>, representing <var title="">url</var>.</p>
1112    
1113     <li>
1114 wakaba 1.22 <p>Create a new <code><a href="#workerutils">WorkerUtils</a></code>
1115     object for the <code title=dom-WorkerGlobalScope-utils><a
1116 wakaba 1.23 href="#utils">utils</a></code> attribute of <var title="">worker</var>.</p>
1117 wakaba 1.9
1118     <li>
1119 wakaba 1.18 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト実行文脈を (ひいては大域オブジェクトをも)
1120     <var
1121     title="">worker</var> とします。</span></p>
1122 wakaba 1.9
1123     <li>
1124 wakaba 1.12 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト閲覧文脈を <var title="">owner browsing context</var>
1125     とします。</span></p>
1126 wakaba 1.9
1127     <li>
1128 wakaba 1.12 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト文書文脈を <var title="">owner document</var>
1129     とします。</span></p>
1130 wakaba 1.9
1131     <li>
1132 wakaba 1.18 <p><span class=ja-translation lang=ja>この段階を呼び出す算法が定義する<a href="#success" title="worker creation
1133     succeeded">成功段階</a>を呼び出します。</span></p>
1134    
1135 wakaba 1.22 <p class=note>This will usually add an event to the <a
1136 wakaba 1.23 href="#queue">queue of events</a> and set the <code
1137     title=dom-WorkerGlobalScope-port><a href="#port">port</a></code>
1138     property on the <var title="">worker</var> object. If it does, that
1139     event will have a <code>MessagePort</code> and thus the list of <a
1140 wakaba 1.22 href="#the-workers0">the worker's ports</a> will not be empty. If it
1141     doesn't, then the next step will set the <var title="">worker</var>
1142     object's <code title=dom-WorkerGlobalScope-closing><a
1143     href="#closing">closing</a></code> attribute to true.</p>
1144    
1145     <li>
1146 wakaba 1.23 <p><strong>Closing orphan workers</strong>: Start monitoring <var
1147     title="">worker</var>, such that as soon as the worker stops being
1148     either an <a href="#active">active needed worker</a> or a <a
1149     href="#suspendable">suspendable worker</a>, the <var
1150 wakaba 1.22 title="">worker</var> object's <code
1151     title=dom-WorkerGlobalScope-closing><a
1152     href="#closing">closing</a></code> attribute is set to true.</p>
1153    
1154     <li>
1155 wakaba 1.23 <p><strong>Suspending workers</strong>: Start monitoring <var
1156     title="">worker</var>, such that whenever the <var title="">worker</var>
1157     object's <code title=dom-WorkerGlobalScope-closing><a
1158     href="#closing">closing</a></code> attribute is false and the worker is
1159     a <a href="#suspendable">suspendable worker</a>, the user agent suspends
1160     execution of script in that worker until such time as either the <code
1161 wakaba 1.22 title=dom-WorkerGlobalScope-closing><a
1162     href="#closing">closing</a></code> attribute switches to true or the
1163 wakaba 1.23 worker stops being a <a href="#suspendable">suspendable worker</a>.</p>
1164 wakaba 1.11
1165     <li>
1166 wakaba 1.14 <p><span class=ja-translation lang=ja><var title="">script</var> を、返るか、例外の捕獲に失敗するか、後述の「<a href="#kill-a">労働者を殺す</a>」算法により早々に停止させられるかのいずれかまで走らせます。</span></p>
1167 wakaba 1.11
1168 wakaba 1.23 <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
1169     worker</a>" algorithm, then that same algorithm will cause there to only
1170     be a single event in the <a href="#queue">queue of events</a> at the
1171     next step, namely the <code title=message-unload>unload</code> event.</p>
1172 wakaba 1.9
1173     <li>
1174 wakaba 1.22 <p><i>Event loop</i>: Wait until either there is an event in the <a
1175     href="#queue">queue of events</a> associated with <var
1176     title="">worker</var> or the <var title="">worker</var> object's <code
1177     title=dom-WorkerGlobalScope-closing><a
1178     href="#closing">closing</a></code> attribute is set to true.</p>
1179 wakaba 1.9
1180     <li>
1181 wakaba 1.14 <p><span class=ja-translation lang=ja><a href="#queue">事象の待ち行列</a>中で最古の事象か呼び戻しがあれば、
1182     これを発送します。この事象の取扱いや呼び戻しの実行は、後述の「<a href="#kill-a">労働者を殺す</a>」
1183     算法により、早々に停止させられるかもしれません。</span></p>
1184 wakaba 1.9
1185     <li>
1186 wakaba 1.22 <p>If there are any more events in the <a href="#queue">queue of
1187     events</a> or if the <var title="">worker</var> object's <code
1188     title=dom-WorkerGlobalScope-closing><a
1189     href="#closing">closing</a></code> attribute is set to false, then jump
1190     back to the step above labeled <i>event loop</i>.</p>
1191 wakaba 1.9
1192     <li>
1193 wakaba 1.23 <p class=big-issue>timers, intervals, XMLHttpRequests, database
1194     transactions, etc, must be killed; ports must be unentangled</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     <span>MessagePort</span> <a href="#createworker" title=dom-WorkerFactory-createWorker>createWorker</a>(in DOMString scriptURL);
1294     <span>MessagePort</span> <a href="#createnamedworker" title=dom-WorkerFactory-createNamedWorker>createNamedWorker</a>(in DOMString name, in DOMString scriptURL);
1295 wakaba 1.9 };</pre>
1296    
1297 wakaba 1.22 <p>Objects that implement the <code>Window</code> and <code><a
1298     href="#workerutils">WorkerUtils</a></code> interfaces must also implement
1299     the <code><a href="#workerfactory">WorkerFactory</a></code> interface.
1300    
1301     <hr>
1302 wakaba 1.13
1303 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> メソッドが呼び出された時、
1304 wakaba 1.15 次の段階を走らせなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1305 wakaba 1.13 <li>
1306 wakaba 1.15 <p><span class=ja-translation lang=ja><var
1307     title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1308     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1309     例外を投げます。</span><li>
1310     <p><span class=ja-translation lang=ja>得られた絶対 URL の起源がメソッドを呼び出したスクリプトの起源と<span title="same origin">同じ</span>でない場合、
1311     保安性例外を投げます。</span><li>
1312     <p><span class=ja-translation lang=ja>得られた絶対 URL から、名前が空文字列である<a href="#create">労働者を作成</a>します。</span><li>
1313     <p><span class=ja-translation lang=ja><a
1314     href="#create">労働者を作成</a>する算法が返した
1315     <code>MessagePort</code> オブジェクトを返します。</span></ol>
1316 wakaba 1.11
1317     <hr>
1318    
1319 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> メソッドが呼び出された時、
1320 wakaba 1.15 次の段階を走らせなければ<em class=rfc2119 title="MUST">なりません</em>。</span><ol>
1321 wakaba 1.13 <li>
1322 wakaba 1.15 <p><span class=ja-translation lang=ja><var
1323     title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1324     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1325     例外を投げます。</span><li>
1326     <p><span class=ja-translation lang=ja>得られた絶対 URL の起源がメソッドを呼び出したスクリプトの起源と<span title="same origin">同じ</span>でない場合、
1327     保安性例外を投げます。</span><li>
1328     <p><span class=ja-translation lang=ja><var title="">name</var> 引数が空文字列の場合、得られた絶対 URL から、
1329     名前が空文字列である<a
1330     href="#create">労働者を作成</a>し、<a
1331     href="#create">労働者を作成</a>する算法が返した <code>MessagePort</code>
1332     オブジェクトを返します。そして、これらの段階を停止します。</span><li>
1333 wakaba 1.22 <p>If there exists a worker whose <code
1334     title=dom-WorkerGlobalScope-closing><a
1335     href="#closing">closing</a></code> attribute is false, whose <code
1336     title=dom-WorkerGlobalScope-name><a href="#name">name</a></code>
1337     attribute is exactly equal to the <var title="">name</var> argument, and
1338 wakaba 1.23 whose <code title=dom-WorkerGlobalScope-location><a
1339     href="#location">location</a></code> attribute represents an
1340     <span>absolute URL</span> that has the <span>same origin</span> as the
1341     resulting <span>absolute URL</span>, then run these substeps:</p>
1342 wakaba 1.13
1343     <ol>
1344     <li>
1345 wakaba 1.23 <p>If that worker's <code title=dom-WorkerGlobalScope-location><a
1346     href="#location">location</a></code> attribute represents an
1347     <span>absolute URL</span> that is not exactly equal to the resulting
1348     <span>absolute URL</span>, then throw a <code>URL_MISMATCH_ERR</code>
1349     exception and abort these steps. <span class=big-issue>code 19</span>
1350 wakaba 1.22
1351     <li>
1352 wakaba 1.15 <p><span class=ja-translation lang=ja>メソッドを呼び出したスクリプトのスクリプト実行文脈が所有する新しい <code>MessagePort</code>
1353     オブジェクトを作成します。</span><li>
1354     <p><span class=ja-translation lang=ja>そのポートを返します。</span><li>
1355 wakaba 1.19 <p><span class=ja-translation lang=ja>非同期に、この既存の労働者を新しく作成されたポートに<a href="#connect" title="connect to a
1356     worker">接続</a>します。</span></ol>
1357 wakaba 1.15
1358     <p><span class=ja-translation lang=ja>そうでない場合、得られた絶対 URL から、名前が <var
1359     title="">name</var> 引数の値である<a href="#create">労働者を作成</a>し、
1360     <a href="#create">労働者を作成</a>する算法が返した <code>MessagePort</code>
1361     オブジェクトを返します。</span></p>
1362 wakaba 1.11 </ol>
1363    
1364     <hr>
1365    
1366 wakaba 1.15 <p><span class=ja-translation lang=ja>メソッド呼び出しの文脈において、 URL <var title="">url</var> から名前 <var
1367     title="">name</var> の<dfn id=create>労働者を作成</dfn>する段階は、次の通りです。</span><ol>
1368     <li>
1369     <p><span class=ja-translation lang=ja>メソッドを呼び出したスクリプトのスクリプト実行文脈が所有する新しい <code>MessagePort</code>
1370     オブジェクトを作成します。</span><li>
1371     <p><span class=ja-translation lang=ja>そのポートを返します。</span><li>
1372     <p><span class=ja-translation lang=ja>並列実行文脈において (つまり、別のスレッドやプロセスや同様の構造において)、 <var title="">owner browsing
1373     context</var> をメソッドを呼び出したスクリプトのスクリプト閲覧文脈、 <var title="">owner
1374     document</var> をメソッドを呼び出したスクリプトのスクリプト文書文脈として、
1375     URL
1376     <var
1377     title="">url</var> のスクリプトのための <var
1378     title="">name</var> という名前の<a href="#run-a">労働者を走らせます</a>。</span></p>
1379    
1380 wakaba 1.23 <p>If that algorithm invokes the steps for <dfn id=success title="worker
1381     creation succeeded">success steps</dfn>, then <a href="#connect"
1382     title="connect to a worker">connect</a> to this new worker, with the
1383     newly created port, and if that algorithm returns a second new port,
1384     then add a new property <code title=dom-WorkerGlobalScope-port><a
1385     href="#port">port</a></code> on the new worker's <code><a
1386     href="#workerglobalscope">WorkerGlobalScope</a></code> object, whose
1387     value is the <code>MessagePort</code> object return by the <a
1388     href="#connect">connect to a worker</a> algorithm.</p>
1389    
1390     <p>Otherwise, if the <dfn id=worker1>worker creation failed</dfn>, then
1391     at the next available opportunity, after any scripts have finished
1392     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1393     <code title=event-error>error</code> at the newly created port.</p>
1394 wakaba 1.11 </ol>
1395    
1396     <hr>
1397    
1398 wakaba 1.19 <p><span class=ja-translation lang=ja>与えられた <code>MessagePort</code> オブジェクト <var title="">port</var>
1399     により<dfn id=connect>労働者に接続</dfn>する段階は、次の通りです。</span><ol>
1400 wakaba 1.13 <li>
1401 wakaba 1.22 <p>If <var title="">port</var> would have been garbage collected, or if
1402     the <span>active document</span> of the owner of <var
1403     title="">port</var> is no longer the same <code>Document</code> object
1404     as when <var title="">port</var> was created, then do nothing. Abort
1405     these steps. If the worker was just created, it'll get killed
1406     immediately.</p>
1407    
1408     <li>
1409     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1410     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> of the
1411     worker.
1412 wakaba 1.13
1413     <li>
1414 wakaba 1.15 <p><span class=ja-translation lang=ja>この新しく作成されたポートとこれらの段階に渡されたポート <var
1415     title="">port</var> を絡めます。</span><li>
1416     <p><span class=ja-translation lang=ja>次の利用可能な機会、何らかのスクリプトが実行し終えた後に、
1417     <var title="">port</var> において <code title=event-load>load</code>
1418     と呼ばれる単純事象を発火します。</span><li>
1419 wakaba 1.22 <p>Create an event that uses the <code>MessageEvent</code> interface,
1420     with the name <code title=event-connect>connect</code>, which does not
1421     bubble, is cancelable, has no default action, has a <code
1422     title=dom-MessageEvent-data>data</code> attribute whose value is the
1423     empty string and has a <code
1424     title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
1425     value is the newly created port, and add it to the worker's <code><a
1426     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1427     href="#queue">queue of events</a>, targetted at the <code><a
1428     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1429 wakaba 1.23
1430     <li>
1431     <p>Return the newly created port.
1432 wakaba 1.22 </ol>
1433 wakaba 1.15
1434     <h2 id=apis-available><span class=ja-translation lang=ja><span class=secno>3. </span>労働者が利用可能な API</span></h2>
1435    
1436 wakaba 1.22 <pre
1437     class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1438     boolean <a href="#importscript" title=dom-WorkerGlobalScope-importScript>importScript</a>(in DOMString url);
1439     readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
1440     <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
1441     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
1442     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
1443     };</pre>
1444 wakaba 1.16
1445 wakaba 1.22 <p>Objects that implement the <code><a
1446     href="#workerutils">WorkerUtils</a></code> interface must also implement
1447 wakaba 1.23 the <code>WindowTimers</code> interface (and <code><a
1448     href="#workerfactory">WorkerFactory</a></code> interface, as noted earlier
1449     in this specification).
1450 wakaba 1.16
1451     <p class=big-issue>Need to define a sync database API.
1452    
1453     <p class=big-issue>May need to define a browser sniffing API (like
1454     window.navigator).</p>
1455     <!-- XXX ApplicationCache -->
1456     <!-- XXX a way to set cookies on the URL for the script -->
1457     <!-- XXX debugging: void log(in DOMString s); // log to console -->
1458     <!-- XXX debugging: onerror -->
1459    
1460     <hr>
1461    
1462 wakaba 1.19 <p><span class=ja-translation lang=ja>DOM API (<code>Node</code> オブジェクト、 <code>Document</code>
1463 wakaba 1.22 オブジェクト、その他) は、この版のこの仕様書では労働者に利用可能ではありません。</span><h3 id=importing><span class=secno>3.1 </span>Importing scripts and
1464     libraries</h3>
1465    
1466     <p>When a script invokes the <dfn id=importscript
1467     title=dom-WorkerGlobalScope-importScript><code>importScript(<var
1468     title="">url</var>)</code></dfn> method on a <code><a
1469     href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
1470     agent must run the following steps:
1471    
1472     <ol>
1473     <li>
1474     <p><span class=ja-translation lang=ja><var title="">url</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1475     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1476     例外を投げます。</span><li>
1477     <p><span class=ja-translation lang=ja>得られた絶対 URL の起源がメソッドを呼び出したスクリプトの起源と<span title="same origin">同じ</span>でない場合、
1478     保安性例外を投げます。</span><li>
1479     <p><span class=ja-translation lang=ja>得られた<span>絶対 URL</span> で識別される資源を<span>取って来</span><!-- XXX -->ようとします。</span></p>
1480    
1481     <p><span class=ja-translation lang=ja>この試みが失敗した場合、偽を返し、これらの段階を停止します。</span></p>
1482    
1483     <p><span class=ja-translation lang=ja>この試みが成功した場合、 <var title="">script</var>
1484     を得られた資源とします。</span></p>
1485    
1486     <p class=note><span class=ja-translation lang=ja>はじめのスクリプトと同様に、ここでのスクリプトも、 MIME 型に関わらず常に JavaScript
1487     であると仮定します。</span></p>
1488     <!-- XXX -->
1489    
1490     <li>
1491     <p><span class=ja-translation lang=ja><var title="">script</var> の<span>スクリプト実行文脈</span>、
1492     <span>スクリプト閲覧文脈</span>、<span>スクリプト文書文脈</span>を、
1493     この労働者についての<a href="#run-a">労働者を走らせる</a>処理モデルで実行されたスクリプトについてのものと同じとします。</span></p>
1494    
1495     <li>
1496     <p>Run <var title="">script</var> until it either returns, fails to catch
1497     an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
1498     a worker</a>" algorithm above.</p>
1499    
1500     <p>If an exception was raised or if the script was prematurely aborted,
1501     then abort these steps, letting the exception or aborting continue to be
1502     processed by the script that called the <code
1503     title=dom-WorkerGlobalScope-importScript><a
1504     href="#importscript">importScript()</a></code> method.</p>
1505    
1506     <p><span class=ja-translation lang=ja>そうでない場合、真を返します。</span></p>
1507     </ol>
1508    
1509     <h3 id=apis-defined><span class=secno>3.2 </span>APIs defined in other
1510     specifications</h3>
1511    
1512     <p>The <dfn id=localstorage
1513     title=dom-localStorage><code>localStorage</code></dfn>, <dfn
1514     id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
1515     must act as defined for the APIs with the same names on the
1516     <code>Window</code> object in the HTML5 specification, with the exception
1517     that where the API would use the <span>origin</span> of the <span>active
1518     document</span> of the <span>browsing context</span> of the
1519     <code>Window</code> object on which the method was supposedly invoked, it
1520     must instead use the <span>origin</span> of the script that invoked the
1521     method. <a href="#refsHTML5">[HTML5]</a>
1522    
1523     <p>The <dfn id=shownotification
1524     title=dom-showNotification><code>showNotification()</code></dfn> methods
1525     must act as defined for the APIs with the same names on the
1526     <code>Window</code> object in the HTML5 specification. <a
1527     href="#refsHTML5">[HTML5]</a>
1528    
1529     <h3 id=interface><span class=secno>3.3 </span>Interface objects and
1530     constructors</h3>
1531    
1532     <p>There must be no interface objects and constructors available in the
1533     global scope of scripts whose <span>script execution context</span> is a
1534     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1535     except for the following:
1536    
1537     <ul>
1538     <li>
1539     <p><span class=ja-translation lang=ja>XMLHttpRequest 仕様書で定義されている
1540     <code>XMLHttpRequest</code> とすべての界面オブジェクトと構築器、
1541     ただし<span>文書応答実体本体</span>は常に null でなければ<em class=rfc2119 title="MUST">なりません</em>。 <a
1542     href="#refsXHR">[XHR]</a></span><li>
1543     <p><span class=ja-translation lang=ja><code>WebSocket</code> 界面オブジェクトと構築器</span><li>
1544     <p><span class=ja-translation lang=ja><code>MessageChannel</code> 界面オブジェクトと構築器</span></ul>
1545 wakaba 1.23
1546     <h3 id=worker0><span class=secno>3.4 </span>Worker locations</h3>
1547    
1548     <pre
1549     class=idl>[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
1550     readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
1551     readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
1552     readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
1553     readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
1554     readonly attribute DOMString <a href="#port0" title=dom-WorkerLocation-port>port</a>;
1555     readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
1556     readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
1557     readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
1558     };</pre>
1559    
1560     <p>A <code><a href="#workerlocation">WorkerLocation</a></code> object
1561     represents an <span>absolute URL</span> set at its creation.
1562    
1563     <p>The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
1564     attribute must return the <span>absolute URL</span> that the object
1565     represents.
1566    
1567     <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface
1568     also has the complement of <span>URL decomposition attributes</span>, <dfn
1569     id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
1570     <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
1571     id=port0 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
1572     id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
1573     <dfn id=pathname
1574     title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
1575     id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
1576     <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
1577     must follow the rules given for URL decomposition attributes, with the
1578     <span title=concept-uda-input>input</span> being the <span>absolute
1579     URL</span> that the object represents (same as the <code
1580     title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
1581     and the <span title=concept-uda-setter>common setter action</span> being a
1582     no-op, since the attributes are defined to be readonly. <a
1583     href="#refsHTML5">[HTML5]</a>
1584 wakaba 1.22
1585     <h2 class=no-num id=references><span class=ja-translation lang=ja>参考文献</span></h2>
1586 wakaba 1.1
1587     <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>
1588     <!-- ACKS -->
1589    
1590 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 チームの方々にも非常に感謝します。
1591 wakaba 1.14 Gears チームの方々はこの技術を開拓し、その経験はこの仕様書にも多大な影響を与えています。
1592     </span>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24