2014-07-17 09:40:54 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=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 "FileSnapshot.h"
|
|
|
|
|
|
|
|
#include "IDBFileHandle.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-09-17 16:36:01 -07:00
|
|
|
#include "nsDebug.h"
|
2014-07-17 09:40:54 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
|
|
|
|
2014-09-17 16:36:01 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(FileImplSnapshot, DOMFileImpl)
|
|
|
|
|
2014-07-17 09:40:54 -07:00
|
|
|
// Create as a stored file
|
|
|
|
FileImplSnapshot::FileImplSnapshot(const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
2014-09-17 16:36:01 -07:00
|
|
|
uint64_t aLength, nsIFile* aFile,
|
2014-07-17 09:40:54 -07:00
|
|
|
IDBFileHandle* aFileHandle,
|
|
|
|
FileInfo* aFileInfo)
|
2014-09-17 16:36:01 -07:00
|
|
|
: DOMFileImplBase(aName, aContentType, aLength),
|
|
|
|
mFile(aFile), mFileHandle(aFileHandle), mWholeFile(true)
|
2014-07-17 09:40:54 -07:00
|
|
|
{
|
2014-09-17 16:36:01 -07:00
|
|
|
MOZ_ASSERT(mFile, "Null file!");
|
|
|
|
MOZ_ASSERT(mFileHandle, "Null file handle!");
|
2014-07-17 09:40:54 -07:00
|
|
|
mFileInfos.AppendElement(aFileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create slice
|
|
|
|
FileImplSnapshot::FileImplSnapshot(const FileImplSnapshot* aOther,
|
2014-09-17 16:36:01 -07:00
|
|
|
uint64_t aStart, uint64_t aLength,
|
2014-07-17 09:40:54 -07:00
|
|
|
const nsAString& aContentType)
|
2014-09-17 16:36:01 -07:00
|
|
|
: DOMFileImplBase(aContentType, aOther->mStart + aStart, aLength),
|
|
|
|
mFile(aOther->mFile), mFileHandle(aOther->mFileHandle),
|
|
|
|
mWholeFile(false)
|
2014-07-17 09:40:54 -07:00
|
|
|
{
|
2014-09-17 16:36:01 -07:00
|
|
|
MOZ_ASSERT(mFile, "Null file!");
|
|
|
|
MOZ_ASSERT(mFileHandle, "Null file handle!");
|
2014-07-17 09:40:54 -07:00
|
|
|
|
|
|
|
FileInfo* fileInfo;
|
|
|
|
|
|
|
|
if (IndexedDatabaseManager::IsClosed()) {
|
|
|
|
fileInfo = aOther->GetFileInfo();
|
|
|
|
} else {
|
|
|
|
MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
|
|
|
|
fileInfo = aOther->GetFileInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
mFileInfos.AppendElement(fileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileImplSnapshot::~FileImplSnapshot()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileImplSnapshot::Unlink()
|
|
|
|
{
|
|
|
|
FileImplSnapshot* tmp = this;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFileHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileImplSnapshot::Traverse(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
FileImplSnapshot* tmp = this;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFileHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileImplSnapshot::GetInternalStream(nsIInputStream** aStream)
|
|
|
|
{
|
2014-09-17 16:36:01 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2014-07-17 09:40:54 -07:00
|
|
|
|
|
|
|
nsresult rv = mFileHandle->OpenInputStream(mWholeFile, mStart, mLength,
|
|
|
|
aStream);
|
2014-09-17 16:36:01 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-07-17 09:40:54 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:36:01 -07:00
|
|
|
already_AddRefed<nsIDOMBlob>
|
|
|
|
FileImplSnapshot::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2014-07-17 09:40:54 -07:00
|
|
|
const nsAString& aContentType)
|
|
|
|
{
|
2014-09-17 16:36:01 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2014-09-17 16:36:01 -07:00
|
|
|
nsCOMPtr<nsIDOMBlob> t =
|
|
|
|
new DOMFile(new FileImplSnapshot(this, aStart, aLength, aContentType));
|
2014-07-17 09:40:54 -07:00
|
|
|
|
2014-09-17 16:36:01 -07:00
|
|
|
return t.forget();
|
2014-07-17 09:40:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileImplSnapshot::GetMozFullPathInternal(nsAString& aFilename)
|
|
|
|
{
|
2014-09-17 16:36:01 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
2014-07-17 09:40:54 -07:00
|
|
|
|
|
|
|
return mFile->GetPath(aFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|