Bug 599300: Make FileReader work correctly for empty files. r=smaug a=blocker

This commit is contained in:
Jonas Sicking 2010-10-07 09:36:23 -07:00
Родитель d3476cd4de
Коммит 2d5feee33e
3 изменённых файлов: 44 добавлений и 11 удалений

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

@ -143,7 +143,7 @@ nsDOMFileReader::SetOnloadend(nsIDOMEventListener* aOnloadend)
NS_IMETHODIMP
nsDOMFileReader::Notify(const char *aCharset, nsDetectionConfident aConf)
{
CopyASCIItoUTF16(aCharset, mCharset);
mCharset = aCharset;
return NS_OK;
}
@ -463,6 +463,8 @@ nsDOMFileReader::OnStopRequest(nsIRequest *aRequest,
rv = GetAsDataURL(mFile, mFileData, mDataLen, mResult);
break;
}
mResult.SetIsVoid(PR_FALSE);
FreeFileData();
@ -499,7 +501,7 @@ nsDOMFileReader::ReadFileContent(nsIDOMFile* aFile,
mFile = aFile;
mDataFormat = aDataFormat;
mCharset = aCharset;
CopyUTF16toUTF8(aCharset, mCharset);
//Establish a channel with our file
{
@ -580,7 +582,7 @@ nsDOMFileReader::DispatchProgressEvent(const nsAString& aType)
}
nsresult
nsDOMFileReader::GetAsText(const nsAString &aCharset,
nsDOMFileReader::GetAsText(const nsACString &aCharset,
const char *aFileData,
PRUint32 aDataLen,
nsAString& aResult)
@ -588,7 +590,7 @@ nsDOMFileReader::GetAsText(const nsAString &aCharset,
nsresult rv;
nsCAutoString charsetGuess;
if (!aCharset.IsEmpty()) {
CopyUTF16toUTF8(aCharset, charsetGuess);
charsetGuess = aCharset;
} else {
rv = GuessCharset(aFileData, aDataLen, charsetGuess);
NS_ENSURE_SUCCESS(rv, rv);
@ -625,7 +627,7 @@ nsDOMFileReader::GetAsDataURL(nsIDOMFile *aFile,
aResult.AppendLiteral(";base64,");
PRUint32 totalRead = 0;
do {
while (aDataLen > totalRead) {
PRUint32 numEncode = 4096;
PRUint32 amtRemaining = aDataLen - totalRead;
if (numEncode > amtRemaining)
@ -643,8 +645,7 @@ nsDOMFileReader::GetAsDataURL(nsIDOMFile *aFile,
PR_Free(base64);
totalRead += numEncode;
} while (aDataLen > totalRead);
}
return NS_OK;
}
@ -700,7 +701,9 @@ nsDOMFileReader::GuessCharset(const char *aFileData,
}
nsresult rv;
if (detector) {
// The charset detector doesn't work for empty (null) aFileData. Testing
// aDataLen instead of aFileData so that we catch potential errors.
if (detector && aDataLen != 0) {
mCharset.Truncate();
detector->Init(this);
@ -712,7 +715,7 @@ nsDOMFileReader::GuessCharset(const char *aFileData,
rv = detector->Done();
NS_ENSURE_SUCCESS(rv, rv);
CopyUTF16toUTF8(mCharset, aCharset);
aCharset = mCharset;
} else {
// no charset detector available, check the BOM
unsigned char sniffBuf[4];

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

@ -118,7 +118,7 @@ protected:
};
nsresult ReadFileContent(nsIDOMFile *aFile, const nsAString &aCharset, eDataFormat aDataFormat);
nsresult GetAsText(const nsAString &aCharset,
nsresult GetAsText(const nsACString &aCharset,
const char *aFileData, PRUint32 aDataLen, nsAString &aResult);
nsresult GetAsDataURL(nsIDOMFile *aFile, const char *aFileData, PRUint32 aDataLen, nsAString &aResult);
nsresult GuessCharset(const char *aFileData, PRUint32 aDataLen, nsACString &aCharset);
@ -134,7 +134,7 @@ protected:
char *mFileData;
nsCOMPtr<nsIDOMFile> mFile;
nsString mCharset;
nsCString mCharset;
PRUint32 mDataLen;
eDataFormat mDataFormat;

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

@ -62,6 +62,7 @@ var nonExistingFile = fileList.files[0];
// Test that plain reading works and fires events as expected, both
// for text and binary reading
var onloadHasRunText = false;
var onloadStartHasRunText = false;
r = new FileReader();
@ -122,6 +123,35 @@ r.readAsText(createFileWithData(convertToUTF32(testTextData)), "UTF-32");
expectedTestCount++;
// Test loading an empty file works (and doesn't crash!)
var emptyFile = createFileWithData("");
dump("hello nurse");
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty no encoding reading");
r.readAsText(emptyFile, "");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty utf8 reading");
r.readAsText(emptyFile, "utf8");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty utf16 reading");
r.readAsText(emptyFile, "utf-16");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty binary string reading");
r.readAsBinaryString(emptyFile);
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(""), 0, "empt binary string reading");
r.readAsDataURL(emptyFile);
expectedTestCount++;
// Test reusing a FileReader to read multiple times
r = new FileReader();
r.onload = getLoadHandler(testASCIIData,