diff --git a/mozilla/network/module/MANIFEST b/mozilla/network/module/MANIFEST index 62cbcd1b548..0b2fa94e0ac 100644 --- a/mozilla/network/module/MANIFEST +++ b/mozilla/network/module/MANIFEST @@ -11,4 +11,5 @@ nsIURLGroup.h nsINetlibURL.h nsIProtocol.h nsIProtocolURLFactory.h +nsIUnicharStreamLoader.h diff --git a/mozilla/network/module/Makefile.in b/mozilla/network/module/Makefile.in index 3d175668ef2..13adaea0f23 100644 --- a/mozilla/network/module/Makefile.in +++ b/mozilla/network/module/Makefile.in @@ -30,6 +30,7 @@ CPPSRCS = \ nsNetThread.cpp \ nsRelatedLinks.cpp \ nsNetStream.cpp \ + nsNetStreamLoader.cpp \ nsHttpUrl.cpp \ nsStubContext.cpp \ nsNetStubs.cpp \ @@ -52,6 +53,7 @@ EXPORTS = nsINetService.h \ nsINetlibURL.h \ nsIProtocol.h \ nsIProtocolURLFactory.h \ + nsIUnicharStreamLoader.h \ $(NULL) EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS)) diff --git a/mozilla/network/module/makefile.win b/mozilla/network/module/makefile.win index ecb2fb5b7be..f8588759d4c 100644 --- a/mozilla/network/module/makefile.win +++ b/mozilla/network/module/makefile.win @@ -27,6 +27,7 @@ EXPORTS = nsINetService.h \ nsIRelatedLinks.h \ nsINetlibURL.h \ nsIURLGroup.h \ + nsIUnicharStreamLoader.h \ nsIConnectionInfo.h \ nsIRefreshUrl.h \ nsIProtocol.h \ @@ -63,6 +64,7 @@ OBJS= \ .\$(OBJDIR)\nsNetService.obj \ .\$(OBJDIR)\nsNetThread.obj \ .\$(OBJDIR)\nsNetStream.obj \ + .\$(OBJDIR)\nsNetStreamLoader.obj \ .\$(OBJDIR)\nsRelatedLinks.obj \ .\$(OBJDIR)\nsHttpUrl.obj \ .\$(OBJDIR)\nsStubContext.obj \ diff --git a/mozilla/network/module/nsIUnicharStreamLoader.h b/mozilla/network/module/nsIUnicharStreamLoader.h new file mode 100644 index 00000000000..30eed3c3edf --- /dev/null +++ b/mozilla/network/module/nsIUnicharStreamLoader.h @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#ifndef nsIUnicharStreamLoader_h___ +#define nsIUnicharStreamLoader_h___ + +#include "nsISupports.h" +#include "nsString.h" + +#define NS_IUNICHARSTREAMLOADER_IID \ + {0xa6cf90d8, 0x15b3, 0x11d2, \ + {0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}} + +class nsIUnicharStreamLoader; +class nsIURL; + +/** + * Funtion registered with the stream loader. This function is called + * when the stream is done loading (or has aborted). + * + * @param aLoader the loader + * @param aData the result of the load as a unicode character string + * @param aRef the opaque data passed to the loader + * @param aStatus the completion status of the stream + */ +typedef void (*nsStreamCompleteFunc)(nsIUnicharStreamLoader* aLoader, + nsString& aData, + void* aRef, + nsresult aStatus); + +/** + * The purpose of this interface is to provide a mechanism for a + * byte stream to be loaded asynchronously from a URL, the stream + * data to be accumulated, and the result to be returned as a + * unicode character string. + */ +class nsIUnicharStreamLoader : public nsISupports { +public: + /** + * Get the number of bytes read so far. + * + * @param aNumBytes out parameter to get number of unicode + * characters read. + */ + NS_IMETHOD GetNumCharsRead(PRInt32* aNumChars) = 0; +}; + +/** + * Start loading the specified URL and accumulating the stream data. + * When the stream is completed, the result is sent to the complete + * function as a unicode character string. + * + * @param aInstancePtrResult new stream loader + * @param aURL the URL to load + * @param aFunc the function to call on termination of stream loading + * @param aRef an opaque value that will later be sent to the termination + * function + */ +extern NS_NET nsresult NS_NewUnicharStreamLoader(nsIUnicharStreamLoader** aInstancePtrResult, + nsIURL* aURL, + nsStreamCompleteFunc aFunc, + void* aRef); + +#endif /* nsIUnicharStreamLoader_h___ */ diff --git a/mozilla/network/module/nsNetStreamLoader.cpp b/mozilla/network/module/nsNetStreamLoader.cpp new file mode 100644 index 00000000000..4edffae44b7 --- /dev/null +++ b/mozilla/network/module/nsNetStreamLoader.cpp @@ -0,0 +1,212 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#include "nsIUnicharStreamLoader.h" +#include "nsIStreamListener.h" +#include "nsIInputStream.h" +#include "nsIURL.h" + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); +static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); +static NS_DEFINE_IID(kIUnicharStreamLoaderIID, NS_IUNICHARSTREAMLOADER_IID); + +class nsUnicharStreamLoader : public nsIUnicharStreamLoader, + public nsIStreamListener +{ +public: + nsUnicharStreamLoader(nsIURL* aURL, + nsStreamCompleteFunc aFunc, + void* aRef); + ~nsUnicharStreamLoader(); + + NS_DECL_ISUPPORTS + + NS_IMETHOD GetNumCharsRead(PRInt32* aNumBytes); + + NS_IMETHOD OnStartBinding(nsIURL* aURL, const char *aContentType); + NS_IMETHOD OnProgress(nsIURL* aURL, PRUint32 aProgress, PRUint32 aProgressMax); + NS_IMETHOD OnStatus(nsIURL* aURL, const PRUnichar* aMsg); + NS_IMETHOD OnStopBinding(nsIURL* aURL, nsresult aStatus, const PRUnichar* aMsg); + NS_IMETHOD GetBindInfo(nsIURL* aURL, nsStreamBindingInfo* aInfo); + NS_IMETHOD OnDataAvailable(nsIURL* aURL, nsIInputStream *aIStream, + PRUint32 aLength); + +protected: + nsStreamCompleteFunc mFunc; + void* mRef; + nsString* mData; +}; + + +nsUnicharStreamLoader::nsUnicharStreamLoader(nsIURL* aURL, + nsStreamCompleteFunc aFunc, + void* aRef) +{ + NS_INIT_REFCNT(); + mFunc = aFunc; + mRef = aRef; + mData = new nsString(); + + nsresult rv; + if (aURL) { + rv = NS_OpenURL(aURL, this); + if ((NS_OK != rv) && (nsnull != mFunc)) { + (*mFunc)(this, *mData, mRef, rv); + } + } +} + +nsUnicharStreamLoader::~nsUnicharStreamLoader() +{ + if (nsnull != mData) { + delete mData; + } +} + +NS_IMPL_ADDREF(nsUnicharStreamLoader) +NS_IMPL_RELEASE(nsUnicharStreamLoader) + +nsresult +nsUnicharStreamLoader::QueryInterface(const nsIID &aIID, void** aInstancePtr) +{ + if (NULL == aInstancePtr) { + return NS_ERROR_NULL_POINTER; + } + if (aIID.Equals(kIStreamListenerIID)) { + *aInstancePtr = (void*) ((nsIStreamListener*)this); + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIUnicharStreamLoaderIID)) { + *aInstancePtr = (void*) ((nsIUnicharStreamLoader*)this); + AddRef(); + return NS_OK; + } + if (aIID.Equals(kISupportsIID)) { + *aInstancePtr = ((nsISupports *)(nsIUnicharStreamLoader *)this); + AddRef(); + return NS_OK; + } + + return NS_NOINTERFACE; +} + +NS_IMETHODIMP +nsUnicharStreamLoader::GetNumCharsRead(PRInt32* aNumBytes) +{ + if (nsnull != mData) { + *aNumBytes = mData->Length(); + } + else { + *aNumBytes = 0; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsUnicharStreamLoader::OnStartBinding(nsIURL* aURL, + const char *aContentType) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsUnicharStreamLoader::OnProgress(nsIURL* aURL, + PRUint32 aProgress, + PRUint32 aProgressMax) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsUnicharStreamLoader::OnStatus(nsIURL* aURL, const PRUnichar* aMsg) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsUnicharStreamLoader::OnStopBinding(nsIURL* aURL, + nsresult aStatus, + const PRUnichar* aMsg) +{ + (*mFunc)(this, *mData, mRef, aStatus); + + return NS_OK; +} + +NS_IMETHODIMP +nsUnicharStreamLoader::GetBindInfo(nsIURL* aURL, + nsStreamBindingInfo* aInfo) +{ + return NS_OK; +} + +#define BUF_SIZE 1024 + +NS_IMETHODIMP +nsUnicharStreamLoader::OnDataAvailable(nsIURL* aURL, + nsIInputStream *aIStream, + PRUint32 aLength) +{ + nsresult rv = NS_OK; + char buffer[BUF_SIZE]; + PRUint32 len, lenRead; + + aIStream->GetLength(&len); + + while (len > 0) { + if (len < BUF_SIZE) { + lenRead = len; + } + else { + lenRead = BUF_SIZE; + } + + rv = aIStream->Read(buffer, 0, lenRead, &lenRead); + if (NS_OK != rv) { + return rv; + } + + mData->Append(buffer, lenRead); + len -= lenRead; + } + + return rv; +} + +extern NS_NET nsresult +NS_NewUnicharStreamLoader(nsIUnicharStreamLoader** aInstancePtrResult, + nsIURL* aURL, + nsStreamCompleteFunc aFunc, + void* aRef) +{ + NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr"); + if (nsnull == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + + nsUnicharStreamLoader* it = new nsUnicharStreamLoader(aURL, + aFunc, + aRef); + if (nsnull == it) { + return NS_ERROR_OUT_OF_MEMORY; + } + return it->QueryInterface(kIUnicharStreamLoaderIID, + (void **) aInstancePtrResult); +}