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,bool aDelayedStart,uint32_t aMaxSize,uint32_t * aSizeUsed,ParentToChildStreamActorManager * aManager)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.");
58   }
59 
Serialize(InputStreamParams & aParams,FileDescriptorArray & aFileDescriptors,bool aDelayedStart,uint32_t aMaxSize,uint32_t * aSizeUsed,ChildToParentStreamActorManager * aManager)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.");
65   }
66 
Deserialize(const InputStreamParams & aParams,const FileDescriptorArray & aFileDescriptors)67   bool Deserialize(const InputStreamParams& aParams,
68                    const FileDescriptorArray& aFileDescriptors) override {
69     MOZ_CRASH("This inputStream cannot be deserialized.");
70     return false;
71   }
72 
73  private:
TemporaryFileInputStream(nsIFile * aFile)74   explicit TemporaryFileInputStream(nsIFile* aFile) : mFile(aFile) {
75     MOZ_ASSERT(XRE_IsParentProcess());
76   }
77 
~TemporaryFileInputStream()78   ~TemporaryFileInputStream() {
79     // Let's delete the file on the IPCBlob Thread.
80     RefPtr<IPCBlobInputStreamThread> thread =
81         IPCBlobInputStreamThread::GetOrCreate();
82     if (NS_WARN_IF(!thread)) {
83       return;
84     }
85 
86     nsCOMPtr<nsIFile> file = std::move(mFile);
87     thread->Dispatch(
88         NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
89                                [file]() { file->Remove(false); }));
90   }
91 
92   nsCOMPtr<nsIFile> mFile;
93 };
94 
95 }  // namespace
96 
TemporaryFileBlobImpl(nsIFile * aFile,const nsAString & aContentType)97 TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
98                                              const nsAString& aContentType)
99     : FileBlobImpl(aFile, EmptyString(), aContentType)
100 #ifdef DEBUG
101       ,
102       mInputStreamCreated(false)
103 #endif
104 {
105   MOZ_ASSERT(XRE_IsParentProcess());
106 
107   // This must be considered a blob.
108   mIsFile = false;
109 }
110 
~TemporaryFileBlobImpl()111 TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
112   MOZ_ASSERT(mInputStreamCreated);
113 }
114 
CreateSlice(uint64_t aStart,uint64_t aLength,const nsAString & aContentType,ErrorResult & aRv)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;
120 }
121 
CreateInputStream(nsIInputStream ** aStream,ErrorResult & aRv)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
129 
130   aRv = TemporaryFileInputStream::Create(mFile, aStream);
131   if (NS_WARN_IF(aRv.Failed())) {
132     return;
133   }
134 }
135 
136 }  // namespace dom
137 }  // namespace mozilla
138