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

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

Webmaster