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