nsIWebProgressListener

This interface is implemented by clients wishing to listen in on the progress associated with the loading of asynchronous requests in the context of a nsIWebProgress instance as well as any child nsIWebProgress instances.
Inherits from: nsISupports Last changed in Gecko 15 (Firefox 15 / Thunderbird 15 / SeaMonkey 2.12)

nsIWebProgress describes the parent-child relationship of nsIWebProgress instances.

Method overview

void onLocationChange(in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI aLocation, [optional] in unsigned long aFlags);
void onProgressChange(in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress);
void onSecurityChange(in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aState);
void onStateChange(in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus);
void onStatusChange(in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage);

Constants

State transition flags

These flags indicate the various states that requests may transition through as they are being loaded. These flags are mutually exclusive.

For any given request, onStateChange() is called once with the STATE_START flag, zero or more times with the STATE_TRANSFERRING flag or once with the STATE_REDIRECTING flag, and then finally once with the STATE_STOP flag.

Note: For document requests, a second STATE_STOP is generated (see the description of STATE_IS_WINDOW for more details).

Constant Value Description
STATE_START 0x00000001 This flag indicates the start of a request. This flag is set when a request is initiated. The request is complete when onStateChange() is called for the same request with the STATE_STOP flag set.
STATE_REDIRECTING 0x00000002 This flag indicates that a request is being redirected. The request passed to onStateChange() is the request that is being redirected. When a redirect occurs, a new request is generated automatically to process the new request. Expect a corresponding STATE_START event for the new request, and a STATE_STOP for the redirected request.
STATE_TRANSFERRING 0x00000004 This flag indicates that data for a request is being transferred to an end consumer. This flag indicates that the request has been targeted, and that the user may start seeing content corresponding to the request.
STATE_NEGOTIATING 0x00000008 This flag is not used.
STATE_STOP 0x00000010 This flag indicates the completion of a request. The aStatus parameter to onStateChange() indicates the final status of the request.

State type flags

These flags further describe the entity for which the state transition is occurring. These flags are not mutually exclusive (that is an onStateChange() event may indicate some combination of these flags).

Constant Value Description
STATE_IS_REQUEST 0x00010000 This flag indicates that the state transition is for a request, which includes but is not limited to document requests. (See below for a description of document requests.) Other types of requests, such as requests for inline content (for example images and stylesheets) are considered normal requests.
STATE_IS_DOCUMENT 0x00020000

This flag indicates that the state transition is for a document request. This flag is set in addition to STATE_IS_REQUEST. A document request supports the nsIChannel interface and its loadFlags attribute includes the nsIChannel::LOAD_DOCUMENT_URI flag.

A document request does not complete until all requests associated with the loading of its corresponding document have completed. This includes other document requests (for example corresponding to HTML <iframe> elements). The document corresponding to a document request is available via the DOMWindow attribute of onStateChange()'s aWebProgress parameter.
STATE_IS_NETWORK 0x00040000

This flag indicates that the state transition corresponds to the start or stop of activity in the indicated nsIWebProgress instance. This flag is accompanied by either STATE_START or STATE_STOP, and it may be combined with other State Type Flags.

Unlike STATE_IS_WINDOW, this flag is only set when activity within the nsIWebProgress instance being observed starts or stops. If activity only occurs in a child nsIWebProgress instance, then this flag will be set to indicate the start and stop of that activity.

For example, in the case of navigation within a single frame of a HTML frameset, a nsIWebProgressListener instance attached to the nsIWebProgress of the frameset window will receive onStateChange() calls with the STATE_IS_NETWORK flag set to indicate the start and stop of said navigation. In other words, an observer of an outer window can determine when activity, that may be constrained to a child window or set of child windows, starts and stops.
STATE_IS_WINDOW 0x00080000

This flag indicates that the state transition corresponds to the start or stop of activity in the indicated nsIWebProgress instance. This flag is accompanied by either STATE_START or STATE_STOP, and it may be combined with other State Type Flags.

This flag is similar to STATE_IS_DOCUMENT. However, when a document request completes, two onStateChange() calls with STATE_STOP are generated. The document request is passed as aRequest to both calls. The first has STATE_IS_REQUEST and STATE_IS_DOCUMENT set, and the second has the STATE_IS_WINDOW flag set (and possibly the STATE_IS_NETWORK flag set as well -- see above for a description of when the STATE_IS_NETWORK flag may be set). This second STATE_STOP event may be useful as a way to partition the work that occurs when a document request completes.

State modifier flags

These flags further describe the entity for which the state transition is occurring. These flags are NOT mutually exclusive (that is an onStateChange() event may indicate some combination of these flags).

Constant Value Description
STATE_RESTORING 0x01000000 This flag indicates that the state transition corresponds to the start or stop of activity for restoring a previously-rendered presentation. As such, there is no actual network activity associated with this request, and any modifications made to the document or presentation when it was originally loaded will still be present.

State security flags

These flags describe the security state reported by a call to the onSecurityChange() method. These flags are mutually exclusive.

Constant Value Description
STATE_IS_INSECURE 0x00000004 This flag indicates that the data corresponding to the request was received over an insecure channel.
STATE_IS_BROKEN 0x00000001 This flag indicates an unknown security state. This may mean that the request is being loaded as part of a page in which some content was received over an insecure channel.
STATE_IS_SECURE 0x00000002 This flag indicates that the data corresponding to the request was received over a secure channel. The degree of security is expressed by STATE_SECURE_HIGH, STATE_SECURE_MED, or STATE_SECURE_LOW.

Security strength flags

These flags describe the security strength and accompany STATE_IS_SECURE in a call to the onSecurityChange() method. These flags are mutually exclusive.

These flags are not meant to provide a precise description of data transfer security. These are instead intended as a rough indicator that may be used to, for example, color code a security indicator or otherwise provide basic data transfer security feedback to the user.

Constant Value Description
STATE_SECURE_HIGH 0x00040000 This flag indicates a high degree of security.
STATE_SECURE_MED 0x00010000 This flag indicates a medium degree of security.
STATE_SECURE_LOW 0x00020000 This flag indicates a low degree of security.

State identity flags

These flags describe the level of identity verification in a call to the onSecurityChange() method.

Constant Value Description
STATE_IDENTITY_EV_TOPLEVEL 0x00100000

State bits for EV == Extended Validation == High Assurance.

The topmost document uses an EV cert.

Location Change flags

Constant Value Description
LOCATION_CHANGE_SAME_DOCUMENT 0x00000001 This flag is on when aWebProgress did not load a new document. For example, the location change is due to an anchor scroll or a pushState/popState/replaceState.
LOCATION_CHANGE_ERROR_PAGE 0x00000002 This flag indicates that aWebProgress redirected from the requested document to an internal page to show error status, such as about:neterror, about:certerror and so on.

Methods

onLocationChange()

Called when the location of the window being watched changes. This is not when a load is requested, but rather once it is verified that the load is going to occur in the given window. For instance, a load that starts in a window might send progress and status messages for the new site, but it will not send the onLocationChange until we are sure that we are loading this new page here. Another example: the loading of a PDF or Flash file might not not send onLocationChange if the document is handed off to a third-party tool rather than being loaded into the browser.

void onLocationChange(
  in nsIWebProgress aWebProgress,
  in nsIRequest aRequest,
  in nsIURI aLocation,
  [optional] in unsigned long aFlags
);
Parameters
aWebProgress
The nsIWebProgress instance that fired the notification.
aRequest
The associated nsIRequest. This may be null in some cases.
aLocation
The URI of the location that is being loaded.
aFlags
This is a value which explains the situation or the reason why the location has changed. Optional from Gecko 11

onProgressChange()

Notification that the progress has changed for one of the requests associated with aWebProgress. Progress totals are reset to zero when all requests in aWebProgress complete (corresponding to onStateChange() being called with aStateFlags including the STATE_STOP and STATE_IS_WINDOW flags).

Note: If any progress value is unknown, or if its value would exceed the maximum value of type long, then its value is replaced with -1.

Note: If the object also implements nsIWebProgressListener2 and the caller knows about that interface, this function will not be called. Instead, nsIWebProgressListener2.onProgressChange64() will be called.

void onProgressChange(
  in nsIWebProgress aWebProgress,
  in nsIRequest aRequest,
  in long aCurSelfProgress,
  in long aMaxSelfProgress,
  in long aCurTotalProgress,
  in long aMaxTotalProgress
);
Parameters
aWebProgress
The nsIWebProgress instance that fired the notification.
aRequest
The nsIRequest that has new progress.
aCurSelfProgress
The current progress for aRequest.
aMaxSelfProgress
The maximum progress for aRequest.
aCurTotalProgress
The current progress for all requests associated with aWebProgress.
aMaxTotalProgress
The total progress for all requests associated with aWebProgress.

onSecurityChange()

Notification called for security progress. This method will be called on security transitions (for example HTTP -> HTTPS, HTTPS -> HTTP, FOO -> HTTPS) and after document load completion. It might also be called if an error occurs during network loading.

Note: These notifications will only occur if a security package is installed.

void onSecurityChange(
  in nsIWebProgress aWebProgress,
  in nsIRequest aRequest,
  in unsigned long aState
);
Parameters
aWebProgress
The nsIWebProgress instance that fired the notification.
aRequest
The nsIRequest that has new security state.
aState
A value composed of the Security State Flags and the Security Strength Flags listed above. Any undefined bits are reserved for future use.

onStateChange()

Notification indicating the state has changed for one of the requests associated with aWebProgress.

void onStateChange(
  in nsIWebProgress aWebProgress,
  in nsIRequest aRequest,
  in unsigned long aStateFlags,
  in nsresult aStatus
);
Parameters
aWebProgress
The nsIWebProgress instance that fired the notification.
aRequest
The nsIRequest that has changed state. This parameter may be null.
aStateFlags
Flags indicating the new state. This value is a combination of one of the State Transition Flags and one or more of the State Type Flags defined above. Any undefined bits are reserved for future use.
aStatus
Error status code associated with the state change. This parameter should be ignored unless aStateFlags includes the STATE_STOP bit. The status code indicates success or failure of the request associated with the state change.
Note: aStatus may be a success code even for server generated errors, such as the HTTP 404 File Not Found error. In such cases, the request itself should be queried for extended error information (for example for HTTP requests see nsIHttpChannel.

onStatusChange()

Notification that the status of a request has changed. The status message is intended to be displayed to the user (for example, in the status bar of the browser).

void onStatusChange(
  in nsIWebProgress aWebProgress,
  in nsIRequest aRequest,
  in nsresult aStatus,
  in wstring aMessage
);
Parameters
aWebProgress
The nsIWebProgress instance that fired the notification.
aRequest
The nsIRequest that has new status.
aStatus

This value is not an error code. Instead, it is a numeric value that indicates the current status of the request. This interface does not define the set of possible status codes.

Note: Some status values are defined by nsITransport and nsISocketTransport.
aMessage
Localized text corresponding to aStatus.

Example

The nsIWebProgressListener for each tab:

  • gBrowser.mTabProgressListener can be used in the parent to listen for most nsIWebProgressListener events, but in browser code, will not have access to the nsIWebProgress's DOMWindow property. See the example below on how to use an nsIWebProgressListener in a frame script to get access to the DOM Window.
  • This example here shows you how to detect what flags are popping up where. It puts the messages into the Browser Console.
    /**
     * This example assumes that it's being run within the context of a browser
     * window, and uses a frame script that will be loaded into every
     * browser tab.
     */
    
    let mm = window.getGroupMessageManager("browsers");
    mm.loadFrameScript("chrome://path/to/some/example-framescript.js", true);
    mm.addMessageListener("MyAddonMessage", (message) => {
      let data = message.data;
      let browser = message.target;
      let outerWindowID = data.outerWindowID;
      // browser is now the <xul:browser> that saw the web progress activity
      // occur. If you need to hold onto an identifier for whichever frame
      // (be it the top-level, or a subframe) was using the WebProgress, use
      // the outerWindowID. This can be communicated back down to the content
      // in order to do send a command to a particular DOM Window.
    
      Cu.reportError("Saw: " + data.name + " -- passing: " + JSON.stringify(data));
    });
    
    /**
     * Below is the contents of example-framescript.js
     */
    
    const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
    
    Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    
    var myListener = {
        QueryInterface: XPCOMUtils.generateQI(["nsIWebProgressListener",
                                               "nsISupportsWeakReference"]),
    
        onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
            let win = aWebProgress.DOMWindow;
            let outerWindowID = win.QueryInterface(Ci.nsIInterfaceRequestor)
                                   .getInterface(Ci.nsIDOMWindowUtils)
                                   .outerWindowID;
    
            sendAsyncMessage("MyAddonMessage", {
                name: "onStateChange",
                flag: aFlag,
                status: aStatus,
            });
        },
    
        onLocationChange: function(aProgress, aRequest, aURI, aFlag) {
            let win = aWebProgress.DOMWindow;
            let outerWindowID = win.QueryInterface(Ci.nsIInterfaceRequestor)
                                   .getInterface(Ci.nsIDOMWindowUtils)
                                   .outerWindowID;
    
            sendAsyncMessage("MyAddonMessage", {
                name: "onLocationChange",
                outerWindowID: outerWindowID,
                uri: aURI.spec,
                charSet: aURI.charSet,
                flag: aFlag,
            });
        },
    
        // For definitions of the remaining functions see related documentation
        onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) {},
        onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {},
        onSecurityChange: function(aWebProgress, aRequest, aState) {}
    }
    
    
    let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"]
                   .createInstance(Ci.nsIWebProgress);
    filter.addProgressListener(myListener, Ci.nsIWebProgress.NOTIFY_ALL);
    
    let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIWebProgress);
    webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL);
  • XULBrowserWindow.onStateChange (etc.)
  • reporterListener.onLocationChange

See also