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

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

Webmaster