2017-09-08 17:06:26 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef PartiallySeekableInputStream_h
|
|
|
|
#define PartiallySeekableInputStream_h
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2018-04-10 18:33:09 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2017-09-08 17:06:26 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsICloneableInputStream.h"
|
2018-05-23 08:12:35 +03:00
|
|
|
#include "nsIInputStreamLength.h"
|
2017-09-08 17:06:26 +03:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
// A wrapper for making a stream seekable for the first |aBufferSize| bytes.
|
2017-10-13 11:07:32 +03:00
|
|
|
// Note that this object takes the ownership of the underlying stream.
|
2017-09-08 17:06:26 +03:00
|
|
|
|
|
|
|
class PartiallySeekableInputStream final : public nsISeekableStream,
|
|
|
|
public nsIAsyncInputStream,
|
|
|
|
public nsICloneableInputStream,
|
|
|
|
public nsIIPCSerializableInputStream,
|
|
|
|
public nsIInputStreamCallback,
|
2018-05-23 08:12:35 +03:00
|
|
|
public nsIInputStreamLength,
|
|
|
|
public nsIAsyncInputStreamLength,
|
|
|
|
public nsIInputStreamLengthCallback {
|
2017-09-08 17:06:26 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
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-09-08 17:06:26 +03:00
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
|
2017-09-08 17:06:26 +03:00
|
|
|
|
2017-10-13 11:07:32 +03:00
|
|
|
explicit PartiallySeekableInputStream(
|
|
|
|
already_AddRefed<nsIInputStream> aInputStream,
|
2017-09-08 17:06:26 +03:00
|
|
|
uint64_t aBufferSize = 4096);
|
|
|
|
|
|
|
|
private:
|
2017-12-13 17:56:31 +03:00
|
|
|
PartiallySeekableInputStream(
|
|
|
|
already_AddRefed<nsIInputStream> aClonedBaseStream,
|
|
|
|
PartiallySeekableInputStream* aClonedFrom);
|
|
|
|
|
|
|
|
~PartiallySeekableInputStream() = default;
|
|
|
|
|
|
|
|
void Init();
|
2017-09-08 17:06:26 +03:00
|
|
|
|
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-09-08 17:06:26 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
|
|
|
|
|
|
|
// Raw pointers because these are just QI of mInputStream.
|
|
|
|
nsICloneableInputStream* mWeakCloneableInputStream;
|
|
|
|
nsIIPCSerializableInputStream* mWeakIPCSerializableInputStream;
|
|
|
|
nsIAsyncInputStream* mWeakAsyncInputStream;
|
2018-05-23 08:12:35 +03:00
|
|
|
nsIInputStreamLength* mWeakInputStreamLength;
|
|
|
|
nsIAsyncInputStreamLength* mWeakAsyncInputStreamLength;
|
2017-09-08 17:06:26 +03:00
|
|
|
|
2018-04-10 18:33:09 +03:00
|
|
|
// Protected by mutex.
|
2017-09-08 17:06:26 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
// Protected by mutex.
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> mAsyncInputStreamLengthCallback;
|
|
|
|
|
2017-09-08 17:06:26 +03:00
|
|
|
nsTArray<char> mCachedBuffer;
|
|
|
|
|
|
|
|
uint64_t mBufferSize;
|
|
|
|
uint64_t mPos;
|
|
|
|
bool mClosed;
|
2018-04-10 18:33:09 +03:00
|
|
|
|
|
|
|
Mutex mMutex;
|
2017-09-08 17:06:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // PartiallySeekableInputStream_h
|