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