Bug 1759569 - Retain nsIInputStreamLength through NS_CloneInputStream, r=asuth

This will help in bug 1754004 as in some cases we will want to clone a
DataPipe, which is non-cloneable, and without this change length information
would be lost.

In the future it might be worth making a more generic mechanism for efficiently
cloning wrapper input streams with a fallback rather than hard-coding this
interface.

Differential Revision: https://phabricator.services.mozilla.com/D141035
This commit is contained in:
Nika Layzell 2022-05-13 14:16:08 +00:00
Родитель a906eaf6b2
Коммит 366aafadae
1 изменённых файлов: 12 добавлений и 0 удалений

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

@ -6,6 +6,8 @@
#include "mozilla/Mutex.h"
#include "mozilla/Attributes.h"
#include "mozilla/InputStreamLengthWrapper.h"
#include "nsIInputStreamLength.h"
#include "nsStreamUtils.h"
#include "nsCOMPtr.h"
#include "nsICloneableInputStream.h"
@ -870,6 +872,16 @@ nsresult NS_CloneInputStream(nsIInputStream* aSource,
return rv;
}
// Propagate length information provided by nsIInputStreamLength. We don't use
// InputStreamLengthHelper::GetSyncLength to avoid the risk of blocking when
// called off-main-thread.
int64_t length = -1;
if (nsCOMPtr<nsIInputStreamLength> streamLength = do_QueryInterface(aSource);
streamLength && NS_SUCCEEDED(streamLength->Length(&length)) &&
length != -1) {
reader = new mozilla::InputStreamLengthWrapper(reader.forget(), length);
}
cloneable = do_QueryInterface(reader);
MOZ_ASSERT(cloneable && cloneable->GetCloneable());