Bug 1863873 - Block ability to perform audio decoding outside of Utility on release...
[gecko.git] / dom / file / TemporaryFileBlobImpl.cpp
blob505d4a96d4bc4e1079b8e9c746fe99cc730a1fb5
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, 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.");
60 return false;
63 private:
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)) {
73 return;
76 nsCOMPtr<nsIFile> file = std::move(mFile);
77 thread->Dispatch(
78 NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
79 [file]() { file->Remove(false); }));
82 nsCOMPtr<nsIFile> mFile;
85 } // namespace
87 TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
88 const nsAString& aContentType)
89 : FileBlobImpl(aFile, u""_ns, aContentType)
90 #ifdef DEBUG
92 mInputStreamCreated(false)
93 #endif
95 MOZ_ASSERT(XRE_IsParentProcess());
97 // This must be considered a blob.
98 mIsFile = false;
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!");
109 return nullptr;
112 void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream** aStream,
113 ErrorResult& aRv) const {
114 #ifdef DEBUG
115 MOZ_ASSERT(!mInputStreamCreated);
116 // CreateInputStream can be called only once.
117 mInputStreamCreated = true;
118 #endif
120 aRv = TemporaryFileInputStream::Create(mFile, aStream);
121 if (NS_WARN_IF(aRv.Failed())) {
122 return;
126 } // namespace mozilla::dom