2013-09-20 13:11:26 +04: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/. */
|
|
|
|
|
|
|
|
#ifndef CacheFileInputStream__h__
|
|
|
|
#define CacheFileInputStream__h__
|
|
|
|
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "CacheFileChunk.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class CacheFile;
|
|
|
|
|
|
|
|
class CacheFileInputStream : public nsIAsyncInputStream,
|
|
|
|
public nsISeekableStream,
|
|
|
|
public CacheFileChunkListener {
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
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
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
explicit CacheFileInputStream(CacheFile* aFile, nsISupports* aEntry,
|
2016-08-17 13:58:44 +03:00
|
|
|
bool aAlternativeData);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHOD OnChunkRead(nsresult aResult, CacheFileChunk* aChunk) override;
|
|
|
|
NS_IMETHOD OnChunkWritten(nsresult aResult, CacheFileChunk* aChunk) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
NS_IMETHOD OnChunkAvailable(nsresult aResult, uint32_t aChunkIdx,
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunk* aChunk) override;
|
|
|
|
NS_IMETHOD OnChunkUpdated(CacheFileChunk* aChunk) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
|
|
|
|
2014-05-02 22:15:14 +04:00
|
|
|
uint32_t GetPosition() const { return mPos; };
|
2016-08-17 13:58:44 +03:00
|
|
|
bool IsAlternativeData() const { return mAlternativeData; };
|
2017-07-08 02:27:30 +03:00
|
|
|
int64_t GetChunkIdx() const {
|
|
|
|
return mChunk ? static_cast<int64_t>(mChunk->Index()) : -1;
|
|
|
|
};
|
2014-05-02 22:15:14 +04:00
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
private:
|
|
|
|
virtual ~CacheFileInputStream();
|
|
|
|
|
2014-05-19 16:21:18 +04:00
|
|
|
nsresult CloseWithStatusLocked(nsresult aStatus);
|
2016-07-12 18:58:38 +03:00
|
|
|
void CleanUp();
|
2013-09-20 13:11:26 +04:00
|
|
|
void ReleaseChunk();
|
|
|
|
void EnsureCorrectChunk(bool aReleaseOnly);
|
2014-07-10 09:59:29 +04:00
|
|
|
|
|
|
|
// CanRead returns negative value when output stream truncates the data before
|
|
|
|
// the input stream's mPos.
|
2019-05-01 11:47:10 +03:00
|
|
|
int64_t CanRead(CacheFileChunkReadHandle* aHandle);
|
2013-09-20 13:11:26 +04:00
|
|
|
void NotifyListener();
|
|
|
|
void MaybeNotifyListener();
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
RefPtr<CacheFile> mFile;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileChunk> mChunk;
|
2016-07-12 18:58:38 +03:00
|
|
|
int64_t mPos;
|
|
|
|
nsresult mStatus;
|
|
|
|
bool mClosed : 1;
|
|
|
|
bool mInReadSegments : 1;
|
|
|
|
bool mWaitingForUpdate : 1;
|
2016-08-17 13:58:44 +03:00
|
|
|
bool const mAlternativeData : 1;
|
2016-07-12 18:58:38 +03:00
|
|
|
int64_t mListeningForChunk;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
uint32_t mCallbackFlags;
|
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
2016-02-12 16:45:00 +03:00
|
|
|
// Held purely for referencing purposes
|
|
|
|
RefPtr<nsISupports> mCacheEntryHandle;
|
2013-09-20 13:11:26 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
#endif
|