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

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.26      ihickson   22:     Draft <!--ZZZ--> 6 August 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.26      ihickson  147:    the W3C Recommendation track. <!--ZZZ:--> This specification is the 6
1.25      ihickson  148:    August 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.22      ihickson  186:      <li><a href="#conformance"><span class=secno>1.2 </span>Conformance
1.1       ihickson  187:       requirements</a>
                    188:       <ul class=toc>
1.22      ihickson  189:        <li><a href="#dependencies"><span class=secno>1.2.1
1.1       ihickson  190:         </span>Dependencies</a>
                    191:       </ul>
                    192: 
1.22      ihickson  193:      <li><a href="#terminology"><span class=secno>1.3 </span>Terminology</a>
1.4       ihickson  194:     </ul>
                    195: 
1.12      ihickson  196:    <li><a href="#infrastructure"><span class=secno>2.
                    197:     </span>Infrastructure</a>
1.4       ihickson  198:     <ul class=toc>
1.26      ihickson  199:      <li><a href="#the-workerglobalscope"><span class=secno>2.1 </span>The
                    200:       <code>WorkerGlobalScope</code> interface</a>
1.4       ihickson  201: 
1.15      ihickson  202:      <li><a href="#base-urls"><span class=secno>2.2 </span>Base URLs and
                    203:       origins of workers</a>
                    204: 
                    205:      <li><a href="#the-queue"><span class=secno>2.3 </span>The queue of
1.4       ihickson  206:       events</a>
                    207: 
1.15      ihickson  208:      <li><a href="#the-workers"><span class=secno>2.4 </span>The worker's
1.9       ihickson  209:       ports</a>
                    210: 
1.15      ihickson  211:      <li><a href="#processing"><span class=secno>2.5 </span>Processing
1.4       ihickson  212:       model</a>
1.12      ihickson  213: 
1.15      ihickson  214:      <li><a href="#creating"><span class=secno>2.6 </span>Creating
1.12      ihickson  215:       workers</a>
1.1       ihickson  216:     </ul>
                    217: 
1.12      ihickson  218:    <li><a href="#apis-available"><span class=secno>3. </span>APIs available
                    219:     to workers</a>
1.26      ihickson  220:     <ul class=toc>
                    221:      <li><a href="#importing"><span class=secno>3.1 </span>Importing scripts
                    222:       and libraries</a>
                    223: 
                    224:      <li><a href="#apis-defined"><span class=secno>3.2 </span>APIs defined in
                    225:       other specifications</a>
                    226: 
                    227:      <li><a href="#interface"><span class=secno>3.3 </span>Interface objects
                    228:       and constructors</a>
                    229:     </ul>
1.4       ihickson  230: 
1.1       ihickson  231:    <li class=no-num><a href="#references">References</a>
                    232: 
                    233:    <li class=no-num><a href="#acknowledgements">Acknowledgements</a>
                    234:   </ul>
                    235:   <!--end-toc-->
                    236: 
                    237:   <hr>
                    238: 
                    239:   <h2 id=introduction><span class=secno>1. </span>Introduction</h2>
                    240: 
1.4       ihickson  241:   <h3 id=tutorial><span class=secno>1.1 </span>Tutorial</h3>
1.1       ihickson  242: 
                    243:   <p><em>This section is non-normative.</em>
                    244: 
1.4       ihickson  245:   <p class=big-issue>This section is missing.
                    246: 
1.22      ihickson  247:   <h3 id=conformance><span class=secno>1.2 </span>Conformance requirements</h3>
1.1       ihickson  248: 
                    249:   <p>All diagrams, examples, and notes in this specification are
                    250:    non-normative, as are all sections explicitly marked non-normative.
                    251:    Everything else in this specification is normative.
                    252: 
                    253:   <p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
                    254:   NOT",-->
                    255:    "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the
                    256:    normative parts of this document are to be interpreted as described in
                    257:    RFC2119. For readability, these words do not appear in all uppercase
                    258:    letters in this specification. <a href="#refsRFC2119">[RFC2119]</a></p>
                    259:   <!-- XXX but they should be
                    260:   marked up -->
                    261: 
                    262:   <p>Requirements phrased in the imperative as part of algorithms (such as
                    263:    "strip any leading space characters" or "return false and abort these
                    264:    steps") are to be interpreted with the meaning of the key word ("must",
                    265:    "should", "may", etc) used in introducing the algorithm.
                    266: 
                    267:   <p>Some conformance requirements are phrased as requirements on attributes,
                    268:    methods or objects. Such requirements are to be interpreted as
                    269:    requirements on user agents.
                    270: 
                    271:   <p>Conformance requirements phrased as algorithms or specific steps may be
                    272:    implemented in any manner, so long as the end result is equivalent. (In
                    273:    particular, the algorithms defined in this specification are intended to
                    274:    be easy to follow, and not intended to be performant.)
                    275: 
                    276:   <p>The only conformance class defined by this specification is user agents.
                    277: 
                    278:   <p>User agents may impose implementation-specific limits on otherwise
                    279:    unconstrained inputs, e.g. to prevent denial of service attacks, to guard
                    280:    against running out of memory, or to work around platform-specific
                    281:    limitations.
                    282: 
1.22      ihickson  283:   <h4 id=dependencies><span class=secno>1.2.1 </span>Dependencies</h4>
1.1       ihickson  284: 
                    285:   <p>This specification relies on several other underlying specifications.
                    286: 
                    287:   <dl>
                    288:    <dt>HTML5
                    289: 
                    290:    <dd>
                    291:     <p>Many fundamental concepts from HTML5 are used by this specification.
                    292:      <a href="#refsHTML5">[HTML5]</a></p>
                    293: 
                    294:    <dt>ECMAScript
                    295: 
                    296:    <dd>
                    297:     <p>This specification is intended to be used with JavaScript as the
                    298:      scripting language. <a href="#refsJS">[JS]</a></p>
                    299: 
                    300:    <dt>WebIDL
                    301: 
                    302:    <dd>
                    303:     <p>The IDL blocks in this specification use the semantics of the WebIDL
                    304:      specification. <a href="#refsWebIDL">[WebIDL]</a></p>
                    305:   </dl>
                    306: 
1.22      ihickson  307:   <h3 id=terminology><span class=secno>1.3 </span>Terminology</h3>
1.1       ihickson  308: 
                    309:   <p>For simplicity, terms such as <em>shown</em>, <em>displayed</em>, and
                    310:    <em>visible</em> might sometimes be used when referring to the way a
                    311:    document is rendered to the user. These terms are not meant to imply a
                    312:    visual medium; they must be considered to apply to other media in
                    313:    equivalent ways.
                    314: 
1.4       ihickson  315:   <p>The construction "a <code title="">Foo</code> object", where <code
                    316:    title="">Foo</code> is actually an interface, is sometimes used instead of
                    317:    the more accurate "an object implementing the interface <code
                    318:    title="">Foo</code>".
1.1       ihickson  319: 
                    320:   <p>The term DOM is used to refer to the API set made available to scripts
                    321:    in Web applications, and does not necessarily imply the existence of an
                    322:    actual <code>Document</code> object or of any other <code>Node</code>
                    323:    objects as defined in the DOM Core specifications. <a
                    324:    href="#refsDOM3CORE">[DOM3CORE]</a>
                    325: 
                    326:   <p>A DOM attribute is said to be <em>getting</em> when its value is being
                    327:    retrieved (e.g. by author script), and is said to be <em>setting</em> when
                    328:    a new value is assigned to it.
                    329: 
                    330:   <p>If a DOM object is said to be <dfn id=live>live</dfn>, then that means
                    331:    that any attributes returning that object must always return the same
                    332:    object (not a new object each time), and the attributes and methods on
                    333:    that object must operate on the actual underlying data, not a snapshot of
                    334:    the data.
                    335: 
1.12      ihickson  336:   <h2 id=infrastructure><span class=secno>2. </span>Infrastructure</h2>
1.4       ihickson  337: 
1.26      ihickson  338:   <h3 id=the-workerglobalscope><span class=secno>2.1 </span>The <code><a
                    339:    href="#workerglobalscope">WorkerGlobalScope</a></code> interface</h3>
1.4       ihickson  340: 
                    341:   <pre
1.26      ihickson  342:    class=idl>[NoInterfaceObject] interface <dfn id=workerglobalscope>WorkerGlobalScope</dfn> {
                    343:   // core worker features
                    344:   readonly attribute <a href="#workerglobalscope">WorkerGlobalScope</a> <a href="#self" title=dom-WorkerGlobalScope-self>self</a>;
                    345:   readonly attribute DOMString <a href="#url" title=dom-WorkerGlobalScope-URL>URL</a>;
                    346:   readonly attribute DOMString <a href="#name" title=dom-WorkerGlobalScope-name>name</a>;
                    347:   readonly attribute boolean <a href="#closing" title=dom-WorkerGlobalScope-closing>closing</a>;
                    348:   void <a href="#close" title=dom-WorkerGlobalScope-close>close</a>();
                    349: 
                    350:   // other APIs
                    351:   readonly attribute WorkerUtils <a href="#utils" title=dom-WorkerGlobalScope-utils>utils</a>;
1.9       ihickson  352: 
                    353:   // event handler attributes
1.26      ihickson  354:            attribute <span>EventListener</span> <a href="#onconnect" title=handler-WorkerGlobalScope-onconnect>onconnect</a>;
                    355:            attribute <span>EventListener</span> <a href="#onunload" title=handler-WorkerGlobalScope-onunload>onunload</a>;
1.4       ihickson  356: };</pre>
                    357: 
1.26      ihickson  358:   <p>The <dfn id=self
                    359:    title=dom-WorkerGlobalScope-self><code>self</code></dfn> attribute must
                    360:    return the <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
                    361:    object itself.
1.9       ihickson  362: 
1.26      ihickson  363:   <p>The <dfn id=url title=dom-WorkerGlobalScope-URL><code>URL</code></dfn>
1.6       ihickson  364:    attribute must return the value it was assigned when the <code><a
1.26      ihickson  365:    href="#workerglobalscope">WorkerGlobalScope</a></code> object was created
                    366:    by the "<a href="#run-a">run a worker</a>" algorithm. It gives the
                    367:    <span>absolute URL</span> of the script that was used to initialize the
                    368:    worker.
                    369: 
                    370:   <p>The <dfn id=name
                    371:    title=dom-WorkerGlobalScope-name><code>name</code></dfn> attribute must
                    372:    return the value it was assigned when the <code><a
                    373:    href="#workerglobalscope">WorkerGlobalScope</a></code> object was created
                    374:    by the "<a href="#run-a">run a worker</a>" algorithm. If it has a value
                    375:    that isn't the empty string, its value represents the name that can be
                    376:    used to obtain a reference to the worker using the <code
                    377:    title=dom-WorkerFactory-createNamedWorker><a
1.11      ihickson  378:    href="#createnamedworker">createNamedWorker()</a></code> method.
1.6       ihickson  379: 
1.4       ihickson  380:   <p>The <dfn id=closing
1.26      ihickson  381:    title=dom-WorkerGlobalScope-closing><code>closing</code></dfn> attribute
                    382:    must return false until it is set to true by one of the algorithms in the
1.19      ihickson  383:    processing model section below.
1.4       ihickson  384: 
1.9       ihickson  385:   <p>The following are the <span>event handler DOM attributes</span> that
                    386:    must be supported by objects implementing the <code><a
1.26      ihickson  387:    href="#workerglobalscope">WorkerGlobalScope</a></code> interface:
1.9       ihickson  388: 
                    389:   <dl>
1.20      ihickson  390:    <dt><dfn id=onconnect
1.26      ihickson  391:     title=handler-WorkerGlobalScope-onconnect><code>onconnect</code></dfn>
1.9       ihickson  392: 
                    393:    <dd>
                    394:     <p>Must be invoked whenever a <code
1.26      ihickson  395:      title=event-WorkerGlobalScope-connect>connect</code> event is targeted
                    396:      at or bubbles through the <code><a
                    397:      href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1.9       ihickson  398: 
                    399:    <dt><dfn id=onunload
1.26      ihickson  400:     title=handler-WorkerGlobalScope-onunload><code>onunload</code></dfn>
1.9       ihickson  401: 
                    402:    <dd>
                    403:     <p>Must be invoked whenever a <code title=event-unload>unload</code>
                    404:      event is targeted at or bubbles through the <code><a
1.26      ihickson  405:      href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1.9       ihickson  406:   </dl>
                    407: 
1.26      ihickson  408:   <p>The <dfn id=utils
                    409:    title=dom-WorkerGlobalScope-utils><code>utils</code></dfn> attribute must
                    410:    return the <code><a href="#workerutils">WorkerUtils</a></code> object
                    411:    created for the <code><a
                    412:    href="#workerglobalscope">WorkerGlobalScope</a></code> object when the
                    413:    worker was created.
                    414: 
1.15      ihickson  415:   <h3 id=base-urls><span class=secno>2.2 </span>Base URLs and origins of
                    416:    workers</h3>
                    417: 
                    418:   <p>The <span>base URL</span> of a <span>URL</span> passed to an API in a
1.16      ihickson  419:    worker is the <span>absolute URL</span> given by the value of the <code
1.26      ihickson  420:    title=dom-WorkerGlobalScope-URL><a href="#url">URL</a></code> attribute of
                    421:    the worker's <code><a
                    422:    href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1.15      ihickson  423: 
1.25      ihickson  424:   <p>Both the <span>origin</span> and <span>effective script origin</span> of
                    425:    scripts running in workers are the <span>origin</span> of the
1.16      ihickson  426:    <span>absolute URL</span> given by the value of the <code
1.26      ihickson  427:    title=dom-WorkerGlobalScope-URL><a href="#url">URL</a></code> attribute of
                    428:    the worker's <code><a
                    429:    href="#workerglobalscope">WorkerGlobalScope</a></code> object.
1.15      ihickson  430: 
                    431:   <h3 id=the-queue><span class=secno>2.3 </span>The queue of events</h3>
1.4       ihickson  432: 
1.26      ihickson  433:   <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
                    434:    object is asssociated with a <dfn id=queue>queue of events</dfn>, which is
                    435:    initially empty.
1.4       ihickson  436: 
                    437:   <p>An event in the queue can be a DOM event or a timeout callback.
                    438: 
1.8       ihickson  439:   <p>All asynchronous callbacks and events that would be called or dispatched
                    440:    in the worker must be added to the worker's queue, with the "<a
                    441:    href="#run-a">run a worker</a>" processing model below taking care of
                    442:    actually calling the callbacks or dispatching the events.
                    443: 
1.26      ihickson  444:   <p>Once the <code><a
                    445:    href="#workerglobalscope">WorkerGlobalScope</a></code>'s <code
                    446:    title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1.8       ihickson  447:    attribute is set to true, the queue must discard anything else that would
                    448:    be added to it. Effectively, once the <code
1.26      ihickson  449:    title=dom-WorkerGlobalScope-closing><a href="#closing">closing</a></code>
1.8       ihickson  450:    attribute is true, timers stop firing, notifications for all pending
                    451:    asynchronous operations are dropped, etc.
                    452: 
1.15      ihickson  453:   <h3 id=the-workers><span class=secno>2.4 </span>The worker's ports</h3>
1.9       ihickson  454: 
                    455:   <p>Workers communicate with other workers and with <span title="browsing
                    456:    context">browsing contexts</span> through <span title="channel
                    457:    messaging">message channels</span> and their <code>MessagePort</code>
                    458:    objects.
                    459: 
1.26      ihickson  460:   <p>Each <code><a href="#workerglobalscope">WorkerGlobalScope</a></code>
                    461:    <var title="">worker</var> has a list of <dfn id=the-workers0>the worker's
1.9       ihickson  462:    ports</dfn>, which consists of all the <code>MessagePort</code> objects
                    463:    that are entangled with another port and that have one (but only one) port
1.26      ihickson  464:    owned by <var title="">worker</var>. This list includes all the
                    465:    <code>MessagePort</code> objects that are in events pending in the <a
                    466:    href="#queue">queue of events</a>.
1.9       ihickson  467: 
1.17      ihickson  468:   <hr>
                    469: 
                    470:   <p>A worker is said to be a <dfn id=front-line>front-line worker</dfn> if
                    471:    at least one of the <a href="#the-workers0">the worker's ports</a> has an
1.26      ihickson  472:    entangled <code>MessagePort</code> owned by a <code>Window</code> object.
1.17      ihickson  473: 
                    474:   <p>A worker is said to be a <dfn id=needed>needed worker</dfn> if either:
                    475: 
                    476:   <ul>
                    477:    <li>it is a <a href="#front-line">front-line worker</a>, or
                    478: 
                    479:    <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1.26      ihickson  480:     an entangled <code>MessagePort</code> owned by a <code><a
                    481:     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
                    482:     itself a <a href="#needed">needed worker</a>.
1.17      ihickson  483:   </ul>
                    484: 
                    485:   <hr>
                    486: 
                    487:   <p>A worker is said to be an <dfn id=active>active front-line worker</dfn>
                    488:    if at least one of the <a href="#the-workers0">the worker's ports</a> has
                    489:    both an <code title=dom-MessagePort-active>active</code> attribute whose
1.26      ihickson  490:    value is true and an entangled <code>MessagePort</code> whose owner is a
                    491:    <code>Window</code> object.
1.17      ihickson  492: 
                    493:   <p>A worker is said to be an <dfn id=active0>active needed worker</dfn> if
                    494:    either:
                    495: 
                    496:   <ul>
                    497:    <li>it is an <a href="#active">active front-line worker</a>, or
                    498: 
                    499:    <li>at least one of the <a href="#the-workers0">the worker's ports</a> has
1.26      ihickson  500:     an entangled <code>MessagePort</code> owned by a <code><a
                    501:     href="#workerglobalscope">WorkerGlobalScope</a></code> object that is
                    502:     itself an <a href="#active0">active needed worker</a>.
1.17      ihickson  503:   </ul>
                    504: 
1.15      ihickson  505:   <h3 id=processing><span class=secno>2.5 </span>Processing model</h3>
1.4       ihickson  506: 
1.6       ihickson  507:   <p>When a user agent is to <dfn id=run-a>run a worker</dfn> named <var
1.7       ihickson  508:    title="">name</var> for a script with <span>URL</span> <var
                    509:    title="">url</var>, a browsing context <var title="">owner browsing
                    510:    context</var> and a <code>Document</code> <var title="">owner
                    511:    document</var>, it must run the following steps in a completely separate
                    512:    and parallel execution environment:
1.4       ihickson  513: 
                    514:   <ol>
                    515:    <li>
1.7       ihickson  516:     <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
                    517:      <var title="">url</var>.</p>
                    518: 
                    519:     <p>If the attempt fails, then abort these steps and invoke the <a
1.9       ihickson  520:      href="#worker" title="worker creation failed">error handling steps</a>
1.7       ihickson  521:      defined by the algorithm that called this one.</p>
                    522: 
                    523:     <p>If the attempt succeeds, then let <var title="">script</var> be the
                    524:      resource that was obtained.</p>
                    525: 
                    526:     <p class=note>As with <code>script</code> elements, the MIME type of the
                    527:      script is ignored. Unlike with <code>script</code> elements, there is no
                    528:      way to override the type. It's always assumed to be JavaScript.</p>
                    529:     <!-- XXX people will complain about
                    530:     this. I guess we might want to examine the MIME type... -->
                    531:     
                    532: 
                    533:    <li>
1.26      ihickson  534:     <p>Create a new <code><a
                    535:      href="#workerglobalscope">WorkerGlobalScope</a></code> object, <var
                    536:      title="">worker</var>.</p>
1.4       ihickson  537: 
                    538:    <li>
1.26      ihickson  539:     <p>Set the <code title=dom-WorkerGlobalScope-URL><a
                    540:      href="#url">URL</a></code> attribute of <var title="">worker</var> to
                    541:      the value of <var title="">url</var>.</p>
1.9       ihickson  542: 
                    543:    <li>
1.26      ihickson  544:     <p>Set the <code title=dom-WorkerGlobalScope-name><a
1.19      ihickson  545:      href="#name">name</a></code> attribute of <var title="">worker</var> to
1.6       ihickson  546:      the value of <var title="">name</var>.</p>
                    547: 
                    548:    <li>
1.26      ihickson  549:     <p>Create a new <code><a href="#workerutils">WorkerUtils</a></code>
                    550:      object for the <code title=dom-WorkerGlobalScope-utils><a
                    551:      href="#utils">utils</a></code> attribute of <var title="">worker</var>.
                    552: 
                    553:    <li>
1.4       ihickson  554:     <p>Let <var title="">script</var>'s <span>script execution context</span>
                    555:      (and thus also <span>global object</span>) be <var
1.19      ihickson  556:      title="">worker</var>.</p>
1.4       ihickson  557: 
                    558:    <li>
                    559:     <p>Let <var title="">script</var>'s <span>script browsing context</span>
                    560:      be <var title="">owner browsing context</var>.</p>
                    561: 
                    562:    <li>
                    563:     <p>Let <var title="">script</var>'s <span>script document context</span>
                    564:      be <var title="">owner document</var>.</p>
                    565: 
                    566:    <li>
1.9       ihickson  567:     <p>Invoke the <a href="#success" title="worker creation
                    568:      succeeded">success steps</a> defined by the algorithm that called this
1.17      ihickson  569:      one.</p>
                    570: 
                    571:     <p class=note>This will usually add an event to the <a
                    572:      href="#queue">queue of events</a>. If it does, that event will have a
                    573:      <code>MessagePort</code> and thus the list of <a
                    574:      href="#the-workers0">the worker's ports</a> will not be empty. If it
1.19      ihickson  575:      doesn't, then the next step will set the <var title="">worker</var>
1.26      ihickson  576:      object's <code title=dom-WorkerGlobalScope-closing><a
1.17      ihickson  577:      href="#closing">closing</a></code> attribute to true.</p>
                    578: 
                    579:    <li>
                    580:     <p>Start monitoring <var title="">worker</var>, such that as soon as the
                    581:      worker stops being a <a href="#needed">needed worker</a>, the <var
1.26      ihickson  582:      title="">worker</var> object's <code
                    583:      title=dom-WorkerGlobalScope-closing><a
1.17      ihickson  584:      href="#closing">closing</a></code> attribute is set to true.</p>
1.9       ihickson  585: 
                    586:    <li>
                    587:     <p>Start monitoring <var title="">worker</var>, such that whenever the
1.19      ihickson  588:      <var title="">worker</var> object's <code
1.26      ihickson  589:      title=dom-WorkerGlobalScope-closing><a
                    590:      href="#closing">closing</a></code> attribute is false and the worker is
                    591:      not an <a href="#active0">active needed worker</a>, the user agent
                    592:      suspends execution of script in that worker until such time as either
                    593:      the <code title=dom-WorkerGlobalScope-closing><a
                    594:      href="#closing">closing</a></code> attribute switches to true or the
                    595:      worker becomes an <a href="#active0">active needed worker</a>.</p>
1.7       ihickson  596: 
                    597:    <li>
                    598:     <p>Run <var title="">script</var> until it either returns, fails to catch
                    599:      an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
                    600:      a worker</a>" algorithm below.</p>
                    601: 
                    602:     <p class=note>If the script gets aborted by the "<a href="#kill-a">kill a
                    603:      worker</a>" algorithm, then that same algorithm will cause there to only
                    604:      be a single event in the <a href="#queue">queue of events</a> at the
1.9       ihickson  605:      next step, namely the <code title=message-unload>unload</code> event.
                    606:      However, if the event is ignored then it will become true as soon as
                    607:      that port is garbage collected.</p>
1.4       ihickson  608: 
                    609:    <li>
1.9       ihickson  610:     <p><i>Event loop</i>: Wait until either there is an event in the <a
1.4       ihickson  611:      href="#queue">queue of events</a> associated with <var
1.19      ihickson  612:      title="">worker</var> or the <var title="">worker</var> object's <code
1.26      ihickson  613:      title=dom-WorkerGlobalScope-closing><a
                    614:      href="#closing">closing</a></code> attribute is set to true.</p>
1.4       ihickson  615: 
                    616:    <li>
                    617:     <p>Dispatch the oldest event or callback in the <a href="#queue">queue of
1.9       ihickson  618:      events</a>, if any. The handling of this event or the execution of this
                    619:      callback might get prematurely aborted by the "<a href="#kill-a">kill a
1.8       ihickson  620:      worker</a>" algorithm below.</p>
1.4       ihickson  621: 
                    622:    <li>
                    623:     <p>If there are any more events in the <a href="#queue">queue of
1.19      ihickson  624:      events</a> or if the <var title="">worker</var> object's <code
1.26      ihickson  625:      title=dom-WorkerGlobalScope-closing><a
                    626:      href="#closing">closing</a></code> attribute is set to false, then jump
                    627:      back to the step above labeled <i>event loop</i>.</p>
1.4       ihickson  628: 
                    629:    <li>
                    630:     <p class=big-issue>timers, intervals, XMLHttpRequests, database
                    631:      transactions, etc, must be killed; ports must be deactivated and
1.8       ihickson  632:      unentangled (do not send unload)</p>
1.4       ihickson  633:   </ol>
                    634: 
                    635:   <hr>
                    636: 
                    637:   <p>When a user agent is to <dfn id=kill-a>kill a worker</dfn>, it must run
                    638:    the following steps in parallel with the worker's main loop (the "<a
                    639:    href="#run-a">run a worker</a>" processing model defined above):
                    640: 
                    641:   <ol>
                    642:    <li>
                    643:     <p>Create an <code>Event</code> object with the event name <code
                    644:      title=event-unload>unload</code>, which does not bubble and is not
                    645:      cancelable, and add it to the worker's <code><a
1.26      ihickson  646:      href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1.8       ihickson  647:      href="#queue">queue of events</a>, targetted at the <code><a
1.26      ihickson  648:      href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1.4       ihickson  649: 
                    650:    <li>
1.26      ihickson  651:     <p>Set the worker's <code><a
                    652:      href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
                    653:      title=dom-WorkerGlobalScope-closing><a
1.8       ihickson  654:      href="#closing">closing</a></code> attribute to true.
                    655: 
                    656:    <li>
                    657:     <p>Wait a user-agent-defined amount of time. If the "<a href="#run-a">run
                    658:      a worker</a>" processing model defined above immediately starts running
                    659:      event listeners registered for <code title=event-unload>unload</code>
                    660:      event, this time should not be zero &mdash; the idea is that the <code
                    661:      title=event-unload>unload</code> event can be used to clean up when
                    662:      shutting down unexpectedly.
1.4       ihickson  663: 
                    664:    <li>
                    665:     <p>If there are any events in the <a href="#queue">queue of events</a>
                    666:      other than the <code title=event-unload>unload</code> event that this
                    667:      algorithm just added, discard them without dispatching them.
                    668: 
                    669:    <li>
                    670:     <p>If the <code title=event-unload>unload</code> event that this
                    671:      algorithm just added hasn't yet been dispatched, then abort the script
                    672:      currently running in the worker.
                    673: 
                    674:    <li>
                    675:     <p>Wait a user-agent-defined amount of time.
                    676: 
                    677:    <li>
                    678:     <p>Abort the script currently running in the worker (if any script is
                    679:      running, then it will be a handler for the <code
                    680:      title=event-unload>unload</code> event).
                    681:   </ol>
                    682: 
1.19      ihickson  683:   <p>User agents may invoke the "<a href="#kill-a">kill a worker</a>"
                    684:    processing model on a worker at any time, e.g. in response to user
                    685:    requests, in response to CPU quota management, or when a worker stops
                    686:    being a <a href="#needed">needed worker</a> if the worker continues
1.26      ihickson  687:    executing even after its <code title=dom-WorkerGlobalScope-closing><a
1.19      ihickson  688:    href="#closing">closing</a></code> attribute was set to true.
                    689: 
1.8       ihickson  690:   <hr>
                    691: 
                    692:   <p>When a script invokes the <dfn id=close
1.26      ihickson  693:    title=dom-WorkerGlobalScope-close><code>close()</code></dfn> method on a
                    694:    <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object,
                    695:    the user agent must run the following steps:
1.8       ihickson  696: 
                    697:   <ol>
                    698:    <li>
                    699:     <p>Create an <code>Event</code> object with the event name <code
                    700:      title=event-unload>unload</code>, which does not bubble and is not
                    701:      cancelable, and add it to the <code><a
1.26      ihickson  702:      href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1.8       ihickson  703:      href="#queue">queue of events</a>, targetted at the <code><a
1.26      ihickson  704:      href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1.8       ihickson  705: 
                    706:    <li>
1.26      ihickson  707:     <p>Set the worker's <code><a
                    708:      href="#workerglobalscope">WorkerGlobalScope</a></code> object's <code
                    709:      title=dom-WorkerGlobalScope-closing><a
1.8       ihickson  710:      href="#closing">closing</a></code> attribute to true.
                    711: 
                    712:    <li>
                    713:     <p>For each <code>MessagePort</code> object that is entangled with
1.26      ihickson  714:      another port and that has one (but only one) port whose owner is the
                    715:      <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
                    716:      on which the method was invoked, run the following substeps:</p>
1.8       ihickson  717: 
                    718:     <ol>
                    719:      <li>
                    720:       <p>Unentangle the two ports.
                    721: 
                    722:      <li>
1.9       ihickson  723:       <p>Set both ports' <code title=dom-MessagePort-active>active</code>
1.8       ihickson  724:        attribute to false.
                    725: 
                    726:      <li>
                    727:       <p>At the next available opportunity, after any scripts have finished
                    728:        executing<!-- XXX queue -->, <span>fire a simple event</span> called
                    729:        <code title=event-unload>unload</code> at the other port (the one
1.26      ihickson  730:        whose owner is not the <code><a
                    731:        href="#workerglobalscope">WorkerGlobalScope</a></code> object on which
                    732:        the <code title=dom-WorkerGlobalScope-close><a
1.8       ihickson  733:        href="#close">close()</a></code> method was called).
                    734:     </ol>
                    735:   </ol>
                    736: 
1.15      ihickson  737:   <h3 id=creating><span class=secno>2.6 </span>Creating workers</h3>
1.5       ihickson  738: 
                    739:   <pre
1.26      ihickson  740:    class=idl>[NoInterfaceObject] interface <dfn id=workerfactory>WorkerFactory</dfn> {
                    741:   <span>MessagePort</span> <a href="#createworker" title=dom-WorkerFactory-createWorker>createWorker</a>(in DOMString scriptURL);
                    742:   <span>MessagePort</span> <a href="#createnamedworker" title=dom-WorkerFactory-createNamedWorker>createNamedWorker</a>(in DOMString name, in DOMString scriptURL);
1.5       ihickson  743: };</pre>
                    744: 
1.26      ihickson  745:   <p>Objects that implement the <code>Window</code> and <code><a
                    746:    href="#workerutils">WorkerUtils</a></code> interfaces must also implement
                    747:    the <code><a href="#workerfactory">WorkerFactory</a></code> interface.
1.5       ihickson  748: 
1.9       ihickson  749:   <hr>
                    750: 
1.11      ihickson  751:   <p>When the <dfn id=createworker
1.26      ihickson  752:    title=dom-WorkerFactory-createWorker><code>createWorker(<var
1.11      ihickson  753:    title="">scriptURL</var>)</code></dfn> method is invoked, the user agent
                    754:    must run the following steps:
1.9       ihickson  755: 
                    756:   <ol>
                    757:    <li>
                    758:     <p><span title="resolve a url">Resolve</span> the <var
                    759:      title="">scriptURL</var> argument.
                    760: 
                    761:    <li>
                    762:     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
                    763: 
                    764:    <li>
                    765:     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
                    766:      is not the <span title="same origin">same</span> as the origin of the
                    767:      script that invoked the method, then throw a <span>security
                    768:      exception</span>.
                    769: 
                    770:    <li>
                    771:     <p><a href="#create">Create a worker</a> from the resulting
                    772:      <span>absolute URL</span> whose name is the empty string.
                    773: 
                    774:    <li>
                    775:     <p>Return the <code>MessagePort</code> object returned from the <a
                    776:      href="#create">create a worker</a> algorithm.
                    777:   </ol>
                    778: 
                    779:   <hr>
                    780: 
1.11      ihickson  781:   <p>When the <dfn id=createnamedworker
1.26      ihickson  782:    title=dom-WorkerFactory-createNamedWorker><code>createNamedWorker(<var
1.11      ihickson  783:    title="">name</var>, <var title="">scriptURL</var>)</code></dfn> method is
1.9       ihickson  784:    invoked, the user agent must run the following steps:
                    785: 
                    786:   <ol>
                    787:    <li>
                    788:     <p><span title="resolve a url">Resolve</span> the <var
                    789:      title="">scriptURL</var> argument.
1.4       ihickson  790: 
1.9       ihickson  791:    <li>
                    792:     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
                    793: 
                    794:    <li>
                    795:     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
                    796:      is not the <span title="same origin">same</span> as the origin of the
                    797:      script that invoked the method, then throw a <span>security
                    798:      exception</span>.
                    799: 
                    800:    <li>
                    801:     <p>If the <var title="">name</var> argument is the empty string, <a
                    802:      href="#create">create a worker</a> from the resulting <span>absolute
                    803:      URL</span>, whose name is the empty string, and return the
                    804:      <code>MessagePort</code> object returned from the <a
                    805:      href="#create">create a worker</a> algorithm. Then, abort these steps.
                    806: 
                    807:    <li>
1.26      ihickson  808:     <p>If there exists a worker whose <code
                    809:      title=dom-WorkerGlobalScope-closing><a
1.9       ihickson  810:      href="#closing">closing</a></code> attribute is false, whose <code
1.26      ihickson  811:      title=dom-WorkerGlobalScope-name><a href="#name">name</a></code>
                    812:      attribute is exactly equal to the <var title="">name</var> argument, and
                    813:      whose <code title=dom-WorkerGlobalScope-URL><a
                    814:      href="#url">URL</a></code> attribute has the <span>same origin</span> as
                    815:      the resulting <span>absolute URL</span>, then run these substeps:</p>
1.9       ihickson  816: 
                    817:     <ol>
                    818:      <li>
1.26      ihickson  819:       <p>If that worker's <code title=dom-WorkerGlobalScope-URL><a
1.9       ihickson  820:        href="#url">URL</a></code> attribute is not exactly equal to the
                    821:        resulting <span>absolute URL</span>, then throw a
                    822:        <code>URL_MISMATCH_ERR</code> exception and abort these steps. <span
                    823:        class=big-issue>code 19</span>
                    824: 
                    825:      <li>
                    826:       <p><span>Create a new <code>MessagePort</code> object</span> owned by
                    827:        the <span>script execution context</span> of the script that invoked
                    828:        the method.
                    829: 
                    830:      <li>
                    831:       <p>Return that port.
                    832: 
                    833:      <li>
1.20      ihickson  834:       <p>Asynchronously, <a href="#connect" title="connect to a
                    835:        worker">connect</a> to this preexisting worker, with the newly created
1.9       ihickson  836:        port.
                    837:     </ol>
                    838: 
                    839:     <p>Otherwise, <a href="#create">create a worker</a> from the resulting
                    840:      <span>absolute URL</span>, whose name is the value of the <var
                    841:      title="">name</var> argument, and return the <code>MessagePort</code>
                    842:      object returned from the <a href="#create">create a worker</a>
                    843:      algorithm.</p>
                    844:   </ol>
                    845: 
                    846:   <hr>
                    847: 
                    848:   <p>The steps to <dfn id=create>create a worker</dfn> from a
                    849:    <span>URL</span> <var title="">url</var> and whose name is <var
                    850:    title="">name</var>, in the context of a method call, are as follows:
                    851: 
                    852:   <ol>
                    853:    <li>
                    854:     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
                    855:      <span>script execution context</span> of the script that invoked the
                    856:      method.
                    857: 
                    858:    <li>
                    859:     <p>Return that port.
                    860: 
                    861:    <li>
                    862:     <p>In a parallel execution context (i.e. a separate thread or process or
                    863:      equivalent construct), <a href="#run-a">run a worker</a> named <var
                    864:      title="">name</var> for the script with <span>URL</span> <var
                    865:      title="">url</var>, with the <span>script browsing context</span> of the
                    866:      script that invoked the method as the <var title="">owner browsing
                    867:      context</var> and with the <span>script document context</span> of the
                    868:      script that invoked the method as the <var title="">owner
                    869:      document</var>.</p>
                    870: 
                    871:     <p>If that algorithm invokes the steps for <dfn id=success title="worker
1.20      ihickson  872:      creation succeeded">success steps</dfn>, then <a href="#connect"
                    873:      title="connect to a worker">connect</a> to this new worker, with the
                    874:      newly created port.</p>
1.9       ihickson  875: 
                    876:     <p>Otherwise, if the <dfn id=worker>worker creation failed</dfn>, then at
                    877:      the next available opportunity, after any scripts have finished
                    878:      executing<!-- XXX queue -->, <span>fire a simple event</span> called
                    879:      <code title=event-error>error</code> at the newly created port.</p>
                    880:   </ol>
                    881: 
                    882:   <hr>
                    883: 
1.20      ihickson  884:   <p>The steps to <dfn id=connect>connect to a worker</dfn> given a
1.9       ihickson  885:    <code>MessagePort</code> object <var title="">port</var> are as follows:
                    886: 
                    887:   <ol>
                    888:    <li>
                    889:     <p>If <var title="">port</var> would have been garbage collected, or if
1.26      ihickson  890:      the <span>active document</span> of the owner of <var
1.9       ihickson  891:      title="">port</var> is no longer the same <code>Document</code> object
                    892:      as when <var title="">port</var> was created, then do nothing. Abort
                    893:      these steps. If the worker was just created, it'll get killed
                    894:      immediately.</p>
                    895: 
                    896:    <li>
                    897:     <p><span>Create a new <code>MessagePort</code> object</span> owned by the
1.26      ihickson  898:      <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> of the
                    899:      worker.
1.9       ihickson  900: 
                    901:    <li>
                    902:     <p><span>Entangle</span> this newly created port and the port <var
                    903:      title="">port</var> that was passed to these steps.
                    904: 
                    905:    <li>
                    906:     <p>Set the <code title=dom-MessagePort-active>active</code> attribute of
                    907:      both ports to true.
                    908: 
                    909:    <li>
                    910:     <p>At the next available opportunity, after any scripts have finished
                    911:      executing<!-- XXX queue -->, <span>fire a simple event</span> called
                    912:      <code title=event-load>load</code> at <var title="">port</var>.
                    913: 
                    914:    <li>
                    915:     <p>Create an event that uses the <code>MessageEvent</code> interface,
1.20      ihickson  916:      with the name <code title=event-connect>connect</code>, which does not
1.9       ihickson  917:      bubble, is cancelable, has no default action, has a <code
                    918:      title=dom-MessageEvent-data>data</code> attribute whose value is the
                    919:      empty string and has a <code
                    920:      title=dom-MessageEvent-messagePort>messagePort</code> attribute whose
                    921:      value is the newly created port, and add it to the worker's <code><a
1.26      ihickson  922:      href="#workerglobalscope">WorkerGlobalScope</a></code> object's <a
1.9       ihickson  923:      href="#queue">queue of events</a>, targetted at the <code><a
1.26      ihickson  924:      href="#workerglobalscope">WorkerGlobalScope</a></code> object itself.
1.9       ihickson  925:   </ol>
1.7       ihickson  926: 
1.12      ihickson  927:   <h2 id=apis-available><span class=secno>3. </span>APIs available to workers</h2>
                    928: 
1.26      ihickson  929:   <pre
                    930:    class=idl>[NoInterfaceObject] interface <dfn id=workerutils>WorkerUtils</dfn> {
1.27    ! ihickson  931:   boolean <a href="#importscript" title=dom-WorkerGlobalScope-importScript>importScript</a>(in DOMString url);
1.26      ihickson  932:   readonly attribute <span>Storage</span> <a href="#localstorage" title=dom-localStorage>localStorage</a>;
                    933:   <span>Database</span> <a href="#opendatabase" title=dom-opendatabase>openDatabase</a>(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
                    934:   void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description);
                    935:   void <a href="#shownotification" title=dom-showNotification>showNotification</a>(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
                    936: };</pre>
1.18      ihickson  937: 
1.12      ihickson  938:   <p>Objects that implement the <code><a
1.26      ihickson  939:    href="#workerutils">WorkerUtils</a></code> interface must also implement
1.12      ihickson  940:    the following interfaces:
                    941: 
                    942:   <ul>
1.26      ihickson  943:    <li>The <code>WindowTimers</code> interface
1.12      ihickson  944: 
1.26      ihickson  945:    <li>The <code>EventTarget</code> interface
1.12      ihickson  946: 
1.26      ihickson  947:    <li>The <code><a href="#workerfactory">WorkerFactory</a></code> interface,
                    948:     as noted earlier in this specification
1.12      ihickson  949:   </ul>
                    950: 
1.23      ihickson  951:   <p class=big-issue>Need to define a sync database API.
                    952: 
                    953:   <p class=big-issue>May need to define a browser sniffing API (like
                    954:    window.navigator).</p>
1.18      ihickson  955:   <!-- XXX ApplicationCache -->
                    956:   <!-- XXX a way to set cookies on the URL for the script -->
1.24      ihickson  957:   <!-- XXX debugging: void log(in DOMString s); // log to console -->
                    958:   <!-- XXX debugging: onerror -->
1.13      ihickson  959: 
1.18      ihickson  960:   <hr>
1.13      ihickson  961: 
1.18      ihickson  962:   <p>The DOM APIs (<code>Node</code> objects, <code>Document</code> objects,
                    963:    etc) are not available to workers in this version of this specification.
1.13      ihickson  964: 
1.26      ihickson  965:   <h3 id=importing><span class=secno>3.1 </span>Importing scripts and
                    966:    libraries</h3>
                    967: 
1.27    ! ihickson  968:   <p>When a script invokes the <dfn id=importscript
        !           969:    title=dom-WorkerGlobalScope-importScript><code>importScript(<var
1.26      ihickson  970:    title="">url</var>)</code></dfn> method on a <code><a
                    971:    href="#workerglobalscope">WorkerGlobalScope</a></code> object, the user
                    972:    agent must run the following steps:
                    973: 
                    974:   <ol>
                    975:    <li>
                    976:     <p><span title="resolve a url">Resolve</span> the <var title="">url</var>
                    977:      argument.
                    978: 
                    979:    <li>
                    980:     <p>If this fails, throw a <code>SYNTAX_ERR</code> exception.
                    981: 
                    982:    <li>
                    983:     <p>If the <span>origin</span> of the resulting <span>absolute URL</span>
                    984:      is not the <span title="same origin">same</span> as the origin of the
                    985:      script that invoked the method, then throw a <span>security
                    986:      exception</span>.
                    987: 
                    988:    <li>
                    989:     <p>Attempt to <span>fetch</span><!-- XXX --> the resource identified by
                    990:      the resulting <span>absolute URL</span>.</p>
                    991: 
                    992:     <p>If the attempt fails, return false and abort these steps.</p>
                    993: 
                    994:     <p>If the attempt succeeds, then let <var title="">script</var> be the
                    995:      resource that was obtained.</p>
                    996: 
                    997:     <p class=note>As with the initial script, the script here is always
                    998:      assumed to be JavaScript, regardless of the MIME type.</p>
                    999:     <!-- XXX -->
                   1000: 
                   1001:    <li>
                   1002:     <p>Let <var title="">script</var>'s <span>script execution
                   1003:      context</span>, <span>script browsing context</span>, and <span>script
                   1004:      document context</span> be the same as for the script that was executed
                   1005:      by the <a href="#run-a">run a worker</a> processing model for this
                   1006:      worker.</p>
                   1007: 
                   1008:    <li>
                   1009:     <p>Run <var title="">script</var> until it either returns, fails to catch
                   1010:      an exception, or gets prematurely aborted by the "<a href="#kill-a">kill
                   1011:      a worker</a>" algorithm above.</p>
                   1012: 
                   1013:     <p>If an exception was raised or if the script was prematurely aborted,
                   1014:      then abort these steps, letting the exception or aborting continue to be
                   1015:      processed by the script that called the <code
1.27    ! ihickson 1016:      title=dom-WorkerGlobalScope-importScript><a
        !          1017:      href="#importscript">importScript()</a></code> method.</p>
1.26      ihickson 1018: 
                   1019:     <p>Otherwise, return true.</p>
                   1020:   </ol>
                   1021: 
                   1022:   <h3 id=apis-defined><span class=secno>3.2 </span>APIs defined in other
                   1023:    specifications</h3>
                   1024: 
1.27    ! ihickson 1025:   <p>The <dfn id=localstorage
1.26      ihickson 1026:    title=dom-localStorage><code>localStorage</code></dfn>, <dfn
                   1027:    id=opendatabase title=dom-opendatabase><code>openDatabase()</code></dfn>
                   1028:    must act as defined for the APIs with the same names on the
                   1029:    <code>Window</code> object in the HTML5 specification, with the exception
                   1030:    that where the API would use the <span>origin</span> of the <span>active
                   1031:    document</span> of the <span>browsing context</span> of the
                   1032:    <code>Window</code> object on which the method was supposedly invoked, it
                   1033:    must instead use the <span>origin</span> of the script that invoked the
                   1034:    method. <a href="#refsHTML5">[HTML5]</a>
                   1035: 
                   1036:   <p>The <dfn id=shownotification
                   1037:    title=dom-showNotification><code>showNotification()</code></dfn> methods
                   1038:    must act as defined for the APIs with the same names on the
                   1039:    <code>Window</code> object in the HTML5 specification. <a
                   1040:    href="#refsHTML5">[HTML5]</a>
                   1041: 
                   1042:   <h3 id=interface><span class=secno>3.3 </span>Interface objects and
                   1043:    constructors</h3>
                   1044: 
                   1045:   <p>There must be no interface objects and constructors available in the
                   1046:    global scope of scripts whose <span>script execution context</span> is a
                   1047:    <code><a href="#workerglobalscope">WorkerGlobalScope</a></code> object
                   1048:    except for the following:
                   1049: 
                   1050:   <ul>
                   1051:    <li>
                   1052:     <p><code>XMLHttpRequest</code> and all interface objects and constructors
                   1053:      defined by the XMLHttpRequest specifications, except that the
                   1054:      <span>document response entity body</span> must always be null. <a
                   1055:      href="#refsXHR">[XHR]</a>
                   1056: 
                   1057:    <li>
                   1058:     <p>The <code>WebSocket</code> interface object and constructor.
                   1059: 
                   1060:    <li>
                   1061:     <p>The <code>MessageChannel</code> interface object and constructor.
                   1062:   </ul>
                   1063: 
1.1       ihickson 1064:   <h2 class=no-num id=references>References</h2>
                   1065: 
                   1066:   <p class=big-issue>This section will be written in a future
                   1067:    draft.<!--XXX-->
                   1068: 
                   1069:   <h2 class=no-num id=acknowledgements>Acknowledgements</h2>
                   1070:   <!-- ACKS -->
                   1071: 
1.26      ihickson 1072:   <p>Thanks to Aaron Boodman, Jonas Sicking, Maciej Stachowiak, Mike Smith,
                   1073:    and Philip Taylor for their useful and substantial comments.
1.4       ihickson 1074: 
                   1075:   <p>Huge thanks to the whole Gears team, who pioneered this technology and
                   1076:    whose experience has been a huge influence on this specification.

Webmaster