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 CacheFileOutputStream__h__
|
|
|
|
#define CacheFileOutputStream__h__
|
|
|
|
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "CacheFileChunk.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class CacheFile;
|
2013-11-21 02:20:17 +04:00
|
|
|
class CacheOutputCloseListener;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
class CacheFileOutputStream : public nsIAsyncOutputStream,
|
|
|
|
public nsISeekableStream,
|
|
|
|
public CacheFileChunkListener {
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCOUTPUTSTREAM
|
|
|
|
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:
|
2016-08-17 13:58:44 +03:00
|
|
|
CacheFileOutputStream(CacheFile* aFile,
|
|
|
|
CacheOutputCloseListener* aCloseListener,
|
|
|
|
bool aAlternativeData);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2015-03-21 19:28:04 +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,
|
2015-03-21 19:28:04 +03:00
|
|
|
CacheFileChunk* aChunk) override;
|
|
|
|
NS_IMETHOD OnChunkUpdated(CacheFileChunk* aChunk) override;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
void NotifyCloseListener();
|
2016-08-17 13:58:44 +03:00
|
|
|
bool IsAlternativeData() const { return mAlternativeData; };
|
2013-11-21 02:20:17 +04:00
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
private:
|
|
|
|
virtual ~CacheFileOutputStream();
|
|
|
|
|
2014-05-19 16:21:18 +04:00
|
|
|
nsresult CloseWithStatusLocked(nsresult aStatus);
|
2013-09-20 13:11:26 +04:00
|
|
|
void ReleaseChunk();
|
|
|
|
void EnsureCorrectChunk(bool aReleaseOnly);
|
|
|
|
void FillHole();
|
|
|
|
void NotifyListener();
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
RefPtr<CacheFile> mFile;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileChunk> mChunk;
|
|
|
|
RefPtr<CacheOutputCloseListener> mCloseListener;
|
2013-09-20 13:11:26 +04:00
|
|
|
int64_t mPos;
|
2016-08-17 13:58:44 +03:00
|
|
|
bool mClosed : 1;
|
|
|
|
bool const mAlternativeData : 1;
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult mStatus;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> mCallback;
|
|
|
|
uint32_t mCallbackFlags;
|
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
#endif
|