no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / storage / SessionStorage.cpp
blob4f13370338b5800937d2ef2513678c3a2a9a1359
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),
36 mCache(aCache),
37 mManager(aManager),
38 mDocumentURI(aDocumentURI),
39 mIsPrivate(aIsPrivate),
40 mHasPendingStableStateCallback(false) {
41 MOZ_ASSERT(aCache);
44 SessionStorage::~SessionStorage() = default;
46 int64_t SessionStorage::GetOriginQuotaUsage() const {
47 nsresult rv = EnsureCacheLoadedOrCloned();
48 if (NS_WARN_IF(NS_FAILED(rv))) {
49 return 0;
52 return mCache->GetOriginQuotaUsage();
55 uint32_t SessionStorage::GetLength(nsIPrincipal& aSubjectPrincipal,
56 ErrorResult& aRv) {
57 if (!CanUseStorage(aSubjectPrincipal)) {
58 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
59 return 0;
62 nsresult rv = EnsureCacheLoadedOrCloned();
63 if (NS_WARN_IF(NS_FAILED(rv))) {
64 aRv.Throw(rv);
65 return 0;
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);
75 return;
78 nsresult rv = EnsureCacheLoadedOrCloned();
79 if (NS_WARN_IF(NS_FAILED(rv))) {
80 aRv.Throw(rv);
81 return;
84 mCache->Key(aIndex, aResult);
87 void SessionStorage::GetItem(const nsAString& aKey, nsAString& aResult,
88 nsIPrincipal& aSubjectPrincipal,
89 ErrorResult& aRv) {
90 if (!CanUseStorage(aSubjectPrincipal)) {
91 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
92 return;
95 nsresult rv = EnsureCacheLoadedOrCloned();
96 if (NS_WARN_IF(NS_FAILED(rv))) {
97 aRv.Throw(rv);
98 return;
101 mCache->GetItem(aKey, aResult);
104 void SessionStorage::GetSupportedNames(nsTArray<nsString>& aKeys) {
105 if (!CanUseStorage(*nsContentUtils::SubjectPrincipal())) {
106 // return just an empty array
107 aKeys.Clear();
108 return;
111 nsresult rv = EnsureCacheLoadedOrCloned();
112 if (NS_WARN_IF(NS_FAILED(rv))) {
113 // return just an empty array
114 aKeys.Clear();
115 return;
118 mCache->GetKeys(aKeys);
121 void SessionStorage::SetItem(const nsAString& aKey, const nsAString& aValue,
122 nsIPrincipal& aSubjectPrincipal,
123 ErrorResult& aRv) {
124 if (!CanUseStorage(aSubjectPrincipal)) {
125 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
126 return;
129 nsresult rv = EnsureCacheLoadedOrCloned();
130 if (NS_WARN_IF(NS_FAILED(rv))) {
131 aRv.Throw(rv);
132 return;
135 nsString oldValue;
136 rv = mCache->SetItem(aKey, aValue, oldValue);
137 if (NS_WARN_IF(NS_FAILED(rv))) {
138 aRv.Throw(rv);
139 return;
142 if (rv == NS_SUCCESS_DOM_NO_OPERATION) {
143 return;
146 BroadcastChangeNotification(aKey, oldValue, aValue);
149 void SessionStorage::RemoveItem(const nsAString& aKey,
150 nsIPrincipal& aSubjectPrincipal,
151 ErrorResult& aRv) {
152 if (!CanUseStorage(aSubjectPrincipal)) {
153 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
154 return;
157 nsresult rv = EnsureCacheLoadedOrCloned();
158 if (NS_WARN_IF(NS_FAILED(rv))) {
159 aRv.Throw(rv);
160 return;
163 nsString oldValue;
164 rv = mCache->RemoveItem(aKey, oldValue);
165 MOZ_ASSERT(NS_SUCCEEDED(rv));
167 if (rv == NS_SUCCESS_DOM_NO_OPERATION) {
168 return;
171 BroadcastChangeNotification(aKey, oldValue, VoidString());
174 void SessionStorage::Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
175 uint32_t length = GetLength(aSubjectPrincipal, aRv);
176 if (!length) {
177 return;
180 nsresult rv = EnsureCacheLoadedOrCloned();
181 if (NS_WARN_IF(NS_FAILED(rv))) {
182 aRv.Throw(rv);
183 return;
186 mCache->Clear();
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 {
203 MOZ_ASSERT(aOther);
204 if (aOther->Type() != eSessionStorage) {
205 return false;
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);
227 MOZ_ASSERT(mCache);
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()) {
241 return NS_OK;
244 // Ensure manager actor.
245 nsresult rv = mManager->EnsureManager();
246 if (NS_WARN_IF(NS_FAILED(rv))) {
247 return rv;
250 // Ensure cache is loaded or cloned.
251 if (mCache->WasLoadedOrCloned()) {
252 return NS_OK;
255 return mManager->LoadData(*StoragePrincipal(), *mCache);
258 } // namespace mozilla::dom