DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
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
/* -*- 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 "TemporaryFileBlobImpl.h"

#include "IPCBlobInputStreamThread.h"
#include "nsFileStreams.h"
#include "nsIFile.h"
#include "nsIFileStreams.h"
#include "nsNetUtil.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"

using namespace mozilla::ipc;

namespace mozilla {
namespace dom {

namespace {

// Here the flags needed in order to keep the temporary file opened.
// 1. REOPEN_ON_REWIND -> otherwise the stream is not serializable more than
//                        once.
// 2. no DEFER_OPEN -> the file must be kept open on windows in order to be
//                     deleted when used.
// 3. no CLOSE_ON_EOF -> the file will be closed by the DTOR. No needs. Also
//                       because the inputStream will not be read directly.
// 4. no SHARE_DELETE -> We don't want to allow this file to be deleted.
const uint32_t sTemporaryFileStreamFlags = nsIFileInputStream::REOPEN_ON_REWIND;

class TemporaryFileInputStream final : public nsFileInputStream {
 public:
  static nsresult Create(nsIFile* aFile, nsIInputStream** aInputStream) {
    MOZ_ASSERT(aFile);
    MOZ_ASSERT(aInputStream);
    MOZ_ASSERT(XRE_IsParentProcess());

    RefPtr<TemporaryFileInputStream> stream =
        new TemporaryFileInputStream(aFile);

    nsresult rv = stream->Init(aFile, -1, -1, sTemporaryFileStreamFlags);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    stream.forget(aInputStream);
    return NS_OK;
  }

  void Serialize(InputStreamParams& aParams,
                 FileDescriptorArray& aFileDescriptors) override {
    MOZ_CRASH("This inputStream cannot be serialized.");
  }

  bool Deserialize(const InputStreamParams& aParams,
                   const FileDescriptorArray& aFileDescriptors) override {
    MOZ_CRASH("This inputStream cannot be deserialized.");
    return false;
  }

 private:
  explicit TemporaryFileInputStream(nsIFile* aFile) : mFile(aFile) {
    MOZ_ASSERT(XRE_IsParentProcess());
  }

  ~TemporaryFileInputStream() {
    // Let's delete the file on the IPCBlob Thread.
    RefPtr<IPCBlobInputStreamThread> thread =
        IPCBlobInputStreamThread::GetOrCreate();
    if (NS_WARN_IF(!thread)) {
      return;
    }

    nsCOMPtr<nsIFile> file = Move(mFile);
    thread->Dispatch(
        NS_NewRunnableFunction("TemporaryFileInputStream::Runnable",
                               [file]() { file->Remove(false); }));
  }

  nsCOMPtr<nsIFile> mFile;
};

}  // namespace

TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
                                             const nsAString& aContentType)
    : FileBlobImpl(aFile, EmptyString(), aContentType)
#ifdef DEBUG
      ,
      mInputStreamCreated(false)
#endif
{
  MOZ_ASSERT(XRE_IsParentProcess());
}

TemporaryFileBlobImpl::~TemporaryFileBlobImpl() {
  MOZ_ASSERT(mInputStreamCreated);
}

already_AddRefed<BlobImpl> TemporaryFileBlobImpl::CreateSlice(
    uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
    ErrorResult& aRv) {
  MOZ_CRASH("This BlobImpl is not meant to be sliced!");
  return nullptr;
}

void TemporaryFileBlobImpl::CreateInputStream(nsIInputStream** aStream,
                                              ErrorResult& aRv) {
#ifdef DEBUG
  MOZ_ASSERT(!mInputStreamCreated);
  // CreateInputStream can be called only once.
  mInputStreamCreated = true;
#endif

  aRv = TemporaryFileInputStream::Create(mFile, aStream);
  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }
}

}  // namespace dom
}  // namespace mozilla