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