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 "nsFileStreams.h"
12 #include "nsIFileStreams.h"
13 #include "nsNetUtil.h"
14 #include "nsThreadUtils.h"
15 #include "nsXULAppAPI.h"
17 using namespace mozilla::ipc
;
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
,
54 FileDescriptorArray
& aFileDescriptors
, bool aDelayedStart
,
55 uint32_t aMaxSize
, uint32_t* aSizeUsed
,
56 ParentToChildStreamActorManager
* aManager
) override
{
57 MOZ_CRASH("This inputStream cannot be serialized.");
60 void Serialize(InputStreamParams
& aParams
,
61 FileDescriptorArray
& aFileDescriptors
, bool aDelayedStart
,
62 uint32_t aMaxSize
, uint32_t* aSizeUsed
,
63 ChildToParentStreamActorManager
* aManager
) override
{
64 MOZ_CRASH("This inputStream cannot be serialized.");
67 bool Deserialize(const InputStreamParams
& aParams
,
68 const FileDescriptorArray
& aFileDescriptors
) override
{
69 MOZ_CRASH("This inputStream cannot be deserialized.");
74 explicit TemporaryFileInputStream(nsIFile
* aFile
) : mFile(aFile
) {
75 MOZ_ASSERT(XRE_IsParentProcess());
78 ~TemporaryFileInputStream() {
79 // Let's delete the file on the RemoteLazyInputStream Thread.
80 RefPtr
<RemoteLazyInputStreamThread
> thread
=
81 RemoteLazyInputStreamThread::GetOrCreate();
82 if (NS_WARN_IF(!thread
)) {
86 nsCOMPtr
<nsIFile
> file
= std::move(mFile
);
88 NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
89 [file
]() { file
->Remove(false); }));
92 nsCOMPtr
<nsIFile
> mFile
;
97 TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile
* aFile
,
98 const nsAString
& aContentType
)
99 : FileBlobImpl(aFile
, EmptyString(), aContentType
)
102 mInputStreamCreated(false)
105 MOZ_ASSERT(XRE_IsParentProcess());
107 // This must be considered a blob.
111 TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
112 MOZ_ASSERT(mInputStreamCreated
);
115 already_AddRefed
<BlobImpl
> TemporaryFileBlobImpl::CreateSlice(
116 uint64_t aStart
, uint64_t aLength
, const nsAString
& aContentType
,
118 MOZ_CRASH("This BlobImpl is not meant to be sliced!");
122 void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream
** aStream
,
125 MOZ_ASSERT(!mInputStreamCreated
);
126 // CreateInputStream can be called only once.
127 mInputStreamCreated
= true;
130 aRv
= TemporaryFileInputStream::Create(mFile
, aStream
);
131 if (NS_WARN_IF(aRv
.Failed())) {
137 } // namespace mozilla