W3C

Progress events 1.0

W3C Working Draft 19 April 2007

This version:
http://www.w3.org/TR/2007/WD-progress-events-20070419
Latest version:
http://www.w3.org/TR/progress-events
Editor:
Charles McCathieNevile, Opera Software

Abstract

This document describes event types that can be used for monitoring the progress of an operation. It is primarily intended for data transfer operations such as XMLHTTPRequest [XHR], but should be usable in other relevant contexts.


Status of this Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document is the First Public Working Draft specification of the Progress events from the Web API group, part of the Rich Web Client Activity. It defines events which can be used to monitor a process and provide feedback to a user, particularly for network-based events. This editor's draft does not imply any consensus of or endorsement by any member of the working group, and may contain minor or major errors.

This document is published to solicit comments from interested parties. All comments are welcome and may be sent to public-webapi@w3.org. All messages received at this address are viewable in a public archive. The group hopes to resolve all outstanding issues and publish a Last Call draft by June 2007. Currently there are several known issues which are unresolved by this specification. These may be identified in the draft thus: @@issue: Something that needs to be settled before the draft is ready for Last Call, and may be logged in the WebAPI group's issue tracker for the ProgressEvents. The current Editor's draft (the most current version, but most prone to contain errors or be changed) is also available, with the latest version at http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

Contents

Conformance

The terms must, should, may, must not, should not, are used in this document in accordance with [RFC2119]. A conformant implementation of this specification meets all relevant requirements identified by the use of these terms.

There are three classes of conformance to this specification:

Specification
A specification may describe how progress events can be dispatched for some operation. A conformant specification is one in which this specification includes all the requirements identified in the section Referring to progress events from other specifications.
User agent
A conforming user agent implements all the requirements described for user agents throughout this specification.
Content
Conforming content generates and consumes progress events in accordance with the requirements defined in this specification, and in accordance with any additional conformance requirements defined by a specification which describes an operation that can lead to progress events being dispatched. (Note that content which meets the requirements of a non-conforming specification may not be conforming content).

The ProgressEvent events

Event definitions

The following events are defined in this specification

Name Description How often? When?
loadstart The operation has begun once Must be dispatched first
progress The operation is in progress zero or more May be dispatched zero or more times after a loadstart event, before any of error, abort or load events are dispatched
error The operation failed to complete, e.g. as a result of a network error never or once Exactly one of these must be dispatched
abort The operation was cancelled, e.g. as a result of user interaction never or once
load The operation successfuly completed never or once

These events must not bubble, and must be cancelable.

These events trigger event listeners attached on Element nodes for that event and on the capture and target phases. @@issue: on anything else?

No default action is defined for these events.

These events are in the null namespace. Initialisation methods are provided in both namespaced and un-namespaced versions, but this specification does not recommend use of one over the other.

Interface definitions

@@issue: clarify that this section inherits from event.initEvent [@@]. Where there is any conflict, that should be the normative version.

IDL Definition
interface ProgressEvent : events::Event {
     readonly attribute boolean         lengthComputable;
     readonly attribute unsigned long   loaded;
     readonly attribute unsigned long   total;
     void               initProgressEvent(in DOMString typeArg,
                                          in boolean       canBubbleArg,
                                          in boolean       cancelableArg,
                                          in boolean       lengthComputableArg,
                                          in unsigned long loadedArg,
                                          in unsigned long totalArg,
     void               initProgressEventNS(in DOMString namespaceURI,
                                            in DOMString typeArg,
                                            in boolean       canBubbleArg,
                                            in boolean       cancelableArg,
                                            in boolean       lengthComputableArg,
                                            in unsigned long loadedArg,
                                            in unsigned long totalArg,
};


Attributes
readonly boolean lengthComputable
Specifies whether the total size of the transfer is known.
readonly unsigned long loaded
Specifies the number of bytes downloaded since the beginning of the download. This refers to the content, excluding headers and overhead from the transaction @@issue: do we need to include the headers or other overhead content?, and where there is a content-encoding or transfer-encoding refers to the number of bytes to be transferred, i.e. with the relevant encodings applied. For more details on HTTP see [RFC2616]. @@issue: presumably this spec should be workable with non-HTTP transfers, HTTP HEAD requests, etc?@@issue: How do you measure the progress of an HTTP POST where a measurable amount of data goes up, and another one comes back?
readonly unsigned long total
Specifies the expected total number of bytes of the content transferred in the operation. Where the size of the transfer is for some reason unknown, the value of this attribute must be zero.
Methods
initProgressEvent
The initProgressEvent method is used to initialize the value of a progress event created through the DocumentEvent interface. If this method is called multiple times, the final invocation takes precedence .
Parameters
DOMString type
This must be one of loadstart, progress, error, abort, load. If it is not one of those values then this specification does not define the resulting event.
canBubbleArg
This must be false. Where a value of true is passed, user agents must override that and change the value to false.
cancelableArg
This must be true. Where a value of false is passed, user agents must override that and change the value to true.
lengthComputableArg
If the user agent has reliable information about the value of total, then this should be true. If the user agent does not have reliable information about the vale of total, this should be false
loadedArg
This parameter specifies the total number of bytes already loaded, In most cases, this will be 0 (zero). If this value is not a non-negative number, the user agent must change it to zero.
totalArg
This specifies the total number of bytes to be loaded. If lengthComputable is false, this must be zero. If any other parameter is passed, and lengthComputable is false, the user agent must override this and set the value to zero. If lengthComputable is true, and the value of this parameter is not a non-negative number, the user agent must set lengthComputable to false and the value of total to zero.
No Return Value
No Exceptions
initProgressEventNS
The initProgressEvent method is used to initialize the value of a namespaced progress event created through the DocumentEvent interface. This method may only be called before the progress event has been dispatched via the dispatchEvent method, though it may be called multiple times during that phase if necessary. If called multiple times, the final invocation takes precedence.
Parameters
namespaceURI
For all events defined in this spcification, the value of this parameter is null.
DOMString type
This must be one of loadstart, progress, error, abort, load. If it is not one of those values then this specification does not define the resulting event.
canBubbleArg
This must be false. Where a value of true is passed, user agents must override that and change the value to false.
cancelableArg
This must be true. Where a value of false is passed, user agents must override that and change the value to true.
lengthComputableArg
If the user agent has reliable information about the value of total, then this should be true. If the user agent does not have reliable information about the vale of total, this should be false
loadedArg
This parameter specifies the total number of bytes already loaded, In most cases, this will be 0 (zero). If this value is not a non-negative number, the user agent must change it to zero.
totalArg
This specifies the total number of bytes to be loaded. If lengthComputable is false, this must be zero. If any other parameter is passed, and lengthComputable is false, the user agent must override this and set the value to zero. If lengthComputable is true, and the value of this parameter is not a non-negative number, the user agent must set lengthComputable to false and the value of total to zero.

No Return Value
No Exceptions

Referring to progress events from other specifications

A progress event occurs when the user agent makes progress in some data transfer operation, such as loading a resource from the web via XMLHttpRequest [XHR]. Other specifications which have a use case for these events should define when ProgressEvent events are dispatched.

In short, there must be at least one start event, followed by zero or more progress events, followed by one event which may be any of error, abort or load, according to the outcome of the operation.

Example: Using progress events in another specification:

This example is informative and does not necessarily illustrate best practice.

FooAPI has a sendAndRetrieve() method, which sends some content via a predefined SMTP server and retrieves some other content via HTTP HEAD from a URI given as a parameter. It specifies two event targets send and receive. Progress events as specified in the ProgressEvent events specification may be dispatched on these targets for the send and receive phases respectively. If any progress events are dispatched, then at least one start event, and one of error, abort, or load must be dispatched on each target. For the send phase, the total attribute of the progress events measures the size of the RFC822 message body. For the receive phase, the total attribute specifies the size of the content to be returned in the HTTP HEAD operation.

Using progress events in Web content

Below is an example of a progress event being used to update a progress bar in an SVG document. Where the size of a download is unknown there is simply an animation.

<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     viewbox="0 0 400 400">
   
   <script type="application/ecmascript"><![CDATA[
   var xlinkNS = "http://www.w3.org/1999/xlink"

   function showImage(imageHref) {
       var image = document.getElementById('myImage');
       image.setAttributeNS(xlinkNS, "href", imageHref);
       image.addEventListener("progress",imageLoadProgress,false);
       image.addEventListener("load",imageLoadComplete,false);
   }           

   function imageLoadProgress(evt) {  
       if (evt.lengthComputable && evt.total != 0) { // we know the size, don't divide by zero
          var progressBar = document.getElementById('progressBar');
          progressBar.setAttribute("width", 100*evt.loaded/evt.total);
          var loadAnimation = document.getElementById('loadAnimation');
          loadAnimation.endElement();
       } else { // we don't know the size and we need not to divide by zero
          var progressBar = document.getElementById('progressBar');
          progressBar.setAttribute("width", 20);
          var loadAnimation = document.getElementById('loadAnimation');
          loadAnimation.beginElement();
       }   
   }
               
   function imageLoadComplete(evt) {
       var progressBar = document.getElementById('progressBar');  
       progressBar.setAttribute("width", 100);
        var loadAnimation = document.getElementById('loadAnimation');
       loadAnimation.endElement();
               
   }
   ]]></script>

   <image id="myImage" xlink:href="imageA.png" width="300" height="400"/>

   <rect onclick="showImage('imageB.png')" width="120" 
         height="30" y="400" id="button" />

   <animate id="loadAnimation" xlink:href="#progressBar" attributeName="x" 
     by="80" dur="1s" begin="button.click" repeatCount="indefinite"/>

   <g id="meter" opacity="0">
     <set attributeName="opacity" to=".7" begin="button.click" end="myImage.load"/>
     <rect width="101" fill="none" height="5" x="5" y="5" stroke-width="1"/>
     <rect id="progressBar" fill="#444" height="4" x="5.5" y="5.5"/>
  </g>
</svg>

Change History

Editor's versions, prior to this First W3C Working Draft:

http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.11
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.10
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.9
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.8
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.7
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.4
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.3
http://dev.w3.org/cvsweb/~checkout~/2006/webapi/progress/Progress.html?rev=1.2
First real version, based somewhat on the draft specification from the SVG Working Group [SVGD]

Various editorial changes and corrections and modifications to the examples are made from draft to draft. These are not noted in the change history.

References

Normative references

[RFC2119] Key words for use in RFCs to indicate Requirement Levels
S Bradner, 1997. The specification for how to use english to specify normativity, as if it were a technical language. Available at http://rfc.net/rfc2119.html
[RFC2616] Hypertext Transfer Protocol -- HTTP/1.1
R Fielding et al., 1999. Available at http://rfc.net/rfc2616.html

Informative references

[IEoP] onProgress event in Internet Explorer
Documentation available at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/07b3e629-a558-4a0e-8307-ca922f56e00c.asp
[SVGD] The proposed progress event in SVG 1.2 Drafts.
Available at http://www.w3.org/TR/2006/CR-SVGMobile12-20060810/svgudom.html#events__ProgressEvent
[WPE] The progress element proposed by WHAT-WG
Ed. I Hickson. Latest version available at http://www.whatwg.org/specs/web-apps/current-work/#progress0
[XHR] XMLHTTPRequest
Ed. A van Kesteren. Latest draft available at http://www.w3.org/TR/xmlhttprequest

Acknowledgements

The editor would like to thank the SVG working group for producing the draft [SVGD] that this was initially based on. The WHATWG's proposed progress element [WPE] and the documentation for Internet Explorer's onProgress implementation [IEoP] were also useful as an initial reference for this specification. In addition, the following individuals' comments have been invaluable in preparing this draft:

Robin Berjon, Jean-Yves Bitterlich, Marcos Caceres, Suresh Chitturi, Alex Danilo, Erik Dahlström, Jean-Claude Duford, Andrew Emmons, João Eiras, Gorm Eriksen, Ian Hickson, Bjoern Hoehrmann, David Håsäther, Bjoern Höhrmann, Anne van Kesteren, Travis Leithead, Aaron Leventhal, Jim Ley, Chris Lilley, Michael Antony Puls, Nandini Ramani, Robert Sayre, Alan Schepers, Doug Schepers, Rich Schwerdtfeger, Lisa Seeman, Andrew Shellshear, Ellen Siegel, Andy Sledd, Maciej Stachowiak, Boris Zbarsky

The editor apologises if anyone has been inadvefrtently left off this list, and welcomes corrections.