-
Notifications
You must be signed in to change notification settings - Fork 54
/
SessionStorageCache.cpp
145 lines (115 loc) · 4.44 KB
/
SessionStorageCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SessionStorageCache.h"
namespace mozilla {
namespace dom {
SessionStorageCache::SessionStorageCache() = default;
SessionStorageCache::DataSet* SessionStorageCache::Set(
DataSetType aDataSetType) {
if (aDataSetType == eDefaultSetType) {
return &mDefaultSet;
}
MOZ_ASSERT(aDataSetType == eSessionSetType);
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(DataSetType aDataSetType, uint32_t aIndex,
nsAString& aResult) {
aResult.SetIsVoid(true);
for (auto iter = Set(aDataSetType)->mKeys.Iter(); !iter.Done(); iter.Next()) {
if (aIndex == 0) {
aResult = iter.Key();
return;
}
aIndex--;
}
}
void 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 (!Set(aDataSetType)->mKeys.Get(aKey, &value)) {
SetDOMStringToNull(value);
}
aResult = value;
}
void SessionStorageCache::GetKeys(DataSetType aDataSetType,
nsTArray<nsString>& aKeys) {
for (auto iter = Set(aDataSetType)->mKeys.Iter(); !iter.Done(); iter.Next()) {
aKeys.AppendElement(iter.Key());
}
}
nsresult SessionStorageCache::SetItem(DataSetType aDataSetType,
const nsAString& aKey,
const nsAString& aValue,
nsString& aOldValue) {
int64_t delta = 0;
DataSet* dataSet = Set(aDataSetType);
if (!dataSet->mKeys.Get(aKey, &aOldValue)) {
SetDOMStringToNull(aOldValue);
// We only consider key size if the key doesn't exist before.
delta = static_cast<int64_t>(aKey.Length());
}
delta += static_cast<int64_t>(aValue.Length()) -
static_cast<int64_t>(aOldValue.Length());
if (aValue == aOldValue &&
DOMStringIsNull(aValue) == DOMStringIsNull(aOldValue)) {
return NS_SUCCESS_DOM_NO_OPERATION;
}
if (!dataSet->ProcessUsageDelta(delta)) {
return NS_ERROR_DOM_QUOTA_EXCEEDED_ERR;
}
dataSet->mKeys.Put(aKey, nsString(aValue));
return NS_OK;
}
nsresult SessionStorageCache::RemoveItem(DataSetType aDataSetType,
const nsAString& aKey,
nsString& aOldValue) {
DataSet* dataSet = Set(aDataSetType);
if (!dataSet->mKeys.Get(aKey, &aOldValue)) {
return NS_SUCCESS_DOM_NO_OPERATION;
}
// Recalculate the cached data size
dataSet->ProcessUsageDelta(-(static_cast<int64_t>(aOldValue.Length()) +
static_cast<int64_t>(aKey.Length())));
dataSet->mKeys.Remove(aKey);
return NS_OK;
}
void SessionStorageCache::Clear(DataSetType aDataSetType,
bool aByUserInteraction) {
DataSet* dataSet = Set(aDataSetType);
dataSet->ProcessUsageDelta(-dataSet->mOriginQuotaUsage);
dataSet->mKeys.Clear();
}
already_AddRefed<SessionStorageCache> SessionStorageCache::Clone() const {
RefPtr<SessionStorageCache> cache = new SessionStorageCache();
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::DataSet::ProcessUsageDelta(int64_t aDelta) {
// Check limit per this origin
uint64_t newOriginUsage = mOriginQuotaUsage + aDelta;
if (aDelta > 0 && newOriginUsage > LocalStorageManager::GetQuota()) {
return false;
}
// Update size in our data set
mOriginQuotaUsage = newOriginUsage;
return true;
}
} // namespace dom
} // namespace mozilla