Bug 1322316 - Split SessionStorage and LocalStorage implementation - part 11 - SessionStorageCache must have 2 DataSet: default and sessionOnly, r=asuth
This commit is contained in:
@@ -10,14 +10,49 @@ namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
SessionStorageCache::SessionStorageCache()
|
||||
: mOriginQuotaUsage(0)
|
||||
: mSessionDataSetActive(false)
|
||||
{}
|
||||
|
||||
SessionStorageCache::DataSet*
|
||||
SessionStorageCache::Set(DataSetType aDataSetType)
|
||||
{
|
||||
if (aDataSetType == eDefaultSetType) {
|
||||
return &mDefaultSet;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(aDataSetType == eSessionSetType);
|
||||
|
||||
if (!mSessionDataSetActive) {
|
||||
mSessionSet.mOriginQuotaUsage = mDefaultSet.mOriginQuotaUsage;
|
||||
|
||||
for (auto iter = mDefaultSet.mKeys.ConstIter(); !iter.Done(); iter.Next()) {
|
||||
mSessionSet.mKeys.Put(iter.Key(), iter.Data());
|
||||
}
|
||||
|
||||
mSessionDataSetActive = true;
|
||||
}
|
||||
|
||||
return &mSessionSet;
|
||||
}
|
||||
|
||||
int64_t
|
||||
SessionStorageCache::GetOriginQuotaUsage(DataSetType aDataSetType)
|
||||
{
|
||||
return Set(aDataSetType)->mOriginQuotaUsage;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
SessionStorageCache::Length(DataSetType aDataSetType)
|
||||
{
|
||||
return Set(aDataSetType)->mKeys.Count();
|
||||
}
|
||||
|
||||
void
|
||||
SessionStorageCache::Key(uint32_t aIndex, nsAString& aResult)
|
||||
SessionStorageCache::Key(DataSetType aDataSetType, uint32_t aIndex,
|
||||
nsAString& aResult)
|
||||
{
|
||||
aResult.SetIsVoid(true);
|
||||
for (auto iter = mKeys.Iter(); !iter.Done(); iter.Next()) {
|
||||
for (auto iter = Set(aDataSetType)->mKeys.Iter(); !iter.Done(); iter.Next()) {
|
||||
if (aIndex == 0) {
|
||||
aResult = iter.Key();
|
||||
return;
|
||||
@@ -27,31 +62,33 @@ SessionStorageCache::Key(uint32_t aIndex, nsAString& aResult)
|
||||
}
|
||||
|
||||
void
|
||||
SessionStorageCache::GetItem(const nsAString& aKey, nsAString& aResult)
|
||||
SessionStorageCache::GetItem(DataSetType aDataSetType, const nsAString& aKey,
|
||||
nsAString& aResult)
|
||||
{
|
||||
// not using AutoString since we don't want to copy buffer to result
|
||||
nsString value;
|
||||
if (!mKeys.Get(aKey, &value)) {
|
||||
if (!Set(aDataSetType)->mKeys.Get(aKey, &value)) {
|
||||
SetDOMStringToNull(value);
|
||||
}
|
||||
aResult = value;
|
||||
}
|
||||
|
||||
void
|
||||
SessionStorageCache::GetKeys(nsTArray<nsString>& aKeys)
|
||||
SessionStorageCache::GetKeys(DataSetType aDataSetType, nsTArray<nsString>& aKeys)
|
||||
{
|
||||
for (auto iter = mKeys.Iter(); !iter.Done(); iter.Next()) {
|
||||
for (auto iter = Set(aDataSetType)->mKeys.Iter(); !iter.Done(); iter.Next()) {
|
||||
aKeys.AppendElement(iter.Key());
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
SessionStorageCache::SetItem(const nsAString& aKey, const nsAString& aValue,
|
||||
nsString& aOldValue)
|
||||
SessionStorageCache::SetItem(DataSetType aDataSetType, const nsAString& aKey,
|
||||
const nsAString& aValue, nsString& aOldValue)
|
||||
{
|
||||
int64_t delta = 0;
|
||||
DataSet* dataSet = Set(aDataSetType);
|
||||
|
||||
if (!mKeys.Get(aKey, &aOldValue)) {
|
||||
if (!dataSet->mKeys.Get(aKey, &aOldValue)) {
|
||||
SetDOMStringToNull(aOldValue);
|
||||
|
||||
// We only consider key size if the key doesn't exist before.
|
||||
@@ -66,38 +103,66 @@ SessionStorageCache::SetItem(const nsAString& aKey, const nsAString& aValue,
|
||||
return NS_SUCCESS_DOM_NO_OPERATION;
|
||||
}
|
||||
|
||||
if (!ProcessUsageDelta(delta)) {
|
||||
if (!dataSet->ProcessUsageDelta(delta)) {
|
||||
return NS_ERROR_DOM_QUOTA_REACHED;
|
||||
}
|
||||
|
||||
mKeys.Put(aKey, nsString(aValue));
|
||||
dataSet->mKeys.Put(aKey, nsString(aValue));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
SessionStorageCache::RemoveItem(const nsAString& aKey, nsString& aOldValue)
|
||||
SessionStorageCache::RemoveItem(DataSetType aDataSetType, const nsAString& aKey,
|
||||
nsString& aOldValue)
|
||||
{
|
||||
if (!mKeys.Get(aKey, &aOldValue)) {
|
||||
DataSet* dataSet = Set(aDataSetType);
|
||||
|
||||
if (!dataSet->mKeys.Get(aKey, &aOldValue)) {
|
||||
return NS_SUCCESS_DOM_NO_OPERATION;
|
||||
}
|
||||
|
||||
// Recalculate the cached data size
|
||||
ProcessUsageDelta(-(static_cast<int64_t>(aOldValue.Length()) +
|
||||
static_cast<int64_t>(aKey.Length())));
|
||||
dataSet->ProcessUsageDelta(-(static_cast<int64_t>(aOldValue.Length()) +
|
||||
static_cast<int64_t>(aKey.Length())));
|
||||
|
||||
mKeys.Remove(aKey);
|
||||
dataSet->mKeys.Remove(aKey);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
SessionStorageCache::Clear()
|
||||
SessionStorageCache::Clear(DataSetType aDataSetType, bool aByUserInteraction)
|
||||
{
|
||||
ProcessUsageDelta(-mOriginQuotaUsage);
|
||||
mKeys.Clear();
|
||||
DataSet* dataSet = Set(aDataSetType);
|
||||
dataSet->ProcessUsageDelta(-dataSet->mOriginQuotaUsage);
|
||||
dataSet->mKeys.Clear();
|
||||
|
||||
if (!aByUserInteraction && aDataSetType == eSessionSetType) {
|
||||
mSessionDataSetActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
already_AddRefed<SessionStorageCache>
|
||||
SessionStorageCache::Clone() const
|
||||
{
|
||||
RefPtr<SessionStorageCache> cache = new SessionStorageCache();
|
||||
|
||||
cache->mSessionDataSetActive = mSessionDataSetActive;
|
||||
|
||||
cache->mDefaultSet.mOriginQuotaUsage = mDefaultSet.mOriginQuotaUsage;
|
||||
for (auto iter = mDefaultSet.mKeys.ConstIter(); !iter.Done(); iter.Next()) {
|
||||
cache->mDefaultSet.mKeys.Put(iter.Key(), iter.Data());
|
||||
}
|
||||
|
||||
cache->mSessionSet.mOriginQuotaUsage = mSessionSet.mOriginQuotaUsage;
|
||||
for (auto iter = mSessionSet.mKeys.ConstIter(); !iter.Done(); iter.Next()) {
|
||||
cache->mSessionSet.mKeys.Put(iter.Key(), iter.Data());
|
||||
}
|
||||
|
||||
return cache.forget();
|
||||
}
|
||||
|
||||
bool
|
||||
SessionStorageCache::ProcessUsageDelta(int64_t aDelta)
|
||||
SessionStorageCache::DataSet::ProcessUsageDelta(int64_t aDelta)
|
||||
{
|
||||
// Check limit per this origin
|
||||
uint64_t newOriginUsage = mOriginQuotaUsage + aDelta;
|
||||
@@ -110,18 +175,5 @@ SessionStorageCache::ProcessUsageDelta(int64_t aDelta)
|
||||
return true;
|
||||
}
|
||||
|
||||
already_AddRefed<SessionStorageCache>
|
||||
SessionStorageCache::Clone() const
|
||||
{
|
||||
RefPtr<SessionStorageCache> cache = new SessionStorageCache();
|
||||
cache->mOriginQuotaUsage = mOriginQuotaUsage;
|
||||
|
||||
for (auto iter = mKeys.ConstIter(); !iter.Done(); iter.Next()) {
|
||||
cache->mKeys.Put(iter.Key(), iter.Data());
|
||||
}
|
||||
|
||||
return cache.forget();
|
||||
}
|
||||
|
||||
} // dom namespace
|
||||
} // mozilla namespace
|
||||
|
||||
Reference in New Issue
Block a user