Revision 371111 of nsIChannel

  • Revision slug: XPCOM_Interface_Reference/nsIChannel
  • Revision title: nsIChannel
  • Revision id: 371111
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment

Revision Content

{{ IFSummary("netwerk/base/public/nsIChannel.idl", "nsIRequest", "Scriptable", "22.0", "This interface allows clients to construct 'GET' requests for specific protocols, and manage them in a uniform way.") }}

Once a channel is created (via {{ ifmethod("nsIIOService","newChannel") }}), parameters for that request may be set by using the channel attributes, or by calling QueryInterface() to retrieve a subclass of nsIChannel for protocol-specific parameters. Then, the URI can be fetched by calling {{ manch("open") }} or {{ manch("asyncOpen") }}. After a request has been completed, the channel is still valid for accessing protocol-specific results. For example, QueryInterfacing to {{ interface("nsIHttpChannel") }} allows response headers to be retrieved for the corresponding http transaction.

This interface must be used only from the XPCOM main thread.

Implemented by {{ source("netwerk/base/src/nsBaseChannel.cpp") }} and {{ source("netwerk/protocol/http/src/nsHttpChannel.cpp") }}.

Method overview

void asyncOpen(in nsIStreamListener aListener, in nsISupports aContext);
nsIInputStream open();

Attributes

Attribute Type Description
contentCharset ACString

The character set of the channel's content if available and if applicable. This attribute only applies to textual data.

Setting contentCharset before the channel has been opened provides a hint to the channel on what the charset should be. The behavior is similar to contentType.

The value of the contentCharset attribute is a mixed case string.
contentLength int64_t

The length of the data associated with the channel if available. A value of -1 indicates that the content length is unknown.

{{gecko_callout_heading("19.0")}}

This parameter changed from long to int64_t. In earlier versions callers could get the "content-length" property as 64-bit value by QueryInterfacing the channel to nsIPropertyBag2, if that interface is exposed by the channel.

contentType ACString

The MIME type of the channel's content if available.

{{ note("The content type can often be wrongly specified (For example wrong file extension, wrong MIME type, wrong document type stored on a server and so on.), and the caller most likely wants to verify with the actual data.") }}

Setting contentType before the channel has been opened provides a hint to the channel as to what the MIME type is. The channel may ignore this hint in deciding on the actual MIME type that it will report.

Setting contentType after onStartRequest has been fired or after {{ manch("open") }} is called will override the type determined by the channel.

Setting contentType between the time that {{ manch("asyncOpen") }} is called and the time when onStartRequest is fired has undefined behavior at this time.

The value of the contentType attribute is a lowercase string. A value assigned to this attribute will be parsed and normalized as follows:

  1. Any parameters (delimited with a ';') will be stripped.
  2. If a charset parameter is given, then its value will replace the the contentCharset attribute of the channel.
  3. The stripped contentType will be lowercased. Any implementation of nsIChannel must follow these rules.
notificationCallbacks {{ Interface("nsIInterfaceRequestor") }}

The notification callbacks for the channel. This is set by clients, who wish to provide a means to receive progress, status and protocol-specific notifications. If this value is null, the channel implementation may use the notification callbacks from its load group. The channel may also query the notification callbacks from its load group if its notification callbacks do not supply the requested interface.

Interfaces commonly requested include: {{ interface("nsIProgressEventSink") }}, {{ interface("nsIPrompt") }}, and {{ interface("nsIAuthPrompt") }} / {{ interface("nsIAuthPrompt2") }}.

When the channel is done, it must not continue holding references to this object.

{{ note("A channel implementation should take care when \"caching\" an interface pointer queried from its notification callbacks. If the notification callbacks are changed, then a cached interface pointer may become invalid and may therefore need to be re-queried.") }}
originalURI {{ Interface("nsIURI") }}

The original URI used to construct the channel. This is used in the case of a redirect or URI "resolution" (For example resolving a resource: URI to a file: URI) so that the original pre-redirect URI can still be obtained.   This is also used as the RI of the document resulting from the channel, unless the channel has the LOAD_REPLACE flag set.  This is never null. Attempts to set it to null must throw.

{{NoteStart()}}This is distinctly different from the http Referer (referring URI), which is typically the page that contained the original URI (accessible from {{interface("nsIHttpChannel")}}.) {{NoteEnd()}}
owner {{ Interface("nsISupports") }}

The owner, corresponding to the entity that is responsible for this channel. Used by the security manager to grant or deny privileges to mobile code loaded from this channel.

{{ note("This is a strong reference to the owner, so if the owner is also holding a strong reference to the channel, care must be taken to explicitly drop its reference to the channel.") }}
securityInfo {{ Interface("nsISupports") }} Transport-level security information (if any, else null) corresponding to the channel, normally presented through the interfaces {{ Interface("nsITransportSecurityInfo") }} and {{ Interface("nsISSLStatusProvider") }} Read only.
URI {{ Interface("nsIURI") }} The URI corresponding to the channel. Its value is immutable. Read only.

Constants

Channel specific load flags:

Bits 23-31 are reserved for future use by this interface or one of its derivatives (For an example see {{ interface("nsICachingChannel") }}).

Constant Value Description
LOAD_DOCUMENT_URI 16 Set (for example by the docshell) to indicate whether or not the channel corresponds to a document URI.
LOAD_RETARGETED_DOCUMENT_URI 17 If the end consumer for this load has been retargeted after discovering its content, this flag will be set:
LOAD_REPLACE 18

This flag is set to indicate that this channel is replacing another channel. This means that:

  1. The stream listener this channel will be notifying was initially passed to the {{ manch("asyncOpen") }} method of some other channel
  2. This channel's URI is a better identifier of the resource being accessed than this channel's originalURI.
This flag can be set, for example, for redirects or for cases when a single channel has multiple parts to it (and thus can follow onStopRequest with another onStartRequest/onStopRequest pair, each pair for a different request).
LOAD_INITIAL_DOCUMENT_URI 19 Set (for example by the docshell) to indicate whether or not the channel corresponds to an initial document URI load (for example link click).
LOAD_TARGETED 20 Set (For example by the URILoader) to indicate whether or not the end consumer for this load has been determined.
LOAD_CALL_CONTENT_SNIFFERS 21

If this flag is set, the channel should call the content sniffers as described in {{ source("netwerk/build/nsNetCID.h") }} about NS_CONTENT_SNIFFER_CATEGORY.{{ gecko_minversion_inline("1.8") }}

{{ note("Channels may ignore this flag; however, new channel implementations should only do so with good reason.") }}
LOAD_CLASSIFY_URI 22 This flag tells the channel to use URI classifier service to check the URI when opening the channel. {{ gecko_minversion_inline("2.0") }}

Methods

asyncOpen()

Asynchronously open this channel. Data is fed to the specified stream listener as it becomes available. The stream listener's methods are called on the thread that calls asyncOpen and are not called until after asyncOpen returns. If asyncOpen returns successfully, the channel promises to call at least onStartRequest and onStopRequest.

If the {{ interface("nsIRequest") }} object passed to the stream listener's methods is not this channel, an appropriate onChannelRedirect notification needs to be sent to the notification callbacks before onStartRequest is called. Once onStartRequest is called, all following method calls on aListener will get the request that was passed to onStartRequest.

If the channel's and loadgroup's notification callbacks do not provide an {{ interface("nsIChannelEventSink") }} when onChannelRedirect would be called, that's equivalent to having called onChannelRedirect.

If asyncOpen returns successfully, the channel is responsible for keeping itself alive until it has called onStopRequest on aListener or called onChannelRedirect.

Implementations are allowed to synchronously add themselves to the associated load group (if any).

void asyncOpen(
  in nsIStreamListener aListener,
  in nsISupports aContext
);
Parameters
aListener
The {{ interface("nsIStreamListener") }} implementation.
aContext
An opaque parameter forwarded to aListener's methods.
Exceptions thrown
NS_ERROR_ALREADY_OPENED
If the channel is reopened
NS_ERROR_PORT_ACCESS_NOT_ALLOWED
If the specified port is in the nsIOService forbidden port list.

open()

Synchronously open the channel.

{{note("Extensions should not call this method, because it tends to fail when a request is redirected, rather than redirecting transparently")}}{{note("nsIChannel implementations are not required to implement this method. Moreover, since this method may block the calling thread, it should not be called on a thread that processes UI events. Like any other nsIChannel method it must not be called on any thread other than the XPCOM main thread.") }}

nsIInputStream open();
Parameters

None.

Return value

Blocking input stream to the channel's data.

Exceptions thrown
NS_ERROR_IN_PROGRESS
If the channel is reopened.

See also

  • {{ Interface("nsIChannelEventSink") }} for onChannelRedirect

Revision Source

<p>{{ IFSummary("netwerk/base/public/nsIChannel.idl", "nsIRequest", "Scriptable", "22.0", "This interface allows clients to construct 'GET' requests for specific protocols, and manage them in a uniform way.") }}</p>
<p>Once a channel is created (via {{ ifmethod("nsIIOService","newChannel") }}), parameters for that request may be set by using the channel attributes, or by calling QueryInterface() to retrieve a subclass of <code>nsIChannel</code> for protocol-specific parameters. Then, the <code>URI</code> can be fetched by calling {{ manch("open") }} or {{ manch("asyncOpen") }}. After a request has been completed, the channel is still valid for accessing protocol-specific results. For example, QueryInterfacing to {{ interface("nsIHttpChannel") }} allows response headers to be retrieved for the corresponding http transaction.</p>
<p>This interface must be used only from the XPCOM main thread.</p>
<p>Implemented by {{ source("netwerk/base/src/nsBaseChannel.cpp") }} and {{ source("netwerk/protocol/http/src/nsHttpChannel.cpp") }}.</p>
<h2 id="Method_overview" name="Method_overview">Method overview</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td><code>void <a href="#asyncOpen()">asyncOpen</a>(in nsIStreamListener aListener, in nsISupports aContext);</code></td>
    </tr>
    <tr>
      <td><code>nsIInputStream <a href="#open()">open</a>();</code></td>
    </tr>
  </tbody>
</table>
<h2 id="Attributes" name="Attributes">Attributes</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header">Attribute</td>
      <td class="header">Type</td>
      <td class="header">Description</td>
    </tr>
    <tr>
      <td><code>contentCharset</code></td>
      <td><code><a href="/en/ACString" title="en/ACString">ACString</a></code></td>
      <td>
        <p>The character set of the channel's content if available and if applicable. This attribute only applies to textual data.</p>
        <p>Setting contentCharset before the channel has been opened provides a hint to the channel on what the charset should be. The behavior is similar to contentType.</p>
        The value of the contentCharset attribute is a mixed case string.</td>
    </tr>
    <tr>
      <td><code>contentLength</code></td>
      <td><code>int64_t</code></td>
      <td>
        <p>The length of the data associated with the channel if available. A value of -1 indicates that the content length is unknown.</p>
        <div class="geckoVersionNote">
          <p>{{gecko_callout_heading("19.0")}}</p>
          <p>This parameter changed from <code>long</code> to <code>int64_t</code>. In earlier versions callers could get the "content-length" property as 64-bit value by QueryInterfacing the channel to <code><a class="external" href="http://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIPropertyBag2">nsIPropertyBag2</a></code>, if that interface is exposed by the channel.</p>
        </div>
      </td>
    </tr>
    <tr>
      <td><code>contentType</code></td>
      <td><code><a href="/en/ACString" title="en/ACString">ACString</a></code></td>
      <td>
        <p>The MIME type of the channel's content if available.</p>
        <p>{{ note("The content type can often be wrongly specified (For example wrong file extension, wrong MIME type, wrong document type stored on a server and so on.), and the caller most likely wants to verify with the actual data.") }}</p>
        <p>Setting contentType before the channel has been opened provides a hint to the channel as to what the MIME type is. The channel may ignore this hint in deciding on the actual MIME type that it will report.</p>
        <p>Setting contentType after onStartRequest has been fired or after {{ manch("open") }} is called will override the type determined by the channel.</p>
        <p>Setting contentType between the time that {{ manch("asyncOpen") }} is called and the time when onStartRequest is fired has undefined behavior at this time.</p>
        <p>The value of the contentType attribute is a lowercase string. A value assigned to this attribute will be parsed and normalized as follows:</p>
        <ol>
          <li>Any parameters (delimited with a ';') will be stripped.</li>
          <li>If a charset parameter is given, then its value will replace the the contentCharset attribute of the channel.</li>
          <li>The stripped contentType will be lowercased. Any implementation of <code>nsIChannel</code> must follow these rules.</li>
        </ol>
      </td>
    </tr>
    <tr>
      <td><code>notificationCallbacks</code></td>
      <td><code>{{ Interface("nsIInterfaceRequestor") }}</code></td>
      <td>
        <p>The notification callbacks for the channel. This is set by clients, who wish to provide a means to receive progress, status and protocol-specific notifications. If this value is <code>null</code>, the channel implementation may use the notification callbacks from its load group. The channel may also query the notification callbacks from its load group if its notification callbacks do not supply the requested interface.</p>
        <p>Interfaces commonly requested include: {{ interface("nsIProgressEventSink") }}, {{ interface("nsIPrompt") }}, and {{ interface("nsIAuthPrompt") }} / {{ interface("nsIAuthPrompt2") }}.</p>
        <p>When the channel is done, it must not continue holding references to this object.</p>
        {{ note("A channel implementation should take care when \"caching\" an interface pointer queried from its notification callbacks. If the notification callbacks are changed, then a cached interface pointer may become invalid and may therefore need to be re-queried.") }}</td>
    </tr>
    <tr>
      <td><code>originalURI</code></td>
      <td><code>{{ Interface("nsIURI") }}</code></td>
      <td>
        <p>The original URI used to construct the channel. This is used in the case of a redirect or URI "resolution" (For example resolving a resource: URI to a file: URI) so that the original pre-redirect URI can still be obtained.&nbsp;&nbsp; This is also used as the RI of the document resulting from the channel, unless the channel has the LOAD_REPLACE flag set.&nbsp; This is never <code>null</code>. Attempts to set it to <code>null</code> must throw.</p>
        {{NoteStart()}}This is distinctly different from the http Referer (referring URI), which is typically the page that contained the original URI (accessible from {{interface("nsIHttpChannel")}}.) {{NoteEnd()}}</td>
    </tr>
    <tr>
      <td><code>owner</code></td>
      <td><code>{{ Interface("nsISupports") }}</code></td>
      <td>
        <p>The owner, corresponding to the entity that is responsible for this channel. Used by the security manager to grant or deny privileges to mobile code loaded from this channel.</p>
        {{ note("This is a strong reference to the owner, so if the owner is also holding a strong reference to the channel, care must be taken to explicitly drop its reference to the channel.") }}</td>
    </tr>
    <tr>
      <td><code>securityInfo</code></td>
      <td><code>{{ Interface("nsISupports") }}</code></td>
      <td>Transport-level security information (if any, else null) corresponding to the channel, normally presented through the interfaces {{ Interface("nsITransportSecurityInfo") }} and {{ Interface("nsISSLStatusProvider") }} <strong>Read only.</strong></td>
    </tr>
    <tr>
      <td><code>URI</code></td>
      <td><code>{{ Interface("nsIURI") }}</code></td>
      <td>The URI corresponding to the channel. Its value is immutable. <strong>Read only.</strong></td>
    </tr>
  </tbody>
</table>
<h2 id="Constants" name="Constants">Constants</h2>
<p>Channel specific load flags:</p>
<p>Bits 23-31 are reserved for future use by this interface or one of its derivatives (For an example see {{ interface("nsICachingChannel") }}).</p>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header">Constant</td>
      <td class="header">Value</td>
      <td class="header">Description</td>
    </tr>
    <tr>
      <td><code>LOAD_DOCUMENT_URI</code></td>
      <td><code>16</code></td>
      <td>Set (for example by the docshell) to indicate whether or not the channel corresponds to a document URI.</td>
    </tr>
    <tr>
      <td><code>LOAD_RETARGETED_DOCUMENT_URI</code></td>
      <td><code>17</code></td>
      <td>If the end consumer for this load has been retargeted after discovering its content, this flag will be set:</td>
    </tr>
    <tr>
      <td><code>LOAD_REPLACE</code></td>
      <td><code>18</code></td>
      <td>
        <p>This flag is set to indicate that this channel is replacing another channel. This means that:</p>
        <ol>
          <li>The stream listener this channel will be notifying was initially passed to the {{ manch("asyncOpen") }} method of some other channel</li>
          <li>This channel's URI is a better identifier of the resource being accessed than this channel's <code>originalURI</code>.</li>
        </ol>
        This flag can be set, for example, for redirects or for cases when a single channel has multiple parts to it (and thus can follow <code>onStopRequest</code> with another <code>onStartRequest</code>/<code>onStopRequest</code> pair, each pair for a different request).</td>
    </tr>
    <tr>
      <td><code>LOAD_INITIAL_DOCUMENT_URI</code></td>
      <td><code>19</code></td>
      <td>Set (for example by the docshell) to indicate whether or not the channel corresponds to an initial document URI load (for example link click).</td>
    </tr>
    <tr>
      <td><code>LOAD_TARGETED</code></td>
      <td><code>20</code></td>
      <td>Set (For example by the <code>URILoader</code>) to indicate whether or not the end consumer for this load has been determined.</td>
    </tr>
    <tr>
      <td><code>LOAD_CALL_CONTENT_SNIFFERS</code></td>
      <td><code>21</code></td>
      <td>
        <p>If this flag is set, the channel should call the content sniffers as described in {{ source("netwerk/build/nsNetCID.h") }} about <code>NS_CONTENT_SNIFFER_CATEGORY</code>.{{ gecko_minversion_inline("1.8") }}</p>
        {{ note("Channels may ignore this flag; however, new channel implementations should only do so with good reason.") }}</td>
    </tr>
    <tr>
      <td><code>LOAD_CLASSIFY_URI</code></td>
      <td><code>22</code></td>
      <td>This flag tells the channel to use URI classifier service to check the URI when opening the channel. {{ gecko_minversion_inline("2.0") }}</td>
    </tr>
  </tbody>
</table>
<h2 id="Methods" name="Methods">Methods</h2>
<h3 id="asyncOpen()" name="asyncOpen()">asyncOpen()</h3>
<p>Asynchronously open this channel. Data is fed to the specified stream listener as it becomes available. The stream listener's methods are called on the thread that calls asyncOpen and are not called until after asyncOpen returns. If asyncOpen returns successfully, the channel promises to call at least onStartRequest and onStopRequest.</p>
<p>If the {{ interface("nsIRequest") }} object passed to the stream listener's methods is not this channel, an appropriate onChannelRedirect notification needs to be sent to the notification callbacks before onStartRequest is called. Once onStartRequest is called, all following method calls on aListener will get the request that was passed to onStartRequest.</p>
<p>If the channel's and loadgroup's notification callbacks do not provide an {{ interface("nsIChannelEventSink") }} when onChannelRedirect would be called, that's equivalent to having called onChannelRedirect.</p>
<p>If asyncOpen returns successfully, the channel is responsible for keeping itself alive until it has called onStopRequest on aListener or called onChannelRedirect.</p>
<p>Implementations are allowed to synchronously add themselves to the associated load group (if any).</p>
<pre class="eval">
void asyncOpen(
  in nsIStreamListener aListener,
  in nsISupports aContext
);
</pre>
<h6 id="Parameters" name="Parameters">Parameters</h6>
<dl>
  <dt>
    <code>aListener</code></dt>
  <dd>
    The {{ interface("nsIStreamListener") }} implementation.</dd>
  <dt>
    <code>aContext</code></dt>
  <dd>
    An opaque parameter forwarded to aListener's methods.</dd>
</dl>
<h6 id="Exceptions_thrown" name="Exceptions_thrown">Exceptions thrown</h6>
<dl>
  <dt>
    <code>NS_ERROR_ALREADY_OPENED</code></dt>
  <dd>
    If the channel is reopened</dd>
  <dt>
    <code>NS_ERROR_PORT_ACCESS_NOT_ALLOWED</code></dt>
  <dd>
    If the specified port is in the <a href="http://dxr.mozilla.org/mozilla-central/netwerk/base/src/nsIOService.cpp.html#l76" title="nsIOService forbidden list"><code>nsIOService</code> forbidden port list</a>.</dd>
</dl>
<h3 id="open()" name="open()">open()</h3>
<p>Synchronously open the channel.</p>
<p>{{note("Extensions should not call this method, because it tends to fail when a request is redirected, rather than redirecting transparently")}}{{note("<code>nsIChannel</code> implementations are not required to implement this method. Moreover, since this method may block the calling thread, it should not be called on a thread that processes UI events. Like any other <code>nsIChannel</code> method it must not be called on any thread other than the XPCOM main thread.") }}</p>
<pre class="eval">
nsIInputStream open();</pre>
<h6 id="Parameters" name="Parameters">Parameters</h6>
<p>None.</p>
<h6 id="Return_value" name="Return_value">Return value</h6>
<p>Blocking input stream to the channel's data.</p>
<h6 id="Exceptions_thrown" name="Exceptions_thrown">Exceptions thrown</h6>
<dl>
  <dt>
    <code>NS_ERROR_IN_PROGRESS</code></dt>
  <dd>
    If the channel is reopened.</dd>
</dl>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{ Interface("nsIChannelEventSink") }} for <code>onChannelRedirect</code></li>
</ul>
Revert to this revision