From 3d981011e9a251742d263e08113045b20e4c5901 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Thu, 30 Jun 2011 14:42:15 -0700 Subject: [PATCH] Bug 661582: XHR.response should return a Blob, not a File. r=sicking --- content/base/src/nsXMLHttpRequest.cpp | 33 +++++++++++++++++++++++---- content/base/test/test_XHR.html | 2 ++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/content/base/src/nsXMLHttpRequest.cpp b/content/base/src/nsXMLHttpRequest.cpp index 6f21c7b0f92..be232ea679f 100644 --- a/content/base/src/nsXMLHttpRequest.cpp +++ b/content/base/src/nsXMLHttpRequest.cpp @@ -1620,9 +1620,21 @@ void nsXMLHttpRequest::CreateResponseBlob(nsIRequest *request) if (cc) { cc->GetCacheToken(getter_AddRefs(cacheToken)); } - mResponseBlob = new nsDOMFile(file, - NS_ConvertASCIItoUTF16(contentType), - cacheToken); + + NS_ConvertASCIItoUTF16 wideContentType(contentType); + + nsCOMPtr blob = + new nsDOMFile(file, wideContentType, cacheToken); + + // XXXkhuey this is a complete hack ... but we need to get 6 out the door + // The response blob here should not be a File object, it should only + // be a Blob. Unfortunately, because nsDOMFile has grown through + // accretion over the years and is in dangerous need of a refactoring, + // slicing it is the easiest way to get there ... + PRUint64 size = 0; + blob->GetSize(&size); + blob->MozSlice(0, size, wideContentType, 2, getter_AddRefs(mResponseBlob)); + mResponseBody.Truncate(); mResponseBodyUnicode.SetIsVoid(PR_TRUE); } @@ -1893,9 +1905,20 @@ nsXMLHttpRequest::OnStopRequest(nsIRequest *request, nsISupports *ctxt, nsresult void *blobData = PR_Malloc(blobLen); if (blobData) { memcpy(blobData, mResponseBody.BeginReading(), blobLen); - mResponseBlob = + + NS_ConvertASCIItoUTF16 wideContentType(contentType); + nsCOMPtr blob = new nsDOMMemoryFile(blobData, blobLen, EmptyString(), - NS_ConvertASCIItoUTF16(contentType)); + wideContentType); + + // XXXkhuey this is a complete hack ... but we need to get 6 out the door + // The response blob here should not be a File object, it should only + // be a Blob. Unfortunately, because nsDOMFile has grown through + // accretion over the years and is in dangerous need of a refactoring, + // slicing it is the easiest way to get there ... + blob->MozSlice(0, blobLen, wideContentType, + 2, getter_AddRefs(mResponseBlob)); + mResponseBody.Truncate(); } NS_ASSERTION(mResponseBodyUnicode.IsVoid(), diff --git a/content/base/test/test_XHR.html b/content/base/test/test_XHR.html index 4f370b34969..7c3a221a8ee 100644 --- a/content/base/test/test_XHR.html +++ b/content/base/test/test_XHR.html @@ -160,6 +160,8 @@ checkResponseTextAccessThrows(xhr); checkResponseXMLAccessThrows(xhr); b = xhr.response; ok(b, "should have a non-null blob"); +ok(b instanceof Blob, "should be a Blob"); +ok(!(b instanceof File), "should not be a File"); is(b.size, "hello pass\n".length, "wrong blob size"); var fr = new FileReader();