Bug 1660051 [wpt PR 25111] - Origin isolation: expand getter test coverage, a=testonly
[gecko.git] / dom / storage / PartitionedLocalStorage.cpp
blob589bc900d516a73a14beddc577151687b9693e90
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 "PartitionedLocalStorage.h"
8 #include "SessionStorageCache.h"
10 #include "mozilla/dom/StorageBinding.h"
12 namespace mozilla {
13 namespace dom {
15 NS_IMPL_CYCLE_COLLECTION_INHERITED(PartitionedLocalStorage, Storage);
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PartitionedLocalStorage)
18 NS_INTERFACE_MAP_END_INHERITING(Storage)
20 NS_IMPL_ADDREF_INHERITED(PartitionedLocalStorage, Storage)
21 NS_IMPL_RELEASE_INHERITED(PartitionedLocalStorage, Storage)
23 PartitionedLocalStorage::PartitionedLocalStorage(
24 nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
25 nsIPrincipal* aStoragePrincipal, SessionStorageCache* aCache)
26 : Storage(aWindow, aPrincipal, aStoragePrincipal), mCache(aCache) {}
28 PartitionedLocalStorage::~PartitionedLocalStorage() = default;
30 int64_t PartitionedLocalStorage::GetOriginQuotaUsage() const {
31 return mCache->GetOriginQuotaUsage(SessionStorageCache::eSessionSetType);
34 uint32_t PartitionedLocalStorage::GetLength(nsIPrincipal& aSubjectPrincipal,
35 ErrorResult& aRv) {
36 if (!CanUseStorage(aSubjectPrincipal)) {
37 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
38 return 0;
41 return mCache->Length(SessionStorageCache::eSessionSetType);
44 void PartitionedLocalStorage::Key(uint32_t aIndex, nsAString& aResult,
45 nsIPrincipal& aSubjectPrincipal,
46 ErrorResult& aRv) {
47 if (!CanUseStorage(aSubjectPrincipal)) {
48 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
49 return;
52 mCache->Key(SessionStorageCache::eSessionSetType, aIndex, aResult);
55 void PartitionedLocalStorage::GetItem(const nsAString& aKey, nsAString& aResult,
56 nsIPrincipal& aSubjectPrincipal,
57 ErrorResult& aRv) {
58 if (!CanUseStorage(aSubjectPrincipal)) {
59 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
60 return;
63 mCache->GetItem(SessionStorageCache::eSessionSetType, aKey, aResult);
66 void PartitionedLocalStorage::GetSupportedNames(nsTArray<nsString>& aKeys) {
67 if (!CanUseStorage(*nsContentUtils::SubjectPrincipal())) {
68 // return just an empty array
69 aKeys.Clear();
70 return;
73 mCache->GetKeys(SessionStorageCache::eSessionSetType, aKeys);
76 void PartitionedLocalStorage::SetItem(const nsAString& aKey,
77 const nsAString& aValue,
78 nsIPrincipal& aSubjectPrincipal,
79 ErrorResult& aRv) {
80 if (!CanUseStorage(aSubjectPrincipal)) {
81 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
82 return;
85 nsString oldValue;
86 nsresult rv = mCache->SetItem(SessionStorageCache::eSessionSetType, aKey,
87 aValue, oldValue);
88 if (NS_WARN_IF(NS_FAILED(rv))) {
89 aRv.Throw(rv);
90 return;
93 if (rv == NS_SUCCESS_DOM_NO_OPERATION) {
94 return;
98 void PartitionedLocalStorage::RemoveItem(const nsAString& aKey,
99 nsIPrincipal& aSubjectPrincipal,
100 ErrorResult& aRv) {
101 if (!CanUseStorage(aSubjectPrincipal)) {
102 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
103 return;
106 nsString oldValue;
107 nsresult rv =
108 mCache->RemoveItem(SessionStorageCache::eSessionSetType, aKey, oldValue);
109 MOZ_ASSERT(NS_SUCCEEDED(rv));
111 if (rv == NS_SUCCESS_DOM_NO_OPERATION) {
112 return;
116 void PartitionedLocalStorage::Clear(nsIPrincipal& aSubjectPrincipal,
117 ErrorResult& aRv) {
118 uint32_t length = GetLength(aSubjectPrincipal, aRv);
119 if (!length) {
120 return;
123 mCache->Clear(SessionStorageCache::eSessionSetType);
126 bool PartitionedLocalStorage::IsForkOf(const Storage* aOther) const {
127 MOZ_ASSERT(aOther);
128 if (aOther->Type() != eLocalStorage) {
129 return false;
132 return mCache == static_cast<const PartitionedLocalStorage*>(aOther)->mCache;
135 } // namespace dom
136 } // namespace mozilla