/[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.33 - (hide annotations) (download)
Sat Oct 25 06:33:19 2008 UTC (17 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.32: +4 -4 lines
Support for en/ja view

1 wakaba 1.1
2     <html lang=en-US-x-hixie>
3     <head>
4     <title>Web Workers</title>
5 wakaba 1.32 <link href=http://www.whatwg.org/style/specification rel=stylesheet>
6     <link href=ja-style rel=stylesheet>
7     <link href=only-ja-style rel="stylesheet" title="Show only Japanese text" id=style-only-ja>
8     <link href=both-en-ja-style rel="alternate stylesheet" title="Show both English and Japanese texts" id=style-both-en-ja>
9 wakaba 1.31 <link href="/images/icon" rel=icon>
10 wakaba 1.1
11 wakaba 1.16 <style>
12     p > span:not([title=""]):not([class="big-issue"]), li > span:not([title=""]):not([class="big-issue"]) { border-bottom: solid #99CC99; }
13     </style>
14    
15 wakaba 1.1 <body class=draft>
16     <div class=head>
17 wakaba 1.32 <p class="no-ja-translation"><a class=logo href="http://www.whatwg.org/" rel=home><img alt=WHATWG
18 wakaba 1.31 src="/images/logo"></a></p>
19 wakaba 1.1
20 wakaba 1.32 <h1 id=web-workers class="no-ja-translation">Web Workers</h1>
21 wakaba 1.1
22 wakaba 1.32 <h2 class="no-num no-toc has-ja-translation" id=draft><span class=en-original lang=en>Draft Recommendation &mdash; 27 August
23     2008</span><span class=ja-translation lang=ja>勧告案<ins class=ja-note>の非公式和訳</ins> &mdash;
24 wakaba 1.29 <ins class=ja-note>(原文出版日)</ins> 2008年8月27日</span></h2>
25 wakaba 1.1
26 wakaba 1.32 <p class="no-ja-translation">You can take part in this work. <a
27 wakaba 1.31 href="http://www.whatwg.org/mailing-list">Join the working group's
28     discussion list.</a></p>
29    
30 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><strong>Web designers!</strong> We have a <a
31 wakaba 1.31 href="http://blog.whatwg.org/faq/">FAQ</a>, a <a
32     href="http://forums.whatwg.org/">forum</a>, and a <a
33     href="http://www.whatwg.org/mailing-list#help">help mailing list</a> for
34 wakaba 1.33 you!</span><span class=ja-translation lang=ja><strong>Web デザイナーのみなさん!</strong> みなさんのための <a
35 wakaba 1.32 href=http://blog.whatwg.org/faq/ lang=en>FAQ</a>、<a
36     href=http://forums.whatwg.org/>掲示板</a>、<a
37     href=http://www.whatwg.org/mailing-list#help>ヘルプ用メイリング・リスト</a>があります!
38    
39     </span><p>
40    
41     <!-- Japanese-translation-only notes -->
42 wakaba 1.33 <ins class="ja-note cover-ja-note" lang=ja>
43 wakaba 1.32 <p>この文書は、 WHATWG 仕様書の日本語訳です。
44     正式な仕様書は英語版だけであり、
45     この日本語訳はあくまで参考としてご利用ください。
46    
47     <p lang=en>This document is a Japanese translation of
48     the WHATWG HTML5 specification. Note that the only normative specification
49     for the HTML5 is the original English version and this Japanese
50     translation is non‐normative.
51    
52     <p>日本語訳は <a href="edit/" rel=edit>Web 界面</a>を使って編集できます。
53     編集結果はおよそ3時間おきに反映されます。
54     また、原文の変更には毎日自動的に追随しています。
55    
56     <p lang=en>The Japanese text can be edited using the <a href=edit/ rel=edit>Web
57     interface</a>. Any modifications will be reflected to this translation
58     in about three hours. Note that any changes to the original specification
59     will automatically be incorporated to this translation every day.
60    
61     <p><button type=button onclick="
62     document.getElementById ('style-only-ja').disabled = false;
63     document.getElementById ('style-both-en-ja').disabled = true;
64 wakaba 1.33 ">日本語訳だけ表示 <span lang=en>Show only Japanese translation</span></button>
65 wakaba 1.32 <p><button type=button onclick="
66     document.getElementById ('style-only-ja').disabled = true;
67     document.getElementById ('style-both-en-ja').disabled = false;
68 wakaba 1.33 ">英和対訳表示 <span lang=en>Show both English and Japanese texts</span></button>
69 wakaba 1.32 </ins>
70    
71     <p><span><!-- dummy --></span></p>
72 wakaba 1.1
73     <dl>
74 wakaba 1.32 <dt class="has-ja-translation"><span class=en-original lang=en>This version:
75    
76     </span><span class=ja-translation lang=ja><ins class=ja-note>原文の</ins>この版</span><dd class="no-ja-translation"><a
77 wakaba 1.13 href="http://www.whatwg.org/specs/web-workers/current-work/">http://whatwg.org/ww</a>
78 wakaba 1.1
79 wakaba 1.32 <dt class="has-ja-translation"><span class=en-original lang=en>Version history:
80    
81     </span><span class=ja-translation lang=ja><ins class=ja-note>原文の</ins>改版履歴:</span><dd class="no-ja-translation">Twitter messages (non-editorial changes only): <a
82 wakaba 1.31 href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a>
83    
84 wakaba 1.32 <dd class="no-ja-translation">Commit-Watchers mailing list: <a
85 wakaba 1.31 href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a>
86    
87 wakaba 1.32 <dd class="has-ja-translation"><span class=en-original lang=en>Interactive Web interface: <a
88     href="http://html5.org/tools/web-workers-tracker">http://html5.org/tools/web-workers-tracker</a>
89    
90     </span><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 class="has-ja-translation"><span class=en-original lang=en>Subversion interface: <a
91     href="http://svn.whatwg.org/webworkers/">http://svn.whatwg.org/webworkers/</a>
92    
93     </span><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 class="has-ja-translation"><span class=en-original lang=en>Issues:
94    
95     </span><span class=ja-translation lang=ja>問題:</span><dd class="no-ja-translation">To send feedback: <a
96 wakaba 1.31 href="http://www.whatwg.org/mailing-list">[email protected]</a>
97    
98 wakaba 1.32 <dd class="no-ja-translation">To view and vote on feedback: <a
99 wakaba 1.31 href="http://www.whatwg.org/issues/">http://www.whatwg.org/issues/</a>
100    
101 wakaba 1.32 <dt class="has-ja-translation"><span class=en-original lang=en>Editor:
102    
103     </span><span class=ja-translation lang=ja>編集者:</span><dd class="no-ja-translation">Ian Hickson, Google, [email protected]
104 wakaba 1.1 </dl>
105    
106 wakaba 1.32 <p class="copyright no-ja-translation">&copy; Copyright 2004-2008 Apple Computer, Inc.,
107 wakaba 1.1 Mozilla Foundation, and Opera Software ASA.</p>
108    
109 wakaba 1.32 <p class="copyright no-ja-translation">You are granted a license to use, reproduce and create
110 wakaba 1.1 derivative works of this document.</p>
111     </div>
112    
113     <hr>
114    
115 wakaba 1.32 <h2 class="no-num no-toc has-ja-translation" id=abstract><span class=en-original lang=en>Abstract</span><span class=ja-translation lang=ja>概要</span></h2>
116 wakaba 1.1
117 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>This specification defines an API that allows Web application authors to
118     spawn background workers running scripts in parallel to their main page.
119     This allows for thread-like operation with message-passing as the
120     coordination mechanism.
121    
122     </span><span class=ja-translation lang=ja>この仕様書は、 Web アプリケーションで、主たる頁とは並列に、
123 wakaba 1.14 背景労働者スクリプトを走らせるための API を定義します。
124 wakaba 1.32 これにより、メッセージ・パッシングによるスレッド的操作を協調機構として利用できるようになります。</span><h2 class="no-num no-toc has-ja-translation" id=status><span class=en-original lang=en>Status of this document</span><span class=ja-translation lang=ja>この文書の状態</span></h2>
125 wakaba 1.1
126 wakaba 1.32 <p class="no-ja-translation"><strong>This is a work in progress!</strong> This document is changing
127 wakaba 1.31 on a daily if not hourly basis in response to comments and as a general
128     part of its development process. Comments are very welcome, please send
129     them to <a href="mailto:[email protected]">[email protected]</a>. Thank
130     you.
131    
132 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The current focus is in developing a first draft proposal.
133    
134     </span><span class=ja-translation lang=ja>現在の焦点は、第1案の開発です。</span><p class="no-ja-translation">Implementors should be aware that this specification is not stable.
135 wakaba 1.31 <strong>Implementors who are not taking part in the discussions are likely
136     to find the specification changing out from under them in incompatible
137     ways.</strong> Vendors interested in implementing this specification
138     before it eventually reaches the call for implementations should join the
139     <a href="/mailing-list">WHATWG mailing list</a> and take part in the
140     discussions.
141    
142 wakaba 1.32 <p class="no-ja-translation">This specification is also being produced by the <a
143 wakaba 1.31 href="http://www.w3.org/html/wg">W3C HTML WG</a>. The two specifications
144     are identical from the table of contents onwards.
145    
146 wakaba 1.32 <h2 class="no-num no-toc has-ja-translation" id=contents><span class=en-original lang=en>Table of contents</span><span class=ja-translation lang=ja>目次
147 wakaba 1.1 </span></h2>
148     <!--begin-toc-->
149    
150     <ul class=toc>
151 wakaba 1.32 <li class="no-ja-translation"><a href="#introduction"><span class=secno>1. </span>Introduction</a>
152 wakaba 1.1 <ul class=toc>
153 wakaba 1.32 <li class="no-ja-translation"><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a>
154 wakaba 1.23 <ul class=toc>
155 wakaba 1.32 <li class="no-ja-translation"><a href="#a-background"><span class=secno>1.1.1 </span>A
156 wakaba 1.23 background number-crunching worker</a>
157    
158 wakaba 1.32 <li class="no-ja-translation"><a href="#a-worker"><span class=secno>1.1.2 </span>A worker for
159 wakaba 1.23 updating a client-side database</a>
160    
161 wakaba 1.32 <li class="no-ja-translation"><a href="#worker"><span class=secno>1.1.3 </span>Worker used for
162 wakaba 1.23 backgroud I/O</a>
163    
164 wakaba 1.32 <li class="no-ja-translation"><a href="#shared"><span class=secno>1.1.4 </span>Shared
165 wakaba 1.23 workers</a>
166    
167 wakaba 1.32 <li class="no-ja-translation"><a href="#delegation"><span class=secno>1.1.5
168 wakaba 1.23 </span>Delegation</a>
169    
170 wakaba 1.32 <li class="no-ja-translation"><a href="#providing"><span class=secno>1.1.6 </span>Providing
171 wakaba 1.28 libraries</a>
172 wakaba 1.23 </ul>
173 wakaba 1.1
174 wakaba 1.32 <li class="no-ja-translation"><a href="#conformance"><span class=secno>1.2 </span>Conformance
175 wakaba 1.1 requirements</a>
176     <ul class=toc>
177 wakaba 1.32 <li class="no-ja-translation"><a href="#dependencies"><span class=secno>1.2.1
178 wakaba 1.1 </span>Dependencies</a>
179     </ul>
180    
181 wakaba 1.32 <li class="no-ja-translation"><a href="#terminology"><span class=secno>1.3 </span>Terminology</a>
182 wakaba 1.9 </ul>
183    
184 wakaba 1.32 <li class="no-ja-translation"><a href="#infrastructure"><span class=secno>2.
185 wakaba 1.13 </span>Infrastructure</a>
186 wakaba 1.9 <ul class=toc>
187 wakaba 1.32 <li class="no-ja-translation"><a href="#the-global"><span class=secno>2.1 </span>The global
188 wakaba 1.29 scope</a>
189     <ul class=toc>
190 wakaba 1.32 <li class="no-ja-translation"><a href="#the-workerglobalscope"><span class=secno>2.1.1
191 wakaba 1.29 </span>The <code>WorkerGlobalScope</code> abstract interface</a>
192    
193 wakaba 1.32 <li class="no-ja-translation"><a href="#dedicated"><span class=secno>2.1.2 </span>Dedicated
194 wakaba 1.29 workers and the <code>DedicatedWorkerGlobalScope</code> interface</a>
195    
196    
197 wakaba 1.32 <li class="no-ja-translation"><a href="#shared0"><span class=secno>2.1.3 </span>Shared workers
198 wakaba 1.29 and the <code>SharedWorkerGlobalScope</code> inteface</a>
199     </ul>
200 wakaba 1.9
201 wakaba 1.32 <li class="no-ja-translation"><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
202 wakaba 1.16 origins of workers</a>
203    
204 wakaba 1.32 <li class="no-ja-translation"><a href="#the-queue"><span class=secno>2.3 </span>The queue of
205 wakaba 1.9 events</a>
206    
207 wakaba 1.32 <li class="no-ja-translation"><a href="#the-workers"><span class=secno>2.4 </span>The worker's
208 wakaba 1.11 ports</a>
209    
210 wakaba 1.32 <li class="no-ja-translation"><a href="#processing"><span class=secno>2.5 </span>Processing
211 wakaba 1.9 model</a>
212 wakaba 1.13
213 wakaba 1.32 <li class="no-ja-translation"><a href="#creating"><span class=secno>2.6 </span>Creating
214 wakaba 1.13 workers</a>
215 wakaba 1.29 <ul class=toc>
216 wakaba 1.32 <li class="no-ja-translation"><a href="#the-abstractworker"><span class=secno>2.6.1 </span>The
217 wakaba 1.29 <code>AbstractWorker</code> abstract interface</a>
218    
219 wakaba 1.32 <li class="no-ja-translation"><a href="#dedicated0"><span class=secno>2.6.2 </span>Dedicated
220 wakaba 1.29 workers and the <code>Worker</code> interface</a>
221    
222 wakaba 1.32 <li class="no-ja-translation"><a href="#shared1"><span class=secno>2.6.3 </span>Shared workers
223 wakaba 1.29 and the <code>SharedWorker</code> interface</a>
224     </ul>
225 wakaba 1.9 </ul>
226    
227 wakaba 1.32 <li class="no-ja-translation"><a href="#apis-available"><span class=secno>3. </span>APIs available
228 wakaba 1.13 to workers</a>
229 wakaba 1.22 <ul class=toc>
230 wakaba 1.32 <li class="no-ja-translation"><a href="#importing"><span class=secno>3.1 </span>Importing scripts
231 wakaba 1.22 and libraries</a>
232    
233 wakaba 1.32 <li class="no-ja-translation"><a href="#apis-defined"><span class=secno>3.2 </span>APIs defined in
234 wakaba 1.22 other specifications</a>
235    
236 wakaba 1.32 <li class="no-ja-translation"><a href="#interface"><span class=secno>3.3 </span>Interface objects
237 wakaba 1.22 and constructors</a>
238 wakaba 1.23
239 wakaba 1.32 <li class="no-ja-translation"><a href="#worker0"><span class=secno>3.4 </span>Worker locations</a>
240 wakaba 1.23
241 wakaba 1.22 </ul>
242 wakaba 1.1
243 wakaba 1.32 <li class="no-num no-ja-translation"><a href="#references">References</a>
244 wakaba 1.1
245 wakaba 1.32 <li class="no-num no-ja-translation"><a href="#acknowledgements">Acknowledgements</a>
246 wakaba 1.1 </ul>
247     <!--end-toc-->
248    
249     <hr>
250    
251 wakaba 1.32 <h2 id=introduction class="has-ja-translation"><span class=en-original lang=en><span class=secno>1. </span>Introduction</span><span class=ja-translation lang=ja><span class=secno>1. </span>はじめに</span></h2>
252    
253     <h3 id=tutorial class="has-ja-translation"><span class=en-original lang=en><span class=secno>1.1 </span>Tutorial</span><span class=ja-translation lang=ja><span class=secno>1.1 </span>解説</span></h3>
254    
255     <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
256    
257     </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="has-ja-translation"><span class=en-original lang=en>There are a variety of uses that workers can be put to. The following
258     subsections show various examples of this use.
259    
260     </span><span class=ja-translation lang=ja>労働者は色々なことに使えます。次の各節では、色々な使い方の例を示します。</span><h4 id=a-background class="has-ja-translation"><span class=en-original lang=en><span class=secno>1.1.1 </span>A background
261     number-crunching worker</span><span class=ja-translation lang=ja><span class=secno>1.1.1 </span>数値処理背景労働者</span></h4>
262    
263     <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
264 wakaba 1.1
265 wakaba 1.32 </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="has-ja-translation"><span class=en-original lang=en>The simplest use of workers is for performing a computationally
266     expensive task without interrupting the user interface.
267 wakaba 1.1
268 wakaba 1.32 </span><span class=ja-translation lang=ja>労働者の一番単純な用法は、計算的に高価な仕事を利用者界面を中断させずに実行することです。</span><p class="has-ja-translation"><span class=en-original lang=en>In this example, the main document spawns a worker to (na&iuml;vely)
269     compute prime numbers, and progressively displays the most recently found
270     prime number.
271 wakaba 1.23
272 wakaba 1.32 </span><span class=ja-translation lang=ja>この例では、主たる文書が労働者を作って素数を (素朴な方法で) 計算させ、
273     新しく見つかった素数を順番に表示してゆきます。</span><p class="has-ja-translation"><span class=en-original lang=en>The main page is as follows:
274    
275     </span><span class=ja-translation lang=ja>主たる頁は次の通りです。</span><pre class="no-ja-translation">&lt;!DOCTYPE HTML>
276 wakaba 1.23 &lt;html>
277     &lt;head>
278     &lt;title>Worker example: One-core computation&lt;/title>
279     &lt;/head>
280     &lt;body>
281     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
282     &lt;script>
283 wakaba 1.29 var worker = new Worker('worker.js');
284     worker.onmessage = function (event) {
285 wakaba 1.23 document.getElementById('result').textContent = event.message;
286     };
287     &lt;/script>
288     &lt;/body>
289     &lt;/html></pre>
290    
291 wakaba 1.32 <p class="no-ja-translation">The <code title=dom-Worker><a href="#worker2">Worker()</a></code>
292 wakaba 1.30 constructor call creates a worker and returns a <code><a
293     href="#worker1">Worker</a></code> object representing that worker, which
294     is used to communicate with the worker. That object's <code
295     title=handler-Worker-onmessage><a href="#onmessage0">onmessage</a></code>
296     event handler attribute allows the code to receive messages from the
297     worker.
298 wakaba 1.23
299 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The worker itself is as follows:
300    
301     </span><span class=ja-translation lang=ja>労働者自体は次の通りです。</span><pre class="no-ja-translation">var n = 1;
302 wakaba 1.23 search: while (true) {
303     n += 1;
304     for (var i = 2; i &lt;= Math.sqrt(n); i += 1)
305     if (n % i == 0)
306     continue search;
307     // found a prime!
308 wakaba 1.29 postMessage(n);
309 wakaba 1.23 }</pre>
310    
311 wakaba 1.32 <p class="no-ja-translation">The bulk of this code is simply an unoptimised search for a prime
312 wakaba 1.23 number. To send a message back to the page, the <code
313 wakaba 1.30 title=dom-DedicatedWorkerGlobalScope-postMessage><a
314     href="#postmessage">postMessage()</a></code> method is used to post a
315     message when a prime is found.
316 wakaba 1.23
317 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><a href="http://www.whatwg.org/demos/workers/primes/page.html">View this
318     example online</a>.
319    
320     </span><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/primes/page.html">この例をオンラインで見る</a>。</span><h4 id=a-worker class="no-ja-translation"><span class=secno>1.1.2 </span>A worker for updating a
321 wakaba 1.23 client-side database</h4>
322    
323 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
324    
325     </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="no-ja-translation">In this example, the main document spawns a worker whose only task is to
326 wakaba 1.23 listen for notifications from the server, and, when appropriate, either
327     add or remove data from the client-side database.
328    
329 wakaba 1.32 <p class="no-ja-translation">Since no communication occurs between the worker and the main page, the
330 wakaba 1.23 main page can start the worker by just doing:
331    
332 wakaba 1.32 <pre class="no-ja-translation">&lt;script>
333 wakaba 1.30 new Worker('worker.js');
334 wakaba 1.23 &lt;/script></pre>
335    
336 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The worker itself is as follows:
337    
338     </span><span class=ja-translation lang=ja>労働者自体は次の通りです。</span><pre class="no-ja-translation">var server = new WebSocket('ws://whatwg.org/database');
339 wakaba 1.25 var database = openDatabase('demobase', '1.0', 'Demo Database', 10240);
340 wakaba 1.23 server.onmessage = function (event) {
341     // data is in the format "command key value"
342     var data = event.message.split(' ');
343     switch (data[0]) {
344     case '+':
345     database.transaction(function(tx) {
346     tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
347     });
348     case '-':
349     database.transaction(function(tx) {
350     tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
351     });
352     }
353     };</pre>
354    
355 wakaba 1.32 <p class="no-ja-translation">This connects to the server using the <code>WebSocket</code> mechanism
356 wakaba 1.23 and opens the local database (which, we presume, has been created
357     earlier). The worker then just listens for messages from the worker and
358     acts on them as appropriate, forever (or until the main page is closed).
359    
360 wakaba 1.32 <p class="no-ja-translation"><a
361 wakaba 1.23 href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
362     this example online</a>. (This example will not actually function, since
363     the server does not actually exist and the database is not created by this
364     sample code.)
365    
366 wakaba 1.32 <h4 id=worker class="no-ja-translation"><span class=secno>1.1.3 </span>Worker used for backgroud I/O</h4>
367    
368     <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
369 wakaba 1.23
370 wakaba 1.32 </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="no-ja-translation">In this example, the main document uses two workers, one for fetching
371 wakaba 1.23 stock updates for at regular intervals, and one for fetching performing
372     search queries that the user requests.
373    
374 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The main page is as follows:
375    
376     </span><span class=ja-translation lang=ja>主たる頁は次の通りです。</span><pre class="no-ja-translation">&lt;!DOCTYPE HTML>
377 wakaba 1.23 &lt;html>
378     &lt;head>
379     &lt;title>Worker example: Stock ticker&lt;/title>
380     &lt;script>
381     // TICKER
382     var symbol = 'GOOG'; // default symbol to watch
383 wakaba 1.29 var ticker = new Worker('ticker.js');
384 wakaba 1.23
385     // SEARCHER
386 wakaba 1.29 var searcher = new Worker('searcher.js');
387 wakaba 1.23 function search(query) {
388 wakaba 1.29 searcher.postMessage(query);
389 wakaba 1.23 }
390    
391     // SYMBOL SELECTION UI
392     function select(newSymbol) {
393     symbol = newSymbol;
394 wakaba 1.29 ticker.postMessage(symbol);
395 wakaba 1.23 }
396     &lt;/script>
397     &lt;/head>
398     &lt;body>
399     &lt;p>&lt;output id="symbol">&lt;/output> &lt;output id="value">&lt;/output>&lt;/p>
400     &lt;script>
401 wakaba 1.29 ticker.onmessage = function (event) {
402 wakaba 1.23 var data = event.message.split(' ');
403     document.getElementById('symbol').textContent = data[0];
404     document.getElementById('value').textContent = data[1];
405     };
406 wakaba 1.29 ticker.postMessage(symbol);
407 wakaba 1.23 &lt;/script>
408     &lt;p>&lt;label>Search: &lt;input type="text" oninput="search(this.value)">&lt;/label>&lt;/p>
409     &lt;ul id="results">&lt;/ul>
410     &lt;script>
411 wakaba 1.29 searcher.onmessage = function (event) {
412 wakaba 1.23 var data = event.message.split(' ');
413     var results = document.getElementById('results');
414     while (results.hasChildNodes()) // clear previous results
415     results.removeChild(results.firstChild);
416     for (var i = 0; i &lt; data.length; i += 1) {
417     // add a list item with a button for each result
418     var li = document.createElement('li');
419     var button = document.createElement('button');
420     button.value = data[i];
421     button.type = 'button';
422     button.onclick = function () { select(this.value); };
423     button.textContent = data[i];
424     li.appendChild(button);
425     results.appendChild(li);
426     }
427     };
428     &lt;/script>
429     &lt;p>(The data in this example is not real. Try searching for "Google" or "Apple".)&lt;/p>
430     &lt;/body>
431     &lt;/html></pre>
432    
433 wakaba 1.32 <p class="no-ja-translation">The two workers use a common library for performing the actual network
434 wakaba 1.23 calls. This library is as follows:
435    
436 wakaba 1.32 <pre class="no-ja-translation">function get(url) {
437 wakaba 1.29 try {
438     var xhr = new XMLHttpRequest();
439     xhr.open('GET', url, false);
440     xhr.send();
441     return xhr.responseText;
442     } catch (e) {
443     return ''; // turn all errors into empty results
444     }
445 wakaba 1.23 }</pre>
446    
447 wakaba 1.32 <p class="no-ja-translation">The stock updater worker is as follows:
448 wakaba 1.23
449 wakaba 1.32 <pre class="no-ja-translation">importScripts('io.js');
450 wakaba 1.23 var timer;
451     var symbol;
452     function update() {
453 wakaba 1.29 postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
454 wakaba 1.23 timer = setTimeout(update, 10000);
455     }
456 wakaba 1.29 onmessage = function (event) {
457 wakaba 1.23 if (timer)
458     clearTimeout(timer);
459     symbol = event.message;
460     update();
461     };</pre>
462    
463 wakaba 1.32 <p class="no-ja-translation">The search query worker is as follows:
464 wakaba 1.23
465 wakaba 1.32 <pre class="no-ja-translation">importScripts('io.js');
466 wakaba 1.29 onmessage = function (event) {
467     postMessage(get('search.cgi?' + event.message));
468 wakaba 1.23 };</pre>
469    
470 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><a href="http://www.whatwg.org/demos/workers/stocks/page.html">View this
471     example online</a>.
472 wakaba 1.23
473 wakaba 1.32 </span><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/stocks/page.html">この例をオンラインで見る</a>。</span><h4 id=shared class="no-ja-translation"><span class=secno>1.1.4 </span>Shared workers</h4>
474    
475     <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
476    
477     </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="no-ja-translation">In this example, multiple windows (viewers) can be opened that are all
478 wakaba 1.23 viewing the same map. All the windows share the same map information, with
479     a single worker coordinating all the viewers. Each viewer can move around
480     idependently, but if they set any data on the map, all the viewers are
481     updated.
482    
483 wakaba 1.32 <p class="no-ja-translation">The main page isn't interesting, it merely provides a way to open the
484 wakaba 1.23 viewers:
485    
486 wakaba 1.32 <pre class="no-ja-translation">&lt;!DOCTYPE HTML>
487 wakaba 1.23 &lt;html>
488     &lt;head>
489     &lt;title>Workers example: Multiviewer&lt;/title>
490     &lt;script>
491     function openViewer() {
492     window.open('viewer.html');
493     }
494     &lt;/script>
495     &lt;/head>
496     &lt;body>
497     &lt;p>&lt;button type=button onclick="openViewer()">Open a new
498     viewer&lt;/button>&lt;/p>
499     &lt;p>Each viewer opens in a new window. You can have as many viewers
500     as you like, they all view the same data.&lt;/p>
501     &lt;/body>
502     &lt;/html></pre>
503    
504 wakaba 1.32 <p class="no-ja-translation">The viewer is more involved:
505 wakaba 1.23
506 wakaba 1.32 <pre class="no-ja-translation">&lt;!DOCTYPE HTML>
507 wakaba 1.23 &lt;html>
508     &lt;head>
509     &lt;title>Workers example: Multiviewer viewer&lt;/title>
510     &lt;script>
511 wakaba 1.29 var worker = new SharedWorker('worker.js', 'core');
512 wakaba 1.23
513     // CONFIGURATION
514     function configure(event) {
515     if (event.message.substr(0, 4) != 'cfg ') return;
516     var name = event.message.substr(4).split(' ', 1);
517     // update display to mention our name is name
518     document.getElementsByTagName('h1')[0].textContent += ' ' + name;
519     // no longer need this listener
520 wakaba 1.25 worker.port.removeEventListener('message', configure, false);
521 wakaba 1.23 }
522 wakaba 1.30 worker.port.addEventListener('message', configure, false);
523 wakaba 1.23
524     // MAP
525     function paintMap(event) {
526     if (event.message.substr(0, 4) != 'map ') return;
527     var data = event.message.substr(4).split(',');
528     // display tiles data[0] .. data[8]
529     var canvas = document.getElementById('map');
530     var context = canvas.getContext('2d');
531     for (var y = 0; y &lt; 3; y += 1) {
532     for (var x = 0; x &lt; 3; x += 1) {
533     var tile = data[y * 3 + x];
534     if (tile == '0')
535     context.fillStyle = 'green';
536     else
537     context.fillStyle = 'maroon';
538     fillRect(x * 50, y * 50, 50, 50);
539     }
540     }
541     }
542 wakaba 1.30 worker.port.addEventListener('message', paintMap, false);
543 wakaba 1.23
544     // PUBLIC CHAT
545     function updatePublicChat(event) {
546     if (event.message.substr(0, 4) != 'txt ') return;
547     var name = event.message.substr(4).split(' ', 1);
548     var message = event.message.substr(4 + length(name) + 1);
549     // display "&lt;name> message" in public chat
550     var dialog = document.getElementById('public');
551     var dt = document.createElement('dt');
552     dt.textContent = name;
553     dialog.appendChild(dt);
554     var dd = document.createElement('dd');
555     dd.textContent = message;
556     dialog.appendChild(dd);
557     }
558 wakaba 1.30 worker.port.addEventListener('message', updatePublicChat, false);
559 wakaba 1.23
560     // PRIVATE CHAT
561     function startPrivateChat(event) {
562     if (event.message.substr(0, 4) != 'msg ') return;
563     var name = event.message.substr(4).split(' ', 1);
564     var port = event.port;
565     // display a private chat UI
566     var ul = document.getElementById('private');
567     var li = document.createElement('li');
568     var h3 = document.createElement('h3');
569     h3.textContent = 'Private chat with ' + name;
570     li.appendChild(h3);
571     var dialog = document.createElement('dialog');
572     var addMessage = function(name, message) {
573     var dt = document.createElement('dt');
574     dt.textContent = name;
575     dialog.appendChild(dt);
576     var dd = document.createElement('dd');
577     dd.textContent = message;
578     dialog.appendChild(dd);
579     };
580     port.onmessage = function (event) {
581     addMessage(name, event.message);
582     };
583     li.appendChild(dialog);
584     var form = document.createElement('form');
585     var p = document.createElement('p');
586     var input = document.createElement('input');
587     input.size = 50;
588     p.appendChild(input);
589     p.appendChild(document.createTextNode(' '));
590     var button = document.createElement('button');
591     button.textContent = 'Post';
592     p.appendChild(button);
593     form.onsubmit = function () {
594     port.postMessage(input.value);
595     addMessage('me', input.value);
596     input.value = '';
597     return false;
598     };
599     form.appendChild(p);
600     li.appendChild(form);
601     }
602 wakaba 1.30 worker.port.addEventListener('message', startPrivateChat, false);
603 wakaba 1.23 &lt;/script>
604     &lt;/head>
605 wakaba 1.29 &lt;body>
606 wakaba 1.23 &lt;h1>Viewer&lt;/h1>
607     &lt;h2>Map&lt;/h2>
608     &lt;p>&lt;canvas id="map" height=150 width=150>&lt;/canvas>&lt;/p>
609     &lt;p>
610 wakaba 1.30 &lt;button type=button onclick="worker.port.postMessage('mov left')">Left&lt;/button>
611     &lt;button type=button onclick="worker.port.postMessage('mov up')">Up&lt;/button>
612     &lt;button type=button onclick="worker.port.postMessage('mov down')">Down&lt;/button>
613     &lt;button type=button onclick="worker.port.postMessage('mov right')">Right&lt;/button>
614     &lt;button type=button onclick="worker.port.postMessage('set 0')">Set 0&lt;/button>
615     &lt;button type=button onclick="worker.port.postMessage('set 1')">Set 1&lt;/button>
616 wakaba 1.23 &lt;/p>
617     &lt;h2>Public Chat&lt;/h2>
618     &lt;dialog id="public">&lt;/dialog>
619 wakaba 1.30 &lt;form onsubmit="worker.port.postMessage('txt ' + message.value); message.value = ''; return false;">
620 wakaba 1.23 &lt;p>
621     &lt;input type="text" name="message" size="50">
622     &lt;button>Post&lt;/button>
623     &lt;/p>
624     &lt;/form>
625     &lt;h2>Private Chat&lt;/h2>
626     &lt;ul id="private">&lt;/ul>
627     &lt;/body>
628 wakaba 1.25 &lt;/html>
629     </pre>
630 wakaba 1.23
631 wakaba 1.32 <p class="no-ja-translation">There are several key things worth noting about the way the viewer is
632 wakaba 1.23 written.
633    
634 wakaba 1.32 <p class="no-ja-translation"><strong>Multiple listeners</strong>. Instead of a single message
635 wakaba 1.23 processing function, the code here attaches multiple event listeners, each
636     one performing a quick check to see if it is relevant for the message. In
637     this example it doesn't make much difference, but if multiple authors
638     wanted to collaborate using a single port to communicate with a worker, it
639     would allow for independent code instead of changes having to all be made
640     to a single event handling function.
641    
642 wakaba 1.32 <p class="no-ja-translation">Registering event listeners in this way also allows you to unregister
643 wakaba 1.23 specific listeners when you are done with them, as is done with the <code
644     title="">configure()</code> method in this example.
645    
646 wakaba 1.32 <p class="no-ja-translation">Finally, the worker:
647 wakaba 1.23
648 wakaba 1.32 <pre class="no-ja-translation">
649 wakaba 1.23 var nextName = 0;
650     function getNextName() {
651     // this could use more friendly names
652     // but for now just return a number
653     return nextName++;
654     }
655    
656     var map = [
657     [0, 0, 0, 0, 0, 0, 0],
658     [1, 1, 0, 1, 0, 1, 1],
659     [0, 1, 0, 1, 0, 0, 0],
660     [0, 1, 0, 1, 0, 1, 1],
661     [0, 0, 0, 1, 0, 0, 0],
662     [1, 0, 0, 1, 1, 1, 1],
663     [1, 1, 0, 1, 1, 0, 1],
664     ];
665    
666     function wrapX(x) {
667     if (x &lt; 0) return wrapX(x + map[0].length);
668     if (x >= map[0].length) return wrapX(x - map[0].length);
669     return x;
670     }
671    
672     function wrapY(y) {
673     if (y &lt; 0) return wrapY(y + map.length);
674     if (y >= map[0].length) return wrapY(y - map.length);
675     return y;
676     }
677    
678 wakaba 1.29 function sendMapData(callback) {
679 wakaba 1.23 var data = '';
680     for (var y = viewer.y-1; y &lt;= viewer.y+1; y += 1) {
681     for (var x = viewer.x-1; x &lt;= viewer.x+1; x += 1) {
682     if (data != '')
683     data += ',';
684     data += map[y][x];
685     }
686     }
687 wakaba 1.29 callback('map ' + data);
688 wakaba 1.23 }
689    
690     var viewers = {};
691     onconnect = function (event) {
692     event.port._name = getNextName();
693     event.port._data = { port: event.port, x: 0, y: 0, };
694     viewers[event.port._name] = event.port._data;
695     event.port.postMessage('cfg ' + name);
696     event.port.onmessage = getMessage;
697 wakaba 1.29 sendMapData(event.port.postMessage);
698 wakaba 1.23 };
699    
700     function getMessage(event) {
701     switch (event.message.substr(0, 4)) {
702     case 'mov ':
703     var direction = event.message.substr(4);
704     var dx = 0;
705     var dy = 0;
706     switch (direction) {
707     case 'up': dy = -1; break;
708     case 'down': dy = 1; break;
709     case 'left': dx = -1; break;
710     case 'right': dx = 1; break;
711     }
712     event.target._data.x = wrapX(event.target._data.x + dx);
713     event.target._data.y = wrapY(event.target._data.y + dy);
714 wakaba 1.29 sendMapData(event.target.postMessage);
715 wakaba 1.23 break;
716     case 'set ':
717     var value = event.message.substr(4);
718     map[event.target._data.y][event.target._data.x] = value;
719     for (var viewer in viewers)
720 wakaba 1.29 sendMapData(viewers[viewer].port.postMessage);
721 wakaba 1.23 break;
722     case 'txt ':
723     var name = event.target._name;
724     var message = event.message.substr(4);
725     for (var viewer in viewers)
726     viewers[viewer].port.postMessage('txt ' + name + ' ' + message);
727     break;
728     case 'msg ':
729     var party1 = event._data;
730     var party2 = viewers[event.message.substr(4).split(' ', 1)];
731     if (party2) {
732     var channel = new MessageChannel();
733     party1.port.postMessage('msg ' + party2.name, channel.port1);
734     party2.port.postMessage('msg ' + party1.name, channel.port2);
735     }
736     break;
737     }
738     }</pre>
739    
740 wakaba 1.32 <p class="no-ja-translation"><strong>Connecting to multiple pages</strong>. The script uses the <code
741 wakaba 1.30 title=handler-SharedWorkerGlobalScope-onconnect><a
742     href="#onconnect">onconnect</a></code> event listener to listen for
743     multiple connections.
744 wakaba 1.23
745 wakaba 1.32 <p class="no-ja-translation"><strong>Direct channels</strong>. When the worker receives a "msg"
746 wakaba 1.23 message from one viewer naming another viewer, it sets up a direct
747     connection between the two, so that the two viewers can communicate
748     directly without the worker having to proxy all the messages.
749    
750 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><a href="http://www.whatwg.org/demos/workers/multiviewer/page.html">View
751     this example online</a>.
752    
753     </span><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/multiviewer/page.html">この例をオンラインで見る</a>。</span><h4 id=delegation class="no-ja-translation"><span class=secno>1.1.5 </span>Delegation</h4>
754 wakaba 1.23
755 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
756    
757     </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="no-ja-translation">With multicore CPUs becoming prevalent, one way to obtain better
758 wakaba 1.23 performance is to split computationally expensive tasks amongst multiple
759     workers. In this example, a computationally expensive task that is to be
760     performed for every number from 1 to 10,000,000 is farmed out to ten
761     subworkers.
762    
763 wakaba 1.32 <p class="no-ja-translation">The main page is as follows, it just reports the result:
764 wakaba 1.23
765 wakaba 1.32 <pre class="no-ja-translation">&lt;!DOCTYPE HTML>
766 wakaba 1.23 &lt;html>
767     &lt;head>
768     &lt;title>Worker example: One-core computation&lt;/title>
769     &lt;/head>
770     &lt;body>
771     &lt;p>The highest prime number discovered so far is: &lt;output id="result">&lt;/output>&lt;/p>
772     &lt;script>
773 wakaba 1.29 var worker = new Worker('worker.js');
774     worker.onmessage = function (event) {
775 wakaba 1.23 document.getElementById('result').textContent = event.message;
776     };
777     &lt;/script>
778     &lt;/body>
779     &lt;/html></pre>
780    
781 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The worker itself is as follows:
782    
783     </span><span class=ja-translation lang=ja>労働者自体は次の通りです。</span><pre class="no-ja-translation">// settings
784 wakaba 1.23 var num_workers = 10;
785     var items_per_worker = 1000000;
786    
787     // start the workers
788     var result = 0;
789     var pending_workers = num_workers;
790     for (var i = 0; i &lt; num_workers; i += 1) {
791 wakaba 1.29 var worker = new Worker('core.js');
792     worker.postMessage(i * items_per_worker);
793     worker.postMessage((i+1) * items_per_worker);
794     worker.onmessage = storeResult;
795 wakaba 1.23 }
796    
797     // handle the results
798     function storeResult(event) {
799     result += 1*event.message;
800     pending_workers -= 1;
801     if (pending_workers &lt;= 0)
802 wakaba 1.29 postMessage(result); // finished!
803 wakaba 1.23 }</pre>
804    
805 wakaba 1.32 <p class="no-ja-translation">It consists of a loop to start the subworkers, and then a handler that
806 wakaba 1.23 waits for all the subworkers to respond.
807    
808 wakaba 1.32 <p class="no-ja-translation">The subworkers are implemented as follows:
809 wakaba 1.23
810 wakaba 1.32 <pre class="no-ja-translation">var start;
811 wakaba 1.23 onmessage = getStart;
812     function getStart(event) {
813     start = 1*event.message;
814     onmessage = getEnd;
815     }
816    
817     var end;
818     function getEnd(event) {
819     end = 1*event.message;
820     onmessage = null;
821     do();
822     }
823    
824     function do() {
825     var result = 0;
826     for (var i = start; i &lt; end; i += 1) {
827     // perform some complex calculation here
828     result += 1;
829     }
830 wakaba 1.29 postMessage(result);
831     close();
832 wakaba 1.23 }</pre>
833    
834 wakaba 1.32 <p class="no-ja-translation">They receive two numbers in two events, perform the computation for the
835 wakaba 1.23 range of numbers thus specified, and then report the result back to the
836     parent.
837    
838 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><a href="http://www.whatwg.org/demos/workers/multicore/page.html">View
839     this example online</a>.
840    
841     </span><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/multicore/page.html">この例をオンラインで見る</a>。</span><h4 id=providing class="no-ja-translation"><span class=secno>1.1.6 </span>Providing libraries</h4>
842 wakaba 1.28
843 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><em>This section is non-normative.</em>
844    
845     </span><span class=ja-translation lang=ja><em>この節は規定の一部ではありません。</em></span><p class="no-ja-translation">Suppose that a cryptography library is made available that provides
846 wakaba 1.28 three tasks:
847    
848     <dl>
849 wakaba 1.32 <dt class="no-ja-translation">Generate a public/private key pair
850 wakaba 1.28
851 wakaba 1.32 <dd class="no-ja-translation">Takes a port, on which it will send two messages, first the public key
852 wakaba 1.28 and then the private key.
853    
854 wakaba 1.32 <dt class="no-ja-translation">Given a plaintext and a public key, return the corresponding
855 wakaba 1.28 cyphertext
856    
857 wakaba 1.32 <dd class="no-ja-translation">Takes a port, to which any number of messages can be sent, the first
858 wakaba 1.28 giving the public key, and the remainder giving the plaintext, each of
859     which is encrypted and then sent on that same channel as the cyphertext.
860     The user can close the port when it is done encrypting content.
861    
862 wakaba 1.32 <dt class="no-ja-translation">Given a cyphertext and a private key, return the corresponding
863 wakaba 1.28 plaintext
864    
865 wakaba 1.32 <dd class="no-ja-translation">Takes a port, to which any number of messages can be sent, the first
866 wakaba 1.28 giving the private key, and the remainder giving the cyphertext, each of
867     which is decrypted and then sent on that same channel as the plaintext.
868     The user can close the port when it is done decrypting content.
869     </dl>
870    
871 wakaba 1.32 <p class="no-ja-translation">The library itself is as follows:
872 wakaba 1.28
873 wakaba 1.32 <pre class="no-ja-translation">function handleMessage(e) {
874 wakaba 1.28 if (e.message == "genkeys")
875     genkeys(e.port);
876     else if (e.message == "encrypt")
877     encrypt(e.port);
878     else if (e.message == "decrypt")
879     decrypt(e.port);
880     }
881    
882     function genkeys(p) {
883     var keys = _generateKeyPair();
884     p.postMessage(keys[0]);
885     p.postMessage(keys[1]);
886     }
887    
888     function encrypt(p) {
889     var key, state = 0;
890     p.onmessage = function (e) {
891     if (state == 0) {
892     key = e.message;
893     state = 1;
894     } else {
895     p.postMessage(_encrypt(key, e.message));
896     }
897     };
898     }
899    
900     function decrypt(p) {
901     var key, state = 0;
902     p.onmessage = function (e) {
903     if (state == 0) {
904     key = e.message;
905     state = 1;
906     } else {
907     p.postMessage(_decrypt(key, e.message));
908     }
909     };
910     }
911    
912 wakaba 1.29 // support being used as a shared worker as well as a dedicated worker
913     if (this.onmessage) // dedicated worker
914     onmessage = handleMessage;
915     else // shared worker
916     onconnect = function (e) { e.port.onmessage = handleMessage; }
917 wakaba 1.28
918    
919     // the "crypto" functions:
920    
921     function _generateKeyPair() {
922     return [Math.random(), Math.random()];
923     }
924    
925     function _encrypt(k, s) {
926     return 'encrypted-' + k + ' ' + s;
927     }
928    
929     function _decrypt(k, s) {
930     return s.substr(s.indexOf(' ')+1);
931     }</pre>
932    
933 wakaba 1.32 <p class="no-ja-translation">Note that the crypto functions here are just stubs and don't do real
934 wakaba 1.28 cryptography.
935    
936 wakaba 1.32 <p class="no-ja-translation">This library could be used as follows:
937 wakaba 1.28
938 wakaba 1.32 <pre class="no-ja-translation">&lt;!DOCTYPE HTML>
939 wakaba 1.28 &lt;html>
940     &lt;head>
941     &lt;title>Worker example: Crypto library&lt;/title>
942     &lt;script>
943 wakaba 1.29 var crytoLib = new Worker('libcrypto-v1.js'); // or could use 'libcrypto-v2.js'
944 wakaba 1.28 function getKeys() {
945     var state = 0;
946 wakaba 1.29 cryptoLib.startConversation("genkeys").onmessage = function (e) {
947 wakaba 1.28 if (state == 0)
948     document.getElementById('public').value = e.message;
949     else if (state == 1)
950     document.getElementById('private').value = e.message;
951     state += 1;
952     };
953     }
954     function enc() {
955 wakaba 1.29 var port = cryptoLib.startConversation("encrypt");
956     port.postMessage(document.getElementById('public').value);
957     port.postMessage(document.getElementById('input').value);
958     port.onmessage = function (e) {
959 wakaba 1.28 document.getElementById('input').value = e.message;
960 wakaba 1.29 port.close();
961 wakaba 1.28 };
962     }
963     function dec() {
964 wakaba 1.29 var port = cryptoLib.startConversation("decrypt");
965     port.postMessage(document.getElementById('private').value);
966     port.postMessage(document.getElementById('input').value);
967     port.onmessage = function (e) {
968 wakaba 1.28 document.getElementById('input').value = e.message;
969 wakaba 1.29 port.close();
970 wakaba 1.28 };
971     }
972     &lt;/script>
973     &lt;style>
974     textarea { display: block; }
975     &lt;/style>
976     &lt;/head>
977     &lt;body onload="getKeys()">
978     &lt;fieldset>
979     &lt;legend>Keys&lt;/legend>
980 wakaba 1.29 &lt;p>&lt;label>Public Key: &lt;textarea id="public">&lt;/textarea>&lt;/label>&lt;/p>
981     &lt;p>&lt;label>Private Key: &lt;textarea id="private">&lt;/textarea>&lt;/label>&lt;/p>
982 wakaba 1.28 &lt;/fieldset>
983 wakaba 1.29 &lt;p>&lt;label>Input: &lt;textarea id="input">&lt;/textarea>&lt;/label>&lt;/p>
984 wakaba 1.28 &lt;p>&lt;button onclick="enc()">Encrypt&lt;/button> &lt;button onclick="dec()">Decrypt&lt;/button>&lt;/p>
985     &lt;/body>
986     &lt;/html></pre>
987    
988 wakaba 1.32 <p class="no-ja-translation">A later version of the API, though, might want to offload all the crypto
989 wakaba 1.28 work onto subworkers. This could be done as follows:
990    
991 wakaba 1.32 <pre class="no-ja-translation">function handleMessage(e) {
992 wakaba 1.28 if (e.message == "genkeys")
993     genkeys(e.port);
994     else if (e.message == "encrypt")
995     encrypt(e.port);
996     else if (e.message == "decrypt")
997     decrypt(e.port);
998     }
999    
1000     function genkeys(p) {
1001 wakaba 1.29 var generator = new Worker('libcrypto-v2-generator.js');
1002 wakaba 1.28 generator.postMessage('', p);
1003     }
1004    
1005     function encrypt(p) {
1006     p.onmessage = function (e) {
1007     var key = e.message;
1008 wakaba 1.29 var encryptor = new Worker('libcrypto-v2-encryptor.js');
1009 wakaba 1.28 encryptor.postMessage(key, p);
1010     };
1011     }
1012    
1013     function encrypt(p) {
1014     p.onmessage = function (e) {
1015     var key = e.message;
1016 wakaba 1.29 var decryptor = new Worker('libcrypto-v2-decryptor.js');
1017 wakaba 1.28 decryptor.postMessage(key, p);
1018     };
1019     }
1020    
1021 wakaba 1.29 // support being used as a shared worker as well as a dedicated worker
1022     if (this.onmessage) // dedicated worker
1023     onmessage = handleMessage;
1024     else // shared worker
1025     onconnect = function (e) { e.port.onmessage = handleMessage; }
1026 wakaba 1.28 </pre>
1027    
1028 wakaba 1.32 <p class="no-ja-translation">The little subworkers would then be as follows.
1029 wakaba 1.28
1030 wakaba 1.32 <p class="no-ja-translation">For generating key pairs:
1031 wakaba 1.28
1032 wakaba 1.32 <pre class="no-ja-translation">onmessage = function (e) {
1033 wakaba 1.29 var k = _generateKeyPair();
1034     e.port.postMessage(k[0]);
1035     e.port.postMessage(k[1]);
1036 wakaba 1.28 close();
1037     }
1038 wakaba 1.29
1039     function _generateKeyPair() {
1040     return [Math.random(), Math.random()];
1041     }</pre>
1042 wakaba 1.28
1043 wakaba 1.32 <p class="no-ja-translation">For encrypting:
1044 wakaba 1.28
1045 wakaba 1.32 <pre class="no-ja-translation">onmessage = function (e) {
1046 wakaba 1.28 var key = e.message;
1047     e.port.onmessage = function (e) {
1048     var s = e.message;
1049 wakaba 1.29 postMessage(_encrypt(key, s));
1050 wakaba 1.28 }
1051 wakaba 1.29 e.port.onclose = function (e) {
1052 wakaba 1.28 close();
1053     }
1054 wakaba 1.29 }
1055    
1056     function _encrypt(k, s) {
1057     return 'encrypted-' + k + ' ' + s;
1058 wakaba 1.28 }</pre>
1059    
1060 wakaba 1.32 <p class="no-ja-translation">For decrypting:
1061 wakaba 1.28
1062 wakaba 1.32 <pre class="no-ja-translation">onmessage = function (e) {
1063 wakaba 1.28 var key = e.message;
1064     e.port.onmessage = function (e) {
1065     var s = e.message;
1066 wakaba 1.29 postMessage(_decrypt(key, s));
1067 wakaba 1.28 }
1068 wakaba 1.29 e.port.onclose = function (e) {
1069 wakaba 1.28 close();
1070     }
1071 wakaba 1.29 }
1072    
1073     function _decrypt(k, s) {
1074     return s.substr(s.indexOf(' ')+1);
1075 wakaba 1.28 }</pre>
1076    
1077 wakaba 1.32 <p class="no-ja-translation">Notice how the users of the API don't have to even know that this is
1078 wakaba 1.28 happening &mdash; the API hasn't changed; the library can delegate to
1079     subworkers without changing its API, even though it is accepting data
1080     using message channels.
1081    
1082 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><a href="http://www.whatwg.org/demos/workers/crypto/page.html">View this
1083     example online</a>.
1084    
1085     </span><span class=ja-translation lang=ja><a href="http://www.whatwg.org/demos/workers/crypto/page.html">この例をオンラインで見る</a>。</span><h3 id=conformance class="has-ja-translation"><span class=en-original lang=en><span class=secno>1.2 </span>Conformance requirements</span><span class=ja-translation lang=ja><span class=secno>1.2 </span>適合性要件</span></h3>
1086 wakaba 1.1
1087 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>All diagrams, examples, and notes in this specification are
1088     non-normative, as are all sections explicitly marked non-normative.
1089     Everything else in this specification is normative.
1090    
1091     </span><span class=ja-translation lang=ja>この仕様書のすべての図、例示、注意書き、ならびに規定の一部ではないと明記したすべての節は、
1092 wakaba 1.1 規定の一部ではありません。
1093 wakaba 1.32 この仕様書のそれ以外の部分はすべて規定の一部です。</span><p class="no-ja-translation">The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
1094 wakaba 1.31 NOT",-->
1095     "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
1096     normative parts of this document are to be interpreted as described in
1097     RFC2119. For readability, these words do not appear in all uppercase
1098     letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
1099 wakaba 1.1 <!-- XXX but they should be
1100     marked up -->
1101    
1102 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Requirements phrased in the imperative as part of algorithms (such as
1103     "strip any leading space characters" or "return false and abort these
1104     steps") are to be interpreted with the meaning of the key word ("must",
1105     "should", "may", etc) used in introducing the algorithm.
1106    
1107     </span><span class=ja-translation lang=ja>算法の一部として<ins class=ja-note>動作の指示の形で (原文では</ins>命令形で<ins class=ja-note>) </ins>記述された要件
1108 wakaba 1.1 (例えば「先頭の間隔文字をすべて除去します」や「偽を返し、これらの段階を停止します」)
1109 wakaba 1.24 は、算法を紹介するに当たって使われたキーワード (<em class=rfc2119 title="MUST">しなければなりません</em>、
1110 wakaba 1.32 <em class=rfc2119 title="SHOULD">するべきです</em>、<em class=rfc2119 title="MAY">して構いません</em>など) の意味により解釈します。</span><p class="has-ja-translation"><span class=en-original lang=en>Some conformance requirements are phrased as requirements on attributes,
1111     methods or objects. Such requirements are to be interpreted as
1112     requirements on user agents.
1113    
1114     </span><span class=ja-translation lang=ja>いくつかの適合性要件は属性やメソッドやオブジェクトの要件として説明されています。
1115     そのような要件は、利用者エージェントに関する要件として解釈します。</span><p class="has-ja-translation"><span class=en-original lang=en>Conformance requirements phrased as algorithms or specific steps may be
1116     implemented in any manner, so long as the end result is equivalent. (In
1117     particular, the algorithms defined in this specification are intended to
1118     be easy to follow, and not intended to be performant.)
1119    
1120     </span><span class=ja-translation lang=ja>算法や特定の段階として説明されている適合性要件は、
1121 wakaba 1.1 最終的な結果が等価である限り、任意の方法で実装して<em class=rfc2119 title="MAY">構いません</em>。
1122     (特に、この仕様書で定義されている算法は追うのを簡単にしており、
1123 wakaba 1.32 高性能であることを目指してはいません。)</span><p class="has-ja-translation"><span class=en-original lang=en>The only conformance class defined by this specification is user agents.
1124    
1125     </span><span class=ja-translation lang=ja>この仕様書で定義する唯一の適合性の級は利用者エージェントです。</span><p class="has-ja-translation"><span class=en-original lang=en>User agents may impose implementation-specific limits on otherwise
1126     unconstrained inputs, e.g. to prevent denial of service attacks, to guard
1127     against running out of memory, or to work around platform-specific
1128     limitations.
1129    
1130     </span><span class=ja-translation lang=ja>利用者エージェントは、サービス拒否攻撃を防ぐため、
1131 wakaba 1.1 記憶容量を使い尽くすことを防ぐため、
1132     あるいは環境特有の制限に対応するためなどの目的で、
1133 wakaba 1.32 別段の制約のない入力に対して実装特有の制限を設けて<em class=rfc2119 title="MAY">構いません</em>。</span><h4 id=dependencies class="has-ja-translation"><span class=en-original lang=en><span class=secno>1.2.1 </span>Dependencies</span><span class=ja-translation lang=ja><span class=secno>1.2.1 </span>依存性</span></h4>
1134 wakaba 1.1
1135 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>This specification relies on several other underlying specifications.
1136    
1137     </span><span class=ja-translation lang=ja>この仕様書は、他のいくつかの下位の仕様書に依存しています。</span><dl>
1138     <dt class="no-ja-translation">HTML5
1139 wakaba 1.1
1140     <dd>
1141 wakaba 1.32 <p class="no-ja-translation">Many fundamental concepts from HTML5 are used by this specification.
1142 wakaba 1.31 <a href="#refsHTML5">[HTML5]</a></p>
1143 wakaba 1.1
1144 wakaba 1.32 <dt class="no-ja-translation">ECMAScript
1145 wakaba 1.1
1146     <dd>
1147 wakaba 1.32 <p class="no-ja-translation">This specification is intended to be used with JavaScript as the
1148 wakaba 1.31 scripting language. <a href="#refsJS">[JS]</a></p>
1149 wakaba 1.1
1150 wakaba 1.32 <dt class="no-ja-translation">WebIDL
1151 wakaba 1.1
1152     <dd>
1153 wakaba 1.32 <p class="no-ja-translation">The IDL blocks in this specification use the semantics of the WebIDL
1154 wakaba 1.31 specification. <a href="#refsWebIDL">[WebIDL]</a></p>
1155 wakaba 1.1 </dl>
1156    
1157 wakaba 1.32 <h3 id=terminology class="has-ja-translation"><span class=en-original lang=en><span class=secno>1.3 </span>Terminology</span><span class=ja-translation lang=ja><span class=secno>1.3 </span>用語</span></h3>
1158    
1159     <p class="has-ja-translation"><span class=en-original lang=en>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
1160     <em>visible</em> might sometimes be used when referring to the way a
1161     document is rendered to the user. These terms are not meant to imply a
1162     visual medium; they must be considered to apply to other media in
1163     equivalent ways.
1164 wakaba 1.1
1165 wakaba 1.32 </span><span class=ja-translation lang=ja>簡単のため、文書が利用者に対してレンダリングされる方法に言及する時に<em>示す</em>、
1166 wakaba 1.24 <em>表示する</em>、<em>可視</em>などの用語を使うことがあります。
1167     このような用語は特に視覚媒体を暗示しているわけではありません。
1168 wakaba 1.32 他の媒体であってもこれらの用語が等価な方法で適用されるものと考えなければ<em class=rfc2119 title="MUST">なりません</em>。</span><p class="has-ja-translation"><span class=en-original lang=en>The construction "a <code title="">Foo</code> object", where <code
1169     title="">Foo</code> is actually an interface, is sometimes used instead of
1170     the more accurate "an object implementing the interface <code
1171     title="">Foo</code>".
1172    
1173     </span><span class=ja-translation lang=ja>「<code title="">Foo</code> オブジェクト」という構文 (<code title="">Foo</code>
1174 wakaba 1.10 は実際には界面) を時々使いますが、より正確な「界面 <code title="">Foo</code>
1175 wakaba 1.32 を実装しているオブジェクト」の代わりに用いています。</span><p class="no-ja-translation">The term DOM is used to refer to the API set made available to scripts
1176 wakaba 1.31 in Web applications, and does not necessarily imply the existence of an
1177     actual <code>Document</code> object or of any other <code>Node</code>
1178     objects as defined in the DOM Core specifications. <a
1179     href="#refsDOM3CORE">[DOM3CORE]</a>
1180    
1181 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>A DOM attribute is said to be <em>getting</em> when its value is being
1182     retrieved (e.g. by author script), and is said to be <em>setting</em> when
1183     a new value is assigned to it.
1184    
1185     </span><span class=ja-translation lang=ja>DOM 属性は、その値が (例えば著者のスクリプトにより) 取り出される時<em>取得される</em>といい、
1186     新しい値がその属性に割り当てられる時<em>設定される</em>といいます。</span><p class="has-ja-translation"><span class=en-original lang=en>If a DOM object is said to be <dfn id="en-live">live</dfn>, then that means
1187     that any attributes returning that object must always return the same
1188     object (not a new object each time), and the attributes and methods on
1189     that object must operate on the actual underlying data, not a snapshot of
1190     the data.
1191    
1192     </span><span class=ja-translation lang=ja>DOM オブジェクトが<dfn id=live>生きている</dfn>という場合、
1193 wakaba 1.1 そのオブジェクトを返す属性は常に同じオブジェクトを返さなければ<em class=rfc2119 title="MUST">ならない</em>
1194     (毎回新しいオブジェクトを返すのではなく) 上に、
1195     そのオブジェクトの属性やメソッドがそのオブジェクトの裏にある実際のデータのスナップ写真ではなく、
1196 wakaba 1.32 データそのものに対して操作されなければ<em class=rfc2119 title="MUST">ならない</em>ことを意味します。</span><h2 id=infrastructure class="has-ja-translation"><span class=en-original lang=en><span class=secno>2. </span>Infrastructure</span><span class=ja-translation lang=ja><span class=secno>2. </span>基盤</span></h2>
1197 wakaba 1.9
1198 wakaba 1.32 <p class="no-ja-translation">There are two kinds of workers; dedicated workers, and shared workers.
1199 wakaba 1.29 Dedicated workers, once created, and are linked to their creator; but
1200     message ports can be used to communicate from a dedicated worker to
1201     multple other browsing contexts or workers. Shared workers, on the other
1202     hand, are named, and once created any script running in the same
1203     <span>origin</span> can obtain a reference to that worker and communicate
1204     with it.
1205    
1206 wakaba 1.32 <h3 id=the-global class="no-ja-translation"><span class=secno>2.1 </span>The global scope</h3>
1207 wakaba 1.29
1208 wakaba 1.32 <p class="no-ja-translation">The global scope is the "inside" of a worker.
1209 wakaba 1.29
1210 wakaba 1.32 <h4 id=the-workerglobalscope class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.1.1 </span>The <code><a
1211     href="#workerglobalscope">WorkerGlobalScope</a></code> abstract interface</span><span class=ja-translation lang=ja><span class=secno>2.1.1 </span><code><a href="#workerglobalscope">WorkerGlobalScope</a></code> abstract 界面</span></h4>
1212 wakaba 1.9
1213     <pre
1214 wakaba 1.32 class="idl no-ja-translation">[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
1215 wakaba 1.22 readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
1216 wakaba 1.23 readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#location" title=dom-WorkerGlobalScope-location>location</a>;
1217 wakaba 1.29 // also implements everything on <a href="#workerutils">WorkerUtils</a>
1218    
1219 wakaba 1.22 void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
1220 wakaba 1.29 attribute <span>EventListener</span> <a href="#onclose" title=handler-WorkerGlobalScope-onclose>onclose</a>;
1221 wakaba 1.9 };</pre>
1222    
1223 wakaba 1.32 <p class="no-ja-translation">Objects implementing the <code><a
1224 wakaba 1.29 href="#workerglobalscope">WorkerGlobalScope</a></code> interface it must
1225     also implement the <code>EventTarget</code> interface.
1226 wakaba 1.23
1227 wakaba 1.32 <p class="no-ja-translation">The <dfn id=self
1228 wakaba 1.22 title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
1229     return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1230     object itself.
1231    
1232 wakaba 1.32 <p class="no-ja-translation">The <dfn id=location
1233 wakaba 1.23 title=dom-WorkerGlobalScope-location><code>location</code></dfn> attribute
1234     must return the <code><a href="#workerlocation">WorkerLocation</a></code>
1235     object created for the <code><a
1236     href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
1237     worker was created. It represents the <span>absolute URL</span> of the
1238     script that was used to initialize the worker.
1239 wakaba 1.22
1240 wakaba 1.29 <hr>
1241    
1242 wakaba 1.32 <p class="no-ja-translation">When a script invokes the <dfn id=close
1243 wakaba 1.29 title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
1244     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
1245     the user agent must run the following steps:
1246    
1247     <ol>
1248     <li>
1249 wakaba 1.32 <p class="no-ja-translation">Create an <code>Event</code> object with the event name <code
1250 wakaba 1.29 title=event-close>close</code>, which does not bubble and is not
1251     cancelable, and add it to the <code><a
1252     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1253     href="#queue">queue of events</a>, targetted at the <code><a
1254     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1255    
1256     <li>
1257 wakaba 1.32 <p class="no-ja-translation">Set the worker's <code><a
1258 wakaba 1.29 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1259     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1260     true.
1261    
1262     <li>
1263 wakaba 1.32 <p class="no-ja-translation">For each <code>MessagePort</code> object that is entangled with
1264 wakaba 1.29 another port and that has one (but only one) port whose owner is the
1265     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
1266     on which the method was invoked (this would include, for instance, the
1267     implicit port in used for dedicated workers), run the following
1268     substeps:</p>
1269    
1270     <ol>
1271     <li>
1272 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Unentangle the two ports.
1273    
1274     </span><span class=ja-translation lang=ja>2つのポートをほどきます。</span><li>
1275     <p class="no-ja-translation">At the next available opportunity, after any scripts have finished
1276 wakaba 1.29 executing<!-- XXX queue -->, <span>fire a simple event</span> called
1277     <code title=event-close>close</code> at the other port (the one whose
1278     owner is not the <code><a
1279     href="#workerglobalscope">WorkerGlobalScope</a></code> object on which
1280     the <code title=dom-WorkerGlobalScope-close><a
1281     href="#close">close()</a></code> method was called).
1282     </ol>
1283     </ol>
1284 wakaba 1.9
1285 wakaba 1.32 <p class="no-ja-translation">The following are the <span>event handler DOM attributes</span> that
1286 wakaba 1.22 must be supported by objects implementing the <code><a
1287     href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
1288    
1289     <dl>
1290 wakaba 1.32 <dt class="no-ja-translation"><dfn id=onclose
1291 wakaba 1.29 title=handler-WorkerGlobalScope-onclose><code>onclose</code></dfn>
1292 wakaba 1.11
1293     <dd>
1294 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Must be invoked whenever a <code title=event-close>close</code> event
1295     is targeted at or bubbles through the <code><a
1296     href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1297     </span><span class=ja-translation lang=ja><code title=event-close>close</code> 事象が当該 <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1298 wakaba 1.29 呼び出されなければなりません。</span></dl>
1299    
1300 wakaba 1.32 <h4 id=dedicated class="no-ja-translation"><span class=secno>2.1.2 </span>Dedicated workers and the
1301 wakaba 1.29 <code><a
1302     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1303     interface</h4>
1304    
1305     <pre
1306 wakaba 1.32 class="idl no-ja-translation">[NoInterfaceObject] interface <dfn id=dedicatedworkerglobalscope>DedicatedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1307 wakaba 1.30 void <a href="#postmessage" title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in DOMString message);
1308     void <a href="#postmessage" title=dom-DedicatedWorkerGlobalScope-postMessage>postMessage</a>(in DOMString message, in <span>MessagePort</span> messagePort);
1309 wakaba 1.29 <span>MessagePort</span> <a href="#startconversation" title=dom-DedicatedWorkerGlobalScope-startConversation>startConversation</a>(in DOMString message);
1310     attribute <span>EventListener</span> <a href="#onmessage" title=handler-DedicatedWorkerGlobalScope-onmessage>onmessage</a>;
1311     };</pre>
1312    
1313 wakaba 1.32 <p class="no-ja-translation"><code><a
1314 wakaba 1.29 href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1315     objects act as if they had an implicit <code>MessagePort</code> associated
1316     with them. This port is part of a channel that is set up when the worker
1317     is created, but it is not exposed. This object must never be garbage
1318     collected before the <code><a
1319     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1320     object.
1321    
1322 wakaba 1.32 <p class="no-ja-translation">All messages received by that port must immediately be retargetted at
1323 wakaba 1.29 the <code><a
1324     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1325     object.
1326    
1327 wakaba 1.32 <p class="no-ja-translation">The <dfn id=postmessage
1328 wakaba 1.29 title=dom-DedicatedWorkerGlobalScope-postMessage><code>postMessage()</code></dfn>
1329     and <dfn id=startconversation
1330     title=dom-DedicatedWorkerGlobalScope-startConversation><code>startConversation()</code></dfn>
1331     methods on <code><a
1332     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1333     objects must act as if, when invoked, they immediately invoked the method
1334 wakaba 1.30 of the same name on the port, with the same arguments, and returned the
1335     same return value.
1336 wakaba 1.29
1337 wakaba 1.32 <p class="no-ja-translation">The following are the <span>event handler DOM attributes</span> that
1338 wakaba 1.29 must be supported by objects implementing the <code><a
1339     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1340     interface:
1341    
1342     <dl>
1343 wakaba 1.32 <dt class="no-ja-translation"><dfn id=onmessage
1344 wakaba 1.29 title=handler-DedicatedWorkerGlobalScope-onmessage><code>onmessage</code></dfn>
1345 wakaba 1.11
1346     <dd>
1347 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Must be invoked whenever a <code
1348     title=event-DedicatedWorkerGlobalScope-message>message</code> event is
1349     targeted at or bubbles through the <code><a
1350     href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1351     object.
1352     </span><span class=ja-translation lang=ja><code title=event-DedicatedWorkerGlobalScope-message>message</code> 事象が当該 <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1353 wakaba 1.12 呼び出されなければなりません。</span></dl>
1354 wakaba 1.11
1355 wakaba 1.32 <h4 id=shared0 class="no-ja-translation"><span class=secno>2.1.3 </span>Shared workers and the
1356 wakaba 1.29 <code><a
1357     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1358     inteface</h4>
1359    
1360     <pre
1361 wakaba 1.32 class="idl no-ja-translation">[NoInterfaceObject] interface <dfn id=sharedworkerglobalscope>SharedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1362 wakaba 1.29 readonly attribute DOMString <a href="#name" title=dom-SharedWorkerGlobalScope-name>name</a>;
1363     attribute <span>EventListener</span> <a href="#onconnect" title=handler-SharedWorkerGlobalScope-onconnect>onconnect</a>;
1364     };</pre>
1365    
1366 wakaba 1.32 <p class="no-ja-translation">Shared workers receive message ports through <code
1367 wakaba 1.29 title=event-WorkerGlobalScope-connect>connect</code> events on their
1368     global object for each connection.
1369    
1370 wakaba 1.32 <p class="no-ja-translation">The <dfn id=name
1371 wakaba 1.29 title=dom-SharedWorkerGlobalScope-name><code>name</code></dfn> attribute
1372     must return the value it was assigned when the <code><a
1373     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object
1374     was created by the "<a href="#run-a">run a worker</a>" algorithm. Its
1375     value represents the name that can be used to obtain a reference to the
1376     worker using the <code><a href="#sharedworker">SharedWorker</a></code>
1377     constructor.
1378    
1379 wakaba 1.32 <p class="no-ja-translation">The following are the <span>event handler DOM attributes</span> that
1380 wakaba 1.29 must be supported by objects implementing the <code><a
1381     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1382     interface:
1383    
1384     <dl>
1385 wakaba 1.32 <dt class="no-ja-translation"><dfn id=onconnect
1386 wakaba 1.29 title=handler-SharedWorkerGlobalScope-onconnect><code>onconnect</code></dfn>
1387    
1388     <dd>
1389 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Must be invoked whenever a <code
1390     title=event-SharedWorkerGlobalScope-connect>connect</code> event is
1391     targeted at or bubbles through the <code><a
1392     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1393     object.
1394     </span><span class=ja-translation lang=ja><code title=event-SharedWorkerGlobalScope-connect>connect</code> 事象が当該 <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1395 wakaba 1.29 呼び出されなければなりません。</span></dl>
1396 wakaba 1.23
1397 wakaba 1.32 <h3 id=base-urls class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.2 </span>Base URLs and origins of
1398     workers</span><span class=ja-translation lang=ja><span class=secno>2.2 </span>労働者の基底 URL と起源</span></h3>
1399 wakaba 1.16
1400 wakaba 1.32 <p class="no-ja-translation">The <span>base URL</span> of a <span>URL</span> passed to an API in a
1401 wakaba 1.23 worker is the <span>absolute URL</span> given that the worker's <code
1402     title=dom-WorkerGlobalScope-location><a
1403     href="#location">location</a></code> attribute represents.
1404 wakaba 1.22
1405 wakaba 1.32 <p class="no-ja-translation">Both the <span>origin</span> and <span>effective script origin</span> of
1406 wakaba 1.22 scripts running in workers are the <span>origin</span> of the
1407 wakaba 1.23 <span>absolute URL</span> given that the worker's <code
1408     title=dom-WorkerGlobalScope-location><a
1409     href="#location">location</a></code> attribute represents.
1410 wakaba 1.22
1411 wakaba 1.32 <h3 id=the-queue class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.3 </span>The queue of events</span><span class=ja-translation lang=ja><span class=secno>2.3 </span>事象の待ち行列</span></h3>
1412 wakaba 1.22
1413 wakaba 1.32 <p class="no-ja-translation">Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1414 wakaba 1.22 object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
1415     initially empty.
1416 wakaba 1.9
1417 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>An event in the queue can be a DOM event or a timeout callback.
1418    
1419     </span><span class=ja-translation lang=ja>待ち行列中の事象は、 DOM 事象か小休止呼び戻しのいずれかです。</span><p class="no-ja-translation">All asynchronous callbacks and events that would be called or dispatched
1420 wakaba 1.31 in the worker must be added to the worker's queue, with the "<a
1421     href="#run-a">run a worker</a>" processing model below taking care of
1422     actually calling the callbacks or dispatching the events.
1423    
1424 wakaba 1.32 <p class="no-ja-translation">Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1425 wakaba 1.29 object also has a <dfn id=closing
1426     title=dom-WorkerGlobalScope-closing>closing</dfn> flag, which must
1427     initially be false, but which can get set to true by the algorithms in the
1428     processing model section below.
1429    
1430 wakaba 1.32 <p class="no-ja-translation">Once the <code><a
1431 wakaba 1.29 href="#workerglobalscope">WorkerGlobalScope</a></code>'s <a
1432     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1433     set to true, the queue must discard anything else that would be added to
1434     it. Effectively, once the <a href="#closing"
1435     title=dom-WorkerGlobalScope-closing>closing</a> flag is true, timers stop
1436     firing, notifications for all pending asynchronous operations are dropped,
1437     etc.
1438 wakaba 1.22
1439 wakaba 1.32 <h3 id=the-workers class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.4 </span>The worker's ports</span><span class=ja-translation lang=ja><span class=secno>2.4 </span>労働者のポート</span></h3>
1440 wakaba 1.11
1441 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Workers communicate with other workers and with <span title="browsing
1442     context">browsing contexts</span> through <span title="channel
1443     messaging">message channels</span> and their <code>MessagePort</code>
1444     objects.
1445    
1446     </span><span class=ja-translation lang=ja>労働者は、メッセージ通信路とその <code>MessagePort</code>
1447     オブジェクトを通じて、他の労働者や閲覧文脈と通信します。</span><p class="no-ja-translation">Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1448 wakaba 1.29 <var title="">worker global scope</var> has a list of <dfn
1449     id=the-workers0>the worker's ports</dfn>, which consists of all the
1450     <code>MessagePort</code> objects that are entangled with another port and
1451     that have one (but only one) port owned by <var title="">worker global
1452     scope</var>. This list includes all the <code>MessagePort</code> objects
1453     that are in events pending in the <a href="#queue">queue of events</a>, as
1454     well as the implicit <code>MessagePort</code> in the case of <a
1455     href="#dedicatedworkerglobalscope"
1456     title=DedicatedWorkerGlobalScope>dedicated workers</a>.
1457 wakaba 1.16
1458     <hr>
1459    
1460 wakaba 1.32 <p class="no-ja-translation">A worker is said to be a <dfn id=permissible>permissible worker</dfn> if
1461 wakaba 1.23 either:
1462    
1463     <ul>
1464 wakaba 1.32 <li class="no-ja-translation">at some point past or present a <code>MessagePort</code> owned by the
1465 wakaba 1.23 worker was entangled with a <code>MessagePort</code> <var
1466     title="">p</var> whose owner is a <code>Window</code> object whose
1467     <span>active document</span> is the <code>Document</code> that was that
1468     <span>browsing context</span>'s <span>active document</span> when <var
1469     title="">p</var> was created, and that <code>Document</code> is
1470     <span>fully active</span>, or
1471    
1472 wakaba 1.32 <li class="no-ja-translation">at some point past or present a <code>MessagePort</code> owned by the
1473 wakaba 1.23 worker was entangled with a <code>MessagePort</code> owned by another
1474     worker that is currently a <a href="#permissible">permissible worker</a>.
1475     </ul>
1476    
1477     <hr>
1478    
1479 wakaba 1.32 <p class="no-ja-translation">A worker is said to be a <dfn id=protected>protected worker</dfn> if
1480 wakaba 1.23 either:
1481    
1482     <ul>
1483 wakaba 1.32 <li class="no-ja-translation">it has outstanding timers, database transactions, or network
1484 wakaba 1.23 connections, and is a <a href="#permissible">permissible worker</a>, or:
1485    
1486 wakaba 1.32 <li class="no-ja-translation">there is a <a href="#protected">protected worker</a> that at some
1487 wakaba 1.23 point past or present owned a <code>MessagePort</code> that was entangled
1488     with a <code>MessagePort</code> owned by this worker.
1489     </ul>
1490    
1491     <hr>
1492    
1493 wakaba 1.32 <p class="no-ja-translation">A worker is said to be an <dfn id=active>active needed worker</dfn> if
1494 wakaba 1.23 either:
1495    
1496     <ul>
1497 wakaba 1.32 <li class="no-ja-translation">the worker is a <a href="#protected">protected worker</a>, or
1498 wakaba 1.23
1499 wakaba 1.32 <li class="no-ja-translation">at least one of the <a href="#the-workers0">the worker's ports</a> is
1500 wakaba 1.23 entangled with a <code>MessagePort</code> <var title="">p</var> whose
1501     owner is a <code>Window</code> object whose <span>active document</span>
1502     is the <code>Document</code> that was that <span>browsing
1503     context</span>'s <span>active document</span> when that
1504     <code>MessagePort</code> <var title="">p</var> was created, and that
1505     <code>Document</code> is <span>fully active</span>, or
1506 wakaba 1.22
1507 wakaba 1.32 <li class="no-ja-translation">at least one of the <a href="#the-workers0">the worker's ports</a> has
1508 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1509     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1510 wakaba 1.23 itself an <a href="#active">active needed worker</a>.
1511 wakaba 1.22 </ul>
1512    
1513     <hr>
1514    
1515 wakaba 1.32 <p class="no-ja-translation">A worker is said to be a <dfn id=suspendable>suspendable worker</dfn> if
1516 wakaba 1.23 it is not an <a href="#active">active needed worker</a> but either:
1517 wakaba 1.22
1518 wakaba 1.23 <ul>
1519 wakaba 1.32 <li class="no-ja-translation">at least one of the <a href="#the-workers0">the worker's ports</a> has
1520 wakaba 1.23 an entangled <code>MessagePort</code> owned by a <code>Window</code>
1521     object, or
1522    
1523 wakaba 1.32 <li class="no-ja-translation">at least one of the <a href="#the-workers0">the worker's ports</a> has
1524 wakaba 1.22 an entangled <code>MessagePort</code> owned by a <code><a
1525     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
1526 wakaba 1.23 itself a <span>needed worker</span>.
1527 wakaba 1.22 </ul>
1528 wakaba 1.16
1529 wakaba 1.32 <h3 id=processing class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.5 </span>Processing model</span><span class=ja-translation lang=ja><span class=secno>2.5 </span>処理モデル</span></h3>
1530 wakaba 1.12
1531 wakaba 1.32 <p class="no-ja-translation">When a user agent is to <dfn id=run-a>run a worker</dfn> for a script
1532 wakaba 1.29 with <span>URL</span> <var title="">url</var>, a browsing context <var
1533     title="">owner browsing context</var>, a <code>Document</code> <var
1534     title="">owner document</var>, and with global scope <var title="">worker
1535 wakaba 1.30 global scope</var>, it must run the following steps:
1536 wakaba 1.29
1537     <ol>
1538 wakaba 1.12 <li>
1539 wakaba 1.32 <p class="no-ja-translation">Create a completely separate and parallel execution environment (i.e.
1540 wakaba 1.30 a separate thread or process or equivalent construct), and run the rest
1541     of these steps asychronously in that context.</p>
1542    
1543     <li>
1544 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
1545     <var title="">url</var>.</span><span class=ja-translation lang=ja><var title="">url</var> で識別される資源を取って来ようと試みます。</span></p>
1546 wakaba 1.12
1547 wakaba 1.32 <p class="no-ja-translation">If the attempt fails, then, at the next available opportunity, after
1548 wakaba 1.29 any scripts have finished executing<!-- XXX queue -->, <span>fire a
1549     simple event</span> called <code title=event-error>error</code> at all
1550     <code><a href="#worker1">Worker</a></code> or <code><a
1551     href="#sharedworker">SharedWorker</a></code> objects associated with
1552     <var title="">worker global scope</var>. Abort these steps.</p>
1553 wakaba 1.12
1554 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>If the attempt succeeds, then let <var title="">script</var> be the
1555     resource that was obtained.</span><span class=ja-translation lang=ja>この試みが成功した場合、 <var title="">script</var>
1556 wakaba 1.12 を得られた資源とします。</span></p>
1557    
1558 wakaba 1.32 <p class="note has-ja-translation"><span class=en-original lang=en>As with <code>script</code> elements, the MIME type of the
1559     script is ignored. Unlike with <code>script</code> elements, there is no
1560     way to override the type. It's always assumed to be JavaScript.</span><span class=ja-translation lang=ja><code>script</code> 要素と同様に、スクリプトの MIME 型は無視します。
1561 wakaba 1.12 <code>script</code> 要素とは異なり、
1562     型を上書きする方法はありません。スクリプトは常に JavaScript
1563     と仮定します。</span></p>
1564 wakaba 1.11 <!-- XXX people will complain about
1565     this. I guess we might want to examine the MIME type... -->
1566    
1567    
1568     <li>
1569 wakaba 1.32 <p class="no-ja-translation">Create a new <code><a href="#workerlocation">WorkerLocation</a></code>
1570 wakaba 1.23 object for the <code title=dom-WorkerGlobalScope-location><a
1571 wakaba 1.29 href="#location">location</a></code> attribute of <var title="">worker
1572     global scope</var>, representing <var title="">url</var>.</p>
1573 wakaba 1.23
1574     <li>
1575 wakaba 1.32 <p class="no-ja-translation">Let <var title="">script</var>'s <span>script execution context</span>
1576 wakaba 1.29 (and thus also <span>global object</span>) be <var title="">worker
1577     global scope</var>.</p>
1578 wakaba 1.9
1579     <li>
1580 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Let <var title="">script</var>'s <span>script browsing context</span>
1581     be <var title="">owner browsing context</var>.</span><span class=ja-translation lang=ja><var title="">script</var> のスクリプト閲覧文脈を <var title="">owner browsing context</var>
1582 wakaba 1.12 とします。</span></p>
1583 wakaba 1.9
1584     <li>
1585 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Let <var title="">script</var>'s <span>script document context</span>
1586     be <var title="">owner document</var>.</span><span class=ja-translation lang=ja><var title="">script</var> のスクリプト文書文脈を <var title="">owner document</var>
1587 wakaba 1.12 とします。</span></p>
1588 wakaba 1.9
1589     <li>
1590 wakaba 1.32 <p class="no-ja-translation"><strong>Closing orphan workers</strong>: Start monitoring the worker
1591 wakaba 1.29 such that as soon as it stops being either an <a href="#active">active
1592     needed worker</a> or a <a href="#suspendable">suspendable worker</a>,
1593     the <var title="">worker global scope</var>'s <a href="#closing"
1594     title=dom-WorkerGlobalScope-closing>closing</a> flag is set to true and
1595     an event named <code title=event-close>close</code>, which uses the
1596 wakaba 1.25 <code>Event</code> object, which does not bubble, and which is not
1597 wakaba 1.29 cancelable, is added it to <var title="">worker global scope</var>'s
1598     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object's
1599     <a href="#queue">queue of events</a>, targetted at the <var
1600     title="">worker global scope</var> itself.</p>
1601    
1602     <li>
1603 wakaba 1.32 <p class="no-ja-translation"><strong>Suspending workers</strong>: Start monitoring the worker, such
1604 wakaba 1.29 that whenever the <var title="">worker global scope</var>'s <a
1605     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1606     false and the worker is a <a href="#suspendable">suspendable worker</a>,
1607     the user agent suspends execution of script in that worker until such
1608     time as either the <a href="#closing"
1609     title=dom-WorkerGlobalScope-closing>closing</a> flag switches to true or
1610     the worker stops being a <a href="#suspendable">suspendable worker</a>.</p>
1611    
1612     <li>
1613 wakaba 1.32 <p class="no-ja-translation">Run <var title="">script</var> until it either returns, fails to catch
1614 wakaba 1.29 an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
1615 wakaba 1.30 a worker</a>" or "<a href="#terminate">terminate a worker</a>"
1616     algorithms defined below.</p>
1617 wakaba 1.11
1618 wakaba 1.32 <p class="note no-ja-translation">If the script gets aborted by the "<a href="#kill-a">kill a
1619 wakaba 1.23 worker</a>" algorithm, then that same algorithm will cause there to only
1620     be a single event in the <a href="#queue">queue of events</a> at the
1621 wakaba 1.29 next step, namely the <code title=message-close>close</code> event. The
1622 wakaba 1.30 "<a href="#terminate">terminate a worker</a>" algorithm removes all the
1623     events.</p>
1624 wakaba 1.9
1625     <li>
1626 wakaba 1.32 <p class="no-ja-translation"><i>Event loop</i>: Wait until either there is an event in the <a
1627 wakaba 1.29 href="#queue">queue of events</a> associated with <var title="">worker
1628     global scope</var> or the <var title="">worker global scope</var>'s <a
1629     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1630     set to true.</p>
1631 wakaba 1.9
1632     <li>
1633 wakaba 1.32 <p class="no-ja-translation">Dispatch the oldest event or callback in the <a href="#queue">queue of
1634 wakaba 1.29 events</a>, if any. The handling of this event or the execution of this
1635     callback might get prematurely aborted by the "<a href="#kill-a">kill a
1636 wakaba 1.30 worker</a>" or "<a href="#terminate">terminate a worker</a>" algorithms
1637     defined below.</p>
1638 wakaba 1.9
1639     <li>
1640 wakaba 1.32 <p class="no-ja-translation">If there are any more events in the <a href="#queue">queue of
1641 wakaba 1.29 events</a> or if the <var title="">worker global scope</var>'s <a
1642     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag is
1643     set to false, then jump back to the step above labeled <i>event
1644     loop</i>.</p>
1645 wakaba 1.9
1646     <li>
1647 wakaba 1.32 <p class="big-issue no-ja-translation">timers, intervals, XMLHttpRequests, database
1648 wakaba 1.23 transactions, etc, must be killed; ports must be unentangled</p>
1649 wakaba 1.25
1650     <li>
1651 wakaba 1.32 <p class="no-ja-translation">At the next available opportunity, after any scripts have finished
1652 wakaba 1.25 executing<!-- XXX queue -->, <span>fire a simple event</span> called
1653 wakaba 1.29 <code title=event-close>close</code> at all <code><a
1654     href="#worker1">Worker</a></code> or <code><a
1655     href="#sharedworker">SharedWorker</a></code> objects associated with
1656     this worker.</p>
1657 wakaba 1.9 </ol>
1658    
1659     <hr>
1660    
1661 wakaba 1.32 <p class="no-ja-translation">When a user agent is to <dfn id=kill-a>kill a worker</dfn> it must run
1662 wakaba 1.29 the following steps in parallel with the worker's main loop (the "<a
1663     href="#run-a">run a worker</a>" processing model defined above):
1664    
1665     <ol>
1666 wakaba 1.14 <li>
1667 wakaba 1.32 <p class="no-ja-translation">Create an <code>Event</code> object with the event name <code
1668 wakaba 1.29 title=event-close>close</code>, which does not bubble and is not
1669 wakaba 1.22 cancelable, and add it to the worker's <code><a
1670     href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1671     href="#queue">queue of events</a>, targetted at the <code><a
1672     href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1673 wakaba 1.23 </li>
1674     <!-- XXX shouldn't add one if closing is
1675 wakaba 1.29 already true, assuming close has already been added to the queue
1676 wakaba 1.23 (?) -->
1677 wakaba 1.22
1678     <li>
1679 wakaba 1.32 <p class="no-ja-translation">Set the worker's <code><a
1680 wakaba 1.29 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1681     href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1682     true.
1683 wakaba 1.22
1684     <li>
1685 wakaba 1.32 <p class="no-ja-translation">Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
1686 wakaba 1.23 a worker</a>" processing model defined above immediately starts running
1687 wakaba 1.29 event listeners registered for the <code title=event-close>close</code>
1688     event, this time should not be zero &mdash; the idea is that the <code
1689     title=event-close>close</code> event can be used to clean up when
1690     shutting down unexpectedly.
1691    
1692     <li>
1693 wakaba 1.32 <p class="no-ja-translation">If there are any events in the <a href="#queue">queue of events</a>
1694 wakaba 1.29 other than the <code title=event-close>close</code> event that this
1695     algorithm just added, discard them without dispatching them.
1696    
1697     <li>
1698 wakaba 1.32 <p class="no-ja-translation">If the <code title=event-close>close</code> event that this algorithm
1699 wakaba 1.29 just added hasn't yet been dispatched, then abort the script currently
1700     running in the worker.
1701 wakaba 1.23
1702     <li>
1703 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Wait a user-agent-defined amount of time.
1704    
1705     </span><span class=ja-translation lang=ja>利用者エージェント定義の時間、待ちます。</span><li>
1706     <p class="no-ja-translation">Abort the script currently running in the worker (if any script is
1707 wakaba 1.29 running, then it will be a handler for the <code
1708     title=event-close>close</code> event).
1709     </ol>
1710 wakaba 1.9
1711 wakaba 1.32 <p class="no-ja-translation">User agents may invoke the "<a href="#kill-a">kill a worker</a>"
1712 wakaba 1.22 processing model on a worker at any time, e.g. in response to user
1713     requests, in response to CPU quota management, or when a worker stops
1714 wakaba 1.23 being an <a href="#active">active needed worker</a> if the worker
1715 wakaba 1.29 continues executing even after its <a href="#closing"
1716     title=dom-WorkerGlobalScope-closing>closing</a> flag was set to true.
1717 wakaba 1.16
1718 wakaba 1.11 <hr>
1719    
1720 wakaba 1.32 <p class="no-ja-translation">When a user agent is to <dfn id=terminate>terminate a worker</dfn> it
1721 wakaba 1.30 must run the following steps in parallel with the worker's main loop (the
1722     "<a href="#run-a">run a worker</a>" processing model defined above):
1723 wakaba 1.22
1724     <ol>
1725     <li>
1726 wakaba 1.32 <p class="no-ja-translation">Set the worker's <code><a
1727 wakaba 1.22 href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1728 wakaba 1.29 href="#closing" title=dom-WorkerGlobalScope-closing>closing</a> flag to
1729     true.
1730 wakaba 1.22
1731     <li>
1732 wakaba 1.32 <p class="no-ja-translation">If there are any events in the <a href="#queue">queue of events</a>
1733 wakaba 1.29 other than the <code title=event-close>close</code> event that this
1734     algorithm just added, discard them without dispatching them.
1735 wakaba 1.22
1736     <li>
1737 wakaba 1.32 <p class="no-ja-translation">Abort the script currently running in the worker.
1738 wakaba 1.11 </ol>
1739    
1740 wakaba 1.32 <h3 id=creating class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.6 </span>Creating workers</span><span class=ja-translation lang=ja><span class=secno>2.6 </span>労働者の作成</span></h3>
1741 wakaba 1.9
1742 wakaba 1.32 <h4 id=the-abstractworker class="has-ja-translation"><span class=en-original lang=en><span class=secno>2.6.1 </span>The <code><a
1743     href="#abstractworker">AbstractWorker</a></code> abstract interface</span><span class=ja-translation lang=ja><span class=secno>2.6.1 </span><code><a href="#abstractworker">AbstractWorker</a></code> abstract 界面</span></h4>
1744 wakaba 1.29
1745 wakaba 1.9 <pre
1746 wakaba 1.32 class="idl no-ja-translation">[NoInterfaceObject] interface <dfn id=abstractworker>AbstractWorker</dfn> {
1747 wakaba 1.29 attribute <span>EventListener</span> <a href="#onerror" title=handler-AbstractWorker-onerror>onerror</a>;
1748     attribute <span>EventListener</span> <a href="#onclose0" title=handler-AbstractWorker-onclose>onclose</a>;
1749     };</pre>
1750    
1751 wakaba 1.32 <p class="no-ja-translation">The following are the <span>event handler DOM attributes</span> that
1752 wakaba 1.29 must be supported by objects implementing the <code><a
1753     href="#abstractworker">AbstractWorker</a></code> interface:
1754    
1755     <dl>
1756 wakaba 1.32 <dt class="no-ja-translation"><dfn id=onerror
1757 wakaba 1.29 title=handler-AbstractWorker-onerror><code>onerror</code></dfn>
1758    
1759     <dd>
1760 wakaba 1.32 <p class="no-ja-translation">Must be invoked whenever an <code title=event-error>error</code> event
1761 wakaba 1.29 is targeted at or bubbles through the <code><a
1762     href="#abstractworker">AbstractWorker</a></code> object.
1763    
1764 wakaba 1.32 <dt class="no-ja-translation"><dfn id=onclose0
1765 wakaba 1.29 title=handler-AbstractWorker-onclose><code>onclose</code></dfn>
1766    
1767     <dd>
1768 wakaba 1.32 <p class="no-ja-translation">Must be invoked whenever an <code title=event-close>close</code> event
1769 wakaba 1.29 is targeted at or bubbles through the <code><a
1770     href="#abstractworker">AbstractWorker</a></code> object.
1771     </dl>
1772 wakaba 1.25
1773 wakaba 1.32 <h4 id=dedicated0 class="no-ja-translation"><span class=secno>2.6.2 </span>Dedicated workers and the
1774 wakaba 1.29 <code><a href="#worker1">Worker</a></code> interface</h4>
1775 wakaba 1.25
1776 wakaba 1.32 <pre class="idl no-ja-translation">[NoInterfaceObject,
1777 wakaba 1.29 <a href="#worker2" title=dom-Worker>Constructor</a>(in DOMString scriptURL)]
1778     interface <dfn id=worker1>Worker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1779     void <a href="#close0" title=dom-Worker-close>close</a>();
1780    
1781 wakaba 1.30 void <a href="#postmessage0" title=dom-Worker-postMessage>postMessage</a>(in DOMString message);
1782     void <a href="#postmessage0" title=dom-Worker-postMessage>postMessage</a>(in DOMString message, in <span>MessagePort</span> messagePort);
1783 wakaba 1.29 <span>MessagePort</span> <a href="#startconversation0" title=dom-Worker-startConversation>startConversation</a>(in DOMString message);
1784     attribute <span>EventListener</span> <a href="#onmessage0" title=handler-Worker-onmessage>onmessage</a>;
1785 wakaba 1.9 };</pre>
1786    
1787 wakaba 1.32 <p class="no-ja-translation">The <dfn id=close0 title=dom-Worker-close><code>close()</code></dfn>
1788 wakaba 1.30 method, when invoked, must cause the "<a href="#terminate">terminate a
1789     worker</a>" algorithm to be run on the worker with with the object is
1790     associated.
1791 wakaba 1.29
1792 wakaba 1.32 <p class="no-ja-translation"><code><a href="#worker1">Worker</a></code> objects act as if they had an
1793 wakaba 1.29 implicit <code>MessagePort</code> associated with them. This port is part
1794     of a channel that is set up when the worker is created, but it is not
1795     exposed. This object must never be garbage collected before the <code><a
1796     href="#worker1">Worker</a></code> object.
1797    
1798 wakaba 1.32 <p class="no-ja-translation">All messages received by that port must immediately be retargetted at
1799 wakaba 1.29 the <code><a href="#worker1">Worker</a></code> object.
1800    
1801 wakaba 1.32 <p class="no-ja-translation">The <dfn id=postmessage0
1802 wakaba 1.29 title=dom-Worker-postMessage><code>postMessage()</code></dfn> and <dfn
1803     id=startconversation0
1804     title=dom-Worker-startConversation><code>startConversation()</code></dfn>
1805     methods on <code><a href="#worker1">Worker</a></code> objects must act as
1806     if, when invoked, they immediately invoked the method of the same name on
1807 wakaba 1.30 the port, with the same arguments, and returned the same return value.
1808 wakaba 1.29
1809 wakaba 1.32 <p class="no-ja-translation">The following are the <span>event handler DOM attributes</span> that
1810 wakaba 1.29 must be supported by objects implementing the <code><a
1811     href="#worker1">Worker</a></code> interface:
1812    
1813     <dl>
1814 wakaba 1.32 <dt class="no-ja-translation"><dfn id=onmessage0
1815 wakaba 1.29 title=handler-Worker-onmessage><code>onmessage</code></dfn>
1816    
1817     <dd>
1818 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Must be invoked whenever a <code
1819     title=event-Worker-message>message</code> event is targeted at or
1820     bubbles through the <code><a href="#worker1">Worker</a></code> object.
1821     </span><span class=ja-translation lang=ja><code title=event-Worker-message>message</code> 事象が当該 <code><a href="#worker1">Worker</a></code> オブジェクトを対象としているか、泡立って当該オブジェクトを通過する時に、
1822 wakaba 1.29 呼び出されなければなりません。</span></dl>
1823 wakaba 1.22
1824     <hr>
1825 wakaba 1.13
1826 wakaba 1.32 <p class="no-ja-translation">When the <dfn id=worker2 title=dom-Worker><code>Worker(<var
1827 wakaba 1.29 title="">scriptURL</var>)</code></dfn> constructor is invoked, the user
1828     agent must run the following steps:
1829    
1830     <ol>
1831 wakaba 1.13 <li>
1832 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><span title="resolve a url">Resolve</span> the <var
1833     title="">scriptURL</var> argument.
1834    
1835     </span><span class=ja-translation lang=ja><var
1836 wakaba 1.15 title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1837 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1838    
1839     </span><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1840 wakaba 1.15 例外を投げます。</span><li>
1841 wakaba 1.32 <p class="no-ja-translation">If the <span>origin</span> of the resulting <span>absolute URL</span>
1842 wakaba 1.29 is not the <span title="same origin">same</span> as the origin of the
1843     script that invoked the constructor, then throw a <span>security
1844     exception</span>.
1845    
1846     <li>
1847 wakaba 1.32 <p class="no-ja-translation"><span>Create a new <code><a
1848 wakaba 1.29 href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1849     object</span>. Let this be the <var title="">worker global scope</var>.
1850    
1851     <li>
1852 wakaba 1.32 <p class="no-ja-translation">Create a new <code><a href="#worker1">Worker</a></code> object,
1853 wakaba 1.29 associated with <var title="">worker global scope</var>. Let this
1854     <code><a href="#worker1">Worker</a></code> object be <var
1855     title="">worker</var>.
1856    
1857     <li>
1858 wakaba 1.32 <p class="no-ja-translation"><span>Create a <code>MessagePort</code> object</span> owned by the
1859 wakaba 1.29 <span>script execution context</span> of the script that invoked the
1860     method. Let this be the <var title="">outside port</var>.
1861    
1862     <li>
1863 wakaba 1.32 <p class="no-ja-translation">Associate the <var title="">outside port</var> with <var
1864 wakaba 1.29 title="">worker</var>.
1865    
1866     <li>
1867 wakaba 1.32 <p class="no-ja-translation"><span>Create a <code>MessagePort</code> object</span> owned by the
1868 wakaba 1.29 <var title="">worker global scope</var>. Let this be the <var
1869     title="">inside port</var>.
1870    
1871     <li>
1872 wakaba 1.32 <p class="no-ja-translation">Associate the <var title="">inside port</var> with <var
1873 wakaba 1.29 title="">worker global scope</var>.
1874    
1875     <li>
1876 wakaba 1.32 <p class="no-ja-translation"><span>Entangle</span> <var title="">outside port</var> and <var
1877 wakaba 1.29 title="">inside port</var>.
1878    
1879     <li>
1880 wakaba 1.32 <p class="no-ja-translation">Return <var title="">worker</var>, and run the following steps
1881 wakaba 1.29 asynchronously.
1882    
1883     <li>
1884 wakaba 1.32 <p class="no-ja-translation">Open <var title="">inside port</var>'s <span>port message
1885 wakaba 1.29 queue</span>.
1886    
1887     <li>
1888 wakaba 1.32 <p class="no-ja-translation">Open <var title="">outside port</var>'s <span>port message
1889 wakaba 1.30 queue</span>.
1890    
1891     <li>
1892 wakaba 1.32 <p class="no-ja-translation"><a href="#run-a">Run a worker</a>, with the <span>script browsing
1893 wakaba 1.30 context</span> of the script that invoked the method as the <var
1894     title="">owner browsing context</var>, with the <span>script document
1895     context</span> of the script that invoked the method as the <var
1896     title="">owner document</var>, and with <var title="">worker global
1897     scope</var> as the global scope.</p>
1898 wakaba 1.25 </ol>
1899 wakaba 1.11
1900 wakaba 1.32 <h4 id=shared1 class="no-ja-translation"><span class=secno>2.6.3 </span>Shared workers and the
1901 wakaba 1.29 <code><a href="#sharedworker">SharedWorker</a></code> interface</h4>
1902    
1903 wakaba 1.32 <pre class="idl no-ja-translation">[NoInterfaceObject,
1904 wakaba 1.29 <a href="#sharedworker0" title=dom-SharedWorker>Constructor</a>(in DOMString scriptURL, in DOMString name)]
1905     interface <dfn id=sharedworker>SharedWorker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1906     readonly attribute <code>MessagePort</code> <a href="#port" title=dom-SharedWorker-port>port</a>;
1907     };</pre>
1908    
1909 wakaba 1.32 <p class="no-ja-translation">The <dfn id=port title=dom-SharedWorker-port><code>port</code></dfn>
1910 wakaba 1.29 attribute must return the value it was assigned by the object's
1911     constructor. It represents the <code>MessagePort</code> for communicating
1912     with the shared worker.
1913    
1914 wakaba 1.32 <p class="no-ja-translation">When the <dfn id=sharedworker0
1915 wakaba 1.29 title=dom-SharedWorker><code>SharedWorker(<var title="">scriptURL</var>,
1916     <var title="">name</var>)</code></dfn> constructor is invoked, the user
1917     agent must run the following steps:
1918 wakaba 1.11
1919 wakaba 1.29 <ol>
1920 wakaba 1.13 <li>
1921 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en><span title="resolve a url">Resolve</span> the <var
1922     title="">scriptURL</var> argument.
1923    
1924     </span><span class=ja-translation lang=ja><var
1925 wakaba 1.15 title="">scriptURL</var> 引数を<span title="resolve a url">解決</span>します。</span><li>
1926 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>If this fails, throw a <code>SYNTAX_ERR</code> exception.
1927    
1928     </span><span class=ja-translation lang=ja>これが失敗した場合、 <code>SYNTAX_ERR</code>
1929 wakaba 1.15 例外を投げます。</span><li>
1930 wakaba 1.32 <p class="no-ja-translation">If the <span>origin</span> of the resulting <span>absolute URL</span>
1931 wakaba 1.29 is not the <span title="same origin">same</span> as the origin of the
1932     script that invoked the constructor, then throw a <span>security
1933     exception</span>.
1934    
1935     <li>
1936 wakaba 1.32 <p class="no-ja-translation">Execute the following substeps atomically:</p>
1937 wakaba 1.13
1938     <ol>
1939     <li>
1940 wakaba 1.32 <p class="no-ja-translation">Create a new <code><a href="#sharedworker">SharedWorker</a></code>
1941 wakaba 1.29 object, which will shortly be associated with a <code><a
1942     href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1943     object. Let this <code><a href="#sharedworker">SharedWorker</a></code>
1944     object be <var title="">worker</var>.
1945 wakaba 1.22
1946     <li>
1947 wakaba 1.32 <p class="no-ja-translation"><span>Create a <code>MessagePort</code> object</span> owned by the
1948 wakaba 1.29 <span>script execution context</span> of the script that invoked the
1949     method. Let this be the <var title="">outside port</var>.
1950 wakaba 1.25
1951     <li>
1952 wakaba 1.32 <p class="no-ja-translation">Assign <var title="">outside port</var> to the <code
1953 wakaba 1.29 title=dom-SharedWorker-port><a href="#port">port</a></code> attribute
1954     of <var title="">worker</var>.
1955 wakaba 1.25
1956     <li>
1957 wakaba 1.32 <p class="no-ja-translation">If there exists a <code><a
1958 wakaba 1.29 href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1959     object whose <a href="#closing"
1960     title=dom-WorkerGlobalScope-closing>closing</a> flag is false, whose
1961     <code title=dom-WorkerGlobalScope-name>name</code> attribute is
1962     exactly equal to the <var title="">name</var> argument, and whose
1963     <code title=dom-WorkerGlobalScope-location><a
1964     href="#location">location</a></code> attribute represents an
1965     <span>absolute URL</span> that has the <span>same origin</span> as the
1966     resulting <span>absolute URL</span>, then run these substeps:</p>
1967 wakaba 1.25
1968 wakaba 1.29 <ol>
1969     <li>
1970 wakaba 1.32 <p class="no-ja-translation">Let <var title="">worker global scope</var> be that <code><a
1971 wakaba 1.29 href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1972     object.
1973    
1974     <li>
1975 wakaba 1.32 <p class="no-ja-translation">If <var title="">worker global scope</var>'s <code
1976 wakaba 1.29 title=dom-WorkerGlobalScope-location><a
1977     href="#location">location</a></code> attribute represents an
1978     <span>absolute URL</span> that is not exactly equal to the resulting
1979     <span>absolute URL</span>, then throw a
1980     <code>URL_MISMATCH_ERR</code> exception and abort all these steps.
1981     <span class=big-issue>code 21</span>
1982    
1983     <li>
1984 wakaba 1.32 <p class="no-ja-translation">Associate <var title="">worker</var> with <var title="">worker
1985 wakaba 1.29 global scope</var>.
1986    
1987     <li>
1988 wakaba 1.32 <p class="no-ja-translation"><span>Create a <code>MessagePort</code> object</span> owned by the
1989 wakaba 1.29 <var title="">worker global scope</var>. Let this be the <var
1990     title="">inside port</var>.
1991    
1992     <li>
1993 wakaba 1.32 <p class="no-ja-translation"><span>Entangle</span> <var title="">outside port</var> and <var
1994 wakaba 1.29 title="">inside port</var>.
1995    
1996     <li>
1997 wakaba 1.32 <p class="no-ja-translation">Return <var title="">worker</var> and perform the next step
1998 wakaba 1.29 asynchronously.
1999    
2000     <li>
2001 wakaba 1.32 <p class="no-ja-translation">Create an event that uses the <code>MessageEvent</code> interface,
2002 wakaba 1.29 with the name <code title=event-connect>connect</code>, which does
2003     not bubble, is cancelable, has no default action, has a <code
2004     title=dom-MessageEvent-data>data</code> attribute whose value is the
2005     empty string and has a <code
2006     title=dom-MessageEvent-messagePort>messagePort</code> attribute
2007     whose value is the newly created port, and add it to <var
2008     title="">worker global scope</var>'s <a href="#queue">queue of
2009     events</a>, targetted at the <var title="">worker global scope</var>
2010     itself.
2011    
2012     <li>
2013 wakaba 1.32 <p class="no-ja-translation">Abort all these steps.
2014 wakaba 1.29 </ol>
2015 wakaba 1.11
2016 wakaba 1.29 <li>
2017 wakaba 1.32 <p class="no-ja-translation"><span>Create a new <code><a
2018 wakaba 1.29 href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
2019     object</span>. Let this be the <var title="">worker global
2020     scope</var>.
2021 wakaba 1.11
2022 wakaba 1.29 <li>
2023 wakaba 1.32 <p class="no-ja-translation">Associate <var title="">worker</var> with <var title="">worker
2024 wakaba 1.29 global scope</var>.
2025 wakaba 1.23
2026 wakaba 1.29 <li>
2027 wakaba 1.32 <p class="no-ja-translation">Set the <code title=dom-SharedWorkerGlobalScope-name><a
2028 wakaba 1.29 href="#name">name</a></code> attribute of <var title="">worker global
2029     scope</var> be <var title="">name</var>.
2030 wakaba 1.11
2031 wakaba 1.29 <li>
2032 wakaba 1.32 <p class="no-ja-translation"><span>Create a <code>MessagePort</code> object</span> owned by the
2033 wakaba 1.29 <var title="">worker global scope</var>. Let this be the <var
2034     title="">inside port</var>.
2035 wakaba 1.11
2036 wakaba 1.29 <li>
2037 wakaba 1.32 <p class="no-ja-translation"><span>Entangle</span> <var title="">outside port</var> and <var
2038 wakaba 1.29 title="">inside port</var>.
2039     </ol>
2040 wakaba 1.13
2041     <li>
2042 wakaba 1.32 <p class="no-ja-translation">Return <var title="">worker</var> and perform the next step
2043 wakaba 1.29 asynchronously.
2044 wakaba 1.25
2045     <li>
2046 wakaba 1.32 <p class="no-ja-translation">Create an event that uses the <code>MessageEvent</code> interface,
2047 wakaba 1.22 with the name <code title=event-connect>connect</code>, which does not
2048     bubble, is cancelable, has no default action, has a <code
2049     title=dom-MessageEvent-data>data</code> attribute whose value is the
2050     empty string and has a <code
2051     title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
2052 wakaba 1.29 value is the newly created port, and add it to <var title="">worker
2053     global scope</var>'s <a href="#queue">queue of events</a>, targetted at
2054     the <var title="">worker global scope</var> itself.
2055 wakaba 1.23
2056     <li>
2057 wakaba 1.32 <p class="no-ja-translation"><a href="#run-a">Run a worker</a>, with the <span>script browsing
2058 wakaba 1.30 context</span> of the script that invoked the method as the <var
2059     title="">owner browsing context</var>, with the <span>script document
2060     context</span> of the script that invoked the method as the <var
2061     title="">owner document</var>, and with <var title="">worker global
2062     scope</var> as the global scope.</p>
2063 wakaba 1.22 </ol>
2064 wakaba 1.15
2065 wakaba 1.32 <h2 id=apis-available class="has-ja-translation"><span class=en-original lang=en><span class=secno>3. </span>APIs available to workers</span><span class=ja-translation lang=ja><span class=secno>3. </span>労働者が利用可能な API</span></h2>
2066 wakaba 1.15
2067 wakaba 1.22 <pre
2068 wakaba 1.32 class="idl no-ja-translation">[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
2069 wakaba 1.25 void <a href="#importscripts" title=dom-WorkerGlobalScope-importScripts>importScripts</a>([Variadic] in DOMString urls);
2070 wakaba 1.22 readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
2071     <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
2072     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
2073     void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
2074     };</pre>
2075 wakaba 1.16
2076 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Objects that implement the <code><a
2077     href="#workerglobalscope">WorkerGlobalScope</a></code> interface must also
2078     implement the <code><a href="#workerutils">WorkerUtils</a></code>
2079     interface.
2080    
2081     </span><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 no-ja-translation">Need to define a sync database API.
2082 wakaba 1.16
2083 wakaba 1.32 <p class="big-issue no-ja-translation">May need to define a browser sniffing API (like
2084 wakaba 1.16 window.navigator).</p>
2085     <!-- XXX ApplicationCache -->
2086     <!-- XXX a way to set cookies on the URL for the script -->
2087     <!-- XXX debugging: void log(in DOMString s); // log to console -->
2088     <!-- XXX debugging: onerror -->
2089    
2090     <hr>
2091    
2092 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects,
2093     etc) are not available to workers in this version of this specification.
2094    
2095     </span><span class=ja-translation lang=ja>DOM API (<code>Node</code> オブジェクト、 <code>Document</code>
2096     オブジェクト、その他) は、この版のこの仕様書では労働者に利用可能ではありません。</span><h3 id=importing class="no-ja-translation"><span class=secno>3.1 </span>Importing scripts and
2097 wakaba 1.22 libraries</h3>
2098    
2099 wakaba 1.32 <p class="no-ja-translation">When a script invokes the <dfn id=importscripts
2100 wakaba 1.25 title=dom-WorkerGlobalScope-importScripts><code>importScripts(<var
2101     title="">urls</var>)</code></dfn> method on a <code><a
2102 wakaba 1.22 href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
2103     agent must run the following steps:
2104    
2105     <ol>
2106     <li>
2107 wakaba 1.32 <p class="no-ja-translation">If there are no arguments, return without doing anything. Abort these
2108 wakaba 1.25 steps.
2109    
2110     <li>
2111 wakaba 1.32 <p class="no-ja-translation"><span title="resolve a url">Resolve</span> each argument.
2112 wakaba 1.22
2113 wakaba 1.25 <li>
2114 wakaba 1.32 <p class="no-ja-translation">If any fail, throw a <code>SYNTAX_ERR</code> exception.
2115 wakaba 1.22
2116 wakaba 1.25 <li>
2117 wakaba 1.32 <p class="no-ja-translation">If any of the resulting <span title="absolute URL">absolute
2118 wakaba 1.25 URLs</span> have an <span>origin</span> that is not the <span
2119     title="same origin">same</span> as the origin of the script that invoked
2120     the method, then throw a <span>security exception</span>.
2121 wakaba 1.22
2122 wakaba 1.25 <li>
2123 wakaba 1.32 <p class="no-ja-translation">Attempt to <span>fetch</span> each resource identified by the
2124 wakaba 1.25 resulting <span title="absolute URLs">absolute URL</span>.</p>
2125 wakaba 1.22
2126     <li>
2127 wakaba 1.32 <p class="no-ja-translation">For each argument in turn, in the order given, starting with the first
2128 wakaba 1.25 one, run these substeps:</p>
2129    
2130     <ol>
2131     <li>
2132 wakaba 1.32 <p class="no-ja-translation">Wait for the fetching attempt for the corresponding resource to
2133 wakaba 1.25 complete.</p>
2134    
2135 wakaba 1.32 <p class="no-ja-translation">If the fetching attempt failed, throw a <code>NETWORK_ERR</code>
2136 wakaba 1.26 exception and abort all these steps. <a
2137     href="#refsXHR">[XHR]</a><!-- XXX get DOM Core updated instead
2138     --></p>
2139 wakaba 1.25
2140 wakaba 1.32 <p class="no-ja-translation">If the fetching attempt succeeded, then let <var
2141 wakaba 1.25 title="">script</var> be the resource that was obtained.</p>
2142    
2143 wakaba 1.32 <p class="note no-ja-translation">As with the worker's script, the script here is always
2144 wakaba 1.25 assumed to be JavaScript, regardless of the MIME type.</p>
2145     <!-- XXX -->
2146    
2147     <li>
2148 wakaba 1.32 <p class="no-ja-translation">Let <var title="">script</var>'s <span>script execution
2149 wakaba 1.31 context</span>, <span>script browsing context</span>, and <span>script
2150     document context</span> be the same as for the script that was
2151     executed by the <a href="#run-a">run a worker</a> processing model for
2152     this worker.</p>
2153 wakaba 1.22
2154 wakaba 1.25 <li>
2155 wakaba 1.32 <p class="no-ja-translation">Run <var title="">script</var> until it either returns, fails to
2156 wakaba 1.25 catch an exception, or gets prematurely aborted by the "<a
2157 wakaba 1.30 href="#kill-a">kill a worker</a>" or "<a href="#terminate">terminate a
2158     worker</a>" algorithms defined above.</p>
2159 wakaba 1.25
2160 wakaba 1.32 <p class="no-ja-translation">If an exception was raised or if the script was prematurely aborted,
2161 wakaba 1.25 then abort all these steps, letting the exception or aborting continue
2162     to be processed by the script that called the <code
2163     title=dom-WorkerGlobalScope-importScripts><a
2164     href="#importscripts">importScripts()</a></code> method.</p>
2165 wakaba 1.22
2166 wakaba 1.32 <p class="no-ja-translation">If the "<a href="#kill-a">kill a worker</a>" or "<a
2167 wakaba 1.30 href="#terminate">terminate a worker</a>" algoriths abort the script
2168     then abort all these steps.</p>
2169 wakaba 1.25 </ol>
2170 wakaba 1.22 </ol>
2171    
2172 wakaba 1.32 <h3 id=apis-defined class="no-ja-translation"><span class=secno>3.2 </span>APIs defined in other
2173 wakaba 1.22 specifications</h3>
2174    
2175 wakaba 1.32 <p class="no-ja-translation">The <dfn id=localstorage
2176 wakaba 1.22 title=dom-localStorage><code>localStorage</code></dfn>, <dfn
2177     id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
2178     must act as defined for the APIs with the same names on the
2179     <code>Window</code> object in the HTML5 specification, with the exception
2180     that where the API would use the <span>origin</span> of the <span>active
2181     document</span> of the <span>browsing context</span> of the
2182     <code>Window</code> object on which the method was supposedly invoked, it
2183     must instead use the <span>origin</span> of the script that invoked the
2184     method. <a href="#refsHTML5">[HTML5]</a>
2185    
2186 wakaba 1.32 <p class="no-ja-translation">The <dfn id=shownotification
2187 wakaba 1.22 title=dom-showNotification><code>showNotification()</code></dfn> methods
2188     must act as defined for the APIs with the same names on the
2189     <code>Window</code> object in the HTML5 specification. <a
2190     href="#refsHTML5">[HTML5]</a>
2191    
2192 wakaba 1.32 <h3 id=interface class="no-ja-translation"><span class=secno>3.3 </span>Interface objects and
2193 wakaba 1.22 constructors</h3>
2194    
2195 wakaba 1.32 <p class="no-ja-translation">There must be no interface objects and constructors available in the
2196 wakaba 1.22 global scope of scripts whose <span>script execution context</span> is a
2197     <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
2198     except for the following:
2199    
2200     <ul>
2201     <li>
2202 wakaba 1.32 <p class="no-ja-translation"><code>XMLHttpRequest</code> and all interface objects and constructors
2203 wakaba 1.31 defined by the XMLHttpRequest specifications, except that the
2204     <span>document response entity body</span> must always be null. <a
2205     href="#refsXHR">[XHR]</a>
2206    
2207     <li>
2208 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>The <code>WebSocket</code> interface object and constructor.
2209 wakaba 1.23
2210 wakaba 1.32 </span><span class=ja-translation lang=ja><code>WebSocket</code> 界面オブジェクトと構築器</span><li>
2211     <p class="has-ja-translation"><span class=en-original lang=en>The <code>MessageChannel</code> interface object and constructor.
2212     </span><span class=ja-translation lang=ja><code>MessageChannel</code> 界面オブジェクトと構築器</span></ul>
2213    
2214     <h3 id=worker0 class="no-ja-translation"><span class=secno>3.4 </span>Worker locations</h3>
2215 wakaba 1.23
2216     <pre
2217 wakaba 1.32 class="idl no-ja-translation">[NoInterfaceObject] interface <dfn id=workerlocation>WorkerLocation</dfn> {
2218 wakaba 1.23 readonly attribute DOMString <a href="#href" title=dom-WorkerLocation-href>href</a>;
2219     readonly attribute DOMString <a href="#protocol" title=dom-WorkerLocation-protocol>protocol</a>;
2220     readonly attribute DOMString <a href="#host" title=dom-WorkerLocation-host>host</a>;
2221     readonly attribute DOMString <a href="#hostname" title=dom-WorkerLocation-hostname>hostname</a>;
2222 wakaba 1.29 readonly attribute DOMString <a href="#port0" title=dom-WorkerLocation-port>port</a>;
2223 wakaba 1.23 readonly attribute DOMString <a href="#pathname" title=dom-WorkerLocation-pathname>pathname</a>;
2224     readonly attribute DOMString <a href="#search" title=dom-WorkerLocation-search>search</a>;
2225     readonly attribute DOMString <a href="#hash" title=dom-WorkerLocation-hash>hash</a>;
2226     };</pre>
2227    
2228 wakaba 1.32 <p class="no-ja-translation">A <code><a href="#workerlocation">WorkerLocation</a></code> object
2229 wakaba 1.23 represents an <span>absolute URL</span> set at its creation.
2230    
2231 wakaba 1.32 <p class="no-ja-translation">The <dfn id=href title=dom-WorkerLocation-href><code>href</code></dfn>
2232 wakaba 1.23 attribute must return the <span>absolute URL</span> that the object
2233     represents.
2234    
2235 wakaba 1.32 <p class="no-ja-translation">The <code><a href="#workerlocation">WorkerLocation</a></code> interface
2236 wakaba 1.23 also has the complement of <span>URL decomposition attributes</span>, <dfn
2237     id=protocol title=dom-WorkerLocation-protocol><code>protocol</code></dfn>,
2238     <dfn id=host title=dom-WorkerLocation-host><code>host</code></dfn>, <dfn
2239 wakaba 1.29 id=port0 title=dom-WorkerLocation-port><code>port</code></dfn>, <dfn
2240 wakaba 1.23 id=hostname title=dom-WorkerLocation-hostname><code>hostname</code></dfn>,
2241     <dfn id=pathname
2242     title=dom-WorkerLocation-pathname><code>pathname</code></dfn>, <dfn
2243     id=search title=dom-WorkerLocation-search><code>search</code></dfn>, and
2244     <dfn id=hash title=dom-WorkerLocation-hash><code>hash</code></dfn>. These
2245     must follow the rules given for URL decomposition attributes, with the
2246     <span title=concept-uda-input>input</span> being the <span>absolute
2247     URL</span> that the object represents (same as the <code
2248     title=dom-WorkerLocation-href><a href="#href">href</a></code> attribute),
2249     and the <span title=concept-uda-setter>common setter action</span> being a
2250     no-op, since the attributes are defined to be readonly. <a
2251     href="#refsHTML5">[HTML5]</a>
2252 wakaba 1.22
2253 wakaba 1.32 <h2 class="no-num has-ja-translation" id=references><span class=en-original lang=en>References</span><span class=ja-translation lang=ja>参考文献</span></h2>
2254    
2255     <p class="big-issue has-ja-translation"><span class=en-original lang=en>This section will be written in a future
2256     draft.<!--XXX-->
2257 wakaba 1.1
2258 wakaba 1.32 </span><span class=ja-translation lang=ja>この章は将来の仕様案で執筆します。</span><h2 class="no-num has-ja-translation" id=acknowledgements><span class=en-original lang=en>Acknowledgements</span><span class=ja-translation lang=ja>謝辞</span></h2>
2259 wakaba 1.1 <!-- ACKS -->
2260    
2261 wakaba 1.32 <p class="has-ja-translation"><span class=en-original lang=en>Thanks to Aaron Boodman, Jonas Sicking, Justin James, Maciej Stachowiak,
2262     Mike Smith, and Philip Taylor for their useful and substantial comments.
2263    
2264     </span><span class=ja-translation lang=ja>Aaron Boodman, Jonas Sicking, Justin James, Maciej Stachowiak, Mike Smith, Philip Taylor の有用かつ重要なコメントに感謝します。</span><p class="has-ja-translation"><span class=en-original lang=en>Huge thanks to the whole Gears team, who pioneered this technology and
2265     whose experience has been a huge influence on this specification.
2266     </span><span class=ja-translation lang=ja>Gears チームの方々にも非常に感謝します。
2267 wakaba 1.14 Gears チームの方々はこの技術を開拓し、その経験はこの仕様書にも多大な影響を与えています。
2268     </span>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24