зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1339710 - SlicedInputStream should be an nsIIPCSerializableInputStream, r=smaug
This commit is contained in:
Родитель
f981e3155b
Коммит
a75afeb38f
|
@ -64,6 +64,15 @@ struct SameProcessInputStreamParams
|
|||
intptr_t addRefedInputStream;
|
||||
};
|
||||
|
||||
struct SlicedInputStreamParams
|
||||
{
|
||||
InputStreamParams stream;
|
||||
uint64_t start;
|
||||
uint64_t length;
|
||||
uint64_t curPos;
|
||||
bool closed;
|
||||
};
|
||||
|
||||
union InputStreamParams
|
||||
{
|
||||
StringInputStreamParams;
|
||||
|
@ -75,6 +84,7 @@ union InputStreamParams
|
|||
MultiplexInputStreamParams;
|
||||
RemoteInputStreamParams;
|
||||
SameProcessInputStreamParams;
|
||||
SlicedInputStreamParams;
|
||||
};
|
||||
|
||||
union OptionalInputStreamParams
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "nsStringStream.h"
|
||||
#include "nsTemporaryFileInputStream.h"
|
||||
#include "nsXULAppAPI.h"
|
||||
#include "SlicedInputStream.h"
|
||||
|
||||
using namespace mozilla::dom;
|
||||
|
||||
|
@ -148,6 +149,10 @@ DeserializeInputStream(const InputStreamParams& aParams,
|
|||
return stream.forget();
|
||||
}
|
||||
|
||||
case InputStreamParams::TSlicedInputStreamParams:
|
||||
serializable = new SlicedInputStream();
|
||||
break;
|
||||
|
||||
default:
|
||||
MOZ_ASSERT(false, "Unknown params!");
|
||||
return nullptr;
|
||||
|
|
|
@ -4,9 +4,12 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "SlicedInputStream.h"
|
||||
#include "mozilla/ipc/InputStreamUtils.h"
|
||||
#include "nsISeekableStream.h"
|
||||
#include "nsStreamUtils.h"
|
||||
|
||||
using namespace mozilla::ipc;
|
||||
|
||||
NS_IMPL_ADDREF(SlicedInputStream);
|
||||
NS_IMPL_RELEASE(SlicedInputStream);
|
||||
|
||||
|
@ -14,13 +17,16 @@ NS_INTERFACE_MAP_BEGIN(SlicedInputStream)
|
|||
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIAsyncInputStream)
|
||||
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
|
||||
mWeakCloneableInputStream)
|
||||
mWeakCloneableInputStream || !mInputStream)
|
||||
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
|
||||
mWeakIPCSerializableInputStream || !mInputStream)
|
||||
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
||||
NS_INTERFACE_MAP_END
|
||||
|
||||
SlicedInputStream::SlicedInputStream(nsIInputStream* aInputStream,
|
||||
uint64_t aStart, uint64_t aLength)
|
||||
: mWeakCloneableInputStream(nullptr)
|
||||
, mWeakIPCSerializableInputStream(nullptr)
|
||||
, mStart(aStart)
|
||||
, mLength(aLength)
|
||||
, mCurPos(0)
|
||||
|
@ -30,12 +36,22 @@ SlicedInputStream::SlicedInputStream(nsIInputStream* aInputStream,
|
|||
SetSourceStream(aInputStream);
|
||||
}
|
||||
|
||||
SlicedInputStream::SlicedInputStream()
|
||||
: mWeakCloneableInputStream(nullptr)
|
||||
, mWeakIPCSerializableInputStream(nullptr)
|
||||
, mStart(0)
|
||||
, mLength(0)
|
||||
, mCurPos(0)
|
||||
, mClosed(false)
|
||||
{}
|
||||
|
||||
SlicedInputStream::~SlicedInputStream()
|
||||
{}
|
||||
|
||||
void
|
||||
SlicedInputStream::SetSourceStream(nsIInputStream* aInputStream)
|
||||
{
|
||||
MOZ_ASSERT(!mInputStream);
|
||||
MOZ_ASSERT(aInputStream);
|
||||
|
||||
mInputStream = aInputStream;
|
||||
|
@ -45,11 +61,20 @@ SlicedInputStream::SetSourceStream(nsIInputStream* aInputStream)
|
|||
if (cloneableStream && SameCOMIdentity(aInputStream, cloneableStream)) {
|
||||
mWeakCloneableInputStream = cloneableStream;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIIPCSerializableInputStream> serializableStream =
|
||||
do_QueryInterface(aInputStream);
|
||||
if (serializableStream &&
|
||||
SameCOMIdentity(aInputStream, serializableStream)) {
|
||||
mWeakIPCSerializableInputStream = serializableStream;
|
||||
}
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
SlicedInputStream::Close()
|
||||
{
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
|
||||
mClosed = true;
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -59,6 +84,8 @@ SlicedInputStream::Close()
|
|||
NS_IMETHODIMP
|
||||
SlicedInputStream::Available(uint64_t* aLength)
|
||||
{
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
|
||||
if (mClosed) {
|
||||
return NS_BASE_STREAM_CLOSED;
|
||||
}
|
||||
|
@ -91,6 +118,8 @@ NS_IMETHODIMP
|
|||
SlicedInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
||||
uint32_t aCount, uint32_t *aResult)
|
||||
{
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
|
||||
uint32_t result;
|
||||
|
||||
if (!aResult) {
|
||||
|
@ -166,6 +195,7 @@ SlicedInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|||
NS_IMETHODIMP
|
||||
SlicedInputStream::IsNonBlocking(bool* aNonBlocking)
|
||||
{
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
return mInputStream->IsNonBlocking(aNonBlocking);
|
||||
}
|
||||
|
||||
|
@ -174,7 +204,9 @@ SlicedInputStream::IsNonBlocking(bool* aNonBlocking)
|
|||
NS_IMETHODIMP
|
||||
SlicedInputStream::GetCloneable(bool* aCloneable)
|
||||
{
|
||||
MOZ_ASSERT(mWeakCloneableInputStream);
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
NS_ENSURE_STATE(mWeakCloneableInputStream);
|
||||
|
||||
*aCloneable = true;
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -182,7 +214,8 @@ SlicedInputStream::GetCloneable(bool* aCloneable)
|
|||
NS_IMETHODIMP
|
||||
SlicedInputStream::Clone(nsIInputStream** aResult)
|
||||
{
|
||||
MOZ_ASSERT(mWeakCloneableInputStream);
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
NS_ENSURE_STATE(mWeakCloneableInputStream);
|
||||
|
||||
nsCOMPtr<nsIInputStream> clonedStream;
|
||||
nsresult rv = mWeakCloneableInputStream->Clone(getter_AddRefs(clonedStream));
|
||||
|
@ -202,6 +235,8 @@ SlicedInputStream::Clone(nsIInputStream** aResult)
|
|||
NS_IMETHODIMP
|
||||
SlicedInputStream::CloseWithStatus(nsresult aStatus)
|
||||
{
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
|
||||
nsCOMPtr<nsIAsyncInputStream> asyncStream =
|
||||
do_QueryInterface(mInputStream);
|
||||
if (!asyncStream) {
|
||||
|
@ -217,6 +252,8 @@ SlicedInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
|
|||
uint32_t aRequestedCount,
|
||||
nsIEventTarget* aEventTarget)
|
||||
{
|
||||
NS_ENSURE_STATE(mInputStream);
|
||||
|
||||
nsCOMPtr<nsIAsyncInputStream> asyncStream =
|
||||
do_QueryInterface(mInputStream);
|
||||
if (!asyncStream) {
|
||||
|
@ -226,3 +263,65 @@ SlicedInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
|
|||
return asyncStream->AsyncWait(aCallback, aFlags, aRequestedCount,
|
||||
aEventTarget);
|
||||
}
|
||||
|
||||
// nsIIPCSerializableInputStream
|
||||
|
||||
void
|
||||
SlicedInputStream::Serialize(mozilla::ipc::InputStreamParams& aParams,
|
||||
FileDescriptorArray& aFileDescriptors)
|
||||
{
|
||||
MOZ_ASSERT(mInputStream);
|
||||
MOZ_ASSERT(mWeakIPCSerializableInputStream);
|
||||
|
||||
SlicedInputStreamParams params;
|
||||
SerializeInputStream(mInputStream, params.stream(), aFileDescriptors);
|
||||
params.start() = mStart;
|
||||
params.length() = mLength;
|
||||
params.curPos() = mCurPos;
|
||||
params.closed() = mClosed;
|
||||
|
||||
aParams = params;
|
||||
}
|
||||
|
||||
bool
|
||||
SlicedInputStream::Deserialize(const mozilla::ipc::InputStreamParams& aParams,
|
||||
const FileDescriptorArray& aFileDescriptors)
|
||||
{
|
||||
MOZ_ASSERT(!mInputStream);
|
||||
MOZ_ASSERT(!mWeakIPCSerializableInputStream);
|
||||
|
||||
if (aParams.type() !=
|
||||
InputStreamParams::TSlicedInputStreamParams) {
|
||||
NS_ERROR("Received unknown parameters from the other process!");
|
||||
return false;
|
||||
}
|
||||
|
||||
const SlicedInputStreamParams& params =
|
||||
aParams.get_SlicedInputStreamParams();
|
||||
|
||||
nsCOMPtr<nsIInputStream> stream =
|
||||
DeserializeInputStream(params.stream(), aFileDescriptors);
|
||||
if (!stream) {
|
||||
NS_WARNING("Deserialize failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
SetSourceStream(stream);
|
||||
|
||||
mStart = params.start();
|
||||
mLength = params.length();
|
||||
mCurPos = params.curPos();
|
||||
mClosed = params.closed();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
mozilla::Maybe<uint64_t>
|
||||
SlicedInputStream::ExpectedSerializedLength()
|
||||
{
|
||||
if (!mInputStream || !mWeakIPCSerializableInputStream) {
|
||||
return mozilla::Nothing();
|
||||
}
|
||||
|
||||
return mWeakIPCSerializableInputStream->ExpectedSerializedLength();
|
||||
}
|
||||
|
|
|
@ -10,17 +10,20 @@
|
|||
#include "nsCOMPtr.h"
|
||||
#include "nsIAsyncInputStream.h"
|
||||
#include "nsICloneableInputStream.h"
|
||||
#include "nsIIPCSerializableInputStream.h"
|
||||
|
||||
// A wrapper for a slice of an underlying input stream.
|
||||
|
||||
class SlicedInputStream final : public nsIAsyncInputStream
|
||||
, public nsICloneableInputStream
|
||||
, public nsIIPCSerializableInputStream
|
||||
{
|
||||
public:
|
||||
NS_DECL_THREADSAFE_ISUPPORTS
|
||||
NS_DECL_NSIINPUTSTREAM
|
||||
NS_DECL_NSICLONEABLEINPUTSTREAM
|
||||
NS_DECL_NSIASYNCINPUTSTREAM
|
||||
NS_DECL_NSICLONEABLEINPUTSTREAM
|
||||
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
||||
|
||||
// Create an input stream whose data comes from a slice of aInputStream. The
|
||||
// slice begins at aStart bytes beyond aInputStream's current position, and
|
||||
|
@ -36,6 +39,9 @@ public:
|
|||
SlicedInputStream(nsIInputStream* aInputStream,
|
||||
uint64_t aStart, uint64_t aLength);
|
||||
|
||||
// This CTOR is meant to be used just for IPC.
|
||||
SlicedInputStream();
|
||||
|
||||
private:
|
||||
~SlicedInputStream();
|
||||
|
||||
|
@ -44,8 +50,9 @@ private:
|
|||
|
||||
nsCOMPtr<nsIInputStream> mInputStream;
|
||||
|
||||
// Raw pointer because this is just QI of mInputStream.
|
||||
// Raw pointers because these are just QI of mInputStream.
|
||||
nsICloneableInputStream* mWeakCloneableInputStream;
|
||||
nsIIPCSerializableInputStream* mWeakIPCSerializableInputStream;
|
||||
|
||||
uint64_t mStart;
|
||||
uint64_t mLength;
|
||||
|
|
Загрузка…
Ссылка в новой задаче