1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "TemporaryFileBlobImpl.h"
9 #include "RemoteLazyInputStreamThread.h"
10 #include "mozilla/ErrorResult.h"
11 #include "nsFileStreams.h"
13 #include "nsIFileStreams.h"
14 #include "nsNetUtil.h"
15 #include "nsThreadUtils.h"
16 #include "nsXULAppAPI.h"
18 using namespace mozilla::ipc
;
20 namespace mozilla::dom
{
24 // Here the flags needed in order to keep the temporary file opened.
25 // 1. REOPEN_ON_REWIND -> otherwise the stream is not serializable more than
27 // 2. no DEFER_OPEN -> the file must be kept open on windows in order to be
29 // 3. no CLOSE_ON_EOF -> the file will be closed by the DTOR. No needs. Also
30 // because the inputStream will not be read directly.
31 // 4. no SHARE_DELETE -> We don't want to allow this file to be deleted.
32 const uint32_t sTemporaryFileStreamFlags
= nsIFileInputStream::REOPEN_ON_REWIND
;
34 class TemporaryFileInputStream final
: public nsFileInputStream
{
36 static nsresult
Create(nsIFile
* aFile
, nsIInputStream
** aInputStream
) {
38 MOZ_ASSERT(aInputStream
);
39 MOZ_ASSERT(XRE_IsParentProcess());
41 RefPtr
<TemporaryFileInputStream
> stream
=
42 new TemporaryFileInputStream(aFile
);
44 nsresult rv
= stream
->Init(aFile
, -1, -1, sTemporaryFileStreamFlags
);
45 if (NS_WARN_IF(NS_FAILED(rv
))) {
49 stream
.forget(aInputStream
);
53 void Serialize(InputStreamParams
& aParams
, uint32_t aMaxSize
,
54 uint32_t* aSizeUsed
) override
{
55 MOZ_CRASH("This inputStream cannot be serialized.");
58 bool Deserialize(const InputStreamParams
& aParams
) override
{
59 MOZ_CRASH("This inputStream cannot be deserialized.");
64 explicit TemporaryFileInputStream(nsIFile
* aFile
) : mFile(aFile
) {
65 MOZ_ASSERT(XRE_IsParentProcess());
68 ~TemporaryFileInputStream() override
{
69 // Let's delete the file on the RemoteLazyInputStream Thread.
70 RefPtr
<RemoteLazyInputStreamThread
> thread
=
71 RemoteLazyInputStreamThread::GetOrCreate();
72 if (NS_WARN_IF(!thread
)) {
76 nsCOMPtr
<nsIFile
> file
= std::move(mFile
);
78 NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
79 [file
]() { file
->Remove(false); }));
82 nsCOMPtr
<nsIFile
> mFile
;
87 TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile
* aFile
,
88 const nsAString
& aContentType
)
89 : FileBlobImpl(aFile
, u
""_ns
, aContentType
)
92 mInputStreamCreated(false)
95 MOZ_ASSERT(XRE_IsParentProcess());
97 // This must be considered a blob.
101 TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
102 MOZ_ASSERT(mInputStreamCreated
);
105 already_AddRefed
<BlobImpl
> TemporaryFileBlobImpl::CreateSlice(
106 uint64_t aStart
, uint64_t aLength
, const nsAString
& aContentType
,
107 ErrorResult
& aRv
) const {
108 MOZ_CRASH("This BlobImpl is not meant to be sliced!");
112 void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream
** aStream
,
113 ErrorResult
& aRv
) const {
115 MOZ_ASSERT(!mInputStreamCreated
);
116 // CreateInputStream can be called only once.
117 mInputStreamCreated
= true;
120 aRv
= TemporaryFileInputStream::Create(mFile
, aStream
);
121 if (NS_WARN_IF(aRv
.Failed())) {
126 } // namespace mozilla::dom