Fix more bugs when reusing FileReaders for multiple reads. Also fix leaks. r=jst a=jst

This commit is contained in:
Jonas Sicking 2009-11-22 22:23:23 -08:00
Родитель 542a1b45ce
Коммит ebb5ae0416
3 изменённых файлов: 57 добавлений и 11 удалений

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

@ -167,6 +167,8 @@ nsDOMFileReader::~nsDOMFileReader()
if (mListenerManager)
mListenerManager->Disconnect();
FreeFileData();
nsLayoutStatics::Release();
}
@ -309,9 +311,7 @@ nsDOMFileReader::Abort()
mFile = nsnull;
//Clean up memory buffer
PR_Free(mFileData);
mFileData = nsnull;
mDataLen = 0;
FreeFileData();
//Dispatch the abort event
DispatchProgressEvent(NS_LITERAL_STRING(ABORT_STR));
@ -449,6 +449,7 @@ nsDOMFileReader::OnStopRequest(nsIRequest *aRequest,
//Set the status field as appropriate
if (NS_FAILED(aStatus)) {
FreeFileData();
DispatchError(aStatus);
return NS_OK;
}
@ -467,6 +468,8 @@ nsDOMFileReader::OnStopRequest(nsIRequest *aRequest,
return NS_ERROR_FAILURE;
}
FreeFileData();
//Dispatch load event to signify end of a successful load
DispatchProgressEvent(NS_LITERAL_STRING(LOAD_STR));
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
@ -485,11 +488,15 @@ nsDOMFileReader::ReadFileContent(nsIDOMFile* aFile,
//Implicit abort to clear any other activity going on
Abort();
mDataFormat = aDataFormat;
mCharset = aCharset;
mError = nsnull;
SetDOMStringToNull(mResult);
mReadTransferred = 0;
mReadTotal = 0;
mReadyState = nsIDOMFileReader::EMPTY;
FreeFileData();
mDataFormat = aDataFormat;
mCharset = aCharset;
//Obtain the nsDOMFile's underlying nsIFile
nsresult rv;

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

@ -60,6 +60,7 @@
#include "nsCOMPtr.h"
#include "nsIStreamLoader.h"
#include "nsIChannel.h"
#include "prmem.h"
#include "nsXMLHttpRequest.h"
@ -119,6 +120,12 @@ protected:
void DispatchError(nsresult rv);
void StartProgressEventTimer();
void FreeFileData() {
PR_Free(mFileData);
mFileData = nsnull;
mDataLen = 0;
}
char *mFileData;
nsCOMPtr<nsIFile> mFile;
nsString mCharset;

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

@ -126,17 +126,49 @@ expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler(testASCIIData,
testASCIIData.length,
"to-be-reused reading")
"to-be-reused reading text")
var makeAnotherReadListener = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener, false);
r.onload = getLoadHandler(testBinaryData,
testBinaryData.length,
"reused reading");
r.readAsBinaryString(binaryFile);
r.onload = getLoadHandler(testASCIIData,
testASCIIData.length,
"reused reading text");
r.readAsText(asciiFile);
};
r.addEventListener("load", makeAnotherReadListener, false);
r.readAsText(asciiFile, "");
r.readAsText(asciiFile);
expectedTestCount += 2;
r = new FileReader();
r.onload = getLoadHandler(testBinaryData,
testBinaryData.length,
"to-be-reused reading binary")
var makeAnotherReadListener2 = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener2, false);
r.onload = getLoadHandler(testBinaryData,
testBinaryData.length,
"reused reading binary");
r.readAsBinaryString(binaryFile);
};
r.addEventListener("load", makeAnotherReadListener2, false);
r.readAsBinaryString(binaryFile);
expectedTestCount += 2;
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(testBinaryData),
testBinaryData.length,
"to-be-reused reading data url")
var makeAnotherReadListener3 = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener3, false);
r.onload = getLoadHandler(convertToDataURL(testBinaryData),
testBinaryData.length,
"reused reading data url");
r.readAsDataURL(binaryFile);
};
r.addEventListener("load", makeAnotherReadListener3, false);
r.readAsDataURL(binaryFile);
expectedTestCount += 2;