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"
|
2017-02-23 05:23:51 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
File::File(nsISupports* aParent, BlobImpl* aImpl) : Blob(aParent, aImpl) {
|
2015-05-12 15:09:51 +03:00
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
File::~File() {}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
File* File::Create(nsISupports* aParent, BlobImpl* aImpl) {
|
2015-05-12 15:09:51 +03:00
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
|
|
|
|
return new File(aParent, aImpl);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<File> File::Create(nsISupports* aParent,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aLength,
|
|
|
|
int64_t aLastModifiedDate) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(
|
2018-12-15 23:40:05 +03:00
|
|
|
aParent, new BaseBlobImpl(NS_LITERAL_STRING("BaseBlobImpl"), aName,
|
|
|
|
aContentType, aLength, aLastModifiedDate));
|
2015-05-11 18:43:59 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<File> File::CreateMemoryFile(nsISupports* aParent,
|
|
|
|
void* aMemoryBuffer,
|
|
|
|
uint64_t aLength,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file =
|
2017-02-16 20:26:38 +03:00
|
|
|
new File(aParent, new MemoryBlobImpl(aMemoryBuffer, aLength, aName,
|
2015-05-12 15:09:51 +03:00
|
|
|
aContentType, aLastModifiedDate));
|
2015-05-12 00:54:02 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<File> File::CreateFromFile(nsISupports* aParent,
|
|
|
|
nsIFile* aFile) {
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
2017-02-16 20:26:38 +03:00
|
|
|
RefPtr<File> file = new File(aParent, new FileBlobImpl(aFile));
|
2014-10-07 21:16:11 +04:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<File> File::CreateFromFile(nsISupports* aParent,
|
|
|
|
nsIFile* aFile,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType) {
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file =
|
2017-02-16 20:26:38 +03:00
|
|
|
new File(aParent, 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) {
|
2016-12-01 17:14:45 +03:00
|
|
|
// Normalizing the filename
|
|
|
|
nsString name(aName);
|
|
|
|
name.ReplaceChar('/', ':');
|
|
|
|
|
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(name);
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2016-11-28 23:58:15 +03:00
|
|
|
nsAutoString type(aBag.mType);
|
|
|
|
MakeValidBlobType(type);
|
2018-10-27 11:26:31 +03:00
|
|
|
impl->InitializeBlob(aData, type, aBag.mEndings == EndingTypes::Native, 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());
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aGlobal.GetAsSupports(), 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());
|
|
|
|
|
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());
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|