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 "LocalStorageManager.h"
8
#include "LocalStorage.h"
9
#include "StorageDBThread.h"
10
#include "StorageUtils.h"
11
12
#include "nsIEffectiveTLDService.h"
13
14
#include "nsNetUtil.h"
15
#include "nsNetCID.h"
16
#include "nsPrintfCString.h"
17
#include "nsXULAppAPI.h"
18
#include "nsThreadUtils.h"
19
#include "nsIObserverService.h"
20
#include "mozilla/Services.h"
21
#include "mozilla/StaticPrefs_dom.h"
22
#include "mozilla/dom/LocalStorageCommon.h"
23
24
namespace mozilla {
25
namespace dom {
26
27
using namespace StorageUtils;
28
29
LocalStorageManager* LocalStorageManager::sSelf = nullptr;
30
31
// static
32
uint32_t LocalStorageManager::GetQuota() {
33
return StaticPrefs::dom_storage_default_quota() * 1024; // pref is in kBs
34
}
35
36
NS_IMPL_ISUPPORTS(LocalStorageManager, nsIDOMStorageManager,
37
nsILocalStorageManager)
38
39
LocalStorageManager::LocalStorageManager() : mCaches(8) {
40
MOZ_ASSERT(!NextGenLocalStorageEnabled());
41
42
StorageObserver* observer = StorageObserver::Self();
43
NS_ASSERTION(
44
observer,
45
"No StorageObserver, cannot observe private data delete notifications!");
46
47
if (observer) {
48
observer->AddSink(this);
49
}
50
51
NS_ASSERTION(!sSelf,
52
"Somebody is trying to "
53
"do_CreateInstance(\"@mozilla/dom/localStorage-manager;1\"");
54
sSelf = this;
55
56
if (!XRE_IsParentProcess()) {
57
// Do this only on the child process. The thread IPC bridge
58
// is also used to communicate chrome observer notifications.
59
// Note: must be called after we set sSelf
60
StorageDBChild::GetOrCreate();
61
}
62
}
63
64
LocalStorageManager::~LocalStorageManager() {
65
StorageObserver* observer = StorageObserver::Self();
66
if (observer) {
67
observer->RemoveSink(this);
68
}
69
70
sSelf = nullptr;
71
}
72
73
namespace {
74
75
nsresult CreateQuotaDBKey(nsIPrincipal* aPrincipal, nsACString& aKey) {
76
nsresult rv;
77
78
nsCOMPtr<nsIEffectiveTLDService> eTLDService(
79
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv));
80
NS_ENSURE_SUCCESS(rv, rv);
81
82
nsCOMPtr<nsIURI> uri;
83
rv = aPrincipal->GetURI(getter_AddRefs(uri));
84
NS_ENSURE_SUCCESS(rv, rv);
85
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
86
87
nsAutoCString eTLDplusOne;
88
rv = eTLDService->GetBaseDomain(uri, 0, eTLDplusOne);
89
if (NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS == rv) {
90
// XXX bug 357323 - what to do for localhost/file exactly?
91
rv = uri->GetAsciiHost(eTLDplusOne);
92
}
93
NS_ENSURE_SUCCESS(rv, rv);
94
95
aKey.Truncate();
96
aPrincipal->OriginAttributesRef().CreateSuffix(aKey);
97
98
nsAutoCString subdomainsDBKey;
99
CreateReversedDomain(eTLDplusOne, subdomainsDBKey);
100
101
aKey.Append(':');
102
aKey.Append(subdomainsDBKey);
103
104
return NS_OK;
105
}
106
107
} // namespace
108
109
// static
110
nsAutoCString LocalStorageManager::CreateOrigin(
111
const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix) {
112
// Note: some hard-coded sqlite statements are dependent on the format this
113
// method returns. Changing this without updating those sqlite statements
114
// will cause malfunction.
115
116
nsAutoCString scope;
117
scope.Append(aOriginSuffix);
118
scope.Append(':');
119
scope.Append(aOriginNoSuffix);
120
return scope;
121
}
122
123
LocalStorageCache* LocalStorageManager::GetCache(
124
const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix) {
125
CacheOriginHashtable* table = mCaches.LookupOrAdd(aOriginSuffix);
126
LocalStorageCacheHashKey* entry = table->GetEntry(aOriginNoSuffix);
127
if (!entry) {
128
return nullptr;
129
}
130
131
return entry->cache();
132
}
133
134
already_AddRefed<StorageUsage> LocalStorageManager::GetOriginUsage(
135
const nsACString& aOriginNoSuffix) {
136
RefPtr<StorageUsage> usage;
137
if (mUsages.Get(aOriginNoSuffix, &usage)) {
138
return usage.forget();
139
}
140
141
usage = new StorageUsage(aOriginNoSuffix);
142
143
StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
144
if (storageChild) {
145
storageChild->AsyncGetUsage(usage);
146
}
147
148
mUsages.Put(aOriginNoSuffix, usage);
149
150
return usage.forget();
151
}
152
153
already_AddRefed<LocalStorageCache> LocalStorageManager::PutCache(
154
const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix,
155
nsIPrincipal* aPrincipal) {
156
CacheOriginHashtable* table = mCaches.LookupOrAdd(aOriginSuffix);
157
LocalStorageCacheHashKey* entry = table->PutEntry(aOriginNoSuffix);
158
RefPtr<LocalStorageCache> cache = entry->cache();
159
160
nsAutoCString quotaOrigin;
161
CreateQuotaDBKey(aPrincipal, quotaOrigin);
162
163
// Lifetime handled by the cache, do persist
164
cache->Init(this, true, aPrincipal, quotaOrigin);
165
return cache.forget();
166
}
167
168
void LocalStorageManager::DropCache(LocalStorageCache* aCache) {
169
if (!NS_IsMainThread()) {
170
NS_WARNING(
171
"StorageManager::DropCache called on a non-main thread, shutting "
172
"down?");
173
}
174
175
CacheOriginHashtable* table = mCaches.LookupOrAdd(aCache->OriginSuffix());
176
table->RemoveEntry(aCache->OriginNoSuffix());
177
}
178
179
nsresult LocalStorageManager::GetStorageInternal(
180
CreateMode aCreateMode, mozIDOMWindow* aWindow, nsIPrincipal* aPrincipal,
181
nsIPrincipal* aStoragePrincipal, const nsAString& aDocumentURI,
182
bool aPrivate, Storage** aRetval) {
183
nsAutoCString originAttrSuffix;
184
nsAutoCString originKey;
185
186
nsresult rv =
187
GenerateOriginKey(aStoragePrincipal, originAttrSuffix, originKey);
188
if (NS_FAILED(rv)) {
189
return NS_ERROR_NOT_AVAILABLE;
190
}
191
192
RefPtr<LocalStorageCache> cache = GetCache(originAttrSuffix, originKey);
193
194
// Get or create a cache for the given scope
195
if (!cache) {
196
if (aCreateMode == CreateMode::UseIfExistsNeverCreate) {
197
*aRetval = nullptr;
198
return NS_OK;
199
}
200
201
if (aCreateMode == CreateMode::CreateIfShouldPreload) {
202
// This is a demand to just preload the cache, if the scope has
203
// no data stored, bypass creation and preload of the cache.
204
StorageDBChild* db = StorageDBChild::Get();
205
if (db) {
206
if (!db->ShouldPreloadOrigin(LocalStorageManager::CreateOrigin(
207
originAttrSuffix, originKey))) {
208
return NS_OK;
209
}
210
} else {
211
if (originKey.EqualsLiteral("knalb.:about")) {
212
return NS_OK;
213
}
214
}
215
}
216
217
#if !defined(MOZ_WIDGET_ANDROID)
218
PBackgroundChild* backgroundActor =
219
BackgroundChild::GetOrCreateForCurrentThread();
220
if (NS_WARN_IF(!backgroundActor)) {
221
return NS_ERROR_FAILURE;
222
}
223
224
PrincipalInfo principalInfo;
225
rv = mozilla::ipc::PrincipalToPrincipalInfo(aStoragePrincipal,
226
&principalInfo);
227
if (NS_WARN_IF(NS_FAILED(rv))) {
228
return rv;
229
}
230
231
uint32_t privateBrowsingId;
232
rv = aPrincipal->GetPrivateBrowsingId(&privateBrowsingId);
233
if (NS_WARN_IF(NS_FAILED(rv))) {
234
return rv;
235
}
236
#endif
237
238
// There is always a single instance of a cache per scope
239
// in a single instance of a DOM storage manager.
240
cache = PutCache(originAttrSuffix, originKey, aStoragePrincipal);
241
242
#if !defined(MOZ_WIDGET_ANDROID)
243
LocalStorageCacheChild* actor = new LocalStorageCacheChild(cache);
244
245
MOZ_ALWAYS_TRUE(
246
backgroundActor->SendPBackgroundLocalStorageCacheConstructor(
247
actor, principalInfo, originKey, privateBrowsingId));
248
249
cache->SetActor(actor);
250
#endif
251
}
252
253
if (aRetval) {
254
nsCOMPtr<nsPIDOMWindowInner> inner = nsPIDOMWindowInner::From(aWindow);
255
256
RefPtr<Storage> storage =
257
new LocalStorage(inner, this, cache, aDocumentURI, aPrincipal,
258
aStoragePrincipal, aPrivate);
259
storage.forget(aRetval);
260
}
261
262
return NS_OK;
263
}
264
265
NS_IMETHODIMP
266
LocalStorageManager::PrecacheStorage(nsIPrincipal* aPrincipal,
267
nsIPrincipal* aStoragePrincipal,
268
Storage** aRetval) {
269
return GetStorageInternal(CreateMode::CreateIfShouldPreload, nullptr,
270
aPrincipal, aStoragePrincipal, EmptyString(), false,
271
aRetval);
272
}
273
274
NS_IMETHODIMP
275
LocalStorageManager::CreateStorage(mozIDOMWindow* aWindow,
276
nsIPrincipal* aPrincipal,
277
nsIPrincipal* aStoragePrincipal,
278
const nsAString& aDocumentURI, bool aPrivate,
279
Storage** aRetval) {
280
return GetStorageInternal(CreateMode::CreateAlways, aWindow, aPrincipal,
281
aStoragePrincipal, aDocumentURI, aPrivate, aRetval);
282
}
283
284
NS_IMETHODIMP
285
LocalStorageManager::GetStorage(mozIDOMWindow* aWindow,
286
nsIPrincipal* aPrincipal,
287
nsIPrincipal* aStoragePrincipal, bool aPrivate,
288
Storage** aRetval) {
289
return GetStorageInternal(CreateMode::UseIfExistsNeverCreate, aWindow,
290
aPrincipal, aStoragePrincipal, EmptyString(),
291
aPrivate, aRetval);
292
}
293
294
NS_IMETHODIMP
295
LocalStorageManager::CloneStorage(Storage* aStorage) {
296
// Cloning is supported only for sessionStorage
297
return NS_ERROR_NOT_IMPLEMENTED;
298
}
299
300
NS_IMETHODIMP
301
LocalStorageManager::CheckStorage(nsIPrincipal* aPrincipal, Storage* aStorage,
302
bool* aRetval) {
303
MOZ_ASSERT(NS_IsMainThread());
304
MOZ_ASSERT(aPrincipal);
305
MOZ_ASSERT(aStorage);
306
MOZ_ASSERT(aRetval);
307
308
// Only used by sessionStorage.
309
return NS_ERROR_NOT_IMPLEMENTED;
310
}
311
312
NS_IMETHODIMP
313
LocalStorageManager::GetNextGenLocalStorageEnabled(bool* aResult) {
314
MOZ_ASSERT(NS_IsMainThread());
315
MOZ_ASSERT(aResult);
316
317
*aResult = NextGenLocalStorageEnabled();
318
return NS_OK;
319
}
320
321
NS_IMETHODIMP
322
LocalStorageManager::Preload(nsIPrincipal* aPrincipal, JSContext* aContext,
323
nsISupports** _retval) {
324
MOZ_ASSERT(NS_IsMainThread());
325
MOZ_ASSERT(aPrincipal);
326
MOZ_ASSERT(_retval);
327
328
return NS_ERROR_NOT_IMPLEMENTED;
329
}
330
331
NS_IMETHODIMP
332
LocalStorageManager::IsPreloaded(nsIPrincipal* aPrincipal, JSContext* aContext,
333
nsISupports** _retval) {
334
MOZ_ASSERT(NS_IsMainThread());
335
MOZ_ASSERT(aPrincipal);
336
MOZ_ASSERT(_retval);
337
338
return NS_ERROR_NOT_IMPLEMENTED;
339
}
340
341
void LocalStorageManager::ClearCaches(uint32_t aUnloadFlags,
342
const OriginAttributesPattern& aPattern,
343
const nsACString& aOriginScope) {
344
for (auto iter1 = mCaches.Iter(); !iter1.Done(); iter1.Next()) {
345
OriginAttributes oa;
346
DebugOnly<bool> rv = oa.PopulateFromSuffix(iter1.Key());
347
MOZ_ASSERT(rv);
348
if (!aPattern.Matches(oa)) {
349
// This table doesn't match the given origin attributes pattern
350
continue;
351
}
352
353
CacheOriginHashtable* table = iter1.Data();
354
355
for (auto iter2 = table->Iter(); !iter2.Done(); iter2.Next()) {
356
LocalStorageCache* cache = iter2.Get()->cache();
357
358
if (aOriginScope.IsEmpty() ||
359
StringBeginsWith(cache->OriginNoSuffix(), aOriginScope)) {
360
cache->UnloadItems(aUnloadFlags);
361
}
362
}
363
}
364
}
365
366
nsresult LocalStorageManager::Observe(const char* aTopic,
367
const nsAString& aOriginAttributesPattern,
368
const nsACString& aOriginScope) {
369
OriginAttributesPattern pattern;
370
if (!pattern.Init(aOriginAttributesPattern)) {
371
NS_ERROR("Cannot parse origin attributes pattern");
372
return NS_ERROR_FAILURE;
373
}
374
375
// Clear everything, caches + database
376
if (!strcmp(aTopic, "cookie-cleared")) {
377
ClearCaches(LocalStorageCache::kUnloadComplete, pattern, EmptyCString());
378
return NS_OK;
379
}
380
381
// Clear everything, caches + database
382
if (!strcmp(aTopic, "extension:purge-localStorage-caches")) {
383
ClearCaches(LocalStorageCache::kUnloadComplete, pattern, aOriginScope);
384
return NS_OK;
385
}
386
387
if (!strcmp(aTopic, "browser:purge-sessionStorage")) {
388
// This is only meant for SessionStorageManager.
389
return NS_OK;
390
}
391
392
// Clear from caches everything that has been stored
393
// while in session-only mode
394
if (!strcmp(aTopic, "session-only-cleared")) {
395
ClearCaches(LocalStorageCache::kUnloadSession, pattern, aOriginScope);
396
return NS_OK;
397
}
398
399
// Clear all private-browsing caches
400
if (!strcmp(aTopic, "private-browsing-data-cleared")) {
401
ClearCaches(LocalStorageCache::kUnloadPrivate, pattern, EmptyCString());
402
return NS_OK;
403
}
404
405
// Clear localStorage data beloging to an origin pattern
406
if (!strcmp(aTopic, "origin-attr-pattern-cleared")) {
407
ClearCaches(LocalStorageCache::kUnloadComplete, pattern, EmptyCString());
408
return NS_OK;
409
}
410
411
if (!strcmp(aTopic, "profile-change")) {
412
// For case caches are still referenced - clear them completely
413
ClearCaches(LocalStorageCache::kUnloadComplete, pattern, EmptyCString());
414
mCaches.Clear();
415
return NS_OK;
416
}
417
418
#ifdef DOM_STORAGE_TESTS
419
if (!strcmp(aTopic, "test-reload")) {
420
// This immediately completely reloads all caches from the database.
421
ClearCaches(LocalStorageCache::kTestReload, pattern, EmptyCString());
422
return NS_OK;
423
}
424
425
if (!strcmp(aTopic, "test-flushed")) {
426
if (!XRE_IsParentProcess()) {
427
nsCOMPtr<nsIObserverService> obs =
428
mozilla::services::GetObserverService();
429
if (obs) {
430
obs->NotifyObservers(nullptr, "domstorage-test-flushed", nullptr);
431
}
432
}
433
434
return NS_OK;
435
}
436
#endif
437
438
NS_ERROR("Unexpected topic");
439
return NS_ERROR_UNEXPECTED;
440
}
441
442
// static
443
LocalStorageManager* LocalStorageManager::Self() {
444
MOZ_ASSERT(!NextGenLocalStorageEnabled());
445
446
return sSelf;
447
}
448
449
LocalStorageManager* LocalStorageManager::Ensure() {
450
MOZ_ASSERT(!NextGenLocalStorageEnabled());
451
452
if (sSelf) {
453
return sSelf;
454
}
455
456
// Cause sSelf to be populated.
457
nsCOMPtr<nsIDOMStorageManager> initializer =
458
do_GetService("@mozilla.org/dom/localStorage-manager;1");
459
MOZ_ASSERT(sSelf, "Didn't initialize?");
460
461
return sSelf;
462
}
463
464
} // namespace dom
465
} // namespace mozilla