2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-10-07 17:16:11 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2017-02-16 17:26:38 +00:00
|
|
|
#include "File.h"
|
|
|
|
#include "FileBlobImpl.h"
|
|
|
|
#include "MemoryBlobImpl.h"
|
2015-05-12 12:11:03 +00:00
|
|
|
#include "MultipartBlobImpl.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "mozilla/dom/FileBinding.h"
|
2017-02-08 09:19:01 +00:00
|
|
|
#include "mozilla/dom/FileCreatorHelper.h"
|
2017-02-06 10:07:54 +00:00
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
2017-02-08 09:18:32 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "nsIFile.h"
|
2017-02-23 02:23:51 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
namespace mozilla::dom {
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2019-10-21 05:33:33 +00:00
|
|
|
File::File(nsIGlobalObject* aGlobal, BlobImpl* aImpl) : Blob(aGlobal, aImpl) {
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:11:24 +00:00
|
|
|
File::~File() = default;
|
2017-02-16 17:26:38 +00:00
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
2019-10-21 05:33:33 +00:00
|
|
|
File* File::Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl) {
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
|
2019-10-21 05:33:19 +00:00
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
if (NS_WARN_IF(!aGlobal)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:33:33 +00:00
|
|
|
return new File(aGlobal, aImpl);
|
2015-05-12 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
2020-03-04 23:10:22 +00:00
|
|
|
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);
|
2015-05-11 15:43:59 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
2020-03-04 23:10:22 +00:00
|
|
|
already_AddRefed<File> File::CreateMemoryFileWithLastModifiedNow(
|
|
|
|
nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength,
|
|
|
|
const nsAString& aName, const nsAString& aContentType) {
|
2020-03-25 14:14:37 +00:00
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
|
2020-03-04 23:10:22 +00:00
|
|
|
RefPtr<MemoryBlobImpl> blobImpl = MemoryBlobImpl::CreateWithLastModifiedNow(
|
2020-03-25 14:14:37 +00:00
|
|
|
aMemoryBuffer, aLength, aName, aContentType,
|
|
|
|
aGlobal->CrossOriginIsolated());
|
2020-03-04 23:10:22 +00:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
RefPtr<File> file = File::Create(aGlobal, blobImpl);
|
2015-05-11 21:54:02 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
2019-10-21 05:33:33 +00:00
|
|
|
already_AddRefed<File> File::CreateFromFile(nsIGlobalObject* aGlobal,
|
2019-02-25 22:05:29 +00:00
|
|
|
nsIFile* aFile) {
|
2017-02-23 02:23:51 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
2019-10-21 05:33:19 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
if (NS_WARN_IF(!aGlobal)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:33:33 +00:00
|
|
|
RefPtr<File> file = new File(aGlobal, new FileBlobImpl(aFile));
|
2014-10-07 17:16:11 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
2019-10-21 05:33:33 +00:00
|
|
|
already_AddRefed<File> File::CreateFromFile(nsIGlobalObject* aGlobal,
|
2019-02-25 22:05:29 +00:00
|
|
|
nsIFile* aFile,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType) {
|
2017-02-23 02:23:51 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
2019-10-21 05:33:19 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
if (NS_WARN_IF(!aGlobal)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file =
|
2019-10-21 05:33:33 +00:00
|
|
|
new File(aGlobal, new FileBlobImpl(aFile, aName, aContentType));
|
2014-10-07 17:16:11 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
JSObject* File::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
2018-06-25 21:20:54 +00:00
|
|
|
return File_Binding::Wrap(aCx, this, aGivenProto);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 10:50:16 +00:00
|
|
|
void File::GetName(nsAString& aFileName) const { mImpl->GetName(aFileName); }
|
2015-05-11 15:43:59 +00:00
|
|
|
|
2017-02-06 10:07:54 +00:00
|
|
|
void File::GetRelativePath(nsAString& aPath) const {
|
|
|
|
aPath.Truncate();
|
2016-05-13 11:11:38 +00:00
|
|
|
|
2017-02-06 10:07:54 +00:00
|
|
|
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));
|
|
|
|
}
|
2015-05-11 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
int64_t File::GetLastModified(ErrorResult& aRv) {
|
|
|
|
return mImpl->GetLastModified(aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 20:43:37 +00:00
|
|
|
void File::GetMozFullPath(nsAString& aFilename,
|
2019-04-26 17:53:07 +00:00
|
|
|
SystemCallerGuarantee aGuarantee, ErrorResult& aRv) {
|
2017-02-01 20:43:37 +00:00
|
|
|
mImpl->GetMozFullPath(aFilename, aGuarantee, aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:53:07 +00:00
|
|
|
void File::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) {
|
2015-05-18 13:51:54 +00:00
|
|
|
mImpl->GetMozFullPathInternal(aFileName, aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<File> File::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Sequence<BlobPart>& aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
const FilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv) {
|
2020-08-17 09:52:14 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(aName);
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2020-03-25 14:14:37 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2016-11-28 20:58:15 +00:00
|
|
|
nsAutoString type(aBag.mType);
|
|
|
|
MakeValidBlobType(type);
|
2020-03-25 14:14:37 +00:00
|
|
|
impl->InitializeBlob(aData, type, aBag.mEndings == EndingType::Native,
|
|
|
|
global->CrossOriginIsolated(), aRv);
|
2014-10-08 16:15:22 +00:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
2014-10-08 16:15:22 +00:00
|
|
|
MOZ_ASSERT(impl->IsFile());
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-02-07 18:29:22 +00:00
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2019-10-21 05:33:33 +00:00
|
|
|
RefPtr<File> file = new File(global, impl);
|
2014-10-08 16:15:22 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Promise> File::CreateFromNsIFile(
|
2016-11-11 17:56:44 +00:00
|
|
|
const GlobalObject& aGlobal, nsIFile* aData,
|
|
|
|
const ChromeFilePropertyBag& aBag, SystemCallerGuarantee aGuarantee,
|
|
|
|
ErrorResult& aRv) {
|
2017-02-08 13:34:42 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
2019-10-21 05:33:19 +00:00
|
|
|
MOZ_ASSERT(global);
|
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-08 09:19:01 +00:00
|
|
|
RefPtr<Promise> promise =
|
2017-02-08 13:34:42 +00:00
|
|
|
FileCreatorHelper::CreateFile(global, aData, aBag, true, aRv);
|
2017-02-08 09:18:32 +00:00
|
|
|
return promise.forget();
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Promise> File::CreateFromFileName(
|
2016-11-11 17:56:44 +00:00
|
|
|
const GlobalObject& aGlobal, const nsAString& aPath,
|
|
|
|
const ChromeFilePropertyBag& aBag, SystemCallerGuarantee aGuarantee,
|
|
|
|
ErrorResult& aRv) {
|
2017-02-08 09:19:01 +00:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
aRv = NS_NewLocalFile(aPath, false, getter_AddRefs(file));
|
2017-02-08 09:18:32 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-08 13:34:42 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
2019-10-21 05:33:19 +00:00
|
|
|
MOZ_ASSERT(global);
|
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-08 09:19:01 +00:00
|
|
|
RefPtr<Promise> promise =
|
2017-02-08 13:34:42 +00:00
|
|
|
FileCreatorHelper::CreateFile(global, file, aBag, false, aRv);
|
2017-02-08 09:18:32 +00:00
|
|
|
return promise.forget();
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
} // namespace mozilla::dom
|