StorageQuotaManager.cpp [plain text]
#include "config.h"
#include "StorageQuotaManager.h"
#include "Logging.h"
#include <wtf/Ref.h>
#include <wtf/RefCounted.h>
#include <wtf/threads/BinarySemaphore.h>
namespace WebCore {
Ref<StorageQuotaManager> StorageQuotaManager::create(uint64_t quota, UsageGetter&& usageGetter, QuotaIncreaseRequester&& quotaIncreaseRequester)
{
return adoptRef(*new StorageQuotaManager(quota, WTFMove(usageGetter), WTFMove(quotaIncreaseRequester)));
}
StorageQuotaManager::StorageQuotaManager(uint64_t quota, UsageGetter&& usageGetter, QuotaIncreaseRequester&& quotaIncreaseRequester)
: m_quota(quota)
, m_usageGetter(WTFMove(usageGetter))
, m_quotaIncreaseRequester(WTFMove(quotaIncreaseRequester))
, m_workQueue(WorkQueue::create("StorageQuotaManager Background Queue", WorkQueue::Type::Serial))
, m_initialQuota(quota)
{
}
void StorageQuotaManager::requestSpaceOnMainThread(uint64_t spaceRequested, RequestCallback&& callback)
{
ASSERT(isMainThread());
if (m_quotaCountDownLock.tryLock()) {
if (tryGrantRequest(spaceRequested)) {
m_quotaCountDownLock.unlock();
callback(Decision::Grant);
return;
}
m_quotaCountDownLock.unlock();
}
m_workQueue->dispatch([this, protectedThis = makeRef(*this), spaceRequested, callback = WTFMove(callback)]() mutable {
auto decision = requestSpaceOnBackgroundThread(spaceRequested);
callOnMainThread([callback = WTFMove(callback), decision]() mutable {
callback(decision);
});
});
}
StorageQuotaManager::Decision StorageQuotaManager::requestSpaceOnBackgroundThread(uint64_t spaceRequested)
{
ASSERT(!isMainThread());
LockHolder locker(m_quotaCountDownLock);
if (tryGrantRequest(spaceRequested))
return Decision::Grant;
m_usage = m_usageGetter();
updateQuotaBasedOnUsage();
m_quotaCountDown = m_usage < m_quota ? m_quota - m_usage : 0;
if (tryGrantRequest(spaceRequested))
return Decision::Grant;
BinarySemaphore semaphore;
callOnMainThread([this, protectedThis = makeRef(*this), spaceRequested, &semaphore]() mutable {
RELEASE_LOG(Storage, "%p - StorageQuotaManager asks for quota increase %" PRIu64, this, spaceRequested);
m_quotaIncreaseRequester(m_quota, m_usage, spaceRequested, [this, protectedThis = WTFMove(protectedThis), &semaphore](Optional<uint64_t> newQuota) mutable {
RELEASE_LOG(Storage, "%p - StorageQuotaManager receives quota increase response %" PRIu64, this, newQuota ? *newQuota : 0);
ASSERT(isMainThread());
if (newQuota)
m_quota = *newQuota;
semaphore.signal();
});
});
semaphore.wait();
m_usage = m_usageGetter();
m_quotaCountDown = m_usage < m_quota ? m_quota - m_usage : 0;
return tryGrantRequest(spaceRequested) ? Decision::Grant : Decision::Deny;
}
bool StorageQuotaManager::tryGrantRequest(uint64_t spaceRequested)
{
ASSERT(m_quotaCountDownLock.isLocked());
if (spaceRequested <= m_quotaCountDown) {
m_quotaCountDown -= spaceRequested;
return true;
}
return false;
}
void StorageQuotaManager::updateQuotaBasedOnUsage()
{
ASSERT(m_quotaCountDownLock.isLocked());
if (!m_quotaUpdatedBasedOnUsage) {
m_quotaUpdatedBasedOnUsage = true;
auto defaultQuotaStep = m_quota / 10;
m_quota = std::max(m_quota, defaultQuotaStep * ((m_usage / defaultQuotaStep) + 1));
}
}
void StorageQuotaManager::resetQuotaUpdatedBasedOnUsageForTesting()
{
LockHolder locker(m_quotaCountDownLock);
m_quota = m_initialQuota;
m_quotaCountDown = 0;
m_quotaUpdatedBasedOnUsage = false;
}
void StorageQuotaManager::resetQuotaForTesting()
{
LockHolder locker(m_quotaCountDownLock);
m_quota = m_initialQuota;
m_quotaCountDown = 0;
}
}