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)
|
2015-05-12 15:09:51 +03:00
|
|
|
: Blob(aParent, aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
File::~File()
|
|
|
|
{}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
/* static */ File*
|
2015-05-12 15:11:03 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:43:59 +03:00
|
|
|
/* static */ already_AddRefed<File>
|
2015-05-12 15:09:51 +03:00
|
|
|
File::Create(nsISupports* aParent, const nsAString& aName,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
2016-03-20 13:56:10 +03:00
|
|
|
int64_t aLastModifiedDate)
|
2015-05-11 18:43:59 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2017-02-16 20:26:38 +03:00
|
|
|
new BaseBlobImpl(aName, aContentType, aLength, aLastModifiedDate));
|
2015-05-11 18:43:59 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 00:54:02 +03:00
|
|
|
/* static */ already_AddRefed<File>
|
2015-05-12 15:09:51 +03:00
|
|
|
File::CreateMemoryFile(nsISupports* aParent, void* aMemoryBuffer,
|
|
|
|
uint64_t aLength, const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate)
|
2015-05-12 00:54:02 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2017-02-16 20:26:38 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
/* static */ already_AddRefed<File>
|
2017-01-24 14:11:07 +03:00
|
|
|
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
/* static */ already_AddRefed<File>
|
|
|
|
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile,
|
|
|
|
const nsAString& aName, const nsAString& aContentType)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2017-02-16 20:26:38 +03:00
|
|
|
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)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
return FileBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2016-04-01 13:50:16 +03:00
|
|
|
File::GetName(nsAString& aFileName) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
mImpl->GetName(aFileName);
|
2015-05-11 18:43:59 +03:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2017-02-06 13:07:54 +03:00
|
|
|
File::GetRelativePath(nsAString& aPath) const
|
2015-05-11 18:43:59 +03:00
|
|
|
{
|
2017-02-06 13:07:54 +03:00
|
|
|
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
|
|
|
Date
|
|
|
|
File::GetLastModifiedDate(ErrorResult& aRv)
|
2015-05-12 00:54:02 +03:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
int64_t value = GetLastModified(aRv);
|
2014-10-08 20:15:22 +04:00
|
|
|
if (aRv.Failed()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
return Date();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-02 05:12:52 +03:00
|
|
|
return Date(JS::TimeClip(value));
|
2015-05-12 00:54:02 +03:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
int64_t
|
|
|
|
File::GetLastModified(ErrorResult& aRv)
|
2015-05-12 00:54:02 +03:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
return mImpl->GetLastModified(aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2017-02-01 23:43:37 +03:00
|
|
|
File::GetMozFullPath(nsAString& aFilename, SystemCallerGuarantee aGuarantee,
|
|
|
|
ErrorResult& aRv) const
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
2017-02-01 23:43:37 +03:00
|
|
|
mImpl->GetMozFullPath(aFilename, aGuarantee, aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2015-06-03 03:11:16 +03:00
|
|
|
File::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-18 16:51:54 +03:00
|
|
|
mImpl->GetMozFullPathInternal(aFileName, aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
/* static */ already_AddRefed<File>
|
2016-06-01 13:33:09 +03:00
|
|
|
File::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Sequence<BlobPart>& aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
const FilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
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);
|
2017-04-25 23:30:23 +03:00
|
|
|
impl->InitializeBlob(aData, type, false, 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();
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:18:32 +03:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2016-11-11 20:56:44 +03:00
|
|
|
File::CreateFromNsIFile(const GlobalObject& aGlobal,
|
|
|
|
nsIFile* aData,
|
|
|
|
const ChromeFilePropertyBag& aBag,
|
2017-02-01 23:43:37 +03:00
|
|
|
SystemCallerGuarantee aGuarantee,
|
2016-11-11 20:56:44 +03:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-02-08 12:18:32 +03:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2016-11-11 20:56:44 +03:00
|
|
|
File::CreateFromFileName(const GlobalObject& aGlobal,
|
2017-02-08 12:19:01 +03:00
|
|
|
const nsAString& aPath,
|
2016-11-11 20:56:44 +03:00
|
|
|
const ChromeFilePropertyBag& aBag,
|
2017-02-01 23:43:37 +03:00
|
|
|
SystemCallerGuarantee aGuarantee,
|
2016-11-11 20:56:44 +03:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
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
|