File: FileBlobImpl.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (304 lines) | stat: -rw-r--r-- 8,900 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FileBlobImpl.h"

#include "BaseBlobImpl.h"
#include "mozilla/SlicedInputStream.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "nsCExternalHandlerService.h"
#include "nsIFile.h"
#include "nsIFileStreams.h"
#include "nsIMIMEService.h"
#include "nsNetUtil.h"
#include "nsStreamUtils.h"

namespace mozilla::dom {

FileBlobImpl::FileBlobImpl(nsIFile* aFile)
    : mMutex("FileBlobImpl::mMutex"),
      mFile(aFile),
      mSerialNumber(BaseBlobImpl::NextSerialNumber()),
      mStart(0),
      mFileId(-1),
      mIsFile(true),
      mWholeFile(true) {
  MOZ_ASSERT(mFile, "must have file");
  MOZ_ASSERT(XRE_IsParentProcess());
  // Lazily get the content type and size
  mContentType.SetIsVoid(true);
  mMozFullPath.SetIsVoid(true);
  mFile->GetLeafName(mName);
}

FileBlobImpl::FileBlobImpl(const nsAString& aName,
                           const nsAString& aContentType, uint64_t aLength,
                           nsIFile* aFile)
    : mMutex("FileBlobImpl::mMutex"),
      mFile(aFile),
      mContentType(aContentType),
      mName(aName),
      mSerialNumber(BaseBlobImpl::NextSerialNumber()),
      mStart(0),
      mFileId(-1),
      mLength(Some(aLength)),
      mIsFile(true),
      mWholeFile(true) {
  MOZ_ASSERT(mFile, "must have file");
  MOZ_ASSERT(XRE_IsParentProcess());
  mMozFullPath.SetIsVoid(true);
}

FileBlobImpl::FileBlobImpl(const nsAString& aName,
                           const nsAString& aContentType, uint64_t aLength,
                           nsIFile* aFile, int64_t aLastModificationDate)
    : mMutex("FileBlobImpl::mMutex"),
      mFile(aFile),
      mContentType(aContentType),
      mName(aName),
      mSerialNumber(BaseBlobImpl::NextSerialNumber()),
      mStart(0),
      mFileId(-1),
      mLength(Some(aLength)),
      mLastModified(Some(aLastModificationDate)),
      mIsFile(true),
      mWholeFile(true) {
  MOZ_ASSERT(mFile, "must have file");
  MOZ_ASSERT(XRE_IsParentProcess());
  mMozFullPath.SetIsVoid(true);
}

FileBlobImpl::FileBlobImpl(nsIFile* aFile, const nsAString& aName,
                           const nsAString& aContentType)
    : mMutex("FileBlobImpl::mMutex"),
      mFile(aFile),
      mContentType(aContentType),
      mName(aName),
      mSerialNumber(BaseBlobImpl::NextSerialNumber()),
      mStart(0),
      mFileId(-1),
      mIsFile(true),
      mWholeFile(true) {
  MOZ_ASSERT(mFile, "must have file");
  MOZ_ASSERT(XRE_IsParentProcess());
  if (aContentType.IsEmpty()) {
    // Lazily get the content type and size
    mContentType.SetIsVoid(true);
  }

  mMozFullPath.SetIsVoid(true);
}

FileBlobImpl::FileBlobImpl(const FileBlobImpl* aOther, uint64_t aStart,
                           uint64_t aLength, const nsAString& aContentType)
    : mMutex("FileBlobImpl::mMutex"),
      mFile(aOther->mFile),
      mContentType(aContentType),
      mSerialNumber(BaseBlobImpl::NextSerialNumber()),
      mStart(aOther->mStart + aStart),
      mFileId(-1),
      mLength(Some(aLength)),
      mIsFile(false),
      mWholeFile(false) {
  MOZ_ASSERT(mFile, "must have file");
  MOZ_ASSERT(XRE_IsParentProcess());
  mMozFullPath = aOther->mMozFullPath;
}

already_AddRefed<BlobImpl> FileBlobImpl::CreateSlice(
    uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
    ErrorResult& aRv) const {
  RefPtr<FileBlobImpl> impl =
      new FileBlobImpl(this, aStart, aLength, aContentType);
  return impl.forget();
}

void FileBlobImpl::GetMozFullPathInternal(nsAString& aFilename,
                                          ErrorResult& aRv) {
  MOZ_ASSERT(mIsFile, "Should only be called on files");

  MutexAutoLock lock(mMutex);

  if (!mMozFullPath.IsVoid()) {
    aFilename = mMozFullPath;
    return;
  }

  aRv = mFile->GetPath(aFilename);
  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  mMozFullPath = aFilename;
}

uint64_t FileBlobImpl::GetSize(ErrorResult& aRv) {
  MutexAutoLock lock(mMutex);

  if (mLength.isNothing()) {
    MOZ_ASSERT(mWholeFile,
               "Should only use lazy size when using the whole file");
    int64_t fileSize;
    aRv = mFile->GetFileSize(&fileSize);
    if (NS_WARN_IF(aRv.Failed())) {
      return 0;
    }

    if (fileSize < 0) {
      aRv.Throw(NS_ERROR_FAILURE);
      return 0;
    }

    mLength.emplace(fileSize);
  }

  return mLength.value();
}

class FileBlobImpl::GetTypeRunnable final : public WorkerMainThreadRunnable {
 public:
  GetTypeRunnable(WorkerPrivate* aWorkerPrivate, FileBlobImpl* aBlobImpl)
      : WorkerMainThreadRunnable(aWorkerPrivate, "FileBlobImpl :: GetType"_ns),
        mBlobImpl(aBlobImpl) {
    MOZ_ASSERT(aBlobImpl);
    aWorkerPrivate->AssertIsOnWorkerThread();
  }

  bool MainThreadRun() override {
    MOZ_ASSERT(NS_IsMainThread());

    nsAutoString type;
    mBlobImpl->GetType(type);
    return true;
  }

 private:
  ~GetTypeRunnable() override = default;

  RefPtr<FileBlobImpl> mBlobImpl;
};

void FileBlobImpl::GetType(nsAString& aType) {
  MutexAutoLock lock(mMutex);
  aType.Truncate();

  if (mContentType.IsVoid()) {
    MOZ_ASSERT(mWholeFile,
               "Should only use lazy ContentType when using the whole file");

    if (!NS_IsMainThread()) {
      WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
      if (!workerPrivate) {
        // I have no idea in which thread this method is called. We cannot
        // return any valid value.
        return;
      }

      // NOTE: We need to unlock the mutex while we're dispatching to the main
      // thread, as otherwise we could deadlock in a few ways:
      //
      // 1. We spin a nested event loop while `Dispatch` is being called to wait
      //    for the runnable to complete. Some event dispatched to that nested
      //    loop could theoretically access `FileBlobImpl` which would lead to a
      //    deadlock on this thread.
      // 2. The main thread could attempt to access a method on the
      //    `FileBlobImpl` while the runnable is being dispatched to the main
      //    thread, which will lead to the main thread being deadlocked (as the
      //    background thread is still holding the mutex).
      //
      // Instead, we unlock here, and we'll re-acquire the mutex on the main
      // thread to update `mContentType`, and acquire it again on this thread to
      // return the relevant value.
      MutexAutoUnlock unlock(mMutex);

      RefPtr<GetTypeRunnable> runnable =
          new GetTypeRunnable(workerPrivate, this);

      ErrorResult rv;
      runnable->Dispatch(workerPrivate, Canceling, rv);
      if (NS_WARN_IF(rv.Failed())) {
        rv.SuppressException();
        return;
      }
    } else {
      nsresult rv;
      nsCOMPtr<nsIMIMEService> mimeService =
          do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return;
      }

      nsAutoCString mimeType;
      rv = mimeService->GetTypeFromFile(mFile, mimeType);
      if (NS_FAILED(rv)) {
        mimeType.Truncate();
      }

      AppendUTF8toUTF16(mimeType, mContentType);
      mContentType.SetIsVoid(false);
    }
  }

  aType = mContentType;
}

void FileBlobImpl::GetBlobImplType(nsAString& aBlobImplType) const {
  aBlobImplType = u"FileBlobImpl"_ns;
}

int64_t FileBlobImpl::GetLastModified(ErrorResult& aRv) {
  MOZ_ASSERT(mIsFile, "Should only be called on files");

  MutexAutoLock lock(mMutex);

  if (mLastModified.isNothing()) {
    PRTime msecs;
    aRv = mFile->GetLastModifiedTime(&msecs);
    if (NS_WARN_IF(aRv.Failed())) {
      return 0;
    }

    mLastModified.emplace(int64_t(msecs));
  }

  return mLastModified.value();
}

const uint32_t sFileStreamFlags =
    nsIFileInputStream::CLOSE_ON_EOF | nsIFileInputStream::REOPEN_ON_REWIND |
    nsIFileInputStream::DEFER_OPEN | nsIFileInputStream::SHARE_DELETE;

void FileBlobImpl::CreateInputStream(nsIInputStream** aStream,
                                     ErrorResult& aRv) const {
  nsCOMPtr<nsIInputStream> stream;
  aRv = NS_NewLocalFileInputStream(getter_AddRefs(stream), mFile, -1, -1,
                                   sFileStreamFlags);
  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  if (mWholeFile) {
    stream.forget(aStream);
    return;
  }

  MOZ_ASSERT(mLength.isSome());

  RefPtr<SlicedInputStream> slicedInputStream =
      new SlicedInputStream(stream.forget(), mStart, mLength.value());
  slicedInputStream.forget(aStream);
}

bool FileBlobImpl::IsDirectory() const {
  bool isDirectory = false;
  if (mFile) {
    mFile->IsDirectory(&isDirectory);
  }
  return isDirectory;
}

}  // namespace mozilla::dom