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/. */
6 
7 #include "TemporaryFileBlobImpl.h"
8 
9 #include "IPCBlobInputStreamThread.h"
10 #include "nsFileStreams.h"
11 #include "nsIFile.h"
12 #include "nsIFileStreams.h"
13 #include "nsNetUtil.h"
14 #include "nsThreadUtils.h"
15 #include "nsXULAppAPI.h"
16 
17 using namespace mozilla::ipc;
18 
19 namespace mozilla {
20 namespace dom {
21 
22 namespace {
23 
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;
33 
34 class TemporaryFileInputStream final : public nsFileInputStream {
35  public:
Create(nsIFile * aFile,nsIInputStream ** aInputStream)36   static nsresult Create(nsIFile* aFile, nsIInputStream** aInputStream) {
37     MOZ_ASSERT(aFile);
38     MOZ_ASSERT(aInputStream);
39     MOZ_ASSERT(XRE_IsParentProcess());
40 
41     RefPtr<TemporaryFileInputStream> stream =
42         new TemporaryFileInputStream(aFile);
43 
44     nsresult rv = stream->Init(aFile, -1, -1, sTemporaryFileStreamFlags);
45     if (NS_WARN_IF(NS_FAILED(rv))) {
46       return rv;
47     }
48 
49     stream.forget(aInputStream);
50     return NS_OK;
51   }
52 
Serialize(InputStreamParams & aParams,FileDescriptorArray & aFileDescriptors)53   void Serialize(InputStreamParams& aParams,
54                  FileDescriptorArray& aFileDescriptors) override {
55     MOZ_CRASH("This inputStream cannot be serialized.");
56   }
57 
Deserialize(const InputStreamParams & aParams,const FileDescriptorArray & aFileDescriptors)58   bool Deserialize(const InputStreamParams& aParams,
59                    const FileDescriptorArray& aFileDescriptors) override {
60     MOZ_CRASH("This inputStream cannot be deserialized.");
61     return false;
62   }
63 
64  private:
TemporaryFileInputStream(nsIFile * aFile)65   explicit TemporaryFileInputStream(nsIFile* aFile) : mFile(aFile) {
66     MOZ_ASSERT(XRE_IsParentProcess());
67   }
68 
~TemporaryFileInputStream()69   ~TemporaryFileInputStream() {
70     // Let's delete the file on the IPCBlob Thread.
71     RefPtr<IPCBlobInputStreamThread> thread =
72         IPCBlobInputStreamThread::GetOrCreate();
73     if (NS_WARN_IF(!thread)) {
74       return;
75     }
76 
77     nsCOMPtr<nsIFile> file = Move(mFile);
78     thread->Dispatch(
79         NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
80                                [file]() { file->Remove(false); }));
81   }
82 
83   nsCOMPtr<nsIFile> mFile;
84 };
85 
86 }  // namespace
87 
TemporaryFileBlobImpl(nsIFile * aFile,const nsAString & aContentType)88 TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
89                                              const nsAString& aContentType)
90     : FileBlobImpl(aFile, EmptyString(), aContentType)
91 #ifdef DEBUG
92       ,
93       mInputStreamCreated(false)
94 #endif
95 {
96   MOZ_ASSERT(XRE_IsParentProcess());
97 }
98 
~TemporaryFileBlobImpl()99 TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
100   MOZ_ASSERT(mInputStreamCreated);
101 }
102 
CreateSlice(uint64_t aStart,uint64_t aLength,const nsAString & aContentType,ErrorResult & aRv)103 already_AddRefed<BlobImpl> TemporaryFileBlobImpl::CreateSlice(
104     uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
105     ErrorResult& aRv) {
106   MOZ_CRASH("This BlobImpl is not meant to be sliced!");
107   return nullptr;
108 }
109 
CreateInputStream(nsIInputStream ** aStream,ErrorResult & aRv)110 void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream** aStream,
111                                               ErrorResult& aRv) {
112 #ifdef DEBUG
113   MOZ_ASSERT(!mInputStreamCreated);
114   // CreateInputStream can be called only once.
115   mInputStreamCreated = true;
116 #endif
117 
118   aRv = TemporaryFileInputStream::Create(mFile, aStream);
119   if (NS_WARN_IF(aRv.Failed())) {
120     return;
121   }
122 }
123 
124 }  // namespace dom
125 }  // namespace mozilla
126