Bug 689008: Part 1 - Skip reading already cached or local resource for blob response; r=jonas

This commit is contained in:
Masatoshi Kimura 2011-10-26 08:52:19 -04:00
parent e026a31272
commit ee11c4dbb7
2 changed files with 20 additions and 6 deletions

View File

@ -1690,7 +1690,7 @@ nsXMLHttpRequest::StreamReaderFunc(nsIInputStream* in,
return rv;
}
void nsXMLHttpRequest::CreateResponseBlob(nsIRequest *request)
bool nsXMLHttpRequest::CreateResponseBlob(nsIRequest *request)
{
nsCOMPtr<nsIFile> file;
nsCOMPtr<nsICachingChannel> cc(do_QueryInterface(request));
@ -1702,18 +1702,27 @@ void nsXMLHttpRequest::CreateResponseBlob(nsIRequest *request)
fc->GetFile(getter_AddRefs(file));
}
}
bool fromFile = false;
if (file) {
nsCAutoString contentType;
mChannel->GetContentType(contentType);
nsCOMPtr<nsISupports> cacheToken;
if (cc) {
cc->GetCacheToken(getter_AddRefs(cacheToken));
// We need to call IsFromCache to determine whether the response is
// fully cached (i.e. whether we can skip reading the response).
cc->IsFromCache(&fromFile);
} else {
// If the response is coming from the local resource, we can skip
// reading the response unconditionally.
fromFile = true;
}
mResponseBlob =
new nsDOMFileFile(file, NS_ConvertASCIItoUTF16(contentType), cacheToken);
mResponseBody.Truncate();
}
return fromFile;
}
NS_IMETHODIMP
@ -1727,12 +1736,17 @@ nsXMLHttpRequest::OnDataAvailable(nsIRequest *request,
NS_ABORT_IF_FALSE(mContext.get() == ctxt,"start context different from OnDataAvailable context");
if (mResponseType == XML_HTTP_RESPONSE_TYPE_BLOB && !mResponseBlob) {
CreateResponseBlob(request);
}
mProgressSinceLastProgressEvent = true;
if (mResponseType == XML_HTTP_RESPONSE_TYPE_BLOB && !mResponseBlob) {
if (CreateResponseBlob(request)) {
// We don't have to read from the local file for the blob response
mResponseBlob->GetSize(&mLoadTransferred);
ChangeState(XML_HTTP_REQUEST_LOADING);
return request->Cancel(NS_OK);
}
}
PRUint32 totalRead;
nsresult rv = inStr->ReadSegments(nsXMLHttpRequest::StreamReaderFunc,
(void*)this, count, &totalRead);

View File

@ -217,7 +217,7 @@ protected:
PRUint32 *writeCount);
nsresult CreateResponseParsedJSON(JSContext* aCx);
nsresult CreateResponseArrayBuffer(JSContext* aCx);
void CreateResponseBlob(nsIRequest *request);
bool CreateResponseBlob(nsIRequest *request);
// Change the state of the object with this. The broadcast argument
// determines if the onreadystatechange listener should be called.
nsresult ChangeState(PRUint32 aState, bool aBroadcast = true);