2012-06-03 09:33:52 -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/. */
|
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
#include "IDBMutableFile.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-05-07 07:32:12 -07:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-06-11 20:35:29 -07:00
|
|
|
#include "mozilla/dom/IDBMutableFileBinding.h"
|
2012-12-17 11:25:10 -08:00
|
|
|
#include "mozilla/dom/quota/FileStreams.h"
|
2014-05-07 07:33:02 -07:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2012-06-19 18:50:39 -07:00
|
|
|
#include "IDBDatabase.h"
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-12-17 11:25:10 -08:00
|
|
|
USING_QUOTA_NAMESPACE
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
|
|
|
already_AddRefed<nsIFile>
|
|
|
|
GetFileFor(FileInfo* aFileInfo)
|
|
|
|
|
|
|
|
{
|
|
|
|
FileManager* fileManager = aFileInfo->Manager();
|
|
|
|
nsCOMPtr<nsIFile> directory = fileManager->GetDirectory();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(directory, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = fileManager->GetFileForId(directory,
|
|
|
|
aFileInfo->Id());
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(file, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::IDBMutableFile(IDBDatabase* aOwner)
|
|
|
|
: MutableFile(aOwner)
|
2014-01-06 18:53:23 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBMutableFile, MutableFile, mDatabase)
|
2014-05-07 07:33:02 -07:00
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBMutableFile)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(MutableFile)
|
2014-05-07 07:33:02 -07:00
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBMutableFile, MutableFile)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBMutableFile, MutableFile)
|
2014-05-07 07:33:02 -07:00
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
// static
|
2014-06-11 20:35:29 -07:00
|
|
|
already_AddRefed<IDBMutableFile>
|
|
|
|
IDBMutableFile::Create(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
IDBDatabase* aDatabase,
|
|
|
|
already_AddRefed<FileInfo> aFileInfo)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<FileInfo> fileInfo(aFileInfo);
|
|
|
|
NS_ASSERTION(fileInfo, "Null pointer!");
|
|
|
|
|
2014-06-11 20:35:29 -07:00
|
|
|
nsRefPtr<IDBMutableFile> newFile = new IDBMutableFile(aDatabase);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
newFile->mName = aName;
|
|
|
|
newFile->mType = aType;
|
|
|
|
|
|
|
|
newFile->mFile = GetFileFor(fileInfo);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(newFile->mFile, nullptr);
|
2014-05-07 07:33:02 -07:00
|
|
|
|
|
|
|
newFile->mStorageId = aDatabase->Id();
|
2012-06-03 09:33:52 -07:00
|
|
|
newFile->mFileName.AppendInt(fileInfo->Id());
|
|
|
|
|
2014-05-07 07:33:02 -07:00
|
|
|
newFile->mDatabase = aDatabase;
|
2012-06-03 09:33:52 -07:00
|
|
|
fileInfo.swap(newFile->mFileInfo);
|
|
|
|
|
|
|
|
return newFile.forget();
|
|
|
|
}
|
|
|
|
|
2014-05-07 07:33:02 -07:00
|
|
|
bool
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::IsShuttingDown()
|
2014-05-07 07:33:02 -07:00
|
|
|
{
|
2014-06-11 20:35:29 -07:00
|
|
|
return QuotaManager::IsShuttingDown() || MutableFile::IsShuttingDown();
|
2014-05-07 07:33:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::IsInvalid()
|
2014-05-07 07:33:02 -07:00
|
|
|
{
|
|
|
|
return mDatabase->IsInvalidated();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIOfflineStorage*
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::Storage()
|
2014-05-07 07:33:02 -07:00
|
|
|
{
|
|
|
|
return mDatabase;
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
already_AddRefed<nsISupports>
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::CreateStream(nsIFile* aFile, bool aReadOnly)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-05-07 07:33:02 -07:00
|
|
|
PersistenceType persistenceType = mDatabase->Type();
|
|
|
|
const nsACString& group = mDatabase->Group();
|
|
|
|
const nsACString& origin = mDatabase->Origin();
|
2012-12-17 11:25:10 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> result;
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
if (aReadOnly) {
|
2013-09-10 21:18:36 -07:00
|
|
|
nsRefPtr<FileInputStream> stream =
|
|
|
|
FileInputStream::Create(persistenceType, group, origin, aFile, -1, -1,
|
|
|
|
nsIFileInputStream::DEFER_OPEN);
|
2012-12-17 11:25:10 -08:00
|
|
|
result = NS_ISUPPORTS_CAST(nsIFileInputStream*, stream);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
|
|
|
else {
|
2013-09-10 21:18:36 -07:00
|
|
|
nsRefPtr<FileStream> stream =
|
|
|
|
FileStream::Create(persistenceType, group, origin, aFile, -1, -1,
|
|
|
|
nsIFileStream::DEFER_OPEN);
|
2012-12-17 11:25:10 -08:00
|
|
|
result = NS_ISUPPORTS_CAST(nsIFileStream*, stream);
|
2012-06-03 09:33:52 -07:00
|
|
|
}
|
2012-12-17 11:25:10 -08:00
|
|
|
NS_ENSURE_TRUE(result, nullptr);
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2014-05-07 07:33:02 -07:00
|
|
|
void
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::SetThreadLocals()
|
2014-05-07 07:33:02 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDatabase->GetOwner(), "Should have owner!");
|
|
|
|
QuotaManager::SetCurrentWindow(mDatabase->GetOwner());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::UnsetThreadLocals()
|
2014-05-07 07:33:02 -07:00
|
|
|
{
|
|
|
|
QuotaManager::SetCurrentWindow(nullptr);
|
|
|
|
}
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
already_AddRefed<nsIDOMFile>
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::CreateFileObject(mozilla::dom::FileHandle* aFileHandle,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFileSize)
|
2012-06-03 09:33:52 -07:00
|
|
|
{
|
2014-06-26 20:46:34 -07:00
|
|
|
nsCOMPtr<nsIDOMFile> file = new DOMFile(
|
2014-06-26 09:47:44 -07:00
|
|
|
new FileImpl(mName, mType, aFileSize, mFile, aFileHandle, mFileInfo));
|
2012-06-03 09:33:52 -07:00
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-24 12:56:13 -08:00
|
|
|
// virtual
|
|
|
|
JSObject*
|
2014-06-11 20:35:29 -07:00
|
|
|
IDBMutableFile::WrapObject(JSContext* aCx)
|
2014-02-24 12:56:13 -08:00
|
|
|
{
|
2014-06-11 20:35:29 -07:00
|
|
|
return IDBMutableFileBinding::Wrap(aCx, this);
|
2014-02-24 12:56:13 -08:00
|
|
|
}
|