Bug 1682536 - Use references/NotNull for OriginInfo. r=dom-workers-and-storage-reviewers,janv

Differential Revision: https://phabricator.services.mozilla.com/D99778
This commit is contained in:
Simon Giesecke 2021-01-19 16:32:38 +00:00
Родитель e672832603
Коммит 93348cd207
1 изменённых файлов: 151 добавлений и 149 удалений

Просмотреть файл

@ -944,16 +944,14 @@ class OriginInfo final {
class OriginInfoLRUComparator {
public:
bool Equals(const OriginInfo* a, const OriginInfo* b) const {
return a && b ? a->LockedAccessTime() == b->LockedAccessTime()
: !a && !b ? true
: false;
bool Equals(const NotNull<RefPtr<OriginInfo>>& a,
const NotNull<RefPtr<OriginInfo>>& b) const {
return a->LockedAccessTime() == b->LockedAccessTime();
}
bool LessThan(const OriginInfo* a, const OriginInfo* b) const {
return a && b ? a->LockedAccessTime() < b->LockedAccessTime()
: b ? true
: false;
bool LessThan(const NotNull<RefPtr<OriginInfo>>& a,
const NotNull<RefPtr<OriginInfo>>& b) const {
return a->LockedAccessTime() < b->LockedAccessTime();
}
};
@ -985,7 +983,7 @@ class GroupInfo final {
already_AddRefed<OriginInfo> LockedGetOriginInfo(const nsACString& aOrigin);
void LockedAddOriginInfo(OriginInfo* aOriginInfo);
void LockedAddOriginInfo(NotNull<RefPtr<OriginInfo>>&& aOriginInfo);
void LockedAdjustUsageForRemovedOriginInfo(const OriginInfo& aOriginInfo);
@ -999,7 +997,7 @@ class GroupInfo final {
return !mOriginInfos.IsEmpty();
}
nsTArray<RefPtr<OriginInfo>> mOriginInfos;
nsTArray<NotNull<RefPtr<OriginInfo>>> mOriginInfos;
GroupInfoPair* mGroupInfoPair;
PersistenceType mPersistenceType;
@ -3739,10 +3737,10 @@ uint64_t QuotaManager::CollectOriginsForEviction(
struct MOZ_STACK_CLASS Helper final {
static void GetInactiveOriginInfos(
nsTArray<RefPtr<OriginInfo>>& aOriginInfos,
nsTArray<DirectoryLockImpl*>& aLocks,
nsTArray<OriginInfo*>& aInactiveOriginInfos) {
for (OriginInfo* originInfo : aOriginInfos) {
const nsTArray<NotNull<RefPtr<OriginInfo>>>& aOriginInfos,
const nsTArray<DirectoryLockImpl*>& aLocks,
nsTArray<NotNull<RefPtr<OriginInfo>>>& aInactiveOriginInfos) {
for (const NotNull<RefPtr<OriginInfo>>& originInfo : aOriginInfos) {
MOZ_ASSERT(originInfo->mGroupInfo->mPersistenceType !=
PERSISTENCE_TYPE_PERSISTENT);
@ -3790,7 +3788,7 @@ uint64_t QuotaManager::CollectOriginsForEviction(
}
}
nsTArray<OriginInfo*> inactiveOrigins;
nsTArray<NotNull<RefPtr<OriginInfo>>> inactiveOrigins;
// Enumerate and process inactive origins. This must be protected by the
// mutex.
@ -3843,7 +3841,7 @@ uint64_t QuotaManager::CollectOriginsForEviction(
// Success, add directory locks for these origins, so any other
// operations for them will be delayed (until origin eviction is finalized).
for (OriginInfo* originInfo : inactiveOrigins) {
for (const auto& originInfo : inactiveOrigins) {
RefPtr<DirectoryLockImpl> lock = CreateDirectoryLockForEviction(
originInfo->mGroupInfo->mPersistenceType,
GroupAndOrigin{originInfo->mGroupInfo->mGroup, originInfo->mOrigin});
@ -3869,7 +3867,7 @@ void QuotaManager::CollectPendingOriginsForListing(P aPredicate) {
RefPtr<GroupInfo> groupInfo =
pair->LockedGetGroupInfo(PERSISTENCE_TYPE_DEFAULT);
if (groupInfo) {
for (RefPtr<OriginInfo>& originInfo : groupInfo->mOriginInfos) {
for (const auto& originInfo : groupInfo->mOriginInfos) {
if (!originInfo->mDirectoryExists) {
aPredicate(originInfo);
}
@ -4151,11 +4149,10 @@ void QuotaManager::InitQuotaForOrigin(PersistenceType aPersistenceType,
RefPtr<GroupInfo> groupInfo =
LockedGetOrCreateGroupInfo(aPersistenceType, aGroupAndOrigin.mGroup);
RefPtr<OriginInfo> originInfo =
new OriginInfo(groupInfo, aGroupAndOrigin.mOrigin, aClientUsages,
aUsageBytes, aAccessTime, aPersisted,
/* aDirectoryExists */ true);
groupInfo->LockedAddOriginInfo(originInfo);
groupInfo->LockedAddOriginInfo(MakeNotNull<RefPtr<OriginInfo>>(
groupInfo, aGroupAndOrigin.mOrigin, aClientUsages, aUsageBytes,
aAccessTime, aPersisted,
/* aDirectoryExists */ true));
}
void QuotaManager::EnsureQuotaForOrigin(PersistenceType aPersistenceType,
@ -4171,12 +4168,11 @@ void QuotaManager::EnsureQuotaForOrigin(PersistenceType aPersistenceType,
RefPtr<OriginInfo> originInfo =
groupInfo->LockedGetOriginInfo(aGroupAndOrigin.mOrigin);
if (!originInfo) {
originInfo =
new OriginInfo(groupInfo, aGroupAndOrigin.mOrigin, ClientUsageArray(),
/* aUsageBytes */ 0,
/* aAccessTime */ PR_Now(), /* aPersisted */ false,
/* aDirectoryExists */ false);
groupInfo->LockedAddOriginInfo(originInfo);
groupInfo->LockedAddOriginInfo(MakeNotNull<RefPtr<OriginInfo>>(
groupInfo, aGroupAndOrigin.mOrigin, ClientUsageArray(),
/* aUsageBytes */ 0,
/* aAccessTime */ PR_Now(), /* aPersisted */ false,
/* aDirectoryExists */ false));
}
}
@ -4201,11 +4197,10 @@ void QuotaManager::NoteOriginDirectoryCreated(
timestamp = originInfo->LockedAccessTime();
} else {
timestamp = PR_Now();
RefPtr<OriginInfo> originInfo = new OriginInfo(
groupInfo->LockedAddOriginInfo(MakeNotNull<RefPtr<OriginInfo>>(
groupInfo, aGroupAndOrigin.mOrigin, ClientUsageArray(),
/* aUsageBytes */ 0,
/* aAccessTime */ timestamp, aPersisted, /* aDirectoryExists */ true);
groupInfo->LockedAddOriginInfo(originInfo);
/* aAccessTime */ timestamp, aPersisted, /* aDirectoryExists */ true));
}
aTimestamp = timestamp;
@ -4586,7 +4581,7 @@ void QuotaManager::UnloadQuota() {
continue;
}
for (RefPtr<OriginInfo>& originInfo : groupInfo->mOriginInfos) {
for (const auto& originInfo : groupInfo->mOriginInfos) {
MOZ_ASSERT(!originInfo->mQuotaObjects.Count());
if (!originInfo->mDirectoryExists) {
@ -7085,69 +7080,10 @@ already_AddRefed<OriginInfo> QuotaManager::LockedGetOriginInfo(
void QuotaManager::CheckTemporaryStorageLimits() {
AssertIsOnIOThread();
nsTArray<OriginInfo*> doomedOriginInfos;
{
MutexAutoLock lock(mQuotaMutex);
for (const auto& entry : mGroupInfoPairs) {
const auto& pair = entry.GetData();
MOZ_ASSERT(!entry.GetKey().IsEmpty());
MOZ_ASSERT(pair);
uint64_t groupUsage = 0;
RefPtr<GroupInfo> temporaryGroupInfo =
pair->LockedGetGroupInfo(PERSISTENCE_TYPE_TEMPORARY);
if (temporaryGroupInfo) {
groupUsage += temporaryGroupInfo->mUsage;
}
RefPtr<GroupInfo> defaultGroupInfo =
pair->LockedGetGroupInfo(PERSISTENCE_TYPE_DEFAULT);
if (defaultGroupInfo) {
groupUsage += defaultGroupInfo->mUsage;
}
if (groupUsage > 0) {
QuotaManager* quotaManager = QuotaManager::Get();
MOZ_ASSERT(quotaManager, "Shouldn't be null!");
if (groupUsage > quotaManager->GetGroupLimit()) {
nsTArray<OriginInfo*> originInfos;
if (temporaryGroupInfo) {
originInfos.AppendElements(temporaryGroupInfo->mOriginInfos);
}
if (defaultGroupInfo) {
originInfos.AppendElements(defaultGroupInfo->mOriginInfos);
}
originInfos.Sort(OriginInfoLRUComparator());
for (uint32_t i = 0; i < originInfos.Length(); i++) {
OriginInfo* originInfo = originInfos[i];
if (originInfo->LockedPersisted()) {
continue;
}
doomedOriginInfos.AppendElement(originInfo);
groupUsage -= originInfo->LockedUsage();
if (groupUsage <= quotaManager->GetGroupLimit()) {
break;
}
}
}
}
}
uint64_t usage = std::accumulate(
doomedOriginInfos.cbegin(), doomedOriginInfos.cend(), uint64_t(0),
[](uint64_t oldValue, const auto& originInfo) {
return oldValue + originInfo->LockedUsage();
});
if (mTemporaryStorageUsage - usage > mTemporaryStorageLimit) {
nsTArray<OriginInfo*> originInfos;
const auto doomedOrigins = [this] {
const auto doomedOriginInfos = [this] {
nsTArray<NotNull<RefPtr<OriginInfo>>> doomedOriginInfos;
MutexAutoLock lock(mQuotaMutex);
for (const auto& entry : mGroupInfoPairs) {
const auto& pair = entry.GetData();
@ -7155,66 +7091,131 @@ void QuotaManager::CheckTemporaryStorageLimits() {
MOZ_ASSERT(!entry.GetKey().IsEmpty());
MOZ_ASSERT(pair);
RefPtr<GroupInfo> groupInfo =
uint64_t groupUsage = 0;
RefPtr<GroupInfo> temporaryGroupInfo =
pair->LockedGetGroupInfo(PERSISTENCE_TYPE_TEMPORARY);
if (groupInfo) {
originInfos.AppendElements(groupInfo->mOriginInfos);
if (temporaryGroupInfo) {
groupUsage += temporaryGroupInfo->mUsage;
}
groupInfo = pair->LockedGetGroupInfo(PERSISTENCE_TYPE_DEFAULT);
if (groupInfo) {
originInfos.AppendElements(groupInfo->mOriginInfos);
RefPtr<GroupInfo> defaultGroupInfo =
pair->LockedGetGroupInfo(PERSISTENCE_TYPE_DEFAULT);
if (defaultGroupInfo) {
groupUsage += defaultGroupInfo->mUsage;
}
if (groupUsage > 0) {
QuotaManager* quotaManager = QuotaManager::Get();
MOZ_ASSERT(quotaManager, "Shouldn't be null!");
if (groupUsage > quotaManager->GetGroupLimit()) {
nsTArray<NotNull<RefPtr<OriginInfo>>> originInfos;
if (temporaryGroupInfo) {
originInfos.AppendElements(temporaryGroupInfo->mOriginInfos);
}
if (defaultGroupInfo) {
originInfos.AppendElements(defaultGroupInfo->mOriginInfos);
}
originInfos.Sort(OriginInfoLRUComparator());
for (uint32_t i = 0; i < originInfos.Length(); i++) {
const NotNull<RefPtr<OriginInfo>>& originInfo = originInfos[i];
if (originInfo->LockedPersisted()) {
continue;
}
doomedOriginInfos.AppendElement(originInfo);
groupUsage -= originInfo->LockedUsage();
if (groupUsage <= quotaManager->GetGroupLimit()) {
break;
}
}
}
}
}
originInfos.RemoveElementsBy(
[&doomedOriginInfos](const auto& originInfo) {
return doomedOriginInfos.Contains(originInfo) ||
originInfo->LockedPersisted();
uint64_t usage = std::accumulate(
doomedOriginInfos.cbegin(), doomedOriginInfos.cend(), uint64_t(0),
[](uint64_t oldValue, const auto& originInfo) {
return oldValue + originInfo->LockedUsage();
});
originInfos.Sort(OriginInfoLRUComparator());
if (mTemporaryStorageUsage - usage > mTemporaryStorageLimit) {
nsTArray<NotNull<RefPtr<OriginInfo>>> originInfos;
for (uint32_t i = 0; i < originInfos.Length(); i++) {
if (mTemporaryStorageUsage - usage <= mTemporaryStorageLimit) {
originInfos.TruncateLength(i);
break;
for (const auto& entry : mGroupInfoPairs) {
const auto& pair = entry.GetData();
MOZ_ASSERT(!entry.GetKey().IsEmpty());
MOZ_ASSERT(pair);
RefPtr<GroupInfo> groupInfo =
pair->LockedGetGroupInfo(PERSISTENCE_TYPE_TEMPORARY);
if (groupInfo) {
originInfos.AppendElements(groupInfo->mOriginInfos);
}
groupInfo = pair->LockedGetGroupInfo(PERSISTENCE_TYPE_DEFAULT);
if (groupInfo) {
originInfos.AppendElements(groupInfo->mOriginInfos);
}
}
usage += originInfos[i]->LockedUsage();
originInfos.RemoveElementsBy(
[&doomedOriginInfos](const auto& originInfo) {
return doomedOriginInfos.Contains(originInfo) ||
originInfo->LockedPersisted();
});
originInfos.Sort(OriginInfoLRUComparator());
for (uint32_t i = 0; i < originInfos.Length(); i++) {
if (mTemporaryStorageUsage - usage <= mTemporaryStorageLimit) {
originInfos.TruncateLength(i);
break;
}
usage += originInfos[i]->LockedUsage();
}
doomedOriginInfos.AppendElements(originInfos);
}
doomedOriginInfos.AppendElements(originInfos);
}
}
return doomedOriginInfos;
}();
for (const OriginInfo* const doomedOriginInfo : doomedOriginInfos) {
for (const auto& doomedOriginInfo : doomedOriginInfos) {
#ifdef DEBUG
{
MutexAutoLock lock(mQuotaMutex);
MOZ_ASSERT(!doomedOriginInfo->LockedPersisted());
}
{
MutexAutoLock lock(mQuotaMutex);
MOZ_ASSERT(!doomedOriginInfo->LockedPersisted());
}
#endif
DeleteFilesForOrigin(doomedOriginInfo->mGroupInfo->mPersistenceType,
doomedOriginInfo->mOrigin);
}
nsTArray<OriginParams> doomedOrigins;
{
MutexAutoLock lock(mQuotaMutex);
for (const OriginInfo* const doomedOriginInfo : doomedOriginInfos) {
PersistenceType persistenceType =
doomedOriginInfo->mGroupInfo->mPersistenceType;
const GroupAndOrigin groupAndOrigin = {
doomedOriginInfo->mGroupInfo->mGroup, doomedOriginInfo->mOrigin};
LockedRemoveQuotaForOrigin(persistenceType, groupAndOrigin);
doomedOrigins.EmplaceBack(
OriginParams(persistenceType, groupAndOrigin.mOrigin));
DeleteFilesForOrigin(doomedOriginInfo->mGroupInfo->mPersistenceType,
doomedOriginInfo->mOrigin);
}
}
nsTArray<OriginParams> doomedOrigins;
{
MutexAutoLock lock(mQuotaMutex);
for (const auto& doomedOriginInfo : doomedOriginInfos) {
PersistenceType persistenceType =
doomedOriginInfo->mGroupInfo->mPersistenceType;
const GroupAndOrigin groupAndOrigin = {
doomedOriginInfo->mGroupInfo->mGroup, doomedOriginInfo->mOrigin};
LockedRemoveQuotaForOrigin(persistenceType, groupAndOrigin);
doomedOrigins.EmplaceBack(
OriginParams(persistenceType, groupAndOrigin.mOrigin));
}
}
return doomedOrigins;
}();
for (const OriginParams& doomedOrigin : doomedOrigins) {
OriginClearCompleted(doomedOrigin.mPersistenceType, doomedOrigin.mOrigin,
@ -7485,7 +7486,7 @@ already_AddRefed<OriginInfo> GroupInfo::LockedGetOriginInfo(
const nsACString& aOrigin) {
AssertCurrentThreadOwnsQuotaMutex();
for (RefPtr<OriginInfo>& originInfo : mOriginInfos) {
for (const auto& originInfo : mOriginInfos) {
if (originInfo->mOrigin == aOrigin) {
RefPtr<OriginInfo> result = originInfo;
return result.forget();
@ -7495,12 +7496,12 @@ already_AddRefed<OriginInfo> GroupInfo::LockedGetOriginInfo(
return nullptr;
}
void GroupInfo::LockedAddOriginInfo(OriginInfo* aOriginInfo) {
void GroupInfo::LockedAddOriginInfo(NotNull<RefPtr<OriginInfo>>&& aOriginInfo) {
AssertCurrentThreadOwnsQuotaMutex();
NS_ASSERTION(!mOriginInfos.Contains(aOriginInfo),
"Replacing an existing entry!");
mOriginInfos.AppendElement(aOriginInfo);
mOriginInfos.AppendElement(std::move(aOriginInfo));
uint64_t usage = aOriginInfo->LockedUsage();
@ -7551,7 +7552,7 @@ void GroupInfo::LockedRemoveOriginInfo(const nsACString& aOrigin) {
void GroupInfo::LockedRemoveOriginInfos() {
AssertCurrentThreadOwnsQuotaMutex();
for (const OriginInfo* const originInfo : std::exchange(mOriginInfos, {})) {
for (const auto& originInfo : std::exchange(mOriginInfos, {})) {
LockedAdjustUsageForRemovedOriginInfo(*originInfo);
}
}
@ -8711,12 +8712,13 @@ nsresult GetUsageOp::DoDirectoryWork(QuotaManager& aQuotaManager) {
// LocalStorage writes for an origin which didn't previously have any
// LocalStorage data.
aQuotaManager.CollectPendingOriginsForListing([&](OriginInfo* aOriginInfo) {
ProcessOriginInternal(
&aQuotaManager, aOriginInfo->GetGroupInfo()->GetPersistenceType(),
aOriginInfo->Origin(), aOriginInfo->LockedAccessTime(),
aOriginInfo->LockedPersisted(), aOriginInfo->LockedUsage());
});
aQuotaManager.CollectPendingOriginsForListing(
[this, &aQuotaManager](const auto& originInfo) {
ProcessOriginInternal(
&aQuotaManager, originInfo->GetGroupInfo()->GetPersistenceType(),
originInfo->Origin(), originInfo->LockedAccessTime(),
originInfo->LockedPersisted(), originInfo->LockedUsage());
});
return NS_OK;
}
@ -9670,8 +9672,8 @@ nsresult ListOriginsOp::DoDirectoryWork(QuotaManager& aQuotaManager) {
// known origins, but we also need to include origins that have pending quota
// usage.
aQuotaManager.CollectPendingOriginsForListing([&](OriginInfo* aOriginInfo) {
mOrigins.AppendElement(aOriginInfo->Origin());
aQuotaManager.CollectPendingOriginsForListing([this](const auto& originInfo) {
mOrigins.AppendElement(originInfo->Origin());
});
return NS_OK;