зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1091986 (part 2) - Change nsStreamLoader::mData to a mozilla::Vector. r=mcmanus.
This commit is contained in:
Родитель
1eee498c59
Коммит
e42db9a0af
|
@ -9,16 +9,15 @@
|
|||
#include "nsError.h"
|
||||
#include "GeckoProfiler.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
nsStreamLoader::nsStreamLoader()
|
||||
: mData(nullptr),
|
||||
mAllocated(0),
|
||||
mLength(0)
|
||||
: mData()
|
||||
{
|
||||
}
|
||||
|
||||
nsStreamLoader::~nsStreamLoader()
|
||||
{
|
||||
ReleaseData();
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
@ -49,7 +48,7 @@ NS_IMPL_ISUPPORTS(nsStreamLoader, nsIStreamLoader,
|
|||
NS_IMETHODIMP
|
||||
nsStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
|
||||
{
|
||||
*aNumBytes = mLength;
|
||||
*aNumBytes = mData.length();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -61,7 +60,7 @@ nsStreamLoader::GetRequest(nsIRequest **aRequest)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
NS_IMETHODIMP
|
||||
nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
|
||||
{
|
||||
nsCOMPtr<nsIChannel> chan( do_QueryInterface(request) );
|
||||
|
@ -69,25 +68,21 @@ nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
|
|||
int64_t contentLength = -1;
|
||||
chan->GetContentLength(&contentLength);
|
||||
if (contentLength >= 0) {
|
||||
if (contentLength > UINT32_MAX) {
|
||||
// Too big to fit into uint32, so let's bail.
|
||||
// XXX we should really make mAllocated and mLength 64-bit instead.
|
||||
if (uint64_t(contentLength) > std::numeric_limits<size_t>::max()) {
|
||||
// Too big to fit into size_t, so let's bail.
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
uint32_t contentLength32 = uint32_t(contentLength);
|
||||
// preallocate buffer
|
||||
mData = static_cast<uint8_t*>(moz_malloc(contentLength32));
|
||||
if (!mData) {
|
||||
if (!mData.initCapacity(contentLength)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mAllocated = contentLength32;
|
||||
}
|
||||
}
|
||||
mContext = ctxt;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
NS_IMETHODIMP
|
||||
nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
||||
nsresult aStatus)
|
||||
{
|
||||
|
@ -97,12 +92,14 @@ nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
|||
if (mObserver) {
|
||||
// provide nsIStreamLoader::request during call to OnStreamComplete
|
||||
mRequest = request;
|
||||
size_t length = mData.length();
|
||||
uint8_t* elems = mData.extractRawBuffer();
|
||||
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 = nullptr;
|
||||
length, elems);
|
||||
if (rv != NS_SUCCESS_ADOPTED_DATA) {
|
||||
// The observer didn't take ownership of the extracted data buffer, so
|
||||
// put it back into mData.
|
||||
mData.replaceRawBuffer(elems, length);
|
||||
}
|
||||
// done.. cleanup
|
||||
ReleaseData();
|
||||
|
@ -123,23 +120,11 @@ nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
|
|||
{
|
||||
nsStreamLoader *self = (nsStreamLoader *) closure;
|
||||
|
||||
if (count > UINT32_MAX - self->mLength) {
|
||||
return NS_ERROR_ILLEGAL_VALUE; // is there a better error to use here?
|
||||
if (!self->mData.append(fromSegment, count)) {
|
||||
self->mData.clearAndFree();
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
if (self->mLength + count > self->mAllocated) {
|
||||
self->mData = static_cast<uint8_t*>(moz_realloc(self->mData,
|
||||
self->mLength + count));
|
||||
if (!self->mData) {
|
||||
self->ReleaseData();
|
||||
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;
|
||||
|
@ -157,10 +142,5 @@ nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt,
|
|||
void
|
||||
nsStreamLoader::ReleaseData()
|
||||
{
|
||||
if (mData) {
|
||||
moz_free(mData);
|
||||
mData = nullptr;
|
||||
}
|
||||
mLength = 0;
|
||||
mAllocated = 0;
|
||||
mData.clearAndFree();
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "nsIStreamLoader.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/Vector.h"
|
||||
|
||||
class nsIRequest;
|
||||
|
||||
|
@ -39,11 +40,9 @@ protected:
|
|||
nsCOMPtr<nsISupports> mContext; // the observer's context
|
||||
nsCOMPtr<nsIRequest> mRequest;
|
||||
|
||||
uint8_t *mData; // buffer to accumulate incoming data
|
||||
uint32_t mAllocated; // allocated size of data buffer (we preallocate if
|
||||
// contentSize is available)
|
||||
uint32_t mLength; // actual length of data in buffer
|
||||
// (must be <= mAllocated)
|
||||
// Buffer to accumulate incoming data. We preallocate if contentSize is
|
||||
// available.
|
||||
mozilla::Vector<uint8_t, 0> mData;
|
||||
};
|
||||
|
||||
#endif // nsStreamLoader_h__
|
||||
|
|
Загрузка…
Ссылка в новой задаче