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 "SessionStorageManager.h"
8
9
#include "mozilla/dom/ContentChild.h"
10
#include "SessionStorage.h"
11
#include "SessionStorageCache.h"
12
#include "SessionStorageObserver.h"
13
#include "StorageUtils.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
using namespace StorageUtils;
19
20
NS_IMPL_ISUPPORTS(SessionStorageManager, nsIDOMStorageManager,
21
nsIDOMSessionStorageManager)
22
23
SessionStorageManager::SessionStorageManager() {
24
StorageObserver* observer = StorageObserver::Self();
25
NS_ASSERTION(
26
observer,
27
"No StorageObserver, cannot observe private data delete notifications!");
28
29
if (observer) {
30
observer->AddSink(this);
31
}
32
33
if (!XRE_IsParentProcess() && NextGenLocalStorageEnabled()) {
34
// When LSNG is enabled the thread IPC bridge doesn't exist, so we have to
35
// create own protocol to distribute chrome observer notifications to
36
// content processes.
37
mObserver = SessionStorageObserver::Get();
38
39
if (!mObserver) {
40
ContentChild* contentActor = ContentChild::GetSingleton();
41
MOZ_ASSERT(contentActor);
42
43
RefPtr<SessionStorageObserver> observer = new SessionStorageObserver();
44
45
SessionStorageObserverChild* actor =
46
new SessionStorageObserverChild(observer);
47
48
MOZ_ALWAYS_TRUE(
49
contentActor->SendPSessionStorageObserverConstructor(actor));
50
51
observer->SetActor(actor);
52
53
mObserver = std::move(observer);
54
}
55
}
56
}
57
58
SessionStorageManager::~SessionStorageManager() {
59
StorageObserver* observer = StorageObserver::Self();
60
if (observer) {
61
observer->RemoveSink(this);
62
}
63
}
64
65
NS_IMETHODIMP
66
SessionStorageManager::PrecacheStorage(nsIPrincipal* aPrincipal,
67
nsIPrincipal* aStoragePrincipal,
68
Storage** aRetval) {
69
// Nothing to preload.
70
return NS_OK;
71
}
72
73
NS_IMETHODIMP
74
SessionStorageManager::GetSessionStorageCache(
75
nsIPrincipal* aPrincipal, nsIPrincipal* aStoragePrincipal,
76
RefPtr<SessionStorageCache>* aRetVal) {
77
return GetSessionStorageCacheHelper(aPrincipal, aStoragePrincipal, true,
78
nullptr, aRetVal);
79
}
80
81
nsresult SessionStorageManager::GetSessionStorageCacheHelper(
82
nsIPrincipal* aPrincipal, nsIPrincipal* aStoragePrincipal,
83
bool aMakeIfNeeded, SessionStorageCache* aCloneFrom,
84
RefPtr<SessionStorageCache>* aRetVal) {
85
*aRetVal = nullptr;
86
87
nsAutoCString originKey;
88
nsAutoCString originAttributes;
89
nsresult rv = GenerateOriginKey(aPrincipal, originAttributes, originKey);
90
if (NS_FAILED(rv)) {
91
return NS_ERROR_NOT_AVAILABLE;
92
}
93
94
OriginKeyHashTable* table;
95
if (!mOATable.Get(originAttributes, &table)) {
96
if (aMakeIfNeeded) {
97
table = new OriginKeyHashTable();
98
mOATable.Put(originAttributes, table);
99
} else {
100
return NS_OK;
101
}
102
}
103
104
RefPtr<SessionStorageCache> cache;
105
if (!table->Get(originKey, getter_AddRefs(cache))) {
106
if (aMakeIfNeeded) {
107
if (aCloneFrom) {
108
cache = aCloneFrom->Clone();
109
} else {
110
cache = new SessionStorageCache();
111
}
112
table->Put(originKey, cache);
113
} else {
114
return NS_OK;
115
}
116
}
117
118
*aRetVal = std::move(cache);
119
120
return NS_OK;
121
}
122
123
NS_IMETHODIMP
124
SessionStorageManager::CreateStorage(mozIDOMWindow* aWindow,
125
nsIPrincipal* aPrincipal,
126
nsIPrincipal* aStoragePrincipal,
127
const nsAString& aDocumentURI,
128
bool aPrivate, Storage** aRetval) {
129
RefPtr<SessionStorageCache> cache;
130
nsresult rv = GetSessionStorageCache(aPrincipal, aStoragePrincipal, &cache);
131
if (NS_FAILED(rv)) {
132
return rv;
133
}
134
135
nsCOMPtr<nsPIDOMWindowInner> inner = nsPIDOMWindowInner::From(aWindow);
136
137
// No StoragePrincipal for sessionStorage.
138
RefPtr<SessionStorage> storage = new SessionStorage(
139
inner, aPrincipal, cache, this, aDocumentURI, aPrivate);
140
141
storage.forget(aRetval);
142
return NS_OK;
143
}
144
145
NS_IMETHODIMP
146
SessionStorageManager::GetStorage(mozIDOMWindow* aWindow,
147
nsIPrincipal* aPrincipal,
148
nsIPrincipal* aStoragePrincipal,
149
bool aPrivate, Storage** aRetval) {
150
*aRetval = nullptr;
151
152
RefPtr<SessionStorageCache> cache;
153
nsresult rv = GetSessionStorageCacheHelper(aPrincipal, aStoragePrincipal,
154
false, nullptr, &cache);
155
if (NS_FAILED(rv) || !cache) {
156
return rv;
157
}
158
159
nsCOMPtr<nsPIDOMWindowInner> inner = nsPIDOMWindowInner::From(aWindow);
160
161
RefPtr<SessionStorage> storage = new SessionStorage(
162
inner, aPrincipal, cache, this, EmptyString(), aPrivate);
163
164
storage.forget(aRetval);
165
return NS_OK;
166
}
167
168
NS_IMETHODIMP
169
SessionStorageManager::CloneStorage(Storage* aStorage) {
170
if (NS_WARN_IF(!aStorage)) {
171
return NS_ERROR_UNEXPECTED;
172
}
173
174
if (aStorage->Type() != Storage::eSessionStorage) {
175
return NS_ERROR_UNEXPECTED;
176
}
177
178
RefPtr<SessionStorageCache> cache;
179
return GetSessionStorageCacheHelper(
180
aStorage->Principal(), aStorage->StoragePrincipal(), true,
181
static_cast<SessionStorage*>(aStorage)->Cache(), &cache);
182
}
183
184
NS_IMETHODIMP
185
SessionStorageManager::CheckStorage(nsIPrincipal* aPrincipal, Storage* aStorage,
186
bool* aRetval) {
187
if (NS_WARN_IF(!aStorage)) {
188
return NS_ERROR_UNEXPECTED;
189
}
190
191
if (!aPrincipal) {
192
return NS_ERROR_NOT_AVAILABLE;
193
}
194
195
*aRetval = false;
196
197
RefPtr<SessionStorageCache> cache;
198
nsresult rv = GetSessionStorageCacheHelper(
199
aPrincipal, aStorage->StoragePrincipal(), false, nullptr, &cache);
200
if (NS_FAILED(rv) || !cache) {
201
return rv;
202
}
203
204
if (aStorage->Type() != Storage::eSessionStorage) {
205
return NS_OK;
206
}
207
208
RefPtr<SessionStorage> sessionStorage =
209
static_cast<SessionStorage*>(aStorage);
210
if (sessionStorage->Cache() != cache) {
211
return NS_OK;
212
}
213
214
if (!StorageUtils::PrincipalsEqual(aStorage->Principal(), aPrincipal)) {
215
return NS_OK;
216
}
217
218
*aRetval = true;
219
return NS_OK;
220
}
221
222
void SessionStorageManager::ClearStorages(
223
ClearStorageType aType, const OriginAttributesPattern& aPattern,
224
const nsACString& aOriginScope) {
225
for (auto iter1 = mOATable.Iter(); !iter1.Done(); iter1.Next()) {
226
OriginAttributes oa;
227
DebugOnly<bool> ok = oa.PopulateFromSuffix(iter1.Key());
228
MOZ_ASSERT(ok);
229
if (!aPattern.Matches(oa)) {
230
// This table doesn't match the given origin attributes pattern
231
continue;
232
}
233
234
OriginKeyHashTable* table = iter1.Data();
235
for (auto iter2 = table->Iter(); !iter2.Done(); iter2.Next()) {
236
if (aOriginScope.IsEmpty() ||
237
StringBeginsWith(iter2.Key(), aOriginScope)) {
238
if (aType == eAll) {
239
iter2.Data()->Clear(SessionStorageCache::eDefaultSetType, false);
240
iter2.Data()->Clear(SessionStorageCache::eSessionSetType, false);
241
} else {
242
MOZ_ASSERT(aType == eSessionOnly);
243
iter2.Data()->Clear(SessionStorageCache::eSessionSetType, false);
244
}
245
}
246
}
247
}
248
}
249
250
nsresult SessionStorageManager::Observe(
251
const char* aTopic, const nsAString& aOriginAttributesPattern,
252
const nsACString& aOriginScope) {
253
OriginAttributesPattern pattern;
254
if (!pattern.Init(aOriginAttributesPattern)) {
255
NS_ERROR("Cannot parse origin attributes pattern");
256
return NS_ERROR_FAILURE;
257
}
258
259
// Clear everything, caches + database
260
if (!strcmp(aTopic, "cookie-cleared")) {
261
ClearStorages(eAll, pattern, EmptyCString());
262
return NS_OK;
263
}
264
265
// Clear from caches everything that has been stored
266
// while in session-only mode
267
if (!strcmp(aTopic, "session-only-cleared")) {
268
ClearStorages(eSessionOnly, pattern, aOriginScope);
269
return NS_OK;
270
}
271
272
// Clear everything (including so and pb data) from caches and database
273
// for the given domain and subdomains.
274
if (!strcmp(aTopic, "browser:purge-sessionStorage")) {
275
ClearStorages(eAll, pattern, aOriginScope);
276
return NS_OK;
277
}
278
279
if (!strcmp(aTopic, "profile-change")) {
280
// For case caches are still referenced - clear them completely
281
ClearStorages(eAll, pattern, EmptyCString());
282
mOATable.Clear();
283
return NS_OK;
284
}
285
286
return NS_OK;
287
}
288
289
} // namespace dom
290
} // namespace mozilla