gecko-dev/dom/file/TemporaryFileBlobImpl.cpp
Nika Layzell 1aa11ffed6 Bug 1754004 - Part 11: Simplify the IPCStream serialization API, r=asuth,necko-reviewers,kershaw
As serializing IPCStream no longer requires a manager or FileDescriptor array,
the arguments are no longer necessary, and can be removed. The AutoIPCStream
helper can also be removed, as managed actors are no longer used for
serialization, so a delayed start callback is not necessary.

The delayed start parameter is also removed from nsIIPCSerializableInputStream
instances, but is still present as `aAllowLazy` on the toplevel serialization
methods.

Differential Revision: https://phabricator.services.mozilla.com/D141048
2022-05-13 14:16:13 +00:00

127 lines
3.8 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "TemporaryFileBlobImpl.h"
#include "RemoteLazyInputStreamThread.h"
#include "mozilla/ErrorResult.h"
#include "nsFileStreams.h"
#include "nsIFile.h"
#include "nsIFileStreams.h"
#include "nsNetUtil.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
using namespace mozilla::ipc;
namespace mozilla::dom {
namespace {
// Here the flags needed in order to keep the temporary file opened.
// 1. REOPEN_ON_REWIND -> otherwise the stream is not serializable more than
// once.
// 2. no DEFER_OPEN -> the file must be kept open on windows in order to be
// deleted when used.
// 3. no CLOSE_ON_EOF -> the file will be closed by the DTOR. No needs. Also
// because the inputStream will not be read directly.
// 4. no SHARE_DELETE -> We don't want to allow this file to be deleted.
const uint32_t sTemporaryFileStreamFlags = nsIFileInputStream::REOPEN_ON_REWIND;
class TemporaryFileInputStream final : public nsFileInputStream {
public:
static nsresult Create(nsIFile* aFile, nsIInputStream** aInputStream) {
MOZ_ASSERT(aFile);
MOZ_ASSERT(aInputStream);
MOZ_ASSERT(XRE_IsParentProcess());
RefPtr<TemporaryFileInputStream> stream =
new TemporaryFileInputStream(aFile);
nsresult rv = stream->Init(aFile, -1, -1, sTemporaryFileStreamFlags);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
stream.forget(aInputStream);
return NS_OK;
}
void Serialize(InputStreamParams& aParams, uint32_t aMaxSize,
uint32_t* aSizeUsed) override {
MOZ_CRASH("This inputStream cannot be serialized.");
}
bool Deserialize(const InputStreamParams& aParams) override {
MOZ_CRASH("This inputStream cannot be deserialized.");
return false;
}
private:
explicit TemporaryFileInputStream(nsIFile* aFile) : mFile(aFile) {
MOZ_ASSERT(XRE_IsParentProcess());
}
~TemporaryFileInputStream() override {
// Let's delete the file on the RemoteLazyInputStream Thread.
RefPtr<RemoteLazyInputStreamThread> thread =
RemoteLazyInputStreamThread::GetOrCreate();
if (NS_WARN_IF(!thread)) {
return;
}
nsCOMPtr<nsIFile> file = std::move(mFile);
thread->Dispatch(
NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
[file]() { file->Remove(false); }));
}
nsCOMPtr<nsIFile> mFile;
};
} // namespace
TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
const nsAString& aContentType)
: FileBlobImpl(aFile, u""_ns, aContentType)
#ifdef DEBUG
,
mInputStreamCreated(false)
#endif
{
MOZ_ASSERT(XRE_IsParentProcess());
// This must be considered a blob.
mIsFile = false;
}
TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
MOZ_ASSERT(mInputStreamCreated);
}
already_AddRefed<BlobImpl> TemporaryFileBlobImpl::CreateSlice(
uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
ErrorResult& aRv) const {
MOZ_CRASH("This BlobImpl is not meant to be sliced!");
return nullptr;
}
void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream** aStream,
ErrorResult& aRv) const {
#ifdef DEBUG
MOZ_ASSERT(!mInputStreamCreated);
// CreateInputStream can be called only once.
mInputStreamCreated = true;
#endif
aRv = TemporaryFileInputStream::Create(mFile, aStream);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
}
} // namespace mozilla::dom