summaryrefslogtreecommitdiffstats
path: root/dom/file/File.cpp
blob: 9995d35c52739c3971fc7659669316bd56f659fb (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
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
/* -*- 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 "File.h"
#include "FileBlobImpl.h"
#include "MemoryBlobImpl.h"
#include "MultipartBlobImpl.h"
#include "mozilla/dom/BlobBinding.h"
#include "mozilla/dom/FileBinding.h"
#include "mozilla/dom/FileCreatorHelper.h"
#include "mozilla/dom/FileSystemUtils.h"
#include "mozilla/dom/Promise.h"
#include "nsIFile.h"
#include "nsContentUtils.h"
#include "nsXULAppAPI.h"

namespace mozilla::dom {

File::File(nsIGlobalObject* aGlobal, BlobImpl* aImpl) : Blob(aGlobal, aImpl) {
  MOZ_ASSERT(aImpl->IsFile());
}

File::~File() = default;

/* static */
File* File::Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl) {
  MOZ_ASSERT(aImpl);
  MOZ_ASSERT(aImpl->IsFile());

  MOZ_ASSERT(aGlobal);
  if (NS_WARN_IF(!aGlobal)) {
    return nullptr;
  }

  return new File(aGlobal, aImpl);
}

/* static */
already_AddRefed<File> File::CreateMemoryFileWithCustomLastModified(
    nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength,
    const nsAString& aName, const nsAString& aContentType,
    int64_t aLastModifiedDate) {
  RefPtr<MemoryBlobImpl> blobImpl =
      MemoryBlobImpl::CreateWithCustomLastModified(
          aMemoryBuffer, aLength, aName, aContentType, aLastModifiedDate);
  MOZ_ASSERT(blobImpl);

  RefPtr<File> file = File::Create(aGlobal, blobImpl);
  return file.forget();
}

/* static */
already_AddRefed<File> File::CreateMemoryFileWithLastModifiedNow(
    nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength,
    const nsAString& aName, const nsAString& aContentType) {
  MOZ_ASSERT(aGlobal);

  RefPtr<MemoryBlobImpl> blobImpl = MemoryBlobImpl::CreateWithLastModifiedNow(
      aMemoryBuffer, aLength, aName, aContentType, aGlobal->GetRTPCallerType());
  MOZ_ASSERT(blobImpl);

  RefPtr<File> file = File::Create(aGlobal, blobImpl);
  return file.forget();
}

/* static */
already_AddRefed<File> File::CreateFromFile(nsIGlobalObject* aGlobal,
                                            nsIFile* aFile) {
  MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());

  MOZ_ASSERT(aGlobal);
  if (NS_WARN_IF(!aGlobal)) {
    return nullptr;
  }

  RefPtr<File> file = new File(aGlobal, new FileBlobImpl(aFile));
  return file.forget();
}

/* static */
already_AddRefed<File> File::CreateFromFile(nsIGlobalObject* aGlobal,
                                            nsIFile* aFile,
                                            const nsAString& aName,
                                            const nsAString& aContentType) {
  MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());

  MOZ_ASSERT(aGlobal);
  if (NS_WARN_IF(!aGlobal)) {
    return nullptr;
  }

  RefPtr<File> file =
      new File(aGlobal, new FileBlobImpl(aFile, aName, aContentType));
  return file.forget();
}

JSObject* File::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return File_Binding::Wrap(aCx, this, aGivenProto);
}

void File::GetName(nsAString& aFileName) const { mImpl->GetName(aFileName); }

void File::GetRelativePath(nsAString& aPath) const {
  aPath.Truncate();

  nsAutoString path;
  mImpl->GetDOMPath(path);

  // WebkitRelativePath doesn't start with '/'
  if (!path.IsEmpty()) {
    MOZ_ASSERT(path[0] == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
    aPath.Assign(Substring(path, 1));
  }
}

int64_t File::GetLastModified(ErrorResult& aRv) {
  return mImpl->GetLastModified(aRv);
}

void File::GetMozFullPath(nsAString& aFilename,
                          SystemCallerGuarantee aGuarantee, ErrorResult& aRv) {
  mImpl->GetMozFullPath(aFilename, aGuarantee, aRv);
}

void File::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) {
  mImpl->GetMozFullPathInternal(aFileName, aRv);
}

/* static */
already_AddRefed<File> File::Constructor(const GlobalObject& aGlobal,
                                         const Sequence<BlobPart>& aData,
                                         const nsAString& aName,
                                         const FilePropertyBag& aBag,
                                         ErrorResult& aRv) {
  RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(aName);

  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
  MOZ_ASSERT(global);

  nsAutoString type(aBag.mType);
  MakeValidBlobType(type);
  impl->InitializeBlob(aData, type, aBag.mEndings == EndingType::Native,
                       global->GetRTPCallerType(), aRv);
  if (aRv.Failed()) {
    return nullptr;
  }
  MOZ_ASSERT(impl->IsFile());

  if (aBag.mLastModified.WasPassed()) {
    impl->SetLastModified(aBag.mLastModified.Value());
  }

  RefPtr<File> file = new File(global, impl);
  return file.forget();
}

/* static */
already_AddRefed<Promise> File::CreateFromNsIFile(
    const GlobalObject& aGlobal, nsIFile* aData,
    const ChromeFilePropertyBag& aBag, SystemCallerGuarantee aGuarantee,
    ErrorResult& aRv) {
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  MOZ_ASSERT(global);
  if (NS_WARN_IF(!global)) {
    aRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  RefPtr<Promise> promise =
      FileCreatorHelper::CreateFile(global, aData, aBag, true, aRv);
  return promise.forget();
}

/* static */
already_AddRefed<Promise> File::CreateFromFileName(
    const GlobalObject& aGlobal, const nsAString& aPath,
    const ChromeFilePropertyBag& aBag, SystemCallerGuarantee aGuarantee,
    ErrorResult& aRv) {
  nsCOMPtr<nsIFile> file;
  aRv = NS_NewLocalFile(aPath, false, getter_AddRefs(file));
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  MOZ_ASSERT(global);
  if (NS_WARN_IF(!global)) {
    aRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  RefPtr<Promise> promise =
      FileCreatorHelper::CreateFile(global, file, aBag, false, aRv);
  return promise.forget();
}

}  // namespace mozilla::dom