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