diff --git a/dom/localstorage/ActorsChild.cpp b/dom/localstorage/ActorsChild.cpp index 852bfa8d2019..926711a53ab4 100644 --- a/dom/localstorage/ActorsChild.cpp +++ b/dom/localstorage/ActorsChild.cpp @@ -80,7 +80,9 @@ LSDatabaseChild::RecvRequestAllowToClose() PBackgroundLSSnapshotChild* LSDatabaseChild::AllocPBackgroundLSSnapshotChild(const nsString& aDocumentURI, + const bool& aIncreasePeakUsage, const int64_t& aRequestedSize, + const int64_t& aMinSize, LSSnapshotInitInfo* aInitInfo) { MOZ_CRASH("PBackgroundLSSnapshotChild actor should be manually constructed!"); diff --git a/dom/localstorage/ActorsChild.h b/dom/localstorage/ActorsChild.h index ab4e43cea1ce..700e61d94f37 100644 --- a/dom/localstorage/ActorsChild.h +++ b/dom/localstorage/ActorsChild.h @@ -68,7 +68,9 @@ private: PBackgroundLSSnapshotChild* AllocPBackgroundLSSnapshotChild(const nsString& aDocumentURI, + const bool& aIncreasePeakUsage, const int64_t& aRequestedSize, + const int64_t& aMinSize, LSSnapshotInitInfo* aInitInfo) override; bool diff --git a/dom/localstorage/ActorsParent.cpp b/dom/localstorage/ActorsParent.cpp index 42f5c4a59ced..57b3dfa17a4d 100644 --- a/dom/localstorage/ActorsParent.cpp +++ b/dom/localstorage/ActorsParent.cpp @@ -1390,8 +1390,7 @@ public: NoteInactiveDatabase(Database* aDatabase); void - GetSnapshotInitInfo(int64_t aRequestedSize, - nsTHashtable& aLoadedItems, + GetSnapshotInitInfo(nsTHashtable& aLoadedItems, nsTArray& aItemInfos, uint32_t& aTotalLength, int64_t& aInitialUsage, @@ -1430,8 +1429,9 @@ public: void EndUpdateBatch(int64_t aSnapshotPeakUsage); - bool - UpdateUsage(int64_t aDelta); + int64_t + RequestUpdateUsage(int64_t aRequestedSize, + int64_t aMinSize); NS_INLINE_DECL_REFCOUNTING(Datastore) @@ -1439,6 +1439,9 @@ private: // Reference counted. ~Datastore(); + bool + UpdateUsage(int64_t aDelta); + void MaybeClose(); @@ -1651,13 +1654,17 @@ private: PBackgroundLSSnapshotParent* AllocPBackgroundLSSnapshotParent(const nsString& aDocumentURI, + const bool& aIncreasePeakUsage, const int64_t& aRequestedSize, + const int64_t& aMinSize, LSSnapshotInitInfo* aInitInfo) override; mozilla::ipc::IPCResult RecvPBackgroundLSSnapshotConstructor(PBackgroundLSSnapshotParent* aActor, const nsString& aDocumentURI, + const bool& aIncreasePeakUsage, const int64_t& aRequestedSize, + const int64_t& aMinSize, LSSnapshotInitInfo* aInitInfo) override; bool @@ -3757,8 +3764,7 @@ Datastore::NoteInactiveDatabase(Database* aDatabase) } void -Datastore::GetSnapshotInitInfo(int64_t aRequestedSize, - nsTHashtable& aLoadedItems, +Datastore::GetSnapshotInitInfo(nsTHashtable& aLoadedItems, nsTArray& aItemInfos, uint32_t& aTotalLength, int64_t& aInitialUsage, @@ -3832,9 +3838,6 @@ Datastore::GetSnapshotInitInfo(int64_t aRequestedSize, aInitialUsage = mUsage; aPeakUsage = aInitialUsage; - if (aRequestedSize && UpdateUsage(aRequestedSize)) { - aPeakUsage += aRequestedSize; - } } void @@ -4078,6 +4081,25 @@ Datastore::EndUpdateBatch(int64_t aSnapshotPeakUsage) #endif } +int64_t +Datastore::RequestUpdateUsage(int64_t aRequestedSize, + int64_t aMinSize) +{ + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aRequestedSize > 0); + MOZ_ASSERT(aMinSize > 0); + + if (UpdateUsage(aRequestedSize)) { + return aRequestedSize; + } + + if (UpdateUsage(aMinSize)) { + return aMinSize; + } + + return 0; +} + bool Datastore::UpdateUsage(int64_t aDelta) { @@ -4411,12 +4433,19 @@ Database::RecvAllowToClose() PBackgroundLSSnapshotParent* Database::AllocPBackgroundLSSnapshotParent(const nsString& aDocumentURI, + const bool& aIncreasePeakUsage, const int64_t& aRequestedSize, + const int64_t& aMinSize, LSSnapshotInitInfo* aInitInfo) { AssertIsOnBackgroundThread(); - if (NS_WARN_IF(aRequestedSize < 0)) { + if (NS_WARN_IF(aIncreasePeakUsage && aRequestedSize <= 0)) { + ASSERT_UNLESS_FUZZING(); + return nullptr; + } + + if (NS_WARN_IF(aIncreasePeakUsage && aMinSize <= 0)) { ASSERT_UNLESS_FUZZING(); return nullptr; } @@ -4436,11 +4465,14 @@ mozilla::ipc::IPCResult Database::RecvPBackgroundLSSnapshotConstructor( PBackgroundLSSnapshotParent* aActor, const nsString& aDocumentURI, + const bool& aIncreasePeakUsage, const int64_t& aRequestedSize, + const int64_t& aMinSize, LSSnapshotInitInfo* aInitInfo) { AssertIsOnBackgroundThread(); - MOZ_ASSERT(aRequestedSize >= 0); + MOZ_ASSERT_IF(aIncreasePeakUsage, aRequestedSize > 0); + MOZ_ASSERT_IF(aIncreasePeakUsage, aMinSize > 0); MOZ_ASSERT(aInitInfo); MOZ_ASSERT(!mAllowedToClose); @@ -4454,14 +4486,18 @@ Database::RecvPBackgroundLSSnapshotConstructor( int64_t initialUsage; int64_t peakUsage; LSSnapshot::LoadState loadState; - mDatastore->GetSnapshotInitInfo(aRequestedSize, - loadedItems, + mDatastore->GetSnapshotInitInfo(loadedItems, itemInfos, totalLength, initialUsage, peakUsage, loadState); + if (aIncreasePeakUsage) { + int64_t size = mDatastore->RequestUpdateUsage(aRequestedSize, aMinSize); + peakUsage += size; + } + snapshot->Init(loadedItems, totalLength, initialUsage, peakUsage, loadState); RegisterSnapshot(snapshot); @@ -4781,15 +4817,11 @@ Snapshot::RecvIncreasePeakUsage(const int64_t& aRequestedSize, return IPC_FAIL_NO_REASON(this); } - if (mDatastore->UpdateUsage(aRequestedSize)) { - mPeakUsage += aRequestedSize; - *aSize = aRequestedSize; - } else if (mDatastore->UpdateUsage(aMinSize)) { - mPeakUsage += aMinSize; - *aSize = aMinSize; - } else { - *aSize = 0; - } + int64_t size = mDatastore->RequestUpdateUsage(aRequestedSize, aMinSize); + + mPeakUsage += size; + + *aSize = size; return IPC_OK(); } diff --git a/dom/localstorage/LSDatabase.cpp b/dom/localstorage/LSDatabase.cpp index 6ce932607964..e643f8ef18f7 100644 --- a/dom/localstorage/LSDatabase.cpp +++ b/dom/localstorage/LSDatabase.cpp @@ -101,7 +101,7 @@ LSDatabase::GetLength(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ false); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -124,7 +124,7 @@ LSDatabase::GetKey(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ false); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -147,7 +147,7 @@ LSDatabase::GetItem(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ false); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -169,7 +169,7 @@ LSDatabase::GetKeys(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ false); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -193,7 +193,7 @@ LSDatabase::SetItem(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ true); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -216,7 +216,7 @@ LSDatabase::RemoveItem(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ false); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -238,7 +238,7 @@ LSDatabase::Clear(LSObject* aObject, MOZ_ASSERT(mActor); MOZ_ASSERT(!mAllowedToClose); - nsresult rv = EnsureSnapshot(aObject, /* aRequestedBySetItem */ false); + nsresult rv = EnsureSnapshot(aObject); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -263,9 +263,7 @@ LSDatabase::BeginExplicitSnapshot(LSObject* aObject) return NS_ERROR_ALREADY_INITIALIZED; } - nsresult rv = EnsureSnapshot(aObject, - /* aRequestedBySetItem */ false, - /* aExplicit */ true); + nsresult rv = EnsureSnapshot(aObject, /* aExplicit */ true); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -297,7 +295,6 @@ LSDatabase::EndExplicitSnapshot(LSObject* aObject) nsresult LSDatabase::EnsureSnapshot(LSObject* aObject, - bool aRequestedBySetItem, bool aExplicit) { MOZ_ASSERT(aObject); @@ -313,13 +310,13 @@ LSDatabase::EnsureSnapshot(LSObject* aObject, LSSnapshotChild* actor = new LSSnapshotChild(snapshot); - int64_t requestedSize = aRequestedBySetItem ? 4096 : 0; - LSSnapshotInitInfo initInfo; bool ok = mActor->SendPBackgroundLSSnapshotConstructor(actor, aObject->DocumentURI(), - requestedSize, + /* increasePeakUsage */ true, + /* requestedSize */ 131072, + /* minSize */ 4096, &initInfo); if (NS_WARN_IF(!ok)) { return NS_ERROR_FAILURE; diff --git a/dom/localstorage/LSDatabase.h b/dom/localstorage/LSDatabase.h index 2854cb70814c..ec7435b789fc 100644 --- a/dom/localstorage/LSDatabase.h +++ b/dom/localstorage/LSDatabase.h @@ -108,7 +108,6 @@ private: nsresult EnsureSnapshot(LSObject* aObject, - bool aRequestedBySetItem, bool aExplicit = false); void diff --git a/dom/localstorage/PBackgroundLSDatabase.ipdl b/dom/localstorage/PBackgroundLSDatabase.ipdl index e802597e41d4..843ea3bd91aa 100644 --- a/dom/localstorage/PBackgroundLSDatabase.ipdl +++ b/dom/localstorage/PBackgroundLSDatabase.ipdl @@ -48,7 +48,9 @@ parent: async AllowToClose(); sync PBackgroundLSSnapshot(nsString documentURI, - int64_t requestedSize) + bool increasePeakUsage, + int64_t requestedSize, + int64_t minSize) returns (LSSnapshotInitInfo initInfo); child: