Bug 1791721 - Create a dedicated interface for read write (input output) streams; r=xpcom-reviewers,necko-reviewers,nika,valentin

Differential Revision: https://phabricator.services.mozilla.com/D157786
This commit is contained in:
Jan Varga 2022-09-23 05:36:24 +00:00
parent 1245ef6f0b
commit a82e48e896
10 changed files with 96 additions and 24 deletions

View file

@ -1165,7 +1165,7 @@ already_AddRefed<nsISupports> BackgroundMutableFileParentBase::CreateStream(
return stream.forget(); return stream.forget();
} }
nsCOMPtr<nsIFileStream> stream; nsCOMPtr<nsIRandomAccessStream> stream;
rv = NS_NewLocalFileStream(getter_AddRefs(stream), mFile, -1, -1, rv = NS_NewLocalFileStream(getter_AddRefs(stream), mFile, -1, -1,
nsIFileStream::DEFER_OPEN); nsIFileStream::DEFER_OPEN);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {

View file

@ -1415,11 +1415,8 @@ nsresult SeekOp::DoDatabaseWork(nsIFileStream* aFileStream) {
AssertIsOnIOThread(); AssertIsOnIOThread();
MOZ_ASSERT(aFileStream); MOZ_ASSERT(aFileStream);
nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(aFileStream);
MOZ_ASSERT(seekableStream);
nsresult rv = nsresult rv =
seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, mParams.offset()); aFileStream->Seek(nsISeekableStream::NS_SEEK_SET, mParams.offset());
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return rv;

View file

@ -876,8 +876,29 @@ nsresult nsFileStream::Create(REFNSIID aIID, void** aResult) {
return stream->QueryInterface(aIID, aResult); return stream->QueryInterface(aIID, aResult);
} }
NS_IMPL_ISUPPORTS_INHERITED(nsFileStream, nsFileStreamBase, nsIInputStream, NS_IMPL_ISUPPORTS_INHERITED(nsFileStream, nsFileStreamBase,
nsIOutputStream, nsIFileStream) nsIRandomAccessStream, nsIFileStream,
nsIInputStream, nsIOutputStream)
NS_IMETHODIMP
nsFileStream::GetInputStream(nsIInputStream** aInputStream) {
nsCOMPtr<nsIInputStream> inputStream(this);
inputStream.forget(aInputStream);
return NS_OK;
}
NS_IMETHODIMP
nsFileStream::GetOutputStream(nsIOutputStream** aOutputStream) {
nsCOMPtr<nsIOutputStream> outputStream(this);
outputStream.forget(aOutputStream);
return NS_OK;
}
nsIInputStream* nsFileStream::InputStream() { return this; }
nsIOutputStream* nsFileStream::OutputStream() { return this; }
NS_IMETHODIMP NS_IMETHODIMP
nsFileStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm, nsFileStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,

View file

@ -12,6 +12,7 @@
#include "nsICloneableInputStream.h" #include "nsICloneableInputStream.h"
#include "nsIInputStream.h" #include "nsIInputStream.h"
#include "nsIOutputStream.h" #include "nsIOutputStream.h"
#include "nsIRandomAccessStream.h"
#include "nsISafeOutputStream.h" #include "nsISafeOutputStream.h"
#include "nsISeekableStream.h" #include "nsISeekableStream.h"
#include "nsILineInputStream.h" #include "nsILineInputStream.h"
@ -251,13 +252,16 @@ class nsSafeFileOutputStream : public nsAtomicFileOutputStream {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class nsFileStream : public nsFileStreamBase, class nsFileStream : public nsFileStreamBase,
public nsIFileStream,
public nsIInputStream, public nsIInputStream,
public nsIOutputStream, public nsIOutputStream {
public nsIFileStream {
public: public:
static nsresult Create(REFNSIID aIID, void** aResult); static nsresult Create(REFNSIID aIID, void** aResult);
NS_DECL_ISUPPORTS_INHERITED NS_DECL_ISUPPORTS_INHERITED
NS_FORWARD_NSITELLABLESTREAM(nsFileStreamBase::)
NS_FORWARD_NSISEEKABLESTREAM(nsFileStreamBase::)
NS_DECL_NSIRANDOMACCESSSTREAM
NS_DECL_NSIFILESTREAM NS_DECL_NSIFILESTREAM
NS_FORWARD_NSIINPUTSTREAM(nsFileStreamBase::) NS_FORWARD_NSIINPUTSTREAM(nsFileStreamBase::)

View file

@ -5,6 +5,7 @@
#include "nsIInputStream.idl" #include "nsIInputStream.idl"
#include "nsIOutputStream.idl" #include "nsIOutputStream.idl"
#include "nsIRandomAccessStream.idl"
interface nsIEventTarget; interface nsIEventTarget;
interface nsIFile; interface nsIFile;
@ -133,7 +134,7 @@ interface nsIFileOutputStream : nsIOutputStream
* A stream that allows you to read from a file or stream to a file. * A stream that allows you to read from a file or stream to a file.
*/ */
[scriptable, builtinclass, uuid(82cf605a-8393-4550-83ab-43cd5578e006)] [scriptable, builtinclass, uuid(82cf605a-8393-4550-83ab-43cd5578e006)]
interface nsIFileStream : nsISupports interface nsIFileStream : nsIRandomAccessStream
{ {
/** /**
* @param file file to read from or stream to * @param file file to read from or stream to

View file

@ -1300,7 +1300,7 @@ nsresult NS_NewSafeLocalFileOutputStream(nsIOutputStream** result,
return rv; return rv;
} }
nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file, nsresult NS_NewLocalFileStream(nsIRandomAccessStream** result, nsIFile* file,
int32_t ioFlags /* = -1 */, int32_t ioFlags /* = -1 */,
int32_t perm /* = -1 */, int32_t perm /* = -1 */,
int32_t behaviorFlags /* = 0 */) { int32_t behaviorFlags /* = 0 */) {
@ -1312,10 +1312,11 @@ nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file,
return rv; return rv;
} }
mozilla::Result<nsCOMPtr<nsIFileStream>, nsresult> NS_NewLocalFileStream( mozilla::Result<nsCOMPtr<nsIRandomAccessStream>, nsresult>
nsIFile* file, int32_t ioFlags /* = -1 */, int32_t perm /* = -1 */, NS_NewLocalFileStream(nsIFile* file, int32_t ioFlags /* = -1 */,
int32_t behaviorFlags /* = 0 */) { int32_t perm /* = -1 */,
nsCOMPtr<nsIFileStream> stream; int32_t behaviorFlags /* = 0 */) {
nsCOMPtr<nsIRandomAccessStream> stream;
const nsresult rv = NS_NewLocalFileStream(getter_AddRefs(stream), file, const nsresult rv = NS_NewLocalFileStream(getter_AddRefs(stream), file,
ioFlags, perm, behaviorFlags); ioFlags, perm, behaviorFlags);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {

View file

@ -49,6 +49,7 @@ class nsIOutputStream;
class nsIParentChannel; class nsIParentChannel;
class nsIPersistentProperties; class nsIPersistentProperties;
class nsIProxyInfo; class nsIProxyInfo;
class nsIRandomAccessStream;
class nsIRequestObserver; class nsIRequestObserver;
class nsIStreamListener; class nsIStreamListener;
class nsIStreamLoader; class nsIStreamLoader;
@ -513,13 +514,13 @@ nsresult NS_NewSafeLocalFileOutputStream(nsIOutputStream** result,
int32_t perm = -1, int32_t perm = -1,
int32_t behaviorFlags = 0); int32_t behaviorFlags = 0);
nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file, nsresult NS_NewLocalFileStream(nsIRandomAccessStream** result, nsIFile* file,
int32_t ioFlags = -1, int32_t perm = -1, int32_t ioFlags = -1, int32_t perm = -1,
int32_t behaviorFlags = 0); int32_t behaviorFlags = 0);
mozilla::Result<nsCOMPtr<nsIFileStream>, nsresult> NS_NewLocalFileStream( mozilla::Result<nsCOMPtr<nsIRandomAccessStream>, nsresult>
nsIFile* file, int32_t ioFlags = -1, int32_t perm = -1, NS_NewLocalFileStream(nsIFile* file, int32_t ioFlags = -1, int32_t perm = -1,
int32_t behaviorFlags = 0); int32_t behaviorFlags = 0);
[[nodiscard]] nsresult NS_NewBufferedInputStream( [[nodiscard]] nsresult NS_NewBufferedInputStream(
nsIInputStream** aResult, already_AddRefed<nsIInputStream> aInputStream, nsIInputStream** aResult, already_AddRefed<nsIInputStream> aInputStream,

View file

@ -1952,7 +1952,7 @@ void WriteFailedProfileLock(nsIFile* aProfileDir) {
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) { if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
return; return;
} }
nsCOMPtr<nsIFileStream> fileStream; nsCOMPtr<nsIRandomAccessStream> fileStream;
rv = NS_NewLocalFileStream(getter_AddRefs(fileStream), file, rv = NS_NewLocalFileStream(getter_AddRefs(fileStream), file,
PR_RDWR | PR_CREATE_FILE, 0640); PR_RDWR | PR_CREATE_FILE, 0640);
NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_SUCCESS_VOID(rv);
@ -1968,11 +1968,9 @@ void WriteFailedProfileLock(nsIFile* aProfileDir) {
++failedLockCount; ++failedLockCount;
nsAutoCString bufStr; nsAutoCString bufStr;
bufStr.AppendInt(static_cast<int>(failedLockCount)); bufStr.AppendInt(static_cast<int>(failedLockCount));
nsCOMPtr<nsISeekableStream> seekStream = do_QueryInterface(fileStream);
NS_ENSURE_TRUE_VOID(seekStream);
// If we read in an existing failed lock count, we need to reset the file ptr // If we read in an existing failed lock count, we need to reset the file ptr
if (fileSize > 0) { if (fileSize > 0) {
rv = seekStream->Seek(nsISeekableStream::NS_SEEK_SET, 0); rv = fileStream->Seek(nsISeekableStream::NS_SEEK_SET, 0);
NS_ENSURE_SUCCESS_VOID(rv); NS_ENSURE_SUCCESS_VOID(rv);
} }
nsCOMPtr<nsIOutputStream> outStream = do_QueryInterface(fileStream); nsCOMPtr<nsIOutputStream> outStream = do_QueryInterface(fileStream);
@ -1987,7 +1985,7 @@ void WriteFailedProfileLock(nsIFile* aProfileDir) {
bytes += written; bytes += written;
bytesLeft -= written; bytesLeft -= written;
} while (bytesLeft > 0); } while (bytesLeft > 0);
seekStream->SetEOF(); fileStream->SetEOF();
} }
void InitIOReporting(nsIFile* aXreDir) { void InitIOReporting(nsIFile* aXreDir) {

View file

@ -27,6 +27,7 @@ XPIDL_SOURCES += [
"nsIObjectOutputStream.idl", "nsIObjectOutputStream.idl",
"nsIOutputStream.idl", "nsIOutputStream.idl",
"nsIPipe.idl", "nsIPipe.idl",
"nsIRandomAccessStream.idl",
"nsISafeOutputStream.idl", "nsISafeOutputStream.idl",
"nsIScriptableBase64Encoder.idl", "nsIScriptableBase64Encoder.idl",
"nsIScriptableInputStream.idl", "nsIScriptableInputStream.idl",

View file

@ -0,0 +1,48 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "nsISupports.idl"
#include "nsISeekableStream.idl"
interface nsIInputStream;
interface nsIOutputStream;
/**
* nsIRandomAccessStream
*
* An interface which supports both reading and writing to a storage starting
* at the current offset. Both the input stream and the output stream share the
* offset in the stream. Read operations invoked on the input stream start at
* the offset and advance it past the bytes read. Write operations invoked on
* the output stream start the offset and advance it past the bytes written.
* The offset can be set to an arbitrary value prior reading or writting. Each
* call to getInputStream or getOutputStream always returns the same object,
* rather than creating a new stream. It's recommended for objects implementing
* this interface to also implement nsIInputStream and nsIOutputStream, so they
* can be easilly used with e.g. NS_AsyncCopy.
*/
[scriptable, builtinclass, uuid(9b5904a8-886a-420f-a1d8-847de8ffc133)]
interface nsIRandomAccessStream : nsISeekableStream
{
/**
* This method always returns the same object.
*/
nsIInputStream getInputStream();
/**
* This method always returns the same object.
*/
nsIOutputStream getOutputStream();
/**
* Like getInputStream but infallible.
*/
[notxpcom, nostdcall] nsIInputStream inputStream();
/**
* Like getOutputStream but infallible.
*/
[notxpcom, nostdcall] nsIOutputStream outputStream();
};