/[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.29 - (hide annotations) (download)
Wed Aug 27 06:16:54 2008 UTC (17 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.28: +672 -455 lines
make

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.29 <ins class=ja-note>(原文出版日)</ins> 2008年8月27日</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 wakaba 1.28 <li><a href="#providing"><span class=secno>1.1.6 </span>Providing
150     libraries</a>
151 wakaba 1.23 </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.29 <li><a href="#the-global"><span class=secno>2.1 </span>The global
167     scope</a>
168     <ul class=toc>
169     <li><a href="#the-workerglobalscope"><span class=secno>2.1.1
170     </span>The <code>WorkerGlobalScope</code> abstract interface</a>
171    
172     <li><a href="#dedicated"><span class=secno>2.1.2 </span>Dedicated
173     workers and the <code>DedicatedWorkerGlobalScope</code> interface</a>
174    
175    
176     <li><a href="#shared0"><span class=secno>2.1.3 </span>Shared workers
177     and the <code>SharedWorkerGlobalScope</code> inteface</a>
178     </ul>
179 wakaba 1.9
180 wakaba 1.16 <li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
181     origins of workers</a>
182    
183     <li><a href="#the-queue"><span class=secno>2.3 </span>The queue of
184 wakaba 1.9 events</a>
185    
186 wakaba 1.16 <li><a href="#the-workers"><span class=secno>2.4 </span>The worker's
187 wakaba 1.11 ports</a>
188    
189 wakaba 1.16 <li><a href="#processing"><span class=secno>2.5 </span>Processing
190 wakaba 1.9 model</a>
191 wakaba 1.13
192 wakaba 1.16 <li><a href="#creating"><span class=secno>2.6 </span>Creating
193 wakaba 1.13 workers</a>
194 wakaba 1.29 <ul class=toc>
195     <li><a href="#the-abstractworker"><span class=secno>2.6.1 </span>The
196     <code>AbstractWorker</code> abstract interface</a>
197    
198     <li><a href="#dedicated0"><span class=secno>2.6.2 </span>Dedicated
199     workers and the <code>Worker</code> interface</a>
200    
201     <li><a href="#shared1"><span class=secno>2.6.3 </span>Shared workers
202     and the <code>SharedWorker</code> interface</a>
203     </ul>
204 wakaba 1.9 </ul>
205    
206 wakaba 1.13 <li><a href="#apis-available"><span class=secno>3. </span>APIs available
207     to workers</a>
208 wakaba 1.22 <ul class=toc>
209     <li><a href="#importing"><span class=secno>3.1 </span>Importing scripts
210     and libraries</a>
211    
212     <li><a href="#apis-defined"><span class=secno>3.2 </span>APIs defined in
213     other specifications</a>
214    
215     <li><a href="#interface"><span class=secno>3.3 </span>Interface objects
216     and constructors</a>
217 wakaba 1.23
218     <li><a href="#worker0"><span class=secno>3.4 </span>Worker locations</a>
219    
220 wakaba 1.22 </ul>
221 wakaba 1.1
222     <li class=no-num><a href="#references">References</a>
223    
224     <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
225     </ul>
226     <!--end-toc-->
227    
228     <hr>
229    
230 wakaba 1.3 <h2 id=introduction><span class=ja-translation lang=ja><span class=secno>1. </span>はじめに</span></h2>
231 wakaba 1.1
232 wakaba 1.12 <h3 id=tutorial><span class=ja-translation lang=ja><span class=secno>1.1 </span>解説</span></h3>
233 wakaba 1.1
234 wakaba 1.27 <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p><span class=ja-translation lang=ja>労働者は色々なことに使えます。次の各節では、色々な使い方の例を示します。</span><h4 id=a-background><span class=ja-translation lang=ja><span class=secno>1.1.1 </span>数値処理背景労働者</span></h4>
235 wakaba 1.23
236 wakaba 1.27 <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p><span class=ja-translation lang=ja>労働者の一番単純な用法は、計算的に高価な仕事を利用者界面を中断させずに実行することです。</span><p><span class=ja-translation lang=ja>この例では、主たる文書が労働者を作って素数を (素朴な方法で) 計算させ、
237     新しく見つかった素数を順番に表示してゆきます。</span><p><span class=ja-translation lang=ja>主たる頁は次の通りです。</span><pre>&lt;!DOCTYPE HTML>
238 wakaba 1.23 &lt;html>
239     &lt;head>
240     &lt;title>Worker example: One-core computation&lt;/title>
241     &lt;/head>
242     &lt;body>
243     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
244     &lt;script>
245 wakaba 1.29 var worker = new Worker('worker.js');
246     worker.onmessage = function (event) {
247 wakaba 1.23 document.getElementById('result').textContent = event.message;
248     };
249     &lt;/script>
250     &lt;/body>
251     &lt;/html></pre>
252    
253 wakaba 1.29 <p>The <code title=dom-WorkerFactory-createWorker>createWorker()</code>
254     method call creates a worker and returns a <code><a
255     href="#worker1">Worker</a></code> object representing that worker and
256     containing a <code>MessagePort</code> object, which is used to communicate
257     with the worker. That object's <code
258 wakaba 1.23 title=dom-MessagePort-onmessage>onmessage</code> event handler attribute
259     allows the code to receive messages from the worker.
260    
261 wakaba 1.27 <p><span class=ja-translation lang=ja>労働者自体は次の通りです。</span><pre>var n = 1;
262 wakaba 1.23 search: while (true) {
263     n += 1;
264     for (var i = 2; i &lt;= Math.sqrt(n); i += 1)
265     if (n % i == 0)
266     continue search;
267     // found a prime!
268 wakaba 1.29 postMessage(n);
269 wakaba 1.23 }</pre>
270    
271     <p>The bulk of this code is simply an unoptimised search for a prime
272     number. To send a message back to the page, the <code
273 wakaba 1.29 title=dom-WorkerGlobalScope-port>port</code> variable (defined
274     automatically when the worker is created) is used to post a message when a
275     prime is found.
276 wakaba 1.23
277 wakaba 1.27 <p><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/primes/page.html">この例をオンラインで見る</a>。</span><h4 id=a-worker><span class=secno>1.1.2 </span>A worker for updating a
278 wakaba 1.23 client-side database</h4>
279    
280     <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
281     listen for notifications from the server, and, when appropriate, either
282     add or remove data from the client-side database.
283    
284     <p>Since no communication occurs between the worker and the main page, the
285     main page can start the worker by just doing:
286    
287     <pre>&lt;script>
288     createWorker('worker.js');
289     &lt;/script></pre>
290    
291 wakaba 1.27 <p><span class=ja-translation lang=ja>労働者自体は次の通りです。</span><pre>var server = new WebSocket('ws://whatwg.org/database');
292 wakaba 1.25 var database = openDatabase('demobase', '1.0', 'Demo Database', 10240);
293 wakaba 1.23 server.onmessage = function (event) {
294     // data is in the format "command key value"
295     var data = event.message.split(' ');
296     switch (data[0]) {
297     case '+':
298     database.transaction(function(tx) {
299     tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
300     });
301     case '-':
302     database.transaction(function(tx) {
303     tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
304     });
305     }
306     };</pre>
307    
308     <p>This connects to the server using the <code>WebSocket</code> mechanism
309     and opens the local database (which, we presume, has been created
310     earlier). The worker then just listens for messages from the worker and
311     acts on them as appropriate, forever (or until the main page is closed).
312    
313     <p><a
314     href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
315     this example online</a>. (This example will not actually function, since
316     the server does not actually exist and the database is not created by this
317     sample code.)
318    
319     <h4 id=worker><span class=secno>1.1.3 </span>Worker used for backgroud I/O</h4>
320    
321     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>In this example, the main document uses two workers, one for fetching
322     stock updates for at regular intervals, and one for fetching performing
323     search queries that the user requests.
324    
325 wakaba 1.27 <p><span class=ja-translation lang=ja>主たる頁は次の通りです。</span><pre>&lt;!DOCTYPE HTML>
326 wakaba 1.23 &lt;html>
327     &lt;head>
328     &lt;title>Worker example: Stock ticker&lt;/title>
329     &lt;script>
330     // TICKER
331     var symbol = 'GOOG'; // default symbol to watch
332 wakaba 1.29 var ticker = new Worker('ticker.js');
333 wakaba 1.23
334     // SEARCHER
335 wakaba 1.29 var searcher = new Worker('searcher.js');
336 wakaba 1.23 function search(query) {
337 wakaba 1.29 searcher.postMessage(query);
338 wakaba 1.23 }
339    
340     // SYMBOL SELECTION UI
341     function select(newSymbol) {
342     symbol = newSymbol;
343 wakaba 1.29 ticker.postMessage(symbol);
344 wakaba 1.23 }
345     &lt;/script>
346     &lt;/head>
347     &lt;body>
348     &lt;p>&lt;output id="symbol">&lt;/output> &lt;output id="value">&lt;/output>&lt;/p>
349     &lt;script>
350 wakaba 1.29 ticker.onmessage = function (event) {
351 wakaba 1.23 var data = event.message.split(' ');
352     document.getElementById('symbol').textContent = data[0];
353     document.getElementById('value').textContent = data[1];
354     };
355 wakaba 1.29 ticker.postMessage(symbol);
356 wakaba 1.23 &lt;/script>
357     &lt;p>&lt;label>Search: &lt;input type="text" oninput="search(this.value)">&lt;/label>&lt;/p>
358     &lt;ul id="results">&lt;/ul>
359     &lt;script>
360 wakaba 1.29 searcher.onmessage = function (event) {
361 wakaba 1.23 var data = event.message.split(' ');
362     var results = document.getElementById('results');
363     while (results.hasChildNodes()) // clear previous results
364     results.removeChild(results.firstChild);
365     for (var i = 0; i &lt; data.length; i += 1) {
366     // add a list item with a button for each result
367     var li = document.createElement('li');
368     var button = document.createElement('button');
369     button.value = data[i];
370     button.type = 'button';
371     button.onclick = function () { select(this.value); };
372     button.textContent = data[i];
373     li.appendChild(button);
374     results.appendChild(li);
375     }
376     };
377     &lt;/script>
378     &lt;p>(The data in this example is not real. Try searching for "Google" or "Apple".)&lt;/p>
379     &lt;/body>
380     &lt;/html></pre>
381    
382     <p>Messages are queued until the <code
383     title=handler-MessagePort-onmessage>onmessage</code> handler is set, so
384     even if it takes a while for the message handler to be attached, no data
385     is lost.
386    
387     <p>The two workers use a common library for performing the actual network
388     calls. This library is as follows:
389    
390     <pre>function get(url) {
391 wakaba 1.29 try {
392     var xhr = new XMLHttpRequest();
393     xhr.open('GET', url, false);
394     xhr.send();
395     return xhr.responseText;
396     } catch (e) {
397     return ''; // turn all errors into empty results
398     }
399 wakaba 1.23 }</pre>
400    
401     <p>The stock updater worker is as follows:
402    
403 wakaba 1.25 <pre>importScripts('io.js');
404 wakaba 1.23 var timer;
405     var symbol;
406     function update() {
407 wakaba 1.29 postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
408 wakaba 1.23 timer = setTimeout(update, 10000);
409     }
410 wakaba 1.29 onmessage = function (event) {
411 wakaba 1.23 if (timer)
412     clearTimeout(timer);
413     symbol = event.message;
414     update();
415     };</pre>
416    
417     <p>The search query worker is as follows:
418    
419 wakaba 1.25 <pre>importScripts('io.js');
420 wakaba 1.29 onmessage = function (event) {
421     postMessage(get('search.cgi?' + event.message));
422 wakaba 1.23 };</pre>
423    
424 wakaba 1.27 <p><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/stocks/page.html">この例をオンラインで見る</a>。</span><h4 id=shared><span class=secno>1.1.4 </span>Shared workers</h4>
425 wakaba 1.23
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 wakaba 1.29 var worker = new SharedWorker('worker.js', 'core');
461 wakaba 1.23
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 wakaba 1.25 worker.port.removeEventListener('message', configure, false);
470 wakaba 1.23 }
471 wakaba 1.29 worker.addEventListener('message', configure, false);
472 wakaba 1.23
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 wakaba 1.29 worker.addEventListener('message', paintMap, false);
492 wakaba 1.23
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 wakaba 1.29 worker.addEventListener('message', updatePublicChat, false);
508 wakaba 1.23
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 wakaba 1.29 worker.addEventListener('message', startPrivateChat, false);
552 wakaba 1.23 &lt;/script>
553     &lt;/head>
554 wakaba 1.29 &lt;body>
555 wakaba 1.23 &lt;h1>Viewer&lt;/h1>
556     &lt;h2>Map&lt;/h2>
557     &lt;p>&lt;canvas id="map" height=150 width=150>&lt;/canvas>&lt;/p>
558     &lt;p>
559 wakaba 1.29 &lt;button type=button onclick="worker.postMessage('mov left')">Left&lt;/button>
560     &lt;button type=button onclick="worker.postMessage('mov up')">Up&lt;/button>
561     &lt;button type=button onclick="worker.postMessage('mov down')">Down&lt;/button>
562     &lt;button type=button onclick="worker.postMessage('mov right')">Right&lt;/button>
563     &lt;button type=button onclick="worker.postMessage('set 0')">Set 0&lt;/button>
564     &lt;button type=button onclick="worker.postMessage('set 1')">Set 1&lt;/button>
565 wakaba 1.23 &lt;/p>
566     &lt;h2>Public Chat&lt;/h2>
567     &lt;dialog id="public">&lt;/dialog>
568 wakaba 1.29 &lt;form onsubmit="worker.postMessage('txt ' + message.value); message.value = ''; return false;">
569 wakaba 1.23 &lt;p>
570     &lt;input type="text" name="message" size="50">
571     &lt;button>Post&lt;/button>
572     &lt;/p>
573     &lt;/form>
574     &lt;h2>Private Chat&lt;/h2>
575     &lt;ul id="private">&lt;/ul>
576     &lt;/body>
577 wakaba 1.25 &lt;/html>
578     </pre>
579 wakaba 1.23
580     <p>There are several key things worth noting about the way the viewer is
581     written.
582    
583     <p><strong>Multiple listeners</strong>. Instead of a single message
584     processing function, the code here attaches multiple event listeners, each
585     one performing a quick check to see if it is relevant for the message. In
586     this example it doesn't make much difference, but if multiple authors
587     wanted to collaborate using a single port to communicate with a worker, it
588     would allow for independent code instead of changes having to all be made
589     to a single event handling function.
590    
591     <p>Because event listeners are registered using <code
592     title="">addEventListener()</code> instead of <code
593     title=handler-MessagePort-onmessage>onmessge</code>, the events remain
594     queued until the <code title=dom-MessagePort-start>start()</code> method
595     is called on the message port, in the <code title="">init()</code>
596     function called from the <code title=handler-onload>onload</code> handler.
597    
598     <p>Registering event listeners in this way also allows you to unregister
599     specific listeners when you are done with them, as is done with the <code
600     title="">configure()</code> method in this example.
601    
602     <p>Finally, the worker:
603    
604     <pre>
605     var nextName = 0;
606     function getNextName() {
607     // this could use more friendly names
608     // but for now just return a number
609     return nextName++;
610     }
611    
612     var map = [
613     [0, 0, 0, 0, 0, 0, 0],
614     [1, 1, 0, 1, 0, 1, 1],
615     [0, 1, 0, 1, 0, 0, 0],
616     [0, 1, 0, 1, 0, 1, 1],
617     [0, 0, 0, 1, 0, 0, 0],
618     [1, 0, 0, 1, 1, 1, 1],
619     [1, 1, 0, 1, 1, 0, 1],
620     ];
621    
622     function wrapX(x) {
623     if (x &lt; 0) return wrapX(x + map[0].length);
624     if (x >= map[0].length) return wrapX(x - map[0].length);
625     return x;
626     }
627    
628     function wrapY(y) {
629     if (y &lt; 0) return wrapY(y + map.length);
630     if (y >= map[0].length) return wrapY(y - map.length);
631     return y;
632     }
633    
634 wakaba 1.29 function sendMapData(callback) {
635 wakaba 1.23 var data = '';
636     for (var y = viewer.y-1; y &lt;= viewer.y+1; y += 1) {
637     for (var x = viewer.x-1; x &lt;= viewer.x+1; x += 1) {
638     if (data != '')
639     data += ',';
640     data += map[y][x];
641     }
642     }
643 wakaba 1.29 callback('map ' + data);
644 wakaba 1.23 }
645    
646     var viewers = {};
647     onconnect = function (event) {
648     event.port._name = getNextName();
649     event.port._data = { port: event.port, x: 0, y: 0, };
650     viewers[event.port._name] = event.port._data;
651     event.port.postMessage('cfg ' + name);
652     event.port.onmessage = getMessage;
653 wakaba 1.29 sendMapData(event.port.postMessage);
654 wakaba 1.23 };
655    
656     function getMessage(event) {
657     switch (event.message.substr(0, 4)) {
658     case 'mov ':
659     var direction = event.message.substr(4);
660     var dx = 0;
661     var dy = 0;
662     switch (direction) {
663     case 'up': dy = -1; break;
664     case 'down': dy = 1; break;
665     case 'left': dx = -1; break;
666     case 'right': dx = 1; break;
667     }
668     event.target._data.x = wrapX(event.target._data.x + dx);
669     event.target._data.y = wrapY(event.target._data.y + dy);
670 wakaba 1.29 sendMapData(event.target.postMessage);
671 wakaba 1.23 break;
672     case 'set ':
673     var value = event.message.substr(4);
674     map[event.target._data.y][event.target._data.x] = value;
675     for (var viewer in viewers)
676 wakaba 1.29 sendMapData(viewers[viewer].port.postMessage);
677 wakaba 1.23 break;
678     case 'txt ':
679     var name = event.target._name;
680     var message = event.message.substr(4);
681     for (var viewer in viewers)
682     viewers[viewer].port.postMessage('txt ' + name + ' ' + message);
683     break;
684     case 'msg ':
685     var party1 = event._data;
686     var party2 = viewers[event.message.substr(4).split(' ', 1)];
687     if (party2) {
688     var channel = new MessageChannel();
689     party1.port.postMessage('msg ' + party2.name, channel.port1);
690     party2.port.postMessage('msg ' + party1.name, channel.port2);
691     }
692     break;
693     }
694     }</pre>
695    
696     <p><strong>Connecting to multiple pages</strong>. Instead of using the
697 wakaba 1.29 <code title=dom-WorkerGlobalScope-port>port</code> global variable in the
698     worker, the script uses the <code
699     title=handler-WorkerGlobalScope-onconnect>onconnect</code> event listener
700     to listen for multiple connections.
701 wakaba 1.23
702     <p><strong>Direct channels</strong>. When the worker receives a "msg"
703     message from one viewer naming another viewer, it sets up a direct
704     connection between the two, so that the two viewers can communicate
705     directly without the worker having to proxy all the messages.
706    
707 wakaba 1.27 <p><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/multiviewer/page.html">この例をオンラインで見る</a>。</span><h4 id=delegation><span class=secno>1.1.5 </span>Delegation</h4>
708 wakaba 1.23
709     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>With multicore CPUs becoming prevalent, one way to obtain better
710     performance is to split computationally expensive tasks amongst multiple
711     workers. In this example, a computationally expensive task that is to be
712     performed for every number from 1 to 10,000,000 is farmed out to ten
713     subworkers.
714    
715     <p>The main page is as follows, it just reports the result:
716    
717     <pre>&lt;!DOCTYPE HTML>
718     &lt;html>
719     &lt;head>
720     &lt;title>Worker example: One-core computation&lt;/title>
721     &lt;/head>
722     &lt;body>
723     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
724     &lt;script>
725 wakaba 1.29 var worker = new Worker('worker.js');
726     worker.onmessage = function (event) {
727 wakaba 1.23 document.getElementById('result').textContent = event.message;
728     };
729     &lt;/script>
730     &lt;/body>
731     &lt;/html></pre>
732    
733 wakaba 1.27 <p><span class=ja-translation lang=ja>労働者自体は次の通りです。</span><pre>// settings
734 wakaba 1.23 var num_workers = 10;
735     var items_per_worker = 1000000;
736    
737     // start the workers
738     var result = 0;
739     var pending_workers = num_workers;
740     for (var i = 0; i &lt; num_workers; i += 1) {
741 wakaba 1.29 var worker = new Worker('core.js');
742     worker.postMessage(i * items_per_worker);
743     worker.postMessage((i+1) * items_per_worker);
744     worker.onmessage = storeResult;
745 wakaba 1.23 }
746    
747     // handle the results
748     function storeResult(event) {
749     result += 1*event.message;
750     pending_workers -= 1;
751     if (pending_workers &lt;= 0)
752 wakaba 1.29 postMessage(result); // finished!
753 wakaba 1.23 }</pre>
754    
755     <p>It consists of a loop to start the subworkers, and then a handler that
756     waits for all the subworkers to respond.
757    
758     <p>The subworkers are implemented as follows:
759    
760     <pre>var start;
761     onmessage = getStart;
762     function getStart(event) {
763     start = 1*event.message;
764     onmessage = getEnd;
765     }
766    
767     var end;
768     function getEnd(event) {
769     end = 1*event.message;
770     onmessage = null;
771     do();
772     }
773    
774     function do() {
775     var result = 0;
776     for (var i = start; i &lt; end; i += 1) {
777     // perform some complex calculation here
778     result += 1;
779     }
780 wakaba 1.29 postMessage(result);
781     close();
782 wakaba 1.23 }</pre>
783    
784     <p>They receive two numbers in two events, perform the computation for the
785     range of numbers thus specified, and then report the result back to the
786     parent.
787    
788 wakaba 1.28 <p><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/multicore/page.html">この例をオンラインで見る</a>。</span><h4 id=providing><span class=secno>1.1.6 </span>Providing libraries</h4>
789    
790     <p><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p>Suppose that a cryptography library is made available that provides
791     three tasks:
792    
793     <dl>
794     <dt>Generate a public/private key pair
795    
796     <dd>Takes a port, on which it will send two messages, first the public key
797     and then the private key.
798    
799     <dt>Given a plaintext and a public key, return the corresponding
800     cyphertext
801    
802     <dd>Takes a port, to which any number of messages can be sent, the first
803     giving the public key, and the remainder giving the plaintext, each of
804     which is encrypted and then sent on that same channel as the cyphertext.
805     The user can close the port when it is done encrypting content.
806    
807     <dt>Given a cyphertext and a private key, return the corresponding
808     plaintext
809    
810     <dd>Takes a port, to which any number of messages can be sent, the first
811     giving the private key, and the remainder giving the cyphertext, each of
812     which is decrypted and then sent on that same channel as the plaintext.
813     The user can close the port when it is done decrypting content.
814     </dl>
815    
816     <p>The library itself is as follows:
817    
818     <pre>function handleMessage(e) {
819     if (e.message == "genkeys")
820     genkeys(e.port);
821     else if (e.message == "encrypt")
822     encrypt(e.port);
823     else if (e.message == "decrypt")
824     decrypt(e.port);
825     }
826    
827     function genkeys(p) {
828     var keys = _generateKeyPair();
829     p.postMessage(keys[0]);
830     p.postMessage(keys[1]);
831     }
832    
833     function encrypt(p) {
834     var key, state = 0;
835     p.onmessage = function (e) {
836     if (state == 0) {
837     key = e.message;
838     state = 1;
839     } else {
840     p.postMessage(_encrypt(key, e.message));
841     }
842     };
843     }
844    
845     function decrypt(p) {
846     var key, state = 0;
847     p.onmessage = function (e) {
848     if (state == 0) {
849     key = e.message;
850     state = 1;
851     } else {
852     p.postMessage(_decrypt(key, e.message));
853     }
854     };
855     }
856    
857 wakaba 1.29 // support being used as a shared worker as well as a dedicated worker
858     if (this.onmessage) // dedicated worker
859     onmessage = handleMessage;
860     else // shared worker
861     onconnect = function (e) { e.port.onmessage = handleMessage; }
862 wakaba 1.28
863    
864     // the "crypto" functions:
865    
866     function _generateKeyPair() {
867     return [Math.random(), Math.random()];
868     }
869    
870     function _encrypt(k, s) {
871     return 'encrypted-' + k + ' ' + s;
872     }
873    
874     function _decrypt(k, s) {
875     return s.substr(s.indexOf(' ')+1);
876     }</pre>
877    
878     <p>Note that the crypto functions here are just stubs and don't do real
879     cryptography.
880    
881     <p>This library could be used as follows:
882    
883     <pre>&lt;!DOCTYPE HTML>
884     &lt;html>
885     &lt;head>
886     &lt;title>Worker example: Crypto library&lt;/title>
887     &lt;script>
888 wakaba 1.29 var crytoLib = new Worker('libcrypto-v1.js'); // or could use 'libcrypto-v2.js'
889 wakaba 1.28 function getKeys() {
890     var state = 0;
891 wakaba 1.29 cryptoLib.startConversation("genkeys").onmessage = function (e) {
892 wakaba 1.28 if (state == 0)
893     document.getElementById('public').value = e.message;
894     else if (state == 1)
895     document.getElementById('private').value = e.message;
896     state += 1;
897     };
898     }
899     function enc() {
900 wakaba 1.29 var port = cryptoLib.startConversation("encrypt");
901     port.postMessage(document.getElementById('public').value);
902     port.postMessage(document.getElementById('input').value);
903     port.onmessage = function (e) {
904 wakaba 1.28 document.getElementById('input').value = e.message;
905 wakaba 1.29 port.close();
906 wakaba 1.28 };
907     }
908     function dec() {
909 wakaba 1.29 var port = cryptoLib.startConversation("decrypt");
910     port.postMessage(document.getElementById('private').value);
911     port.postMessage(document.getElementById('input').value);
912     port.onmessage = function (e) {
913 wakaba 1.28 document.getElementById('input').value = e.message;
914 wakaba 1.29 port.close();
915 wakaba 1.28 };
916     }
917     &lt;/script>
918     &lt;style>
919     textarea { display: block; }
920     &lt;/style>
921     &lt;/head>
922     &lt;body onload="getKeys()">
923     &lt;fieldset>
924     &lt;legend>Keys&lt;/legend>
925 wakaba 1.29 &lt;p>&lt;label>Public Key: &lt;textarea id="public">&lt;/textarea>&lt;/label>&lt;/p>
926     &lt;p>&lt;label>Private Key: &lt;textarea id="private">&lt;/textarea>&lt;/label>&lt;/p>
927 wakaba 1.28 &lt;/fieldset>
928 wakaba 1.29 &lt;p>&lt;label>Input: &lt;textarea id="input">&lt;/textarea>&lt;/label>&lt;/p>
929 wakaba 1.28 &lt;p>&lt;button onclick="enc()">Encrypt&lt;/button> &lt;button onclick="dec()">Decrypt&lt;/button>&lt;/p>
930     &lt;/body>
931     &lt;/html></pre>
932    
933     <p>A later version of the API, though, might want to offload all the crypto
934     work onto subworkers. This could be done as follows:
935    
936     <pre>function handleMessage(e) {
937     if (e.message == "genkeys")
938     genkeys(e.port);
939     else if (e.message == "encrypt")
940     encrypt(e.port);
941     else if (e.message == "decrypt")
942     decrypt(e.port);
943     }
944    
945     function genkeys(p) {
946 wakaba 1.29 var generator = new Worker('libcrypto-v2-generator.js');
947 wakaba 1.28 generator.postMessage('', p);
948     }
949    
950     function encrypt(p) {
951     p.onmessage = function (e) {
952     var key = e.message;
953 wakaba 1.29 var encryptor = new Worker('libcrypto-v2-encryptor.js');
954 wakaba 1.28 encryptor.postMessage(key, p);
955     };
956     }
957    
958     function encrypt(p) {
959     p.onmessage = function (e) {
960     var key = e.message;
961 wakaba 1.29 var decryptor = new Worker('libcrypto-v2-decryptor.js');
962 wakaba 1.28 decryptor.postMessage(key, p);
963     };
964     }
965    
966 wakaba 1.29 // support being used as a shared worker as well as a dedicated worker
967     if (this.onmessage) // dedicated worker
968     onmessage = handleMessage;
969     else // shared worker
970     onconnect = function (e) { e.port.onmessage = handleMessage; }
971 wakaba 1.28 </pre>
972    
973     <p>The little subworkers would then be as follows.
974    
975     <p>For generating key pairs:
976    
977 wakaba 1.29 <pre>onmessage = function (e) {
978     var k = _generateKeyPair();
979     e.port.postMessage(k[0]);
980     e.port.postMessage(k[1]);
981 wakaba 1.28 close();
982     }
983 wakaba 1.29
984     function _generateKeyPair() {
985     return [Math.random(), Math.random()];
986     }</pre>
987 wakaba 1.28
988     <p>For encrypting:
989    
990 wakaba 1.29 <pre>onmessage = function (e) {
991 wakaba 1.28 var key = e.message;
992     e.port.onmessage = function (e) {
993     var s = e.message;
994 wakaba 1.29 postMessage(_encrypt(key, s));
995 wakaba 1.28 }
996 wakaba 1.29 e.port.onclose = function (e) {
997 wakaba 1.28 close();
998     }
999 wakaba 1.29 }
1000    
1001     function _encrypt(k, s) {
1002     return 'encrypted-' + k + ' ' + s;
1003 wakaba 1.28 }</pre>
1004    
1005     <p>For decrypting:
1006    
1007 wakaba 1.29 <pre>onmessage = function (e) {
1008 wakaba 1.28 var key = e.message;
1009     e.port.onmessage = function (e) {
1010     var s = e.message;
1011 wakaba 1.29 postMessage(_decrypt(key, s));
1012 wakaba 1.28 }
1013 wakaba 1.29 e.port.onclose = function (e) {
1014 wakaba 1.28 close();
1015     }
1016 wakaba 1.29 }
1017    
1018     function _decrypt(k, s) {
1019     return s.substr(s.indexOf(' ')+1);
1020 wakaba 1.28 }</pre>
1021    
1022     <p>Notice how the users of the API don't have to even know that this is
1023     happening &mdash; the API hasn't changed; the library can delegate to
1024     subworkers without changing its API, even though it is accepting data
1025     using message channels.
1026    
1027 wakaba 1.29 <p><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/crypto/page.html">この例をオンラインで見る</a>。</span><h3 id=conformance><span class=ja-translation lang=ja><span class=secno>1.2 </span>適合性要件</span></h3>
1028 wakaba 1.1
1029 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書のすべての図、例示、注意書き、ならびに規定の一部ではないと明記したすべての節は、
1030 wakaba 1.1 規定の一部ではありません。
1031 wakaba 1.24 この仕様書のそれ以外の部分はすべて規定の一部です。</span><p><span class=ja-translation lang=ja>この文書の規定の部分にあるキーワード「<em class=rfc2119 title="MUST">しなければなりません</em>」、
1032 wakaba 1.1 「<em class=rfc2119 title="MUST NOT">してはなりません</em>」、
1033 wakaba 1.24 「<em class=rfc2119 title="REQUIRED">必須です</em>」、<!--"SHALL", "SHALL
1034     NOT",-->「<em class=rfc2119 title="SHOULD">するべきです</em>」、
1035 wakaba 1.1 「<em class=rfc2119 title="SHOULD NOT">するべきではありません</em>」、
1036     「<em class=rfc2119 title="RECOMMENDED">推奨します</em>」、「<em class=rfc2119 title="MAY">して構いません</em>」、
1037     「<em class=rfc2119 title="OPTIONAL">任意です</em>」は、 RFC 2119 で説明されているように解釈します。
1038 wakaba 1.24 この仕様書<ins class=ja-note>の原文</ins>では、
1039     可読性の観点から、
1040     これらの語<ins class=ja-note>の原語</ins>のすべて大文字による表記は行っていません。
1041 wakaba 1.1 <a href="#refsRFC2119">[RFC2119]</a></span></p>
1042     <!-- XXX but they should be
1043     marked up -->
1044    
1045     <p><span class=ja-translation lang=ja>算法の一部として<ins class=ja-note>動作の指示の形で (原文では</ins>命令形で<ins class=ja-note>) </ins>記述された要件
1046     (例えば「先頭の間隔文字をすべて除去します」や「偽を返し、これらの段階を停止します」)
1047 wakaba 1.24 は、算法を紹介するに当たって使われたキーワード (<em class=rfc2119 title="MUST">しなければなりません</em>、
1048 wakaba 1.1 <em class=rfc2119 title="SHOULD">するべきです</em>、<em class=rfc2119 title="MAY">して構いません</em>など) の意味により解釈します。</span><p><span class=ja-translation lang=ja>いくつかの適合性要件は属性やメソッドやオブジェクトの要件として説明されています。
1049     そのような要件は、利用者エージェントに関する要件として解釈します。</span><p><span class=ja-translation lang=ja>算法や特定の段階として説明されている適合性要件は、
1050     最終的な結果が等価である限り、任意の方法で実装して<em class=rfc2119 title="MAY">構いません</em>。
1051     (特に、この仕様書で定義されている算法は追うのを簡単にしており、
1052 wakaba 1.24 高性能であることを目指してはいません。)</span><p><span class=ja-translation lang=ja>この仕様書で定義する唯一の適合性の級は利用者エージェントです。</span><p><span class=ja-translation lang=ja>利用者エージェントは、サービス拒否攻撃を防ぐため、
1053 wakaba 1.1 記憶容量を使い尽くすことを防ぐため、
1054     あるいは環境特有の制限に対応するためなどの目的で、
1055 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>
1056 wakaba 1.1
1057 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書は、他のいくつかの下位の仕様書に依存しています。</span><dl>
1058 wakaba 1.1 <dt>HTML5
1059    
1060     <dd>
1061 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書では、 HTML5 の多くの基礎的な概念を使用します。
1062 wakaba 1.1 <a href="#refsHTML5">[HTML5]</a></span></p>
1063    
1064     <dt>ECMAScript
1065    
1066     <dd>
1067 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書は、スクリプト言語として JavaScript
1068 wakaba 1.1 を使うことを想定しています。 <a href="#refsJS">[JS]</a></span></p>
1069    
1070     <dt>WebIDL
1071    
1072     <dd>
1073 wakaba 1.24 <p><span class=ja-translation lang=ja>この仕様書の IDL ブロックは、WebIDL 仕様書における意味を用います。 <a href="#refsWebIDL">[WebIDL]</a></span></p>
1074 wakaba 1.1 </dl>
1075    
1076 wakaba 1.16 <h3 id=terminology><span class=ja-translation lang=ja><span class=secno>1.3 </span>用語</span></h3>
1077 wakaba 1.1
1078 wakaba 1.24 <p><span class=ja-translation lang=ja>簡単のため、文書が利用者に対してレンダリングされる方法に言及する時に<em>示す</em>、
1079     <em>表示する</em>、<em>可視</em>などの用語を使うことがあります。
1080     このような用語は特に視覚媒体を暗示しているわけではありません。
1081     他の媒体であってもこれらの用語が等価な方法で適用されるものと考えなければ<em class=rfc2119 title="MUST">なりません</em>。</span><p><span class=ja-translation lang=ja>「<code title="">Foo</code> オブジェクト」という構文 (<code title="">Foo</code>
1082 wakaba 1.10 は実際には界面) を時々使いますが、より正確な「界面 <code title="">Foo</code>
1083     を実装しているオブジェクト」の代わりに用いています。</span><p><span class=ja-translation lang=ja>DOM という語は、 Web アプリケーションでスクリプトに利用可能とされている API
1084 wakaba 1.1 の集合を指して用い、必ずしも DOM 中核仕様書で定義されている実際の
1085     <code>Document</code> オブジェクトその他の <code>Node</code>
1086     オブジェクトの存在を暗示してはいません。<a
1087     href="#refsDOM3CORE">[DOM3CORE]</a></span><p><span class=ja-translation lang=ja>DOM 属性は、その値が (例えば著者のスクリプトにより) 取り出される時<em>取得される</em>といい、
1088     新しい値がその属性に割り当てられる時<em>設定される</em>といいます。</span><p><span class=ja-translation lang=ja>DOM オブジェクトが<dfn id=live>生きている</dfn>という場合、
1089     そのオブジェクトを返す属性は常に同じオブジェクトを返さなければ<em class=rfc2119 title="MUST">ならない</em>
1090     (毎回新しいオブジェクトを返すのではなく) 上に、
1091     そのオブジェクトの属性やメソッドがそのオブジェクトの裏にある実際のデータのスナップ写真ではなく、
1092 wakaba 1.27 データそのものに対して操作されなければ<em class=rfc2119 title="MUST">ならない</em>ことを意味します。</span><h2 id=infrastructure><span class=ja-translation lang=ja><span class=secno>2. </span>基盤</span></h2>
1093 wakaba 1.9
1094 wakaba 1.29 <p>There are two kinds of workers; dedicated workers, and shared workers.
1095     Dedicated workers, once created, and are linked to their creator; but
1096     message ports can be used to communicate from a dedicated worker to
1097     multple other browsing contexts or workers. Shared workers, on the other
1098     hand, are named, and once created any script running in the same
1099     <span>origin</span> can obtain a reference to that worker and communicate
1100     with it.
1101    
1102     <h3 id=the-global><span class=secno>2.1 </span>The global scope</h3>
1103    
1104     <p>The global scope is the "inside" of a worker.
1105    
1106     <h4 id=the-workerglobalscope><span class=ja-translation lang=ja><span class=secno>2.1.1 </span><code><a href="#workerglobalscope">WorkerGlobalScope</a></code> abstract 界面</span></h4>
1107 wakaba 1.9
1108     <pre
1109 wakaba 1.22 class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
1110     readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
1111 wakaba 1.23 readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
1112 wakaba 1.29 // also implements everything on <a href="#workerutils">WorkerUtils</a>
1113    
1114 wakaba 1.22 void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
1115 wakaba 1.29 attribute <span>EventListener</span> <a href="#onclose" title=handler-WorkerGlobalScope-onclose>onclose</a>;
1116 wakaba 1.9 };</pre>
1117    
1118 wakaba 1.23 <p>Objects implementing the <code><a
1119 wakaba 1.29 href="#workerglobalscope">WorkerGlobalScope</a></code> interface it must
1120     also implement the <code>EventTarget</code> interface.
1121 wakaba 1.23
1122 wakaba 1.22 <p>The <dfn id=self
1123     title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
1124     return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1125     object itself.
1126    
1127 wakaba 1.23 <p>The <dfn id=location
1128     title=dom-WorkerGlobalScope-location><code>location</code></dfn> attribute
1129     must return the <code><a href="#workerlocation">WorkerLocation</a></code>
1130     object created for the <code><a
1131     href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
1132     worker was created. It represents the <span>absolute URL</span> of the
1133     script that was used to initialize the worker.
1134 wakaba 1.22
1135 wakaba 1.29 <hr>
1136    
1137     <p>When a script invokes the <dfn id=close
1138     title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
1139     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
1140     the user agent must run the following steps:
1141    
1142     <ol>
1143     <li>
1144     <p>Create an <code>Event</code> object with the event name <code
1145     title=event-close>close</code>, which does not bubble and is not
1146     cancelable, and add it to the <code><a
1147     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1148     href="#queue">queue of events</a>, targetted at the <code><a
1149     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1150    
1151     <li>
1152     <p>Set the worker's <code><a
1153     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1154     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1155     true.
1156    
1157     <li>
1158     <p>For each <code>MessagePort</code> object that is entangled with
1159     another port and that has one (but only one) port whose owner is the
1160     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1161     on which the method was invoked (this would include, for instance, the
1162     implicit port in used for dedicated workers), run the following
1163     substeps:</p>
1164    
1165     <ol>
1166     <li>
1167     <p><span class=ja-translation lang=ja>2つのポートをほどきます。</span><li>
1168     <p>At the next available opportunity, after any scripts have finished
1169     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1170     <code title=event-close>close</code> at the other port (the one whose
1171     owner is not the <code><a
1172     href="#workerglobalscope">WorkerGlobalScope</a></code> object on which
1173     the <code title=dom-WorkerGlobalScope-close><a
1174     href="#close">close()</a></code> method was called).
1175     </ol>
1176     </ol>
1177 wakaba 1.9
1178 wakaba 1.22 <p>The following are the <span>event handler DOM attributes</span> that
1179     must be supported by objects implementing the <code><a
1180     href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
1181    
1182     <dl>
1183 wakaba 1.29 <dt><dfn id=onclose
1184     title=handler-WorkerGlobalScope-onclose><code>onclose</code></dfn>
1185 wakaba 1.11
1186     <dd>
1187 wakaba 1.29 <p><span class=ja-translation lang=ja><code title=event-close>close</code> 事象が当該 <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1188     呼び出されなければなりません。</span></dl>
1189    
1190     <h4 id=dedicated><span class=secno>2.1.2 </span>Dedicated workers and the
1191     <code><a
1192     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1193     interface</h4>
1194    
1195     <pre
1196     class=idl>[NoInterfaceObject] interface <dfn id=dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1197     boolean <a href="#postmessage" title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in DOMString message);
1198     boolean <a href="#postmessage" title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in DOMString message, in <span>MessagePort</span> messagePort);
1199     <span>MessagePort</span> <a href="#startconversation" title=dom-DedicatedWorkerGlobalScope-startConversation>startConversation</a>(in DOMString message);
1200     attribute <span>EventListener</span> <a href="#onmessage" title=handler-DedicatedWorkerGlobalScope-onmessage>onmessage</a>;
1201     };</pre>
1202    
1203     <p><code><a
1204     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1205     objects act as if they had an implicit <code>MessagePort</code> associated
1206     with them. This port is part of a channel that is set up when the worker
1207     is created, but it is not exposed. This object must never be garbage
1208     collected before the <code><a
1209     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1210     object.
1211    
1212     <p>All messages received by that port must immediately be retargetted at
1213     the <code><a
1214     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1215     object.
1216    
1217     <p>The <dfn id=postmessage
1218     title=dom-DedicatedWorkerGlobalScope-postMessage><code>postMessage()</code></dfn>
1219     and <dfn id=startconversation
1220     title=dom-DedicatedWorkerGlobalScope-startConversation><code>startConversation()</code></dfn>
1221     methods on <code><a
1222     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1223     objects must act as if, when invoked, they immediately invoked the method
1224     of the same name on the port, with the same arguments.
1225    
1226     <p>The following are the <span>event handler DOM attributes</span> that
1227     must be supported by objects implementing the <code><a
1228     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1229     interface:
1230    
1231     <dl>
1232     <dt><dfn id=onmessage
1233     title=handler-DedicatedWorkerGlobalScope-onmessage><code>onmessage</code></dfn>
1234 wakaba 1.11
1235     <dd>
1236 wakaba 1.29 <p><span class=ja-translation lang=ja><code title=event-DedicatedWorkerGlobalScope-message>message</code> 事象が当該 <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1237 wakaba 1.12 呼び出されなければなりません。</span></dl>
1238 wakaba 1.11
1239 wakaba 1.29 <h4 id=shared0><span class=secno>2.1.3 </span>Shared workers and the
1240     <code><a
1241     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1242     inteface</h4>
1243    
1244     <pre
1245     class=idl>[NoInterfaceObject] interface <dfn id=sharedworkerglobalscope>SharedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1246     readonly attribute DOMString <a href="#name" title=dom-SharedWorkerGlobalScope-name>name</a>;
1247     attribute <span>EventListener</span> <a href="#onconnect" title=handler-SharedWorkerGlobalScope-onconnect>onconnect</a>;
1248     };</pre>
1249    
1250     <p>Shared workers receive message ports through <code
1251     title=event-WorkerGlobalScope-connect>connect</code> events on their
1252     global object for each connection.
1253    
1254     <p>The <dfn id=name
1255     title=dom-SharedWorkerGlobalScope-name><code>name</code></dfn> attribute
1256     must return the value it was assigned when the <code><a
1257     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object
1258     was created by the "<a href="#run-a">run a worker</a>" algorithm. Its
1259     value represents the name that can be used to obtain a reference to the
1260     worker using the <code><a href="#sharedworker">SharedWorker</a></code>
1261     constructor.
1262    
1263     <p>The following are the <span>event handler DOM attributes</span> that
1264     must be supported by objects implementing the <code><a
1265     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1266     interface:
1267    
1268     <dl>
1269     <dt><dfn id=onconnect
1270     title=handler-SharedWorkerGlobalScope-onconnect><code>onconnect</code></dfn>
1271    
1272     <dd>
1273     <p><span class=ja-translation lang=ja><code title=event-SharedWorkerGlobalScope-connect>connect</code> 事象が当該 <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1274     呼び出されなければなりません。</span></dl>
1275 wakaba 1.23
1276 wakaba 1.18 <h3 id=base-urls><span class=ja-translation lang=ja><span class=secno>2.2 </span>労働者の基底 URL と起源</span></h3>
1277 wakaba 1.16
1278 wakaba 1.22 <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
1279 wakaba 1.23 worker is the <span>absolute URL</span> given that the worker's <code
1280     title=dom-WorkerGlobalScope-location><a
1281     href="#location">location</a></code> attribute represents.
1282 wakaba 1.22
1283     <p>Both the <span>origin</span> and <span>effective script origin</span> of
1284     scripts running in workers are the <span>origin</span> of the
1285 wakaba 1.23 <span>absolute URL</span> given that the worker's <code
1286     title=dom-WorkerGlobalScope-location><a
1287     href="#location">location</a></code> attribute represents.
1288 wakaba 1.22
1289     <h3 id=the-queue><span class=ja-translation lang=ja><span class=secno>2.3 </span>事象の待ち行列</span></h3>
1290    
1291     <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1292     object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
1293     initially empty.
1294 wakaba 1.9
1295 wakaba 1.22 <p><span class=ja-translation lang=ja>待ち行列中の事象は、 DOM 事象か小休止呼び戻しのいずれかです。</span><p><span class=ja-translation lang=ja>労働者中で呼び出しまたは発送されるすべての非同期の呼び戻しと事象は、
1296 wakaba 1.17 労働者の待ち行列に追加されなければなりません。
1297 wakaba 1.12 ただし、実際の呼び戻しの呼び出しや事象の発送は、後述の「<a
1298 wakaba 1.29 href="#run-a">労働者を走らせる</a>」処理モデルに従い処理されます。</span><p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1299     object also has a <dfn id=closing
1300     title=dom-WorkerGlobalScope-closing>closing</dfn> flag, which must
1301     initially be false, but which can get set to true by the algorithms in the
1302     processing model section below.
1303    
1304     <p>Once the <code><a
1305     href="#workerglobalscope">WorkerGlobalScope</a></code>'s <a
1306     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1307     set to true, the queue must discard anything else that would be added to
1308     it. Effectively, once the <a href="#closing"
1309     title=dom-WorkerGlobalScope-closing>closing</a> flag is true, timers stop
1310     firing, notifications for all pending asynchronous operations are dropped,
1311     etc.
1312 wakaba 1.22
1313     <h3 id=the-workers><span class=ja-translation lang=ja><span class=secno>2.4 </span>労働者のポート</span></h3>
1314 wakaba 1.11
1315 wakaba 1.14 <p><span class=ja-translation lang=ja>労働者は、メッセージ通信路とその <code>MessagePort</code>
1316 wakaba 1.22 オブジェクトを通じて、他の労働者や閲覧文脈と通信します。</span><p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1317 wakaba 1.29 <var title="">worker global scope</var> has a list of <dfn
1318     id=the-workers0>the worker's ports</dfn>, which consists of all the
1319     <code>MessagePort</code> objects that are entangled with another port and
1320     that have one (but only one) port owned by <var title="">worker global
1321     scope</var>. This list includes all the <code>MessagePort</code> objects
1322     that are in events pending in the <a href="#queue">queue of events</a>, as
1323     well as the implicit <code>MessagePort</code> in the case of <a
1324     href="#dedicatedworkerglobalscope"
1325     title=DedicatedWorkerGlobalScope>dedicated workers</a>.
1326 wakaba 1.16
1327     <hr>
1328    
1329 wakaba 1.23 <p>A worker is said to be a <dfn id=permissible>permissible worker</dfn> if
1330     either:
1331    
1332     <ul>
1333     <li>at some point past or present a <code>MessagePort</code> owned by the
1334     worker was entangled with a <code>MessagePort</code> <var
1335     title="">p</var> whose owner is a <code>Window</code> object whose
1336     <span>active document</span> is the <code>Document</code> that was that
1337     <span>browsing context</span>'s <span>active document</span> when <var
1338     title="">p</var> was created, and that <code>Document</code> is
1339     <span>fully active</span>, or
1340    
1341     <li>at some point past or present a <code>MessagePort</code> owned by the
1342     worker was entangled with a <code>MessagePort</code> owned by another
1343     worker that is currently a <a href="#permissible">permissible worker</a>.
1344     </ul>
1345    
1346     <hr>
1347    
1348     <p>A worker is said to be a <dfn id=protected>protected worker</dfn> if
1349     either:
1350    
1351     <ul>
1352     <li>it has outstanding timers, database transactions, or network
1353     connections, and is a <a href="#permissible">permissible worker</a>, or:
1354    
1355     <li>there is a <a href="#protected">protected worker</a> that at some
1356     point past or present owned a <code>MessagePort</code> that was entangled
1357     with a <code>MessagePort</code> owned by this worker.
1358     </ul>
1359    
1360     <hr>
1361    
1362     <p>A worker is said to be an <dfn id=active>active needed worker</dfn> if
1363     either:
1364    
1365     <ul>
1366     <li>the worker is a <a href="#protected">protected worker</a>, or
1367    
1368     <li>at least one of the <a href="#the-workers0">the worker's ports</a> is
1369     entangled with a <code>MessagePort</code> <var title="">p</var> whose
1370     owner is a <code>Window</code> object whose <span>active document</span>
1371     is the <code>Document</code> that was that <span>browsing
1372     context</span>'s <span>active document</span> when that
1373     <code>MessagePort</code> <var title="">p</var> was created, and that
1374     <code>Document</code> is <span>fully active</span>, or
1375 wakaba 1.22
1376 wakaba 1.23 <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1377 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1378     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1379 wakaba 1.23 itself an <a href="#active">active needed worker</a>.
1380 wakaba 1.22 </ul>
1381    
1382     <hr>
1383    
1384 wakaba 1.23 <p>A worker is said to be a <dfn id=suspendable>suspendable worker</dfn> if
1385     it is not an <a href="#active">active needed worker</a> but either:
1386 wakaba 1.22
1387 wakaba 1.23 <ul>
1388     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1389     an entangled <code>MessagePort</code> owned by a <code>Window</code>
1390     object, or
1391    
1392     <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1393 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1394     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1395 wakaba 1.23 itself a <span>needed worker</span>.
1396 wakaba 1.22 </ul>
1397 wakaba 1.16
1398     <h3 id=processing><span class=ja-translation lang=ja><span class=secno>2.5 </span>処理モデル</span></h3>
1399 wakaba 1.12
1400 wakaba 1.29 <p>When a user agent is to <dfn id=run-a>run a worker</dfn> for a script
1401     with <span>URL</span> <var title="">url</var>, a browsing context <var
1402     title="">owner browsing context</var>, a <code>Document</code> <var
1403     title="">owner document</var>, and with global scope <var title="">worker
1404     global scope</var>, it must run the following steps in a completely
1405     separate and parallel execution environment:
1406    
1407     <ol>
1408 wakaba 1.12 <li>
1409     <p><span class=ja-translation lang=ja><var title="">url</var> で識別される資源を取って来ようと試みます。</span></p>
1410    
1411 wakaba 1.29 <p>If the attempt fails, then, at the next available opportunity, after
1412     any scripts have finished executing<!-- XXX queue -->, <span>fire a
1413     simple event</span> called <code title=event-error>error</code> at all
1414     <code><a href="#worker1">Worker</a></code> or <code><a
1415     href="#sharedworker">SharedWorker</a></code> objects associated with
1416     <var title="">worker global scope</var>. Abort these steps.</p>
1417 wakaba 1.12
1418     <p><span class=ja-translation lang=ja>この試みが成功した場合、 <var title="">script</var>
1419     を得られた資源とします。</span></p>
1420    
1421     <p class=note><span class=ja-translation lang=ja><code>script</code> 要素と同様に、スクリプトの MIME 型は無視します。
1422     <code>script</code> 要素とは異なり、
1423     型を上書きする方法はありません。スクリプトは常に JavaScript
1424     と仮定します。</span></p>
1425 wakaba 1.11 <!-- XXX people will complain about
1426     this. I guess we might want to examine the MIME type... -->
1427    
1428    
1429     <li>
1430 wakaba 1.23 <p>Create a new <code><a href="#workerlocation">WorkerLocation</a></code>
1431     object for the <code title=dom-WorkerGlobalScope-location><a
1432 wakaba 1.29 href="#location">location</a></code> attribute of <var title="">worker
1433     global scope</var>, representing <var title="">url</var>.</p>
1434 wakaba 1.23
1435     <li>
1436 wakaba 1.29 <p>Let <var title="">script</var>'s <span>script execution context</span>
1437     (and thus also <span>global object</span>) be <var title="">worker
1438     global scope</var>.</p>
1439 wakaba 1.9
1440     <li>
1441 wakaba 1.12 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト閲覧文脈を <var title="">owner browsing context</var>
1442     とします。</span></p>
1443 wakaba 1.9
1444     <li>
1445 wakaba 1.12 <p><span class=ja-translation lang=ja><var title="">script</var> のスクリプト文書文脈を <var title="">owner document</var>
1446     とします。</span></p>
1447 wakaba 1.9
1448     <li>
1449 wakaba 1.29 <p><strong>Closing orphan workers</strong>: Start monitoring the worker
1450     such that as soon as it stops being either an <a href="#active">active
1451     needed worker</a> or a <a href="#suspendable">suspendable worker</a>,
1452     the <var title="">worker global scope</var>'s <a href="#closing"
1453     title=dom-WorkerGlobalScope-closing>closing</a> flag is set to true and
1454     an event named <code title=event-close>close</code>, which uses the
1455 wakaba 1.25 <code>Event</code> object, which does not bubble, and which is not
1456 wakaba 1.29 cancelable, is added it to <var title="">worker global scope</var>'s
1457     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object's
1458     <a href="#queue">queue of events</a>, targetted at the <var
1459     title="">worker global scope</var> itself.</p>
1460    
1461     <li>
1462     <p><strong>Suspending workers</strong>: Start monitoring the worker, such
1463     that whenever the <var title="">worker global scope</var>'s <a
1464     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1465     false and the worker is a <a href="#suspendable">suspendable worker</a>,
1466     the user agent suspends execution of script in that worker until such
1467     time as either the <a href="#closing"
1468     title=dom-WorkerGlobalScope-closing>closing</a> flag switches to true or
1469     the worker stops being a <a href="#suspendable">suspendable worker</a>.</p>
1470    
1471     <li>
1472     <p>Run <var title="">script</var> until it either returns, fails to catch
1473     an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
1474     a worker</a>" or "<span>terminate a worker</span>" algorithms defined
1475     below.</p>
1476 wakaba 1.11
1477 wakaba 1.23 <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
1478     worker</a>" algorithm, then that same algorithm will cause there to only
1479     be a single event in the <a href="#queue">queue of events</a> at the
1480 wakaba 1.29 next step, namely the <code title=message-close>close</code> event. The
1481     "<span>terminate a worker</span>" algorithm removes all the events.</p>
1482 wakaba 1.9
1483     <li>
1484 wakaba 1.22 <p><i>Event loop</i>: Wait until either there is an event in the <a
1485 wakaba 1.29 href="#queue">queue of events</a> associated with <var title="">worker
1486     global scope</var> or the <var title="">worker global scope</var>'s <a
1487     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1488     set to true.</p>
1489 wakaba 1.9
1490     <li>
1491 wakaba 1.29 <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1492     events</a>, if any. The handling of this event or the execution of this
1493     callback might get prematurely aborted by the "<a href="#kill-a">kill a
1494     worker</a>" or "<span>terminate a worker</span>" algorithms defined
1495     below.</p>
1496 wakaba 1.9
1497     <li>
1498 wakaba 1.22 <p>If there are any more events in the <a href="#queue">queue of
1499 wakaba 1.29 events</a> or if the <var title="">worker global scope</var>'s <a
1500     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1501     set to false, then jump back to the step above labeled <i>event
1502     loop</i>.</p>
1503 wakaba 1.9
1504     <li>
1505 wakaba 1.23 <p class=big-issue>timers, intervals, XMLHttpRequests, database
1506     transactions, etc, must be killed; ports must be unentangled</p>
1507 wakaba 1.25
1508     <li>
1509     <p>At the next available opportunity, after any scripts have finished
1510     executing<!-- XXX queue -->, <span>fire a simple event</span> called
1511 wakaba 1.29 <code title=event-close>close</code> at all <code><a
1512     href="#worker1">Worker</a></code> or <code><a
1513     href="#sharedworker">SharedWorker</a></code> objects associated with
1514     this worker.</p>
1515 wakaba 1.9 </ol>
1516    
1517     <hr>
1518    
1519 wakaba 1.29 <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn> it must run
1520     the following steps in parallel with the worker's main loop (the "<a
1521     href="#run-a">run a worker</a>" processing model defined above):
1522    
1523     <ol>
1524 wakaba 1.14 <li>
1525 wakaba 1.22 <p>Create an <code>Event</code> object with the event name <code
1526 wakaba 1.29 title=event-close>close</code>, which does not bubble and is not
1527 wakaba 1.22 cancelable, and add it to the worker's <code><a
1528     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1529     href="#queue">queue of events</a>, targetted at the <code><a
1530     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1531 wakaba 1.23 </li>
1532     <!-- XXX shouldn't add one if closing is
1533 wakaba 1.29 already true, assuming close has already been added to the queue
1534 wakaba 1.23 (?) -->
1535 wakaba 1.22
1536     <li>
1537     <p>Set the worker's <code><a
1538 wakaba 1.29 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1539     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1540     true.
1541 wakaba 1.22
1542     <li>
1543 wakaba 1.23 <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
1544     a worker</a>" processing model defined above immediately starts running
1545 wakaba 1.29 event listeners registered for the <code title=event-close>close</code>
1546     event, this time should not be zero &mdash; the idea is that the <code
1547     title=event-close>close</code> event can be used to clean up when
1548     shutting down unexpectedly.
1549    
1550     <li>
1551     <p>If there are any events in the <a href="#queue">queue of events</a>
1552     other than the <code title=event-close>close</code> event that this
1553     algorithm just added, discard them without dispatching them.
1554    
1555     <li>
1556     <p>If the <code title=event-close>close</code> event that this algorithm
1557     just added hasn't yet been dispatched, then abort the script currently
1558     running in the worker.
1559 wakaba 1.23
1560     <li>
1561 wakaba 1.14 <p><span class=ja-translation lang=ja>利用者エージェント定義の時間、待ちます。</span><li>
1562 wakaba 1.29 <p>Abort the script currently running in the worker (if any script is
1563     running, then it will be a handler for the <code
1564     title=event-close>close</code> event).
1565     </ol>
1566 wakaba 1.9
1567 wakaba 1.22 <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
1568     processing model on a worker at any time, e.g. in response to user
1569     requests, in response to CPU quota management, or when a worker stops
1570 wakaba 1.23 being an <a href="#active">active needed worker</a> if the worker
1571 wakaba 1.29 continues executing even after its <a href="#closing"
1572     title=dom-WorkerGlobalScope-closing>closing</a> flag was set to true.
1573 wakaba 1.16
1574 wakaba 1.11 <hr>
1575    
1576 wakaba 1.29 <p>When a user agent is to <dfn id=immediately>immediately terminate a
1577     worker</dfn> it must run the following steps in parallel with the worker's
1578     main loop (the "<a href="#run-a">run a worker</a>" processing model
1579     defined above):
1580 wakaba 1.22
1581     <ol>
1582     <li>
1583 wakaba 1.29 <p>Set the worker's <code><a
1584 wakaba 1.22 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1585 wakaba 1.29 href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1586     true.
1587 wakaba 1.22
1588     <li>
1589 wakaba 1.29 <p>If there are any events in the <a href="#queue">queue of events</a>
1590     other than the <code title=event-close>close</code> event that this
1591     algorithm just added, discard them without dispatching them.
1592 wakaba 1.22
1593     <li>
1594 wakaba 1.29 <p>Abort the script currently running in the worker.
1595 wakaba 1.11 </ol>
1596    
1597 wakaba 1.16 <h3 id=creating><span class=ja-translation lang=ja><span class=secno>2.6 </span>労働者の作成</span></h3>
1598 wakaba 1.9
1599 wakaba 1.29 <h4 id=the-abstractworker><span class=ja-translation lang=ja><span class=secno>2.6.1 </span><code><a href="#abstractworker">AbstractWorker</a></code> abstract 界面</span></h4>
1600    
1601 wakaba 1.9 <pre
1602 wakaba 1.29 class=idl>[NoInterfaceObject] interface <dfn id=abstractworker>AbstractWorker</dfn> {
1603     attribute <span>EventListener</span> <a href="#onerror" title=handler-AbstractWorker-onerror>onerror</a>;
1604     attribute <span>EventListener</span> <a href="#onclose0" title=handler-AbstractWorker-onclose>onclose</a>;
1605     };</pre>
1606    
1607     <p>The following are the <span>event handler DOM attributes</span> that
1608     must be supported by objects implementing the <code><a
1609     href="#abstractworker">AbstractWorker</a></code> interface:
1610    
1611     <dl>
1612     <dt><dfn id=onerror
1613     title=handler-AbstractWorker-onerror><code>onerror</code></dfn>
1614    
1615     <dd>
1616     <p>Must be invoked whenever an <code title=event-error>error</code> event
1617     is targeted at or bubbles through the <code><a
1618     href="#abstractworker">AbstractWorker</a></code> object.
1619    
1620     <dt><dfn id=onclose0
1621     title=handler-AbstractWorker-onclose><code>onclose</code></dfn>
1622    
1623     <dd>
1624     <p>Must be invoked whenever an <code title=event-close>close</code> event
1625     is targeted at or bubbles through the <code><a
1626     href="#abstractworker">AbstractWorker</a></code> object.
1627     </dl>
1628 wakaba 1.25
1629 wakaba 1.29 <h4 id=dedicated0><span class=secno>2.6.2 </span>Dedicated workers and the
1630     <code><a href="#worker1">Worker</a></code> interface</h4>
1631 wakaba 1.25
1632 wakaba 1.29 <pre class=idl>[NoInterfaceObject,
1633     <a href="#worker2" title=dom-Worker>Constructor</a>(in DOMString scriptURL)]
1634     interface <dfn id=worker1>Worker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1635     void <a href="#close0" title=dom-Worker-close>close</a>();
1636    
1637     boolean <a href="#postmessage0" title=dom-Worker-postMessage>postMessage</a>(in DOMString message);
1638     boolean <a href="#postmessage0" title=dom-Worker-postMessage>postMessage</a>(in DOMString message, in <span>MessagePort</span> messagePort);
1639     <span>MessagePort</span> <a href="#startconversation0" title=dom-Worker-startConversation>startConversation</a>(in DOMString message);
1640     attribute <span>EventListener</span> <a href="#onmessage0" title=handler-Worker-onmessage>onmessage</a>;
1641 wakaba 1.9 };</pre>
1642    
1643 wakaba 1.29 <p>The <dfn id=close0 title=dom-Worker-close><code>close()</code></dfn>
1644     method, when invoked, must cause the "<span>terminate a worker</span>"
1645     algorithm to be run on the worker with with the object is associated.
1646    
1647     <p><code><a href="#worker1">Worker</a></code> objects act as if they had an
1648     implicit <code>MessagePort</code> associated with them. This port is part
1649     of a channel that is set up when the worker is created, but it is not
1650     exposed. This object must never be garbage collected before the <code><a
1651     href="#worker1">Worker</a></code> object.
1652    
1653     <p>All messages received by that port must immediately be retargetted at
1654     the <code><a href="#worker1">Worker</a></code> object.
1655    
1656     <p>The <dfn id=postmessage0
1657     title=dom-Worker-postMessage><code>postMessage()</code></dfn> and <dfn
1658     id=startconversation0
1659     title=dom-Worker-startConversation><code>startConversation()</code></dfn>
1660     methods on <code><a href="#worker1">Worker</a></code> objects must act as
1661     if, when invoked, they immediately invoked the method of the same name on
1662     the port, with the same arguments.
1663    
1664     <p>The following are the <span>event handler DOM attributes</span> that
1665     must be supported by objects implementing the <code><a
1666     href="#worker1">Worker</a></code> interface:
1667    
1668     <dl>
1669     <dt><dfn id=onmessage0
1670     title=handler-Worker-onmessage><code>onmessage</code></dfn>
1671    
1672     <dd>
1673     <p><span class=ja-translation lang=ja><code title=event-Worker-message>message</code> 事象が当該 <code><a href="#worker1">Worker</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1674     呼び出されなければなりません。</span></dl>
1675 wakaba 1.22
1676     <hr>
1677 wakaba 1.13
1678 wakaba 1.29 <p>When the <dfn id=worker2 title=dom-Worker><code>Worker(<var
1679     title="">scriptURL</var>)</code></dfn> constructor is invoked, the user
1680     agent must run the following steps:
1681    
1682     <ol>
1683 wakaba 1.13 <li>
1684 wakaba 1.15 <p><span class=ja-translation lang=ja><var
1685     title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1686     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1687     例外を投げます。</span><li>
1688 wakaba 1.29 <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1689     is not the <span title="same origin">same</span> as the origin of the
1690     script that invoked the constructor, then throw a <span>security
1691     exception</span>.
1692    
1693     <li>
1694     <p><span>Create a new <code><a
1695     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1696     object</span>. Let this be the <var title="">worker global scope</var>.
1697    
1698     <li>
1699     <p>Create a new <code><a href="#worker1">Worker</a></code> object,
1700     associated with <var title="">worker global scope</var>. Let this
1701     <code><a href="#worker1">Worker</a></code> object be <var
1702     title="">worker</var>.
1703    
1704     <li>
1705     <p><span>Create a <code>MessagePort</code> object</span> owned by the
1706     <span>script execution context</span> of the script that invoked the
1707     method. Let this be the <var title="">outside port</var>.
1708    
1709     <li>
1710     <p>Associate the <var title="">outside port</var> with <var
1711     title="">worker</var>.
1712    
1713     <li>
1714     <p><span>Create a <code>MessagePort</code> object</span> owned by the
1715     <var title="">worker global scope</var>. Let this be the <var
1716     title="">inside port</var>.
1717    
1718     <li>
1719     <p>Associate the <var title="">inside port</var> with <var
1720     title="">worker global scope</var>.
1721    
1722     <li>
1723     <p><span>Entangle</span> <var title="">outside port</var> and <var
1724     title="">inside port</var>.
1725    
1726     <li>
1727     <p>Return <var title="">worker</var>, and run the following steps
1728     asynchronously.
1729    
1730     <li>
1731     <p>Open <var title="">inside port</var>'s <span>port message
1732     queue</span>.
1733    
1734     <li>
1735     <p>In a parallel execution context (i.e. a separate thread or process or
1736     equivalent construct), <a href="#run-a">run a worker</a>, with the
1737     <span>script browsing context</span> of the script that invoked the
1738     method as the <var title="">owner browsing context</var>, with the
1739     <span>script document context</span> of the script that invoked the
1740     method as the <var title="">owner document</var>, and with <var
1741     title="">worker global scope</var> as the global scope.</p>
1742 wakaba 1.25 </ol>
1743 wakaba 1.11
1744 wakaba 1.29 <h4 id=shared1><span class=secno>2.6.3 </span>Shared workers and the
1745     <code><a href="#sharedworker">SharedWorker</a></code> interface</h4>
1746    
1747     <pre class=idl>[NoInterfaceObject,
1748     <a href="#sharedworker0" title=dom-SharedWorker>Constructor</a>(in DOMString scriptURL, in DOMString name)]
1749     interface <dfn id=sharedworker>SharedWorker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1750     readonly attribute <code>MessagePort</code> <a href="#port" title=dom-SharedWorker-port>port</a>;
1751     };</pre>
1752    
1753     <p>The <dfn id=port title=dom-SharedWorker-port><code>port</code></dfn>
1754     attribute must return the value it was assigned by the object's
1755     constructor. It represents the <code>MessagePort</code> for communicating
1756     with the shared worker.
1757    
1758     <p>When the <dfn id=sharedworker0
1759     title=dom-SharedWorker><code>SharedWorker(<var title="">scriptURL</var>,
1760     <var title="">name</var>)</code></dfn> constructor is invoked, the user
1761     agent must run the following steps:
1762 wakaba 1.11
1763 wakaba 1.29 <ol>
1764 wakaba 1.13 <li>
1765 wakaba 1.15 <p><span class=ja-translation lang=ja><var
1766     title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1767     <p><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1768     例外を投げます。</span><li>
1769 wakaba 1.29 <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
1770     is not the <span title="same origin">same</span> as the origin of the
1771     script that invoked the constructor, then throw a <span>security
1772     exception</span>.
1773    
1774     <li>
1775     <p>Execute the following substeps atomically:</p>
1776 wakaba 1.13
1777     <ol>
1778     <li>
1779 wakaba 1.29 <p>Create a new <code><a href="#sharedworker">SharedWorker</a></code>
1780     object, which will shortly be associated with a <code><a
1781     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1782     object. Let this <code><a href="#sharedworker">SharedWorker</a></code>
1783     object be <var title="">worker</var>.
1784 wakaba 1.22
1785     <li>
1786 wakaba 1.29 <p><span>Create a <code>MessagePort</code> object</span> owned by the
1787     <span>script execution context</span> of the script that invoked the
1788     method. Let this be the <var title="">outside port</var>.
1789 wakaba 1.25
1790     <li>
1791 wakaba 1.29 <p>Assign <var title="">outside port</var> to the <code
1792     title=dom-SharedWorker-port><a href="#port">port</a></code> attribute
1793     of <var title="">worker</var>.
1794 wakaba 1.25
1795     <li>
1796 wakaba 1.29 <p>If there exists a <code><a
1797     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1798     object whose <a href="#closing"
1799     title=dom-WorkerGlobalScope-closing>closing</a> flag is false, whose
1800     <code title=dom-WorkerGlobalScope-name>name</code> attribute is
1801     exactly equal to the <var title="">name</var> argument, and whose
1802     <code title=dom-WorkerGlobalScope-location><a
1803     href="#location">location</a></code> attribute represents an
1804     <span>absolute URL</span> that has the <span>same origin</span> as the
1805     resulting <span>absolute URL</span>, then run these substeps:</p>
1806 wakaba 1.25
1807 wakaba 1.29 <ol>
1808     <li>
1809     <p>Let <var title="">worker global scope</var> be that <code><a
1810     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1811     object.
1812    
1813     <li>
1814     <p>If <var title="">worker global scope</var>'s <code
1815     title=dom-WorkerGlobalScope-location><a
1816     href="#location">location</a></code> attribute represents an
1817     <span>absolute URL</span> that is not exactly equal to the resulting
1818     <span>absolute URL</span>, then throw a
1819     <code>URL_MISMATCH_ERR</code> exception and abort all these steps.
1820     <span class=big-issue>code 21</span>
1821    
1822     <li>
1823     <p>Associate <var title="">worker</var> with <var title="">worker
1824     global scope</var>.
1825    
1826     <li>
1827     <p><span>Create a <code>MessagePort</code> object</span> owned by the
1828     <var title="">worker global scope</var>. Let this be the <var
1829     title="">inside port</var>.
1830    
1831     <li>
1832     <p><span>Entangle</span> <var title="">outside port</var> and <var
1833     title="">inside port</var>.
1834    
1835     <li>
1836     <p>Return <var title="">worker</var> and perform the next step
1837     asynchronously.
1838    
1839     <li>
1840     <p>Create an event that uses the <code>MessageEvent</code> interface,
1841     with the name <code title=event-connect>connect</code>, which does
1842     not bubble, is cancelable, has no default action, has a <code
1843     title=dom-MessageEvent-data>data</code> attribute whose value is the
1844     empty string and has a <code
1845     title=dom-MessageEvent-messagePort>messagePort</code> attribute
1846     whose value is the newly created port, and add it to <var
1847     title="">worker global scope</var>'s <a href="#queue">queue of
1848     events</a>, targetted at the <var title="">worker global scope</var>
1849     itself.
1850    
1851     <li>
1852     <p>Abort all these steps.
1853     </ol>
1854 wakaba 1.11
1855 wakaba 1.29 <li>
1856     <p><span>Create a new <code><a
1857     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1858     object</span>. Let this be the <var title="">worker global
1859     scope</var>.
1860 wakaba 1.11
1861 wakaba 1.29 <li>
1862     <p>Associate <var title="">worker</var> with <var title="">worker
1863     global scope</var>.
1864 wakaba 1.23
1865 wakaba 1.29 <li>
1866     <p>Set the <code title=dom-SharedWorkerGlobalScope-name><a
1867     href="#name">name</a></code> attribute of <var title="">worker global
1868     scope</var> be <var title="">name</var>.
1869 wakaba 1.11
1870 wakaba 1.29 <li>
1871     <p><span>Create a <code>MessagePort</code> object</span> owned by the
1872     <var title="">worker global scope</var>. Let this be the <var
1873     title="">inside port</var>.
1874 wakaba 1.11
1875 wakaba 1.29 <li>
1876     <p><span>Entangle</span> <var title="">outside port</var> and <var
1877     title="">inside port</var>.
1878     </ol>
1879 wakaba 1.13
1880     <li>
1881 wakaba 1.29 <p>Return <var title="">worker</var> and perform the next step
1882     asynchronously.
1883 wakaba 1.25
1884     <li>
1885 wakaba 1.22 <p>Create an event that uses the <code>MessageEvent</code> interface,
1886     with the name <code title=event-connect>connect</code>, which does not
1887     bubble, is cancelable, has no default action, has a <code
1888     title=dom-MessageEvent-data>data</code> attribute whose value is the
1889     empty string and has a <code
1890     title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
1891 wakaba 1.29 value is the newly created port, and add it to <var title="">worker
1892     global scope</var>'s <a href="#queue">queue of events</a>, targetted at
1893     the <var title="">worker global scope</var> itself.
1894 wakaba 1.23
1895     <li>
1896 wakaba 1.29 <p>In a parallel execution context (i.e. a separate thread or process or
1897     equivalent construct), <a href="#run-a">run a worker</a>, with the
1898     <span>script browsing context</span> of the script that invoked the
1899     method as the <var title="">owner browsing context</var>, with the
1900     <span>script document context</span> of the script that invoked the
1901     method as the <var title="">owner document</var>, and with <var
1902     title="">worker global scope</var> as the global scope.</p>
1903 wakaba 1.22 </ol>
1904 wakaba 1.15
1905     <h2 id=apis-available><span class=ja-translation lang=ja><span class=secno>3. </span>労働者が利用可能な API</span></h2>
1906    
1907 wakaba 1.22 <pre
1908     class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1909 wakaba 1.25 void <a href="#importscripts" title=dom-WorkerGlobalScope-importScripts>importScripts</a>([Variadic] in DOMString urls);
1910 wakaba 1.22 readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
1911     <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
1912     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
1913     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
1914     };</pre>
1915 wakaba 1.16
1916 wakaba 1.25 <p><span class=ja-translation lang=ja><code><a href="#workerglobalscope">WorkerGlobalScope</a></code> 界面を実装するオブジェクトは、 <code><a href="#workerutils">WorkerUtils</a></code> 界面も実装しなければ<em class=rfc2119 title="MUST">なりません</em>。</span><p class=big-issue>Need to define a sync database API.
1917 wakaba 1.16
1918     <p class=big-issue>May need to define a browser sniffing API (like
1919     window.navigator).</p>
1920     <!-- XXX ApplicationCache -->
1921     <!-- XXX a way to set cookies on the URL for the script -->
1922     <!-- XXX debugging: void log(in DOMString s); // log to console -->
1923     <!-- XXX debugging: onerror -->
1924    
1925     <hr>
1926    
1927 wakaba 1.19 <p><span class=ja-translation lang=ja>DOM API (<code>Node</code> オブジェクト、 <code>Document</code>
1928 wakaba 1.22 オブジェクト、その他) は、この版のこの仕様書では労働者に利用可能ではありません。</span><h3 id=importing><span class=secno>3.1 </span>Importing scripts and
1929     libraries</h3>
1930    
1931 wakaba 1.25 <p>When a script invokes the <dfn id=importscripts
1932     title=dom-WorkerGlobalScope-importScripts><code>importScripts(<var
1933     title="">urls</var>)</code></dfn> method on a <code><a
1934 wakaba 1.22 href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
1935     agent must run the following steps:
1936    
1937     <ol>
1938     <li>
1939 wakaba 1.25 <p>If there are no arguments, return without doing anything. Abort these
1940     steps.
1941    
1942     <li>
1943     <p><span title="resolve a url">Resolve</span> each argument.
1944 wakaba 1.22
1945 wakaba 1.25 <li>
1946     <p>If any fail, throw a <code>SYNTAX_ERR</code> exception.
1947 wakaba 1.22
1948 wakaba 1.25 <li>
1949     <p>If any of the resulting <span title="absolute URL">absolute
1950     URLs</span> have an <span>origin</span> that is not the <span
1951     title="same origin">same</span> as the origin of the script that invoked
1952     the method, then throw a <span>security exception</span>.
1953 wakaba 1.22
1954 wakaba 1.25 <li>
1955     <p>Attempt to <span>fetch</span> each resource identified by the
1956     resulting <span title="absolute URLs">absolute URL</span>.</p>
1957 wakaba 1.22
1958     <li>
1959 wakaba 1.25 <p>For each argument in turn, in the order given, starting with the first
1960     one, run these substeps:</p>
1961    
1962     <ol>
1963     <li>
1964     <p>Wait for the fetching attempt for the corresponding resource to
1965     complete.</p>
1966    
1967     <p>If the fetching attempt failed, throw a <code>NETWORK_ERR</code>
1968 wakaba 1.26 exception and abort all these steps. <a
1969     href="#refsXHR">[XHR]</a><!-- XXX get DOM Core updated instead
1970     --></p>
1971 wakaba 1.25
1972     <p>If the fetching attempt succeeded, then let <var
1973     title="">script</var> be the resource that was obtained.</p>
1974    
1975     <p class=note>As with the worker's script, the script here is always
1976     assumed to be JavaScript, regardless of the MIME type.</p>
1977     <!-- XXX -->
1978    
1979     <li>
1980     <p><span class=ja-translation lang=ja><var title="">script</var> の<span>スクリプト実行文脈</span>、
1981 wakaba 1.22 <span>スクリプト閲覧文脈</span>、<span>スクリプト文書文脈</span>を、
1982     この労働者についての<a href="#run-a">労働者を走らせる</a>処理モデルで実行されたスクリプトについてのものと同じとします。</span></p>
1983    
1984 wakaba 1.25 <li>
1985     <p>Run <var title="">script</var> until it either returns, fails to
1986     catch an exception, or gets prematurely aborted by the "<a
1987 wakaba 1.29 href="#kill-a">kill a worker</a>" or "<span>terminate a worker</span>"
1988     algorithms defined above.</p>
1989 wakaba 1.25
1990     <p>If an exception was raised or if the script was prematurely aborted,
1991     then abort all these steps, letting the exception or aborting continue
1992     to be processed by the script that called the <code
1993     title=dom-WorkerGlobalScope-importScripts><a
1994     href="#importscripts">importScripts()</a></code> method.</p>
1995 wakaba 1.22
1996 wakaba 1.29 <p>If the "<a href="#kill-a">kill a worker</a>" or "<span>terminate a
1997     worker</span>" algoriths abort the script then abort all these steps.</p>
1998 wakaba 1.25 </ol>
1999 wakaba 1.22 </ol>
2000    
2001     <h3 id=apis-defined><span class=secno>3.2 </span>APIs defined in other
2002     specifications</h3>
2003    
2004     <p>The <dfn id=localstorage
2005     title=dom-localStorage><code>localStorage</code></dfn>, <dfn
2006     id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
2007     must act as defined for the APIs with the same names on the
2008     <code>Window</code> object in the HTML5 specification, with the exception
2009     that where the API would use the <span>origin</span> of the <span>active
2010     document</span> of the <span>browsing context</span> of the
2011     <code>Window</code> object on which the method was supposedly invoked, it
2012     must instead use the <span>origin</span> of the script that invoked the
2013     method. <a href="#refsHTML5">[HTML5]</a>
2014    
2015     <p>The <dfn id=shownotification
2016     title=dom-showNotification><code>showNotification()</code></dfn> methods
2017     must act as defined for the APIs with the same names on the
2018     <code>Window</code> object in the HTML5 specification. <a
2019     href="#refsHTML5">[HTML5]</a>
2020    
2021     <h3 id=interface><span class=secno>3.3 </span>Interface objects and
2022     constructors</h3>
2023    
2024     <p>There must be no interface objects and constructors available in the
2025     global scope of scripts whose <span>script execution context</span> is a
2026     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
2027     except for the following:
2028    
2029     <ul>
2030     <li>
2031     <p><span class=ja-translation lang=ja>XMLHttpRequest 仕様書で定義されている
2032     <code>XMLHttpRequest</code> とすべての界面オブジェクトと構築器、
2033     ただし<span>文書応答実体本体</span>は常に null でなければ<em class=rfc2119 title="MUST">なりません</em>。 <a
2034     href="#refsXHR">[XHR]</a></span><li>
2035     <p><span class=ja-translation lang=ja><code>WebSocket</code> 界面オブジェクトと構築器</span><li>
2036     <p><span class=ja-translation lang=ja><code>MessageChannel</code> 界面オブジェクトと構築器</span></ul>
2037 wakaba 1.23
2038     <h3 id=worker0><span class=secno>3.4 </span>Worker locations</h3>
2039    
2040     <pre
2041     class=idl>[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
2042     readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
2043     readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
2044     readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
2045     readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
2046 wakaba 1.29 readonly attribute DOMString <a href="#port0" title=dom-WorkerLocation-port>port</a>;
2047 wakaba 1.23 readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
2048     readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
2049     readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
2050     };</pre>
2051    
2052     <p>A <code><a href="#workerlocation">WorkerLocation</a></code> object
2053     represents an <span>absolute URL</span> set at its creation.
2054    
2055     <p>The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
2056     attribute must return the <span>absolute URL</span> that the object
2057     represents.
2058    
2059     <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface
2060     also has the complement of <span>URL decomposition attributes</span>, <dfn
2061     id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
2062     <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
2063 wakaba 1.29 id=port0 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
2064 wakaba 1.23 id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
2065     <dfn id=pathname
2066     title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
2067     id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
2068     <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
2069     must follow the rules given for URL decomposition attributes, with the
2070     <span title=concept-uda-input>input</span> being the <span>absolute
2071     URL</span> that the object represents (same as the <code
2072     title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
2073     and the <span title=concept-uda-setter>common setter action</span> being a
2074     no-op, since the attributes are defined to be readonly. <a
2075     href="#refsHTML5">[HTML5]</a>
2076 wakaba 1.22
2077     <h2 class=no-num id=references><span class=ja-translation lang=ja>参考文献</span></h2>
2078 wakaba 1.1
2079     <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>
2080     <!-- ACKS -->
2081    
2082 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 チームの方々にも非常に感謝します。
2083 wakaba 1.14 Gears チームの方々はこの技術を開拓し、その経験はこの仕様書にも多大な影響を与えています。
2084     </span>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24