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

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

Webmaster