summaryrefslogtreecommitdiffstats
path: root/dom/file/FileBlobImpl.h
blob: 84565dbd4be1af873b0c13335c12d07fd4ecae70 (plain)
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
/* -*- 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/. */

#ifndef mozilla_dom_FileBlobImpl_h
#define mozilla_dom_FileBlobImpl_h

#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/BlobImpl.h"
#include "mozilla/Maybe.h"
#include "mozilla/Mutex.h"
#include "nsCOMPtr.h"

class nsIFile;

namespace mozilla::dom {

class FileBlobImpl : public BlobImpl {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(FileBlobImpl, BlobImpl)

  // Create as a file
  explicit FileBlobImpl(nsIFile* aFile);

  // Create as a file
  FileBlobImpl(const nsAString& aName, const nsAString& aContentType,
               uint64_t aLength, nsIFile* aFile);

  FileBlobImpl(const nsAString& aName, const nsAString& aContentType,
               uint64_t aLength, nsIFile* aFile, int64_t aLastModificationDate);

  // Create as a file with custom name
  FileBlobImpl(nsIFile* aFile, const nsAString& aName,
               const nsAString& aContentType);

  void GetName(nsAString& aName) const override {
    MOZ_ASSERT(mIsFile, "Should only be called on files");
    aName = mName;
  }

  void SetName(const nsAString& aName) { mName = aName; }

  void GetDOMPath(nsAString& aPath) const override {
    MOZ_ASSERT(mIsFile, "Should only be called on files");
    aPath = mPath;
  }

  void SetDOMPath(const nsAString& aPath) override {
    MOZ_ASSERT(mIsFile, "Should only be called on files");
    mPath = aPath;
  }

  int64_t GetLastModified(ErrorResult& aRv) override;

  void GetMozFullPath(nsAString& aFileName, SystemCallerGuarantee /* unused */,
                      ErrorResult& aRv) override {
    MOZ_ASSERT(mIsFile, "Should only be called on files");

    GetMozFullPathInternal(aFileName, aRv);
  }

  void GetMozFullPathInternal(nsAString& aFilename, ErrorResult& aRv) override;

  uint64_t GetSize(ErrorResult& aRv) override;

  void GetType(nsAString& aType) override;

  void GetBlobImplType(nsAString& aBlobImplType) const override;

  size_t GetAllocationSize() const override { return 0; }

  size_t GetAllocationSize(
      FallibleTArray<BlobImpl*>& aVisitedBlobImpls) const override {
    return GetAllocationSize();
  }

  uint64_t GetSerialNumber() const override { return mSerialNumber; }

  const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const override {
    return nullptr;
  }

  void CreateInputStream(nsIInputStream** aStream,
                         ErrorResult& aRv) const override;

  int64_t GetFileId() const override { return mFileId; }

  void SetLazyData(const nsAString& aName, const nsAString& aContentType,
                   uint64_t aLength, int64_t aLastModifiedDate) override {
    mName = aName;
    mContentType = aContentType;
    mIsFile = !aName.IsVoid();
    mLength.emplace(aLength);
    mLastModified.emplace(aLastModifiedDate);
  }

  bool IsMemoryFile() const override { return false; }

  bool IsFile() const override { return mIsFile; }

  bool IsDirectory() const override;

  void SetType(const nsAString& aType) { mContentType = aType; }

  void SetFileId(int64_t aFileId) { mFileId = aFileId; }

  void SetEmptySize() { mLength.emplace(0); }

  void SetMozFullPath(const nsAString& aPath) { mMozFullPath = aPath; }

  void SetLastModified(int64_t aLastModified) {
    mLastModified.emplace(aLastModified);
  }

 protected:
  ~FileBlobImpl() override = default;

  // Create slice
  FileBlobImpl(const FileBlobImpl* aOther, uint64_t aStart, uint64_t aLength,
               const nsAString& aContentType);

  already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart, uint64_t aLength,
                                         const nsAString& aContentType,
                                         ErrorResult& aRv) const override;

  class GetTypeRunnable;
  void GetTypeInternal(nsAString& aType, const MutexAutoLock& aProofOfLock);

  // FileBlobImpl has getter methods with lazy initialization. Because any
  // BlobImpl must work thread-safe, we use a mutex.
  Mutex mMutex MOZ_UNANNOTATED;

  nsCOMPtr<nsIFile> mFile;

  nsString mContentType;
  nsString mName;
  nsString mPath;  // The path relative to a directory chosen by the user
  nsString mMozFullPath;

  const uint64_t mSerialNumber;
  uint64_t mStart;

  int64_t mFileId;

  Maybe<uint64_t> mLength;

  Maybe<int64_t> mLastModified;

  bool mIsFile;
  bool mWholeFile;
};

}  // namespace mozilla::dom

#endif  // mozilla_dom_FileBlobImpl_h