2011-12-16 11:34:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=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>
|
2015-03-21 19:28:04 +03:00
|
|
|
class FileInfoImpl final
|
2014-09-27 03:21:57 +04:00
|
|
|
: 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
|
2015-03-21 19:28:04 +03:00
|
|
|
Id() const override
|
2014-09-27 03:21:57 +04:00
|
|
|
{
|
|
|
|
return int64_t(mFileId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CleanupFileRunnable final
|
2016-04-26 03:23:21 +03:00
|
|
|
: public Runnable
|
2014-09-27 03:21:57 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FileManager> mFileManager;
|
2013-11-19 15:00:20 +04:00
|
|
|
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)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::indexedDB::CleanupFileRunnable")
|
|
|
|
, mFileManager(aFileManager)
|
2014-09-27 03:21:57 +04:00
|
|
|
, 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
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-11-19 15:00:20 +04:00
|
|
|
|
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,
|
2016-03-15 09:00:15 +03:00
|
|
|
int32_t aDelta,
|
|
|
|
CustomCleanupCallback* aCustomCleanupCallback)
|
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...
|
2015-09-09 14:15:05 +03:00
|
|
|
// Looking at you, BlobImplBase...
|
|
|
|
// BlobImplBase is being addressed in bug 1068975.
|
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);
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2014-11-13 12:43:22 +03:00
|
|
|
aRefCount = aRefCount + aDelta;
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
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) {
|
2016-03-15 09:00:15 +03:00
|
|
|
if (aCustomCleanupCallback) {
|
|
|
|
nsresult rv = aCustomCleanupCallback->Cleanup(mFileManager, Id());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Custom cleanup failed!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Cleanup();
|
|
|
|
}
|
2013-06-05 12:11:23 +04:00
|
|
|
}
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2014-11-13 12:43:22 +03:00
|
|
|
bool
|
|
|
|
FileInfo::LockedClearDBRefs()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IndexedDatabaseManager::IsClosed());
|
|
|
|
|
|
|
|
IndexedDatabaseManager::FileMutex().AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
mDBRefCnt = 0;
|
|
|
|
|
|
|
|
if (mRefCnt || mSliceRefCnt) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In this case, we are not responsible for removing the file info from the
|
|
|
|
// hashtable. It's up to FileManager which is the only caller of this method.
|
|
|
|
|
|
|
|
MOZ_ASSERT(mFileManager->Invalidated());
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
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()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CleanupFileRunnable> cleaner =
|
2014-09-27 03:21:57 +04:00
|
|
|
new CleanupFileRunnable(mFileManager, id);
|
|
|
|
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(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
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<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
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(CleanupFileRunnable, Runnable)
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-01-18 15:19:06 +03:00
|
|
|
/* static */ already_AddRefed<nsIFile>
|
|
|
|
FileInfo::GetFileForFileInfo(FileInfo* aFileInfo)
|
|
|
|
{
|
|
|
|
FileManager* fileManager = aFileInfo->Manager();
|
|
|
|
nsCOMPtr<nsIFile> directory = fileManager->GetDirectory();
|
|
|
|
if (NS_WARN_IF(!directory)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file = fileManager->GetFileForId(directory,
|
|
|
|
aFileInfo->Id());
|
|
|
|
if (NS_WARN_IF(!file)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|