Source code

Revision control

Other Tools

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