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
#ifndef mozilla_dom_StorageManager_h
8
#define mozilla_dom_StorageManager_h
9
10
#include "nsIDOMStorageManager.h"
11
#include "nsILocalStorageManager.h"
12
#include "StorageObserver.h"
13
14
#include "LocalStorage.h"
15
#include "LocalStorageCache.h"
16
#include "mozilla/dom/Storage.h"
17
18
#include "nsTHashtable.h"
19
#include "nsDataHashtable.h"
20
#include "nsClassHashtable.h"
21
#include "nsHashKeys.h"
22
23
namespace mozilla {
24
25
class OriginAttributesPattern;
26
27
namespace dom {
28
29
class LocalStorageManager final : public nsIDOMStorageManager,
30
public nsILocalStorageManager,
31
public StorageObserverSink {
32
NS_DECL_ISUPPORTS
33
NS_DECL_NSIDOMSTORAGEMANAGER
34
NS_DECL_NSILOCALSTORAGEMANAGER
35
36
public:
37
LocalStorageManager();
38
39
// Reads the preference for DOM storage quota
40
static uint32_t GetQuota();
41
42
// Gets (but not ensures) cache for the given scope
43
LocalStorageCache* GetCache(const nsACString& aOriginSuffix,
44
const nsACString& aOriginNoSuffix);
45
46
// Returns object keeping usage cache for the scope.
47
already_AddRefed<StorageUsage> GetOriginUsage(
48
const nsACString& aOriginNoSuffix);
49
50
static nsAutoCString CreateOrigin(const nsACString& aOriginSuffix,
51
const nsACString& aOriginNoSuffix);
52
53
private:
54
~LocalStorageManager();
55
56
// StorageObserverSink, handler to various chrome clearing notification
57
nsresult Observe(const char* aTopic,
58
const nsAString& aOriginAttributesPattern,
59
const nsACString& aOriginScope) override;
60
61
// Since nsTHashtable doesn't like multiple inheritance, we have to aggregate
62
// LocalStorageCache into the entry.
63
class LocalStorageCacheHashKey : public nsCStringHashKey {
64
public:
65
explicit LocalStorageCacheHashKey(const nsACString* aKey)
66
: nsCStringHashKey(aKey), mCache(new LocalStorageCache(aKey)) {}
67
68
LocalStorageCacheHashKey(LocalStorageCacheHashKey&& aOther)
69
: nsCStringHashKey(std::move(aOther)),
70
mCache(std::move(aOther.mCache)),
71
mCacheRef(std::move(aOther.mCacheRef)) {
72
NS_ERROR("Shouldn't be called");
73
}
74
75
LocalStorageCache* cache() { return mCache; }
76
// Keep the cache referenced forever, used for sessionStorage.
77
void HardRef() { mCacheRef = mCache; }
78
79
private:
80
// weak ref only since cache references its manager.
81
LocalStorageCache* mCache;
82
// hard ref when this is sessionStorage to keep it alive forever.
83
RefPtr<LocalStorageCache> mCacheRef;
84
};
85
86
// Ensures cache for a scope, when it doesn't exist it is created and
87
// initalized, this also starts preload of persistent data.
88
already_AddRefed<LocalStorageCache> PutCache(
89
const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix,
90
nsIPrincipal* aPrincipal);
91
92
enum class CreateMode {
93
// GetStorage: do not create if it's not already in memory.
94
UseIfExistsNeverCreate,
95
// CreateStorage: Create it if it's not already in memory.
96
CreateAlways,
97
// PrecacheStorage: Create only if the database says we ShouldPreloadOrigin.
98
CreateIfShouldPreload
99
};
100
101
// Helper for creation of DOM storage objects
102
nsresult GetStorageInternal(CreateMode aCreate, mozIDOMWindow* aWindow,
103
nsIPrincipal* aPrincipal,
104
nsIPrincipal* aStoragePrincipal,
105
const nsAString& aDocumentURI, bool aPrivate,
106
Storage** aRetval);
107
108
// Suffix->origin->cache map
109
typedef nsTHashtable<LocalStorageCacheHashKey> CacheOriginHashtable;
110
nsClassHashtable<nsCStringHashKey, CacheOriginHashtable> mCaches;
111
112
void ClearCaches(uint32_t aUnloadFlags,
113
const OriginAttributesPattern& aPattern,
114
const nsACString& aKeyPrefix);
115
116
// Global getter of localStorage manager service
117
static LocalStorageManager* Self();
118
119
// Like Self, but creates an instance if we're not yet initialized.
120
static LocalStorageManager* Ensure();
121
122
private:
123
// Keeps usage cache objects for eTLD+1 scopes we have touched.
124
nsDataHashtable<nsCStringHashKey, RefPtr<StorageUsage> > mUsages;
125
126
friend class LocalStorageCache;
127
friend class StorageDBChild;
128
// Releases cache since it is no longer referrered by any Storage object.
129
virtual void DropCache(LocalStorageCache* aCache);
130
131
static LocalStorageManager* sSelf;
132
};
133
134
} // namespace dom
135
} // namespace mozilla
136
137
#endif // mozilla_dom_StorageManager_h