2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +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/. */
|
2000-01-13 12:11:01 +03:00
|
|
|
|
|
|
|
#ifndef nsBufferedStreams_h__
|
|
|
|
#define nsBufferedStreams_h__
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIBufferedStreams.h"
|
2000-01-13 12:11:01 +03:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2004-07-20 02:00:03 +04:00
|
|
|
#include "nsISafeOutputStream.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2001-07-31 23:05:34 +04:00
|
|
|
#include "nsIStreamBufferAccess.h"
|
2000-01-13 12:11:01 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2012-08-23 06:13:54 +04:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
2017-05-02 20:32:42 +03:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2017-12-13 03:38:19 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2018-05-23 08:12:35 +03:00
|
|
|
#include "nsIInputStreamLength.h"
|
2018-04-10 18:33:08 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
class nsBufferedStream : public nsISeekableStream {
|
2000-01-13 12:11:01 +03:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2000-02-04 10:31:43 +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
|
2000-01-13 12:11:01 +03:00
|
|
|
|
|
|
|
nsBufferedStream();
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
nsresult Close();
|
|
|
|
|
2000-02-04 10:31:43 +03:00
|
|
|
protected:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~nsBufferedStream();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Init(nsISupports* stream, uint32_t bufferSize);
|
2017-05-06 00:51:13 +03:00
|
|
|
nsresult GetData(nsISupports** aResult);
|
2000-02-04 10:31:43 +03:00
|
|
|
NS_IMETHOD Fill() = 0;
|
|
|
|
NS_IMETHOD Flush() = 0;
|
2000-01-13 12:11:01 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mBufferSize;
|
2000-01-13 12:11:01 +03:00
|
|
|
char* mBuffer;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
|
|
|
// mBufferStartOffset is the offset relative to the start of mStream.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mBufferStartOffset;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
2000-02-04 10:31:43 +03:00
|
|
|
// mCursor is the read cursor for input streams, or write cursor for
|
2001-07-31 23:05:34 +04:00
|
|
|
// output streams, and is relative to mBufferStartOffset.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCursor;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
2000-02-04 10:31:43 +03:00
|
|
|
// mFillPoint is the amount available in the buffer for input streams,
|
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works
for me on optimized and debug gcc2.96, rh7.1.
- Better failure codes from nsXULPrototypeScript::Deserialize.
- Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize
failure, instead of just nulling the FastLoad service's output stream.
- Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from
nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for
good measure.
- The needless "Current" adjective in nsIFastLoadService attribute and method
names is no more.
- Add a do_GetFastLoadService() helper, to use CID instead of contractid, and
to let the compiler consolidate the static inline CID.
- Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without
the checksum verification step when reading a FastLoad file.
- Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad
service so we don't recompute it when re-opening the FastLoad file as mailnews
and other top-levels start up. Fill the checksum cache in EndFastLoad, when
the last pseudo-concurrent top-level finishes loading.
My hope to compute the checksum while writing the FastLoad file ran afoul of
misordered writes. The old code to checksum the in-memory nsFastLoadHeader
also was broken on little endian platforms. Now all checksumming is done via
a separate read pass over the complete file, save for the header's checksum
field, which is summed as if it contained zero.
- Track and check FastLoad file dependencies. This required groveling with a
bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it
and weep. Dependency checking, as well as checksum access and computation,
use better-factored nsIFastLoad{File,Read,Write}Control interfaces.
- nsBufferedStream::Seek wasn't flushing the buffer when seeking backward
within the buffer, but it must, because mCursor bounds the amount to write
if the buffer contains the end of file.
- Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we
don't have to screw around with the bufferying layer when checksumming. Also
implement nsIStreamBufferAccess in nsBufferedOutputStream.
- nsISeekableOutputStream was bogus, based on a bad state I had put the
nsBufferedOutputStream code in on its way from being completely broken when
you seek backwards outside of the buffer. Removing this interface required
using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful
ordering of Close calls (the Reader must close after the Writer or Updater,
so that the Reader's underlying, unbuffered input stream can be read by
nsFastLoadFileWriter::Close to compute the checksum.
- Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style,
nsnull vs. 0, useless variable elimination, tortured control flow,
AutoString instead of String, and gratuitous ; after nsISupportsUtils.h
macro call cleanups.
2001-08-22 00:51:34 +04:00
|
|
|
// or the high watermark of bytes written into the buffer, and therefore
|
|
|
|
// is relative to mBufferStartOffset.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mFillPoint;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
nsISupports* mStream; // cast to appropriate subclass
|
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works
for me on optimized and debug gcc2.96, rh7.1.
- Better failure codes from nsXULPrototypeScript::Deserialize.
- Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize
failure, instead of just nulling the FastLoad service's output stream.
- Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from
nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for
good measure.
- The needless "Current" adjective in nsIFastLoadService attribute and method
names is no more.
- Add a do_GetFastLoadService() helper, to use CID instead of contractid, and
to let the compiler consolidate the static inline CID.
- Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without
the checksum verification step when reading a FastLoad file.
- Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad
service so we don't recompute it when re-opening the FastLoad file as mailnews
and other top-levels start up. Fill the checksum cache in EndFastLoad, when
the last pseudo-concurrent top-level finishes loading.
My hope to compute the checksum while writing the FastLoad file ran afoul of
misordered writes. The old code to checksum the in-memory nsFastLoadHeader
also was broken on little endian platforms. Now all checksumming is done via
a separate read pass over the complete file, save for the header's checksum
field, which is summed as if it contained zero.
- Track and check FastLoad file dependencies. This required groveling with a
bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it
and weep. Dependency checking, as well as checksum access and computation,
use better-factored nsIFastLoad{File,Read,Write}Control interfaces.
- nsBufferedStream::Seek wasn't flushing the buffer when seeking backward
within the buffer, but it must, because mCursor bounds the amount to write
if the buffer contains the end of file.
- Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we
don't have to screw around with the bufferying layer when checksumming. Also
implement nsIStreamBufferAccess in nsBufferedOutputStream.
- nsISeekableOutputStream was bogus, based on a bad state I had put the
nsBufferedOutputStream code in on its way from being completely broken when
you seek backwards outside of the buffer. Removing this interface required
using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful
ordering of Close calls (the Reader must close after the Writer or Updater,
so that the Reader's underlying, unbuffered input stream can be read by
nsFastLoadFileWriter::Close to compute the checksum.
- Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style,
nsnull vs. 0, useless variable elimination, tortured control flow,
AutoString instead of String, and gratuitous ; after nsISupportsUtils.h
macro call cleanups.
2001-08-22 00:51:34 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mBufferDisabled;
|
2011-10-19 00:17:10 +04:00
|
|
|
bool mEOF; // True if mStream is at EOF
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mGetBufferCount;
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-03-09 17:37:18 +03:00
|
|
|
class nsBufferedInputStream final : public nsBufferedStream,
|
|
|
|
public nsIBufferedInputStream,
|
|
|
|
public nsIStreamBufferAccess,
|
|
|
|
public nsIIPCSerializableInputStream,
|
|
|
|
public nsIAsyncInputStream,
|
|
|
|
public nsIInputStreamCallback,
|
2018-05-23 08:12:35 +03:00
|
|
|
public nsICloneableInputStream,
|
|
|
|
public nsIInputStreamLength,
|
|
|
|
public nsIAsyncInputStreamLength,
|
|
|
|
public nsIInputStreamLengthCallback {
|
2000-01-13 12:11:01 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
2000-02-04 10:31:43 +03:00
|
|
|
NS_DECL_NSIBUFFEREDINPUTSTREAM
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_DECL_NSISTREAMBUFFERACCESS
|
2012-08-23 06:13:54 +04:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2017-05-02 20:32:42 +03:00
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2017-12-13 03:38:19 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
2018-05-23 08:12:35 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAMLENGTH
|
|
|
|
NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-28 09:14:41 +03:00
|
|
|
nsBufferedInputStream();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2000-01-13 12:11:01 +03:00
|
|
|
static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2000-02-04 10:31:43 +03:00
|
|
|
nsIInputStream* Source() { return (nsIInputStream*)mStream; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2000-02-04 10:31:43 +03:00
|
|
|
protected:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~nsBufferedInputStream() = default;
|
2018-11-30 13:46:48 +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
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Fill() override;
|
|
|
|
NS_IMETHOD Flush() override { return NS_OK; } // no-op for input streams
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
mozilla::Mutex mMutex;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-10 18:33:08 +03:00
|
|
|
// This value is protected by mutex.
|
2017-05-02 20:32:42 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
// This value is protected by mutex.
|
|
|
|
nsCOMPtr<nsIInputStreamLengthCallback> mAsyncInputStreamLengthCallback;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-28 09:14:41 +03:00
|
|
|
bool mIsIPCSerializable;
|
|
|
|
bool mIsAsyncInputStream;
|
|
|
|
bool mIsCloneableInputStream;
|
2018-05-23 08:12:35 +03:00
|
|
|
bool mIsInputStreamLength;
|
|
|
|
bool mIsAsyncInputStreamLength;
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-19 08:17:07 +03:00
|
|
|
class nsBufferedOutputStream : public nsBufferedStream,
|
|
|
|
public nsISafeOutputStream,
|
|
|
|
public nsIBufferedOutputStream,
|
|
|
|
public nsIStreamBufferAccess {
|
2000-01-13 12:11:01 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
2004-07-20 02:00:03 +04:00
|
|
|
NS_DECL_NSISAFEOUTPUTSTREAM
|
2000-02-04 10:31:43 +03:00
|
|
|
NS_DECL_NSIBUFFEREDOUTPUTSTREAM
|
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works
for me on optimized and debug gcc2.96, rh7.1.
- Better failure codes from nsXULPrototypeScript::Deserialize.
- Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize
failure, instead of just nulling the FastLoad service's output stream.
- Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from
nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for
good measure.
- The needless "Current" adjective in nsIFastLoadService attribute and method
names is no more.
- Add a do_GetFastLoadService() helper, to use CID instead of contractid, and
to let the compiler consolidate the static inline CID.
- Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without
the checksum verification step when reading a FastLoad file.
- Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad
service so we don't recompute it when re-opening the FastLoad file as mailnews
and other top-levels start up. Fill the checksum cache in EndFastLoad, when
the last pseudo-concurrent top-level finishes loading.
My hope to compute the checksum while writing the FastLoad file ran afoul of
misordered writes. The old code to checksum the in-memory nsFastLoadHeader
also was broken on little endian platforms. Now all checksumming is done via
a separate read pass over the complete file, save for the header's checksum
field, which is summed as if it contained zero.
- Track and check FastLoad file dependencies. This required groveling with a
bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it
and weep. Dependency checking, as well as checksum access and computation,
use better-factored nsIFastLoad{File,Read,Write}Control interfaces.
- nsBufferedStream::Seek wasn't flushing the buffer when seeking backward
within the buffer, but it must, because mCursor bounds the amount to write
if the buffer contains the end of file.
- Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we
don't have to screw around with the bufferying layer when checksumming. Also
implement nsIStreamBufferAccess in nsBufferedOutputStream.
- nsISeekableOutputStream was bogus, based on a bad state I had put the
nsBufferedOutputStream code in on its way from being completely broken when
you seek backwards outside of the buffer. Removing this interface required
using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful
ordering of Close calls (the Reader must close after the Writer or Updater,
so that the Reader's underlying, unbuffered input stream can be read by
nsFastLoadFileWriter::Close to compute the checksum.
- Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style,
nsnull vs. 0, useless variable elimination, tortured control flow,
AutoString instead of String, and gratuitous ; after nsISupportsUtils.h
macro call cleanups.
2001-08-22 00:51:34 +04:00
|
|
|
NS_DECL_NSISTREAMBUFFERACCESS
|
2000-01-13 12:11:01 +03:00
|
|
|
|
|
|
|
nsBufferedOutputStream() : nsBufferedStream() {}
|
|
|
|
|
|
|
|
static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
|
|
|
|
2000-02-04 10:31:43 +03:00
|
|
|
nsIOutputStream* Sink() { return (nsIOutputStream*)mStream; }
|
|
|
|
|
|
|
|
protected:
|
2014-07-09 01:23:18 +04:00
|
|
|
virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Fill() override { return NS_OK; } // no-op for output streams
|
2004-07-20 02:00:03 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISafeOutputStream> mSafeStream; // QI'd from mStream
|
2000-01-13 12:11:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#endif // nsBufferedStreams_h__
|