Bug 507970, part 1: nsIStreamLoader changes to support downloadable font code, r=bzbarsky

--HG--
extra : rebase_source : d0142c7afc7b7788a76caf2a2508e7604c2b74f1
This commit is contained in:
Jonathan Kew 2009-09-17 11:14:25 +01:00
Родитель 8edd087cde
Коммит 594fbda9eb
4 изменённых файлов: 76 добавлений и 10 удалений

Просмотреть файл

@ -55,6 +55,13 @@ interface nsIStreamLoaderObserver : nsISupports
* This method will always be called asynchronously by the
* nsIStreamLoader involved, on the thread that called the
* loader's init() method.
*
* If the observer wants to take over responsibility for the
* data buffer (result), it returns NS_SUCCESS_ADOPTED_DATA
* in place of NS_OK as its success code. The loader will then
* "forget" about the data, and not free() it in its own
* destructor; observer must call free() when the data is
* no longer required.
*/
void onStreamComplete(in nsIStreamLoader loader,
in nsISupports ctxt,

Просмотреть файл

@ -362,4 +362,17 @@
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 81)
/******************************************************************************
* StreamLoader specific result codes:
*/
/**
* Result code returned by nsIStreamLoaderObserver to indicate that
* the observer is taking over responsibility for the data buffer,
* and the loader should NOT free it.
*/
#define NS_SUCCESS_ADOPTED_DATA \
NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_NETWORK, 90)
#endif // !nsNetError_h__

Просмотреть файл

@ -38,6 +38,23 @@
#include "nsStreamLoader.h"
#include "nsIInputStream.h"
#include "nsIChannel.h"
#include "nsNetError.h"
#include <stdlib.h>
nsStreamLoader::nsStreamLoader()
: mData(nsnull),
mAllocated(0),
mLength(0)
{
}
nsStreamLoader::~nsStreamLoader()
{
if (mData) {
::free(mData);
}
}
NS_IMETHODIMP
nsStreamLoader::Init(nsIStreamLoaderObserver* observer)
@ -67,7 +84,7 @@ NS_IMPL_ISUPPORTS3(nsStreamLoader, nsIStreamLoader,
NS_IMETHODIMP
nsStreamLoader::GetNumBytesRead(PRUint32* aNumBytes)
{
*aNumBytes = mData.Length();
*aNumBytes = mLength;
return NS_OK;
}
@ -88,7 +105,11 @@ nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
chan->GetContentLength(&contentLength);
if (contentLength >= 0) {
// preallocate buffer
mData.SetCapacity(contentLength + 1);
mData = static_cast<PRUint8*>(::malloc(contentLength));
if (!mData) {
return NS_ERROR_OUT_OF_MEMORY;
}
mAllocated = contentLength;
}
}
mContext = ctxt;
@ -102,10 +123,15 @@ nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
if (mObserver) {
// provide nsIStreamLoader::request during call to OnStreamComplete
mRequest = request;
mObserver->OnStreamComplete(this, mContext, aStatus,
mData.Length(),
reinterpret_cast<const PRUint8*>
(mData.get()));
nsresult rv = mObserver->OnStreamComplete(this, mContext, aStatus,
mLength, mData);
if (rv == NS_SUCCESS_ADOPTED_DATA) {
// the observer now owns the data buffer, and the loader must
// not deallocate it
mData = nsnull;
mLength = 0;
mAllocated = 0;
}
// done.. cleanup
mRequest = 0;
mObserver = 0;
@ -124,7 +150,24 @@ nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
{
nsStreamLoader *self = (nsStreamLoader *) closure;
self->mData.Append(fromSegment, count);
if (count > 0xffffffffU - self->mLength) {
return NS_ERROR_ILLEGAL_VALUE; // is there a better error to use here?
}
if (self->mLength + count > self->mAllocated) {
self->mData = static_cast<PRUint8*>(::realloc(self->mData,
self->mLength + count));
if (!self->mData) {
self->mLength = 0;
self->mAllocated = 0;
return NS_ERROR_OUT_OF_MEMORY;
}
self->mAllocated = self->mLength + count;
}
::memcpy(self->mData + self->mLength, fromSegment, count);
self->mLength += count;
*writeCount = count;
return NS_OK;

Просмотреть файл

@ -51,8 +51,8 @@ public:
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
nsStreamLoader() { }
~nsStreamLoader() {}
nsStreamLoader();
~nsStreamLoader();
static NS_METHOD
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
@ -63,8 +63,11 @@ protected:
nsCOMPtr<nsIStreamLoaderObserver> mObserver;
nsCOMPtr<nsISupports> mContext; // the observer's context
nsCString mData;
nsCOMPtr<nsIRequest> mRequest;
PRUint8 *mData;
PRUint32 mAllocated;
PRUint32 mLength;
};
#endif // nsStreamLoader_h__