2017-10-26 21:39:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2016-07-25 23:41:02 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-11-06 19:51:56 +03:00
|
|
|
#ifndef SlicedInputStream_h
|
|
|
|
#define SlicedInputStream_h
|
2016-07-25 23:41:02 +03:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2018-04-10 18:33:08 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2016-07-25 23:41:02 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsICloneableInputStream.h"
|
2017-02-22 11:41:05 +03:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
2017-02-22 11:41:59 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2018-05-23 08:12:35 +03:00
|
|
|
#include "nsIInputStreamLength.h"
|
2016-07-25 23:41:02 +03:00
|
|
|
|
2017-11-07 03:04:06 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
// A wrapper for a slice of an underlying input stream.
|
|
|
|
|
|
|
|
class SlicedInputStream final : public nsIAsyncInputStream,
|
|
|
|
public nsICloneableInputStream,
|
2017-02-22 11:41:05 +03:00
|
|
|
public nsIIPCSerializableInputStream,
|
2017-02-22 11:41:59 +03:00
|
|
|
public nsISeekableStream,
|
2017-04-12 14:49:08 +03:00
|
|
|
public nsIInputStreamCallback,
|
2018-05-23 08:12:35 +03:00
|
|
|
public nsIInputStreamLength,
|
|
|
|
public nsIAsyncInputStreamLength,
|
|
|
|
public nsIInputStreamLengthCallback {
|
2016-07-25 23:41:02 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
2017-02-22 11:19:50 +03:00
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
2017-02-22 11:41:05 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2017-02-22 11:41:59 +03:00
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
NS_DECL_NSITELLABLESTREAM
|
2017-04-12 14:49:08 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
|
2016-07-25 23:41:02 +03:00
|
|
|
|
|
|
|
// Create an input stream whose data comes from a slice of aInputStream. The
|
|
|
|
// slice begins at aStart bytes beyond aInputStream's current position, and
|
|
|
|
// extends for a maximum of aLength bytes. If aInputStream contains fewer
|
|
|
|
// than aStart bytes, reading from SlicedInputStream returns no data. If
|
|
|
|
// aInputStream contains more than aStart bytes, but fewer than aStart +
|
|
|
|
// aLength bytes, reading from SlicedInputStream returns as many bytes as can
|
|
|
|
// be consumed from aInputStream after reading aLength bytes.
|
|
|
|
//
|
|
|
|
// aInputStream should not be read from after constructing a
|
|
|
|
// SlicedInputStream wrapper around it.
|
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
SlicedInputStream(already_AddRefed<nsIInputStream> aInputStream,
|
2016-07-25 23:41:02 +03:00
|
|
|
uint64_t aStart, uint64_t aLength);
|
|
|
|
|
2017-02-22 11:41:05 +03:00
|
|
|
// This CTOR is meant to be used just for IPC.
|
|
|
|
SlicedInputStream();
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
private:
|
|
|
|
~SlicedInputStream();
|
|
|
|
|
2019-01-28 12:48:35 +03:00
|
|
|
template <typename M>
|
|
|
|
void SerializeInternal(mozilla::ipc::InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors,
|
2019-02-05 01:50:51 +03:00
|
|
|
bool aDelayedStart, uint32_t aMaxSize,
|
|
|
|
uint32_t* aSizeUsed, M* aManager);
|
2019-01-28 12:48:35 +03:00
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
void SetSourceStream(already_AddRefed<nsIInputStream> aInputStream);
|
2017-02-22 11:40:46 +03:00
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
uint64_t AdjustRange(uint64_t aRange);
|
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
2017-02-22 11:40:46 +03:00
|
|
|
|
2017-02-22 11:41:05 +03:00
|
|
|
// Raw pointers because these are just QI of mInputStream.
|
2017-02-22 11:40:46 +03:00
|
|
|
nsICloneableInputStream* mWeakCloneableInputStream;
|
2017-02-22 11:41:05 +03:00
|
|
|
nsIIPCSerializableInputStream* mWeakIPCSerializableInputStream;
|
2017-02-22 11:41:59 +03:00
|
|
|
nsISeekableStream* mWeakSeekableInputStream;
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
nsITellableStream* mWeakTellableInputStream;
|
2017-04-12 14:49:08 +03:00
|
|
|
nsIAsyncInputStream* mWeakAsyncInputStream;
|
2018-05-23 08:12:35 +03:00
|
|
|
nsIInputStreamLength* mWeakInputStreamLength;
|
|
|
|
nsIAsyncInputStreamLength* mWeakAsyncInputStreamLength;
|
2017-02-22 11:40:46 +03:00
|
|
|
|
2016-07-25 23:41:02 +03:00
|
|
|
uint64_t mStart;
|
|
|
|
uint64_t mLength;
|
|
|
|
uint64_t mCurPos;
|
|
|
|
|
|
|
|
bool mClosed;
|
2017-04-12 14:49:08 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
// These four are used for AsyncWait. They are protected by mutex because
|
|
|
|
// touched on multiple threads.
|
2017-04-12 14:49:08 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
|
|
|
|
nsCOMPtr<nsIEventTarget> mAsyncWaitEventTarget;
|
|
|
|
uint32_t mAsyncWaitFlags;
|
|
|
|
uint32_t mAsyncWaitRequestedCount;
|
2018-04-10 18:33:08 +03:00
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
// This is use for nsIAsyncInputStreamLength::AsyncWait.
|
|
|
|
// This is protected by mutex.
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> mAsyncWaitLengthCallback;
|
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
Mutex mMutex;
|
2016-07-25 23:41:02 +03:00
|
|
|
};
|
|
|
|
|
2017-11-07 03:04:06 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-11-06 19:51:56 +03:00
|
|
|
#endif // SlicedInputStream_h
|