2011-12-16 11:34:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
#include "FileInfo.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
#include "FileManager.h"
|
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2014-09-18 03:36:01 +04:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
using namespace mozilla::dom::quota;
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2013-11-19 15:00:20 +04:00
|
|
|
namespace {
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
template <typename IdType>
|
|
|
|
class FileInfoImpl MOZ_FINAL
|
|
|
|
: public FileInfo
|
2013-11-19 15:00:20 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
IdType mFileId;
|
2014-06-23 23:56:07 +04:00
|
|
|
|
2013-11-19 15:00:20 +04:00
|
|
|
public:
|
2014-09-27 03:21:57 +04:00
|
|
|
FileInfoImpl(FileManager* aFileManager, IdType aFileId)
|
|
|
|
: FileInfo(aFileManager)
|
|
|
|
, mFileId(aFileId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFileManager);
|
|
|
|
MOZ_ASSERT(aFileId > 0);
|
|
|
|
}
|
2014-09-13 20:12:19 +04:00
|
|
|
|
2014-09-18 03:36:01 +04:00
|
|
|
private:
|
2014-09-27 03:21:57 +04:00
|
|
|
~FileInfoImpl()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual int64_t
|
|
|
|
Id() const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return int64_t(mFileId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CleanupFileRunnable MOZ_FINAL
|
|
|
|
: public nsRunnable
|
|
|
|
{
|
2013-11-19 15:00:20 +04:00
|
|
|
nsRefPtr<FileManager> mFileManager;
|
|
|
|
int64_t mFileId;
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
static void
|
|
|
|
DoCleanup(FileManager* aFileManager, int64_t aFileId);
|
|
|
|
|
|
|
|
CleanupFileRunnable(FileManager* aFileManager, int64_t aFileId)
|
|
|
|
: mFileManager(aFileManager)
|
|
|
|
, mFileId(aFileId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFileManager);
|
|
|
|
MOZ_ASSERT(aFileId > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CleanupFileRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
2013-11-19 15:00:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
FileInfo::FileInfo(FileManager* aFileManager)
|
|
|
|
: mFileManager(aFileManager)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFileManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo::~FileInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
// static
|
|
|
|
FileInfo*
|
2012-08-22 19:56:38 +04:00
|
|
|
FileInfo::Create(FileManager* aFileManager, int64_t aId)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(aFileManager);
|
|
|
|
MOZ_ASSERT(aId > 0);
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
if (aId <= INT16_MAX) {
|
2014-09-27 03:21:57 +04:00
|
|
|
return new FileInfoImpl<int16_t>(aFileManager, aId);
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
if (aId <= INT32_MAX) {
|
2014-09-27 03:21:57 +04:00
|
|
|
return new FileInfoImpl<int32_t>(aFileManager, aId);
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
return new FileInfoImpl<int64_t>(aFileManager, aId);
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-27 03:21:57 +04:00
|
|
|
FileInfo::GetReferences(int32_t* aRefCnt,
|
|
|
|
int32_t* aDBRefCnt,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aSliceRefCnt)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(!IndexedDatabaseManager::IsClosed());
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
|
|
|
|
|
|
|
|
if (aRefCnt) {
|
|
|
|
*aRefCnt = mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDBRefCnt) {
|
|
|
|
*aDBRefCnt = mDBRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSliceRefCnt) {
|
|
|
|
*aSliceRefCnt = mSliceRefCnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-27 03:21:57 +04:00
|
|
|
FileInfo::UpdateReferences(ThreadSafeAutoRefCnt& aRefCount,
|
|
|
|
int32_t aDelta,
|
|
|
|
bool aClear)
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
// XXX This can go away once DOM objects no longer hold FileInfo objects...
|
|
|
|
// Looking at you, IDBMutableFile...
|
2011-12-16 11:34:24 +04:00
|
|
|
if (IndexedDatabaseManager::IsClosed()) {
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(&aRefCount == &mRefCnt);
|
|
|
|
MOZ_ASSERT(aDelta == 1 || aDelta == -1);
|
|
|
|
MOZ_ASSERT(!aClear);
|
|
|
|
|
|
|
|
if (aDelta > 0) {
|
|
|
|
++aRefCount;
|
|
|
|
} else {
|
|
|
|
nsrefcnt count = --aRefCount;
|
|
|
|
if (!count) {
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 11:34:24 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(!IndexedDatabaseManager::IsClosed());
|
|
|
|
|
2013-06-05 12:11:23 +04:00
|
|
|
bool needsCleanup;
|
2011-12-16 11:34:24 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(IndexedDatabaseManager::FileMutex());
|
|
|
|
|
|
|
|
aRefCount = aClear ? 0 : aRefCount + aDelta;
|
|
|
|
|
|
|
|
if (mRefCnt + mDBRefCnt + mSliceRefCnt > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFileManager->mFileInfos.Remove(Id());
|
2013-06-05 12:11:23 +04:00
|
|
|
|
|
|
|
needsCleanup = !mFileManager->Invalidated();
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 12:11:23 +04:00
|
|
|
if (needsCleanup) {
|
|
|
|
Cleanup();
|
|
|
|
}
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileInfo::Cleanup()
|
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
int64_t id = Id();
|
2013-06-05 12:11:23 +04:00
|
|
|
|
2013-11-19 15:00:20 +04:00
|
|
|
// IndexedDatabaseManager is main-thread only.
|
|
|
|
if (!NS_IsMainThread()) {
|
2014-09-27 03:21:57 +04:00
|
|
|
nsRefPtr<CleanupFileRunnable> cleaner =
|
|
|
|
new CleanupFileRunnable(mFileManager, id);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(cleaner)));
|
2011-12-16 11:34:24 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
CleanupFileRunnable::DoCleanup(mFileManager, id);
|
2013-11-19 15:00:20 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
CleanupFileRunnable::DoCleanup(FileManager* aFileManager, int64_t aFileId)
|
2013-11-19 15:00:20 +04:00
|
|
|
{
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aFileManager);
|
|
|
|
MOZ_ASSERT(aFileId > 0);
|
2014-09-18 03:36:01 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_WARN_IF(QuotaManager::IsShuttingDown())) {
|
|
|
|
return;
|
2013-11-19 15:00:20 +04:00
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
nsRefPtr<IndexedDatabaseManager> mgr = IndexedDatabaseManager::Get();
|
2013-11-19 15:00:20 +04:00
|
|
|
MOZ_ASSERT(mgr);
|
2011-12-16 11:34:24 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_FAILED(mgr->AsyncDeleteFile(aFileManager, aFileId))) {
|
2011-12-16 11:34:24 +04:00
|
|
|
NS_WARNING("Failed to delete file asynchronously!");
|
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(CleanupFileRunnable, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CleanupFileRunnable::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
DoCleanup(mFileManager, mFileId);
|
2013-11-19 15:00:20 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-12-16 11:34:24 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|