| 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 |
|
|
<body class=draft> |
| 9 |
|
|
<div class=head> |
| 10 |
|
|
<p><a class=logo href="http://www.whatwg.org/" rel=home><img alt=WHATWG |
| 11 |
|
|
src="/images/logo"></a></p> |
| 12 |
|
|
|
| 13 |
|
|
<h1 id=web-workers>Web Workers</h1> |
| 14 |
|
|
|
| 15 |
wakaba |
1.4 |
<h2 class="no-num no-toc" id=draft>Draft Recommendation — 17 July |
| 16 |
wakaba |
1.1 |
2008</h2> |
| 17 |
|
|
|
| 18 |
|
|
<p>You can take part in this work. <a |
| 19 |
|
|
href="http://www.whatwg.org/mailing-list">Join the working group's |
| 20 |
|
|
discussion list.</a></p> |
| 21 |
|
|
|
| 22 |
|
|
<p><strong>Web designers!</strong> We have a <a |
| 23 |
|
|
href="http://blog.whatwg.org/faq/">FAQ</a>, a <a |
| 24 |
|
|
href="http://forums.whatwg.org/">forum</a>, and a <a |
| 25 |
|
|
href="http://www.whatwg.org/mailing-list#help">help mailing list</a> for |
| 26 |
|
|
you!</p> |
| 27 |
|
|
|
| 28 |
|
|
<dl> |
| 29 |
|
|
<dt>This version: |
| 30 |
|
|
|
| 31 |
|
|
<dd><a |
| 32 |
|
|
href="http://www.whatwg.org/specs/web-workers/current-work/">http://www.whatwg.org/specs/web-workers/current-work/</a> |
| 33 |
|
|
|
| 34 |
|
|
<dt>Version history: |
| 35 |
|
|
|
| 36 |
|
|
<dd>Twitter messages (non-editorial changes only): <a |
| 37 |
|
|
href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a> |
| 38 |
|
|
|
| 39 |
|
|
<dd>Commit-Watchers mailing list: <a |
| 40 |
|
|
href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a> |
| 41 |
|
|
|
| 42 |
|
|
<dd>Interactive Web interface: <a |
| 43 |
|
|
href="http://html5.org/tools/web-workers-tracker">http://html5.org/tools/web-workers-tracker</a> |
| 44 |
|
|
|
| 45 |
|
|
<dd>Subversion interface: <a |
| 46 |
|
|
href="http://svn.whatwg.org/webworkers/">http://svn.whatwg.org/webworkers/</a> |
| 47 |
|
|
|
| 48 |
|
|
<dt>Issues: |
| 49 |
|
|
|
| 50 |
|
|
<dd>To send feedback: <a |
| 51 |
|
|
href="http://www.whatwg.org/mailing-list">[email protected]</a> |
| 52 |
|
|
|
| 53 |
|
|
<dd>To view and vote on feedback: <a |
| 54 |
|
|
href="http://www.whatwg.org/issues/">http://www.whatwg.org/issues/</a> |
| 55 |
|
|
|
| 56 |
|
|
<dt>Editor: |
| 57 |
|
|
|
| 58 |
|
|
<dd>Ian Hickson, Google, [email protected] |
| 59 |
|
|
</dl> |
| 60 |
|
|
|
| 61 |
|
|
<p class=copyright>© Copyright 2004-2008 Apple Computer, Inc., |
| 62 |
|
|
Mozilla Foundation, and Opera Software ASA.</p> |
| 63 |
|
|
|
| 64 |
|
|
<p class=copyright>You are granted a license to use, reproduce and create |
| 65 |
|
|
derivative works of this document.</p> |
| 66 |
|
|
</div> |
| 67 |
|
|
|
| 68 |
|
|
<hr> |
| 69 |
|
|
|
| 70 |
|
|
<h2 class="no-num no-toc" id=abstract>Abstract</h2> |
| 71 |
|
|
|
| 72 |
|
|
<p>This specification defines an API that allows Web application authors to |
| 73 |
|
|
spawn background workers running scripts in parallel to their main page. |
| 74 |
|
|
This allows for thread-like operation with message-passing as the |
| 75 |
|
|
coordination mechanism. |
| 76 |
|
|
|
| 77 |
|
|
<h2 class="no-num no-toc" id=status>Status of this document</h2> |
| 78 |
|
|
|
| 79 |
|
|
<p><strong>This is a work in progress!</strong> This document is changing |
| 80 |
|
|
on a daily if not hourly basis in response to comments and as a general |
| 81 |
|
|
part of its development process. Comments are very welcome, please send |
| 82 |
|
|
them to <a href="mailto:[email protected]">[email protected]</a>. Thank |
| 83 |
|
|
you. |
| 84 |
|
|
|
| 85 |
|
|
<p>The current focus is in developing a first draft proposal. |
| 86 |
|
|
|
| 87 |
|
|
<p>Implementors should be aware that this specification is not stable. |
| 88 |
|
|
<strong>Implementors who are not taking part in the discussions are likely |
| 89 |
|
|
to find the specification changing out from under them in incompatible |
| 90 |
|
|
ways.</strong> Vendors interested in implementing this specification |
| 91 |
|
|
before it eventually reaches the call for implementations should join the |
| 92 |
|
|
<a href="/mailing-list">WHATWG mailing list</a> and take part in the |
| 93 |
|
|
discussions. |
| 94 |
|
|
|
| 95 |
|
|
<p>This specification is also being produced by the <a |
| 96 |
|
|
href="http://www.w3.org/html/wg">W3C HTML WG</a>. The two specifications |
| 97 |
|
|
are identical from the table of contents onwards. |
| 98 |
|
|
|
| 99 |
|
|
<h2 class="no-num no-toc" id=contents>Table of contents</h2> |
| 100 |
|
|
<!--begin-toc--> |
| 101 |
|
|
|
| 102 |
|
|
<ul class=toc> |
| 103 |
|
|
<li><a href="#introduction"><span class=secno>1. </span>Introduction</a> |
| 104 |
|
|
<ul class=toc> |
| 105 |
wakaba |
1.3 |
<li><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a> |
| 106 |
wakaba |
1.1 |
|
| 107 |
wakaba |
1.2 |
<li><a href="#requirements"><span class=secno>1.2 |
| 108 |
|
|
</span>Requirements</a> |
| 109 |
wakaba |
1.1 |
|
| 110 |
wakaba |
1.3 |
<li><a href="#conformance"><span class=secno>1.3 </span>Conformance |
| 111 |
wakaba |
1.1 |
requirements</a> |
| 112 |
|
|
<ul class=toc> |
| 113 |
wakaba |
1.3 |
<li><a href="#dependencies"><span class=secno>1.3.1 |
| 114 |
wakaba |
1.1 |
</span>Dependencies</a> |
| 115 |
|
|
</ul> |
| 116 |
|
|
|
| 117 |
wakaba |
1.3 |
<li><a href="#terminology"><span class=secno>1.4 </span>Terminology</a> |
| 118 |
|
|
</ul> |
| 119 |
|
|
|
| 120 |
|
|
<li><a href="#inside"><span class=secno>2. </span>Inside workers</a> |
| 121 |
|
|
<ul class=toc> |
| 122 |
|
|
<li><a href="#the-windowworker"><span class=secno>2.1 </span>The |
| 123 |
|
|
<code>WindowWorker</code> interface</a> |
| 124 |
|
|
|
| 125 |
|
|
<li><a href="#the-queue"><span class=secno>2.2 </span>The queue of |
| 126 |
|
|
events</a> |
| 127 |
|
|
|
| 128 |
wakaba |
1.4 |
<li><a href="#the-workers"><span class=secno>2.3 </span>The worker's |
| 129 |
|
|
ports</a> |
| 130 |
|
|
|
| 131 |
|
|
<li><a href="#processing"><span class=secno>2.4 </span>Processing |
| 132 |
wakaba |
1.3 |
model</a> |
| 133 |
|
|
</ul> |
| 134 |
|
|
|
| 135 |
|
|
<li><a href="#creating"><span class=secno>3. </span>Creating workers</a> |
| 136 |
|
|
<ul class=toc> |
| 137 |
|
|
<li><a href="#the-windowworkercreators"><span class=secno>3.1 </span>The |
| 138 |
|
|
<code>WindowWorkerCreators</code> interface</a> |
| 139 |
wakaba |
1.1 |
</ul> |
| 140 |
|
|
|
| 141 |
|
|
<li class=no-num><a href="#references">References</a> |
| 142 |
|
|
|
| 143 |
|
|
<li class=no-num><a href="#acknowledgements">Acknowledgements</a> |
| 144 |
|
|
</ul> |
| 145 |
|
|
<!--end-toc--> |
| 146 |
|
|
|
| 147 |
|
|
<hr> |
| 148 |
|
|
|
| 149 |
|
|
<h2 id=introduction><span class=secno>1. </span>Introduction</h2> |
| 150 |
|
|
|
| 151 |
wakaba |
1.3 |
<h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3> |
| 152 |
wakaba |
1.1 |
|
| 153 |
|
|
<p><em>This section is non-normative.</em> |
| 154 |
|
|
|
| 155 |
wakaba |
1.3 |
<p class=big-issue>This section is missing. |
| 156 |
|
|
|
| 157 |
wakaba |
1.2 |
<h3 id=requirements><span class=secno>1.2 </span>Requirements</h3> |
| 158 |
wakaba |
1.1 |
|
| 159 |
|
|
<p><em>This section is non-normative.</em> |
| 160 |
wakaba |
1.2 |
|
| 161 |
|
|
<p>This specification aims to address the following use cases and |
| 162 |
|
|
requirements: |
| 163 |
|
|
|
| 164 |
|
|
<ul> |
| 165 |
|
|
<li>Background workers: A Web application needs to keep its data |
| 166 |
|
|
synchronised with the server, both sending updates to the server and |
| 167 |
|
|
receiving updates from the server, including handling buffering of |
| 168 |
|
|
updates for when the application goes offline. The code to do this would |
| 169 |
|
|
ideally be independent of the UI code. |
| 170 |
|
|
|
| 171 |
|
|
<li>URLs: Workers should be spawned from URLs, not from strings, since |
| 172 |
|
|
script rarely has access to its own source. |
| 173 |
|
|
|
| 174 |
|
|
<li>Message queuing: Messages sent to a worker before the worker has |
| 175 |
|
|
initialised should not be lost. |
| 176 |
|
|
|
| 177 |
|
|
<li>Workers should have access to timers. |
| 178 |
|
|
|
| 179 |
|
|
<li>Workers should have access to the network. |
| 180 |
|
|
|
| 181 |
|
|
<li>Workers should be able to use libraries. |
| 182 |
|
|
|
| 183 |
|
|
<li>Implementations should not have to expose <code>Node</code> or |
| 184 |
|
|
<code>Document</code> objects to workers. |
| 185 |
|
|
|
| 186 |
|
|
<li>Workers should not share anything with the outside world. The objects |
| 187 |
|
|
representing the worker in the worker itself and in the context that |
| 188 |
|
|
created the worker should be different, for instance. |
| 189 |
|
|
|
| 190 |
|
|
<li>Shared workers: Multiple instances of the same Web application would |
| 191 |
|
|
want to keep just one connection back to the server. |
| 192 |
|
|
|
| 193 |
|
|
<li>Capabilities granting: It should be possible for code running in one |
| 194 |
|
|
iframe to negotiate a connection to another iframe, with that connection |
| 195 |
|
|
granting certain rights (e.g. adding to an address book but not reading |
| 196 |
|
|
from it). |
| 197 |
|
|
|
| 198 |
|
|
<li>Delegation: It should be possible for one worker to spawn another |
| 199 |
|
|
worker and efficiently delagate a request to that worker, without the |
| 200 |
|
|
caller being aware of the delagate and without the original worker having |
| 201 |
|
|
to proxy all the messages. |
| 202 |
|
|
|
| 203 |
|
|
<li>Workers whose parents are not longer useful should be killed. Workers |
| 204 |
|
|
should be able to detect this is about to happen and exit gracefully. |
| 205 |
|
|
</ul> |
| 206 |
wakaba |
1.1 |
|
| 207 |
wakaba |
1.3 |
<h3 id=conformance><span class=secno>1.3 </span>Conformance requirements</h3> |
| 208 |
wakaba |
1.1 |
|
| 209 |
|
|
<p>All diagrams, examples, and notes in this specification are |
| 210 |
|
|
non-normative, as are all sections explicitly marked non-normative. |
| 211 |
|
|
Everything else in this specification is normative. |
| 212 |
|
|
|
| 213 |
|
|
<p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL |
| 214 |
|
|
NOT",--> |
| 215 |
|
|
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the |
| 216 |
|
|
normative parts of this document are to be interpreted as described in |
| 217 |
|
|
RFC2119. For readability, these words do not appear in all uppercase |
| 218 |
|
|
letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p> |
| 219 |
|
|
<!-- XXX but they should be |
| 220 |
|
|
marked up --> |
| 221 |
|
|
|
| 222 |
|
|
<p>Requirements phrased in the imperative as part of algorithms (such as |
| 223 |
|
|
"strip any leading space characters" or "return false and abort these |
| 224 |
|
|
steps") are to be interpreted with the meaning of the key word ("must", |
| 225 |
|
|
"should", "may", etc) used in introducing the algorithm. |
| 226 |
|
|
|
| 227 |
|
|
<p>Some conformance requirements are phrased as requirements on attributes, |
| 228 |
|
|
methods or objects. Such requirements are to be interpreted as |
| 229 |
|
|
requirements on user agents. |
| 230 |
|
|
|
| 231 |
|
|
<p>Conformance requirements phrased as algorithms or specific steps may be |
| 232 |
|
|
implemented in any manner, so long as the end result is equivalent. (In |
| 233 |
|
|
particular, the algorithms defined in this specification are intended to |
| 234 |
|
|
be easy to follow, and not intended to be performant.) |
| 235 |
|
|
|
| 236 |
|
|
<p>The only conformance class defined by this specification is user agents. |
| 237 |
|
|
|
| 238 |
|
|
<p>User agents may impose implementation-specific limits on otherwise |
| 239 |
|
|
unconstrained inputs, e.g. to prevent denial of service attacks, to guard |
| 240 |
|
|
against running out of memory, or to work around platform-specific |
| 241 |
|
|
limitations. |
| 242 |
|
|
|
| 243 |
wakaba |
1.3 |
<h4 id=dependencies><span class=secno>1.3.1 </span>Dependencies</h4> |
| 244 |
wakaba |
1.1 |
|
| 245 |
|
|
<p>This specification relies on several other underlying specifications. |
| 246 |
|
|
|
| 247 |
|
|
<dl> |
| 248 |
|
|
<dt>HTML5 |
| 249 |
|
|
|
| 250 |
|
|
<dd> |
| 251 |
|
|
<p>Many fundamental concepts from HTML5 are used by this specification. |
| 252 |
|
|
<a href="#refsHTML5">[HTML5]</a></p> |
| 253 |
|
|
|
| 254 |
|
|
<dt>ECMAScript |
| 255 |
|
|
|
| 256 |
|
|
<dd> |
| 257 |
|
|
<p>This specification is intended to be used with JavaScript as the |
| 258 |
|
|
scripting language. <a href="#refsJS">[JS]</a></p> |
| 259 |
|
|
|
| 260 |
|
|
<dt>WebIDL |
| 261 |
|
|
|
| 262 |
|
|
<dd> |
| 263 |
|
|
<p>The IDL blocks in this specification use the semantics of the WebIDL |
| 264 |
|
|
specification. <a href="#refsWebIDL">[WebIDL]</a></p> |
| 265 |
|
|
</dl> |
| 266 |
|
|
|
| 267 |
wakaba |
1.3 |
<h3 id=terminology><span class=secno>1.4 </span>Terminology</h3> |
| 268 |
wakaba |
1.1 |
|
| 269 |
|
|
<p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and |
| 270 |
|
|
<em>visible</em> might sometimes be used when referring to the way a |
| 271 |
|
|
document is rendered to the user. These terms are not meant to imply a |
| 272 |
|
|
visual medium; they must be considered to apply to other media in |
| 273 |
|
|
equivalent ways. |
| 274 |
|
|
|
| 275 |
wakaba |
1.3 |
<p>The construction "a <code title="">Foo</code> object", where <code |
| 276 |
|
|
title="">Foo</code> is actually an interface, is sometimes used instead of |
| 277 |
|
|
the more accurate "an object implementing the interface <code |
| 278 |
|
|
title="">Foo</code>". |
| 279 |
wakaba |
1.1 |
|
| 280 |
|
|
<p>The term DOM is used to refer to the API set made available to scripts |
| 281 |
|
|
in Web applications, and does not necessarily imply the existence of an |
| 282 |
|
|
actual <code>Document</code> object or of any other <code>Node</code> |
| 283 |
|
|
objects as defined in the DOM Core specifications. <a |
| 284 |
|
|
href="#refsDOM3CORE">[DOM3CORE]</a> |
| 285 |
|
|
|
| 286 |
|
|
<p>A DOM attribute is said to be <em>getting</em> when its value is being |
| 287 |
|
|
retrieved (e.g. by author script), and is said to be <em>setting</em> when |
| 288 |
|
|
a new value is assigned to it. |
| 289 |
|
|
|
| 290 |
|
|
<p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means |
| 291 |
|
|
that any attributes returning that object must always return the same |
| 292 |
|
|
object (not a new object each time), and the attributes and methods on |
| 293 |
|
|
that object must operate on the actual underlying data, not a snapshot of |
| 294 |
|
|
the data. |
| 295 |
|
|
|
| 296 |
wakaba |
1.3 |
<h2 id=inside><span class=secno>2. </span>Inside workers</h2> |
| 297 |
|
|
|
| 298 |
|
|
<h3 id=the-windowworker><span class=secno>2.1 </span>The <code><a |
| 299 |
|
|
href="#windowworker">WindowWorker</a></code> interface</h3> |
| 300 |
|
|
|
| 301 |
|
|
<pre |
| 302 |
|
|
class=idl>[NoInterfaceObject] interface <dfn id=windowworker>WindowWorker</dfn> { |
| 303 |
|
|
readonly attribute boolean <a href="#name" title=dom-windowworker-name>name</a>; |
| 304 |
|
|
readonly attribute boolean <a href="#closing" title=dom-windowworker-closing>closing</a>; |
| 305 |
wakaba |
1.4 |
void <a href="#close" title=dom-windowworker-close>close</a>(); |
| 306 |
|
|
|
| 307 |
|
|
// event handler attributes |
| 308 |
|
|
attribute <span>EventListener</span> <a href="#onjoin" title=handler-windowworker-onjoin>onjoin</a>; |
| 309 |
|
|
attribute <span>EventListener</span> <a href="#onunload" title=handler-windowworker-onunload>onunload</a>; |
| 310 |
wakaba |
1.3 |
};</pre> |
| 311 |
|
|
|
| 312 |
|
|
<p>Objects that implement the <code><a |
| 313 |
|
|
href="#windowworker">WindowWorker</a></code> interface must also implement |
| 314 |
|
|
the <code>Window</code> interface (and thus also the |
| 315 |
|
|
<code>WindowTimers</code> and <code><a |
| 316 |
|
|
href="#windowworkercreators">WindowWorkerCreators</a></code> interfaces) |
| 317 |
|
|
and the <code>EventTarget</code> interface. |
| 318 |
|
|
|
| 319 |
|
|
<p>The <dfn id=name title=dom-windowworker-name><code>name</code></dfn> |
| 320 |
|
|
attribute must return the value it was assigned when the <code><a |
| 321 |
|
|
href="#windowworker">WindowWorker</a></code> object was created by the "<a |
| 322 |
|
|
href="#run-a">run a worker</a>" algorithm. |
| 323 |
|
|
|
| 324 |
|
|
<p>The <dfn id=closing |
| 325 |
|
|
title=dom-windowworker-closing><code>closing</code></dfn> attribute must |
| 326 |
|
|
return false until the "<a href="#kill-a">kill a worker</a>" processing |
| 327 |
|
|
model defined below sets it to false. |
| 328 |
|
|
|
| 329 |
wakaba |
1.4 |
<p>The following are the <span>event handler DOM attributes</span> that |
| 330 |
|
|
must be supported by objects implementing the <code><a |
| 331 |
|
|
href="#windowworker">WindowWorker</a></code> interface: |
| 332 |
|
|
|
| 333 |
|
|
<dl> |
| 334 |
|
|
<dt><dfn id=onjoin |
| 335 |
|
|
title=handler-windowworker-onjoin><code>onjoin</code></dfn> |
| 336 |
|
|
|
| 337 |
|
|
<dd> |
| 338 |
|
|
<p>Must be invoked whenever a <code |
| 339 |
|
|
title=event-windowworker-join>join</code> event is targeted at or |
| 340 |
|
|
bubbles through the <code><a |
| 341 |
|
|
href="#windowworker">WindowWorker</a></code> object. |
| 342 |
|
|
|
| 343 |
|
|
<dt><dfn id=onunload |
| 344 |
|
|
title=handler-windowworker-onunload><code>onunload</code></dfn> |
| 345 |
|
|
|
| 346 |
|
|
<dd> |
| 347 |
|
|
<p>Must be invoked whenever a <code title=event-unload>unload</code> |
| 348 |
|
|
event is targeted at or bubbles through the <code><a |
| 349 |
|
|
href="#windowworker">WindowWorker</a></code> object. |
| 350 |
|
|
</dl> |
| 351 |
|
|
|
| 352 |
wakaba |
1.3 |
<h3 id=the-queue><span class=secno>2.2 </span>The queue of events</h3> |
| 353 |
|
|
|
| 354 |
|
|
<p>Each <code><a href="#windowworker">WindowWorker</a></code> object is |
| 355 |
|
|
asssociated with a <dfn id=queue>queue of events</dfn>, which is initially |
| 356 |
|
|
empty. |
| 357 |
|
|
|
| 358 |
|
|
<p>An event in the queue can be a DOM event or a timeout callback. |
| 359 |
|
|
|
| 360 |
wakaba |
1.4 |
<p>All asynchronous callbacks and events that would be called or dispatched |
| 361 |
|
|
in the worker must be added to the worker's queue, with the "<a |
| 362 |
|
|
href="#run-a">run a worker</a>" processing model below taking care of |
| 363 |
|
|
actually calling the callbacks or dispatching the events. |
| 364 |
|
|
|
| 365 |
|
|
<p>Once the <code><a href="#windowworker">WindowWorker</a></code>'s <code |
| 366 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 367 |
|
|
attribute is set to true, the queue must discard anything else that would |
| 368 |
|
|
be added to it. Effectively, once the <code |
| 369 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 370 |
|
|
attribute is true, timers stop firing, notifications for all pending |
| 371 |
|
|
asynchronous operations are dropped, etc. |
| 372 |
|
|
|
| 373 |
|
|
<h3 id=the-workers><span class=secno>2.3 </span>The worker's ports</h3> |
| 374 |
|
|
|
| 375 |
|
|
<p>Workers communicate with other workers and with <span title="browsing |
| 376 |
|
|
context">browsing contexts</span> through <span title="channel |
| 377 |
|
|
messaging">message channels</span> and their <code>MessagePort</code> |
| 378 |
|
|
objects. |
| 379 |
|
|
|
| 380 |
|
|
<p>Each <code><a href="#windowworker">WindowWorker</a></code> <var |
| 381 |
|
|
title="">worker</var> has a list of <dfn id=the-workers0>the worker's |
| 382 |
|
|
ports</dfn>, which consists of all the <code>MessagePort</code> objects |
| 383 |
|
|
that are entangled with another port and that have one (but only one) port |
| 384 |
|
|
whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is <var |
| 385 |
|
|
title="">worker</var>. This list includes all the <code>MessagePort</code> |
| 386 |
|
|
objects that are in events pending in the <a href="#queue">queue of |
| 387 |
|
|
events</a>. |
| 388 |
|
|
|
| 389 |
|
|
<h3 id=processing><span class=secno>2.4 </span>Processing model</h3> |
| 390 |
wakaba |
1.3 |
|
| 391 |
|
|
<p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var |
| 392 |
wakaba |
1.4 |
title="">name</var> for a script with <span>URL</span> <var |
| 393 |
|
|
title="">url</var>, a browsing context <var title="">owner browsing |
| 394 |
|
|
context</var> and a <code>Document</code> <var title="">owner |
| 395 |
|
|
document</var>, it must run the following steps in a completely separate |
| 396 |
|
|
and parallel execution environment: |
| 397 |
wakaba |
1.3 |
|
| 398 |
|
|
<ol> |
| 399 |
|
|
<li> |
| 400 |
wakaba |
1.4 |
<p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by |
| 401 |
|
|
<var title="">url</var>.</p> |
| 402 |
|
|
|
| 403 |
|
|
<p>If the attempt fails, then abort these steps and invoke the <a |
| 404 |
|
|
href="#worker0" title="worker creation failed">error handling steps</a> |
| 405 |
|
|
defined by the algorithm that called this one.</p> |
| 406 |
|
|
|
| 407 |
|
|
<p>If the attempt succeeds, then let <var title="">script</var> be the |
| 408 |
|
|
resource that was obtained.</p> |
| 409 |
|
|
|
| 410 |
|
|
<p class=note>As with <code>script</code> elements, the MIME type of the |
| 411 |
|
|
script is ignored. Unlike with <code>script</code> elements, there is no |
| 412 |
|
|
way to override the type. It's always assumed to be JavaScript.</p> |
| 413 |
|
|
<!-- XXX people will complain about |
| 414 |
|
|
this. I guess we might want to examine the MIME type... --> |
| 415 |
|
|
|
| 416 |
|
|
|
| 417 |
|
|
<li> |
| 418 |
wakaba |
1.3 |
<p>Create a new <code><a href="#windowworker">WindowWorker</a></code> |
| 419 |
|
|
object, <var title="">window</var>.</p> |
| 420 |
|
|
|
| 421 |
|
|
<li> |
| 422 |
|
|
<p>Set the <code title=dom-windowworker-name><a |
| 423 |
|
|
href="#name">name</a></code> attribute of <var title="">window</var> to |
| 424 |
|
|
the value of <var title="">name</var>.</p> |
| 425 |
|
|
|
| 426 |
|
|
<li> |
| 427 |
|
|
<p>Let <var title="">script</var>'s <span>script execution context</span> |
| 428 |
|
|
(and thus also <span>global object</span>) be <var |
| 429 |
|
|
title="">window</var>.</p> |
| 430 |
|
|
|
| 431 |
|
|
<li> |
| 432 |
|
|
<p>Let <var title="">script</var>'s <span>script browsing context</span> |
| 433 |
|
|
be <var title="">owner browsing context</var>.</p> |
| 434 |
|
|
|
| 435 |
|
|
<li> |
| 436 |
|
|
<p>Let <var title="">script</var>'s <span>script document context</span> |
| 437 |
|
|
be <var title="">owner document</var>.</p> |
| 438 |
|
|
|
| 439 |
|
|
<li> |
| 440 |
wakaba |
1.4 |
<p>Invoke the <a href="#worker" title="worker creation succeeded">success |
| 441 |
|
|
steps</a> defined by the algorithm that called this one. (This will add |
| 442 |
|
|
an event to the <a href="#queue">queue of events</a>.)</p> |
| 443 |
|
|
|
| 444 |
|
|
<li> |
| 445 |
|
|
<p>Start monitoring <var title="">worker</var>, such that whenever the |
| 446 |
|
|
<var title="">window</var> object's <code |
| 447 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 448 |
|
|
attribute is false and all <a href="#the-workers0">the worker's |
| 449 |
|
|
ports</a> have their <code title=dom-MessagePort-active>active</code> |
| 450 |
|
|
attributes set to false, the user agent suspends execution of script in |
| 451 |
|
|
that worker until such time as either the <code |
| 452 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 453 |
|
|
attribute switches to true or one of the <code>MessagePort</code> |
| 454 |
|
|
objects in the list of <a href="#the-workers0">the worker's ports</a> |
| 455 |
|
|
has an <code title=dom-MessagePort-active>active</code> attribute with |
| 456 |
|
|
the value true.</p> |
| 457 |
wakaba |
1.3 |
|
| 458 |
|
|
<li> |
| 459 |
wakaba |
1.4 |
<p>Start monitoring <var title="">worker</var>, such that as soon as the |
| 460 |
|
|
list of <a href="#the-workers0">the worker's ports</a> becomes empty, |
| 461 |
|
|
the <var title="">window</var> object's <code |
| 462 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 463 |
|
|
attribute is set to true.</p> |
| 464 |
|
|
|
| 465 |
|
|
<p class=note>This can never happen at this point in the algorithm: as |
| 466 |
|
|
soon as the worker ran the <a href="#worker" title="worker creation |
| 467 |
|
|
succeeded">success steps</a>, the <a href="#queue">queue of events</a> |
| 468 |
|
|
received a <code title=event-join>join</code> event with a |
| 469 |
|
|
<code>MessagePort</code>.</p> |
| 470 |
|
|
|
| 471 |
|
|
<li> |
| 472 |
|
|
<p>Run <var title="">script</var> until it either returns, fails to catch |
| 473 |
|
|
an exception, or gets prematurely aborted by the "<a href="#kill-a">kill |
| 474 |
|
|
a worker</a>" algorithm below.</p> |
| 475 |
|
|
|
| 476 |
|
|
<p class=note>If the script gets aborted by the "<a href="#kill-a">kill a |
| 477 |
|
|
worker</a>" algorithm, then that same algorithm will cause there to only |
| 478 |
|
|
be a single event in the <a href="#queue">queue of events</a> at the |
| 479 |
|
|
next step, namely the <code title=message-unload>unload</code> event. |
| 480 |
|
|
However, if the event is ignored then it will become true as soon as |
| 481 |
|
|
that port is garbage collected.</p> |
| 482 |
|
|
|
| 483 |
|
|
<li> |
| 484 |
|
|
<p><i>Event loop</i>: Wait until either there is an event in the <a |
| 485 |
wakaba |
1.3 |
href="#queue">queue of events</a> associated with <var |
| 486 |
wakaba |
1.4 |
title="">window</var> or the <var title="">window</var> object's <code |
| 487 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 488 |
|
|
attribute is set to true.</p> |
| 489 |
wakaba |
1.3 |
|
| 490 |
|
|
<li> |
| 491 |
|
|
<p>Dispatch the oldest event or callback in the <a href="#queue">queue of |
| 492 |
wakaba |
1.4 |
events</a>, if any. The handling of this event or the execution of this |
| 493 |
|
|
callback might get prematurely aborted by the "<a href="#kill-a">kill a |
| 494 |
|
|
worker</a>" algorithm below.</p> |
| 495 |
wakaba |
1.3 |
|
| 496 |
|
|
<li> |
| 497 |
|
|
<p>If there are any more events in the <a href="#queue">queue of |
| 498 |
|
|
events</a> or if the <var title="">window</var> object's <code |
| 499 |
|
|
title=dom-windowworker-closing><a href="#closing">closing</a></code> |
| 500 |
|
|
attribute is set to false, then jump back to the step above labeled |
| 501 |
|
|
<i>event loop</i>.</p> |
| 502 |
|
|
|
| 503 |
|
|
<li> |
| 504 |
|
|
<p class=big-issue>timers, intervals, XMLHttpRequests, database |
| 505 |
|
|
transactions, etc, must be killed; ports must be deactivated and |
| 506 |
wakaba |
1.4 |
unentangled (do not send unload)</p> |
| 507 |
wakaba |
1.3 |
</ol> |
| 508 |
|
|
|
| 509 |
|
|
<hr> |
| 510 |
|
|
|
| 511 |
|
|
<p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run |
| 512 |
|
|
the following steps in parallel with the worker's main loop (the "<a |
| 513 |
|
|
href="#run-a">run a worker</a>" processing model defined above): |
| 514 |
|
|
|
| 515 |
|
|
<ol> |
| 516 |
|
|
<li> |
| 517 |
|
|
<p>Create an <code>Event</code> object with the event name <code |
| 518 |
|
|
title=event-unload>unload</code>, which does not bubble and is not |
| 519 |
|
|
cancelable, and add it to the worker's <code><a |
| 520 |
|
|
href="#windowworker">WindowWorker</a></code> object's <a |
| 521 |
wakaba |
1.4 |
href="#queue">queue of events</a>, targetted at the <code><a |
| 522 |
|
|
href="#windowworker">WindowWorker</a></code> object itself. |
| 523 |
|
|
|
| 524 |
|
|
<li> |
| 525 |
|
|
<p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code> |
| 526 |
|
|
object's <code title=dom-windowworker-closing><a |
| 527 |
|
|
href="#closing">closing</a></code> attribute to true. |
| 528 |
wakaba |
1.3 |
|
| 529 |
|
|
<li> |
| 530 |
wakaba |
1.4 |
<p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run |
| 531 |
|
|
a worker</a>" processing model defined above immediately starts running |
| 532 |
|
|
event listeners registered for <code title=event-unload>unload</code> |
| 533 |
|
|
event, this time should not be zero — the idea is that the <code |
| 534 |
|
|
title=event-unload>unload</code> event can be used to clean up when |
| 535 |
|
|
shutting down unexpectedly. |
| 536 |
wakaba |
1.3 |
|
| 537 |
|
|
<li> |
| 538 |
|
|
<p>If there are any events in the <a href="#queue">queue of events</a> |
| 539 |
|
|
other than the <code title=event-unload>unload</code> event that this |
| 540 |
|
|
algorithm just added, discard them without dispatching them. |
| 541 |
|
|
|
| 542 |
|
|
<li> |
| 543 |
|
|
<p>If the <code title=event-unload>unload</code> event that this |
| 544 |
|
|
algorithm just added hasn't yet been dispatched, then abort the script |
| 545 |
|
|
currently running in the worker. |
| 546 |
|
|
|
| 547 |
|
|
<li> |
| 548 |
|
|
<p>Wait a user-agent-defined amount of time. |
| 549 |
|
|
|
| 550 |
|
|
<li> |
| 551 |
|
|
<p>Abort the script currently running in the worker (if any script is |
| 552 |
|
|
running, then it will be a handler for the <code |
| 553 |
|
|
title=event-unload>unload</code> event). |
| 554 |
|
|
</ol> |
| 555 |
|
|
|
| 556 |
wakaba |
1.4 |
<hr> |
| 557 |
|
|
|
| 558 |
|
|
<p>When a script invokes the <dfn id=close |
| 559 |
|
|
title=dom-windowworker-close><code>close()</code></dfn> method on a |
| 560 |
|
|
<code><a href="#windowworker">WindowWorker</a></code> object, the user |
| 561 |
|
|
agent must run the following steps: |
| 562 |
|
|
|
| 563 |
|
|
<ol> |
| 564 |
|
|
<li> |
| 565 |
|
|
<p>Create an <code>Event</code> object with the event name <code |
| 566 |
|
|
title=event-unload>unload</code>, which does not bubble and is not |
| 567 |
|
|
cancelable, and add it to the <code><a |
| 568 |
|
|
href="#windowworker">WindowWorker</a></code> object's <a |
| 569 |
|
|
href="#queue">queue of events</a>, targetted at the <code><a |
| 570 |
|
|
href="#windowworker">WindowWorker</a></code> object itself. |
| 571 |
|
|
|
| 572 |
|
|
<li> |
| 573 |
|
|
<p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code> |
| 574 |
|
|
object's <code title=dom-windowworker-closing><a |
| 575 |
|
|
href="#closing">closing</a></code> attribute to true. |
| 576 |
|
|
|
| 577 |
|
|
<li> |
| 578 |
|
|
<p>For each <code>MessagePort</code> object that is entangled with |
| 579 |
|
|
another port and that has one (but only one) port whose <code |
| 580 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> is the <code><a |
| 581 |
|
|
href="#windowworker">WindowWorker</a></code> object on which the method |
| 582 |
|
|
was invoked, run the following substeps:</p> |
| 583 |
|
|
|
| 584 |
|
|
<ol> |
| 585 |
|
|
<li> |
| 586 |
|
|
<p>Unentangle the two ports. |
| 587 |
|
|
|
| 588 |
|
|
<li> |
| 589 |
|
|
<p>Set both ports' <code title=dom-MessagePort-active>active</code> |
| 590 |
|
|
attribute to false. |
| 591 |
|
|
|
| 592 |
|
|
<li> |
| 593 |
|
|
<p>At the next available opportunity, after any scripts have finished |
| 594 |
|
|
executing<!-- XXX queue -->, <span>fire a simple event</span> called |
| 595 |
|
|
<code title=event-unload>unload</code> at the other port (the one |
| 596 |
|
|
whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is |
| 597 |
|
|
not the <code><a href="#windowworker">WindowWorker</a></code> object |
| 598 |
|
|
on which the <code title=dom-windowworker-close><a |
| 599 |
|
|
href="#close">close()</a></code> method was called). |
| 600 |
|
|
</ol> |
| 601 |
|
|
</ol> |
| 602 |
|
|
|
| 603 |
wakaba |
1.3 |
<h2 id=creating><span class=secno>3. </span>Creating workers</h2> |
| 604 |
|
|
|
| 605 |
|
|
<h3 id=the-windowworkercreators><span class=secno>3.1 </span>The <code><a |
| 606 |
|
|
href="#windowworkercreators">WindowWorkerCreators</a></code> interface</h3> |
| 607 |
|
|
|
| 608 |
|
|
<pre |
| 609 |
|
|
class=idl>[NoInterfaceObject] interface <dfn id=windowworkercreators>WindowWorkerCreators</dfn> { |
| 610 |
|
|
<span>MessagePort</span> <span title=dom-WindowWorkerCreators-createWorker>createWorker</span>(in DOMString scriptURI); |
| 611 |
|
|
<span>MessagePort</span> <span title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker</span>(in DOMString name, in DOMString scriptURI); |
| 612 |
|
|
};</pre> |
| 613 |
|
|
|
| 614 |
|
|
<p>Objects that implement the <code>Window</code> interface must also |
| 615 |
|
|
implement the <code><a |
| 616 |
|
|
href="#windowworkercreators">WindowWorkerCreators</a></code> interface. |
| 617 |
wakaba |
1.4 |
|
| 618 |
|
|
<hr> |
| 619 |
|
|
|
| 620 |
|
|
<p>When the <code |
| 621 |
|
|
title=dom-WindowWorkerCreators-createWorker>createWorker()</code> method |
| 622 |
|
|
is invoked, the user agent must run the following steps: |
| 623 |
|
|
|
| 624 |
|
|
<ol> |
| 625 |
|
|
<li class=big-issue>... <!-- |
| 626 |
|
|
resolve url |
| 627 |
|
|
if that fails, throw |
| 628 |
|
|
if it's not same-orgin, throw |
| 629 |
|
|
run create worker steps |
| 630 |
|
|
--> |
| 631 |
|
|
|
| 632 |
|
|
</ol> |
| 633 |
|
|
|
| 634 |
|
|
<hr> |
| 635 |
|
|
|
| 636 |
|
|
<p>When the <code |
| 637 |
|
|
title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker()</code> |
| 638 |
|
|
method is invoked, the user agent must run the following steps: |
| 639 |
|
|
|
| 640 |
|
|
<ol> |
| 641 |
|
|
<li class=big-issue>... <!-- |
| 642 |
|
|
if /name/ = "", jump to createWorker() steps |
| 643 |
|
|
resolve url |
| 644 |
|
|
if that fails, throw |
| 645 |
|
|
if it's not same-orgin, throw |
| 646 |
|
|
look for named worker whose name is /name/ and that has closing=false and whose document's origin is the same as the calling scripts' |
| 647 |
|
|
if you don't find one, run 'create worker' steps with given name and resulting absolute url; return returned port |
| 648 |
|
|
check that resulting absolute url matches the one in the worker; if not, throw |
| 649 |
|
|
create two ports |
| 650 |
|
|
return port 1 |
| 651 |
|
|
invoke the "join a worker" steps with the two ports |
| 652 |
|
|
--> |
| 653 |
|
|
|
| 654 |
|
|
</ol> |
| 655 |
|
|
|
| 656 |
|
|
<hr> |
| 657 |
|
|
|
| 658 |
|
|
<p>The steps to <dfn id=create>create a worker</dfn> are as follows: |
| 659 |
|
|
|
| 660 |
|
|
<ol> |
| 661 |
|
|
<li class=big-issue>... <!-- |
| 662 |
|
|
create two ports |
| 663 |
|
|
in a separate thread, 'run a worker' with name "" and resulting absolute url |
| 664 |
|
|
return port 1 |
| 665 |
|
|
success steps: invoke the "join a worker" steps with the two ports |
| 666 |
|
|
failure steps: fire 'error' on port1 |
| 667 |
|
|
--> |
| 668 |
|
|
|
| 669 |
|
|
</ol> |
| 670 |
|
|
|
| 671 |
|
|
<p class=big-issue><dfn id=worker>worker creation succeeded</dfn>, <dfn |
| 672 |
|
|
id=worker0>worker creation failed</dfn> |
| 673 |
|
|
|
| 674 |
|
|
<hr> |
| 675 |
|
|
|
| 676 |
|
|
<p>The steps to <dfn id=join-a>join a worker</dfn> are as follows: |
| 677 |
|
|
|
| 678 |
|
|
<ol> |
| 679 |
|
|
<li class=big-issue>... <!-- |
| 680 |
|
|
given two ports port1, port2, and a worker |
| 681 |
|
|
entangle the ports |
| 682 |
|
|
fire 'join' event on the worker, passing port2 |
| 683 |
|
|
fire 'load' event on port1. |
| 684 |
|
|
--> |
| 685 |
|
|
|
| 686 |
|
|
</ol> |
| 687 |
wakaba |
1.3 |
|
| 688 |
|
|
<p class=big-issue>... |
| 689 |
|
|
|
| 690 |
wakaba |
1.1 |
<h2 class=no-num id=references>References</h2> |
| 691 |
|
|
|
| 692 |
|
|
<p class=big-issue>This section will be written in a future |
| 693 |
|
|
draft.<!--XXX--> |
| 694 |
|
|
|
| 695 |
|
|
<h2 class=no-num id=acknowledgements>Acknowledgements</h2> |
| 696 |
|
|
<!-- ACKS --> |
| 697 |
|
|
|
| 698 |
|
|
<p>Thanks to Maciej Stachowiak and Mike Smith for their useful and |
| 699 |
|
|
substantial comments. |
| 700 |
wakaba |
1.3 |
|
| 701 |
|
|
<p>Huge thanks to the whole Gears team, who pioneered this technology and |
| 702 |
|
|
whose experience has been a huge influence on this specification. |