зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1353629 - PBlob refactoring - part 8 - FileReader should use nsIAsyncInputStream if available, r=smaug
Currently FileReader API uses a nsITransport. This is not needed if the inputStream is a nsIAsyncInputStream already, and IPCBlobInputStream is always a nsIAsyncInputStream. Note that, we must create a bufferedStream in order to use ReadSegments in case the remote inputStream, received by IPCBlobInputStream, is a FileInputStream. This was not needed with nsITransport.
This commit is contained in:
Родитель
d6659b61c0
Коммит
7bbba02e9a
|
@ -290,10 +290,20 @@ FileReader::DoReadData(uint64_t aCount)
|
|||
mResult.GetMutableData(&buf, oldLen + aCount, fallible);
|
||||
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
||||
|
||||
nsresult rv;
|
||||
|
||||
// nsFileStreams do not implement ReadSegment. In case here we are dealing
|
||||
// with a nsIAsyncInputStream, in content process, we need to wrap a
|
||||
// nsIBufferedInputStream around it.
|
||||
if (!mBufferedStream) {
|
||||
rv = NS_NewBufferedInputStream(getter_AddRefs(mBufferedStream),
|
||||
mAsyncStream, 8192);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
uint32_t bytesRead = 0;
|
||||
nsresult rv =
|
||||
mAsyncStream->ReadSegments(ReadFuncBinaryString, buf + oldLen, aCount,
|
||||
&bytesRead);
|
||||
rv = mBufferedStream->ReadSegments(ReadFuncBinaryString, buf + oldLen,
|
||||
aCount, &bytesRead);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
@ -350,6 +360,7 @@ FileReader::ReadFileContent(Blob& aBlob,
|
|||
mResultArrayBuffer = nullptr;
|
||||
|
||||
mAsyncStream = nullptr;
|
||||
mBufferedStream = nullptr;
|
||||
|
||||
mTransferred = 0;
|
||||
mTotal = 0;
|
||||
|
@ -374,27 +385,30 @@ FileReader::ReadFileContent(Blob& aBlob,
|
|||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsITransport> transport;
|
||||
aRv = sts->CreateInputTransport(stream,
|
||||
/* aStartOffset */ 0,
|
||||
/* aReadLimit */ -1,
|
||||
/* aCloseWhenDone */ true,
|
||||
getter_AddRefs(transport));
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return;
|
||||
mAsyncStream = do_QueryInterface(stream);
|
||||
if (!mAsyncStream) {
|
||||
nsCOMPtr<nsITransport> transport;
|
||||
aRv = sts->CreateInputTransport(stream,
|
||||
/* aStartOffset */ 0,
|
||||
/* aReadLimit */ -1,
|
||||
/* aCloseWhenDone */ true,
|
||||
getter_AddRefs(transport));
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIInputStream> wrapper;
|
||||
aRv = transport->OpenInputStream(/* aFlags */ 0,
|
||||
/* aSegmentSize */ 0,
|
||||
/* aSegmentCount */ 0,
|
||||
getter_AddRefs(wrapper));
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return;
|
||||
}
|
||||
|
||||
mAsyncStream = do_QueryInterface(wrapper);
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIInputStream> wrapper;
|
||||
aRv = transport->OpenInputStream(/* aFlags */ 0,
|
||||
/* aSegmentSize */ 0,
|
||||
/* aSegmentCount */ 0,
|
||||
getter_AddRefs(wrapper));
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(!mAsyncStream);
|
||||
mAsyncStream = do_QueryInterface(wrapper);
|
||||
MOZ_ASSERT(mAsyncStream);
|
||||
|
||||
mTotal = mBlob->GetSize(aRv);
|
||||
|
@ -529,6 +543,7 @@ FileReader::FreeDataAndDispatchSuccess()
|
|||
FreeFileData();
|
||||
mResult.SetIsVoid(false);
|
||||
mAsyncStream = nullptr;
|
||||
mBufferedStream = nullptr;
|
||||
mBlob = nullptr;
|
||||
|
||||
// Dispatch event to signify end of a successful operation
|
||||
|
@ -544,6 +559,7 @@ FileReader::FreeDataAndDispatchError()
|
|||
FreeFileData();
|
||||
mResult.SetIsVoid(true);
|
||||
mAsyncStream = nullptr;
|
||||
mBufferedStream = nullptr;
|
||||
mBlob = nullptr;
|
||||
|
||||
// Dispatch error event to signify load failure
|
||||
|
@ -729,6 +745,7 @@ FileReader::Abort()
|
|||
mResultArrayBuffer = nullptr;
|
||||
|
||||
mAsyncStream = nullptr;
|
||||
mBufferedStream = nullptr;
|
||||
mBlob = nullptr;
|
||||
|
||||
//Clean up memory buffer
|
||||
|
@ -782,6 +799,11 @@ FileReader::Shutdown()
|
|||
mAsyncStream = nullptr;
|
||||
}
|
||||
|
||||
if (mBufferedStream) {
|
||||
mBufferedStream->Close();
|
||||
mBufferedStream = nullptr;
|
||||
}
|
||||
|
||||
FreeFileData();
|
||||
mResultArrayBuffer = nullptr;
|
||||
|
||||
|
|
|
@ -180,6 +180,7 @@ private:
|
|||
bool mTimerIsActive;
|
||||
|
||||
nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
|
||||
nsCOMPtr<nsIInputStream> mBufferedStream;
|
||||
|
||||
RefPtr<DOMError> mError;
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче