nsIIOService

This interface provides a set of URL parsing utility functions. These are provided as a convenience to the programmer and in some cases to improve performance by eliminating intermediate data structures and interfaces.
Inherits from: nsISupports Last changed in Gecko 1.2

This interface duplicates many of the nsIProtocolHandler methods in a protocol handler independent way (For example newURI() inspects the scheme in order to delegate creation of the new URI to the appropriate protocol handler).

Note: nsIIOService may only be used from the main thread.

Implemented by @mozilla.org/network/io-service;1 as a service:

var iOService = Components.classes["@mozilla.org/network/io-service;1"]
                .getService(Components.interfaces.nsIIOService);

Method overview

boolean allowPort(in long aPort, in string aScheme);
ACString extractScheme(in AUTF8String urlString);
unsigned long getProtocolFlags(in string aScheme);
nsIProtocolHandler getProtocolHandler(in string aScheme);
nsIChannel newChannel(in AUTF8String aSpec, in string aOriginCharset, in nsIURI aBaseURI); Obsolete since Gecko 48
nsIChannel newChannel2(in AUTF8String aSpec, in string aOriginCharset, in nsIURI aBaseURI, in nsIDOMNode aLoadingNode, in nsIPrincipal aLoadingPrincipal, in nsIPrincipal aTriggeringPrincipal, in uint32_t aSecurityFlags, in uint32_t aContentPolicyType);
nsIChannel newChannelFromURI(in nsIURI aURI); Obsolete since Gecko 48
nsIChannel newChannelFromURI2(in nsIURI aURI, in nsIDOMNode aLoadingNode, in nsIPrincipal aLoadingPrincipal, in nsIPrincipal aTriggeringPrincipal, in unsigned long aSecurityFlags, in unsigned long aContentPolicyType);
nsIChannel newChannelFromURIWithLoadInfo(in nsIURI aURI, in nsILoadInfo aLoadInfo);
nsIChannel newChannelFromURIWithProxyFlags2(in nsIURI aURI, in nsIURI aProxyURI, in uint32_t aProxyFlags,in nsIDOMNode aLoadingNode, in nsIPrincipal aLoadingPrincipal, in nsIPrincipal aTriggeringPrincipal, in uint32_t aSecurityFlags, in uint32_t aContentPolicyType); Requires Gecko 48
nsIURI newFileURI(in nsIFile aFile);
nsIURI newURI(in AUTF8String aSpec, in string aOriginCharset, in nsIURI aBaseURI);

Attributes

Attribute Type Description
offline boolean Returns true if networking is in "offline" mode. When in offline mode, attempts to access the network will fail (although this does not necessarily correlate with whether there is actually a network available -- that's hard to detect without causing the dialer to come up). Observers will be notified of changes to this attribute.

Methods

allowPort()

Checks if a port number is banned. This involves consulting a list of unsafe ports, corresponding to network services that may be easily exploitable. If the given port is considered unsafe, then the protocol handler (corresponding to aScheme) will be asked whether it wishes to override the IO service's decision to block the port. This gives the protocol handler ultimate control over its own security policy while ensuring reasonable, default protection.

Is similar to nsIProtocolHandler.allowPort().

boolean allowPort(
  in long aPort,
  in string aScheme
);
Parameters
aPort
The port to check
aScheme
The scheme for the protocol handler that could override the IOService's decision.
Return value

true if the port is allowed, false otherwise.

extractScheme()

Utility to extract the scheme from a URL string, consistently and according to spec (see RFC 3986).

Note:  Most URL parsing is done via nsIURI, and in fact the scheme can also be extracted from a URL string via nsIURI. This method is provided purely as an optimization.

ACString extractScheme(
  in AUTF8String urlString
);
Parameters
urlString
The string for the URL to extract the scheme from.
Return value

A string corresponding to the scheme.

Exceptions thrown
NS_ERROR_MALFORMED_URI
If URL string is not of the right form.

getProtocolFlags()

Returns the protocol flags for a given scheme.

unsigned long getProtocolFlags(
  in string aScheme
);
Parameters
aScheme
The scheme for which to get the protocol flags.
Return value

The value of the protocolFlags attribute for the corresponding nsIProtocolHandler.

getProtocolHandler()

Returns a protocol handler for a given URI scheme.

nsIProtocolHandler getProtocolHandler(
  in string aScheme
);
Parameters
aScheme
The URI scheme for which to get a protocol handler.
Return value

An nsIProtocolHandler for the scheme.

newChannel()

Obsolete since Gecko 48
A shortcut method to avoid repeated calls of newURI() and newChannelFromURI().

nsIChannel newChannel(
  in AUTF8String aSpec,
  in string aOriginCharset,
  in nsIURI aBaseURI
);
Parameters
aSpec
The spec for the desired uri.
aOriginCharset
The charset for the uri. May be null.
aBaseURI
The base URI for the spec. May be null. If aSpec is an absolute URL, this parameter is ignored.
Return value

Returns an nsIChannel based on aSpec and aBaseURI.

newChannel2()

A shortcut method to avoid repeated calls of newURI() and newChannelFromURI().

nsIChannel newChannel2(
   in AUTF8String aSpec,
   in string aOriginCharset,
   in nsIURI aBaseURI,
   in nsIDOMNode aLoadingNode,
   in nsIPrincipal aLoadingPrincipal,
   in nsIPrincipal aTriggeringPrincipal,
   in uint32_t aSecurityFlags,
   in uint32_t aContentPolicyType,

);
Parameters
aSpec
The spec for the desired uri.
aOriginCharset
The charset for the uri. May be null.
aBaseURI
The base URI for the spec. May be null. If aSpec is an absolute URL, this parameter is ignored.
aLoadingNode
The loadingDocument of the channel. This is the document/element that will get access to the result of this request. For example for an image load, it's the document in which the image will be loaded. And for a CSS stylesheet it's the document whose rendering will be affected by the stylesheet. If possible, pass in the element which is performing the load. But if the load is coming from a JS API (such as XMLHttpRequest) or if the load might be coalesced across multiple elements (such as for ) then pass in the Document node instead. For loads that are not related to any document, such as loads coming from addons or internal browser features, use null here.
aLoadingPrincipal
The loadingPrincipal of the channel. The principal of the document where the result of this request will be used. This defaults to the principal of aLoadingNode, so when aLoadingNode is passed this can be left as null. However for loads where aLoadingNode is null this argument must be passed. For example for loads from a WebWorker, pass the principal of that worker. For loads from an addon or from internal browser features, pass the system principal. This principal should almost always be the system principal if aLoadingNode is null. The only exception to this is for loads from WebWorkers since they don't have any nodes to be passed as aLoadingNode. Please note, aLoadingPrincipal is *not* the principal of the resource being loaded. But rather the principal of the context where the resource will be used.
aTriggeringPrincipal
The triggeringPrincipal of the load. The triggeringPrincipal is the principal of the resource that caused this particular URL to be loaded. Most likely the triggeringPrincipal and the loadingPrincipal are identical, in which case the triggeringPrincipal can be left out. In some cases the loadingPrincipal and the triggeringPrincipal are different however, e.g. a stylesheet may import a subresource. In that case the principal of the stylesheet which contains the import command is the triggeringPrincipal, and the principal of the document whose rendering is affected is the loadingPrincipal.
aSecurityFlags
The securityFlags of the channel. Any of the securityflags defined in nsILoadInfo.
aContentPolicyType
The contentPolicyType of the channel. Any of the content types defined in nsIContentPolicyBase.idl.
Return value

Returns an nsIChannel based on aSpec and aBaseURI.

newChannelFromURI()

Obsolete since Gecko 48
Creates a channel for a given URI.

nsIChannel newChannelFromURI(
  in nsIURI aURI
);
Parameters
aURI
An nsIURI from which to make a channel.
Return value

An nsIChannel for the uri.

newChannelFromURI2()

Creates a channel for a given URI.

nsIChannel newChannelFromURI2(
  in nsIURI aURI,
  in nsIDOMNode aLoadingNode,
  in nsIPrincipal aLoadingPrincipal,
  in nsIPrincipal aTriggeringPrincipal,
  in unsigned long aSecurityFlags,
  in unsigned long aContentPolicyType
);

Please note, if you provide both a loadingNode and a loadingPrincipal, then loadingPrincipal must be equal to loadingNode->NodePrincipal(). But less error prone is to just supply a loadingNode.

Keep in mind that URIs coming from a webpage should never use the systemPrincipal as the loadingPrincipal.

Parameters
aURI
An nsIURI from which to make a channel.
aLoadingNode
The loadingDocument of the channel.
The element or document where the result of this request will be used. This is the document/element that will get access to the result of this request. For example for an image load, it's the document in which the image will be loaded. And for a CSS stylesheet it's the document whose rendering will be affected by the stylesheet. If possible, pass in the element which is performing the load. But if the load is coming from a JS API (such as XMLHttpRequest) or if the load might be coalesced across multiple elements (such as for <img>) then pass in the Document node instead.

For loads that are not related to any document, such as loads coming from addons or internal browser features, use null here.
aLoadingPrincipal                                                     
The loadingPrincipal of the channel.

The principal of the document where the result of this request will be used.

This defaults to the principal of aLoadingNode, so when aLoadingNode is passed this can be left as null. However for loads where aLoadingNode is null this argument must be passed.  For example for loads from a WebWorker, pass the principal of that worker. For loads from an addon or from internal browser features, pass the system principal. This principal should almost always be the system principal if aLoadingNode is null. The only exception to this is for loads from WebWorkers since they don't have any nodes to be passed as aLoadingNode. Please note, aLoadingPrincipal is *not* the principal of the resource being loaded. But rather the principal of the context here the resource will be used.
aTriggeringPrincipal                                                  
The triggeringPrincipal of the load.

The triggeringPrincipal is the principal of the resource that caused this particular URL to be loaded. Most likely the triggeringPrincipal and the loadingPrincipal are identical, in which case the triggeringPrincipal can be left out. In some cases the loadingPrincipal and the triggeringPrincipal are different however, e.g. a stylesheet may import a subresource. In that case the principal of the stylesheet which contains the import command is the triggeringPrincipal, and the principal of the document whose rendering is affected is the loadingPrincipal.
aSecurityFlags                                                        
The securityFlags of the channel. Any of the securityflags defined in nsILoadInfo.idl
aContentPolicyType                                                        
The contentPolicyType of the channel. Any of the content types defined in nsIContentPolicy.idl              
Return value

An nsIChannel for the uri.

newChannelFromURIWithLoadInfo()

Creates a channel for a given URI. Equivalent to newChannelFromURI2(aURI, aLoadingNode, ...)

nsIChannel newChannelFromURIWithLoadInfo(
  in nsIURI aURI,
  in nsILoadInfo aLoadInfo
);
Parameters
aURI
An nsIURI from which to make a channel.
aLoadInfo
A load info object.
Return value

An nsIChannel for the uri.

newChannelFromURIWithProxyFlags2()

Requires Gecko 48
Creates a channel for a given URI. Equivalent to newChannelFromURI2(aURI, aLoadingNode, ...)

nsIChannel newChannelFromURIWithProxyFlags2(
  in nsIURI aURI,
  in nsIURI aProxyURI,
  in uint32_t aProxyFlags,
  in nsIDOMNode aLoadingNode,
  in nsIPrincipal aLoadingPrincipal,
  in nsIPrincipal aTriggeringPrincipal,
  in uint32_t aSecurityFlags,
  in uint32_t aContentPolicyType
);
Parameters
aURI
An nsIURI from which to make a channel.
aProxyURI
nsIURI to use for proxy resolution. Can be null in which case aURI is used.
aProxyFlags
Flags from nsIProtocolProxyService to use when resolving proxies for this new channel.
aLoadingNode
The loadingDocument of the channel. The element or document where the result of this request will be used. This is the document/element that will get access to the result of this request. For example for an image load, it's the document in which the image will be loaded. And for a CSS stylesheet it's the document whose rendering will be affected by the stylesheet. If possible, pass in the element which is performing the load. But if the load is coming from a JS API (such as XMLHttpRequest) or if the load might be coalesced across multiple elements (such as for ) then pass in the Document node instead. For loads that are not related to any document, such as loads coming from addons or internal browser features, use null here.
aLoadingPrincipal
The loadingPrincipal of the channel. The principal of the document where the result of this request will be used. This defaults to the principal of aLoadingNode, so when aLoadingNode is passed this can be left as null. However for loads where aLoadingNode is null this argument must be passed. For example for loads from a WebWorker, pass the principal of that worker. For loads from an addon or from internal browser features, pass the system principal. This principal should almost always be the system principal if aLoadingNode is null. The only exception to this is for loads from WebWorkers since they don't have any nodes to be passed as aLoadingNode. Please note, aLoadingPrincipal is *not* the principal of the resource being loaded. But rather the principal of the context where the resource will be used.
aTriggeringPrincipal
The triggeringPrincipal of the load. The triggeringPrincipal is the principal of the resource that caused this particular URL to be loaded. Most likely the triggeringPrincipal and the loadingPrincipal are identical, in which case the triggeringPrincipal can be left out. In some cases the loadingPrincipal and the triggeringPrincipal are different however, e.g. a stylesheet may import a subresource. In that case the principal of the stylesheet which contains the import command is the triggeringPrincipal, and the principal of the document whose rendering is affected is the loadingPrincipal.
aSecurityFlags
The securityFlags of the channel. Any of the securityflags defined in nsILoadInfo.
aContentPolicyType
The contentPolicyType of the channel. Any of the content types defined in nsIContentPolicy.
Return value

An nsIChannel for the uri.

newFileURI()

This method constructs a new URI from a nsIFile.

nsIURI newFileURI(
  in nsIFile aFile
);
Parameters
aFile
The nsIFile whose URI is desired.
Return value

An nsIURI corresponding to the file.

newURI()

This method constructs a new URI by determining the scheme of the URI spec, and then delegating the construction of the URI to the protocol handler for that scheme. QueryInterface can be used on the resulting URI object to obtain a more specific type of URI.

nsIURI newURI(
  in AUTF8String aSpec,
  in string aOriginCharset,
  in nsIURI aBaseURI
);
Parameters
aSpec
The spec for the desired uri.
aOriginCharset
The charset for the uri. May be null.
aBaseURI
The base URI for the spec. May be null. If aSpec is an absolute URL, this parameter is ignored.
Return value

An nsIURI object corresponding to aSpec and aBaseURI.

Exceptions thrown
NS_ERROR_MALFORMED_URI
If URI does not begin with a valid scheme (as defined by RFC 3986) followed by a colon.

Remarks

Note:  The proper way to create a new nsIURI is with newURI() method defined above. Do NOT create a new nsIURI with createInstance().

This was a frozen interface see bug 157131 for details. From Gecko 2.0 interfaces are no longer frozen.

See also

Document Tags and Contributors

 Last updated by: luciancor,