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

1.1       ihickson    1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
                      2: <!-- when publishing, change bits marked ZZZ -->
                      3: 
                      4: <html lang=en-US-x-Hixie>
                      5:  <head>
                      6:   <title>Web Workers</title>
                      7:   <link href="http://www.w3.org/StyleSheets/TR/%57%33%43-ED" rel=stylesheet
                      8:   type="text/css">
                      9:   <!-- ZZZ ED vs WD -->
                     10: 
                     11:  <body>
                     12:   <div class=head>
                     13:    <p><a href="http://www.w3.org/"><img alt=W3C height=48
                     14:     src="http://www.w3.org/Icons/w3c_home" width=72></a></p>
                     15: 
                     16:    <h1 id=web-workers>Web Workers</h1>
                     17: 
                     18:    <h2 class="no-num no-toc" id=an-accompaniment>An accompaniment
                     19:     specification for HTML5</h2>
                     20: 
                     21:    <h2 class="no-num no-toc" id=editors><!-- "W3C Working Draft" --> Editor's
1.9       ihickson   22:     Draft <!--ZZZ-->18 July 2008</h2>
1.1       ihickson   23: 
                     24:    <dl><!-- ZZZ: update the month/day
                     25:     <dt>This Version:</dt>
                     26:     <dd><a href="http://www.w3.org/TR/2008/WD-workers-20080101/">http://www.w3.org/TR/2008/WD-workers-20080101/</a></dd>
1.2       ihickson   27:     <dt>Latest Published Version:</dt>
                     28:     <dd><a href="http://www.w3.org/TR/workers/">http://www.w3.org/TR/workers/</a></dd>
1.1       ihickson   29:  :ZZZ -->
                     30: 
                     31:     <dt>Latest Editor's Draft:
                     32: 
                     33:     <dd><a
                     34:      href="http://dev.w3.org/html5/workers/">http://dev.w3.org/html5/workers/</a></dd>
1.2       ihickson   35:     <!-- ZZZ: add the new version after it has shipped
                     36:     <dt>Previous Versions:</dt>
                     37:     <dd><a href="http://www.w3.org/TR/2008/WD-workers-20080101/">http://www.w3.org/TR/2008/WD-workers-20080101/</a>
                     38:  :ZZZ -->
1.1       ihickson   39: 
                     40:     <dt>Editors:
                     41: 
                     42:     <dd><a href="mailto:ian@hixie.ch">Ian Hickson</a>, Google, Inc.
                     43:    </dl>
                     44: 
                     45:    <p class=copyright><a
                     46:     href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
                     47:     &#169; 2008 <a href="http://www.w3.org/"><abbr title="World Wide Web
                     48:     Consortium">W3C</abbr></a><sup>&#174;</sup> (<a
                     49:     href="http://www.csail.mit.edu/"><abbr title="Massachusetts Institute of
                     50:     Technology">MIT</abbr></a>, <a href="http://www.ercim.org/"><abbr
                     51:     title="European Research Consortium for Informatics and
                     52:     Mathematics">ERCIM</abbr></a>, <a
                     53:     href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C <a
                     54:     href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
                     55:     <a
                     56:     href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
                     57:     and <a
                     58:     href="http://www.w3.org/Consortium/Legal/copyright-documents">document
                     59:     use</a> rules apply.</p>
                     60:    <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                     61:    
                     62:    <p class="alt copyright">The <a
                     63:     href="http://www.whatwg.org/specs/web-workers/current-work/">WHATWG
                     64:     version</a> of this specification is available under a more permissive
                     65:     license.</p>
                     66:    <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                     67:    </div>
                     68: 
                     69:   <hr>
                     70: 
                     71:   <h2 class="no-num no-toc" id=abstract>Abstract</h2>
                     72: 
                     73:   <p>This specification defines an API that allows Web application authors to
                     74:    spawn background workers running scripts in parallel to their main page.
                     75:    This allows for thread-like operation with message-passing as the
                     76:    coordination mechanism.
                     77: 
                     78:   <h2 class="no-num no-toc" id=status>Status of this document</h2>
                     79:   <!-- intro boilerplate (required) -->
                     80: 
                     81:   <p><em>This section describes the status of this document at the time of
                     82:    its publication. Other documents may supersede this document. A list of
                     83:    current W3C publications and the most recently formally published revision
                     84:    of this technical report can be found in the <a
                     85:    href="http://www.w3.org/TR/">W3C technical reports index</a> at
                     86:    http://www.w3.org/TR/.</em></p>
                     87:   <!-- where to send feedback (required) -->
                     88: 
                     89:   <p>If you wish to make comments regarding this document, please send them
                     90:    to <a
                     91:    href="mailto:public-html-comments@w3.org">public-html-comments@w3.org</a>
                     92:    (<a
                     93:    href="mailto:public-html-comments-request@w3.org?subject=subscribe">subscribe</a>,
                     94:    <a
                     95:    href="http://lists.w3.org/Archives/Public/public-html-comments/">archives</a>)
                     96:    <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING SENTENCE TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                     97:    or <a href="mailto:whatwg@whatwg.org">whatwg@whatwg.org</a> (<a
                     98:    href="http://lists.whatwg.org/listinfo.cgi/whatwg-whatwg.org">subscribe</a>,
                     99:    <a
                    100:    href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/">archives</a>).
                    101:    <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING SENTENCE TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    102:    All feedback is welcome.</p>
                    103:   <!-- stability (required) -->
                    104: 
                    105:   <p>Implementors should be aware that this specification is not stable.
                    106:    <strong>Implementors who are not taking part in the discussions are likely
                    107:    to find the specification changing out from under them in incompatible
                    108:    ways.</strong> Vendors interested in implementing this specification
                    109:    before it eventually reaches the Candidate Recommendation stage should
                    110:    join the aforementioned mailing lists and take part in the discussions.</p>
                    111:   <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    112:   <!-- version history or list of changes (required) -->
                    113: 
                    114:   <p>The latest stable version of the editor's draft of this specification is
                    115:    always available on <a
                    116:    href="http://dev.w3.org/html5/workers/Overview.html">the W3C CVS
                    117:    server</a> and in the <a href="http://svn.whatwg.org/webworkers/">WHATWG
                    118:    Subversion repository</a>. The latest editor's working copy (which may
                    119:    contain unfinished text in the process of being prepared) is available <a
                    120:    href="http://www.whatwg.org/specs/web-workers/current-work/">on the WHATWG
                    121:    site</a>. Detailed change history can be obtained from the following
                    122:    locations:</p>
                    123:   <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    124:   <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING LIST TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    125: 
                    126:   <ul>
                    127:    <li>Twitter messages (non-editorial changes only): <a
                    128:     href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a>
                    129: 
                    130:    <li>Interactive Web interface: <a
                    131:     href="http://html5.org/tools/web-workers-tracker">http://html5.org/tools/web-workers-tracker</a>
                    132: 
                    133:    <li>Commit-Watchers mailing list: <a
                    134:     href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a>
                    135: 
                    136:    <li>Subversion interface: <a
                    137:     href="http://svn.whatwg.org/webworkers/">http://svn.whatwg.org/webworkers/</a>
                    138: 
                    139:    <li>CVS log: <a
                    140:     href="http://dev.w3.org/cvsweb/html5/workers/Overview.html">http://dev.w3.org/cvsweb/html5/workers/Overview.html</a>
                    141:   </ul>
                    142:   <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING LIST TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    143:   <!-- status of document, group responsible (required) -->
                    144: 
                    145:   <p>The W3C <a href="http://www.w3.org/html/wg/">HTML Working Group</a> is
                    146:    the W3C working group responsible for this specification's progress along
1.9       ihickson  147:    the W3C Recommendation track. <!--ZZZ:--> This specification is the 18
1.3       ihickson  148:    July 2008 <!--ZZZ "Working Draft"-->Editor's Draft. <!--:ZZZ--></p>
1.1       ihickson  149:   <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    150:   <!-- relationship to other work (required) -->
                    151: 
                    152:   <p>This specification is also being produced by the <a
                    153:    href="http://www.whatwg.org/">WHATWG</a>. The two specifications are
                    154:    identical from the table of contents onwards.</p>
                    155:   <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    156:   <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    157:   <!-- context and rationale (required) -->
                    158: 
                    159:   <p>This specification is intended to specify a part of the Web platform
                    160:    closely related to HTML5. It is defined in a separate document primarily
                    161:    to ease the cognitive load on reviewers.</p>
                    162:   <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
                    163:   <!-- required patent boilerplate -->
                    164: 
                    165:   <p>This document was produced by a group operating under the <a
                    166:    href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February
                    167:    2004 W3C Patent Policy</a>. W3C maintains a <a
                    168:    href="http://www.w3.org/2004/01/pp-impl/40318/status"
                    169:    rel=disclosure>public list of any patent disclosures</a> made in
                    170:    connection with the deliverables of the group; that page also includes
                    171:    instructions for disclosing a patent. An individual who has actual
                    172:    knowledge of a patent which the individual believes contains <a
                    173:    href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
                    174:    Claim(s)</a> must disclose the information in accordance with <a
                    175:    href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
                    176:    6 of the W3C Patent Policy</a>.
                    177: 
                    178:   <h2 class="no-num no-toc" id=contents>Table of contents</h2>
                    179:   <!--begin-toc-->
                    180: 
                    181:   <ul class=toc>
                    182:    <li><a href="#introduction"><span class=secno>1. </span>Introduction</a>
                    183:     <ul class=toc>
1.4       ihickson  184:      <li><a href="#tutorial"><span class=secno>1.1 </span>Tutorial</a>
1.1       ihickson  185: 
1.3       ihickson  186:      <li><a href="#requirements"><span class=secno>1.2
                    187:       </span>Requirements</a>
1.1       ihickson  188: 
1.4       ihickson  189:      <li><a href="#conformance"><span class=secno>1.3 </span>Conformance
1.1       ihickson  190:       requirements</a>
                    191:       <ul class=toc>
1.4       ihickson  192:        <li><a href="#dependencies"><span class=secno>1.3.1
1.1       ihickson  193:         </span>Dependencies</a>
                    194:       </ul>
                    195: 
1.4       ihickson  196:      <li><a href="#terminology"><span class=secno>1.4 </span>Terminology</a>
                    197:     </ul>
                    198: 
1.12      ihickson  199:    <li><a href="#infrastructure"><span class=secno>2.
                    200:     </span>Infrastructure</a>
1.4       ihickson  201:     <ul class=toc>
                    202:      <li><a href="#the-windowworker"><span class=secno>2.1 </span>The
                    203:       <code>WindowWorker</code> interface</a>
                    204: 
1.15      ihickson  205:      <li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
                    206:       origins of workers</a>
                    207: 
                    208:      <li><a href="#the-queue"><span class=secno>2.3 </span>The queue of
1.4       ihickson  209:       events</a>
                    210: 
1.15      ihickson  211:      <li><a href="#the-workers"><span class=secno>2.4 </span>The worker's
1.9       ihickson  212:       ports</a>
                    213: 
1.15      ihickson  214:      <li><a href="#processing"><span class=secno>2.5 </span>Processing
1.4       ihickson  215:       model</a>
1.12      ihickson  216: 
1.15      ihickson  217:      <li><a href="#creating"><span class=secno>2.6 </span>Creating
1.12      ihickson  218:       workers</a>
1.1       ihickson  219:     </ul>
                    220: 
1.12      ihickson  221:    <li><a href="#apis-available"><span class=secno>3. </span>APIs available
                    222:     to workers</a>
1.4       ihickson  223: 
1.1       ihickson  224:    <li class=no-num><a href="#references">References</a>
                    225: 
                    226:    <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
                    227:   </ul>
                    228:   <!--end-toc-->
                    229: 
                    230:   <hr>
                    231: 
                    232:   <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
                    233: 
1.4       ihickson  234:   <h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3>
1.1       ihickson  235: 
                    236:   <p><em>This section is non-normative.</em>
                    237: 
1.4       ihickson  238:   <p class=big-issue>This section is missing.
                    239: 
1.3       ihickson  240:   <h3 id=requirements><span class=secno>1.2 </span>Requirements</h3>
1.1       ihickson  241: 
                    242:   <p><em>This section is non-normative.</em>
                    243: 
1.3       ihickson  244:   <p>This specification aims to address the following use cases and
                    245:    requirements:
                    246: 
                    247:   <ul>
                    248:    <li>Background workers: A Web application needs to keep its data
                    249:     synchronised with the server, both sending updates to the server and
                    250:     receiving updates from the server, including handling buffering of
                    251:     updates for when the application goes offline. The code to do this would
                    252:     ideally be independent of the UI code.
                    253: 
                    254:    <li>URLs: Workers should be spawned from URLs, not from strings, since
                    255:     script rarely has access to its own source.
                    256: 
                    257:    <li>Message queuing: Messages sent to a worker before the worker has
                    258:     initialised should not be lost.
                    259: 
                    260:    <li>Workers should have access to timers.
                    261: 
                    262:    <li>Workers should have access to the network.
                    263: 
                    264:    <li>Workers should be able to use libraries.
                    265: 
                    266:    <li>Implementations should not have to expose <code>Node</code> or
                    267:     <code>Document</code> objects to workers.
                    268: 
                    269:    <li>Workers should not share anything with the outside world. The objects
                    270:     representing the worker in the worker itself and in the context that
                    271:     created the worker should be different, for instance.
                    272: 
                    273:    <li>Shared workers: Multiple instances of the same Web application would
                    274:     want to keep just one connection back to the server.
                    275: 
                    276:    <li>Capabilities granting: It should be possible for code running in one
                    277:     iframe to negotiate a connection to another iframe, with that connection
                    278:     granting certain rights (e.g. adding to an address book but not reading
                    279:     from it).
                    280: 
                    281:    <li>Delegation: It should be possible for one worker to spawn another
                    282:     worker and efficiently delagate a request to that worker, without the
                    283:     caller being aware of the delagate and without the original worker having
                    284:     to proxy all the messages.
                    285: 
                    286:    <li>Workers whose parents are not longer useful should be killed. Workers
                    287:     should be able to detect this is about to happen and exit gracefully.
                    288:   </ul>
                    289: 
1.4       ihickson  290:   <h3 id=conformance><span class=secno>1.3 </span>Conformance requirements</h3>
1.1       ihickson  291: 
                    292:   <p>All diagrams, examples, and notes in this specification are
                    293:    non-normative, as are all sections explicitly marked non-normative.
                    294:    Everything else in this specification is normative.
                    295: 
                    296:   <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
                    297:   NOT",-->
                    298:    "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
                    299:    normative parts of this document are to be interpreted as described in
                    300:    RFC2119. For readability, these words do not appear in all uppercase
                    301:    letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
                    302:   <!-- XXX but they should be
                    303:   marked up -->
                    304: 
                    305:   <p>Requirements phrased in the imperative as part of algorithms (such as
                    306:    "strip any leading space characters" or "return false and abort these
                    307:    steps") are to be interpreted with the meaning of the key word ("must",
                    308:    "should", "may", etc) used in introducing the algorithm.
                    309: 
                    310:   <p>Some conformance requirements are phrased as requirements on attributes,
                    311:    methods or objects. Such requirements are to be interpreted as
                    312:    requirements on user agents.
                    313: 
                    314:   <p>Conformance requirements phrased as algorithms or specific steps may be
                    315:    implemented in any manner, so long as the end result is equivalent. (In
                    316:    particular, the algorithms defined in this specification are intended to
                    317:    be easy to follow, and not intended to be performant.)
                    318: 
                    319:   <p>The only conformance class defined by this specification is user agents.
                    320: 
                    321:   <p>User agents may impose implementation-specific limits on otherwise
                    322:    unconstrained inputs, e.g. to prevent denial of service attacks, to guard
                    323:    against running out of memory, or to work around platform-specific
                    324:    limitations.
                    325: 
1.4       ihickson  326:   <h4 id=dependencies><span class=secno>1.3.1 </span>Dependencies</h4>
1.1       ihickson  327: 
                    328:   <p>This specification relies on several other underlying specifications.
                    329: 
                    330:   <dl>
                    331:    <dt>HTML5
                    332: 
                    333:    <dd>
                    334:     <p>Many fundamental concepts from HTML5 are used by this specification.
                    335:      <a href="#refsHTML5">[HTML5]</a></p>
                    336: 
                    337:    <dt>ECMAScript
                    338: 
                    339:    <dd>
                    340:     <p>This specification is intended to be used with JavaScript as the
                    341:      scripting language. <a href="#refsJS">[JS]</a></p>
                    342: 
                    343:    <dt>WebIDL
                    344: 
                    345:    <dd>
                    346:     <p>The IDL blocks in this specification use the semantics of the WebIDL
                    347:      specification. <a href="#refsWebIDL">[WebIDL]</a></p>
                    348:   </dl>
                    349: 
1.4       ihickson  350:   <h3 id=terminology><span class=secno>1.4 </span>Terminology</h3>
1.1       ihickson  351: 
                    352:   <p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
                    353:    <em>visible</em> might sometimes be used when referring to the way a
                    354:    document is rendered to the user. These terms are not meant to imply a
                    355:    visual medium; they must be considered to apply to other media in
                    356:    equivalent ways.
                    357: 
1.4       ihickson  358:   <p>The construction "a <code title="">Foo</code> object", where <code
                    359:    title="">Foo</code> is actually an interface, is sometimes used instead of
                    360:    the more accurate "an object implementing the interface <code
                    361:    title="">Foo</code>".
1.1       ihickson  362: 
                    363:   <p>The term DOM is used to refer to the API set made available to scripts
                    364:    in Web applications, and does not necessarily imply the existence of an
                    365:    actual <code>Document</code> object or of any other <code>Node</code>
                    366:    objects as defined in the DOM Core specifications. <a
                    367:    href="#refsDOM3CORE">[DOM3CORE]</a>
                    368: 
                    369:   <p>A DOM attribute is said to be <em>getting</em> when its value is being
                    370:    retrieved (e.g. by author script), and is said to be <em>setting</em> when
                    371:    a new value is assigned to it.
                    372: 
                    373:   <p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means
                    374:    that any attributes returning that object must always return the same
                    375:    object (not a new object each time), and the attributes and methods on
                    376:    that object must operate on the actual underlying data, not a snapshot of
                    377:    the data.
                    378: 
1.12      ihickson  379:   <h2 id=infrastructure><span class=secno>2. </span>Infrastructure</h2>
1.4       ihickson  380: 
                    381:   <h3 id=the-windowworker><span class=secno>2.1 </span>The <code><a
                    382:    href="#windowworker">WindowWorker</a></code> interface</h3>
                    383: 
                    384:   <pre
                    385:    class=idl>[NoInterfaceObject] interface <dfn id=windowworker>WindowWorker</dfn> {
1.10      ihickson  386:   readonly attribute DOMString <a href="#url" title=dom-WindowWorker-URL>URL</a>;
                    387:   readonly attribute DOMString <a href="#name" title=dom-WindowWorker-name>name</a>;
                    388:   readonly attribute boolean <a href="#closing" title=dom-WindowWorker-closing>closing</a>;
1.14      ihickson  389:   boolean <a href="#import" title=dom-WindowWorker-import>import</a>(in DOMString url);
1.10      ihickson  390:   void <a href="#close" title=dom-WindowWorker-close>close</a>();
1.9       ihickson  391: 
                    392:   // event handler attributes
1.10      ihickson  393:            attribute <span>EventListener</span> <a href="#onattach" title=handler-WindowWorker-onattach>onattach</a>;
                    394:            attribute <span>EventListener</span> <a href="#onunload" title=handler-WindowWorker-onunload>onunload</a>;
1.4       ihickson  395: };</pre>
                    396: 
1.10      ihickson  397:   <p>The <dfn id=url title=dom-WindowWorker-URL><code>URL</code></dfn>
1.9       ihickson  398:    attribute must return the value it was assigned when the <code><a
                    399:    href="#windowworker">WindowWorker</a></code> object was created by the "<a
                    400:    href="#run-a">run a worker</a>" algorithm. It gives the <span>absolute
                    401:    URL</span> of the script that was used to initialize the worker.
                    402: 
1.10      ihickson  403:   <p>The <dfn id=name title=dom-WindowWorker-name><code>name</code></dfn>
1.6       ihickson  404:    attribute must return the value it was assigned when the <code><a
                    405:    href="#windowworker">WindowWorker</a></code> object was created by the "<a
1.9       ihickson  406:    href="#run-a">run a worker</a>" algorithm. If it has a value that isn't
                    407:    the empty string, its value represents the name that can be used to obtain
                    408:    a reference to the worker using the <code
1.11      ihickson  409:    title=dom-WindowWorkerCreators-createNamedWorker><a
                    410:    href="#createnamedworker">createNamedWorker()</a></code> method.
1.6       ihickson  411: 
1.4       ihickson  412:   <p>The <dfn id=closing
1.10      ihickson  413:    title=dom-WindowWorker-closing><code>closing</code></dfn> attribute must
1.19    ! ihickson  414:    return false until it is set to true by one of the algorithms in the
        !           415:    processing model section below.
1.4       ihickson  416: 
1.9       ihickson  417:   <p>The following are the <span>event handler DOM attributes</span> that
                    418:    must be supported by objects implementing the <code><a
                    419:    href="#windowworker">WindowWorker</a></code> interface:
                    420: 
                    421:   <dl>
                    422:    <dt><dfn id=onattach
1.10      ihickson  423:     title=handler-WindowWorker-onattach><code>onattach</code></dfn>
1.9       ihickson  424: 
                    425:    <dd>
                    426:     <p>Must be invoked whenever a <code
1.10      ihickson  427:      title=event-WindowWorker-attach>attach</code> event is targeted at or
1.9       ihickson  428:      bubbles through the <code><a
                    429:      href="#windowworker">WindowWorker</a></code> object.
                    430: 
                    431:    <dt><dfn id=onunload
1.10      ihickson  432:     title=handler-WindowWorker-onunload><code>onunload</code></dfn>
1.9       ihickson  433: 
                    434:    <dd>
                    435:     <p>Must be invoked whenever a <code title=event-unload>unload</code>
                    436:      event is targeted at or bubbles through the <code><a
                    437:      href="#windowworker">WindowWorker</a></code> object.
                    438:   </dl>
                    439: 
1.15      ihickson  440:   <h3 id=base-urls><span class=secno>2.2 </span>Base URLs and origins of
                    441:    workers</h3>
                    442: 
                    443:   <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
1.16      ihickson  444:    worker is the <span>absolute URL</span> given by the value of the <code
                    445:    title=dom-WindowWorker-URL><a href="#url">URL</a></code> attribute of the
                    446:    worker's <code><a href="#windowworker">WindowWorker</a></code> object.
1.15      ihickson  447: 
                    448:   <p>The <span>origin</span> and <span>effective script origin</span> of
                    449:    scripts running in workers are both the <span>origin</span> of the
1.16      ihickson  450:    <span>absolute URL</span> given by the value of the <code
                    451:    title=dom-WindowWorker-URL><a href="#url">URL</a></code> attribute of the
                    452:    worker's <code><a href="#windowworker">WindowWorker</a></code> object.
1.15      ihickson  453: 
                    454:   <h3 id=the-queue><span class=secno>2.3 </span>The queue of events</h3>
1.4       ihickson  455: 
                    456:   <p>Each <code><a href="#windowworker">WindowWorker</a></code> object is
                    457:    asssociated with a <dfn id=queue>queue of events</dfn>, which is initially
                    458:    empty.
                    459: 
                    460:   <p>An event in the queue can be a DOM event or a timeout callback.
                    461: 
1.8       ihickson  462:   <p>All asynchronous callbacks and events that would be called or dispatched
                    463:    in the worker must be added to the worker's queue, with the "<a
                    464:    href="#run-a">run a worker</a>" processing model below taking care of
                    465:    actually calling the callbacks or dispatching the events.
                    466: 
                    467:   <p>Once the <code><a href="#windowworker">WindowWorker</a></code>'s <code
1.10      ihickson  468:    title=dom-WindowWorker-closing><a href="#closing">closing</a></code>
1.8       ihickson  469:    attribute is set to true, the queue must discard anything else that would
                    470:    be added to it. Effectively, once the <code
1.10      ihickson  471:    title=dom-WindowWorker-closing><a href="#closing">closing</a></code>
1.8       ihickson  472:    attribute is true, timers stop firing, notifications for all pending
                    473:    asynchronous operations are dropped, etc.
                    474: 
1.15      ihickson  475:   <h3 id=the-workers><span class=secno>2.4 </span>The worker's ports</h3>
1.9       ihickson  476: 
                    477:   <p>Workers communicate with other workers and with <span title="browsing
                    478:    context">browsing contexts</span> through <span title="channel
                    479:    messaging">message channels</span> and their <code>MessagePort</code>
                    480:    objects.
                    481: 
                    482:   <p>Each <code><a href="#windowworker">WindowWorker</a></code> <var
                    483:    title="">worker</var> has a list of <dfn id=the-workers0>the worker's
                    484:    ports</dfn>, which consists of all the <code>MessagePort</code> objects
                    485:    that are entangled with another port and that have one (but only one) port
                    486:    whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is <var
                    487:    title="">worker</var>. This list includes all the <code>MessagePort</code>
                    488:    objects that are in events pending in the <a href="#queue">queue of
                    489:    events</a>.
                    490: 
1.17      ihickson  491:   <hr>
                    492: 
                    493:   <p>A worker is said to be a <dfn id=front-line>front-line worker</dfn> if
                    494:    at least one of the <a href="#the-workers0">the worker's ports</a> has an
                    495:    entangled <code>MessagePort</code> whose <code
                    496:    title=dom-MessagePort-ownerWindow>ownerWindow</code> is a
                    497:    <code>WindowBrowsingContext</code> object.
                    498: 
                    499:   <p>A worker is said to be a <dfn id=needed>needed worker</dfn> if either:
                    500: 
                    501:   <ul>
                    502:    <li>it is a <a href="#front-line">front-line worker</a>, or
                    503: 
                    504:    <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
                    505:     an entangled <code>MessagePort</code> whose <code
                    506:     title=dom-MessagePort-ownerWindow>ownerWindow</code> is a <code><a
                    507:     href="#windowworker">WindowWorker</a></code> object that is itself a <a
                    508:     href="#needed">needed worker</a>.
                    509:   </ul>
                    510: 
                    511:   <hr>
                    512: 
                    513:   <p>A worker is said to be an <dfn id=active>active front-line worker</dfn>
                    514:    if at least one of the <a href="#the-workers0">the worker's ports</a> has
                    515:    both an <code title=dom-MessagePort-active>active</code> attribute whose
                    516:    value is true and an entangled <code>MessagePort</code> whose <code
                    517:    title=dom-MessagePort-ownerWindow>ownerWindow</code> is a
                    518:    <code>WindowBrowsingContext</code> object.
                    519: 
                    520:   <p>A worker is said to be an <dfn id=active0>active needed worker</dfn> if
                    521:    either:
                    522: 
                    523:   <ul>
                    524:    <li>it is an <a href="#active">active front-line worker</a>, or
                    525: 
                    526:    <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
                    527:     an entangled <code>MessagePort</code> whose <code
                    528:     title=dom-MessagePort-ownerWindow>ownerWindow</code> is a <code><a
                    529:     href="#windowworker">WindowWorker</a></code> object that is itself an <a
                    530:     href="#active0">active needed worker</a>.
                    531:   </ul>
                    532: 
1.15      ihickson  533:   <h3 id=processing><span class=secno>2.5 </span>Processing model</h3>
1.4       ihickson  534: 
1.6       ihickson  535:   <p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var
1.7       ihickson  536:    title="">name</var> for a script with <span>URL</span> <var
                    537:    title="">url</var>, a browsing context <var title="">owner browsing
                    538:    context</var> and a <code>Document</code> <var title="">owner
                    539:    document</var>, it must run the following steps in a completely separate
                    540:    and parallel execution environment:
1.4       ihickson  541: 
                    542:   <ol>
                    543:    <li>
1.7       ihickson  544:     <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
                    545:      <var title="">url</var>.</p>
                    546: 
                    547:     <p>If the attempt fails, then abort these steps and invoke the <a
1.9       ihickson  548:      href="#worker" title="worker creation failed">error handling steps</a>
1.7       ihickson  549:      defined by the algorithm that called this one.</p>
                    550: 
                    551:     <p>If the attempt succeeds, then let <var title="">script</var> be the
                    552:      resource that was obtained.</p>
                    553: 
                    554:     <p class=note>As with <code>script</code> elements, the MIME type of the
                    555:      script is ignored. Unlike with <code>script</code> elements, there is no
                    556:      way to override the type. It's always assumed to be JavaScript.</p>
                    557:     <!-- XXX people will complain about
                    558:     this. I guess we might want to examine the MIME type... -->
                    559:     
                    560: 
                    561:    <li>
1.4       ihickson  562:     <p>Create a new <code><a href="#windowworker">WindowWorker</a></code>
1.19    ! ihickson  563:      object, <var title="">worker</var>.</p>
1.4       ihickson  564: 
                    565:    <li>
1.10      ihickson  566:     <p>Set the <code title=dom-WindowWorker-URL><a href="#url">URL</a></code>
1.19    ! ihickson  567:      attribute of <var title="">worker</var> to the value of <var
1.9       ihickson  568:      title="">url</var>.</p>
                    569: 
                    570:    <li>
1.10      ihickson  571:     <p>Set the <code title=dom-WindowWorker-name><a
1.19    ! ihickson  572:      href="#name">name</a></code> attribute of <var title="">worker</var> to
1.6       ihickson  573:      the value of <var title="">name</var>.</p>
                    574: 
                    575:    <li>
1.4       ihickson  576:     <p>Let <var title="">script</var>'s <span>script execution context</span>
                    577:      (and thus also <span>global object</span>) be <var
1.19    ! ihickson  578:      title="">worker</var>.</p>
1.4       ihickson  579: 
                    580:    <li>
                    581:     <p>Let <var title="">script</var>'s <span>script browsing context</span>
                    582:      be <var title="">owner browsing context</var>.</p>
                    583: 
                    584:    <li>
                    585:     <p>Let <var title="">script</var>'s <span>script document context</span>
                    586:      be <var title="">owner document</var>.</p>
                    587: 
                    588:    <li>
1.9       ihickson  589:     <p>Invoke the <a href="#success" title="worker creation
                    590:      succeeded">success steps</a> defined by the algorithm that called this
1.17      ihickson  591:      one.</p>
                    592: 
                    593:     <p class=note>This will usually add an event to the <a
                    594:      href="#queue">queue of events</a>. If it does, that event will have a
                    595:      <code>MessagePort</code> and thus the list of <a
                    596:      href="#the-workers0">the worker's ports</a> will not be empty. If it
1.19    ! ihickson  597:      doesn't, then the next step will set the <var title="">worker</var>
1.17      ihickson  598:      object's <code title=dom-WindowWorker-closing><a
                    599:      href="#closing">closing</a></code> attribute to true.</p>
                    600: 
                    601:    <li>
                    602:     <p>Start monitoring <var title="">worker</var>, such that as soon as the
                    603:      worker stops being a <a href="#needed">needed worker</a>, the <var
1.19    ! ihickson  604:      title="">worker</var> object's <code title=dom-WindowWorker-closing><a
1.17      ihickson  605:      href="#closing">closing</a></code> attribute is set to true.</p>
1.9       ihickson  606: 
                    607:    <li>
                    608:     <p>Start monitoring <var title="">worker</var>, such that whenever the
1.19    ! ihickson  609:      <var title="">worker</var> object's <code
1.10      ihickson  610:      title=dom-WindowWorker-closing><a href="#closing">closing</a></code>
1.17      ihickson  611:      attribute is false and the worker is not an <a href="#active0">active
                    612:      needed worker</a>, the user agent suspends execution of script in that
                    613:      worker until such time as either the <code
1.10      ihickson  614:      title=dom-WindowWorker-closing><a href="#closing">closing</a></code>
1.17      ihickson  615:      attribute switches to true or the worker becomes an <a
                    616:      href="#active0">active needed worker</a>.</p>
1.7       ihickson  617: 
                    618:    <li>
                    619:     <p>Run <var title="">script</var> until it either returns, fails to catch
                    620:      an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
                    621:      a worker</a>" algorithm below.</p>
                    622: 
                    623:     <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
                    624:      worker</a>" algorithm, then that same algorithm will cause there to only
                    625:      be a single event in the <a href="#queue">queue of events</a> at the
1.9       ihickson  626:      next step, namely the <code title=message-unload>unload</code> event.
                    627:      However, if the event is ignored then it will become true as soon as
                    628:      that port is garbage collected.</p>
1.4       ihickson  629: 
                    630:    <li>
1.9       ihickson  631:     <p><i>Event loop</i>: Wait until either there is an event in the <a
1.4       ihickson  632:      href="#queue">queue of events</a> associated with <var
1.19    ! ihickson  633:      title="">worker</var> or the <var title="">worker</var> object's <code
1.10      ihickson  634:      title=dom-WindowWorker-closing><a href="#closing">closing</a></code>
1.9       ihickson  635:      attribute is set to true.</p>
1.4       ihickson  636: 
                    637:    <li>
                    638:     <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1.9       ihickson  639:      events</a>, if any. The handling of this event or the execution of this
                    640:      callback might get prematurely aborted by the "<a href="#kill-a">kill a
1.8       ihickson  641:      worker</a>" algorithm below.</p>
1.4       ihickson  642: 
                    643:    <li>
                    644:     <p>If there are any more events in the <a href="#queue">queue of
1.19    ! ihickson  645:      events</a> or if the <var title="">worker</var> object's <code
1.10      ihickson  646:      title=dom-WindowWorker-closing><a href="#closing">closing</a></code>
1.4       ihickson  647:      attribute is set to false, then jump back to the step above labeled
                    648:      <i>event loop</i>.</p>
                    649: 
                    650:    <li>
                    651:     <p class=big-issue>timers, intervals, XMLHttpRequests, database
                    652:      transactions, etc, must be killed; ports must be deactivated and
1.8       ihickson  653:      unentangled (do not send unload)</p>
1.4       ihickson  654:   </ol>
                    655: 
                    656:   <hr>
                    657: 
                    658:   <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run
                    659:    the following steps in parallel with the worker's main loop (the "<a
                    660:    href="#run-a">run a worker</a>" processing model defined above):
                    661: 
                    662:   <ol>
                    663:    <li>
                    664:     <p>Create an <code>Event</code> object with the event name <code
                    665:      title=event-unload>unload</code>, which does not bubble and is not
                    666:      cancelable, and add it to the worker's <code><a
                    667:      href="#windowworker">WindowWorker</a></code> object's <a
1.8       ihickson  668:      href="#queue">queue of events</a>, targetted at the <code><a
                    669:      href="#windowworker">WindowWorker</a></code> object itself.
1.4       ihickson  670: 
                    671:    <li>
1.8       ihickson  672:     <p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code>
1.10      ihickson  673:      object's <code title=dom-WindowWorker-closing><a
1.8       ihickson  674:      href="#closing">closing</a></code> attribute to true.
                    675: 
                    676:    <li>
                    677:     <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
                    678:      a worker</a>" processing model defined above immediately starts running
                    679:      event listeners registered for <code title=event-unload>unload</code>
                    680:      event, this time should not be zero &mdash; the idea is that the <code
                    681:      title=event-unload>unload</code> event can be used to clean up when
                    682:      shutting down unexpectedly.
1.4       ihickson  683: 
                    684:    <li>
                    685:     <p>If there are any events in the <a href="#queue">queue of events</a>
                    686:      other than the <code title=event-unload>unload</code> event that this
                    687:      algorithm just added, discard them without dispatching them.
                    688: 
                    689:    <li>
                    690:     <p>If the <code title=event-unload>unload</code> event that this
                    691:      algorithm just added hasn't yet been dispatched, then abort the script
                    692:      currently running in the worker.
                    693: 
                    694:    <li>
                    695:     <p>Wait a user-agent-defined amount of time.
                    696: 
                    697:    <li>
                    698:     <p>Abort the script currently running in the worker (if any script is
                    699:      running, then it will be a handler for the <code
                    700:      title=event-unload>unload</code> event).
                    701:   </ol>
                    702: 
1.19    ! ihickson  703:   <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
        !           704:    processing model on a worker at any time, e.g. in response to user
        !           705:    requests, in response to CPU quota management, or when a worker stops
        !           706:    being a <a href="#needed">needed worker</a> if the worker continues
        !           707:    executing even after its <code title=dom-WindowWorker-closing><a
        !           708:    href="#closing">closing</a></code> attribute was set to true.
        !           709: 
1.8       ihickson  710:   <hr>
                    711: 
1.14      ihickson  712:   <p>When a script invokes the <dfn id=import
                    713:    title=dom-WindowWorker-import><code>import(<var
                    714:    title="">url</var>)</code></dfn> method on a <code><a
                    715:    href="#windowworker">WindowWorker</a></code> object, the user agent must
                    716:    run the following steps:
                    717: 
                    718:   <ol>
                    719:    <li>
                    720:     <p><span title="resolve a url">Resolve</span> the <var title="">url</var>
                    721:      argument.
                    722: 
                    723:    <li>
                    724:     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
                    725: 
                    726:    <li>
                    727:     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
                    728:      is not the <span title="same origin">same</span> as the origin of the
                    729:      script that invoked the method, then throw a <span>security
                    730:      exception</span>.
                    731: 
                    732:    <li>
                    733:     <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
                    734:      the resulting <span>absolute URL</span>.</p>
                    735: 
                    736:     <p>If the attempt fails, return false and abort these steps.</p>
                    737: 
                    738:     <p>If the attempt succeeds, then let <var title="">script</var> be the
                    739:      resource that was obtained.</p>
                    740: 
                    741:     <p class=note>As with the initial script, the script here is always
                    742:      assumed to be JavaScript, regardless of the MIME type.</p>
                    743:     <!-- XXX -->
                    744: 
                    745:    <li>
                    746:     <p>Let <var title="">script</var>'s <span>script execution
                    747:      context</span>, <span>script browsing context</span>, and <span>script
                    748:      document context</span> be the same as for the script that was executed
                    749:      by the <a href="#run-a">run a worker</a> processing model for this
                    750:      worker.</p>
                    751: 
                    752:    <li>
                    753:     <p>Run <var title="">script</var> until it either returns, fails to catch
                    754:      an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
                    755:      a worker</a>" algorithm below.</p>
                    756: 
                    757:     <p>If an exception was raised or if the script was prematurely aborted,
                    758:      then abort these steps, letting the exception or aborting continue to be
                    759:      processed by the script that called the <code
                    760:      title=dom-WindowWorker-import><a href="#import">import()</a></code>
                    761:      method.</p>
                    762: 
                    763:     <p>Otherwise, return true.</p>
                    764:   </ol>
                    765: 
                    766:   <hr>
                    767: 
1.8       ihickson  768:   <p>When a script invokes the <dfn id=close
1.10      ihickson  769:    title=dom-WindowWorker-close><code>close()</code></dfn> method on a
1.8       ihickson  770:    <code><a href="#windowworker">WindowWorker</a></code> object, the user
                    771:    agent must run the following steps:
                    772: 
                    773:   <ol>
                    774:    <li>
                    775:     <p>Create an <code>Event</code> object with the event name <code
                    776:      title=event-unload>unload</code>, which does not bubble and is not
                    777:      cancelable, and add it to the <code><a
                    778:      href="#windowworker">WindowWorker</a></code> object's <a
                    779:      href="#queue">queue of events</a>, targetted at the <code><a
                    780:      href="#windowworker">WindowWorker</a></code> object itself.
                    781: 
                    782:    <li>
                    783:     <p>Set the worker's <code><a href="#windowworker">WindowWorker</a></code>
1.10      ihickson  784:      object's <code title=dom-WindowWorker-closing><a
1.8       ihickson  785:      href="#closing">closing</a></code> attribute to true.
                    786: 
                    787:    <li>
                    788:     <p>For each <code>MessagePort</code> object that is entangled with
                    789:      another port and that has one (but only one) port whose <code
1.9       ihickson  790:      title=dom-MessagePort-ownerWindow>ownerWindow</code> is the <code><a
1.8       ihickson  791:      href="#windowworker">WindowWorker</a></code> object on which the method
                    792:      was invoked, run the following substeps:</p>
                    793: 
                    794:     <ol>
                    795:      <li>
                    796:       <p>Unentangle the two ports.
                    797: 
                    798:      <li>
1.9       ihickson  799:       <p>Set both ports' <code title=dom-MessagePort-active>active</code>
1.8       ihickson  800:        attribute to false.
                    801: 
                    802:      <li>
                    803:       <p>At the next available opportunity, after any scripts have finished
                    804:        executing<!-- XXX queue -->, <span>fire a simple event</span> called
                    805:        <code title=event-unload>unload</code> at the other port (the one
1.9       ihickson  806:        whose <code title=dom-MessagePort-ownerWindow>ownerWindow</code> is
1.8       ihickson  807:        not the <code><a href="#windowworker">WindowWorker</a></code> object
1.10      ihickson  808:        on which the <code title=dom-WindowWorker-close><a
1.8       ihickson  809:        href="#close">close()</a></code> method was called).
                    810:     </ol>
                    811:   </ol>
                    812: 
1.15      ihickson  813:   <h3 id=creating><span class=secno>2.6 </span>Creating workers</h3>
1.5       ihickson  814: 
                    815:   <pre
                    816:    class=idl>[NoInterfaceObject] interface <dfn id=windowworkercreators>WindowWorkerCreators</dfn> {
1.11      ihickson  817:   <span>MessagePort</span> <a href="#createworker" title=dom-WindowWorkerCreators-createWorker>createWorker</a>(in DOMString scriptURL);
                    818:   <span>MessagePort</span> <a href="#createnamedworker" title=dom-WindowWorkerCreators-createNamedWorker>createNamedWorker</a>(in DOMString name, in DOMString scriptURL);
1.5       ihickson  819: };</pre>
                    820: 
                    821:   <p>Objects that implement the <code>Window</code> interface must also
                    822:    implement the <code><a
                    823:    href="#windowworkercreators">WindowWorkerCreators</a></code> interface.
                    824: 
1.9       ihickson  825:   <hr>
                    826: 
1.11      ihickson  827:   <p>When the <dfn id=createworker
                    828:    title=dom-WindowWorkerCreators-createWorker><code>createWorker(<var
                    829:    title="">scriptURL</var>)</code></dfn> method is invoked, the user agent
                    830:    must run the following steps:
1.9       ihickson  831: 
                    832:   <ol>
                    833:    <li>
                    834:     <p><span title="resolve a url">Resolve</span> the <var
                    835:      title="">scriptURL</var> argument.
                    836: 
                    837:    <li>
                    838:     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
                    839: 
                    840:    <li>
                    841:     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
                    842:      is not the <span title="same origin">same</span> as the origin of the
                    843:      script that invoked the method, then throw a <span>security
                    844:      exception</span>.
                    845: 
                    846:    <li>
                    847:     <p><a href="#create">Create a worker</a> from the resulting
                    848:      <span>absolute URL</span> whose name is the empty string.
                    849: 
                    850:    <li>
                    851:     <p>Return the <code>MessagePort</code> object returned from the <a
                    852:      href="#create">create a worker</a> algorithm.
                    853:   </ol>
                    854: 
                    855:   <hr>
                    856: 
1.11      ihickson  857:   <p>When the <dfn id=createnamedworker
                    858:    title=dom-WindowWorkerCreators-createNamedWorker><code>createNamedWorker(<var
                    859:    title="">name</var>, <var title="">scriptURL</var>)</code></dfn> method is
1.9       ihickson  860:    invoked, the user agent must run the following steps:
                    861: 
                    862:   <ol>
                    863:    <li>
                    864:     <p><span title="resolve a url">Resolve</span> the <var
                    865:      title="">scriptURL</var> argument.
1.4       ihickson  866: 
1.9       ihickson  867:    <li>
                    868:     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
                    869: 
                    870:    <li>
                    871:     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
                    872:      is not the <span title="same origin">same</span> as the origin of the
                    873:      script that invoked the method, then throw a <span>security
                    874:      exception</span>.
                    875: 
                    876:    <li>
                    877:     <p>If the <var title="">name</var> argument is the empty string, <a
                    878:      href="#create">create a worker</a> from the resulting <span>absolute
                    879:      URL</span>, whose name is the empty string, and return the
                    880:      <code>MessagePort</code> object returned from the <a
                    881:      href="#create">create a worker</a> algorithm. Then, abort these steps.
                    882: 
                    883:    <li>
1.10      ihickson  884:     <p>If there exists a worker whose <code title=dom-WindowWorker-closing><a
1.9       ihickson  885:      href="#closing">closing</a></code> attribute is false, whose <code
1.10      ihickson  886:      title=dom-WindowWorker-name><a href="#name">name</a></code> attribute is
1.9       ihickson  887:      exactly equal to the <var title="">name</var> argument, and whose <code
1.10      ihickson  888:      title=dom-WindowWorker-URL><a href="#url">URL</a></code> attribute has
1.9       ihickson  889:      the <span>same origin</span> as the resulting <span>absolute URL</span>,
                    890:      then run these substeps:</p>
                    891: 
                    892:     <ol>
                    893:      <li>
1.10      ihickson  894:       <p>If that worker's <code title=dom-WindowWorker-URL><a
1.9       ihickson  895:        href="#url">URL</a></code> attribute is not exactly equal to the
                    896:        resulting <span>absolute URL</span>, then throw a
                    897:        <code>URL_MISMATCH_ERR</code> exception and abort these steps. <span
                    898:        class=big-issue>code 19</span>
                    899: 
                    900:      <li>
                    901:       <p><span>Create a new <code>MessagePort</code> object</span> owned by
                    902:        the <span>script execution context</span> of the script that invoked
                    903:        the method.
                    904: 
                    905:      <li>
                    906:       <p>Return that port.
                    907: 
                    908:      <li>
                    909:       <p>Asynchronously, <a href="#attach" title="attach to a
                    910:        worker">attach</a> to this preexisting worker, with the newly created
                    911:        port.
                    912:     </ol>
                    913: 
                    914:     <p>Otherwise, <a href="#create">create a worker</a> from the resulting
                    915:      <span>absolute URL</span>, whose name is the value of the <var
                    916:      title="">name</var> argument, and return the <code>MessagePort</code>
                    917:      object returned from the <a href="#create">create a worker</a>
                    918:      algorithm.</p>
                    919:   </ol>
                    920: 
                    921:   <hr>
                    922: 
                    923:   <p>The steps to <dfn id=create>create a worker</dfn> from a
                    924:    <span>URL</span> <var title="">url</var> and whose name is <var
                    925:    title="">name</var>, in the context of a method call, are as follows:
                    926: 
                    927:   <ol>
                    928:    <li>
                    929:     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
                    930:      <span>script execution context</span> of the script that invoked the
                    931:      method.
                    932: 
                    933:    <li>
                    934:     <p>Return that port.
                    935: 
                    936:    <li>
                    937:     <p>In a parallel execution context (i.e. a separate thread or process or
                    938:      equivalent construct), <a href="#run-a">run a worker</a> named <var
                    939:      title="">name</var> for the script with <span>URL</span> <var
                    940:      title="">url</var>, with the <span>script browsing context</span> of the
                    941:      script that invoked the method as the <var title="">owner browsing
                    942:      context</var> and with the <span>script document context</span> of the
                    943:      script that invoked the method as the <var title="">owner
                    944:      document</var>.</p>
                    945: 
                    946:     <p>If that algorithm invokes the steps for <dfn id=success title="worker
                    947:      creation succeeded">success steps</dfn>, then <a href="#attach"
                    948:      title="attach to a worker">attach</a> to this new worker, with the newly
                    949:      created port.</p>
                    950: 
                    951:     <p>Otherwise, if the <dfn id=worker>worker creation failed</dfn>, then at
                    952:      the next available opportunity, after any scripts have finished
                    953:      executing<!-- XXX queue -->, <span>fire a simple event</span> called
                    954:      <code title=event-error>error</code> at the newly created port.</p>
                    955:   </ol>
                    956: 
                    957:   <hr>
                    958: 
                    959:   <p>The steps to <dfn id=attach>attach to a worker</dfn> given a
                    960:    <code>MessagePort</code> object <var title="">port</var> are as follows:
                    961: 
                    962:   <ol>
                    963:    <li>
                    964:     <p>If <var title="">port</var> would have been garbage collected, or if
                    965:      the <span>active document</span> of the <code
                    966:      title=dom-MessagePort-ownerWindow>ownerWindow</code> of <var
                    967:      title="">port</var> is no longer the same <code>Document</code> object
                    968:      as when <var title="">port</var> was created, then do nothing. Abort
                    969:      these steps. If the worker was just created, it'll get killed
                    970:      immediately.</p>
                    971: 
                    972:    <li>
                    973:     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
                    974:      <code><a href="#windowworker">WindowWorker</a></code> of the worker.
                    975: 
                    976:    <li>
                    977:     <p><span>Entangle</span> this newly created port and the port <var
                    978:      title="">port</var> that was passed to these steps.
                    979: 
                    980:    <li>
                    981:     <p>Set the <code title=dom-MessagePort-active>active</code> attribute of
                    982:      both ports to true.
                    983: 
                    984:    <li>
                    985:     <p>At the next available opportunity, after any scripts have finished
                    986:      executing<!-- XXX queue -->, <span>fire a simple event</span> called
                    987:      <code title=event-load>load</code> at <var title="">port</var>.
                    988: 
                    989:    <li>
                    990:     <p>Create an event that uses the <code>MessageEvent</code> interface,
                    991:      with the name <code title=event-attach>attach</code>, which does not
                    992:      bubble, is cancelable, has no default action, has a <code
                    993:      title=dom-MessageEvent-data>data</code> attribute whose value is the
                    994:      empty string and has a <code
                    995:      title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
                    996:      value is the newly created port, and add it to the worker's <code><a
                    997:      href="#windowworker">WindowWorker</a></code> object's <a
                    998:      href="#queue">queue of events</a>, targetted at the <code><a
                    999:      href="#windowworker">WindowWorker</a></code> object itself.
                   1000:   </ol>
1.7       ihickson 1001: 
1.12      ihickson 1002:   <h2 id=apis-available><span class=secno>3. </span>APIs available to workers</h2>
                   1003: 
1.18      ihickson 1004:   <p>There must be no interface objects and constructors available in the
                   1005:    global scope of scripts whose <span>script execution context</span> is a
                   1006:    <code><a href="#windowworker">WindowWorker</a></code> object except for
                   1007:    the following:
                   1008: 
                   1009:   <ul>
                   1010:    <li>
                   1011:     <p><code>XMLHttpRequest</code> and all interface objects and constructors
                   1012:      defined by the XMLHttpRequest specifications, except that the
                   1013:      <span>document response entity body</span> must always be null. <a
                   1014:      href="#refsXHR">[XHR]</a>
                   1015: 
                   1016:    <li>
                   1017:     <p>The <code>WebSocket</code> interface object and constructor.
                   1018: 
                   1019:    <li>
                   1020:     <p>The <code>MessageChannel</code> interface object and constructor.
                   1021:   </ul>
                   1022: 
                   1023:   <hr>
                   1024: 
1.12      ihickson 1025:   <p>Objects that implement the <code><a
                   1026:    href="#windowworker">WindowWorker</a></code> interface must also implement
                   1027:    the following interfaces:
                   1028: 
                   1029:   <ul>
                   1030:    <li>
                   1031:     <p>The <code>Window</code> interface, and interfaces that are required to
                   1032:      be implemented by objects implementing that interface, including:</p>
                   1033: 
                   1034:     <ul>
                   1035:      <li>The <code>WindowTimers</code> interface
                   1036: 
                   1037:      <li>The <code><a
                   1038:       href="#windowworkercreators">WindowWorkerCreators</a></code> interface
                   1039:     </ul>
                   1040: 
                   1041:    <li>The <code>EventTarget</code> interface.
                   1042:   </ul>
                   1043: 
1.18      ihickson 1044:   <div class=note>
                   1045:    <p>The following APIs are available in workers due to their being defined
                   1046:     on the <code>Window</code> object or one of the interfaces that are
                   1047:     required to be implemented by objects implementing that interface:</p>
                   1048: 
                   1049:    <ul>
                   1050:     <li>The <code title=dom-showNotification>showNotification()</code> APIs.
                   1051: 
                   1052:     <li>The <code title=dom-localStorage>localStorage</code> object and
                   1053:      related name/value pair storage APIs.
                   1054: 
                   1055:     <li>The <code title=dom-openDatabase>openDatabase()</code> method and the
                   1056:      related SQL database API.
                   1057:    </ul>
                   1058:   </div>
                   1059: 
                   1060:   <p class=big-issue>Need to define a sync database API.</p>
                   1061:   <!-- XXX ApplicationCache -->
                   1062:   <!-- XXX a way to set cookies on the URL for the script -->
                   1063:   <!-- XXX the navigator object, for UA sniffing (?) -->
                   1064:   <!-- XXX void log(in DOMString s); // log to console -->
1.13      ihickson 1065: 
1.18      ihickson 1066:   <hr>
1.13      ihickson 1067: 
1.18      ihickson 1068:   <p>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects,
                   1069:    etc) are not available to workers in this version of this specification.
1.13      ihickson 1070: 
1.1       ihickson 1071:   <h2 class=no-num id=references>References</h2>
                   1072: 
                   1073:   <p class=big-issue>This section will be written in a future
                   1074:    draft.<!--XXX-->
                   1075: 
                   1076:   <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
                   1077:   <!-- ACKS -->
                   1078: 
                   1079:   <p>Thanks to Maciej Stachowiak and Mike Smith for their useful and
                   1080:    substantial comments.
1.4       ihickson 1081: 
                   1082:   <p>Huge thanks to the whole Gears team, who pioneered this technology and
                   1083:    whose experience has been a huge influence on this specification.

Webmaster