| 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 |
|
|
<h1 id=web-workers>Web Workers</h1> |
| 18 |
|
|
|
| 19 |
wakaba |
1.5 |
<h2 class="no-num no-toc" id=draft>Draft Recommendation — 18 July |
| 20 |
wakaba |
1.1 |
2008</h2> |
| 21 |
|
|
|
| 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 |
|
|
href="http://www.w3.org/html/wg">W3C HTML WG</a>. The two specifications |
| 101 |
|
|
are identical from the table of contents onwards. |
| 102 |
|
|
|
| 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.3 |
<li><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a> |
| 110 |
wakaba |
1.1 |
|
| 111 |
wakaba |
1.6 |
<li><a href="#conformance"><span class=secno>1.2 </span>Conformance |
| 112 |
wakaba |
1.1 |
requirements</a> |
| 113 |
|
|
<ul class=toc> |
| 114 |
wakaba |
1.6 |
<li><a href="#dependencies"><span class=secno>1.2.1 |
| 115 |
wakaba |
1.1 |
</span>Dependencies</a> |
| 116 |
|
|
</ul> |
| 117 |
|
|
|
| 118 |
wakaba |
1.6 |
<li><a href="#terminology"><span class=secno>1.3 </span>Terminology</a> |
| 119 |
wakaba |
1.3 |
</ul> |
| 120 |
|
|
|
| 121 |
wakaba |
1.5 |
<li><a href="#infrastructure"><span class=secno>2. |
| 122 |
|
|
</span>Infrastructure</a> |
| 123 |
wakaba |
1.3 |
<ul class=toc> |
| 124 |
|
|
<li><a href="#the-windowworker"><span class=secno>2.1 </span>The |
| 125 |
|
|
<code>WindowWorker</code> interface</a> |
| 126 |
|
|
|
| 127 |
wakaba |
1.6 |
<li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and |
| 128 |
|
|
origins of workers</a> |
| 129 |
|
|
|
| 130 |
|
|
<li><a href="#the-queue"><span class=secno>2.3 </span>The queue of |
| 131 |
wakaba |
1.3 |
events</a> |
| 132 |
|
|
|
| 133 |
wakaba |
1.6 |
<li><a href="#the-workers"><span class=secno>2.4 </span>The worker's |
| 134 |
wakaba |
1.4 |
ports</a> |
| 135 |
|
|
|
| 136 |
wakaba |
1.6 |
<li><a href="#processing"><span class=secno>2.5 </span>Processing |
| 137 |
wakaba |
1.3 |
model</a> |
| 138 |
wakaba |
1.5 |
|
| 139 |
wakaba |
1.6 |
<li><a href="#creating"><span class=secno>2.6 </span>Creating |
| 140 |
wakaba |
1.5 |
workers</a> |
| 141 |
wakaba |
1.3 |
</ul> |
| 142 |
|
|
|
| 143 |
wakaba |
1.5 |
<li><a href="#apis-available"><span class=secno>3. </span>APIs available |
| 144 |
|
|
to workers</a> |
| 145 |
wakaba |
1.1 |
|
| 146 |
|
|
<li class=no-num><a href="#references">References</a> |
| 147 |
|
|
|
| 148 |
|
|
<li class=no-num><a href="#acknowledgements">Acknowledgements</a> |
| 149 |
|
|
</ul> |
| 150 |
|
|
<!--end-toc--> |
| 151 |
|
|
|
| 152 |
|
|
<hr> |
| 153 |
|
|
|
| 154 |
|
|
<h2 id=introduction><span class=secno>1. </span>Introduction</h2> |
| 155 |
|
|
|
| 156 |
wakaba |
1.3 |
<h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3> |
| 157 |
wakaba |
1.1 |
|
| 158 |
|
|
<p><em>This section is non-normative.</em> |
| 159 |
|
|
|
| 160 |
wakaba |
1.3 |
<p class=big-issue>This section is missing. |
| 161 |
|
|
|
| 162 |
wakaba |
1.6 |
<h3 id=conformance><span class=secno>1.2 </span>Conformance requirements</h3> |
| 163 |
wakaba |
1.1 |
|
| 164 |
|
|
<p>All diagrams, examples, and notes in this specification are |
| 165 |
|
|
non-normative, as are all sections explicitly marked non-normative. |
| 166 |
|
|
Everything else in this specification is normative. |
| 167 |
|
|
|
| 168 |
|
|
<p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL |
| 169 |
|
|
NOT",--> |
| 170 |
|
|
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the |
| 171 |
|
|
normative parts of this document are to be interpreted as described in |
| 172 |
|
|
RFC2119. For readability, these words do not appear in all uppercase |
| 173 |
|
|
letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p> |
| 174 |
|
|
<!-- XXX but they should be |
| 175 |
|
|
marked up --> |
| 176 |
|
|
|
| 177 |
|
|
<p>Requirements phrased in the imperative as part of algorithms (such as |
| 178 |
|
|
"strip any leading space characters" or "return false and abort these |
| 179 |
|
|
steps") are to be interpreted with the meaning of the key word ("must", |
| 180 |
|
|
"should", "may", etc) used in introducing the algorithm. |
| 181 |
|
|
|
| 182 |
|
|
<p>Some conformance requirements are phrased as requirements on attributes, |
| 183 |
|
|
methods or objects. Such requirements are to be interpreted as |
| 184 |
|
|
requirements on user agents. |
| 185 |
|
|
|
| 186 |
|
|
<p>Conformance requirements phrased as algorithms or specific steps may be |
| 187 |
|
|
implemented in any manner, so long as the end result is equivalent. (In |
| 188 |
|
|
particular, the algorithms defined in this specification are intended to |
| 189 |
|
|
be easy to follow, and not intended to be performant.) |
| 190 |
|
|
|
| 191 |
|
|
<p>The only conformance class defined by this specification is user agents. |
| 192 |
|
|
|
| 193 |
|
|
<p>User agents may impose implementation-specific limits on otherwise |
| 194 |
|
|
unconstrained inputs, e.g. to prevent denial of service attacks, to guard |
| 195 |
|
|
against running out of memory, or to work around platform-specific |
| 196 |
|
|
limitations. |
| 197 |
|
|
|
| 198 |
wakaba |
1.6 |
<h4 id=dependencies><span class=secno>1.2.1 </span>Dependencies</h4> |
| 199 |
wakaba |
1.1 |
|
| 200 |
|
|
<p>This specification relies on several other underlying specifications. |
| 201 |
|
|
|
| 202 |
|
|
<dl> |
| 203 |
|
|
<dt>HTML5 |
| 204 |
|
|
|
| 205 |
|
|
<dd> |
| 206 |
|
|
<p>Many fundamental concepts from HTML5 are used by this specification. |
| 207 |
|
|
<a href="#refsHTML5">[HTML5]</a></p> |
| 208 |
|
|
|
| 209 |
|
|
<dt>ECMAScript |
| 210 |
|
|
|
| 211 |
|
|
<dd> |
| 212 |
|
|
<p>This specification is intended to be used with JavaScript as the |
| 213 |
|
|
scripting language. <a href="#refsJS">[JS]</a></p> |
| 214 |
|
|
|
| 215 |
|
|
<dt>WebIDL |
| 216 |
|
|
|
| 217 |
|
|
<dd> |
| 218 |
|
|
<p>The IDL blocks in this specification use the semantics of the WebIDL |
| 219 |
|
|
specification. <a href="#refsWebIDL">[WebIDL]</a></p> |
| 220 |
|
|
</dl> |
| 221 |
|
|
|
| 222 |
wakaba |
1.6 |
<h3 id=terminology><span class=secno>1.3 </span>Terminology</h3> |
| 223 |
wakaba |
1.1 |
|
| 224 |
|
|
<p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and |
| 225 |
|
|
<em>visible</em> might sometimes be used when referring to the way a |
| 226 |
|
|
document is rendered to the user. These terms are not meant to imply a |
| 227 |
|
|
visual medium; they must be considered to apply to other media in |
| 228 |
|
|
equivalent ways. |
| 229 |
|
|
|
| 230 |
wakaba |
1.3 |
<p>The construction "a <code title="">Foo</code> object", where <code |
| 231 |
|
|
title="">Foo</code> is actually an interface, is sometimes used instead of |
| 232 |
|
|
the more accurate "an object implementing the interface <code |
| 233 |
|
|
title="">Foo</code>". |
| 234 |
wakaba |
1.1 |
|
| 235 |
|
|
<p>The term DOM is used to refer to the API set made available to scripts |
| 236 |
|
|
in Web applications, and does not necessarily imply the existence of an |
| 237 |
|
|
actual <code>Document</code> object or of any other <code>Node</code> |
| 238 |
|
|
objects as defined in the DOM Core specifications. <a |
| 239 |
|
|
href="#refsDOM3CORE">[DOM3CORE]</a> |
| 240 |
|
|
|
| 241 |
|
|
<p>A DOM attribute is said to be <em>getting</em> when its value is being |
| 242 |
|
|
retrieved (e.g. by author script), and is said to be <em>setting</em> when |
| 243 |
|
|
a new value is assigned to it. |
| 244 |
|
|
|
| 245 |
|
|
<p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means |
| 246 |
|
|
that any attributes returning that object must always return the same |
| 247 |
|
|
object (not a new object each time), and the attributes and methods on |
| 248 |
|
|
that object must operate on the actual underlying data, not a snapshot of |
| 249 |
|
|
the data. |
| 250 |
|
|
|
| 251 |
wakaba |
1.5 |
<h2 id=infrastructure><span class=secno>2. </span>Infrastructure</h2> |
| 252 |
wakaba |
1.3 |
|
| 253 |
|
|
<h3 id=the-windowworker><span class=secno>2.1 </span>The <code><a |
| 254 |
|
|
href="#windowworker">WindowWorker</a></code> interface</h3> |
| 255 |
|
|
|
| 256 |
|
|
<pre |
| 257 |
|
|
class=idl>[NoInterfaceObject] interface <dfn id=windowworker>WindowWorker</dfn> { |
| 258 |
wakaba |
1.5 |
readonly attribute DOMString <a href="#url" title=dom-WindowWorker-URL>URL</a>; |
| 259 |
|
|
readonly attribute DOMString <a href="#name" title=dom-WindowWorker-name>name</a>; |
| 260 |
|
|
readonly attribute boolean <a href="#closing" title=dom-WindowWorker-closing>closing</a>; |
| 261 |
wakaba |
1.6 |
boolean <a href="#import" title=dom-WindowWorker-import>import</a>(in DOMString url); |
| 262 |
wakaba |
1.5 |
void <a href="#close" title=dom-WindowWorker-close>close</a>(); |
| 263 |
wakaba |
1.4 |
|
| 264 |
|
|
// event handler attributes |
| 265 |
wakaba |
1.6 |
attribute <span>EventListener</span> <a href="#onconnect" title=handler-WindowWorker-onconnect>onconnect</a>; |
| 266 |
wakaba |
1.5 |
attribute <span>EventListener</span> <a href="#onunload" title=handler-WindowWorker-onunload>onunload</a>; |
| 267 |
wakaba |
1.3 |
};</pre> |
| 268 |
|
|
|
| 269 |
wakaba |
1.5 |
<p>The <dfn id=url title=dom-WindowWorker-URL><code>URL</code></dfn> |
| 270 |
|
|
attribute must return the value it was assigned when the <code><a |
| 271 |
|
|
href="#windowworker">WindowWorker</a></code> object was created by the "<a |
| 272 |
|
|
href="#run-a">run a worker</a>" algorithm. It gives the <span>absolute |
| 273 |
|
|
URL</span> of the script that was used to initialize the worker. |
| 274 |
wakaba |
1.3 |
|
| 275 |
wakaba |
1.5 |
<p>The <dfn id=name title=dom-WindowWorker-name><code>name</code></dfn> |
| 276 |
wakaba |
1.3 |
attribute must return the value it was assigned when the <code><a |
| 277 |
|
|
href="#windowworker">WindowWorker</a></code> object was created by the "<a |
| 278 |
wakaba |
1.5 |
href="#run-a">run a worker</a>" algorithm. If it has a value that isn't |
| 279 |
|
|
the empty string, its value represents the name that can be used to obtain |
| 280 |
|
|
a reference to the worker using the <code |
| 281 |
|
|
title=dom-WindowWorkerCreators-createNamedWorker><a |
| 282 |
|
|
href="#createnamedworker">createNamedWorker()</a></code> method. |
| 283 |
wakaba |
1.3 |
|
| 284 |
|
|
<p>The <dfn id=closing |
| 285 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><code>closing</code></dfn> attribute must |
| 286 |
wakaba |
1.6 |
return false until it is set to true by one of the algorithms in the |
| 287 |
|
|
processing model section below. |
| 288 |
wakaba |
1.3 |
|
| 289 |
wakaba |
1.4 |
<p>The following are the <span>event handler DOM attributes</span> that |
| 290 |
|
|
must be supported by objects implementing the <code><a |
| 291 |
|
|
href="#windowworker">WindowWorker</a></code> interface: |
| 292 |
|
|
|
| 293 |
|
|
<dl> |
| 294 |
wakaba |
1.6 |
<dt><dfn id=onconnect |
| 295 |
|
|
title=handler-WindowWorker-onconnect><code>onconnect</code></dfn> |
| 296 |
wakaba |
1.4 |
|
| 297 |
|
|
<dd> |
| 298 |
|
|
<p>Must be invoked whenever a <code |
| 299 |
wakaba |
1.6 |
title=event-WindowWorker-connect>connect</code> event is targeted at or |
| 300 |
wakaba |
1.4 |
bubbles through the <code><a |
| 301 |
|
|
href="#windowworker">WindowWorker</a></code> object. |
| 302 |
|
|
|
| 303 |
|
|
<dt><dfn id=onunload |
| 304 |
wakaba |
1.5 |
title=handler-WindowWorker-onunload><code>onunload</code></dfn> |
| 305 |
wakaba |
1.4 |
|
| 306 |
|
|
<dd> |
| 307 |
|
|
<p>Must be invoked whenever a <code title=event-unload>unload</code> |
| 308 |
|
|
event is targeted at or bubbles through the <code><a |
| 309 |
|
|
href="#windowworker">WindowWorker</a></code> object. |
| 310 |
|
|
</dl> |
| 311 |
|
|
|
| 312 |
wakaba |
1.6 |
<h3 id=base-urls><span class=secno>2.2 </span>Base URLs and origins of |
| 313 |
|
|
workers</h3> |
| 314 |
|
|
|
| 315 |
|
|
<p>The <span>base URL</span> of a <span>URL</span> passed to an API in a |
| 316 |
|
|
worker is the <span>absolute URL</span> given by the value of the <code |
| 317 |
|
|
title=dom-WindowWorker-URL><a href="#url">URL</a></code> attribute of the |
| 318 |
|
|
worker's <code><a href="#windowworker">WindowWorker</a></code> object. |
| 319 |
|
|
|
| 320 |
|
|
<p>The <span>origin</span> and <span>effective script origin</span> of |
| 321 |
|
|
scripts running in workers are both the <span>origin</span> of the |
| 322 |
|
|
<span>absolute URL</span> given by the value of the <code |
| 323 |
|
|
title=dom-WindowWorker-URL><a href="#url">URL</a></code> attribute of the |
| 324 |
|
|
worker's <code><a href="#windowworker">WindowWorker</a></code> object. |
| 325 |
|
|
|
| 326 |
|
|
<h3 id=the-queue><span class=secno>2.3 </span>The queue of events</h3> |
| 327 |
wakaba |
1.3 |
|
| 328 |
|
|
<p>Each <code><a href="#windowworker">WindowWorker</a></code> object is |
| 329 |
|
|
asssociated with a <dfn id=queue>queue of events</dfn>, which is initially |
| 330 |
|
|
empty. |
| 331 |
|
|
|
| 332 |
|
|
<p>An event in the queue can be a DOM event or a timeout callback. |
| 333 |
|
|
|
| 334 |
wakaba |
1.4 |
<p>All asynchronous callbacks and events that would be called or dispatched |
| 335 |
|
|
in the worker must be added to the worker's queue, with the "<a |
| 336 |
|
|
href="#run-a">run a worker</a>" processing model below taking care of |
| 337 |
|
|
actually calling the callbacks or dispatching the events. |
| 338 |
|
|
|
| 339 |
|
|
<p>Once the <code><a href="#windowworker">WindowWorker</a></code>'s <code |
| 340 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><a href="#closing">closing</a></code> |
| 341 |
wakaba |
1.4 |
attribute is set to true, the queue must discard anything else that would |
| 342 |
|
|
be added to it. Effectively, once the <code |
| 343 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><a href="#closing">closing</a></code> |
| 344 |
wakaba |
1.4 |
attribute is true, timers stop firing, notifications for all pending |
| 345 |
|
|
asynchronous operations are dropped, etc. |
| 346 |
|
|
|
| 347 |
wakaba |
1.6 |
<h3 id=the-workers><span class=secno>2.4 </span>The worker's ports</h3> |
| 348 |
wakaba |
1.4 |
|
| 349 |
|
|
<p>Workers communicate with other workers and with <span title="browsing |
| 350 |
|
|
context">browsing contexts</span> through <span title="channel |
| 351 |
|
|
messaging">message channels</span> and their <code>MessagePort</code> |
| 352 |
|
|
objects. |
| 353 |
|
|
|
| 354 |
|
|
<p>Each <code><a href="#windowworker">WindowWorker</a></code> <var |
| 355 |
|
|
title="">worker</var> has a list of <dfn id=the-workers0>the worker's |
| 356 |
|
|
ports</dfn>, which consists of all the <code>MessagePort</code> objects |
| 357 |
|
|
that are entangled with another port and that have one (but only one) port |
| 358 |
|
|
whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is <var |
| 359 |
|
|
title="">worker</var>. This list includes all the <code>MessagePort</code> |
| 360 |
|
|
objects that are in events pending in the <a href="#queue">queue of |
| 361 |
|
|
events</a>. |
| 362 |
|
|
|
| 363 |
wakaba |
1.6 |
<hr> |
| 364 |
|
|
|
| 365 |
|
|
<p>A worker is said to be a <dfn id=front-line>front-line worker</dfn> if |
| 366 |
|
|
at least one of the <a href="#the-workers0">the worker's ports</a> has an |
| 367 |
|
|
entangled <code>MessagePort</code> whose <code |
| 368 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> is a |
| 369 |
|
|
<code>WindowBrowsingContext</code> object. |
| 370 |
|
|
|
| 371 |
|
|
<p>A worker is said to be a <dfn id=needed>needed worker</dfn> if either: |
| 372 |
|
|
|
| 373 |
|
|
<ul> |
| 374 |
|
|
<li>it is a <a href="#front-line">front-line worker</a>, or |
| 375 |
|
|
|
| 376 |
|
|
<li>at least one of the <a href="#the-workers0">the worker's ports</a> has |
| 377 |
|
|
an entangled <code>MessagePort</code> whose <code |
| 378 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> is a <code><a |
| 379 |
|
|
href="#windowworker">WindowWorker</a></code> object that is itself a <a |
| 380 |
|
|
href="#needed">needed worker</a>. |
| 381 |
|
|
</ul> |
| 382 |
|
|
|
| 383 |
|
|
<hr> |
| 384 |
|
|
|
| 385 |
|
|
<p>A worker is said to be an <dfn id=active>active front-line worker</dfn> |
| 386 |
|
|
if at least one of the <a href="#the-workers0">the worker's ports</a> has |
| 387 |
|
|
both an <code title=dom-MessagePort-active>active</code> attribute whose |
| 388 |
|
|
value is true and an entangled <code>MessagePort</code> whose <code |
| 389 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> is a |
| 390 |
|
|
<code>WindowBrowsingContext</code> object. |
| 391 |
|
|
|
| 392 |
|
|
<p>A worker is said to be an <dfn id=active0>active needed worker</dfn> if |
| 393 |
|
|
either: |
| 394 |
|
|
|
| 395 |
|
|
<ul> |
| 396 |
|
|
<li>it is an <a href="#active">active front-line worker</a>, or |
| 397 |
|
|
|
| 398 |
|
|
<li>at least one of the <a href="#the-workers0">the worker's ports</a> has |
| 399 |
|
|
an entangled <code>MessagePort</code> whose <code |
| 400 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> is a <code><a |
| 401 |
|
|
href="#windowworker">WindowWorker</a></code> object that is itself an <a |
| 402 |
|
|
href="#active0">active needed worker</a>. |
| 403 |
|
|
</ul> |
| 404 |
|
|
|
| 405 |
|
|
<h3 id=processing><span class=secno>2.5 </span>Processing model</h3> |
| 406 |
wakaba |
1.3 |
|
| 407 |
|
|
<p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var |
| 408 |
wakaba |
1.4 |
title="">name</var> for a script with <span>URL</span> <var |
| 409 |
|
|
title="">url</var>, a browsing context <var title="">owner browsing |
| 410 |
|
|
context</var> and a <code>Document</code> <var title="">owner |
| 411 |
|
|
document</var>, it must run the following steps in a completely separate |
| 412 |
|
|
and parallel execution environment: |
| 413 |
wakaba |
1.3 |
|
| 414 |
|
|
<ol> |
| 415 |
|
|
<li> |
| 416 |
wakaba |
1.4 |
<p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by |
| 417 |
|
|
<var title="">url</var>.</p> |
| 418 |
|
|
|
| 419 |
|
|
<p>If the attempt fails, then abort these steps and invoke the <a |
| 420 |
wakaba |
1.5 |
href="#worker" title="worker creation failed">error handling steps</a> |
| 421 |
wakaba |
1.4 |
defined by the algorithm that called this one.</p> |
| 422 |
|
|
|
| 423 |
|
|
<p>If the attempt succeeds, then let <var title="">script</var> be the |
| 424 |
|
|
resource that was obtained.</p> |
| 425 |
|
|
|
| 426 |
|
|
<p class=note>As with <code>script</code> elements, the MIME type of the |
| 427 |
|
|
script is ignored. Unlike with <code>script</code> elements, there is no |
| 428 |
|
|
way to override the type. It's always assumed to be JavaScript.</p> |
| 429 |
|
|
<!-- XXX people will complain about |
| 430 |
|
|
this. I guess we might want to examine the MIME type... --> |
| 431 |
|
|
|
| 432 |
|
|
|
| 433 |
|
|
<li> |
| 434 |
wakaba |
1.3 |
<p>Create a new <code><a href="#windowworker">WindowWorker</a></code> |
| 435 |
wakaba |
1.6 |
object, <var title="">worker</var>.</p> |
| 436 |
wakaba |
1.3 |
|
| 437 |
|
|
<li> |
| 438 |
wakaba |
1.5 |
<p>Set the <code title=dom-WindowWorker-URL><a href="#url">URL</a></code> |
| 439 |
wakaba |
1.6 |
attribute of <var title="">worker</var> to the value of <var |
| 440 |
wakaba |
1.5 |
title="">url</var>.</p> |
| 441 |
|
|
|
| 442 |
|
|
<li> |
| 443 |
|
|
<p>Set the <code title=dom-WindowWorker-name><a |
| 444 |
wakaba |
1.6 |
href="#name">name</a></code> attribute of <var title="">worker</var> to |
| 445 |
wakaba |
1.3 |
the value of <var title="">name</var>.</p> |
| 446 |
|
|
|
| 447 |
|
|
<li> |
| 448 |
|
|
<p>Let <var title="">script</var>'s <span>script execution context</span> |
| 449 |
|
|
(and thus also <span>global object</span>) be <var |
| 450 |
wakaba |
1.6 |
title="">worker</var>.</p> |
| 451 |
wakaba |
1.3 |
|
| 452 |
|
|
<li> |
| 453 |
|
|
<p>Let <var title="">script</var>'s <span>script browsing context</span> |
| 454 |
|
|
be <var title="">owner browsing context</var>.</p> |
| 455 |
|
|
|
| 456 |
|
|
<li> |
| 457 |
|
|
<p>Let <var title="">script</var>'s <span>script document context</span> |
| 458 |
|
|
be <var title="">owner document</var>.</p> |
| 459 |
|
|
|
| 460 |
|
|
<li> |
| 461 |
wakaba |
1.5 |
<p>Invoke the <a href="#success" title="worker creation |
| 462 |
|
|
succeeded">success steps</a> defined by the algorithm that called this |
| 463 |
wakaba |
1.6 |
one.</p> |
| 464 |
|
|
|
| 465 |
|
|
<p class=note>This will usually add an event to the <a |
| 466 |
|
|
href="#queue">queue of events</a>. If it does, that event will have a |
| 467 |
|
|
<code>MessagePort</code> and thus the list of <a |
| 468 |
|
|
href="#the-workers0">the worker's ports</a> will not be empty. If it |
| 469 |
|
|
doesn't, then the next step will set the <var title="">worker</var> |
| 470 |
|
|
object's <code title=dom-WindowWorker-closing><a |
| 471 |
|
|
href="#closing">closing</a></code> attribute to true.</p> |
| 472 |
|
|
|
| 473 |
|
|
<li> |
| 474 |
|
|
<p>Start monitoring <var title="">worker</var>, such that as soon as the |
| 475 |
|
|
worker stops being a <a href="#needed">needed worker</a>, the <var |
| 476 |
|
|
title="">worker</var> object's <code title=dom-WindowWorker-closing><a |
| 477 |
|
|
href="#closing">closing</a></code> attribute is set to true.</p> |
| 478 |
wakaba |
1.4 |
|
| 479 |
|
|
<li> |
| 480 |
|
|
<p>Start monitoring <var title="">worker</var>, such that whenever the |
| 481 |
wakaba |
1.6 |
<var title="">worker</var> object's <code |
| 482 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><a href="#closing">closing</a></code> |
| 483 |
wakaba |
1.6 |
attribute is false and the worker is not an <a href="#active0">active |
| 484 |
|
|
needed worker</a>, the user agent suspends execution of script in that |
| 485 |
|
|
worker until such time as either the <code |
| 486 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><a href="#closing">closing</a></code> |
| 487 |
wakaba |
1.6 |
attribute switches to true or the worker becomes an <a |
| 488 |
|
|
href="#active0">active needed worker</a>.</p> |
| 489 |
wakaba |
1.4 |
|
| 490 |
|
|
<li> |
| 491 |
|
|
<p>Run <var title="">script</var> until it either returns, fails to catch |
| 492 |
|
|
an exception, or gets prematurely aborted by the "<a href="#kill-a">kill |
| 493 |
|
|
a worker</a>" algorithm below.</p> |
| 494 |
|
|
|
| 495 |
|
|
<p class=note>If the script gets aborted by the "<a href="#kill-a">kill a |
| 496 |
|
|
worker</a>" algorithm, then that same algorithm will cause there to only |
| 497 |
|
|
be a single event in the <a href="#queue">queue of events</a> at the |
| 498 |
|
|
next step, namely the <code title=message-unload>unload</code> event. |
| 499 |
|
|
However, if the event is ignored then it will become true as soon as |
| 500 |
|
|
that port is garbage collected.</p> |
| 501 |
|
|
|
| 502 |
|
|
<li> |
| 503 |
|
|
<p><i>Event loop</i>: Wait until either there is an event in the <a |
| 504 |
wakaba |
1.3 |
href="#queue">queue of events</a> associated with <var |
| 505 |
wakaba |
1.6 |
title="">worker</var> or the <var title="">worker</var> object's <code |
| 506 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><a href="#closing">closing</a></code> |
| 507 |
wakaba |
1.4 |
attribute is set to true.</p> |
| 508 |
wakaba |
1.3 |
|
| 509 |
|
|
<li> |
| 510 |
|
|
<p>Dispatch the oldest event or callback in the <a href="#queue">queue of |
| 511 |
wakaba |
1.4 |
events</a>, if any. The handling of this event or the execution of this |
| 512 |
|
|
callback might get prematurely aborted by the "<a href="#kill-a">kill a |
| 513 |
|
|
worker</a>" algorithm below.</p> |
| 514 |
wakaba |
1.3 |
|
| 515 |
|
|
<li> |
| 516 |
|
|
<p>If there are any more events in the <a href="#queue">queue of |
| 517 |
wakaba |
1.6 |
events</a> or if the <var title="">worker</var> object's <code |
| 518 |
wakaba |
1.5 |
title=dom-WindowWorker-closing><a href="#closing">closing</a></code> |
| 519 |
wakaba |
1.3 |
attribute is set to false, then jump back to the step above labeled |
| 520 |
|
|
<i>event loop</i>.</p> |
| 521 |
|
|
|
| 522 |
|
|
<li> |
| 523 |
|
|
<p class=big-issue>timers, intervals, XMLHttpRequests, database |
| 524 |
|
|
transactions, etc, must be killed; ports must be deactivated and |
| 525 |
wakaba |
1.4 |
unentangled (do not send unload)</p> |
| 526 |
wakaba |
1.3 |
</ol> |
| 527 |
|
|
|
| 528 |
|
|
<hr> |
| 529 |
|
|
|
| 530 |
|
|
<p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run |
| 531 |
|
|
the following steps in parallel with the worker's main loop (the "<a |
| 532 |
|
|
href="#run-a">run a worker</a>" processing model defined above): |
| 533 |
|
|
|
| 534 |
|
|
<ol> |
| 535 |
|
|
<li> |
| 536 |
|
|
<p>Create an <code>Event</code> object with the event name <code |
| 537 |
|
|
title=event-unload>unload</code>, which does not bubble and is not |
| 538 |
|
|
cancelable, and add it to the worker's <code><a |
| 539 |
|
|
href="#windowworker">WindowWorker</a></code> object's <a |
| 540 |
wakaba |
1.4 |
href="#queue">queue of events</a>, targetted at the <code><a |
| 541 |
|
|
href="#windowworker">WindowWorker</a></code> object itself. |
| 542 |
|
|
|
| 543 |
|
|
<li> |
| 544 |
|
|
<p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code> |
| 545 |
wakaba |
1.5 |
object's <code title=dom-WindowWorker-closing><a |
| 546 |
wakaba |
1.4 |
href="#closing">closing</a></code> attribute to true. |
| 547 |
wakaba |
1.3 |
|
| 548 |
|
|
<li> |
| 549 |
wakaba |
1.4 |
<p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run |
| 550 |
|
|
a worker</a>" processing model defined above immediately starts running |
| 551 |
|
|
event listeners registered for <code title=event-unload>unload</code> |
| 552 |
|
|
event, this time should not be zero — the idea is that the <code |
| 553 |
|
|
title=event-unload>unload</code> event can be used to clean up when |
| 554 |
|
|
shutting down unexpectedly. |
| 555 |
wakaba |
1.3 |
|
| 556 |
|
|
<li> |
| 557 |
|
|
<p>If there are any events in the <a href="#queue">queue of events</a> |
| 558 |
|
|
other than the <code title=event-unload>unload</code> event that this |
| 559 |
|
|
algorithm just added, discard them without dispatching them. |
| 560 |
|
|
|
| 561 |
|
|
<li> |
| 562 |
|
|
<p>If the <code title=event-unload>unload</code> event that this |
| 563 |
|
|
algorithm just added hasn't yet been dispatched, then abort the script |
| 564 |
|
|
currently running in the worker. |
| 565 |
|
|
|
| 566 |
|
|
<li> |
| 567 |
|
|
<p>Wait a user-agent-defined amount of time. |
| 568 |
|
|
|
| 569 |
|
|
<li> |
| 570 |
|
|
<p>Abort the script currently running in the worker (if any script is |
| 571 |
|
|
running, then it will be a handler for the <code |
| 572 |
|
|
title=event-unload>unload</code> event). |
| 573 |
|
|
</ol> |
| 574 |
|
|
|
| 575 |
wakaba |
1.6 |
<p>User agents may invoke the "<a href="#kill-a">kill a worker</a>" |
| 576 |
|
|
processing model on a worker at any time, e.g. in response to user |
| 577 |
|
|
requests, in response to CPU quota management, or when a worker stops |
| 578 |
|
|
being a <a href="#needed">needed worker</a> if the worker continues |
| 579 |
|
|
executing even after its <code title=dom-WindowWorker-closing><a |
| 580 |
|
|
href="#closing">closing</a></code> attribute was set to true. |
| 581 |
|
|
|
| 582 |
|
|
<hr> |
| 583 |
|
|
|
| 584 |
|
|
<p>When a script invokes the <dfn id=import |
| 585 |
|
|
title=dom-WindowWorker-import><code>import(<var |
| 586 |
|
|
title="">url</var>)</code></dfn> method on a <code><a |
| 587 |
|
|
href="#windowworker">WindowWorker</a></code> object, the user agent must |
| 588 |
|
|
run the following steps: |
| 589 |
|
|
|
| 590 |
|
|
<ol> |
| 591 |
|
|
<li> |
| 592 |
|
|
<p><span title="resolve a url">Resolve</span> the <var title="">url</var> |
| 593 |
|
|
argument. |
| 594 |
|
|
|
| 595 |
|
|
<li> |
| 596 |
|
|
<p>If this fails, throw a <code>SYNTAX_ERR</code> exception. |
| 597 |
|
|
|
| 598 |
|
|
<li> |
| 599 |
|
|
<p>If the <span>origin</span> of the resulting <span>absolute URL</span> |
| 600 |
|
|
is not the <span title="same origin">same</span> as the origin of the |
| 601 |
|
|
script that invoked the method, then throw a <span>security |
| 602 |
|
|
exception</span>. |
| 603 |
|
|
|
| 604 |
|
|
<li> |
| 605 |
|
|
<p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by |
| 606 |
|
|
the resulting <span>absolute URL</span>.</p> |
| 607 |
|
|
|
| 608 |
|
|
<p>If the attempt fails, return false and abort these steps.</p> |
| 609 |
|
|
|
| 610 |
|
|
<p>If the attempt succeeds, then let <var title="">script</var> be the |
| 611 |
|
|
resource that was obtained.</p> |
| 612 |
|
|
|
| 613 |
|
|
<p class=note>As with the initial script, the script here is always |
| 614 |
|
|
assumed to be JavaScript, regardless of the MIME type.</p> |
| 615 |
|
|
<!-- XXX --> |
| 616 |
|
|
|
| 617 |
|
|
<li> |
| 618 |
|
|
<p>Let <var title="">script</var>'s <span>script execution |
| 619 |
|
|
context</span>, <span>script browsing context</span>, and <span>script |
| 620 |
|
|
document context</span> be the same as for the script that was executed |
| 621 |
|
|
by the <a href="#run-a">run a worker</a> processing model for this |
| 622 |
|
|
worker.</p> |
| 623 |
|
|
|
| 624 |
|
|
<li> |
| 625 |
|
|
<p>Run <var title="">script</var> until it either returns, fails to catch |
| 626 |
|
|
an exception, or gets prematurely aborted by the "<a href="#kill-a">kill |
| 627 |
|
|
a worker</a>" algorithm below.</p> |
| 628 |
|
|
|
| 629 |
|
|
<p>If an exception was raised or if the script was prematurely aborted, |
| 630 |
|
|
then abort these steps, letting the exception or aborting continue to be |
| 631 |
|
|
processed by the script that called the <code |
| 632 |
|
|
title=dom-WindowWorker-import><a href="#import">import()</a></code> |
| 633 |
|
|
method.</p> |
| 634 |
|
|
|
| 635 |
|
|
<p>Otherwise, return true.</p> |
| 636 |
|
|
</ol> |
| 637 |
|
|
|
| 638 |
wakaba |
1.4 |
<hr> |
| 639 |
|
|
|
| 640 |
|
|
<p>When a script invokes the <dfn id=close |
| 641 |
wakaba |
1.5 |
title=dom-WindowWorker-close><code>close()</code></dfn> method on a |
| 642 |
wakaba |
1.4 |
<code><a href="#windowworker">WindowWorker</a></code> object, the user |
| 643 |
|
|
agent must run the following steps: |
| 644 |
|
|
|
| 645 |
|
|
<ol> |
| 646 |
|
|
<li> |
| 647 |
|
|
<p>Create an <code>Event</code> object with the event name <code |
| 648 |
|
|
title=event-unload>unload</code>, which does not bubble and is not |
| 649 |
|
|
cancelable, and add it to the <code><a |
| 650 |
|
|
href="#windowworker">WindowWorker</a></code> object's <a |
| 651 |
|
|
href="#queue">queue of events</a>, targetted at the <code><a |
| 652 |
|
|
href="#windowworker">WindowWorker</a></code> object itself. |
| 653 |
|
|
|
| 654 |
|
|
<li> |
| 655 |
|
|
<p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code> |
| 656 |
wakaba |
1.5 |
object's <code title=dom-WindowWorker-closing><a |
| 657 |
wakaba |
1.4 |
href="#closing">closing</a></code> attribute to true. |
| 658 |
|
|
|
| 659 |
|
|
<li> |
| 660 |
|
|
<p>For each <code>MessagePort</code> object that is entangled with |
| 661 |
|
|
another port and that has one (but only one) port whose <code |
| 662 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> is the <code><a |
| 663 |
|
|
href="#windowworker">WindowWorker</a></code> object on which the method |
| 664 |
|
|
was invoked, run the following substeps:</p> |
| 665 |
|
|
|
| 666 |
|
|
<ol> |
| 667 |
|
|
<li> |
| 668 |
|
|
<p>Unentangle the two ports. |
| 669 |
|
|
|
| 670 |
|
|
<li> |
| 671 |
|
|
<p>Set both ports' <code title=dom-MessagePort-active>active</code> |
| 672 |
|
|
attribute to false. |
| 673 |
|
|
|
| 674 |
|
|
<li> |
| 675 |
|
|
<p>At the next available opportunity, after any scripts have finished |
| 676 |
|
|
executing<!-- XXX queue -->, <span>fire a simple event</span> called |
| 677 |
|
|
<code title=event-unload>unload</code> at the other port (the one |
| 678 |
|
|
whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is |
| 679 |
|
|
not the <code><a href="#windowworker">WindowWorker</a></code> object |
| 680 |
wakaba |
1.5 |
on which the <code title=dom-WindowWorker-close><a |
| 681 |
wakaba |
1.4 |
href="#close">close()</a></code> method was called). |
| 682 |
|
|
</ol> |
| 683 |
|
|
</ol> |
| 684 |
|
|
|
| 685 |
wakaba |
1.6 |
<h3 id=creating><span class=secno>2.6 </span>Creating workers</h3> |
| 686 |
wakaba |
1.3 |
|
| 687 |
|
|
<pre |
| 688 |
|
|
class=idl>[NoInterfaceObject] interface <dfn id=windowworkercreators>WindowWorkerCreators</dfn> { |
| 689 |
wakaba |
1.5 |
<span>MessagePort</span> <a href="#createworker" title=dom-WindowWorkerCreators-createWorker>createWorker</a>(in DOMString scriptURL); |
| 690 |
|
|
<span>MessagePort</span> <a href="#createnamedworker" title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker</a>(in DOMString name, in DOMString scriptURL); |
| 691 |
wakaba |
1.3 |
};</pre> |
| 692 |
|
|
|
| 693 |
|
|
<p>Objects that implement the <code>Window</code> interface must also |
| 694 |
|
|
implement the <code><a |
| 695 |
|
|
href="#windowworkercreators">WindowWorkerCreators</a></code> interface. |
| 696 |
wakaba |
1.4 |
|
| 697 |
|
|
<hr> |
| 698 |
|
|
|
| 699 |
wakaba |
1.5 |
<p>When the <dfn id=createworker |
| 700 |
|
|
title=dom-WindowWorkerCreators-createWorker><code>createWorker(<var |
| 701 |
|
|
title="">scriptURL</var>)</code></dfn> method is invoked, the user agent |
| 702 |
|
|
must run the following steps: |
| 703 |
wakaba |
1.4 |
|
| 704 |
|
|
<ol> |
| 705 |
wakaba |
1.5 |
<li> |
| 706 |
|
|
<p><span title="resolve a url">Resolve</span> the <var |
| 707 |
|
|
title="">scriptURL</var> argument. |
| 708 |
|
|
|
| 709 |
|
|
<li> |
| 710 |
|
|
<p>If this fails, throw a <code>SYNTAX_ERR</code> exception. |
| 711 |
|
|
|
| 712 |
|
|
<li> |
| 713 |
|
|
<p>If the <span>origin</span> of the resulting <span>absolute URL</span> |
| 714 |
|
|
is not the <span title="same origin">same</span> as the origin of the |
| 715 |
|
|
script that invoked the method, then throw a <span>security |
| 716 |
|
|
exception</span>. |
| 717 |
|
|
|
| 718 |
|
|
<li> |
| 719 |
|
|
<p><a href="#create">Create a worker</a> from the resulting |
| 720 |
|
|
<span>absolute URL</span> whose name is the empty string. |
| 721 |
|
|
|
| 722 |
|
|
<li> |
| 723 |
|
|
<p>Return the <code>MessagePort</code> object returned from the <a |
| 724 |
|
|
href="#create">create a worker</a> algorithm. |
| 725 |
wakaba |
1.4 |
</ol> |
| 726 |
|
|
|
| 727 |
|
|
<hr> |
| 728 |
|
|
|
| 729 |
wakaba |
1.5 |
<p>When the <dfn id=createnamedworker |
| 730 |
|
|
title=dom-WindowWorkerCreators-createNamedWorker><code>createNamedWorker(<var |
| 731 |
|
|
title="">name</var>, <var title="">scriptURL</var>)</code></dfn> method is |
| 732 |
|
|
invoked, the user agent must run the following steps: |
| 733 |
wakaba |
1.4 |
|
| 734 |
|
|
<ol> |
| 735 |
wakaba |
1.5 |
<li> |
| 736 |
|
|
<p><span title="resolve a url">Resolve</span> the <var |
| 737 |
|
|
title="">scriptURL</var> argument. |
| 738 |
|
|
|
| 739 |
|
|
<li> |
| 740 |
|
|
<p>If this fails, throw a <code>SYNTAX_ERR</code> exception. |
| 741 |
|
|
|
| 742 |
|
|
<li> |
| 743 |
|
|
<p>If the <span>origin</span> of the resulting <span>absolute URL</span> |
| 744 |
|
|
is not the <span title="same origin">same</span> as the origin of the |
| 745 |
|
|
script that invoked the method, then throw a <span>security |
| 746 |
|
|
exception</span>. |
| 747 |
|
|
|
| 748 |
|
|
<li> |
| 749 |
|
|
<p>If the <var title="">name</var> argument is the empty string, <a |
| 750 |
|
|
href="#create">create a worker</a> from the resulting <span>absolute |
| 751 |
|
|
URL</span>, whose name is the empty string, and return the |
| 752 |
|
|
<code>MessagePort</code> object returned from the <a |
| 753 |
|
|
href="#create">create a worker</a> algorithm. Then, abort these steps. |
| 754 |
|
|
|
| 755 |
|
|
<li> |
| 756 |
|
|
<p>If there exists a worker whose <code title=dom-WindowWorker-closing><a |
| 757 |
|
|
href="#closing">closing</a></code> attribute is false, whose <code |
| 758 |
|
|
title=dom-WindowWorker-name><a href="#name">name</a></code> attribute is |
| 759 |
|
|
exactly equal to the <var title="">name</var> argument, and whose <code |
| 760 |
|
|
title=dom-WindowWorker-URL><a href="#url">URL</a></code> attribute has |
| 761 |
|
|
the <span>same origin</span> as the resulting <span>absolute URL</span>, |
| 762 |
|
|
then run these substeps:</p> |
| 763 |
|
|
|
| 764 |
|
|
<ol> |
| 765 |
|
|
<li> |
| 766 |
|
|
<p>If that worker's <code title=dom-WindowWorker-URL><a |
| 767 |
|
|
href="#url">URL</a></code> attribute is not exactly equal to the |
| 768 |
|
|
resulting <span>absolute URL</span>, then throw a |
| 769 |
|
|
<code>URL_MISMATCH_ERR</code> exception and abort these steps. <span |
| 770 |
|
|
class=big-issue>code 19</span> |
| 771 |
|
|
|
| 772 |
|
|
<li> |
| 773 |
|
|
<p><span>Create a new <code>MessagePort</code> object</span> owned by |
| 774 |
|
|
the <span>script execution context</span> of the script that invoked |
| 775 |
|
|
the method. |
| 776 |
|
|
|
| 777 |
|
|
<li> |
| 778 |
|
|
<p>Return that port. |
| 779 |
|
|
|
| 780 |
|
|
<li> |
| 781 |
wakaba |
1.6 |
<p>Asynchronously, <a href="#connect" title="connect to a |
| 782 |
|
|
worker">connect</a> to this preexisting worker, with the newly created |
| 783 |
wakaba |
1.5 |
port. |
| 784 |
|
|
</ol> |
| 785 |
|
|
|
| 786 |
|
|
<p>Otherwise, <a href="#create">create a worker</a> from the resulting |
| 787 |
|
|
<span>absolute URL</span>, whose name is the value of the <var |
| 788 |
|
|
title="">name</var> argument, and return the <code>MessagePort</code> |
| 789 |
|
|
object returned from the <a href="#create">create a worker</a> |
| 790 |
|
|
algorithm.</p> |
| 791 |
wakaba |
1.4 |
</ol> |
| 792 |
|
|
|
| 793 |
|
|
<hr> |
| 794 |
|
|
|
| 795 |
wakaba |
1.5 |
<p>The steps to <dfn id=create>create a worker</dfn> from a |
| 796 |
|
|
<span>URL</span> <var title="">url</var> and whose name is <var |
| 797 |
|
|
title="">name</var>, in the context of a method call, are as follows: |
| 798 |
wakaba |
1.4 |
|
| 799 |
|
|
<ol> |
| 800 |
wakaba |
1.5 |
<li> |
| 801 |
|
|
<p><span>Create a new <code>MessagePort</code> object</span> owned by the |
| 802 |
|
|
<span>script execution context</span> of the script that invoked the |
| 803 |
|
|
method. |
| 804 |
|
|
|
| 805 |
|
|
<li> |
| 806 |
|
|
<p>Return that port. |
| 807 |
|
|
|
| 808 |
|
|
<li> |
| 809 |
|
|
<p>In a parallel execution context (i.e. a separate thread or process or |
| 810 |
|
|
equivalent construct), <a href="#run-a">run a worker</a> named <var |
| 811 |
|
|
title="">name</var> for the script with <span>URL</span> <var |
| 812 |
|
|
title="">url</var>, with the <span>script browsing context</span> of the |
| 813 |
|
|
script that invoked the method as the <var title="">owner browsing |
| 814 |
|
|
context</var> and with the <span>script document context</span> of the |
| 815 |
|
|
script that invoked the method as the <var title="">owner |
| 816 |
|
|
document</var>.</p> |
| 817 |
|
|
|
| 818 |
|
|
<p>If that algorithm invokes the steps for <dfn id=success title="worker |
| 819 |
wakaba |
1.6 |
creation succeeded">success steps</dfn>, then <a href="#connect" |
| 820 |
|
|
title="connect to a worker">connect</a> to this new worker, with the |
| 821 |
|
|
newly created port.</p> |
| 822 |
wakaba |
1.5 |
|
| 823 |
|
|
<p>Otherwise, if the <dfn id=worker>worker creation failed</dfn>, then at |
| 824 |
|
|
the next available opportunity, after any scripts have finished |
| 825 |
|
|
executing<!-- XXX queue -->, <span>fire a simple event</span> called |
| 826 |
|
|
<code title=event-error>error</code> at the newly created port.</p> |
| 827 |
wakaba |
1.4 |
</ol> |
| 828 |
|
|
|
| 829 |
|
|
<hr> |
| 830 |
|
|
|
| 831 |
wakaba |
1.6 |
<p>The steps to <dfn id=connect>connect to a worker</dfn> given a |
| 832 |
wakaba |
1.5 |
<code>MessagePort</code> object <var title="">port</var> are as follows: |
| 833 |
wakaba |
1.4 |
|
| 834 |
|
|
<ol> |
| 835 |
wakaba |
1.5 |
<li> |
| 836 |
|
|
<p>If <var title="">port</var> would have been garbage collected, or if |
| 837 |
|
|
the <span>active document</span> of the <code |
| 838 |
|
|
title=dom-MessagePort-ownerWindow>ownerWindow</code> of <var |
| 839 |
|
|
title="">port</var> is no longer the same <code>Document</code> object |
| 840 |
|
|
as when <var title="">port</var> was created, then do nothing. Abort |
| 841 |
|
|
these steps. If the worker was just created, it'll get killed |
| 842 |
|
|
immediately.</p> |
| 843 |
|
|
|
| 844 |
|
|
<li> |
| 845 |
|
|
<p><span>Create a new <code>MessagePort</code> object</span> owned by the |
| 846 |
|
|
<code><a href="#windowworker">WindowWorker</a></code> of the worker. |
| 847 |
|
|
|
| 848 |
|
|
<li> |
| 849 |
|
|
<p><span>Entangle</span> this newly created port and the port <var |
| 850 |
|
|
title="">port</var> that was passed to these steps. |
| 851 |
|
|
|
| 852 |
|
|
<li> |
| 853 |
|
|
<p>Set the <code title=dom-MessagePort-active>active</code> attribute of |
| 854 |
|
|
both ports to true. |
| 855 |
|
|
|
| 856 |
|
|
<li> |
| 857 |
|
|
<p>At the next available opportunity, after any scripts have finished |
| 858 |
|
|
executing<!-- XXX queue -->, <span>fire a simple event</span> called |
| 859 |
|
|
<code title=event-load>load</code> at <var title="">port</var>. |
| 860 |
|
|
|
| 861 |
|
|
<li> |
| 862 |
|
|
<p>Create an event that uses the <code>MessageEvent</code> interface, |
| 863 |
wakaba |
1.6 |
with the name <code title=event-connect>connect</code>, which does not |
| 864 |
wakaba |
1.5 |
bubble, is cancelable, has no default action, has a <code |
| 865 |
|
|
title=dom-MessageEvent-data>data</code> attribute whose value is the |
| 866 |
|
|
empty string and has a <code |
| 867 |
|
|
title=dom-MessageEvent-messagePort>messagePort</code> attribute whose |
| 868 |
|
|
value is the newly created port, and add it to the worker's <code><a |
| 869 |
|
|
href="#windowworker">WindowWorker</a></code> object's <a |
| 870 |
|
|
href="#queue">queue of events</a>, targetted at the <code><a |
| 871 |
|
|
href="#windowworker">WindowWorker</a></code> object itself. |
| 872 |
wakaba |
1.4 |
</ol> |
| 873 |
wakaba |
1.3 |
|
| 874 |
wakaba |
1.5 |
<h2 id=apis-available><span class=secno>3. </span>APIs available to workers</h2> |
| 875 |
|
|
|
| 876 |
wakaba |
1.6 |
<p>There must be no interface objects and constructors available in the |
| 877 |
|
|
global scope of scripts whose <span>script execution context</span> is a |
| 878 |
|
|
<code><a href="#windowworker">WindowWorker</a></code> object except for |
| 879 |
|
|
the following: |
| 880 |
|
|
|
| 881 |
|
|
<ul> |
| 882 |
|
|
<li> |
| 883 |
|
|
<p><code>XMLHttpRequest</code> and all interface objects and constructors |
| 884 |
|
|
defined by the XMLHttpRequest specifications, except that the |
| 885 |
|
|
<span>document response entity body</span> must always be null. <a |
| 886 |
|
|
href="#refsXHR">[XHR]</a> |
| 887 |
|
|
|
| 888 |
|
|
<li> |
| 889 |
|
|
<p>The <code>WebSocket</code> interface object and constructor. |
| 890 |
|
|
|
| 891 |
|
|
<li> |
| 892 |
|
|
<p>The <code>MessageChannel</code> interface object and constructor. |
| 893 |
|
|
</ul> |
| 894 |
|
|
|
| 895 |
|
|
<hr> |
| 896 |
|
|
|
| 897 |
wakaba |
1.5 |
<p>Objects that implement the <code><a |
| 898 |
|
|
href="#windowworker">WindowWorker</a></code> interface must also implement |
| 899 |
|
|
the following interfaces: |
| 900 |
|
|
|
| 901 |
|
|
<ul> |
| 902 |
|
|
<li> |
| 903 |
|
|
<p>The <code>Window</code> interface, and interfaces that are required to |
| 904 |
|
|
be implemented by objects implementing that interface, including:</p> |
| 905 |
|
|
|
| 906 |
|
|
<ul> |
| 907 |
|
|
<li>The <code>WindowTimers</code> interface |
| 908 |
|
|
|
| 909 |
|
|
<li>The <code><a |
| 910 |
|
|
href="#windowworkercreators">WindowWorkerCreators</a></code> interface |
| 911 |
|
|
</ul> |
| 912 |
|
|
|
| 913 |
|
|
<li>The <code>EventTarget</code> interface. |
| 914 |
|
|
</ul> |
| 915 |
wakaba |
1.3 |
|
| 916 |
wakaba |
1.6 |
<div class=note> |
| 917 |
|
|
<p>The following APIs are available in workers due to their being defined |
| 918 |
|
|
on the <code>Window</code> object or one of the interfaces that are |
| 919 |
|
|
required to be implemented by objects implementing that interface:</p> |
| 920 |
|
|
|
| 921 |
|
|
<ul> |
| 922 |
|
|
<li>The <code title=dom-showNotification>showNotification()</code> APIs. |
| 923 |
|
|
|
| 924 |
|
|
<li>The <code title=dom-localStorage>localStorage</code> object and |
| 925 |
|
|
related name/value pair storage APIs. |
| 926 |
|
|
|
| 927 |
|
|
<li>The <code title=dom-openDatabase>openDatabase()</code> method and the |
| 928 |
|
|
related SQL database API. |
| 929 |
|
|
</ul> |
| 930 |
|
|
</div> |
| 931 |
|
|
|
| 932 |
|
|
<p class=big-issue>Need to define a sync database API. |
| 933 |
|
|
|
| 934 |
|
|
<p class=big-issue>May need to define a browser sniffing API (like |
| 935 |
|
|
window.navigator).</p> |
| 936 |
|
|
<!-- XXX ApplicationCache --> |
| 937 |
|
|
<!-- XXX a way to set cookies on the URL for the script --> |
| 938 |
|
|
<!-- XXX debugging: void log(in DOMString s); // log to console --> |
| 939 |
|
|
<!-- XXX debugging: onerror --> |
| 940 |
|
|
|
| 941 |
|
|
<hr> |
| 942 |
|
|
|
| 943 |
|
|
<p>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects, |
| 944 |
|
|
etc) are not available to workers in this version of this specification. |
| 945 |
|
|
|
| 946 |
wakaba |
1.1 |
<h2 class=no-num id=references>References</h2> |
| 947 |
|
|
|
| 948 |
|
|
<p class=big-issue>This section will be written in a future |
| 949 |
|
|
draft.<!--XXX--> |
| 950 |
|
|
|
| 951 |
|
|
<h2 class=no-num id=acknowledgements>Acknowledgements</h2> |
| 952 |
|
|
<!-- ACKS --> |
| 953 |
|
|
|
| 954 |
wakaba |
1.6 |
<p>Thanks to Maciej Stachowiak, Mike Smith, and Philip Taylor for their |
| 955 |
|
|
useful and substantial comments. |
| 956 |
wakaba |
1.3 |
|
| 957 |
|
|
<p>Huge thanks to the whole Gears team, who pioneered this technology and |
| 958 |
|
|
whose experience has been a huge influence on this specification. |