From 15704220eee2fd723d2daf4cdd580831703b88d6 Mon Sep 17 00:00:00 2001 From: Tom Tung Date: Thu, 14 Jan 2021 16:01:17 +0000 Subject: [PATCH] Bug 1683299 - Introduce site quota for legacy LocalStorage; r=dom-workers-and-storage-reviewers,janv This patch: - adds a new pref for site quota. - sets 25 MB as the default site qutoa. - renames LocalStorageManager::GetQuota() to LocalStorageManager::GetOriginQuota(). - adds LocalStorageManager::GetSiteQuota(). - updates LocalStorage quota tests. Differential Revision: https://phabricator.services.mozilla.com/D101756 --- dom/localstorage/test/unit/test_groupLimit.js | 1 + dom/storage/LocalStorageCache.cpp | 4 ++-- dom/storage/LocalStorageManager.cpp | 9 ++++++++- dom/storage/LocalStorageManager.h | 5 ++++- dom/storage/SessionStorageCache.cpp | 2 +- .../mochitest/localstorage/test_localStorageQuota.html | 2 +- ...est_localStorageQuotaPrivateBrowsing_perwindowpb.html | 3 +++ .../localstorage/test_localStorageQuotaSessionOnly.html | 2 +- .../localstorage/test_localStorageQuotaSessionOnly2.html | 2 +- modules/libpref/init/StaticPrefList.yaml | 6 ++++++ 10 files changed, 28 insertions(+), 8 deletions(-) diff --git a/dom/localstorage/test/unit/test_groupLimit.js b/dom/localstorage/test/unit/test_groupLimit.js index cdc102318bd9..6036c24717a1 100644 --- a/dom/localstorage/test/unit/test_groupLimit.js +++ b/dom/localstorage/test/unit/test_groupLimit.js @@ -25,6 +25,7 @@ async function testSteps() { info("Setting pref"); + Services.prefs.setBoolPref("dom.storage.next_gen", true); Services.prefs.setBoolPref("dom.storage.snapshot_reusing", false); info("Setting limits"); diff --git a/dom/storage/LocalStorageCache.cpp b/dom/storage/LocalStorageCache.cpp index 44423755dc17..a06564a92c58 100644 --- a/dom/storage/LocalStorageCache.cpp +++ b/dom/storage/LocalStorageCache.cpp @@ -194,7 +194,7 @@ bool LocalStorageCache::ProcessUsageDelta(uint32_t aGetDataSetIndex, Data& data = mData[aGetDataSetIndex]; uint64_t newOriginUsage = data.mOriginQuotaUsage + aDelta; if (aSource == ContentMutation && aDelta > 0 && - newOriginUsage > LocalStorageManager::GetQuota()) { + newOriginUsage > LocalStorageManager::GetOriginQuota()) { return false; } @@ -609,7 +609,7 @@ bool StorageUsage::CheckAndSetETLD1UsageDelta( int64_t newUsage = mUsage[aDataSetIndex] + aDelta; if (aSource == LocalStorageCache::ContentMutation && aDelta > 0 && - newUsage > LocalStorageManager::GetQuota()) { + newUsage > LocalStorageManager::GetSiteQuota()) { return false; } diff --git a/dom/storage/LocalStorageManager.cpp b/dom/storage/LocalStorageManager.cpp index c475aedced28..1a9f566c42d7 100644 --- a/dom/storage/LocalStorageManager.cpp +++ b/dom/storage/LocalStorageManager.cpp @@ -33,10 +33,17 @@ using namespace StorageUtils; LocalStorageManager* LocalStorageManager::sSelf = nullptr; // static -uint32_t LocalStorageManager::GetQuota() { +uint32_t LocalStorageManager::GetOriginQuota() { return StaticPrefs::dom_storage_default_quota() * 1024; // pref is in kBs } +// static +uint32_t LocalStorageManager::GetSiteQuota() { + return std::max(StaticPrefs::dom_storage_default_quota(), + StaticPrefs::dom_storage_default_site_quota()) * + 1024; // pref is in kBs +} + NS_IMPL_ISUPPORTS(LocalStorageManager, nsIDOMStorageManager, nsILocalStorageManager) diff --git a/dom/storage/LocalStorageManager.h b/dom/storage/LocalStorageManager.h index f4cb09643a70..2ebd3681aca6 100644 --- a/dom/storage/LocalStorageManager.h +++ b/dom/storage/LocalStorageManager.h @@ -37,7 +37,10 @@ class LocalStorageManager final : public nsIDOMStorageManager, LocalStorageManager(); // Reads the preference for DOM storage quota - static uint32_t GetQuota(); + static uint32_t GetOriginQuota(); + + // Reads the preference for DOM storage site quota + static uint32_t GetSiteQuota(); // Gets (but not ensures) cache for the given scope LocalStorageCache* GetCache(const nsACString& aOriginSuffix, diff --git a/dom/storage/SessionStorageCache.cpp b/dom/storage/SessionStorageCache.cpp index 373f448396b8..a0f355b508e6 100644 --- a/dom/storage/SessionStorageCache.cpp +++ b/dom/storage/SessionStorageCache.cpp @@ -327,7 +327,7 @@ void SessionStorageCache::ClearActor() { bool SessionStorageCache::DataSet::ProcessUsageDelta(int64_t aDelta) { // Check limit per this origin uint64_t newOriginUsage = mOriginQuotaUsage + aDelta; - if (aDelta > 0 && newOriginUsage > LocalStorageManager::GetQuota()) { + if (aDelta > 0 && newOriginUsage > LocalStorageManager::GetOriginQuota()) { return false; } diff --git a/dom/tests/mochitest/localstorage/test_localStorageQuota.html b/dom/tests/mochitest/localstorage/test_localStorageQuota.html index 960e2ccc6da1..fdd0cb6cad03 100644 --- a/dom/tests/mochitest/localstorage/test_localStorageQuota.html +++ b/dom/tests/mochitest/localstorage/test_localStorageQuota.html @@ -117,7 +117,7 @@ SimpleTest.waitForExplicitFinish(); function startTest() { // Initialy setup the quota to testing value of 1024B and // set a 500 bytes key with name length 1 (allocate 501 bytes) - SpecialPowers.pushPrefEnv({"set": [["dom.storage.default_quota", 1], ["security.mixed_content.block_display_content", false], ["security.mixed_content.block_active_content", false]]}, doNextTest); + SpecialPowers.pushPrefEnv({"set": [["dom.storage.default_quota", 1], ["dom.storage.default_site_quota", 1], ["security.mixed_content.block_display_content", false], ["security.mixed_content.block_active_content", false]]}, doNextTest); } diff --git a/dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html b/dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html index d7b4ea43c77e..71981ffd721d 100644 --- a/dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html +++ b/dom/tests/mochitest/localstorage/test_localStorageQuotaPrivateBrowsing_perwindowpb.html @@ -14,8 +14,10 @@ const CONTENT_PAGE = "http://mochi.test:8888/chrome/dom/tests/mochitest/localsto const slavePath = "/chrome/dom/tests/mochitest/localstorage/"; var currentTest = 1; var quota = Services.prefs.getIntPref("dom.storage.default_quota", 5 * 1024); +var siteQuota = Services.prefs.getIntPref("dom.storage.default_site_quota", 25 * 1024); Services.prefs.setIntPref("browser.startup.page", 0); Services.prefs.setIntPref("dom.storage.default_quota", 1); +Services.prefs.setIntPref("dom.storage.default_site_quota", 1); var slaveLoadsPending = 1; var slaveOrigin = ""; @@ -128,6 +130,7 @@ function doNextTest(aWindow) { default: Services.prefs.clearUserPref("browser.startup.page") Services.prefs.setIntPref("dom.storage.default_quota", quota); + Services.prefs.setIntPref("dom.storage.default_site_quota", siteQuota); aWindow.close(); SimpleTest.finish(); } diff --git a/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly.html b/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly.html index 5229ccc919ab..1d32776907a1 100644 --- a/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly.html +++ b/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly.html @@ -121,7 +121,7 @@ function startTest() { ], function() { // Initialy setup the quota to testing value of 1024B and // set a 500 bytes key with name length 1 (allocate 501 bytes) - SpecialPowers.pushPrefEnv({"set": [["dom.storage.default_quota", 1], ["security.mixed_content.block_display_content", false], ["security.mixed_content.block_active_content", false]]}, doNextTest); + SpecialPowers.pushPrefEnv({"set": [["dom.storage.default_quota", 1], ["dom.storage.default_site_quota", 1], ["security.mixed_content.block_display_content", false], ["security.mixed_content.block_active_content", false]]}, doNextTest); }); } diff --git a/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly2.html b/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly2.html index 1f9fe71a1c7d..156ff02bf66a 100644 --- a/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly2.html +++ b/dom/tests/mochitest/localstorage/test_localStorageQuotaSessionOnly2.html @@ -84,7 +84,7 @@ SimpleTest.waitForExplicitFinish(); function startTest() { // Initialy setup the quota to testing value of 1024B and // set a 500 bytes key with name length 1 (allocate 501 bytes) - SpecialPowers.pushPrefEnv({"set": [["dom.storage.default_quota", 1], ["security.mixed_content.block_display_content", false], ["security.mixed_content.block_active_content", false]]}, function() { + SpecialPowers.pushPrefEnv({"set": [["dom.storage.default_quota", 1], ["dom.storage.default_site_quota", 1], ["security.mixed_content.block_display_content", false], ["security.mixed_content.block_active_content", false]]}, function() { SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], doNextTest); }); } diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index d399fe5147e5..54693e085d97 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -2906,6 +2906,12 @@ value: 5 * 1024 mirror: always +# Per-site quota for legacy LocalStorage implementation. +- name: dom.storage.default_site_quota + type: RelaxedAtomicUint32 + value: 25 * 1024 + mirror: always + # Whether or not LSNG (Next Generation Local Storage) is enabled. # See bug 1517090 for enabling this on Nightly. # See bug 1534736 for changing it to EARLY_BETA_OR_EARLIER.