Annotation of html5/workers/Overview.html, revision 1.333

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

Webmaster