Make nsIStreamLoader and nsIUnicharStreamLoader be stream listeners. Bug

281153, patch by Son Le <son.le0@gmail.com>, r=biesi, sr=darin
This commit is contained in:
bzbarsky%mit.edu 2006-10-24 21:47:40 +00:00
parent df977f5e9a
commit 0ce72c77cd
14 changed files with 84 additions and 97 deletions

View File

@ -221,7 +221,11 @@ nsresult RemoteURILoadManager::RequestURILoad(const nsAString& inURI, id<RemoteL
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIStreamLoader> streamLoader;
rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), channel, this, loaderContext) ; // , mLoadGroup, nsnull, loadFlags);
rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), this);
if (NS_SUCCEEDED(rv))
rv = channel->AsyncOpen(streamLoader, loaderContext);
if (NS_FAILED(rv))
{
#if DEBUG

View File

@ -90,6 +90,7 @@
#include "nsILoadGroup.h"
#include "nsTObserverArray.h"
#include "nsStubMutationObserver.h"
#include "nsIChannel.h"
// Put these here so all document impls get them automatically
#include "nsHTMLStyleSheet.h"

View File

@ -607,7 +607,9 @@ nsScriptLoader::DoProcessScriptElement(nsIScriptElement *aElement,
PR_FALSE);
httpChannel->SetReferrer(mDocument->GetDocumentURI());
}
rv = NS_NewStreamLoader(getter_AddRefs(loader), channel, this, request);
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
if (NS_SUCCEEDED(rv))
rv = channel->AsyncOpen(loader, request);
}
if (NS_FAILED(rv)) {
mPendingRequests.RemoveObject(request);

View File

@ -1360,7 +1360,10 @@ CSSLoaderImpl::LoadSheet(SheetLoadData* aLoadData, StyleSheetState aSheetState)
// which owns us
nsCOMPtr<nsIUnicharStreamLoader> streamLoader;
rv = NS_NewUnicharStreamLoader(getter_AddRefs(streamLoader),
channel, aLoadData);
aLoadData);
if (NS_SUCCEEDED(rv))
rv = channel->AsyncOpen(streamLoader, nsnull);
#ifdef DEBUG
mSyncCallback = PR_FALSE;

View File

@ -35,14 +35,10 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
#include "nsIChannel.idl"
#include "nsIStreamListener.idl"
interface nsIRequest;
interface nsIURI;
interface nsILoadGroup;
interface nsIStreamLoader;
interface nsIInterfaceRequestor;
[scriptable, uuid(359F7990-D4E9-11d3-A1A5-0050041CAF44)]
interface nsIStreamLoaderObserver : nsISupports
@ -51,7 +47,7 @@ interface nsIStreamLoaderObserver : nsISupports
* Called when the entire stream has been loaded.
*
* @param loader the stream loader that loaded the stream.
* @param context the aContext parameter passed to the loader's init method
* @param ctxt the context parameter of the underlying channel
* @param status the status of the underlying channel
* @param resultLength the length of the data loaded
* @param result the data
@ -70,29 +66,24 @@ interface nsIStreamLoaderObserver : nsISupports
/**
* Asynchronously loads a channel into a memory buffer.
*
* To use this interface, first call init() with a nsIStreamLoaderObserver
* that will be notified when the data has been loaded. Then call asyncOpen()
* on the channel with the nsIStreamLoader as the listener. The context
* argument in the asyncOpen() call will be passed to the onStreamComplete()
* callback.
*
* XXX define behaviour for sizes >4 GB
*/
[scriptable, uuid(31d37360-8e5a-11d3-93ad-00104ba0fd40)]
interface nsIStreamLoader : nsISupports
[scriptable, uuid(8ea7e890-8211-11d9-8bde-f66bad1e3f3a)]
interface nsIStreamLoader : nsIStreamListener
{
/**
* Initialize this stream loader, and start loading the data.
*
* @param aChannel
* A Channel to load data from. This must not be asyncOpen'd yet!
* @param aObserver
* An observer that will be notified when the data is complete.
* @param aContext
* May be null. Will be passed to the observer.
*
* @note Failure to open the channel will cause init() to throw. A
* successful call to init() means that onStreamComplete is
* guaranteed to be called on aObserver asynchronously (i.e. after
* init() returns).
*/
void init(in nsIChannel aChannel,
in nsIStreamLoaderObserver aObserver,
in nsISupports aContext);
void init(in nsIStreamLoaderObserver aObserver);
/**
* Gets the number of bytes read so far.

View File

@ -36,20 +36,20 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
#include "nsIStreamListener.idl"
interface nsIUnicharInputStream;
interface nsIUnicharStreamLoader;
interface nsIChannel;
[scriptable, uuid(e06e8b08-8cdd-4503-a0a0-6f3b943602af)]
[scriptable, uuid(6bb3e55b-69c0-4fc9-87e5-bed780d997ce)]
interface nsIUnicharStreamLoaderObserver : nsISupports
{
/**
* Called when the first full segment of data if available.
*
* @param aLoader the unichar stream loader
* @param aContext the aContext parameter passed to the loader's init method
* @param aContext the context parameter of the underlying channel
* @param aFirstSegment the raw bytes of the first full data segment
* @param aLength the length of aFirstSegment
*
@ -64,7 +64,7 @@ interface nsIUnicharStreamLoaderObserver : nsISupports
* Called when the entire stream has been loaded.
*
* @param aLoader the unichar stream loader
* @param aContext the aContext parameter passed to the loader's init method
* @param aContext the context parameter of the underlying channel
* @param aStatus the status of the underlying channel
* @param aUnicharData the unichar input stream containing the data. This
* can be null in some failure conditions.
@ -79,29 +79,29 @@ interface nsIUnicharStreamLoaderObserver : nsISupports
in nsIUnicharInputStream aUnicharData);
};
[scriptable, uuid(8a3eca16-167e-443d-9485-7e84ed822e95)]
interface nsIUnicharStreamLoader : nsISupports
/**
* Asynchronously load a channel, converting the data to UTF-16.
*
* To use this interface, first call init() with a
* nsIUnicharStreamLoaderObserver that will be notified when the data has been
* loaded. Then call asyncOpen() on the channel with the nsIUnicharStreamLoader
* as the listener. The context argument in the asyncOpen() call will be
* passed to the onStreamComplete() callback.
*/
[scriptable, uuid(9037f476-7c08-4729-b690-3e425269802b)]
interface nsIUnicharStreamLoader : nsIStreamListener
{
const unsigned long DEFAULT_SEGMENT_SIZE = 4096;
/**
* Initializes the unichar stream loader
*
* @param aChannel the channel to read data from. This should _not_ be
* opened; the loader will open the channel itself.
* @param aObserver the observer to notify when a charset is needed and when
* the load is complete
* @param aContext an opaque context pointer
* @param aSegmentSize the size of the segments to use for the data, in bytes
*
* @note Failure to open the channel will cause init() to throw. A
* successful call to init() means that onStreamComplete is guaranteed
* to be called on aObserver asynchronously (i.e. after init() returns).
*/
void init(in nsIChannel aChannel,
in nsIUnicharStreamLoaderObserver aObserver,
in nsISupports aContext,
void init(in nsIUnicharStreamLoaderObserver aObserver,
in unsigned long aSegmentSize);
/**

View File

@ -465,15 +465,13 @@ NS_NewDownloader(nsIStreamListener **result,
inline nsresult
NS_NewStreamLoader(nsIStreamLoader **result,
nsIChannel *channel,
nsIStreamLoaderObserver *observer,
nsISupports *context)
nsIStreamLoaderObserver *observer)
{
nsresult rv;
nsCOMPtr<nsIStreamLoader> loader =
do_CreateInstance(NS_STREAMLOADER_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = loader->Init(channel, observer, context);
rv = loader->Init(observer);
if (NS_SUCCEEDED(rv)) {
*result = nsnull;
loader.swap(*result);
@ -504,23 +502,23 @@ NS_NewStreamLoader(nsIStreamLoader **result,
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
if (httpChannel)
httpChannel->SetReferrer(referrer);
rv = NS_NewStreamLoader(result, channel, observer, context);
rv = NS_NewStreamLoader(result, observer);
if (NS_SUCCEEDED(rv))
rv = channel->AsyncOpen(*result, context);
}
return rv;
}
inline nsresult
NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **result,
nsIChannel *channel,
nsIUnicharStreamLoaderObserver *observer,
nsISupports *context = nsnull,
PRUint32 segmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE)
{
nsresult rv;
nsCOMPtr<nsIUnicharStreamLoader> loader =
do_CreateInstance(NS_UNICHARSTREAMLOADER_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = loader->Init(channel, observer, context, segmentSize);
rv = loader->Init(observer, segmentSize);
if (NS_SUCCEEDED(rv)) {
*result = nsnull;
loader.swap(*result);

View File

@ -298,19 +298,21 @@ nsPACMan::StartLoading()
return;
}
// Always hit the origin server when loading PAC.
nsCOMPtr<nsIIOService> ios = do_GetIOService();
if (ios) {
nsCOMPtr<nsIChannel> channel;
if (NS_SUCCEEDED(mLoader->Init(this))) {
// Always hit the origin server when loading PAC.
nsCOMPtr<nsIIOService> ios = do_GetIOService();
if (ios) {
nsCOMPtr<nsIChannel> channel;
// NOTE: This results in GetProxyForURI being called
ios->NewChannelFromURI(mPACURI, getter_AddRefs(channel));
// NOTE: This results in GetProxyForURI being called
ios->NewChannelFromURI(mPACURI, getter_AddRefs(channel));
if (channel) {
channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE);
channel->SetNotificationCallbacks(this);
if (NS_SUCCEEDED(mLoader->Init(channel, this, nsnull)))
return;
if (channel) {
channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE);
channel->SetNotificationCallbacks(this);
if (NS_SUCCEEDED(channel->AsyncOpen(mLoader, nsnull)))
return;
}
}
}

View File

@ -40,19 +40,11 @@
#include "nsIChannel.h"
NS_IMETHODIMP
nsStreamLoader::Init(nsIChannel *channel,
nsIStreamLoaderObserver* observer,
nsISupports* context)
nsStreamLoader::Init(nsIStreamLoaderObserver* observer)
{
NS_ENSURE_ARG_POINTER(channel);
NS_ENSURE_ARG_POINTER(observer);
nsresult rv = channel->AsyncOpen(this, context);
NS_ENSURE_SUCCESS(rv, rv);
mObserver = observer;
mContext = context;
return rv;
return NS_OK;
}
NS_METHOD
@ -83,8 +75,8 @@ nsStreamLoader::GetNumBytesRead(PRUint32* aNumBytes)
NS_IMETHODIMP
nsStreamLoader::GetRequest(nsIRequest **aRequest)
{
NS_IF_ADDREF(*aRequest = mRequest);
return NS_OK;
NS_IF_ADDREF(*aRequest = mRequest);
return NS_OK;
}
NS_IMETHODIMP
@ -92,13 +84,14 @@ nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
{
nsCOMPtr<nsIChannel> chan( do_QueryInterface(request) );
if (chan) {
PRInt32 contentLength = -1;
chan->GetContentLength(&contentLength);
if (contentLength >= 0) {
// preallocate buffer
mData.SetCapacity(contentLength + 1);
}
PRInt32 contentLength = -1;
chan->GetContentLength(&contentLength);
if (contentLength >= 0) {
// preallocate buffer
mData.SetCapacity(contentLength + 1);
}
}
mContext = ctxt;
return NS_OK;
}

View File

@ -39,12 +39,10 @@
#define nsStreamLoader_h__
#include "nsIStreamLoader.h"
#include "nsIStreamListener.h"
#include "nsCOMPtr.h"
#include "nsString.h"
class nsStreamLoader : public nsIStreamLoader,
public nsIStreamListener
class nsStreamLoader : public nsIStreamLoader
{
public:
NS_DECL_ISUPPORTS

View File

@ -37,8 +37,6 @@
* ***** END LICENSE BLOCK ***** */
#include "nsUnicharStreamLoader.h"
#include "nsIPipe.h"
#include "nsIChannel.h"
#include "nsNetUtil.h"
#include "nsProxiedService.h"
#include "nsIChannel.h"
@ -51,27 +49,20 @@
#endif // DEBUG
NS_IMETHODIMP
nsUnicharStreamLoader::Init(nsIChannel *aChannel,
nsIUnicharStreamLoaderObserver *aObserver,
nsISupports *aContext,
nsUnicharStreamLoader::Init(nsIUnicharStreamLoaderObserver *aObserver,
PRUint32 aSegmentSize)
{
NS_ENSURE_ARG_POINTER(aChannel);
NS_ENSURE_ARG_POINTER(aObserver);
if (aSegmentSize <= 0) {
aSegmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE;
}
nsresult rv = aChannel->AsyncOpen(this, aContext);
NS_ENSURE_SUCCESS(rv, rv);
mObserver = aObserver;
mContext = aContext;
mCharset.Truncate();
mChannel = nsnull; // Leave this null till OnStopRequest
mSegmentSize = aSegmentSize;
return rv;
return NS_OK;
}
NS_METHOD
@ -114,6 +105,7 @@ NS_IMETHODIMP
nsUnicharStreamLoader::OnStartRequest(nsIRequest* request,
nsISupports *ctxt)
{
mContext = ctxt;
return NS_OK;
}

View File

@ -40,15 +40,13 @@
#define nsUnicharStreamLoader_h__
#include "nsIUnicharStreamLoader.h"
#include "nsIStreamListener.h"
#include "nsCOMPtr.h"
#include "nsIChannel.h"
#include "nsString.h"
#include "nsIInputStream.h"
#include "nsIOutputStream.h"
class nsUnicharStreamLoader : public nsIUnicharStreamLoader,
public nsIStreamListener
class nsUnicharStreamLoader : public nsIUnicharStreamLoader
{
public:
NS_DECL_ISUPPORTS

View File

@ -72,7 +72,11 @@ int main(int argc, char **argv)
return -1;
nsCOMPtr<nsIStreamLoader> loader;
rv = NS_NewStreamLoader(getter_AddRefs(loader), chan, observer, nsnull);
rv = NS_NewStreamLoader(getter_AddRefs(loader), observer);
if (NS_FAILED(rv))
return -1;
rv = chan->AsyncOpen(loader, nsnull);
if (NS_FAILED(rv))
return -1;

View File

@ -148,9 +148,10 @@ nsHTTPDownloadEvent::Run()
mListener->mResponsibleForDoneSignal = PR_TRUE;
rv = NS_NewStreamLoader(getter_AddRefs(mListener->mLoader),
hchan,
mListener,
nsnull);
mListener);
if (NS_SUCCEEDED(rv))
rv = hchan->AsyncOpen(mListener->mLoader, nsnull);
if (NS_FAILED(rv)) {
mListener->mResponsibleForDoneSignal = PR_FALSE;