1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsDOMStorageManager_h__
7 #define nsDOMStorageManager_h__
9 #include "nsIDOMStorageManager.h"
10 #include "DOMStorageObserver.h"
12 #include "DOMStorageCache.h"
13 #include "mozilla/dom/DOMStorage.h"
15 #include "nsTHashtable.h"
16 #include "nsDataHashtable.h"
17 #include "nsHashKeys.h"
24 const DOMStorage::StorageType SessionStorage
= DOMStorage::SessionStorage
;
25 const DOMStorage::StorageType LocalStorage
= DOMStorage::LocalStorage
;
27 class DOMStorageManager
: public nsIDOMStorageManager
28 , public DOMStorageObserverSink
31 NS_DECL_NSIDOMSTORAGEMANAGER
34 virtual DOMStorage::StorageType
Type() { return mType
; }
36 // Reads the preference for DOM storage quota
37 static uint32_t GetQuota();
38 // Gets (but not ensures) cache for the given scope
39 DOMStorageCache
* GetCache(const nsACString
& aScope
) const;
40 // Returns object keeping usage cache for the scope.
41 already_AddRefed
<DOMStorageUsage
> GetScopeUsage(const nsACString
& aScope
);
44 explicit DOMStorageManager(DOMStorage::StorageType aType
);
45 virtual ~DOMStorageManager();
48 // DOMStorageObserverSink, handler to various chrome clearing notification
49 virtual nsresult
Observe(const char* aTopic
, const nsACString
& aScopePrefix
);
51 // Since nsTHashtable doesn't like multiple inheritance, we have to aggregate
52 // DOMStorageCache into the entry.
53 class DOMStorageCacheHashKey
: public nsCStringHashKey
56 explicit DOMStorageCacheHashKey(const nsACString
* aKey
)
57 : nsCStringHashKey(aKey
)
58 , mCache(new DOMStorageCache(aKey
))
61 DOMStorageCacheHashKey(const DOMStorageCacheHashKey
& aOther
)
62 : nsCStringHashKey(aOther
)
64 NS_ERROR("Shouldn't be called");
67 DOMStorageCache
* cache() { return mCache
; }
68 // Keep the cache referenced forever, used for sessionStorage.
69 void HardRef() { mCacheRef
= mCache
; }
72 // weak ref only since cache references its manager.
73 DOMStorageCache
* mCache
;
74 // hard ref when this is sessionStorage to keep it alive forever.
75 nsRefPtr
<DOMStorageCache
> mCacheRef
;
78 // Ensures cache for a scope, when it doesn't exist it is created and initalized,
79 // this also starts preload of persistent data.
80 already_AddRefed
<DOMStorageCache
> PutCache(const nsACString
& aScope
,
81 nsIPrincipal
* aPrincipal
);
83 // Helper for creation of DOM storage objects
84 nsresult
GetStorageInternal(bool aCreate
,
85 nsIDOMWindow
* aWindow
,
86 nsIPrincipal
* aPrincipal
,
87 const nsAString
& aDocumentURI
,
89 nsIDOMStorage
** aRetval
);
92 nsTHashtable
<DOMStorageCacheHashKey
> mCaches
;
93 const DOMStorage::StorageType mType
;
95 // If mLowDiskSpace is true it indicates a low device storage situation and
96 // so no localStorage writes are allowed. sessionStorage writes are still
99 bool IsLowDiskSpace() const { return mLowDiskSpace
; };
101 static PLDHashOperator
ClearCacheEnumerator(DOMStorageCacheHashKey
* aCache
,
105 // Keeps usage cache objects for eTLD+1 scopes we have touched.
106 nsDataHashtable
<nsCStringHashKey
, nsRefPtr
<DOMStorageUsage
> > mUsages
;
108 friend class DOMStorageCache
;
109 // Releases cache since it is no longer referrered by any DOMStorage object.
110 virtual void DropCache(DOMStorageCache
* aCache
);
113 // Derived classes to allow two different contract ids, one for localStorage and
114 // one for sessionStorage management. localStorage manager is used as service
115 // scoped to the application while sessionStorage managers are instantiated by each
116 // top doc shell in the application since sessionStorages are isolated per top level
117 // browsing context. The code may easily by shared by both.
119 class DOMLocalStorageManager MOZ_FINAL
: public DOMStorageManager
122 DOMLocalStorageManager();
123 virtual ~DOMLocalStorageManager();
125 // Global getter of localStorage manager service
126 static DOMLocalStorageManager
* Self() { return sSelf
; }
129 static DOMLocalStorageManager
* sSelf
;
132 class DOMSessionStorageManager MOZ_FINAL
: public DOMStorageManager
135 DOMSessionStorageManager();
141 #endif /* nsDOMStorageManager_h__ */