Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/file/TemporaryFileBlobImpl.cpp
Line
Count
Source (jump to first uncovered line)
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 =
33
  nsIFileInputStream::REOPEN_ON_REWIND;
34
35
class TemporaryFileInputStream final : public nsFileInputStream
36
{
37
public:
38
  static nsresult
39
  Create(nsIFile* aFile, nsIInputStream** aInputStream)
40
0
  {
41
0
    MOZ_ASSERT(aFile);
42
0
    MOZ_ASSERT(aInputStream);
43
0
    MOZ_ASSERT(XRE_IsParentProcess());
44
0
45
0
    RefPtr<TemporaryFileInputStream> stream =
46
0
      new TemporaryFileInputStream(aFile);
47
0
48
0
    nsresult rv = stream->Init(aFile, -1, -1, sTemporaryFileStreamFlags);
49
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
50
0
      return rv;
51
0
    }
52
0
53
0
    stream.forget(aInputStream);
54
0
    return NS_OK;
55
0
  }
56
57
  void
58
  Serialize(InputStreamParams& aParams,
59
            FileDescriptorArray& aFileDescriptors) override
60
0
  {
61
0
    MOZ_CRASH("This inputStream cannot be serialized.");
62
0
  }
63
64
  bool
65
  Deserialize(const InputStreamParams& aParams,
66
              const FileDescriptorArray& aFileDescriptors) override
67
0
  {
68
0
    MOZ_CRASH("This inputStream cannot be deserialized.");
69
0
    return false;
70
0
  }
71
72
private:
73
  explicit TemporaryFileInputStream(nsIFile* aFile)
74
    : mFile(aFile)
75
0
  {
76
0
    MOZ_ASSERT(XRE_IsParentProcess());
77
0
  }
78
79
  ~TemporaryFileInputStream()
80
0
  {
81
0
    // Let's delete the file on the IPCBlob Thread.
82
0
    RefPtr<IPCBlobInputStreamThread> thread =
83
0
      IPCBlobInputStreamThread::GetOrCreate();
84
0
    if (NS_WARN_IF(!thread)) {
85
0
      return;
86
0
    }
87
0
88
0
    nsCOMPtr<nsIFile> file = std::move(mFile);
89
0
    thread->Dispatch(NS_NewRunnableFunction(
90
0
      "TemporaryFileInputStream::Runnable",
91
0
      [file]() {
92
0
        file->Remove(false);
93
0
      }
94
0
    ));
95
0
  }
96
97
  nsCOMPtr<nsIFile> mFile;
98
};
99
100
} // anonymous
101
102
TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
103
                                             const nsAString& aContentType)
104
  : FileBlobImpl(aFile, EmptyString(), aContentType)
105
#ifdef DEBUG
106
  , mInputStreamCreated(false)
107
#endif
108
0
{
109
0
  MOZ_ASSERT(XRE_IsParentProcess());
110
0
}
111
112
TemporaryFileBlobImpl::~TemporaryFileBlobImpl()
113
0
{
114
0
  MOZ_ASSERT(mInputStreamCreated);
115
0
}
116
117
already_AddRefed<BlobImpl>
118
TemporaryFileBlobImpl::CreateSlice(uint64_t aStart, uint64_t aLength,
119
                                   const nsAString& aContentType,
120
                                   ErrorResult& aRv)
121
0
{
122
0
  MOZ_CRASH("This BlobImpl is not meant to be sliced!");
123
0
  return nullptr;
124
0
}
125
126
void
127
TemporaryFileBlobImpl::CreateInputStream(nsIInputStream** aStream, ErrorResult& aRv)
128
0
{
129
#ifdef DEBUG
130
  MOZ_ASSERT(!mInputStreamCreated);
131
  // CreateInputStream can be called only once.
132
  mInputStreamCreated = true;
133
#endif
134
135
0
  aRv = TemporaryFileInputStream::Create(mFile, aStream);
136
0
  if (NS_WARN_IF(aRv.Failed())) {
137
0
    return;
138
0
  }
139
0
}
140
141
} // namespace dom
142
} // namespace mozilla