Annotation of html5/workers/Overview.html, revision 1.301
1.281 ihickson 1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html lang="en-US-x-Hixie"><title>Web Workers</title><style type="text/css">
1.82 ihickson 2: pre { margin-left: 2em; white-space: pre-wrap; }
3: h2 { margin: 3em 0 1em 0; }
4: h3 { margin: 2.5em 0 1em 0; }
5: h4 { margin: 2.5em 0 0.75em 0; }
6: h5, h6 { margin: 2.5em 0 1em; }
7: h1 + h2, h1 + h2 + h2 { margin: 0.75em 0 0.75em; }
8: h2 + h3, h3 + h4, h4 + h5, h5 + h6 { margin-top: 0.5em; }
9: p { margin: 1em 0; }
10: hr:not(.top) { display: block; background: none; border: none; padding: 0; margin: 2em 0; height: auto; }
11: dl, dd { margin-top: 0; margin-bottom: 0; }
12: dt { margin-top: 0.75em; margin-bottom: 0.25em; clear: left; }
13: dt + dt { margin-top: 0; }
14: dd dt { margin-top: 0.25em; margin-bottom: 0; }
15: dd p { margin-top: 0; }
16: dd dl + p { margin-top: 1em; }
17: dd table + p { margin-top: 1em; }
18: p + * > li, dd li { margin: 1em 0; }
19: dt, dfn { font-weight: bold; font-style: normal; }
1.300 ihickson 20: i, em { font-style: italic; }
1.82 ihickson 21: dt dfn { font-style: italic; }
22: pre, code { font-size: inherit; font-family: monospace; font-variant: normal; }
23: pre strong { color: black; font: inherit; font-weight: bold; background: yellow; }
24: pre em { font-weight: bolder; font-style: normal; }
1.89 ihickson 25: @media screen { code { color: orangered; } code :link, code :visited { color: inherit; } }
1.82 ihickson 26: var sub { vertical-align: bottom; font-size: smaller; position: relative; top: 0.1em; }
27: table { border-collapse: collapse; border-style: hidden hidden none hidden; }
1.249 ihickson 28: table thead, table tbody { border-bottom: solid; }
1.82 ihickson 29: table tbody th:first-child { border-left: solid; }
1.180 ihickson 30: table tbody th { text-align: left; }
1.82 ihickson 31: table td, table th { border-left: solid; border-right: solid; border-bottom: solid thin; vertical-align: top; padding: 0.2em; }
32: blockquote { margin: 0 0 0 2em; border: 0; padding: 0; font-style: italic; }
33:
34: .bad, .bad *:not(.XXX) { color: gray; border-color: gray; background: transparent; }
35: .matrix, .matrix td { border: none; text-align: right; }
36: .matrix { margin-left: 2em; }
37: .dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
38: .dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
39: .dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
40:
41: .toc dfn, h1 dfn, h2 dfn, h3 dfn, h4 dfn, h5 dfn, h6 dfn { font: inherit; }
42: img.extra { float: right; }
43: pre.idl { border: solid thin; background: #EEEEEE; color: black; padding: 0.5em 1em; }
44: pre.idl :link, pre.idl :visited { color: inherit; background: transparent; }
45: pre.css { border: solid thin; background: #FFFFEE; color: black; padding: 0.5em 1em; }
46: pre.css:first-line { color: #AAAA50; }
1.231 ihickson 47: dl.domintro { color: green; margin: 2em 0 2em 2em; padding: 0.5em 1em; border: none; background: #DDFFDD; }
1.82 ihickson 48: hr + dl.domintro, div.impl + dl.domintro { margin-top: 2.5em; margin-bottom: 1.5em; }
49: dl.domintro dt, dl.domintro dt * { color: black; text-decoration: none; }
50: dl.domintro dd { margin: 0.5em 0 1em 2em; padding: 0; }
51: dl.domintro dd p { margin: 0.5em 0; }
52: dl.switch { padding-left: 2em; }
53: dl.switch > dt { text-indent: -1.5em; }
54: dl.switch > dt:before { content: '\21AA'; padding: 0 0.5em 0 0; display: inline-block; width: 1em; text-align: right; line-height: 0.5em; }
1.121 ihickson 55: dl.triple { padding: 0 0 0 1em; }
56: dl.triple dt, dl.triple dd { margin: 0; display: inline }
57: dl.triple dt:after { content: ':'; }
58: dl.triple dd:after { content: '\A'; white-space: pre; }
1.82 ihickson 59: .diff-old { text-decoration: line-through; color: silver; background: transparent; }
60: .diff-chg, .diff-new { text-decoration: underline; color: green; background: transparent; }
61: a .diff-new { border-bottom: 1px blue solid; }
62:
63: h2 { page-break-before: always; }
1.139 ihickson 64: h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
1.82 ihickson 65: h1 + h2, hr + h2.no-toc { page-break-before: auto; }
66:
1.267 ihickson 67: p > span:not([title=""]):not([class="XXX"]):not([class="impl"]):not([class="note"]),
68: li > span:not([title=""]):not([class="XXX"]):not([class="impl"]):not([class="note"]), { border-bottom: solid #9999CC; }
1.82 ihickson 69:
70: div.head { margin: 0 0 1em; padding: 1em 0 0 0; }
71: div.head p { margin: 0; }
72: div.head h1 { margin: 0; }
73: div.head .logo { float: right; margin: 0 1em; }
74: div.head .logo img { border: none } /* remove border from top image */
75: div.head dl { margin: 1em 0; }
1.212 ihickson 76: div.head p.copyright, div.head p.alt { font-size: x-small; font-style: oblique; margin: 0; }
1.82 ihickson 77:
78: body > .toc > li { margin-top: 1em; margin-bottom: 1em; }
79: body > .toc.brief > li { margin-top: 0.35em; margin-bottom: 0.35em; }
80: body > .toc > li > * { margin-bottom: 0.5em; }
81: body > .toc > li > * > li > * { margin-bottom: 0.25em; }
82: .toc, .toc li { list-style: none; }
83:
84: .brief { margin-top: 1em; margin-bottom: 1em; line-height: 1.1; }
85: .brief li { margin: 0; padding: 0; }
86: .brief li p { margin: 0; padding: 0; }
87:
1.137 ihickson 88: .category-list { margin-top: -0.75em; margin-bottom: 1em; line-height: 1.5; }
89: .category-list::before { content: '\21D2\A0'; font-size: 1.2em; font-weight: 900; }
90: .category-list li { display: inline; }
91: .category-list li:not(:last-child)::after { content: ', '; }
92: .category-list li > span, .category-list li > a { text-transform: lowercase; }
93: .category-list li * { text-transform: none; } /* don't affect <code> nested in <a> */
94:
1.82 ihickson 95: .XXX { color: #E50000; background: white; border: solid red; padding: 0.5em; margin: 1em 0; }
96: .XXX > :first-child { margin-top: 0; }
97: p .XXX { line-height: 3em; }
1.158 ihickson 98: .annotation { border: solid thin black; background: #0C479D; color: white; position: relative; margin: 8px 0 20px 0; }
99: .annotation:before { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 6px -6px -6px 6px; background: #333333; z-index: -1; content: ''; }
100: .annotation :link, .annotation :visited { color: inherit; }
101: .annotation :link:hover, .annotation :visited:hover { background: transparent; }
102: .annotation span { border: none ! important; }
1.82 ihickson 103: .note { color: green; background: transparent; font-family: sans-serif; }
104: .warning { color: red; background: transparent; }
105: .note, .warning { font-weight: bolder; font-style: italic; }
1.300 ihickson 106: .note em, .warning em, .note i, .warning i { font-style: normal; }
1.82 ihickson 107: p.note, div.note { padding: 0.5em 2em; }
108: span.note { padding: 0 2em; }
109: .note p:first-child, .warning p:first-child { margin-top: 0; }
110: .note p:last-child, .warning p:last-child { margin-bottom: 0; }
111: .warning:before { font-style: normal; }
112: p.note:before { content: 'Note: '; }
113: p.warning:before { content: '\26A0 Warning! '; }
114:
115: .bookkeeping:before { display: block; content: 'Bookkeeping details'; font-weight: bolder; font-style: italic; }
116: .bookkeeping { font-size: 0.8em; margin: 2em 0; }
117: .bookkeeping p { margin: 0.5em 2em; display: list-item; list-style: square; }
1.262 ihickson 118: .bookkeeping dt { margin: 0.5em 2em 0; }
119: .bookkeeping dd { margin: 0 3em 0.5em; }
1.82 ihickson 120:
121: h4 { position: relative; z-index: 3; }
122: h4 + .element, h4 + div + .element { margin-top: -2.5em; padding-top: 2em; }
123: .element {
1.88 ihickson 124: background: #EEEEFF;
1.82 ihickson 125: color: black;
126: margin: 0 0 1em 0.15em;
127: padding: 0 1em 0.25em 0.75em;
1.88 ihickson 128: border-left: solid #9999FF 0.25em;
1.82 ihickson 129: position: relative;
130: z-index: 1;
131: }
132: .element:before {
133: position: absolute;
134: z-index: 2;
135: top: 0;
136: left: -1.15em;
137: height: 2em;
138: width: 0.9em;
1.88 ihickson 139: background: #EEEEFF;
1.82 ihickson 140: content: ' ';
141: border-style: none none solid solid;
1.88 ihickson 142: border-color: #9999FF;
1.82 ihickson 143: border-width: 0.25em;
144: }
145:
1.156 ihickson 146: .example { display: block; color: #222222; background: #FCFCFC; border-left: double; margin-left: 2em; padding-left: 1em; }
147: td > .example:only-child { margin: 0 0 0 0.1em; }
1.82 ihickson 148:
1.131 ihickson 149: ul.domTree, ul.domTree ul { padding: 0 0 0 1em; margin: 0; }
150: ul.domTree li { padding: 0; margin: 0; list-style: none; position: relative; }
151: ul.domTree li li { list-style: none; }
152: ul.domTree li:first-child::before { position: absolute; top: 0; height: 0.6em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
153: ul.domTree li:not(:last-child)::after { position: absolute; top: 0; bottom: -0.6em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
154: ul.domTree span { font-style: italic; font-family: serif; }
155: ul.domTree .t1 code { color: purple; font-weight: bold; }
156: ul.domTree .t2 { font-style: normal; font-family: monospace; }
157: ul.domTree .t2 .name { color: black; font-weight: bold; }
158: ul.domTree .t2 .value { color: blue; font-weight: normal; }
159: ul.domTree .t3 code, .domTree .t4 code, .domTree .t5 code { color: gray; }
160: ul.domTree .t7 code, .domTree .t8 code { color: green; }
161: ul.domTree .t10 code { color: teal; }
162:
1.225 ihickson 163: body.dfnEnabled dfn { cursor: pointer; }
164: .dfnPanel {
165: display: inline;
166: position: absolute;
167: z-index: 10;
168: height: auto;
169: width: auto;
170: padding: 0.5em 0.75em;
171: font: small sans-serif, Droid Sans Fallback;
172: background: #DDDDDD;
173: color: black;
174: border: outset 0.2em;
175: }
176: .dfnPanel * { margin: 0; padding: 0; font: inherit; text-indent: 0; }
177: .dfnPanel :link, .dfnPanel :visited { color: black; }
178: .dfnPanel p { font-weight: bolder; }
179: .dfnPanel * + p { margin-top: 0.25em; }
180: .dfnPanel li { list-style-position: inside; }
181:
1.207 ihickson 182: #configUI { position: absolute; z-index: 20; top: 10em; right: 1em; width: 11em; font-size: small; }
183: #configUI p { margin: 0.5em 0; padding: 0.3em; background: #EEEEEE; color: black; border: inset thin; }
184: #configUI p label { display: block; }
185: #configUI #updateUI, #configUI .loginUI { text-align: center; }
186: #configUI input[type=button] { display: block; margin: auto; }
1.260 ihickson 187:
1.268 ihickson 188: fieldset { margin: 1em; padding: 0.5em 1em; }
189: fieldset > legend + * { margin-top: 0; }
1.266 ihickson 190: fieldset > :last-child { margin-bottom: 0; }
1.268 ihickson 191: fieldset p { margin: 0.5em 0; }
1.266 ihickson 192:
1.271 ihickson 193: </style><link href="http://www.w3.org/StyleSheets/TR/W3C-ED" rel="stylesheet" type="text/css"><script type="text/javascript">
194: function getCookie(name) {
195: var params = location.search.substr(1).split("&");
196: for (var index = 0; index < params.length; index++) {
197: if (params[index] == name)
198: return "1";
199: var data = params[index].split("=");
200: if (data[0] == name)
201: return unescape(data[1]);
202: }
203: var cookies = document.cookie.split("; ");
204: for (var index = 0; index < cookies.length; index++) {
205: var data = cookies[index].split("=");
206: if (data[0] == name)
207: return unescape(data[1]);
208: }
209: return null;
210: }
1.283 ihickson 211: </script><div class="head" id="head">
1.115 ihickson 212: <p><a href="http://www.w3.org/"><img alt="W3C" height="48" src="http://www.w3.org/Icons/w3c_home" width="72"></a></p>
1.271 ihickson 213:
1.54 ihickson 214: <h1>Web Workers</h1>
1.281 ihickson 215:
1.301 ! ihickson 216: <h2 class="no-num no-toc" id="editor-s-draft-5-august-2011">Editor's Draft 5 August 2011</h2>
1.218 ihickson 217: <dl><dt>Latest Published Version:</dt>
1.115 ihickson 218: <dd><a href="http://www.w3.org/TR/workers/">http://www.w3.org/TR/workers/</a></dd>
1.109 ihickson 219: <dt>Latest Editor's Draft:</dt>
1.260 ihickson 220: <dd><a class="latest-link" href="http://dev.w3.org/html5/workers/">http://dev.w3.org/html5/workers/</a></dd>
1.284 ihickson 221:
1.2 ihickson 222: <dt>Previous Versions:</dt>
1.279 ihickson 223: <dd><a href="http://www.w3.org/TR/2011/WD-workers-20110310/">http://www.w3.org/TR/2011/WD-workers-20110310/</a></dd>
224: <dd><a href="http://www.w3.org/TR/2011/WD-workers-20110208/">http://www.w3.org/TR/2011/WD-workers-20110208/</a></dd>
1.198 ihickson 225: <dd><a href="http://www.w3.org/TR/2009/WD-workers-20091029/">http://www.w3.org/TR/2009/WD-workers-20091029/</a></dd>
1.279 ihickson 226: <dd><a href="http://www.w3.org/TR/2009/WD-workers-20090423/">http://www.w3.org/TR/2009/WD-workers-20090423/</a></dd>
1.117 ihickson 227: <!-- :ZZZ -->
1.271 ihickson 228: <dt>Editor:</dt>
1.115 ihickson 229: <dd><a href="mailto:ian@hixie.ch">Ian Hickson</a>, Google, Inc.</dd>
1.155 ihickson 230: </dl><p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
1.285 ihickson 231: © 2011 <a href="http://www.w3.org/"><abbr title="World Wide
1.155 ihickson 232: Web Consortium">W3C</abbr></a><sup>®</sup> (<a href="http://www.csail.mit.edu/"><abbr title="Massachusetts
1.297 ihickson 233: Institute of Technology">MIT</abbr></a>, <a href="http://www.ercim.eu/"><abbr title="European Research
1.115 ihickson 234: Consortium for Informatics and Mathematics">ERCIM</abbr></a>, <a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C
235: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
236: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
237: and <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document
1.81 ihickson 238: use</a> rules apply.</p>
1.201 ihickson 239:
1.254 ihickson 240:
1.252 ihickson 241: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
1.255 ihickson 242: <p class="alt">The bulk of the text of this specification is also
1.254 ihickson 243: available in the WHATWG <a href="http://www.whatwg.org/specs/web-apps/current-work/complete.html#workers">Web Applications 1.0</a> specification, under a license that permits
244: reuse of the specification text.</p>
1.252 ihickson 245: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
246:
1.254 ihickson 247:
1.155 ihickson 248: </div><hr class="top"><h2 class="no-num no-toc" id="abstract">Abstract</h2><p>This specification defines an API that allows Web application
1.81 ihickson 249: authors to spawn background workers running scripts in parallel to
250: their main page. This allows for thread-like operation with
1.281 ihickson 251: message-passing as the coordination mechanism.<h2 class="no-num no-toc" id="status-of-this-document">Status of This document</h2><p><em>This section describes the status of this document at the
1.81 ihickson 252: time of its publication. Other documents may supersede this
1.297 ihickson 253: document. A list of current W3C publications and the
254:
255: most recently formally published <!-- DO NOT CHANGE THIS BACK TO THE STANDARD BOILERPLATE, AS IT IS INACCURATE -->
256:
257: revision of this technical report can be found in the <a href="http://www.w3.org/TR/">W3C technical reports index</a> at
258: http://www.w3.org/TR/.</em></p><p>If you wish to make comments regarding this document in a manner
1.271 ihickson 259: that is tracked by the W3C, please submit them via using <a href="http://www.w3.org/Bugs/Public/enter_bug.cgi?product=HTML%20WG">our
260: public bug database</a>. If you do not have an account then you can
261: enter feedback using this form:<form action="http://www.whatwg.org/specs/web-apps/current-work/file-spam.cgi" method="post">
262: <fieldset><legend>Feedback Comments</legend>
263: <input name="id" type="hidden" value="top"><input name="component" type="hidden" value="Web Workers (editor: Ian Hickson)"><input name="response" type="hidden" value="html"><p><label for="feedbackBox">Please enter your feedback, carefully
264: indicating the title of the section for which you are submitting
265: feedback, quoting the text that's wrong today if appropriate. If
266: you're suggesting a new feature, it's really important to say
267: <em>what</em> the problem you're trying to solve is. That's more
268: important than the solution, in fact.</label></p>
269: <p><textarea cols="79" id="feedbackBox" name="text" rows="10"></textarea></p>
270: <p class="note">Please don't use section numbers as these tend to
271: change rapidly and make your feedback harder to understand.</p>
272: <script type="text/javascript">
1.287 ihickson 273: function checkFeedbackForm(form) {
274: if (form.elements.text.value.match(/^ *</)) {
275: alert('Please don\'t start your feedback with an angle bracket, instead explain what topic your feedback is about first.');
276: return true;
277: } else if (form.elements.text.value.match(/ [^ ]+ [^ ]+ [^ ]+ [^ ]+ /)) {
278: if (form.elements.text.value.match(/^Please enter your feedback, carefully/)) {
279: alert('Please enter your feedback, explaining what is wrong, and without repeating the instructions. Thanks!');
280: return true;
281: } else if (form.elements.text.value.match(/ [^ ]+ [^ ]+ [^ ]+ [^ ]+ /)) {
282: form.action = "http://www.whatwg.org/specs/web-apps/current-work/file-bug.cgi";
283: return true;
284: } else {
285: alert('Please include significantly more detail about exactly what problem you are trying to solve.');
286: return false;
287: }
288: }
289: }
1.271 ihickson 290: </script><p>
1.277 ihickson 291: <input onclick="return checkFeedbackForm(form)" type="submit" value="Submit feedback"><small>(Note: Your IP address and user agent will be publicly recorded for spam prevention purposes.)</small>
1.271 ihickson 292: </p>
293: </fieldset></form><p>You can also e-mail feedback to <a href="mailto:public-webapps@w3.org">public-webapps@w3.org</a> (<a href="mailto:public-webapps-request@w3.org?subject=subscribe">subscribe</a>,
294: <a href="http://lists.w3.org/Archives/Public/public-webapps/">archives</a>),
1.115 ihickson 295: or <a href="mailto:whatwg@whatwg.org">whatwg@whatwg.org</a> (<a href="http://lists.whatwg.org/listinfo.cgi/whatwg-whatwg.org">subscribe</a>,
296: <a href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/">archives</a>).
1.281 ihickson 297: All feedback is welcome.</p><p>Implementors should be aware that this specification is not
1.81 ihickson 298: stable. <strong>Implementors who are not taking part in the
299: discussions are likely to find the specification changing out from
300: under them in incompatible ways.</strong> Vendors interested in
301: implementing this specification before it eventually reaches the
302: Candidate Recommendation stage should join the aforementioned
1.271 ihickson 303: mailing lists and take part in the discussions.<div id="multipage-common">
1.281 ihickson 304: </div><p>The latest
1.271 ihickson 305: stable version of the editor's draft of this specification is always
306: available on <a href="http://dev.w3.org/html5/workers/">the W3C CVS server</a>
307: and in the <a href="http://svn.whatwg.org/webapps/">WHATWG
308: Subversion repository</a>. The <a href="http://www.whatwg.org/specs/web-apps/current-work/complete.html">latest
309: editor's working copy</a> (which may contain unfinished text in the
310: process of being prepared) contains the latest draft text of this
311: specification (amongst others). For more details, please see the <a href="http://wiki.whatwg.org/wiki/FAQ#What_are_the_various_versions_of_the_spec.3F">WHATWG
312: FAQ</a>.<p>Notifications of changes to this specification are sent along
313: with notifications of changes to related specifications using the
314: following mechanisms:<dl><dt>E-mail notifications of changes</dt>
315: <dd>Commit-Watchers mailing list (complete source diffs): <a href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a></dd>
316: <dt>Browsable version-control record of all changes:</dt>
317: <dd>CVSWeb interface with side-by-side diffs: <a href="http://dev.w3.org/cvsweb/html5/">http://dev.w3.org/cvsweb/html5/</a></dd>
318: <dd>Annotated summary with unified diffs: <a href="http://html5.org/tools/web-apps-tracker">http://html5.org/tools/web-apps-tracker</a></dd>
319: <dd>Raw Subversion interface: <code>svn checkout http://svn.whatwg.org/webapps/</code></dd>
320: </dl><p>The W3C <a href="http://www.w3.org/2008/webapps/">Web Applications
1.81 ihickson 321: Working Group</a> is the W3C working group responsible for this
322: specification's progress along the W3C Recommendation track.
1.301 ! ihickson 323: This specification is the 5 August 2011 Editor's Draft.
1.281 ihickson 324: </p><p>This document was produced by a group operating under the <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5
1.115 ihickson 325: February 2004 W3C Patent Policy</a>. W3C maintains a <a href="http://www.w3.org/2004/01/pp-impl/42538/status" rel="disclosure">public list of
1.81 ihickson 326: any patent disclosures</a> made in connection with the deliverables
327: of the group; that page also includes instructions for disclosing a
328: patent. An individual who has actual knowledge of a patent which the
1.115 ihickson 329: individual believes contains <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
330: Claim(s)</a> must disclose the information in accordance with <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
1.252 ihickson 331: 6 of the W3C Patent Policy</a>.<h2 class="no-num no-toc" id="contents">Table of Contents</h2>
1.115 ihickson 332: <ol class="toc">
333: <li><a href="#introduction"><span class="secno">1 </span>Introduction</a>
1.81 ihickson 334: <ol>
1.115 ihickson 335: <li><a href="#scope"><span class="secno">1.1 </span>Scope</a></li>
336: <li><a href="#tutorial"><span class="secno">1.2 </span>Tutorial</a>
1.81 ihickson 337: <ol>
1.115 ihickson 338: <li><a href="#a-background-number-crunching-worker"><span class="secno">1.2.1 </span>A background number-crunching worker</a></li>
339: <li><a href="#a-worker-for-updating-a-client-side-database"><span class="secno">1.2.2 </span>A worker for updating a client-side database</a></li>
340: <li><a href="#worker-used-for-background-i-o"><span class="secno">1.2.3 </span>Worker used for background I/O</a></li>
1.238 ihickson 341: <li><a href="#shared-workers-introduction"><span class="secno">1.2.4 </span>Shared workers introduction</a></li>
342: <li><a href="#shared-state-using-a-shared-worker"><span class="secno">1.2.5 </span>Shared state using a shared worker</a></li>
1.275 ihickson 343: <li><a href="#delegation"><span class="secno">1.2.6 </span>Delegation</a></ol></ol></li>
1.148 ihickson 344: <li><a href="#conformance-requirements"><span class="secno">2 </span>Conformance requirements</a>
1.82 ihickson 345: <ol>
1.148 ihickson 346: <li><a href="#dependencies"><span class="secno">2.1 </span>Dependencies</a></ol></li>
347: <li><a href="#terminology"><span class="secno">3 </span>Terminology</a></li>
348: <li><a href="#infrastructure"><span class="secno">4 </span>Infrastructure</a>
349: <ol>
1.301 ! ihickson 350: <li><a href="#dependencies-0"><span class="secno">4.1 </span>Dependencies</a></li>
! 351: <li><a href="#the-global-scope"><span class="secno">4.2 </span>The global scope</a>
1.81 ihickson 352: <ol>
1.301 ! ihickson 353: <li><a href="#the-workerglobalscope-abstract-interface"><span class="secno">4.2.1 </span>The <code>WorkerGlobalScope</code> abstract interface</a></li>
! 354: <li><a href="#dedicated-workers-and-the-dedicatedworkerglobalscope-interface"><span class="secno">4.2.2 </span>Dedicated workers and the <code>DedicatedWorkerGlobalScope</code> interface</a></li>
! 355: <li><a href="#shared-workers-and-the-sharedworkerglobalscope-interface"><span class="secno">4.2.3 </span>Shared workers and the <code>SharedWorkerGlobalScope</code> interface</a></ol></li>
! 356: <li><a href="#origins-of-workers"><span class="secno">4.3 </span>Origins of workers</a></li>
! 357: <li><a href="#the-event-loop"><span class="secno">4.4 </span>The event loop</a></li>
! 358: <li><a href="#the-worker-s-lifetime"><span class="secno">4.5 </span>The worker's lifetime</a></li>
! 359: <li><a href="#processing-model"><span class="secno">4.6 </span>Processing model</a></li>
! 360: <li><a href="#runtime-script-errors"><span class="secno">4.7 </span>Runtime script errors</a></li>
! 361: <li><a href="#creating-workers"><span class="secno">4.8 </span>Creating workers</a>
1.81 ihickson 362: <ol>
1.301 ! ihickson 363: <li><a href="#the-abstractworker-abstract-interface"><span class="secno">4.8.1 </span>The <code>AbstractWorker</code> abstract interface</a></li>
! 364: <li><a href="#dedicated-workers-and-the-worker-interface"><span class="secno">4.8.2 </span>Dedicated workers and the <code>Worker</code> interface</a></li>
! 365: <li><a href="#shared-workers-and-the-sharedworker-interface"><span class="secno">4.8.3 </span>Shared workers and the <code>SharedWorker</code> interface</a></ol></ol></li>
1.148 ihickson 366: <li><a href="#apis-available-to-workers"><span class="secno">5 </span>APIs available to workers</a>
1.81 ihickson 367: <ol>
1.148 ihickson 368: <li><a href="#importing-scripts-and-libraries"><span class="secno">5.1 </span>Importing scripts and libraries</a></li>
1.210 ihickson 369: <li><a href="#the-workernavigator-object"><span class="secno">5.2 </span>The <code>WorkerNavigator</code> object</a></li>
1.272 ihickson 370: <li><a href="#interface-objects-and-constructors"><span class="secno">5.3 </span>Interface objects and constructors</a></li>
371: <li><a href="#worker-locations"><span class="secno">5.4 </span>Worker locations</a></ol></li>
1.115 ihickson 372: <li><a class="no-num" href="#references">References</a></li>
373: <li><a class="no-num" href="#acknowledgements">Acknowledgements</a></ol>
1.281 ihickson 374: <hr><h2 id="introduction"><span class="secno">1 </span>Introduction</h2><h3 id="scope"><span class="secno">1.1 </span>Scope</h3><p><i>This section is non-normative.</i><p>This specification defines an API for running scripts in the
1.155 ihickson 375: background independently of any user interface scripts.<p>This allows for long-running scripts that are not interrupted by
1.81 ihickson 376: scripts that respond to clicks or other user interactions, and
377: allows long tasks to be executed without yielding to keep the page
1.155 ihickson 378: responsive.<p>Workers (as these background scripts are called herein) are
1.81 ihickson 379: relatively heavy-weight, and are not intended to be used in large
380: numbers. For example, it would be inappropriate to launch one worker
381: for each pixel of a four megapixel image. The examples below show
1.155 ihickson 382: some appropriate uses of workers.<p>Generally, workers are expected to be long-lived, have a high
383: start-up performance cost, and a high per-instance memory cost.<h3 id="tutorial"><span class="secno">1.2 </span>Tutorial</h3><p><i>This section is non-normative.</i><p>There are a variety of uses that workers can be put to. The
384: following subsections show various examples of this use.<h4 id="a-background-number-crunching-worker"><span class="secno">1.2.1 </span>A background number-crunching worker</h4><p><i>This section is non-normative.</i><p>The simplest use of workers is for performing a computationally
385: expensive task without interrupting the user interface.<p>In this example, the main document spawns a worker to
386: (naïvely) compute prime numbers, and progressively displays the
387: most recently found prime number.<p>The main page is as follows:<pre><!DOCTYPE HTML>
1.81 ihickson 388: <html>
389: <head>
390: <title>Worker example: One-core computation</title>
391: </head>
392: <body>
393: <p>The highest prime number discovered so far is: <output id="result"></output></p>
394: <script>
1.50 ihickson 395: var worker = new Worker('worker.js');
396: worker.onmessage = function (event) {
1.54 ihickson 397: document.getElementById('result').textContent = event.data;
1.29 ihickson 398: };
1.81 ihickson 399: </script>
400: </body>
1.155 ihickson 401: </html></pre><p>The <code title="dom-Worker"><a href="#dom-worker">Worker()</a></code> constructor call
1.115 ihickson 402: creates a worker and returns a <code><a href="#worker">Worker</a></code> object
1.81 ihickson 403: representing that worker, which is used to communicate with the
1.177 ihickson 404: worker. That object's <code title="handler-Worker-onmessage"><a href="#handler-worker-onmessage">onmessage</a></code> event handler allows the code to receive messages from the worker.<p>The worker itself is as follows:<pre>var n = 1;
1.29 ihickson 405: search: while (true) {
406: n += 1;
407: for (var i = 2; i <= Math.sqrt(n); i += 1)
408: if (n % i == 0)
409: continue search;
410: // found a prime!
1.50 ihickson 411: postMessage(n);
1.155 ihickson 412: }</pre><p>The bulk of this code is simply an unoptimized search for a prime
1.295 ihickson 413: number. The <code title="dom-DedicatedWorkerGlobalScope-postMessage"><a href="#dom-dedicatedworkerglobalscope-postmessage">postMessage()</a></code>
414: method is used to send a message back to the page when a prime is
415: found.<p><a href="http://www.whatwg.org/demos/workers/primes/page.html">View this example online</a>.<h4 id="a-worker-for-updating-a-client-side-database"><span class="secno">1.2.2 </span>A worker for updating a client-side database</h4><p><i>This section is non-normative.</i><p>In this example, the main document spawns a worker whose only
1.81 ihickson 416: task is to listen for notifications from the server, and, when
417: appropriate, either add or remove data from the client-side
1.155 ihickson 418: database.<p>Since no communication occurs between the worker and the main
419: page, the main page can start the worker by just doing:<pre><script>
1.50 ihickson 420: new Worker('worker.js');
1.155 ihickson 421: </script></pre><p>The worker itself is as follows:<pre>var server = new WebSocket('ws://whatwg.org/database');
1.42 ihickson 422: var database = openDatabase('demobase', '1.0', 'Demo Database', 10240);
1.31 ihickson 423: server.onmessage = function (event) {
424: // data is in the format "command key value"
1.54 ihickson 425: var data = event.data.split(' ');
1.31 ihickson 426: switch (data[0]) {
427: case '+':
428: database.transaction(function(tx) {
429: tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
430: });
431: case '-':
432: database.transaction(function(tx) {
433: tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
434: });
435: }
1.155 ihickson 436: };</pre><p>This connects to the server using the <code>WebSocket</code>
1.81 ihickson 437: mechanism and opens the local database (which, we presume, has been
438: created earlier). The worker then just listens for messages from the
439: server and acts on them as appropriate, forever (or until the main
1.155 ihickson 440: page is closed).<p><a href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
1.81 ihickson 441: this example online</a>. (This example will not actually function,
442: since the server does not actually exist and the database is not
1.155 ihickson 443: created by this sample code.)<h4 id="worker-used-for-background-i-o"><span class="secno">1.2.3 </span>Worker used for background I/O</h4><p><i>This section is non-normative.</i><p>In this example, the main document uses two workers, one for
1.81 ihickson 444: fetching stock updates for at regular intervals, and one for
1.155 ihickson 445: fetching performing search queries that the user requests.<p>The main page is as follows:<pre><!DOCTYPE HTML>
1.81 ihickson 446: <html>
447: <head>
448: <title>Worker example: Stock ticker</title>
449: <script>
1.33 ihickson 450: // TICKER
451: var symbol = 'GOOG'; // default symbol to watch
1.50 ihickson 452: var ticker = new Worker('ticker.js');
1.33 ihickson 453:
454: // SEARCHER
1.50 ihickson 455: var searcher = new Worker('searcher.js');
1.33 ihickson 456: function search(query) {
1.50 ihickson 457: searcher.postMessage(query);
1.33 ihickson 458: }
459:
460: // SYMBOL SELECTION UI
461: function select(newSymbol) {
462: symbol = newSymbol;
1.50 ihickson 463: ticker.postMessage(symbol);
1.33 ihickson 464: }
1.81 ihickson 465: </script>
466: </head>
1.122 ihickson 467: <body onload="search('')">
1.81 ihickson 468: <p><output id="symbol"></output> <output id="value"></output></p>
469: <script>
1.50 ihickson 470: ticker.onmessage = function (event) {
1.54 ihickson 471: var data = event.data.split(' ');
1.33 ihickson 472: document.getElementById('symbol').textContent = data[0];
473: document.getElementById('value').textContent = data[1];
474: };
1.50 ihickson 475: ticker.postMessage(symbol);
1.81 ihickson 476: </script>
1.122 ihickson 477: <p><label>Search: <input type="text" autofocus oninput="search(this.value)"></label></p>
1.81 ihickson 478: <ul id="results"></ul>
479: <script>
1.50 ihickson 480: searcher.onmessage = function (event) {
1.54 ihickson 481: var data = event.data.split(' ');
1.33 ihickson 482: var results = document.getElementById('results');
483: while (results.hasChildNodes()) // clear previous results
484: results.removeChild(results.firstChild);
485: for (var i = 0; i < data.length; i += 1) {
486: // add a list item with a button for each result
487: var li = document.createElement('li');
488: var button = document.createElement('button');
489: button.value = data[i];
490: button.type = 'button';
491: button.onclick = function () { select(this.value); };
492: button.textContent = data[i];
493: li.appendChild(button);
494: results.appendChild(li);
495: }
496: };
1.81 ihickson 497: </script>
498: <p>(The data in this example is not real. Try searching for "Google" or "Apple".)</p>
499: </body>
1.155 ihickson 500: </html></pre><p>The two workers use a common library for performing the actual
501: network calls. This library is as follows:<pre>function get(url) {
1.50 ihickson 502: try {
503: var xhr = new XMLHttpRequest();
504: xhr.open('GET', url, false);
505: xhr.send();
506: return xhr.responseText;
507: } catch (e) {
508: return ''; // turn all errors into empty results
509: }
1.155 ihickson 510: }</pre><p>The stock updater worker is as follows:<pre>importScripts('io.js');
1.33 ihickson 511: var timer;
512: var symbol;
513: function update() {
1.50 ihickson 514: postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
1.33 ihickson 515: timer = setTimeout(update, 10000);
516: }
1.50 ihickson 517: onmessage = function (event) {
1.33 ihickson 518: if (timer)
519: clearTimeout(timer);
1.54 ihickson 520: symbol = event.data;
1.33 ihickson 521: update();
1.155 ihickson 522: };</pre><p>The search query worker is as follows:<pre>importScripts('io.js');
1.50 ihickson 523: onmessage = function (event) {
1.54 ihickson 524: postMessage(get('search.cgi?' + event.data));
1.238 ihickson 525: };</pre><p><a href="http://www.whatwg.org/demos/workers/stocks/page.html">View this example online</a>.<h4 id="shared-workers-introduction"><span class="secno">1.2.4 </span>Shared workers introduction</h4><p><i>This section is non-normative.</i><p>This section introduces shared workers using a Hello World
526: example. Shared workers use slightly different APIs, since each
527: worker can have multiple connections.<p>This first example shows how you connect to a worker and how a
528: worker can send a message back to the page when it connects to
1.239 ihickson 529: it. Received messages are displayed in a log.<p>Here is the HTML page:<pre><!DOCTYPE HTML>
530: <title>Shared workers: demo 1</title>
531: <pre id="log">Log:</pre>
532: <script>
533: var worker = new SharedWorker('test.js');
534: var log = document.getElementById('log');
535: worker.port.onmessage = function(e) { // note: not worker.onmessage!
536: log.textContent += '\n' + e.data;
537: }
538: </script>
539: </pre><p>Here is the JavaScript worker:<pre>onconnect = function(e) {
540: var port = e.ports[0];
541: port.postMessage('Hello World!');
542: }
1.259 ihickson 543: </pre><p><a href="http://www.whatwg.org/demos/workers/shared/001/test.html">View this example online</a>.<hr><p>This second example extends the first one by changing two things:
1.238 ihickson 544: first, messages are received using <code title="">addEventListener()</code> instead of an <span title="event
545: handler IDL attributes">event handler IDL attribute</span>, and
546: second, a message is sent <em>to</em> the worker, causing the worker
547: to send another message in return. Received messages are again
1.239 ihickson 548: displayed in a log.<p>Here is the HTML page:<pre><!DOCTYPE HTML>
1.240 ihickson 549: <title>Shared workers: demo 2</title>
1.239 ihickson 550: <pre id="log">Log:</pre>
551: <script>
552: var worker = new SharedWorker('test.js');
553: var log = document.getElementById('log');
1.240 ihickson 554: worker.port.addEventListener('message', function(e) {
1.239 ihickson 555: log.textContent += '\n' + e.data;
1.240 ihickson 556: }, false);
557: worker.port.start(); // note: need this when using addEventListener
558: worker.port.postMessage('ping');
1.239 ihickson 559: </script>
560: </pre><p>Here is the JavaScript worker:<pre>onconnect = function(e) {
561: var port = e.ports[0];
562: port.postMessage('Hello World!');
1.240 ihickson 563: port.onmessage = function(e) {
564: port.postMessage('pong'); // not e.ports[0].postMessage!
565: // e.target.postMessage('pong'); would work also
566: }
1.239 ihickson 567: }
1.259 ihickson 568: </pre><p><a href="http://www.whatwg.org/demos/workers/shared/002/test.html">View this example online</a>.<hr><p>Finally, the example is extended to show how two pages can
1.238 ihickson 569: connect to the same worker; in this case, the second page is merely
570: in an <code>iframe</code> on the first page, but the same principle
571: would apply to an entirely separate page in a separate
1.239 ihickson 572: <span>top-level browsing context</span>.<p>Here is the outer HTML page:<pre><!DOCTYPE HTML>
573: <title>Shared workers: demo 3</title>
574: <pre id="log">Log:</pre>
575: <script>
576: var worker = new SharedWorker('test.js');
577: var log = document.getElementById('log');
578: worker.port.addEventListener('message', function(e) {
579: log.textContent += '\n' + e.data;
580: }, false);
581: worker.port.start();
582: worker.port.postMessage('ping');
583: </script>
584: <iframe src="inner.html"></iframe>
585: </pre><p>Here is the inner HTML page:<pre><!DOCTYPE HTML>
586: <title>Shared workers: demo 3 inner frame</title>
587: <pre id=log>Inner log:</pre>
588: <script>
589: var worker = new SharedWorker('test.js');
590: var log = document.getElementById('log');
591: worker.port.onmessage = function(e) {
592: log.textContent += '\n' + e.data;
593: }
594: </script>
595: </pre><p>Here is the JavaScript worker:<pre>var count = 0;
596: onconnect = function(e) {
597: count += 1;
598: var port = e.ports[0];
599: port.postMessage('Hello World! You are connection #' + count);
600: port.onmessage = function(e) {
601: port.postMessage('pong');
602: }
603: }
1.259 ihickson 604: </pre><p><a href="http://www.whatwg.org/demos/workers/shared/003/test.html">View this example online</a>.<h4 id="shared-state-using-a-shared-worker"><span class="secno">1.2.5 </span>Shared state using a shared worker</h4><p><i>This section is non-normative.</i><p>In this example, multiple windows (viewers) can be opened that
1.81 ihickson 605: are all viewing the same map. All the windows share the same map
606: information, with a single worker coordinating all the viewers. Each
607: viewer can move around independently, but if they set any data on
1.155 ihickson 608: the map, all the viewers are updated.<p>The main page isn't interesting, it merely provides a way to open
609: the viewers:<pre><!DOCTYPE HTML>
1.81 ihickson 610: <html>
611: <head>
612: <title>Workers example: Multiviewer</title>
613: <script>
1.36 ihickson 614: function openViewer() {
615: window.open('viewer.html');
616: }
1.81 ihickson 617: </script>
618: </head>
619: <body>
620: <p><button type=button onclick="openViewer()">Open a new
621: viewer</button></p>
622: <p>Each viewer opens in a new window. You can have as many viewers
623: as you like, they all view the same data.</p>
624: </body>
1.155 ihickson 625: </html></pre><p>The viewer is more involved:<pre><!DOCTYPE HTML>
1.81 ihickson 626: <html>
627: <head>
628: <title>Workers example: Multiviewer viewer</title>
629: <script>
1.50 ihickson 630: var worker = new SharedWorker('worker.js', 'core');
1.36 ihickson 631:
632: // CONFIGURATION
633: function configure(event) {
1.54 ihickson 634: if (event.data.substr(0, 4) != 'cfg ') return;
635: var name = event.data.substr(4).split(' ', 1);
1.36 ihickson 636: // update display to mention our name is name
637: document.getElementsByTagName('h1')[0].textContent += ' ' + name;
638: // no longer need this listener
1.43 ihickson 639: worker.port.removeEventListener('message', configure, false);
1.36 ihickson 640: }
1.53 ihickson 641: worker.port.addEventListener('message', configure, false);
1.36 ihickson 642:
643: // MAP
644: function paintMap(event) {
1.54 ihickson 645: if (event.data.substr(0, 4) != 'map ') return;
646: var data = event.data.substr(4).split(',');
1.36 ihickson 647: // display tiles data[0] .. data[8]
648: var canvas = document.getElementById('map');
649: var context = canvas.getContext('2d');
650: for (var y = 0; y < 3; y += 1) {
651: for (var x = 0; x < 3; x += 1) {
652: var tile = data[y * 3 + x];
653: if (tile == '0')
654: context.fillStyle = 'green';
655: else
656: context.fillStyle = 'maroon';
657: fillRect(x * 50, y * 50, 50, 50);
658: }
659: }
660: }
1.53 ihickson 661: worker.port.addEventListener('message', paintMap, false);
1.36 ihickson 662:
663: // PUBLIC CHAT
664: function updatePublicChat(event) {
1.54 ihickson 665: if (event.data.substr(0, 4) != 'txt ') return;
666: var name = event.data.substr(4).split(' ', 1);
667: var message = event.data.substr(4 + length(name) + 1);
1.81 ihickson 668: // display "<name> message" in public chat
1.36 ihickson 669: var dialog = document.getElementById('public');
670: var dt = document.createElement('dt');
671: dt.textContent = name;
672: dialog.appendChild(dt);
673: var dd = document.createElement('dd');
674: dd.textContent = message;
675: dialog.appendChild(dd);
676: }
1.53 ihickson 677: worker.port.addEventListener('message', updatePublicChat, false);
1.36 ihickson 678:
679: // PRIVATE CHAT
680: function startPrivateChat(event) {
1.54 ihickson 681: if (event.data.substr(0, 4) != 'msg ') return;
682: var name = event.data.substr(4).split(' ', 1);
1.84 ihickson 683: var port = event.ports[0];
1.36 ihickson 684: // display a private chat UI
685: var ul = document.getElementById('private');
686: var li = document.createElement('li');
687: var h3 = document.createElement('h3');
688: h3.textContent = 'Private chat with ' + name;
689: li.appendChild(h3);
690: var dialog = document.createElement('dialog');
691: var addMessage = function(name, message) {
692: var dt = document.createElement('dt');
693: dt.textContent = name;
694: dialog.appendChild(dt);
695: var dd = document.createElement('dd');
696: dd.textContent = message;
697: dialog.appendChild(dd);
698: };
699: port.onmessage = function (event) {
1.54 ihickson 700: addMessage(name, event.data);
1.36 ihickson 701: };
702: li.appendChild(dialog);
703: var form = document.createElement('form');
704: var p = document.createElement('p');
705: var input = document.createElement('input');
706: input.size = 50;
707: p.appendChild(input);
708: p.appendChild(document.createTextNode(' '));
709: var button = document.createElement('button');
710: button.textContent = 'Post';
711: p.appendChild(button);
712: form.onsubmit = function () {
713: port.postMessage(input.value);
714: addMessage('me', input.value);
715: input.value = '';
716: return false;
717: };
718: form.appendChild(p);
719: li.appendChild(form);
720: }
1.53 ihickson 721: worker.port.addEventListener('message', startPrivateChat, false);
1.188 ihickson 722:
723: worker.port.start();
1.81 ihickson 724: </script>
725: </head>
726: <body>
727: <h1>Viewer</h1>
728: <h2>Map</h2>
729: <p><canvas id="map" height=150 width=150></canvas></p>
730: <p>
731: <button type=button onclick="worker.port.postMessage('mov left')">Left</button>
732: <button type=button onclick="worker.port.postMessage('mov up')">Up</button>
733: <button type=button onclick="worker.port.postMessage('mov down')">Down</button>
734: <button type=button onclick="worker.port.postMessage('mov right')">Right</button>
735: <button type=button onclick="worker.port.postMessage('set 0')">Set 0</button>
736: <button type=button onclick="worker.port.postMessage('set 1')">Set 1</button>
737: </p>
738: <h2>Public Chat</h2>
739: <dialog id="public"></dialog>
740: <form onsubmit="worker.port.postMessage('txt ' + message.value); message.value = ''; return false;">
741: <p>
742: <input type="text" name="message" size="50">
743: <button>Post</button>
744: </p>
745: </form>
746: <h2>Private Chat</h2>
747: <ul id="private"></ul>
748: </body>
749: </html>
1.155 ihickson 750: </pre><p>There are several key things worth noting about the way the
751: viewer is written.<p><strong>Multiple listeners</strong>. Instead of a single message
1.81 ihickson 752: processing function, the code here attaches multiple event
753: listeners, each one performing a quick check to see if it is
754: relevant for the message. In this example it doesn't make much
755: difference, but if multiple authors wanted to collaborate using a
756: single port to communicate with a worker, it would allow for
757: independent code instead of changes having to all be made to a
1.155 ihickson 758: single event handling function.<p>Registering event listeners in this way also allows you to
1.81 ihickson 759: unregister specific listeners when you are done with them, as is
760: done with the <code title="">configure()</code> method in this
1.155 ihickson 761: example.<p>Finally, the worker:<pre>
762: var nextName = 0;
1.36 ihickson 763: function getNextName() {
764: // this could use more friendly names
765: // but for now just return a number
766: return nextName++;
767: }
768:
769: var map = [
770: [0, 0, 0, 0, 0, 0, 0],
771: [1, 1, 0, 1, 0, 1, 1],
772: [0, 1, 0, 1, 0, 0, 0],
773: [0, 1, 0, 1, 0, 1, 1],
774: [0, 0, 0, 1, 0, 0, 0],
775: [1, 0, 0, 1, 1, 1, 1],
776: [1, 1, 0, 1, 1, 0, 1],
777: ];
778:
779: function wrapX(x) {
780: if (x < 0) return wrapX(x + map[0].length);
1.81 ihickson 781: if (x >= map[0].length) return wrapX(x - map[0].length);
1.36 ihickson 782: return x;
783: }
784:
785: function wrapY(y) {
786: if (y < 0) return wrapY(y + map.length);
1.81 ihickson 787: if (y >= map[0].length) return wrapY(y - map.length);
1.36 ihickson 788: return y;
789: }
790:
1.50 ihickson 791: function sendMapData(callback) {
1.36 ihickson 792: var data = '';
793: for (var y = viewer.y-1; y <= viewer.y+1; y += 1) {
794: for (var x = viewer.x-1; x <= viewer.x+1; x += 1) {
795: if (data != '')
796: data += ',';
797: data += map[y][x];
798: }
799: }
1.50 ihickson 800: callback('map ' + data);
1.36 ihickson 801: }
802:
803: var viewers = {};
804: onconnect = function (event) {
1.84 ihickson 805: event.ports[0]._name = getNextName();
806: event.ports[0]._data = { port: event.port, x: 0, y: 0, };
807: viewers[event.ports[0]._name] = event.port._data;
808: event.ports[0].postMessage('cfg ' + name);
809: event.ports[0].onmessage = getMessage;
810: sendMapData(event.ports[0].postMessage);
1.36 ihickson 811: };
812:
813: function getMessage(event) {
1.54 ihickson 814: switch (event.data.substr(0, 4)) {
1.36 ihickson 815: case 'mov ':
1.54 ihickson 816: var direction = event.data.substr(4);
1.36 ihickson 817: var dx = 0;
818: var dy = 0;
819: switch (direction) {
820: case 'up': dy = -1; break;
821: case 'down': dy = 1; break;
822: case 'left': dx = -1; break;
823: case 'right': dx = 1; break;
824: }
825: event.target._data.x = wrapX(event.target._data.x + dx);
826: event.target._data.y = wrapY(event.target._data.y + dy);
1.50 ihickson 827: sendMapData(event.target.postMessage);
1.36 ihickson 828: break;
829: case 'set ':
1.54 ihickson 830: var value = event.data.substr(4);
1.36 ihickson 831: map[event.target._data.y][event.target._data.x] = value;
832: for (var viewer in viewers)
1.50 ihickson 833: sendMapData(viewers[viewer].port.postMessage);
1.36 ihickson 834: break;
835: case 'txt ':
836: var name = event.target._name;
1.54 ihickson 837: var message = event.data.substr(4);
1.36 ihickson 838: for (var viewer in viewers)
839: viewers[viewer].port.postMessage('txt ' + name + ' ' + message);
840: break;
841: case 'msg ':
842: var party1 = event._data;
1.54 ihickson 843: var party2 = viewers[event.data.substr(4).split(' ', 1)];
1.36 ihickson 844: if (party2) {
845: var channel = new MessageChannel();
1.84 ihickson 846: party1.port.postMessage('msg ' + party2.name, [channel.port1]);
847: party2.port.postMessage('msg ' + party1.name, [channel.port2]);
1.36 ihickson 848: }
849: break;
850: }
1.155 ihickson 851: }</pre><p><strong>Connecting to multiple pages</strong>. The script uses
1.115 ihickson 852: the <code title="handler-SharedWorkerGlobalScope-onconnect"><a href="#handler-sharedworkerglobalscope-onconnect">onconnect</a></code>
1.155 ihickson 853: event listener to listen for multiple connections.<p><strong>Direct channels</strong>. When the worker receives a
1.81 ihickson 854: "msg" message from one viewer naming another viewer, it sets up a
855: direct connection between the two, so that the two viewers can
856: communicate directly without the worker having to proxy all the
1.238 ihickson 857: messages.<p><a href="http://www.whatwg.org/demos/workers/multiviewer/page.html">View this example online</a>.<h4 id="delegation"><span class="secno">1.2.6 </span>Delegation</h4><p><i>This section is non-normative.</i><p>With multicore CPUs becoming prevalent, one way to obtain better
1.81 ihickson 858: performance is to split computationally expensive tasks amongst
859: multiple workers. In this example, a computationally expensive task
860: that is to be performed for every number from 1 to 10,000,000 is
1.155 ihickson 861: farmed out to ten subworkers.<p>The main page is as follows, it just reports the result:<pre><!DOCTYPE HTML>
1.81 ihickson 862: <html>
863: <head>
1.203 ihickson 864: <title>Worker example: Multicore computation</title>
1.81 ihickson 865: </head>
866: <body>
1.203 ihickson 867: <p>Result: <output id="result"></output></p>
1.81 ihickson 868: <script>
1.50 ihickson 869: var worker = new Worker('worker.js');
870: worker.onmessage = function (event) {
1.54 ihickson 871: document.getElementById('result').textContent = event.data;
1.40 ihickson 872: };
1.81 ihickson 873: </script>
874: </body>
1.155 ihickson 875: </html></pre><p>The worker itself is as follows:<pre>// settings
1.40 ihickson 876: var num_workers = 10;
877: var items_per_worker = 1000000;
878:
879: // start the workers
880: var result = 0;
881: var pending_workers = num_workers;
882: for (var i = 0; i < num_workers; i += 1) {
1.50 ihickson 883: var worker = new Worker('core.js');
884: worker.postMessage(i * items_per_worker);
885: worker.postMessage((i+1) * items_per_worker);
886: worker.onmessage = storeResult;
1.40 ihickson 887: }
888:
889: // handle the results
890: function storeResult(event) {
1.54 ihickson 891: result += 1*event.data;
1.40 ihickson 892: pending_workers -= 1;
893: if (pending_workers <= 0)
1.50 ihickson 894: postMessage(result); // finished!
1.155 ihickson 895: }</pre><p>It consists of a loop to start the subworkers, and then a handler
896: that waits for all the subworkers to respond.<p>The subworkers are implemented as follows:<pre>var start;
1.41 ihickson 897: onmessage = getStart;
1.40 ihickson 898: function getStart(event) {
1.54 ihickson 899: start = 1*event.data;
1.40 ihickson 900: onmessage = getEnd;
901: }
902:
903: var end;
904: function getEnd(event) {
1.54 ihickson 905: end = 1*event.data;
1.40 ihickson 906: onmessage = null;
1.66 ihickson 907: work();
1.40 ihickson 908: }
909:
1.66 ihickson 910: function work() {
1.40 ihickson 911: var result = 0;
912: for (var i = start; i < end; i += 1) {
913: // perform some complex calculation here
914: result += 1;
915: }
1.50 ihickson 916: postMessage(result);
917: close();
1.155 ihickson 918: }</pre><p>They receive two numbers in two events, perform the computation
1.81 ihickson 919: for the range of numbers thus specified, and then report the result
1.281 ihickson 920: back to the parent.<p><a href="http://www.whatwg.org/demos/workers/multicore/page.html">View this example online</a>.</p><h2 id="conformance-requirements"><span class="secno">2 </span>Conformance requirements</h2><p>All diagrams, examples, and notes in this specification are
1.81 ihickson 921: non-normative, as are all sections explicitly marked non-normative.
1.281 ihickson 922: Everything else in this specification is normative.<p>The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
1.81 ihickson 923: "OPTIONAL" in the normative parts of this document are to be
924: interpreted as described in RFC2119. For readability, these words do
1.155 ihickson 925: not appear in all uppercase letters in this specification. <a href="#refsRFC2119">[RFC2119]</a><p>Requirements phrased in the imperative as part of algorithms
1.81 ihickson 926: (such as "strip any leading space characters" or "return false and
927: abort these steps") are to be interpreted with the meaning of the
928: key word ("must", "should", "may", etc) used in introducing the
1.155 ihickson 929: algorithm.<p>Some conformance requirements are phrased as requirements on
1.81 ihickson 930: attributes, methods or objects. Such requirements are to be
1.155 ihickson 931: interpreted as requirements on user agents.<p>Conformance requirements phrased as algorithms or specific steps
1.81 ihickson 932: may be implemented in any manner, so long as the end result is
933: equivalent. (In particular, the algorithms defined in this
934: specification are intended to be easy to follow, and not intended to
1.155 ihickson 935: be performant.)<p>The only conformance class defined by this specification is user
936: agents.<p>User agents may impose implementation-specific limits on
1.81 ihickson 937: otherwise unconstrained inputs, e.g. to prevent denial of service
938: attacks, to guard against running out of memory, or to work around
1.229 ihickson 939: platform-specific limitations.<p>When support for a feature is disabled (e.g. as an emergency
940: measure to mitigate a security problem, or to aid in development, or
941: for performance reasons), user agents must act as if they had no
942: support for the feature whatsoever, and as if the feature was not
943: mentioned in this specification. For example, if a particular
944: feature is accessed via an attribute in a Web IDL interface, the
945: attribute itself would be omitted from the objects that implement
946: that interface — leaving the attribute on the object but
947: making it return null or throw an exception is insufficient.<h3 id="dependencies"><span class="secno">2.1 </span>Dependencies</h3><p>This specification relies on several other underlying
1.226 ihickson 948: specifications.<dl><dt>HTML</dt>
1.49 ihickson 949:
1.81 ihickson 950: <dd>
1.49 ihickson 951:
1.226 ihickson 952: <p>Many fundamental concepts from HTML are used by this
953: specification. <a href="#refsHTML">[HTML]</a></p>
1.49 ihickson 954:
1.81 ihickson 955: </dd>
1.1 ihickson 956:
1.81 ihickson 957: <dt>WebIDL</dt>
1.1 ihickson 958:
959: <dd>
960:
1.81 ihickson 961: <p>The IDL blocks in this specification use the semantics of the
1.153 ihickson 962: WebIDL specification. <a href="#refsWEBIDL">[WEBIDL]</a></p>
1.1 ihickson 963:
1.81 ihickson 964: </dd>
1.1 ihickson 965:
1.155 ihickson 966: </dl><h2 id="terminology"><span class="secno">3 </span>Terminology</h2><p>The construction "a <code title="">Foo</code> object", where
1.81 ihickson 967: <code title="">Foo</code> is actually an interface, is sometimes
968: used instead of the more accurate "an object implementing the
1.155 ihickson 969: interface <code title="">Foo</code>".<p>The term DOM is used to refer to the API set made available to
1.81 ihickson 970: scripts in Web applications, and does not necessarily imply the
971: existence of an actual <code>Document</code> object or of any other
972: <code>Node</code> objects as defined in the DOM Core
1.171 ihickson 973: specifications. <a href="#refsDOMCORE">[DOMCORE]</a><p>An IDL attribute is said to be <em>getting</em> when its value is
1.81 ihickson 974: being retrieved (e.g. by author script), and is said to be
1.155 ihickson 975: <em>setting</em> when a new value is assigned to it.<p>The term "JavaScript" is used to refer to ECMA262, rather than
1.103 ihickson 976: the official term ECMAScript, since the term JavaScript is more
1.155 ihickson 977: widely known. <a href="#refsECMA262">[ECMA262]</a><h2 id="infrastructure"><span class="secno">4 </span>Infrastructure</h2><p>There are two kinds of workers; dedicated workers, and shared
1.81 ihickson 978: workers. Dedicated workers, once created, and are linked to their
979: creator; but message ports can be used to communicate from a
980: dedicated worker to multiple other browsing contexts or
981: workers. Shared workers, on the other hand, are named, and once
982: created any script running in the same <span>origin</span> can
1.301 ! ihickson 983: obtain a reference to that worker and communicate with it.<h3 id="dependencies-0"><span class="secno">4.1 </span>Dependencies</h3><p>The <dfn id="messageport"><code>MessagePort</code></dfn> interface is defined in
! 984: the HTML specification. <a href="#refsHTML">[HTML]</a><h3 id="the-global-scope"><span class="secno">4.2 </span>The global scope</h3><p>The global scope is the "inside" of a worker.<h4 id="the-workerglobalscope-abstract-interface"><span class="secno">4.2.1 </span>The <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> abstract interface</h4><pre class="idl">interface <dfn id="workerglobalscope">WorkerGlobalScope</dfn> {
1.115 ihickson 985: readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#dom-workerglobalscope-self" title="dom-WorkerGlobalScope-self">self</a>;
986: readonly attribute <a href="#workerlocation">WorkerLocation</a> <a href="#dom-workerglobalscope-location" title="dom-WorkerGlobalScope-location">location</a>;
987:
988: void <a href="#dom-workerglobalscope-close" title="dom-WorkerGlobalScope-close">close</a>();
1.291 ihickson 989: attribute <span>Function</span>? <a href="#handler-workerglobalscope-onerror" title="handler-WorkerGlobalScope-onerror">onerror</a>;
990: attribute <span>Function</span>? <a href="#handler-workerglobalscope-onoffline" title="handler-WorkerGlobalScope-onoffline">onoffline</a>;
991: attribute <span>Function</span>? <a href="#handler-workerglobalscope-ononline" title="handler-WorkerGlobalScope-ononline">ononline</a>;
1.150 ihickson 992: };
1.172 ihickson 993: <a href="#workerglobalscope">WorkerGlobalScope</a> implements <a href="#workerutils">WorkerUtils</a>;
994: <a href="#workerglobalscope">WorkerGlobalScope</a> implements <span>EventTarget</span>;</pre><p>The <dfn id="dom-workerglobalscope-self" title="dom-WorkerGlobalScope-self"><code>self</code></dfn> attribute
1.155 ihickson 995: must return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.<p>The <dfn id="dom-workerglobalscope-location" title="dom-WorkerGlobalScope-location"><code>location</code></dfn>
1.115 ihickson 996: attribute must return the <code><a href="#workerlocation">WorkerLocation</a></code> object created
997: for the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object when the worker was
1.81 ihickson 998: created. It represents the <span>absolute URL</span> of the script
1.155 ihickson 999: that was used to initialize the worker, after any redirects.<hr><p>When a script invokes the <dfn id="dom-workerglobalscope-close" title="dom-WorkerGlobalScope-close"><code>close()</code></dfn>
1.123 ihickson 1000: method on a <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user agent
1.155 ihickson 1001: must run the following steps (atomically):<ol><li><p>Discard any <span title="concept-task">tasks</span> that
1.123 ihickson 1002: have been added to the <span>event loop</span>'s <span title="task
1003: queue">task queues</span>.</p>
1004:
1.81 ihickson 1005:
1.115 ihickson 1006: <li><p>Set the worker's <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object's
1007: <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag to
1.123 ihickson 1008: true. (This prevents any further tasks from being queued.)</li>
1.81 ihickson 1009:
1.177 ihickson 1010: </ol><p>The following are the <span>event handlers</span> (and their
1011: corresponding <span title="event handler event type">event handler
1012: event types</span>) that must be supported, as IDL attributes, by
1013: objects implementing the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1014: interface:<table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1.281 ihickson 1015: <tbody><tr><td><dfn id="handler-workerglobalscope-onerror" title="handler-WorkerGlobalScope-onerror"><code>onerror</code></dfn> <td> <code title="event-error">error</code>
1.289 ihickson 1016: <tr><td><dfn id="handler-workerglobalscope-onoffline" title="handler-WorkerGlobalScope-onoffline"><code>onoffline</code></dfn> <td> <code title="event-offline">offline</code>
1017: <tr><td><dfn id="handler-workerglobalscope-ononline" title="handler-WorkerGlobalScope-ononline"><code>ononline</code></dfn> <td> <code title="event-online">online</code>
1.209 ihickson 1018: </table><hr><p>The <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> interface must not exist if
1019: the interface's <span>relevant namespace object</span> is a
1.301 ! ihickson 1020: <code>Window</code> object. <a href="#refsWEBIDL">[WEBIDL]</a><h4 id="dedicated-workers-and-the-dedicatedworkerglobalscope-interface"><span class="secno">4.2.2 </span>Dedicated workers and the <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> interface</h4><pre class="idl">[Supplemental, NoInterfaceObject]
1.150 ihickson 1021: interface <dfn id="dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1.296 ihickson 1022: void <a href="#dom-dedicatedworkerglobalscope-postmessage" title="dom-DedicatedWorkerGlobalScope-postMessage">postMessage</a>(in any message, in optional sequence<<span>Transferable</span>> transfer); attribute <span>Function</span>? <a href="#handler-dedicatedworkerglobalscope-onmessage" title="handler-DedicatedWorkerGlobalScope-onmessage">onmessage</a>;
1.155 ihickson 1023: };</pre><p><code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> objects act as if they
1.301 ! ihickson 1024: had an implicit <code><a href="#messageport">MessagePort</a></code> associated with them. This
1.81 ihickson 1025: port is part of a channel that is set up when the worker is created,
1026: but it is not exposed. This object must never be garbage collected
1.155 ihickson 1027: before the <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> object.<p>All messages received by that port must immediately be retargeted
1.281 ihickson 1028: at the <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> object.<p>The <dfn id="dom-dedicatedworkerglobalscope-postmessage" title="dom-DedicatedWorkerGlobalScope-postMessage"><code>postMessage()</code></dfn> method on
1.115 ihickson 1029: <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> objects must act as if, when
1.281 ihickson 1030: invoked, it immediately invoked the
1.81 ihickson 1031: method of the same name on the port, with the same arguments, and
1.177 ihickson 1032: returned the same return value.<p>The following are the <span>event handlers</span> (and their
1033: corresponding <span title="event handler event type">event handler
1034: event types</span>) that must be supported, as IDL attributes, by
1035: objects implementing the <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1036: interface:<table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1.155 ihickson 1037: <tbody><tr><td><dfn id="handler-dedicatedworkerglobalscope-onmessage" title="handler-DedicatedWorkerGlobalScope-onmessage"><code>onmessage</code></dfn> <td> <code title="event-message">message</code>
1.191 ihickson 1038: </table><p>For the purposes of the <span>application cache</span> networking
1039: model, a dedicated worker is an extension of the <span>cache
1.301 ! ihickson 1040: host</span> from which it was created.<h4 id="shared-workers-and-the-sharedworkerglobalscope-interface"><span class="secno">4.2.3 </span>Shared workers and the <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> interface</h4><pre class="idl">[Supplemental, NoInterfaceObject]
1.150 ihickson 1041: interface <dfn id="sharedworkerglobalscope">SharedWorkerGlobalScope</dfn> : <a href="#workerglobalscope">WorkerGlobalScope</a> {
1.115 ihickson 1042: readonly attribute DOMString <a href="#dom-sharedworkerglobalscope-name" title="dom-SharedWorkerGlobalScope-name">name</a>;
1.181 ihickson 1043: readonly attribute <span>ApplicationCache</span> <span title="dom-SharedWorkerGlobalScope-applicationCache">applicationCache</span>;
1.291 ihickson 1044: attribute <span>Function</span>? <a href="#handler-sharedworkerglobalscope-onconnect" title="handler-SharedWorkerGlobalScope-onconnect">onconnect</a>;
1.155 ihickson 1045: };</pre><p>Shared workers receive message ports through <code title="event-WorkerGlobalScope-connect">connect</code> events on
1046: their global object for each connection.<p>The <dfn id="dom-sharedworkerglobalscope-name" title="dom-SharedWorkerGlobalScope-name"><code>name</code></dfn>
1.81 ihickson 1047: attribute must return the value it was assigned when the
1.115 ihickson 1048: <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object was created by the
1049: "<a href="#run-a-worker">run a worker</a>" algorithm. Its value represents the name
1.81 ihickson 1050: that can be used to obtain a reference to the worker using the
1.177 ihickson 1051: <code><a href="#sharedworker">SharedWorker</a></code> constructor.<p>The following are the <span>event handlers</span> (and their
1052: corresponding <span title="event handler event type">event handler
1053: event types</span>) that must be supported, as IDL attributes, by
1054: objects implementing the <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1055: interface:<table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1.155 ihickson 1056: <tbody><tr><td><dfn id="handler-sharedworkerglobalscope-onconnect" title="handler-SharedWorkerGlobalScope-onconnect"><code>onconnect</code></dfn> <td> <code title="event-connect">connect</code>
1.191 ihickson 1057: </table><p>For the purposes of the <span>application cache</span> networking
1058: model, a shared worker is its own <span>cache host</span>. The
1059: <a href="#run-a-worker">run a worker</a> algorithm takes care of associating the
1060: worker with an <span>application cache</span>.<p class="note">The <code title="dom-SharedWorkerGlobalScope-applicationCache">applicationCache</code>
1.123 ihickson 1061: attribute returns the <code>ApplicationCache</code> object for the
1.301 ! ihickson 1062: worker.</p><h3 id="origins-of-workers"><span class="secno">4.3 </span>Origins of workers</h3><p>Both the <span>origin</span> and <span>effective script
1.81 ihickson 1063: origin</span> of scripts running in workers are the
1064: <span>origin</span> of the <span>absolute URL</span> given in that
1.115 ihickson 1065: the worker's <code title="dom-WorkerGlobalScope-location"><a href="#dom-workerglobalscope-location">location</a></code> attribute
1.301 ! ihickson 1066: represents.<h3 id="the-event-loop"><span class="secno">4.4 </span>The event loop</h3><p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object has an <span>event
1.128 ihickson 1067: loop</span> distinct from those defined for <span title="unit of
1068: related similar-origin browsing contexts">units of related
1069: similar-origin browsing contexts</span>. This <span>event
1070: loop</span> has no associated <span>browsing context</span>, and its
1071: <span title="task queue">task queues</span> only have events,
1072: callbacks, and networking activity as <span title="concept-task">tasks</span>. The processing model of these
1073: <span title="event loop">event loops</span> is defined below in the
1.155 ihickson 1074: <a href="#run-a-worker">run a worker</a> algorithm.<p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object also has a <dfn id="dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</dfn> flag, which must
1.81 ihickson 1075: initially be false, but which can get set to true by the algorithms
1.155 ihickson 1076: in the processing model section below.<p>Once the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>'s <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag is set to
1.81 ihickson 1077: true, the <span>event loop</span>'s <span title="task queue">task
1.115 ihickson 1078: queues</span> must discard any further <span title="concept-task">tasks</span> that would be added to them (tasks
1.175 ihickson 1079: already on the queue are unaffected except where otherwise
1.115 ihickson 1080: specified). Effectively, once the <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag is true,
1.81 ihickson 1081: timers stop firing, notifications for all pending asynchronous
1.301 ! ihickson 1082: operations are dropped, etc.<h3 id="the-worker-s-lifetime"><span class="secno">4.5 </span>The worker's lifetime</h3><p>Workers communicate with other workers and with <span title="browsing context">browsing contexts</span> through <span title="channel messaging">message channels</span> and their
! 1083: <code><a href="#messageport">MessagePort</a></code> objects.<p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> <var title="">worker global
1.124 ihickson 1084: scope</var> has a list of <dfn id="the-worker-s-ports">the worker's ports</dfn>, which
1.301 ! ihickson 1085: consists of all the <code><a href="#messageport">MessagePort</a></code> objects that are
1.81 ihickson 1086: entangled with another port and that have one (but only one) port
1087: owned by <var title="">worker global scope</var>. This list includes
1.281 ihickson 1088: the implicit
1.301 ! ihickson 1089: <code><a href="#messageport">MessagePort</a></code> in the case of <a href="#dedicatedworkerglobalscope" title="DedicatedWorkerGlobalScope">dedicated workers</a>.<p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> also has a list of <dfn id="the-worker-s-workers">the
1.124 ihickson 1090: worker's workers</dfn>. Initially this list is empty; it is
1.155 ihickson 1091: populated when the worker creates or obtains further workers.<p>Finally, each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> also has a list of
1.124 ihickson 1092: <dfn id="the-worker-s-documents">the worker's <code>Document</code>s</dfn>. Initially this list
1.155 ihickson 1093: is empty; it is populated when the worker is created.<p>Whenever a <code>Document</code> <var title="">d</var> is <dfn id="add-a-document-to-the-worker-s-documents" title="add a document to the worker's documents">added to the
1.123 ihickson 1094: worker's <code>Document</code>s</dfn>, the user agent must, for each
1.124 ihickson 1095: worker in the list of <a href="#the-worker-s-workers">the worker's workers</a> whose list
1096: of <a href="#the-worker-s-documents">the worker's <code>Document</code>s</a> does not contain
1097: <var title="">d</var>, <a href="#add-a-document-to-the-worker-s-documents" title="add a document to the worker's
1.123 ihickson 1098: documents">add <var title="">d</var> to <var title="">q</var>'s
1099: <code>WorkerGlobalScope</code> owner's list of <span>the worker's
1.281 ihickson 1100: <code>Document</code>s</span></a>.</p><p>Whenever a <code>Document</code> object is <span title="discard a
1.123 ihickson 1101: Document">discarded</span>, it must be removed from the list of
1102: <a href="#the-worker-s-documents">the worker's <code>Document</code>s</a> of each worker
1.192 ihickson 1103: whose list contains that <code>Document</code>.<p>Given a <span>script's global object</span> <var title="">o</var>
1.123 ihickson 1104: when creating or obtaining a worker, the <dfn id="list-of-relevant-document-objects-to-add">list of relevant
1105: <code>Document</code> objects to add</dfn> depends on the type of
1106: <var title="">o</var>. If <var title="">o</var> is a
1107: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object (i.e. if we are creating a
1108: nested worker), then the relevant <code>Document</code>s are the
1109: <code>Document</code>s that are in <var title="">o</var>'s own list
1110: of <a href="#the-worker-s-documents">the worker's <code>Document</code>s</a>. Otherwise, <var title="">o</var> is a <code>Window</code> object, and the relevant
1111: <code>Document</code> is just the <code>Document</code> that is the
1.155 ihickson 1112: <span>active document</span> of the <code>Window</code> object <var title="">o</var>.<hr><p>A worker is said to be a <dfn id="permissible-worker">permissible worker</dfn> if its
1.123 ihickson 1113: list of <a href="#the-worker-s-documents">the worker's <code>Document</code>s</a> is not
1.155 ihickson 1114: empty.<p>A worker is said to be a <dfn id="protected-worker">protected worker</dfn> if it is a
1.123 ihickson 1115: <a href="#permissible-worker">permissible worker</a> and either it has outstanding
1116: timers, database transactions, or network connections, or its list
1.124 ihickson 1117: of <a href="#the-worker-s-ports">the worker's ports</a> is not empty, or its
1.123 ihickson 1118: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> is actually a
1119: <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object (i.e. the worker is a
1.155 ihickson 1120: shared worker).<p>A worker is said to be an <dfn id="active-needed-worker">active needed worker</dfn> if any
1.123 ihickson 1121: of the <code>Document</code> objects in <a href="#the-worker-s-documents">the worker's
1.155 ihickson 1122: <code>Document</code>s</a> are <span>fully active</span>.<p>A worker is said to be a <dfn id="suspendable-worker">suspendable worker</dfn> if it is
1.123 ihickson 1123: not an <a href="#active-needed-worker">active needed worker</a> but it is a
1.301 ! ihickson 1124: <a href="#permissible-worker">permissible worker</a>.<h3 id="processing-model"><span class="secno">4.6 </span>Processing model</h3><p>When a user agent is to <dfn id="run-a-worker">run a worker</dfn> for a script with
1.232 ihickson 1125: <span>URL</span> <var title="">url</var>, a <span>browsing
1126: context</span> <var title="">owner browsing context</var>, a
1127: <code>Document</code> <var title="">owner document</var>, an
1128: <span>origin</span> <var title="">owner origin</var>, and with
1129: global scope <var title="">worker global scope</var>, it must run
1130: the following steps:<ol><li>
1.81 ihickson 1131:
1.274 ihickson 1132: <p>Create a separate parallel execution environment (i.e. a
1133: separate thread or process or equivalent construct), and run the
1134: rest of these steps asynchronously in that context.</p>
1.81 ihickson 1135:
1136: </li>
1.4 ihickson 1137:
1.101 ihickson 1138: <li><p>If <var title="">worker global scope</var> is actually a
1.115 ihickson 1139: <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object (i.e. the worker is a
1.101 ihickson 1140: shared worker), and there are any <span title="relevant application
1141: cache">relevant application caches</span> that are identified by a
1142: manifest URL with the <span>same origin</span> as <var title="">url</var> and that have <var title="">url</var> as one of
1.115 ihickson 1143: their entries, <em>not</em> excluding entries marked as <span title="concept-appcache-foreign">foreign</span>, then associate the
1144: <var title="">worker global scope</var> with the <span title="concept-appcache-selection">most appropriate application
1.101 ihickson 1145: cache</span> of those that match.</li>
1146:
1.4 ihickson 1147: <li>
1.52 ihickson 1148:
1.256 ihickson 1149: <p>Attempt to <span>fetch</span> the resource identified by <var title="">url</var>, from the <var title="">owner origin</var>,
1150: with the <i>synchronous flag</i> set and the <i>force same-origin
1.281 ihickson 1151: flag</i> set.</p>
1.256 ihickson 1152: <p>If the attempt fails, then for each <code><a href="#worker">Worker</a></code> or
1153: <code><a href="#sharedworker">SharedWorker</a></code> object associated with <var title="">worker global scope</var>, <span>queue a task</span> to
1154: <span>fire a simple event</span> named <code title="event-error">error</code> at that object. Abort these
1155: steps.</p>
1.7 ihickson 1156:
1.264 ihickson 1157: <p>If the attempt succeeds, then let <var title="">source</var> be
1158: the script resource <span>decoded as UTF-8, with error
1159: handling</span>.
1160:
1161: <a href="#refsHTML">[HTML]</a>
1162:
1163: </p>
1.81 ihickson 1164:
1.68 ihickson 1165: <p>Let <var title="">language</var> be JavaScript.</p>
1.7 ihickson 1166:
1.115 ihickson 1167: <p class="note">As with <code>script</code> elements, the MIME
1.81 ihickson 1168: type of the script is ignored. Unlike with <code>script</code>
1169: elements, there is no way to override the type. It's always
1.170 ihickson 1170: assumed to be JavaScript.</p>
1.81 ihickson 1171:
1172: </li>
1.7 ihickson 1173:
1174: <li>
1.68 ihickson 1175:
1.115 ihickson 1176: <p>A new <span title="concept-script">script</span> is now
1.81 ihickson 1177: created, as follows.</p>
1178:
1179: <p>Create a new <span>script execution environment</span>
1180: set up as appropriate for the scripting language <var title="">language</var>.</p>
1.34 ihickson 1181:
1.68 ihickson 1182: <p>Parse/compile/initialize <var title="">source</var> using that
1.81 ihickson 1183: <span>script execution environment</span>, as appropriate for <var title="">language</var>, and thus obtain a <span>list of code
1184: entry-points</span>; set the <i>initial code entry-point</i> to
1185: the entry-point for any executable code to be immediately run.</p>
1186:
1187: <p>Set the <span>script's global object</span> to <var title="">worker global scope</var>.</p>
1188:
1189: <p>Set the <span>script's browsing context</span> to <var title="">owner browsing context</var>.</p>
1190:
1.232 ihickson 1191: <p>Set the <span>script's document</span> to <var title="">owner
1192: document</var>.</p>
1193:
1.83 ihickson 1194: <p>Set the <span>script's URL character encoding</span> to
1.81 ihickson 1195: UTF-8. (This is just used for encoding non-ASCII characters in the
1196: query component of URLs.)</p>
1.68 ihickson 1197:
1198: <p>Set the <span>script's base URL</span> to <var title="">url</var>.</p>
1.4 ihickson 1199:
1.81 ihickson 1200: </li>
1.4 ihickson 1201:
1202: <li>
1.81 ihickson 1203:
1204: <p><strong>Closing orphan workers</strong>: Start monitoring the
1.125 ihickson 1205: worker such that no sooner than it stops being either a
1206: <a href="#protected-worker">protected worker</a> or a <a href="#suspendable-worker">suspendable
1.123 ihickson 1207: worker</a>, and no later than it stops being a
1208: <a href="#permissible-worker">permissible worker</a>, <var title="">worker global
1209: scope</var>'s <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag is set
1.281 ihickson 1210: to true.</p>
1.81 ihickson 1211:
1212: </li>
1.9 ihickson 1213:
1214: <li>
1.7 ihickson 1215:
1.81 ihickson 1216: <p><strong>Suspending workers</strong>: Start monitoring the
1217: worker, such that whenever <var title="">worker global
1.115 ihickson 1218: scope</var>'s <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag is false
1219: and the worker is a <a href="#suspendable-worker">suspendable worker</a>, the user
1.81 ihickson 1220: agent suspends execution of script in that worker until such time
1.115 ihickson 1221: as either the <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag switches
1222: to true or the worker stops being a <a href="#suspendable-worker">suspendable
1.81 ihickson 1223: worker</a>.</p>
1.4 ihickson 1224:
1.81 ihickson 1225: </li>
1.68 ihickson 1226:
1227: <li>
1.4 ihickson 1228:
1.81 ihickson 1229: <p><span title="jump to a code entry-point">Jump</span> to the
1.115 ihickson 1230: <span title="concept-script">script</span>'s <i>initial code
1.81 ihickson 1231: entry-point</i>, and let that run until it either returns, fails
1232: to catch an exception, or gets prematurely aborted by the
1.115 ihickson 1233: "<a href="#kill-a-worker">kill a worker</a>" or "<a href="#terminate-a-worker">terminate a worker</a>"
1.81 ihickson 1234: algorithms defined below.</p>
1235:
1.4 ihickson 1236:
1.81 ihickson 1237: </li>
1.4 ihickson 1238:
1.95 ihickson 1239: <li><p>If <var title="">worker global scope</var> is actually a
1.115 ihickson 1240: <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> object (i.e. the worker is
1.126 ihickson 1241: a dedicated worker), then enable the <span>port message
1242: queue</span> of the worker's implicit port.</li>
1.95 ihickson 1243:
1.4 ihickson 1244: <li>
1.68 ihickson 1245:
1.115 ihickson 1246: <p><i title="">Event loop</i>: Wait until either there is a <span title="concept-task">task</span> in one of the <span>event
1247: loop</span>'s <span title="task queue">task queues</span> or <var title="">worker global scope</var>'s <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag is set
1.81 ihickson 1248: to true.</p>
1.43 ihickson 1249:
1.81 ihickson 1250: </li>
1.4 ihickson 1251:
1252: <li>
1253:
1.81 ihickson 1254: <p>Run the oldest task on one of the <span>event loop</span>'s
1255: <span title="task queue">task queues</span>, if any. The user
1256: agent may pick any <span>task queue</span>.</p>
1257:
1.115 ihickson 1258: <p class="note">The handling of events or the execution of
1259: callbacks might get prematurely aborted by the "<a href="#kill-a-worker">kill a
1260: worker</a>" or "<a href="#terminate-a-worker">terminate a worker</a>" algorithms
1.81 ihickson 1261: defined below.</p>
1.8 ihickson 1262:
1.81 ihickson 1263: </li>
1.4 ihickson 1264:
1265: <li>
1266:
1.248 ihickson 1267: <p>If the <span>storage mutex</span> is now owned by the worker's
1268: <span>event loop</span>, release it so that it is once again
1269: free.</p>
1270:
1271: </li>
1272:
1273: <li>
1274:
1275: <p>Remove the task just run in the earlier step, if any, from its
1.81 ihickson 1276: <span>task queue</span>.</p>
1.4 ihickson 1277:
1.81 ihickson 1278: </li>
1.4 ihickson 1279:
1280: <li>
1.8 ihickson 1281:
1.81 ihickson 1282: <p>If there are any more events in the <span>event loop</span>'s
1.115 ihickson 1283: <span title="task queue">task queues</span> or if <var title="">worker global scope</var>'s <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag is set
1.81 ihickson 1284: to false, then jump back to the step above labeled <i>event
1285: loop</i>.</p>
1.8 ihickson 1286:
1.81 ihickson 1287: </li>
1.8 ihickson 1288:
1289: <li>
1.71 ihickson 1290:
1.123 ihickson 1291: <p>Empty the <var title="">worker global scope</var>'s <span>list
1292: of active timeouts</span> and its <span>list of active
1293: intervals</span>.</p>
1.50 ihickson 1294:
1.81 ihickson 1295: </li>
1.50 ihickson 1296:
1.247 ihickson 1297: <li>
1298:
1299: <p>Disentangle all the ports in the list of <a href="#the-worker-s-ports">the worker's
1300: ports</a>.</p>
1301:
1302: </li>
1303:
1.155 ihickson 1304: </ol><hr><p>When a user agent is to <dfn id="kill-a-worker">kill a worker</dfn> it must
1.81 ihickson 1305: run the following steps in parallel with the worker's main loop (the
1.281 ihickson 1306: "<a href="#run-a-worker">run a worker</a>" processing model defined above):<ol><li><p>Set the worker's <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag to
1.81 ihickson 1307: true.</li>
1308:
1309:
1.115 ihickson 1310: <li><p>If there are any <span title="concept-task">tasks</span>
1.81 ihickson 1311: queued in the <span>event loop</span>'s <span title="task
1.281 ihickson 1312: queue">task queues</span>, discard them without processing them.</li>
1313:
1.81 ihickson 1314:
1315: <li><p>Wait a user-agent-defined amount of time.</li>
1316:
1.281 ihickson 1317: <li><p>Abort the script currently running in the worker.</li>
1.81 ihickson 1318:
1.155 ihickson 1319: </ol><p>User agents may invoke the "<a href="#kill-a-worker">kill a worker</a>"
1.81 ihickson 1320: processing model on a worker at any time, e.g. in response to user
1321: requests, in response to CPU quota management, or when a worker
1.115 ihickson 1322: stops being an <a href="#active-needed-worker">active needed worker</a> if the worker
1323: continues executing even after its <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag was
1.155 ihickson 1324: set to true.<hr><p>When a user agent is to <dfn id="terminate-a-worker">terminate a worker</dfn> it must run
1.81 ihickson 1325: the following steps in parallel with the worker's main loop (the
1.155 ihickson 1326: "<a href="#run-a-worker">run a worker</a>" processing model defined above):<ol><li><p>Set the worker's <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object's
1.115 ihickson 1327: <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag to
1.81 ihickson 1328: true.</li>
1329:
1.115 ihickson 1330: <li><p>If there are any <span title="concept-task">tasks</span>
1.81 ihickson 1331: queued in the <span>event loop</span>'s <span title="task
1332: queue">task queues</span>, discard them without processing
1333: them.</li>
1334:
1335: <li><p>Abort the script currently running in the worker.</li>
1336:
1.115 ihickson 1337: <li><p>If the worker's <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object is
1338: actually a <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code> object (i.e. the
1.81 ihickson 1339: worker is a dedicated worker), then empty the <span>port message
1340: queue</span> of the port that the worker's implicit port is
1341: entangled with.</li>
1342:
1.155 ihickson 1343: </ol><hr><p>The <span>task source</span> for the tasks mentioned above is the
1.301 ! ihickson 1344: <span>DOM manipulation task source</span>.<h3 id="runtime-script-errors"><span class="secno">4.7 </span>Runtime script errors</h3><p>Whenever an uncaught runtime script error occurs in one of the
1.143 ihickson 1345: worker's scripts, if the error did not occur while handling a
1346: previous script error, the user agent must <span>report the
1347: error</span> using the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code title="handler-WorkerGlobalScope-onerror"><a href="#handler-workerglobalscope-onerror">onerror</a></code>
1.205 ihickson 1348: attribute.
1349:
1.226 ihickson 1350: <a href="#refsHTML">[HTML]</a>
1.205 ihickson 1351:
1352: <p>For shared workers, if the error is still <i title="concept-error-nothandled">not handled</i> afterwards, or if
1.132 ihickson 1353: the error occurred while handling a previous script error, the error
1.215 ihickson 1354: may be reported to the user.
1.205 ihickson 1355:
1.226 ihickson 1356: <a href="#refsHTML">[HTML]</a>
1.205 ihickson 1357:
1358: <p>For dedicated workers, if the error is still <i title="concept-error-nothandled">not handled</i> afterwards, or if
1.132 ihickson 1359: the error occurred while handling a previous script error, the user
1.292 ihickson 1360: agent must <span>queue a task</span> to fire an event that uses the
1361: <code><a href="#errorevent">ErrorEvent</a></code> interface, with the name <code title="event-error">error</code>, that doesn't bubble and is
1.115 ihickson 1362: cancelable, with its <code title="dom-ErrorEvent-message"><a href="#dom-errorevent-message">message</a></code>, <code title="dom-ErrorEvent-filename"><a href="#dom-errorevent-filename">filename</a></code>, and <code title="dom-ErrorEvent-lineno"><a href="#dom-errorevent-lineno">lineno</a></code> attributes set
1.292 ihickson 1363: appropriately, at the <code><a href="#worker">Worker</a></code> object associated with the
1364: worker. If the event is not canceled, the user agent must act as if the
1365: uncaught runtime script error had occurred in the global scope that
1366: the <code><a href="#worker">Worker</a></code> object is in, thus repeating the entire
1367: runtime script error reporting process one level up.<p>If the implicit port connecting the worker to its
1368: <code><a href="#worker">Worker</a></code> object has been disentangled (i.e. if the parent
1369: worker has been terminated), then the user agent must act as if the
1370: <code><a href="#worker">Worker</a></code> object had no <code title="event-error">error</code> event handler and as if that
1371: worker's <code title="handler-WorkerGlobalScope-onerror"><a href="#handler-workerglobalscope-onerror">onerror</a></code> attribute
1372: was null, but must otherwise act as described above.<p class="note">Thus, error reports proagate up to the chain of
1373: dedicated workers up to the original <code>Document</code>, even if
1374: some of the workers along this chain have been terminated and
1375: garbage collected.<p>The <span>task source</span> for the task mentioned above is the
1.233 ihickson 1376: <span>DOM manipulation task source</span>.<hr><pre class="idl">interface <dfn id="errorevent">ErrorEvent</dfn> : <span>Event</span> {
1.147 ihickson 1377: readonly attribute DOMString <a href="#dom-errorevent-message" title="dom-ErrorEvent-message">message</a>;
1378: readonly attribute DOMString <a href="#dom-errorevent-filename" title="dom-ErrorEvent-filename">filename</a>;
1.115 ihickson 1379: readonly attribute unsigned long <a href="#dom-errorevent-lineno" title="dom-ErrorEvent-lineno">lineno</a>;
1.147 ihickson 1380: void <a href="#dom-errorevent-initerrorevent" title="dom-ErrorEvent-initErrorEvent">initErrorEvent</a>(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in DOMString messageArg, in DOMString filenameArg, in unsigned long linenoArg);
1.155 ihickson 1381: };</pre><p>The <dfn id="dom-errorevent-initerrorevent" title="dom-ErrorEvent-initErrorEvent"><code>initErrorEvent()</code></dfn>
1.190 ihickson 1382: method must initialize the event in a manner analogous to the
1383: similarly-named method in the DOM Events interfaces. <a href="#refsDOMEVENTS">[DOMEVENTS]</a><p>The <dfn id="dom-errorevent-message" title="dom-ErrorEvent-message"><code>message</code></dfn>
1.155 ihickson 1384: attribute represents the error message.<p>The <dfn id="dom-errorevent-filename" title="dom-ErrorEvent-filename"><code>filename</code></dfn>
1.81 ihickson 1385: attribute represents the <span>absolute URL</span> of the script in
1.155 ihickson 1386: which the error originally occurred.<p>The <dfn id="dom-errorevent-lineno" title="dom-ErrorEvent-lineno"><code>lineno</code></dfn>
1.132 ihickson 1387: attribute represents the line number where the error occurred in the
1.301 ! ihickson 1388: script.<h3 id="creating-workers"><span class="secno">4.8 </span>Creating workers</h3><h4 id="the-abstractworker-abstract-interface"><span class="secno">4.8.1 </span>The <code><a href="#abstractworker">AbstractWorker</a></code> abstract interface</h4><pre class="idl">[Supplemental, NoInterfaceObject]
1.208 ihickson 1389: interface <dfn id="abstractworker">AbstractWorker</dfn> {
1.291 ihickson 1390: attribute <span>Function</span>? <a href="#handler-abstractworker-onerror" title="handler-AbstractWorker-onerror">onerror</a>;
1.172 ihickson 1391: };
1.177 ihickson 1392: <a href="#abstractworker">AbstractWorker</a> implements <span>EventTarget</span>;</pre><p>The following are the <span>event handlers</span> (and their
1393: corresponding <span title="event handler event type">event handler
1394: event types</span>) that must be supported, as IDL attributes, by
1395: objects implementing the <code><a href="#abstractworker">AbstractWorker</a></code> interface:<table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1.155 ihickson 1396: <tbody><tr><td><dfn id="handler-abstractworker-onerror" title="handler-AbstractWorker-onerror"><code>onerror</code></dfn> <td> <code title="event-error">error</code>
1.301 ! ihickson 1397: </table><h4 id="dedicated-workers-and-the-worker-interface"><span class="secno">4.8.2 </span>Dedicated workers and the <code><a href="#worker">Worker</a></code> interface</h4><pre class="idl">[<a href="#dom-worker" title="dom-Worker">Constructor</a>(in DOMString scriptURL)]
1.115 ihickson 1398: interface <dfn id="worker">Worker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1399: void <a href="#dom-worker-terminate" title="dom-Worker-terminate">terminate</a>();
1.50 ihickson 1400:
1.296 ihickson 1401: void <a href="#dom-worker-postmessage" title="dom-Worker-postMessage">postMessage</a>(in any message, in optional sequence<<span>Transferable</span>> transfer); attribute <span>Function</span>? <a href="#handler-worker-onmessage" title="handler-Worker-onmessage">onmessage</a>;
1.155 ihickson 1402: };</pre><p>The <dfn id="dom-worker-terminate" title="dom-Worker-terminate"><code>terminate()</code></dfn> method,
1.115 ihickson 1403: when invoked, must cause the "<a href="#terminate-a-worker">terminate a worker</a>"
1.81 ihickson 1404: algorithm to be run on the worker with with the object is
1.155 ihickson 1405: associated.<p><code><a href="#worker">Worker</a></code> objects act as if they had an implicit
1.301 ! ihickson 1406: <code><a href="#messageport">MessagePort</a></code> associated with them. This port is part of
1.81 ihickson 1407: a channel that is set up when the worker is created, but it is not
1408: exposed. This object must never be garbage collected before the
1.155 ihickson 1409: <code><a href="#worker">Worker</a></code> object.<p>All messages received by that port must immediately be retargeted
1.281 ihickson 1410: at the <code><a href="#worker">Worker</a></code> object.<p>The <dfn id="dom-worker-postmessage" title="dom-Worker-postMessage"><code>postMessage()</code></dfn> method on <code><a href="#worker">Worker</a></code> objects
1411: must act as if, when invoked, it
1.81 ihickson 1412: immediately invoked the method of the same name on the port, with
1.177 ihickson 1413: the same arguments, and returned the same return value.<p>The following are the <span>event handlers</span> (and their
1414: corresponding <span title="event handler event type">event handler
1415: event types</span>) that must be supported, as IDL attributes, by
1416: objects implementing the <code><a href="#worker">Worker</a></code> interface:<table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1.155 ihickson 1417: <tbody><tr><td><dfn id="handler-worker-onmessage" title="handler-Worker-onmessage"><code>onmessage</code></dfn> <td> <code title="event-message">message</code>
1418: </table><hr><p>When the <dfn id="dom-worker" title="dom-Worker"><code>Worker(<var title="">scriptURL</var>)</code></dfn> constructor is invoked, the
1.216 ihickson 1419: user agent must run the following steps:<ol><li><p><span title="resolve a url">Resolve</span> the <var title="">scriptURL</var> argument relative to the <span>entry
1.133 ihickson 1420: script</span>'s <span title="script's base URL">base URL</span>,
1421: when the method is invoked.</li>
1.9 ihickson 1422:
1.81 ihickson 1423: <li><p>If this fails, throw a <code>SYNTAX_ERR</code>
1424: exception.</li>
1.9 ihickson 1425:
1.167 ihickson 1426: <li>
1427:
1428: <p>If the <span>origin</span> of the resulting <span>absolute
1429: URL</span> is not the <span title="same origin">same</span> as the
1.216 ihickson 1430: origin of the <span>entry script</span>, then throw a
1.182 ihickson 1431: <code>SECURITY_ERR</code> exception.</p>
1.167 ihickson 1432:
1433: <p class="note">Thus, scripts must be external files with the same
1.269 ihickson 1434: scheme as the original page: you can't load a script from a <span title="data protocol"><code title="">data:</code> URL</span> or
1435: <span title="javascript protocol"><code title="">javascript:</code> URL</span>, and an <code>http:</code>
1436: page couldn't start workers using scripts with <code>http:</code>
1437: URLs.</p>
1.167 ihickson 1438:
1439: </li>
1.9 ihickson 1440:
1.185 ihickson 1441: <li><p>Create a new <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>
1442: object. Let <var title="">worker global scope</var> be this new
1443: object.</li>
1.9 ihickson 1444:
1.115 ihickson 1445: <li><p>Create a new <code><a href="#worker">Worker</a></code> object, associated with
1.81 ihickson 1446: <var title="">worker global scope</var>. Let <var title="">worker</var> be this new object.</li>
1.50 ihickson 1447:
1.301 ! ihickson 1448: <li><p><span>Create a new <code><a href="#messageport">MessagePort</a></code> object</span>
1.192 ihickson 1449: owned by the <span title="script's global object">global
1450: object</span> of the <span title="concept-script">script</span> that
1.185 ihickson 1451: invoked the constructor. Let this be the <var title="">outside
1.81 ihickson 1452: port</var>.</li>
1.9 ihickson 1453:
1.81 ihickson 1454: <li><p>Associate the <var title="">outside port</var> with <var title="">worker</var>.</li>
1.50 ihickson 1455:
1.301 ! ihickson 1456: <li><p><span>Create a new <code><a href="#messageport">MessagePort</a></code> object</span>
1.185 ihickson 1457: owned by <var title="">worker global scope</var>. Let <var title="">inside port</var> be this new object.</li>
1.50 ihickson 1458:
1.81 ihickson 1459: <li><p>Associate <var title="">inside port</var> with <var title="">worker global scope</var>.</li>
1.50 ihickson 1460:
1.81 ihickson 1461: <li><p><span>Entangle</span> <var title="">outside port</var> and
1462: <var title="">inside port</var>.</li>
1.50 ihickson 1463:
1.81 ihickson 1464: <li><p>Return <var title="">worker</var>, and run the following
1465: steps asynchronously.</li>
1.50 ihickson 1466:
1467:
1.126 ihickson 1468: <li><p>Enable <var title="">outside port</var>'s <span>port message
1.81 ihickson 1469: queue</span>.</li>
1.50 ihickson 1470:
1471: <li>
1.51 ihickson 1472:
1.123 ihickson 1473: <p>Let <var title="">docs</var> be the <a href="#list-of-relevant-document-objects-to-add">list of relevant
1.192 ihickson 1474: <code>Document</code> objects to add</a> given the <span title="script's global object">global object</span> of the <span title="concept-script">script</span> that invoked the
1.123 ihickson 1475: constructor.</p>
1476:
1477: </li>
1478:
1479: <li>
1480:
1481: <p><a href="#add-a-document-to-the-worker-s-documents" title="add a document to the worker's documents">Add to
1482: <var title="">worker global scope</var>'s list of <span>the
1483: worker's <code>Document</code>s</span></a> the
1484: <code>Document</code> objects in <var title="">docs</var>.</p>
1485:
1486: </li>
1487:
1488: <li>
1489:
1.192 ihickson 1490: <p>If the <span title="script's global object">global object</span>
1.124 ihickson 1491: of the <span title="concept-script">script</span> that invoked the
1492: constructor is a <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object (i.e. we
1493: are creating a nested worker), add <var title="">worker global
1494: scope</var> to the list of <a href="#the-worker-s-workers">the worker's workers</a> of the
1.192 ihickson 1495: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object that is the <span title="script's global object">global object</span> of the <span title="concept-script">script</span> that invoked the
1.124 ihickson 1496: constructor.</p>
1497:
1498: </li>
1499:
1500: <li>
1501:
1.115 ihickson 1502: <p><a href="#run-a-worker">Run a worker</a> for the resulting <span>absolute
1.193 ihickson 1503: URL</span>, with the <span>script's browsing context</span> of the
1.81 ihickson 1504: script that invoked the method as the <var title="">owner browsing
1.232 ihickson 1505: context</var>, with the <span>script's document</span> of the
1506: script that invoked the method as the <var title="">owner
1507: document</var>, with the <span>origin</span> of the <span>entry
1.176 ihickson 1508: script</span> as the <var title="">owner origin</var>, and with
1509: <var title="">worker global scope</var> as the global scope.</p>
1.9 ihickson 1510:
1.81 ihickson 1511: </li>
1.4 ihickson 1512:
1.155 ihickson 1513: </ol><p>This constructor must be visible when the <span>script's global
1.192 ihickson 1514: object</span> is either a <code>Window</code> object or an object
1.301 ! ihickson 1515: implementing the <code><a href="#workerutils">WorkerUtils</a></code> interface.<h4 id="shared-workers-and-the-sharedworker-interface"><span class="secno">4.8.3 </span>Shared workers and the <code><a href="#sharedworker">SharedWorker</a></code> interface</h4><pre class="idl">[<a href="#dom-sharedworker" title="dom-SharedWorker">Constructor</a>(in DOMString scriptURL, in optional DOMString name)]
1.115 ihickson 1516: interface <dfn id="sharedworker">SharedWorker</dfn> : <a href="#abstractworker">AbstractWorker</a> {
1.301 ! ihickson 1517: readonly attribute <a href="#messageport">MessagePort</a> <a href="#dom-sharedworker-port" title="dom-SharedWorker-port">port</a>;
1.155 ihickson 1518: };</pre><p>The <dfn id="dom-sharedworker-port" title="dom-SharedWorker-port"><code>port</code></dfn>
1.81 ihickson 1519: attribute must return the value it was assigned by the object's
1.301 ! ihickson 1520: constructor. It represents the <code><a href="#messageport">MessagePort</a></code> for
1.155 ihickson 1521: communicating with the shared worker.<p>When the <dfn id="dom-sharedworker" title="dom-SharedWorker"><code>SharedWorker(<var title="">scriptURL</var>, <var title="">name</var>)</code></dfn>
1.81 ihickson 1522: constructor is invoked, the user agent must run the following
1.155 ihickson 1523: steps:<ol><li><p><span title="resolve a url">Resolve</span> the <var title="">scriptURL</var> argument.</li>
1.81 ihickson 1524:
1525: <li><p>If this fails, throw a <code>SYNTAX_ERR</code>
1526: exception.</li>
1527:
1.159 ihickson 1528: <li><p>Otherwise, let <var title="">scriptURL</var> be the
1529: resulting <span>absolute URL</span>.</li>
1530:
1531: <li><p>Let <var title="">name</var> be the value of the second
1.164 ihickson 1532: argument, or the empty string if the second argument was
1533: omitted.</li>
1.159 ihickson 1534:
1.167 ihickson 1535: <li>
1536:
1537: <p>If the <span>origin</span> of <var title="">scriptURL</var> is
1538: not the <span title="same origin">same</span> as the origin of the
1.216 ihickson 1539: <span>entry script</span>, then throw a <code>SECURITY_ERR</code>
1.182 ihickson 1540: exception.</p>
1.167 ihickson 1541:
1542: <p class="note">Thus, scripts must be external files with the same
1.269 ihickson 1543: scheme as the original page: you can't load a script from a <span title="data protocol"><code title="">data:</code> URL</span> or
1544: <span title="javascript protocol"><code title="">javascript:</code> URL</span>, and a <code>http:</code>
1545: page couldn't start workers using scripts with <code>http:</code>
1546: URLs.</p>
1.167 ihickson 1547:
1548: </li>
1.9 ihickson 1549:
1550: <li>
1551:
1.123 ihickson 1552: <p>Let <var title="">docs</var> be the <a href="#list-of-relevant-document-objects-to-add">list of relevant
1.192 ihickson 1553: <code>Document</code> objects to add</a> given the <span title="script's global object">global object</span> of the <span title="concept-script">script</span> that invoked the
1.123 ihickson 1554: constructor.</p>
1555:
1556: </li>
1557:
1558: <li>
1559:
1.50 ihickson 1560: <p>Execute the following substeps atomically:</p>
1.9 ihickson 1561:
1.155 ihickson 1562: <ol><li><p>Create a new <code><a href="#sharedworker">SharedWorker</a></code> object, which will
1.115 ihickson 1563: shortly be associated with a <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1564: object. Let this <code><a href="#sharedworker">SharedWorker</a></code> object be <var title="">worker</var>.</li>
1.81 ihickson 1565:
1.301 ! ihickson 1566: <li><p><span>Create a new <code><a href="#messageport">MessagePort</a></code> object</span>
1.192 ihickson 1567: owned by the <span title="script's global object">global
1568: object</span> of the script that invoked the method. Let this be
1.123 ihickson 1569: the <var title="">outside port</var>.</li>
1.81 ihickson 1570:
1.115 ihickson 1571: <li><p>Assign <var title="">outside port</var> to the <code title="dom-SharedWorker-port"><a href="#dom-sharedworker-port">port</a></code> attribute of <var title="">worker</var>.</li>
1.81 ihickson 1572:
1.164 ihickson 1573: <li><p>Let <var title="">worker global scope</var> be
1574: null.</li>
1575:
1576: <li><p>If <var title="">name</var> is not the empty string and
1577: there exists a <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object whose
1578: <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a> flag
1579: is false, whose <code title="dom-WorkerGlobalScope-name">name</code> attribute is
1580: exactly equal to <var title="">name</var>, and whose <code title="dom-WorkerGlobalScope-location"><a href="#dom-workerglobalscope-location">location</a></code> attribute
1581: represents an <span>absolute URL</span> with the <span>same
1582: origin</span> as <var title="">scriptURL</var>, then let <var title="">worker global scope</var> be that
1583: <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object.</li>
1584:
1585: <li><p>Otherwise, if <var title="">name</var> is the empty string
1586: and there exists a <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code> object
1587: whose <a href="#dom-workerglobalscope-closing" title="dom-WorkerGlobalScope-closing">closing</a>
1.293 ihickson 1588: flag is false, whose <code title="dom-WorkerGlobalScope-name">name</code> attribute is the
1589: empty string, and whose <code title="dom-WorkerGlobalScope-location"><a href="#dom-workerglobalscope-location">location</a></code> attribute
1.237 ihickson 1590: represents an <span>absolute URL</span> that is exactly equal to
1591: <var title="">scriptURL</var>, then let <var title="">worker
1592: global scope</var> be that <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1593: object.</li>
1.164 ihickson 1594:
1.9 ihickson 1595: <li>
1596:
1.164 ihickson 1597: <p>If <var title="">worker global scope</var> is not null, then
1598: run these steps:</p>
1.81 ihickson 1599:
1.164 ihickson 1600: <ol><li><p>If <var title="">worker global scope</var>'s <code title="dom-WorkerGlobalScope-location"><a href="#dom-workerglobalscope-location">location</a></code>
1.81 ihickson 1601: attribute represents an <span>absolute URL</span> that is not
1.159 ihickson 1602: exactly equal to <var title="">scriptURL</var>, then throw a
1603: <code>URL_MISMATCH_ERR</code> exception and abort all these
1604: steps.</li>
1.81 ihickson 1605:
1606: <li><p>Associate <var title="">worker</var> with <var title="">worker global scope</var>.</li>
1607:
1.301 ! ihickson 1608: <li><p><span>Create a new <code><a href="#messageport">MessagePort</a></code>
1.185 ihickson 1609: object</span> owned by <var title="">worker global
1610: scope</var>. Let this be the <var title="">inside
1611: port</var>.</li>
1.81 ihickson 1612:
1613: <li><p><span>Entangle</span> <var title="">outside port</var>
1614: and <var title="">inside port</var>.</li>
1615:
1616: <li><p>Return <var title="">worker</var> and perform the next
1617: step asynchronously.</li>
1.9 ihickson 1618:
1.81 ihickson 1619: <li><p>Create an event that uses the <code>MessageEvent</code>
1.115 ihickson 1620: interface, with the name <code title="event-connect">connect</code>, which does not bubble, is
1.118 ihickson 1621: not cancelable, has no default action, has a <code title="dom-MessageEvent-data">data</code> attribute whose value
1622: is the empty string and has a <code title="dom-MessageEvent-ports">ports</code> attribute whose
1.286 ihickson 1623: value is a <span title="dfn-read-only-array">read only</span>
1624: array containing only the newly created port, and <span>queue a
1625: task</span> to dispatch the event at <var title="">worker
1626: global scope</var>.</li>
1.43 ihickson 1627:
1.123 ihickson 1628: <li>
1629:
1630: <p><a href="#add-a-document-to-the-worker-s-documents" title="add a document to the worker's documents">Add to
1631: <var title="">worker global scope</var>'s list of <span>the
1632: worker's <code>Document</code>s</span></a> the
1633: <code>Document</code> objects in <var title="">docs</var>.</p>
1634:
1635: </li>
1636:
1.124 ihickson 1637: <li>
1638:
1.192 ihickson 1639: <p>If the <span title="script's global object">global
1640: object</span> of the <span title="concept-script">script</span>
1.124 ihickson 1641: that invoked the constructor is a
1642: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object, add <var title="">worker global scope</var> to the list of <a href="#the-worker-s-workers">the
1643: worker's workers</a> of the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1.192 ihickson 1644: object that is the <span title="script's global object">global
1645: object</span> of the <span title="concept-script">script</span>
1.124 ihickson 1646: that invoked the constructor.</p>
1647:
1648: </li>
1649:
1.81 ihickson 1650: <li><p>Abort all these steps.</li>
1.9 ihickson 1651:
1.155 ihickson 1652: </ol></li>
1.9 ihickson 1653:
1.185 ihickson 1654: <li><p>Create a new <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>
1655: object. Let <var title="">worker global scope</var> be this new
1656: object.</li>
1.9 ihickson 1657:
1.81 ihickson 1658: <li><p>Associate <var title="">worker</var> with <var title="">worker global scope</var>.</li>
1.9 ihickson 1659:
1.115 ihickson 1660: <li><p>Set the <code title="dom-SharedWorkerGlobalScope-name"><a href="#dom-sharedworkerglobalscope-name">name</a></code> attribute of
1.81 ihickson 1661: <var title="">worker global scope</var> to <var title="">name</var>.</li>
1.9 ihickson 1662:
1.301 ! ihickson 1663: <li><p><span>Create a new <code><a href="#messageport">MessagePort</a></code> object</span>
1.81 ihickson 1664: owned by <var title="">worker global scope</var>. Let <var title="">inside port</var> be this new object.</li>
1.9 ihickson 1665:
1.81 ihickson 1666: <li><p><span>Entangle</span> <var title="">outside port</var> and
1667: <var title="">inside port</var>.</li>
1.9 ihickson 1668:
1.155 ihickson 1669: </ol></li>
1.28 ihickson 1670:
1.123 ihickson 1671: <li><p>Return <var title="">worker</var> and perform the remaining
1672: steps asynchronously.</li>
1.18 ihickson 1673:
1.81 ihickson 1674: <li><p>Create an event that uses the <code>MessageEvent</code>
1.118 ihickson 1675: interface, with the name <code title="event-connect">connect</code>, which does not bubble, is not
1.115 ihickson 1676: cancelable, has no default action, has a <code title="dom-MessageEvent-data">data</code> attribute whose value is
1.118 ihickson 1677: the empty string and has a <code title="dom-MessageEvent-ports">ports</code> attribute whose value
1.286 ihickson 1678: is a <span title="dfn-read-only-array">read only</span> array
1679: containing only the newly created port, and <span>queue a
1680: task</span> to dispatch the event at <var title="">worker global
1.118 ihickson 1681: scope</var>.</li>
1.26 ihickson 1682:
1683: <li>
1.46 ihickson 1684:
1.123 ihickson 1685: <p><a href="#add-a-document-to-the-worker-s-documents" title="add a document to the worker's documents">Add to
1686: <var title="">worker global scope</var>'s list of <span>the
1687: worker's <code>Document</code>s</span></a> the
1688: <code>Document</code> objects in <var title="">docs</var>.</p>
1689:
1690: </li>
1691:
1692: <li>
1693:
1.192 ihickson 1694: <p>If the <span title="script's global object">global object</span>
1.124 ihickson 1695: of the <span title="concept-script">script</span> that invoked the
1696: constructor is a <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object, add <var title="">worker global scope</var> to the list of <a href="#the-worker-s-workers">the
1697: worker's workers</a> of the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
1.192 ihickson 1698: object that is the <span title="script's global object">global
1699: object</span> of the <span title="concept-script">script</span>
1.124 ihickson 1700: that invoked the constructor.</p>
1701:
1702: </li>
1703:
1704: <li>
1705:
1.159 ihickson 1706: <p><a href="#run-a-worker">Run a worker</a> for <var title="">scriptURL</var>,
1.193 ihickson 1707: with the <span>script's browsing context</span> of the script that
1.159 ihickson 1708: invoked the method as the <var title="">owner browsing
1.232 ihickson 1709: context</var>, with the <span>script's document</span> of the
1710: script that invoked the method as the <var title="">owner
1711: document</var>, with the <span>origin</span> of the <span>entry
1.176 ihickson 1712: script</span> as the <var title="">owner origin</var>, and with
1713: <var title="">worker global scope</var> as the global scope.</p>
1.26 ihickson 1714:
1.60 ihickson 1715: </li>
1.81 ihickson 1716:
1.155 ihickson 1717: </ol><p>This constructor must be visible when the <span>script's global
1.192 ihickson 1718: object</span> is either a <code>Window</code> object or an object
1.155 ihickson 1719: implementing the <code><a href="#workerutils">WorkerUtils</a></code> interface.<p>The <span>task source</span> for the tasks mentioned above is the
1.299 ihickson 1720: <span>DOM manipulation task source</span>.<h2 id="apis-available-to-workers"><span class="secno">5 </span>APIs available to workers</h2><pre class="idl">[NoInterfaceObject]
1.150 ihickson 1721: interface <dfn id="workerutils">WorkerUtils</dfn> {
1722: void <a href="#dom-workerglobalscope-importscripts" title="dom-WorkerGlobalScope-importScripts">importScripts</a>(in DOMString... urls);
1.210 ihickson 1723: readonly attribute <a href="#workernavigator">WorkerNavigator</a> <a href="#dom-worker-navigator" title="dom-worker-navigator">navigator</a>;
1.150 ihickson 1724: };
1.290 ihickson 1725: <a href="#workerutils">WorkerUtils</a> implements <span>WindowTimers</span>;
1.301 ! ihickson 1726: <a href="#workerutils">WorkerUtils</a> implements <span>WindowBase64</span>;</pre><p>The <code>WindowTimers</code> and <code>WindowBase64</code>
! 1727: interfaces are defined in the HTML specification. <a href="#refsHTML">[HTML]</a><p>The DOM APIs (<code>Node</code> objects, <code>Document</code>
1.81 ihickson 1728: objects, etc) are not available to workers in this version of this
1.155 ihickson 1729: specification.<h3 id="importing-scripts-and-libraries"><span class="secno">5.1 </span>Importing scripts and libraries</h3><p>When a script invokes the <dfn id="dom-workerglobalscope-importscripts" title="dom-WorkerGlobalScope-importScripts"><code>importScripts(<var title="">urls</var>)</code></dfn> method on a
1.115 ihickson 1730: <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user agent must run the
1.155 ihickson 1731: following steps:<ol><li><p>If there are no arguments, return without doing
1.81 ihickson 1732: anything. Abort these steps.</li>
1733:
1734: <li><p><span title="resolve a url">Resolve</span> each
1735: argument.</li>
1736:
1737: <li><p>If any fail, throw a <code>SYNTAX_ERR</code>
1738: exception.</li>
1739:
1.26 ihickson 1740:
1741: <li>
1.81 ihickson 1742:
1.45 ihickson 1743: <p>Attempt to <span>fetch</span> each resource identified by the
1.194 ihickson 1744: resulting <span title="absolute URL">absolute URLs</span>, from
1.256 ihickson 1745: the <span>entry script</span>'s <span>origin</span>, with the
1.281 ihickson 1746: <i>synchronous flag</i> set.</p>
1.81 ihickson 1747: </li>
1.26 ihickson 1748:
1749: <li>
1750:
1.81 ihickson 1751: <p>For each argument in turn, in the order given, starting with
1752: the first one, run these substeps:</p>
1.26 ihickson 1753:
1.155 ihickson 1754: <ol><li>
1.26 ihickson 1755:
1.81 ihickson 1756: <p>Wait for the fetching attempt for the corresponding resource
1757: to complete.</p>
1.60 ihickson 1758:
1.81 ihickson 1759: <p>If the fetching attempt failed, throw a
1760: <code>NETWORK_ERR</code> exception and abort all these
1761: steps.</p>
1.45 ihickson 1762:
1.264 ihickson 1763: <p>If the attempt succeeds, then let <var title="">source</var> be
1764: the script resource <span>decoded as UTF-8, with error
1765: handling</span>.
1766:
1767: <a href="#refsHTML">[HTML]</a>
1768:
1769: </p>
1.85 ihickson 1770:
1771: <p>Let <var title="">language</var> be JavaScript.</p>
1.61 ihickson 1772:
1.115 ihickson 1773: <p class="note">As with the worker's script, the script here is
1.81 ihickson 1774: always assumed to be JavaScript, regardless of the MIME
1.170 ihickson 1775: type.</p>
1.61 ihickson 1776:
1.81 ihickson 1777: </li>
1.26 ihickson 1778:
1.81 ihickson 1779: <li>
1.26 ihickson 1780:
1.81 ihickson 1781: <p><span>Create a script</span>, using <var title="">source</var> as the script source and <var title="">language</var> as the scripting language, using the
1.189 ihickson 1782: same global object, browsing context, URL character encoding,
1783: base URL, and script group as the <span title="concept-script">script</span> that was created by the
1.115 ihickson 1784: worker's <a href="#run-a-worker">run a worker</a> algorithm.</p>
1.81 ihickson 1785:
1.115 ihickson 1786: <p>Let the newly created <span title="concept-script">script</span> run until it either
1.81 ihickson 1787: returns, fails to parse, fails to catch an exception, or gets
1.115 ihickson 1788: prematurely aborted by the "<a href="#kill-a-worker">kill a worker</a>" or
1789: "<a href="#terminate-a-worker">terminate a worker</a>" algorithms defined above.</p>
1.26 ihickson 1790:
1.199 ihickson 1791: <p>If it failed to parse, then throw an ECMAScript
1792: <code>SyntaxError</code> exception and abort all these
1793: steps. <a href="#refsECMA262">[ECMA262]</a></p>
1.63 ihickson 1794:
1.81 ihickson 1795: <p>If an exception was raised or if the script was prematurely
1796: aborted, then abort all these steps, letting the exception or
1797: aborting continue to be processed by the script that called the
1.115 ihickson 1798: <code title="dom-WorkerGlobalScope-importScripts"><a href="#dom-workerglobalscope-importscripts">importScripts()</a></code>
1.81 ihickson 1799: method.</p>
1800:
1.115 ihickson 1801: <p>If the "<a href="#kill-a-worker">kill a worker</a>" or "<a href="#terminate-a-worker">terminate a
1.81 ihickson 1802: worker</a>" algorithms abort the script then abort all these
1803: steps.</p>
1804:
1805: </li>
1806:
1.155 ihickson 1807: </ol></li>
1.152 ihickson 1808:
1.210 ihickson 1809: </ol><h3 id="the-workernavigator-object"><span class="secno">5.2 </span>The <code><a href="#workernavigator">WorkerNavigator</a></code> object</h3><p>The <dfn id="dom-worker-navigator" title="dom-worker-navigator"><code>navigator</code></dfn> attribute
1.115 ihickson 1810: of the <code><a href="#workerutils">WorkerUtils</a></code> interface must return an instance of
1.210 ihickson 1811: the <code><a href="#workernavigator">WorkerNavigator</a></code> interface, which represents the
1812: identity and state of the user agent (the client):<pre class="idl">interface <dfn id="workernavigator">WorkerNavigator</dfn> {};
1813: <a href="#workernavigator">WorkerNavigator</a> implements <span>NavigatorID</span>;
1814: <a href="#workernavigator">WorkerNavigator</a> implements <span>NavigatorOnLine</span>;</pre><p>Objects implementing the <code><a href="#workernavigator">WorkerNavigator</a></code> interface
1815: also implement the <code>NavigatorID</code> and
1816: <code>NavigatorOnLine</code> interfaces.
1.205 ihickson 1817:
1.226 ihickson 1818: <a href="#refsHTML">[HTML]</a>
1.205 ihickson 1819:
1.210 ihickson 1820: <p>This <code><a href="#workernavigator">WorkerNavigator</a></code> interface must not exist if the
1821: interface's <span>relevant namespace object</span> is a
1.272 ihickson 1822: <code>Window</code> object. <a href="#refsWEBIDL">[WEBIDL]</a><h3 id="interface-objects-and-constructors"><span class="secno">5.3 </span>Interface objects and constructors</h3><p>There must be no interface objects and constructors available in
1.192 ihickson 1823: the global scope of scripts whose <span>script's global
1824: object</span> is a <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object except for
1.155 ihickson 1825: the following:<ul><li><p><code>XMLHttpRequest</code> and all interface objects and
1.81 ihickson 1826: constructors defined by the XMLHttpRequest specifications, except
1827: that the <span>document response entity body</span> must always be
1.160 ihickson 1828: null. The <span><code>XMLHttpRequest</code> base URL</span> is the
1829: <span>script's base URL</span>; the
1830: <span><code>XMLHttpRequest</code> origin</span> is the script's
1831: <span>origin</span>. <a href="#refsXHR">[XHR]</a></li>
1.81 ihickson 1832:
1.205 ihickson 1833: <li><p>The interface objects and constructors defined by this
1834: specification.</li>
1.119 ihickson 1835:
1.205 ihickson 1836: <li><p>Constructors defined by specifications that explicitly say
1837: that they should be visible when the <span>script's global
1838: object</span> is a <code><a href="#dedicatedworkerglobalscope">DedicatedWorkerGlobalScope</a></code>, a
1839: <code><a href="#sharedworkerglobalscope">SharedWorkerGlobalScope</a></code>, or an object implementing the
1840: <code><a href="#workerutils">WorkerUtils</a></code> interface; the interfaces of any objects
1841: with such constructors; and the interfaces of any objects made
1842: accessible through APIs exposed by those constructors or made
1843: accessible through interfaces to be implemented by any objects that
1844: are themselves accessible to scripts whose <span>script's global
1845: object</span> implements the <code><a href="#workerutils">WorkerUtils</a></code>
1846: interface.</li>
1847:
1848: </ul><p class="note">These requirements do not override the requirements
1849: defined by the Web IDL specification, in particular concerning the
1.272 ihickson 1850: visibility of interfaces annotated with the <code title="">[NoInterfaceObject]</code> extended attribute.<h3 id="worker-locations"><span class="secno">5.4 </span>Worker locations</h3><pre class="idl">interface <dfn id="workerlocation">WorkerLocation</dfn> {
1.263 ihickson 1851: // <span>URL decomposition IDL attributes</span>
1.294 ihickson 1852: stringifier readonly attribute DOMString <a href="#dom-workerlocation-href" title="dom-WorkerLocation-href">href</a>;
1.115 ihickson 1853: readonly attribute DOMString <a href="#dom-workerlocation-protocol" title="dom-WorkerLocation-protocol">protocol</a>;
1854: readonly attribute DOMString <a href="#dom-workerlocation-host" title="dom-WorkerLocation-host">host</a>;
1855: readonly attribute DOMString <a href="#dom-workerlocation-hostname" title="dom-WorkerLocation-hostname">hostname</a>;
1856: readonly attribute DOMString <a href="#dom-workerlocation-port" title="dom-WorkerLocation-port">port</a>;
1857: readonly attribute DOMString <a href="#dom-workerlocation-pathname" title="dom-WorkerLocation-pathname">pathname</a>;
1858: readonly attribute DOMString <a href="#dom-workerlocation-search" title="dom-WorkerLocation-search">search</a>;
1859: readonly attribute DOMString <a href="#dom-workerlocation-hash" title="dom-WorkerLocation-hash">hash</a>;
1.155 ihickson 1860: };</pre><p>A <code><a href="#workerlocation">WorkerLocation</a></code> object represents an <span>absolute
1861: URL</span> set at its creation.<p>The <dfn id="dom-workerlocation-href" title="dom-WorkerLocation-href"><code>href</code></dfn>
1.81 ihickson 1862: attribute must return the <span>absolute URL</span> that the object
1.155 ihickson 1863: represents.<p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface also has the complement
1.163 ihickson 1864: of <span>URL decomposition IDL attributes</span>, <dfn id="dom-workerlocation-protocol" title="dom-WorkerLocation-protocol"><code>protocol</code></dfn>,
1.115 ihickson 1865: <dfn id="dom-workerlocation-host" title="dom-WorkerLocation-host"><code>host</code></dfn>, <dfn id="dom-workerlocation-port" title="dom-WorkerLocation-port"><code>port</code></dfn>, <dfn id="dom-workerlocation-hostname" title="dom-WorkerLocation-hostname"><code>hostname</code></dfn>,
1866: <dfn id="dom-workerlocation-pathname" title="dom-WorkerLocation-pathname"><code>pathname</code></dfn>,
1867: <dfn id="dom-workerlocation-search" title="dom-WorkerLocation-search"><code>search</code></dfn>,
1.263 ihickson 1868: and <dfn id="dom-workerlocation-hash" title="dom-WorkerLocation-hash"><code>hash</code></dfn>.
1869: These must follow the rules given for <span>URL decomposition IDL
1870: attributes</span>, with the <span title="concept-uda-input">input</span> being the <span>absolute
1871: URL</span> that the object represents (same as the <code title="dom-WorkerLocation-href"><a href="#dom-workerlocation-href">href</a></code> attribute), and the
1872: <span title="concept-uda-setter">common setter action</span> being a
1873: no-op, since the attributes are defined to be readonly.
1.205 ihickson 1874:
1.226 ihickson 1875: <a href="#refsHTML">[HTML]</a>
1.205 ihickson 1876:
1.209 ihickson 1877: <p>The <code><a href="#workerlocation">WorkerLocation</a></code> interface must not exist if the
1878: interface's <span>relevant namespace object</span> is a
1.281 ihickson 1879: <code>Window</code> object. <a href="#refsWEBIDL">[WEBIDL]</a><h2 class="no-num" id="references">References</h2><p>All references are normative unless marked "Non-normative".</p><dl><dt id="refsDOMCORE">[DOMCORE]</dt>
1.276 ihickson 1880: <dd><cite><a href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html">Web DOM Core</a></cite>, A. van Kesteren. W3C.</dd>
1.153 ihickson 1881:
1.152 ihickson 1882: <dt id="refsDOMEVENTS">[DOMEVENTS]</dt>
1.281 ihickson 1883: <dd><cite><a href="http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html">Document
1.152 ihickson 1884: Object Model (DOM) Level 3 Events Specification</a></cite>,
1.226 ihickson 1885: D. Schepers. W3C.</dd>
1.152 ihickson 1886:
1887: <dt id="refsECMA262">[ECMA262]</dt>
1888: <dd><cite><a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript
1.226 ihickson 1889: Language Specification</a></cite>. ECMA.</dd>
1.152 ihickson 1890:
1.226 ihickson 1891: <dt id="refsHTML">[HTML]</dt>
1892: <dd><cite><a href="http://www.whatwg.org/specs/web-apps/current-work/">HTML</a></cite>,
1893: I. Hickson. WHATWG.</dd>
1.152 ihickson 1894:
1.154 ihickson 1895: <dt id="refsRFC2119">[RFC2119]</dt>
1.265 ihickson 1896: <dd><cite><a href="http://tools.ietf.org/html/rfc2119">Key words for use in
1.226 ihickson 1897: RFCs to Indicate Requirement Levels</a></cite>, S. Bradner. IETF.</dd>
1.154 ihickson 1898:
1899: <dt id="refsWEBIDL">[WEBIDL]</dt>
1.281 ihickson 1900: <dd><cite><a href="http://dev.w3.org/2006/webapi/WebIDL/">Web
1.226 ihickson 1901: IDL</a></cite>, C. McCormack. W3C.</dd>
1.154 ihickson 1902:
1.152 ihickson 1903: <dt id="refsXHR">[XHR]</dt>
1.281 ihickson 1904: <dd><cite><a href="http://dev.w3.org/2006/webapi/XMLHttpRequest-2/"><code>XMLHttpRequest</code></a></cite>,
1.226 ihickson 1905: A. van Kesteren. W3C.</dd>
1.152 ihickson 1906:
1.221 ihickson 1907: </dl><h2 class="no-num" id="acknowledgements">Acknowledgements</h2><!-- ACKS --><p>Thanks to
1.81 ihickson 1908:
1909: Aaron Boodman,
1.155 ihickson 1910: Алексей Проскуряков (Alexey Proskuryakov),
1.81 ihickson 1911: Anne van Kesteren,
1912: Ben Turner,
1913: Dmitry Titov,
1.96 ihickson 1914: Drew Wilson,
1.97 ihickson 1915: Jeremy Orlow,
1.81 ihickson 1916: Jonas Sicking,
1917: Justin James,
1918: Kevin Hakanson,
1919: Maciej Stachowiak,
1920: Michael Nordman,
1921: Mike Smith,
1922:
1923: and
1924:
1925: Philip Taylor
1926:
1.155 ihickson 1927: for their useful and substantial comments.<p>Huge thanks to the whole Gears team, who pioneered this
1.81 ihickson 1928: technology and whose experience has been a huge influence on this
1.155 ihickson 1929: specification.
Webmaster