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 #include "SessionStorage.h"
9 #include "SessionStorageCache.h"
10 #include "SessionStorageManager.h"
12 #include "mozilla/dom/StorageBinding.h"
13 #include "mozilla/Preferences.h"
14 #include "nsContentUtils.h"
15 #include "nsIPrincipal.h"
16 #include "nsPIDOMWindow.h"
17 #include "nsThreadUtils.h"
19 namespace mozilla::dom
{
21 NS_IMPL_CYCLE_COLLECTION_INHERITED(SessionStorage
, Storage
, mManager
);
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SessionStorage
)
24 NS_INTERFACE_MAP_END_INHERITING(Storage
)
26 NS_IMPL_ADDREF_INHERITED(SessionStorage
, Storage
)
27 NS_IMPL_RELEASE_INHERITED(SessionStorage
, Storage
)
29 SessionStorage::SessionStorage(nsPIDOMWindowInner
* aWindow
,
30 nsIPrincipal
* aPrincipal
,
31 nsIPrincipal
* aStoragePrincipal
,
32 SessionStorageCache
* aCache
,
33 SessionStorageManager
* aManager
,
34 const nsAString
& aDocumentURI
, bool aIsPrivate
)
35 : Storage(aWindow
, aPrincipal
, aStoragePrincipal
),
38 mDocumentURI(aDocumentURI
),
39 mIsPrivate(aIsPrivate
),
40 mHasPendingStableStateCallback(false) {
44 SessionStorage::~SessionStorage() = default;
46 int64_t SessionStorage::GetOriginQuotaUsage() const {
47 nsresult rv
= EnsureCacheLoadedOrCloned();
48 if (NS_WARN_IF(NS_FAILED(rv
))) {
52 return mCache
->GetOriginQuotaUsage();
55 uint32_t SessionStorage::GetLength(nsIPrincipal
& aSubjectPrincipal
,
57 if (!CanUseStorage(aSubjectPrincipal
)) {
58 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
62 nsresult rv
= EnsureCacheLoadedOrCloned();
63 if (NS_WARN_IF(NS_FAILED(rv
))) {
68 return mCache
->Length();
71 void SessionStorage::Key(uint32_t aIndex
, nsAString
& aResult
,
72 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {
73 if (!CanUseStorage(aSubjectPrincipal
)) {
74 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
78 nsresult rv
= EnsureCacheLoadedOrCloned();
79 if (NS_WARN_IF(NS_FAILED(rv
))) {
84 mCache
->Key(aIndex
, aResult
);
87 void SessionStorage::GetItem(const nsAString
& aKey
, nsAString
& aResult
,
88 nsIPrincipal
& aSubjectPrincipal
,
90 if (!CanUseStorage(aSubjectPrincipal
)) {
91 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
95 nsresult rv
= EnsureCacheLoadedOrCloned();
96 if (NS_WARN_IF(NS_FAILED(rv
))) {
101 mCache
->GetItem(aKey
, aResult
);
104 void SessionStorage::GetSupportedNames(nsTArray
<nsString
>& aKeys
) {
105 if (!CanUseStorage(*nsContentUtils::SubjectPrincipal())) {
106 // return just an empty array
111 nsresult rv
= EnsureCacheLoadedOrCloned();
112 if (NS_WARN_IF(NS_FAILED(rv
))) {
113 // return just an empty array
118 mCache
->GetKeys(aKeys
);
121 void SessionStorage::SetItem(const nsAString
& aKey
, const nsAString
& aValue
,
122 nsIPrincipal
& aSubjectPrincipal
,
124 if (!CanUseStorage(aSubjectPrincipal
)) {
125 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
129 nsresult rv
= EnsureCacheLoadedOrCloned();
130 if (NS_WARN_IF(NS_FAILED(rv
))) {
136 rv
= mCache
->SetItem(aKey
, aValue
, oldValue
);
137 if (NS_WARN_IF(NS_FAILED(rv
))) {
142 if (rv
== NS_SUCCESS_DOM_NO_OPERATION
) {
146 BroadcastChangeNotification(aKey
, oldValue
, aValue
);
149 void SessionStorage::RemoveItem(const nsAString
& aKey
,
150 nsIPrincipal
& aSubjectPrincipal
,
152 if (!CanUseStorage(aSubjectPrincipal
)) {
153 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
157 nsresult rv
= EnsureCacheLoadedOrCloned();
158 if (NS_WARN_IF(NS_FAILED(rv
))) {
164 rv
= mCache
->RemoveItem(aKey
, oldValue
);
165 MOZ_ASSERT(NS_SUCCEEDED(rv
));
167 if (rv
== NS_SUCCESS_DOM_NO_OPERATION
) {
171 BroadcastChangeNotification(aKey
, oldValue
, VoidString());
174 void SessionStorage::Clear(nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {
175 uint32_t length
= GetLength(aSubjectPrincipal
, aRv
);
180 nsresult rv
= EnsureCacheLoadedOrCloned();
181 if (NS_WARN_IF(NS_FAILED(rv
))) {
187 BroadcastChangeNotification(VoidString(), VoidString(), VoidString());
190 void SessionStorage::BroadcastChangeNotification(const nsAString
& aKey
,
191 const nsAString
& aOldValue
,
192 const nsAString
& aNewValue
) {
193 NotifyChange(this, StoragePrincipal(), aKey
, aOldValue
, aNewValue
,
194 u
"sessionStorage", mDocumentURI
, mIsPrivate
, false);
196 // Sync changes on SessionStorageCache at the next statble state.
197 if (mManager
->CanLoadData()) {
198 MaybeScheduleStableStateCallback();
202 bool SessionStorage::IsForkOf(const Storage
* aOther
) const {
204 if (aOther
->Type() != eSessionStorage
) {
208 return mCache
== static_cast<const SessionStorage
*>(aOther
)->mCache
;
211 void SessionStorage::MaybeScheduleStableStateCallback() {
212 AssertIsOnOwningThread();
214 if (!mHasPendingStableStateCallback
) {
215 nsContentUtils::RunInStableState(
216 NewRunnableMethod("SessionStorage::StableStateCallback", this,
217 &SessionStorage::StableStateCallback
));
219 mHasPendingStableStateCallback
= true;
223 void SessionStorage::StableStateCallback() {
224 AssertIsOnOwningThread();
225 MOZ_ASSERT(mHasPendingStableStateCallback
);
226 MOZ_ASSERT(mManager
);
229 mHasPendingStableStateCallback
= false;
231 if (mManager
->CanLoadData()) {
232 mManager
->CheckpointData(*StoragePrincipal(), *mCache
);
236 nsresult
SessionStorage::EnsureCacheLoadedOrCloned() const {
237 AssertIsOnOwningThread();
238 MOZ_ASSERT(mManager
);
240 if (!mManager
->CanLoadData()) {
244 // Ensure manager actor.
245 nsresult rv
= mManager
->EnsureManager();
246 if (NS_WARN_IF(NS_FAILED(rv
))) {
250 // Ensure cache is loaded or cloned.
251 if (mCache
->WasLoadedOrCloned()) {
255 return mManager
->LoadData(*StoragePrincipal(), *mCache
);
258 } // namespace mozilla::dom