2017-02-16 20:26:38 +03:00
|
|
|
/* -*- 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 "FileBlobImpl.h"
|
2017-11-07 03:04:06 +03:00
|
|
|
#include "mozilla/SlicedInputStream.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
FileBlobImpl::FileBlobImpl(nsIFile* aFile)
|
2018-12-15 23:40:05 +03:00
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), EmptyString(),
|
|
|
|
EmptyString(), UINT64_MAX, INT64_MAX),
|
2017-02-16 20:26:38 +03:00
|
|
|
mFile(aFile),
|
2019-04-26 20:53:07 +03:00
|
|
|
mFileId(-1),
|
|
|
|
mWholeFile(true) {
|
2017-02-16 20:26:38 +03:00
|
|
|
MOZ_ASSERT(mFile, "must have file");
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2017-02-16 20:26:38 +03:00
|
|
|
// Lazily get the content type and size
|
|
|
|
mContentType.SetIsVoid(true);
|
2019-04-26 20:53:07 +03:00
|
|
|
mMozFullPath.SetIsVoid(true);
|
2017-02-16 20:26:38 +03:00
|
|
|
mFile->GetLeafName(mName);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileBlobImpl::FileBlobImpl(const nsAString& aName,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
|
|
|
nsIFile* aFile)
|
2018-12-15 23:40:05 +03:00
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), aName, aContentType,
|
|
|
|
aLength, UINT64_MAX),
|
2017-02-16 20:26:38 +03:00
|
|
|
mFile(aFile),
|
2019-04-26 20:53:07 +03:00
|
|
|
mFileId(-1),
|
|
|
|
mWholeFile(true) {
|
2017-02-16 20:26:38 +03:00
|
|
|
MOZ_ASSERT(mFile, "must have file");
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2019-04-26 20:53:07 +03:00
|
|
|
mMozFullPath.SetIsVoid(true);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FileBlobImpl::FileBlobImpl(const nsAString& aName,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
|
|
|
nsIFile* aFile, int64_t aLastModificationDate)
|
2018-12-15 23:40:05 +03:00
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), aName, aContentType,
|
|
|
|
aLength, aLastModificationDate),
|
2017-02-16 20:26:38 +03:00
|
|
|
mFile(aFile),
|
2019-04-26 20:53:07 +03:00
|
|
|
mFileId(-1),
|
|
|
|
mWholeFile(true) {
|
2017-02-16 20:26:38 +03:00
|
|
|
MOZ_ASSERT(mFile, "must have file");
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2019-04-26 20:53:07 +03:00
|
|
|
mMozFullPath.SetIsVoid(true);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FileBlobImpl::FileBlobImpl(nsIFile* aFile, const nsAString& aName,
|
2019-01-04 13:29:48 +03:00
|
|
|
const nsAString& aContentType,
|
|
|
|
const nsAString& aBlobImplType)
|
|
|
|
: BaseBlobImpl(aBlobImplType, aName, aContentType, UINT64_MAX, INT64_MAX),
|
2017-02-16 20:26:38 +03:00
|
|
|
mFile(aFile),
|
2019-04-26 20:53:07 +03:00
|
|
|
mFileId(-1),
|
|
|
|
mWholeFile(true) {
|
2017-02-16 20:26:38 +03:00
|
|
|
MOZ_ASSERT(mFile, "must have file");
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2017-02-16 20:26:38 +03:00
|
|
|
if (aContentType.IsEmpty()) {
|
|
|
|
// Lazily get the content type and size
|
|
|
|
mContentType.SetIsVoid(true);
|
|
|
|
}
|
2019-04-26 20:53:07 +03:00
|
|
|
|
|
|
|
mMozFullPath.SetIsVoid(true);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FileBlobImpl::FileBlobImpl(const FileBlobImpl* aOther, uint64_t aStart,
|
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
2018-12-15 23:40:05 +03:00
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), aContentType,
|
|
|
|
aOther->mStart + aStart, aLength),
|
2017-02-16 20:26:38 +03:00
|
|
|
mFile(aOther->mFile),
|
2019-04-26 20:53:07 +03:00
|
|
|
mFileId(-1),
|
|
|
|
mWholeFile(false) {
|
2017-02-16 20:26:38 +03:00
|
|
|
MOZ_ASSERT(mFile, "must have file");
|
2017-02-23 05:23:51 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2017-02-16 20:26:38 +03:00
|
|
|
mImmutable = aOther->mImmutable;
|
2019-04-26 20:53:07 +03:00
|
|
|
mMozFullPath = aOther->mMozFullPath;
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<BlobImpl> FileBlobImpl::CreateSlice(
|
|
|
|
uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv) {
|
2019-04-26 20:53:07 +03:00
|
|
|
RefPtr<FileBlobImpl> impl =
|
|
|
|
new FileBlobImpl(this, aStart, aLength, aContentType);
|
2017-02-16 20:26:38 +03:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileBlobImpl::GetMozFullPathInternal(nsAString& aFilename,
|
2019-04-26 20:53:07 +03:00
|
|
|
ErrorResult& aRv) {
|
2017-02-16 20:26:38 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2019-04-26 20:53:07 +03:00
|
|
|
|
|
|
|
if (!mMozFullPath.IsVoid()) {
|
|
|
|
aFilename = mMozFullPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
aRv = mFile->GetPath(aFilename);
|
2019-04-26 20:53:07 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMozFullPath = aFilename;
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t FileBlobImpl::GetSize(ErrorResult& aRv) {
|
|
|
|
if (BaseBlobImpl::IsSizeUnknown()) {
|
|
|
|
MOZ_ASSERT(mWholeFile,
|
|
|
|
"Should only use lazy size when using the whole file");
|
|
|
|
int64_t fileSize;
|
|
|
|
aRv = mFile->GetFileSize(&fileSize);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileSize < 0) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLength = fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GetTypeRunnable final : public WorkerMainThreadRunnable {
|
|
|
|
public:
|
|
|
|
GetTypeRunnable(WorkerPrivate* aWorkerPrivate, BlobImpl* aBlobImpl)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("FileBlobImpl :: GetType")),
|
|
|
|
mBlobImpl(aBlobImpl) {
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
mBlobImpl->GetType(type);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~GetTypeRunnable() = default;
|
|
|
|
|
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void FileBlobImpl::GetType(nsAString& aType) {
|
|
|
|
aType.Truncate();
|
|
|
|
|
|
|
|
if (mContentType.IsVoid()) {
|
|
|
|
MOZ_ASSERT(mWholeFile,
|
|
|
|
"Should only use lazy ContentType when using the whole file");
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
2018-02-05 21:55:07 +03:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
2017-02-16 20:26:38 +03:00
|
|
|
if (!workerPrivate) {
|
|
|
|
// I have no idea in which thread this method is called. We cannot
|
|
|
|
// return any valid value.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GetTypeRunnable> runnable =
|
|
|
|
new GetTypeRunnable(workerPrivate, this);
|
|
|
|
|
|
|
|
ErrorResult rv;
|
2018-07-12 20:33:41 +03:00
|
|
|
runnable->Dispatch(Canceling, rv);
|
2017-02-16 20:26:38 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService =
|
|
|
|
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString mimeType;
|
|
|
|
rv = mimeService->GetTypeFromFile(mFile, mimeType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mimeType.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF8toUTF16(mimeType, mContentType);
|
|
|
|
mContentType.SetIsVoid(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
aType = mContentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t FileBlobImpl::GetLastModified(ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
|
|
|
if (BaseBlobImpl::IsDateUnknown()) {
|
|
|
|
PRTime msecs;
|
|
|
|
aRv = mFile->GetLastModifiedTime(&msecs);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastModificationDate = msecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLastModificationDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t sFileStreamFlags =
|
|
|
|
nsIFileInputStream::CLOSE_ON_EOF | nsIFileInputStream::REOPEN_ON_REWIND |
|
|
|
|
nsIFileInputStream::DEFER_OPEN | nsIFileInputStream::SHARE_DELETE;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-02 14:53:12 +03:00
|
|
|
void FileBlobImpl::CreateInputStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) {
|
2017-02-22 11:41:51 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
aRv = NS_NewLocalFileInputStream(getter_AddRefs(stream), mFile, -1, -1,
|
|
|
|
sFileStreamFlags);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
if (mWholeFile) {
|
2017-02-22 11:41:51 +03:00
|
|
|
stream.forget(aStream);
|
2017-02-16 20:26:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:41:51 +03:00
|
|
|
RefPtr<SlicedInputStream> slicedInputStream =
|
2017-10-05 08:38:48 +03:00
|
|
|
new SlicedInputStream(stream.forget(), mStart, mLength);
|
2017-02-22 11:41:51 +03:00
|
|
|
slicedInputStream.forget(aStream);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileBlobImpl::IsDirectory() const {
|
|
|
|
bool isDirectory = false;
|
|
|
|
if (mFile) {
|
|
|
|
mFile->IsDirectory(&isDirectory);
|
|
|
|
}
|
|
|
|
return isDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|