Bug 1685822 [wpt PR 27117] - [Import Maps] Add tests for rejecting multiple import...
[gecko.git] / dom / file / TemporaryFileBlobImpl.cpp
blob887d2c47bdebacd05bfcf9a66a749dff7f8c052c
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"
12 #include "nsIFile.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 {
22 namespace {
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
26 // once.
27 // 2. no DEFER_OPEN -> the file must be kept open on windows in order to be
28 // deleted when used.
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 {
35 public:
36 static nsresult Create(nsIFile* aFile, nsIInputStream** aInputStream) {
37 MOZ_ASSERT(aFile);
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))) {
46 return rv;
49 stream.forget(aInputStream);
50 return NS_OK;
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.");
70 return false;
73 private:
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)) {
83 return;
86 nsCOMPtr<nsIFile> file = std::move(mFile);
87 thread->Dispatch(
88 NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
89 [file]() { file->Remove(false); }));
92 nsCOMPtr<nsIFile> mFile;
95 } // namespace
97 TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
98 const nsAString& aContentType)
99 : FileBlobImpl(aFile, u""_ns, aContentType)
100 #ifdef DEBUG
102 mInputStreamCreated(false)
103 #endif
105 MOZ_ASSERT(XRE_IsParentProcess());
107 // This must be considered a blob.
108 mIsFile = false;
111 TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
112 MOZ_ASSERT(mInputStreamCreated);
115 already_AddRefed<BlobImpl> TemporaryFileBlobImpl::CreateSlice(
116 uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
117 ErrorResult& aRv) {
118 MOZ_CRASH("This BlobImpl is not meant to be sliced!");
119 return nullptr;
122 void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream** aStream,
123 ErrorResult& aRv) {
124 #ifdef DEBUG
125 MOZ_ASSERT(!mInputStreamCreated);
126 // CreateInputStream can be called only once.
127 mInputStreamCreated = true;
128 #endif
130 aRv = TemporaryFileInputStream::Create(mFile, aStream);
131 if (NS_WARN_IF(aRv.Failed())) {
132 return;
136 } // namespace mozilla::dom