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