Bug 1361974 - Add nsIDOMWindowUtils API to check usage of a given storage r=mayhemer

This commit is contained in:
Tim Taubert 2017-05-04 14:29:36 +02:00
Родитель c49dee9e16
Коммит 1eae0fcd88
8 изменённых файлов: 95 добавлений и 0 удалений

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

@ -4249,6 +4249,19 @@ nsDOMWindowUtils::RemoveManuallyManagedState(nsIDOMElement* aElement,
return NS_OK;
}
NS_IMETHODIMP
nsDOMWindowUtils::GetStorageUsage(nsIDOMStorage* aStorage, int64_t* aRetval)
{
RefPtr<Storage> storage = static_cast<Storage*>(aStorage);
if (!storage) {
return NS_ERROR_UNEXPECTED;
}
*aRetval = storage->GetOriginQuotaUsage();
return NS_OK;
}
NS_INTERFACE_MAP_BEGIN(nsTranslationNodeList)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(nsITranslationNodeList)

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

@ -48,6 +48,7 @@ interface nsITranslationNodeList;
interface nsIJSRAIIHelper;
interface nsIContentPermissionRequest;
interface nsIObserver;
interface nsIDOMStorage;
[scriptable, uuid(c471d440-004b-4c50-a6f2-747db5f443b6)]
interface nsIDOMWindowUtils : nsISupports {
@ -2006,6 +2007,14 @@ interface nsIDOMWindowUtils : nsISupports {
void removeManuallyManagedState(in nsIDOMElement element,
in AString state);
/**
* Returns usage data for a given storage object.
*
* @param aStorage
* The storage object to get usage data for.
*/
int64_t getStorageUsage(in nsIDOMStorage aStorage);
// These consts are only for testing purposes.
const long DEFAULT_MOUSE_POINTER_ID = 0;
const long DEFAULT_PEN_POINTER_ID = 1;

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

@ -72,6 +72,12 @@ Storage::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
return StorageBinding::Wrap(aCx, this, aGivenProto);
}
int64_t
Storage::GetOriginQuotaUsage() const
{
return mCache->GetOriginQuotaUsage(this);
}
uint32_t
Storage::GetLength(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv)

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

@ -73,6 +73,8 @@ public:
return mWindow;
}
int64_t GetOriginQuotaUsage() const;
uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv);

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

@ -540,6 +540,12 @@ StorageCache::CloneFrom(const StorageCache* aThat)
}
}
int64_t
StorageCache::GetOriginQuotaUsage(const Storage* aStorage) const
{
return mData[GetDataSetIndex(aStorage)].mOriginQuotaUsage;
}
// Defined in StorageManager.cpp
extern bool
PrincipalsEqual(nsIPrincipal* aObjectPrincipal,

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

@ -109,6 +109,9 @@ public:
// Copies all data from the other storage.
void CloneFrom(const StorageCache* aThat);
// Get size of per-origin data.
int64_t GetOriginQuotaUsage(const Storage* aStorage) const;
// Starts async preload of this cache if it persistent and not loaded.
void Preload();

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

@ -16,3 +16,4 @@ skip-if = toolkit == 'android'
[test_sessionStorageHttpHttps.html]
skip-if = toolkit == 'android' #TIMED_OUT
[test_sessionStorageReplace.html]
[test_sessionStorageUsage.html]

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

@ -0,0 +1,55 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>sessionStorage basic test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript">
function setup() {
sessionStorage.clear();
SimpleTest.executeSoon(startTest);
}
function startTest()
{
var util = SpecialPowers.wrap(window)
.QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
.getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
// Check initial state.
is(sessionStorage.length, 0, "storage is empty");
is(util.getStorageUsage(sessionStorage), 0, "usage is zero");
// Add some data.
sessionStorage.setItem("one", "data");
var usage = util.getStorageUsage(sessionStorage);
ok(usage > 0, "storage contains data");
// Add some more data.
sessionStorage.setItem("two", "data");
ok(usage < util.getStorageUsage(sessionStorage), "storage size grew");
// Remove data.
sessionStorage.removeItem("two");
is(util.getStorageUsage(sessionStorage), usage, "storage size shrunk");
// Cleanup.
sessionStorage.clear();
is(sessionStorage.length, 0, "storage is empty");
is(util.getStorageUsage(sessionStorage), 0, "usage is zero");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
</script>
</head>
<body onload="setup();">
</body>
</html>