mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-12-03 02:25:34 +00:00
409 lines
10 KiB
C++
409 lines
10 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=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 "QuotaObject.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
#include "QuotaManager.h"
|
|
#include "Utilities.h"
|
|
|
|
USING_QUOTA_NAMESPACE
|
|
|
|
namespace {
|
|
|
|
template <typename T, bool = mozilla::IsUnsigned<T>::value>
|
|
struct IntChecker
|
|
{
|
|
static void
|
|
Assert(T aInt)
|
|
{
|
|
static_assert(mozilla::IsIntegral<T>::value, "Not an integer!");
|
|
MOZ_ASSERT(aInt >= 0);
|
|
}
|
|
};
|
|
|
|
template <typename T>
|
|
struct IntChecker<T, true>
|
|
{
|
|
static void
|
|
Assert(T aInt)
|
|
{
|
|
static_assert(mozilla::IsIntegral<T>::value, "Not an integer!");
|
|
}
|
|
};
|
|
|
|
template <typename T>
|
|
void
|
|
AssertNoOverflow(uint64_t aDest, T aArg)
|
|
{
|
|
IntChecker<T>::Assert(aDest);
|
|
IntChecker<T>::Assert(aArg);
|
|
MOZ_ASSERT(UINT64_MAX - aDest >= uint64_t(aArg));
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
void
|
|
AssertNoUnderflow(T aDest, U aArg)
|
|
{
|
|
IntChecker<T>::Assert(aDest);
|
|
IntChecker<T>::Assert(aArg);
|
|
MOZ_ASSERT(uint64_t(aDest) >= uint64_t(aArg));
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
void
|
|
QuotaObject::AddRef()
|
|
{
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
if (!quotaManager) {
|
|
NS_ERROR("Null quota manager, this shouldn't happen, possible leak!");
|
|
|
|
++mRefCnt;
|
|
|
|
return;
|
|
}
|
|
|
|
MutexAutoLock lock(quotaManager->mQuotaMutex);
|
|
|
|
++mRefCnt;
|
|
}
|
|
|
|
void
|
|
QuotaObject::Release()
|
|
{
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
if (!quotaManager) {
|
|
NS_ERROR("Null quota manager, this shouldn't happen, possible leak!");
|
|
|
|
nsrefcnt count = --mRefCnt;
|
|
if (count == 0) {
|
|
mRefCnt = 1;
|
|
delete this;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
{
|
|
MutexAutoLock lock(quotaManager->mQuotaMutex);
|
|
|
|
--mRefCnt;
|
|
|
|
if (mRefCnt > 0) {
|
|
return;
|
|
}
|
|
|
|
if (mOriginInfo) {
|
|
mOriginInfo->mQuotaObjects.Remove(mPath);
|
|
}
|
|
}
|
|
|
|
delete this;
|
|
}
|
|
|
|
bool
|
|
QuotaObject::MaybeUpdateSize(int64_t aSize, bool aTruncate)
|
|
{
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
MOZ_ASSERT(quotaManager);
|
|
|
|
MutexAutoLock lock(quotaManager->mQuotaMutex);
|
|
|
|
if (mSize == aSize) {
|
|
return true;
|
|
}
|
|
|
|
if (!mOriginInfo) {
|
|
mSize = aSize;
|
|
return true;
|
|
}
|
|
|
|
GroupInfo* groupInfo = mOriginInfo->mGroupInfo;
|
|
MOZ_ASSERT(groupInfo);
|
|
|
|
if (mSize > aSize) {
|
|
if (aTruncate) {
|
|
const int64_t delta = mSize - aSize;
|
|
|
|
AssertNoUnderflow(quotaManager->mTemporaryStorageUsage, delta);
|
|
quotaManager->mTemporaryStorageUsage -= delta;
|
|
|
|
AssertNoUnderflow(groupInfo->mUsage, delta);
|
|
groupInfo->mUsage -= delta;
|
|
|
|
AssertNoUnderflow(mOriginInfo->mUsage, delta);
|
|
mOriginInfo->mUsage -= delta;
|
|
|
|
mSize = aSize;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
MOZ_ASSERT(mSize < aSize);
|
|
|
|
nsRefPtr<GroupInfo> complementaryGroupInfo =
|
|
groupInfo->mGroupInfoPair->LockedGetGroupInfo(
|
|
ComplementaryPersistenceType(groupInfo->mPersistenceType));
|
|
|
|
uint64_t delta = aSize - mSize;
|
|
|
|
AssertNoOverflow(mOriginInfo->mUsage, delta);
|
|
uint64_t newUsage = mOriginInfo->mUsage + delta;
|
|
|
|
// Temporary storage has no limit for origin usage (there's a group and the
|
|
// global limit though).
|
|
|
|
AssertNoOverflow(groupInfo->mUsage, delta);
|
|
uint64_t newGroupUsage = groupInfo->mUsage + delta;
|
|
|
|
uint64_t groupUsage = groupInfo->mUsage;
|
|
if (complementaryGroupInfo) {
|
|
AssertNoOverflow(groupUsage, complementaryGroupInfo->mUsage);
|
|
groupUsage += complementaryGroupInfo->mUsage;
|
|
}
|
|
|
|
// Temporary storage has a hard limit for group usage (20 % of the global
|
|
// limit).
|
|
AssertNoOverflow(groupUsage, delta);
|
|
if (groupUsage + delta > quotaManager->GetGroupLimit()) {
|
|
return false;
|
|
}
|
|
|
|
AssertNoOverflow(quotaManager->mTemporaryStorageUsage, delta);
|
|
uint64_t newTemporaryStorageUsage = quotaManager->mTemporaryStorageUsage +
|
|
delta;
|
|
|
|
if (newTemporaryStorageUsage > quotaManager->mTemporaryStorageLimit) {
|
|
// This will block the thread without holding the lock while waitting.
|
|
|
|
nsAutoTArray<OriginInfo*, 10> originInfos;
|
|
uint64_t sizeToBeFreed =
|
|
quotaManager->LockedCollectOriginsForEviction(delta, originInfos);
|
|
|
|
if (!sizeToBeFreed) {
|
|
return false;
|
|
}
|
|
|
|
NS_ASSERTION(sizeToBeFreed >= delta, "Huh?");
|
|
|
|
{
|
|
MutexAutoUnlock autoUnlock(quotaManager->mQuotaMutex);
|
|
|
|
for (uint32_t i = 0; i < originInfos.Length(); i++) {
|
|
OriginInfo* originInfo = originInfos[i];
|
|
|
|
quotaManager->DeleteFilesForOrigin(
|
|
originInfo->mGroupInfo->mPersistenceType,
|
|
originInfo->mOrigin);
|
|
}
|
|
}
|
|
|
|
// Relocked.
|
|
|
|
NS_ASSERTION(mOriginInfo, "How come?!");
|
|
|
|
nsTArray<OriginParams> origins;
|
|
for (uint32_t i = 0; i < originInfos.Length(); i++) {
|
|
OriginInfo* originInfo = originInfos[i];
|
|
|
|
NS_ASSERTION(originInfo != mOriginInfo, "Deleted itself!");
|
|
|
|
PersistenceType persistenceType =
|
|
originInfo->mGroupInfo->mPersistenceType;
|
|
nsCString group = originInfo->mGroupInfo->mGroup;
|
|
nsCString origin = originInfo->mOrigin;
|
|
bool isApp = originInfo->mIsApp;
|
|
quotaManager->LockedRemoveQuotaForOrigin(persistenceType, group, origin);
|
|
|
|
#ifdef DEBUG
|
|
originInfos[i] = nullptr;
|
|
#endif
|
|
|
|
origins.AppendElement(OriginParams(persistenceType, origin, isApp));
|
|
}
|
|
|
|
// We unlocked and relocked several times so we need to recompute all the
|
|
// essential variables and recheck the group limit.
|
|
|
|
AssertNoUnderflow(aSize, mSize);
|
|
delta = aSize - mSize;
|
|
|
|
AssertNoOverflow(mOriginInfo->mUsage, delta);
|
|
newUsage = mOriginInfo->mUsage + delta;
|
|
|
|
AssertNoOverflow(groupInfo->mUsage, delta);
|
|
newGroupUsage = groupInfo->mUsage + delta;
|
|
|
|
groupUsage = groupInfo->mUsage;
|
|
if (complementaryGroupInfo) {
|
|
AssertNoOverflow(groupUsage, complementaryGroupInfo->mUsage);
|
|
groupUsage += complementaryGroupInfo->mUsage;
|
|
}
|
|
|
|
AssertNoOverflow(groupUsage, delta);
|
|
if (groupUsage + delta > quotaManager->GetGroupLimit()) {
|
|
// Unfortunately some other thread increased the group usage in the
|
|
// meantime and we are not below the group limit anymore.
|
|
|
|
// However, the origin eviction must be finalized in this case too.
|
|
MutexAutoUnlock autoUnlock(quotaManager->mQuotaMutex);
|
|
|
|
quotaManager->FinalizeOriginEviction(origins);
|
|
|
|
return false;
|
|
}
|
|
|
|
AssertNoOverflow(quotaManager->mTemporaryStorageUsage, delta);
|
|
newTemporaryStorageUsage = quotaManager->mTemporaryStorageUsage + delta;
|
|
|
|
NS_ASSERTION(newTemporaryStorageUsage <=
|
|
quotaManager->mTemporaryStorageLimit, "How come?!");
|
|
|
|
// Ok, we successfully freed enough space and the operation can continue
|
|
// without throwing the quota error.
|
|
mOriginInfo->mUsage = newUsage;
|
|
groupInfo->mUsage = newGroupUsage;
|
|
quotaManager->mTemporaryStorageUsage = newTemporaryStorageUsage;;
|
|
|
|
// Some other thread could increase the size in the meantime, but no more
|
|
// than this one.
|
|
MOZ_ASSERT(mSize < aSize);
|
|
mSize = aSize;
|
|
|
|
// Finally, release IO thread only objects and allow next synchronized
|
|
// ops for the evicted origins.
|
|
MutexAutoUnlock autoUnlock(quotaManager->mQuotaMutex);
|
|
|
|
quotaManager->FinalizeOriginEviction(origins);
|
|
|
|
return true;
|
|
}
|
|
|
|
mOriginInfo->mUsage = newUsage;
|
|
groupInfo->mUsage = newGroupUsage;
|
|
quotaManager->mTemporaryStorageUsage = newTemporaryStorageUsage;
|
|
|
|
mSize = aSize;
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
OriginInfo::LockedDecreaseUsage(int64_t aSize)
|
|
{
|
|
AssertCurrentThreadOwnsQuotaMutex();
|
|
|
|
AssertNoUnderflow(mUsage, aSize);
|
|
mUsage -= aSize;
|
|
|
|
AssertNoUnderflow(mGroupInfo->mUsage, aSize);
|
|
mGroupInfo->mUsage -= aSize;
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
MOZ_ASSERT(quotaManager);
|
|
|
|
AssertNoUnderflow(quotaManager->mTemporaryStorageUsage, aSize);
|
|
quotaManager->mTemporaryStorageUsage -= aSize;
|
|
}
|
|
|
|
already_AddRefed<OriginInfo>
|
|
GroupInfo::LockedGetOriginInfo(const nsACString& aOrigin)
|
|
{
|
|
AssertCurrentThreadOwnsQuotaMutex();
|
|
|
|
for (uint32_t index = 0; index < mOriginInfos.Length(); index++) {
|
|
nsRefPtr<OriginInfo>& originInfo = mOriginInfos[index];
|
|
|
|
if (originInfo->mOrigin == aOrigin) {
|
|
nsRefPtr<OriginInfo> result = originInfo;
|
|
return result.forget();
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
void
|
|
GroupInfo::LockedAddOriginInfo(OriginInfo* aOriginInfo)
|
|
{
|
|
AssertCurrentThreadOwnsQuotaMutex();
|
|
|
|
NS_ASSERTION(!mOriginInfos.Contains(aOriginInfo),
|
|
"Replacing an existing entry!");
|
|
mOriginInfos.AppendElement(aOriginInfo);
|
|
|
|
AssertNoOverflow(mUsage, aOriginInfo->mUsage);
|
|
mUsage += aOriginInfo->mUsage;
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
MOZ_ASSERT(quotaManager);
|
|
|
|
AssertNoOverflow(quotaManager->mTemporaryStorageUsage, aOriginInfo->mUsage);
|
|
quotaManager->mTemporaryStorageUsage += aOriginInfo->mUsage;
|
|
}
|
|
|
|
void
|
|
GroupInfo::LockedRemoveOriginInfo(const nsACString& aOrigin)
|
|
{
|
|
AssertCurrentThreadOwnsQuotaMutex();
|
|
|
|
for (uint32_t index = 0; index < mOriginInfos.Length(); index++) {
|
|
if (mOriginInfos[index]->mOrigin == aOrigin) {
|
|
AssertNoUnderflow(mUsage, mOriginInfos[index]->mUsage);
|
|
mUsage -= mOriginInfos[index]->mUsage;
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
MOZ_ASSERT(quotaManager);
|
|
|
|
AssertNoUnderflow(quotaManager->mTemporaryStorageUsage,
|
|
mOriginInfos[index]->mUsage);
|
|
quotaManager->mTemporaryStorageUsage -= mOriginInfos[index]->mUsage;
|
|
|
|
mOriginInfos.RemoveElementAt(index);
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
GroupInfo::LockedRemoveOriginInfos()
|
|
{
|
|
AssertCurrentThreadOwnsQuotaMutex();
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
MOZ_ASSERT(quotaManager);
|
|
|
|
for (uint32_t index = mOriginInfos.Length(); index > 0; index--) {
|
|
OriginInfo* originInfo = mOriginInfos[index - 1];
|
|
|
|
AssertNoUnderflow(mUsage, originInfo->mUsage);
|
|
mUsage -= originInfo->mUsage;
|
|
|
|
AssertNoUnderflow(quotaManager->mTemporaryStorageUsage, originInfo->mUsage);
|
|
quotaManager->mTemporaryStorageUsage -= originInfo->mUsage;
|
|
|
|
mOriginInfos.RemoveElementAt(index - 1);
|
|
}
|
|
}
|
|
|
|
nsRefPtr<GroupInfo>&
|
|
GroupInfoPair::GetGroupInfoForPersistenceType(PersistenceType aPersistenceType)
|
|
{
|
|
switch (aPersistenceType) {
|
|
case PERSISTENCE_TYPE_TEMPORARY:
|
|
return mTemporaryStorageGroupInfo;
|
|
case PERSISTENCE_TYPE_DEFAULT:
|
|
return mDefaultStorageGroupInfo;
|
|
|
|
case PERSISTENCE_TYPE_PERSISTENT:
|
|
case PERSISTENCE_TYPE_INVALID:
|
|
default:
|
|
MOZ_CRASH("Bad persistence type value!");
|
|
}
|
|
}
|