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 "PersistenceType.h"
11 #include "nsLiteralString.h"
14 namespace mozilla::dom::quota
{
18 constexpr auto kPersistentCString
= "persistent"_ns
;
19 constexpr auto kTemporaryCString
= "temporary"_ns
;
20 constexpr auto kDefaultCString
= "default"_ns
;
22 constexpr auto kPermanentString
= u
"permanent"_ns
;
23 constexpr auto kTemporaryString
= u
"temporary"_ns
;
24 constexpr auto kDefaultString
= u
"default"_ns
;
26 static_assert(PERSISTENCE_TYPE_PERSISTENT
== 0 &&
27 PERSISTENCE_TYPE_TEMPORARY
== 1 &&
28 PERSISTENCE_TYPE_DEFAULT
== 2 &&
29 PERSISTENCE_TYPE_INVALID
== 3,
30 "Incorrect enum values!");
32 template <PersistenceType type
>
33 struct PersistenceTypeTraits
;
36 struct PersistenceTypeTraits
<PERSISTENCE_TYPE_PERSISTENT
> {
40 static bool From(const nsACString
& aString
) {
41 return aString
== kPersistentCString
;
44 static bool From(const StorageType aStorageType
) {
45 return aStorageType
== StorageType::Persistent
;
48 static bool From(const int32_t aInt32
) { return aInt32
== 0; }
50 static bool From(nsIFile
& aFile
) {
51 nsAutoString leafName
;
52 MOZ_ALWAYS_SUCCEEDS(aFile
.GetLeafName(leafName
));
53 return leafName
== kPermanentString
;
59 PersistenceTypeTraits
<PERSISTENCE_TYPE_PERSISTENT
>::To
<nsLiteralCString
>() {
60 return kPersistentCString
;
65 PersistenceTypeTraits
<PERSISTENCE_TYPE_PERSISTENT
>::To
<StorageType
>() {
66 return StorageType::Persistent
;
70 struct PersistenceTypeTraits
<PERSISTENCE_TYPE_TEMPORARY
> {
74 static bool From(const nsACString
& aString
) {
75 return aString
== kTemporaryCString
;
78 static bool From(const StorageType aStorageType
) {
79 return aStorageType
== StorageType::Temporary
;
82 static bool From(const int32_t aInt32
) { return aInt32
== 1; }
84 static bool From(nsIFile
& aFile
) {
85 nsAutoString leafName
;
86 MOZ_ALWAYS_SUCCEEDS(aFile
.GetLeafName(leafName
));
87 return leafName
== kTemporaryString
;
93 PersistenceTypeTraits
<PERSISTENCE_TYPE_TEMPORARY
>::To
<nsLiteralCString
>() {
94 return kTemporaryCString
;
99 PersistenceTypeTraits
<PERSISTENCE_TYPE_TEMPORARY
>::To
<StorageType
>() {
100 return StorageType::Temporary
;
104 struct PersistenceTypeTraits
<PERSISTENCE_TYPE_DEFAULT
> {
105 template <typename T
>
108 static bool From(const nsACString
& aString
) {
109 return aString
== kDefaultCString
;
112 static bool From(const StorageType aStorageType
) {
113 return aStorageType
== StorageType::Default
;
116 static bool From(const int32_t aInt32
) { return aInt32
== 2; }
118 static bool From(nsIFile
& aFile
) {
119 nsAutoString leafName
;
120 MOZ_ALWAYS_SUCCEEDS(aFile
.GetLeafName(leafName
));
121 return leafName
== kDefaultString
;
127 PersistenceTypeTraits
<PERSISTENCE_TYPE_DEFAULT
>::To
<nsLiteralCString
>() {
128 return kDefaultCString
;
132 StorageType PersistenceTypeTraits
<PERSISTENCE_TYPE_DEFAULT
>::To
<StorageType
>() {
133 return StorageType::Default
;
136 template <typename T
>
137 Maybe
<T
> TypeTo_impl(const PersistenceType aPersistenceType
) {
138 switch (aPersistenceType
) {
139 case PERSISTENCE_TYPE_PERSISTENT
:
140 return Some(PersistenceTypeTraits
<PERSISTENCE_TYPE_PERSISTENT
>::To
<T
>());
142 case PERSISTENCE_TYPE_TEMPORARY
:
143 return Some(PersistenceTypeTraits
<PERSISTENCE_TYPE_TEMPORARY
>::To
<T
>());
145 case PERSISTENCE_TYPE_DEFAULT
:
146 return Some(PersistenceTypeTraits
<PERSISTENCE_TYPE_DEFAULT
>::To
<T
>());
153 template <typename T
>
154 Maybe
<PersistenceType
> TypeFrom_impl(T
& aData
) {
155 if (PersistenceTypeTraits
<PERSISTENCE_TYPE_PERSISTENT
>::From(aData
)) {
156 return Some(PERSISTENCE_TYPE_PERSISTENT
);
159 if (PersistenceTypeTraits
<PERSISTENCE_TYPE_TEMPORARY
>::From(aData
)) {
160 return Some(PERSISTENCE_TYPE_TEMPORARY
);
163 if (PersistenceTypeTraits
<PERSISTENCE_TYPE_DEFAULT
>::From(aData
)) {
164 return Some(PERSISTENCE_TYPE_DEFAULT
);
170 void BadPersistenceType() { MOZ_CRASH("Bad persistence type value!"); }
174 bool IsValidPersistenceType(const PersistenceType aPersistenceType
) {
175 switch (aPersistenceType
) {
176 case PERSISTENCE_TYPE_PERSISTENT
:
177 case PERSISTENCE_TYPE_TEMPORARY
:
178 case PERSISTENCE_TYPE_DEFAULT
:
186 bool IsBestEffortPersistenceType(const PersistenceType aPersistenceType
) {
187 switch (aPersistenceType
) {
188 case PERSISTENCE_TYPE_TEMPORARY
:
189 case PERSISTENCE_TYPE_DEFAULT
:
192 case PERSISTENCE_TYPE_PERSISTENT
:
193 case PERSISTENCE_TYPE_INVALID
:
199 nsLiteralCString
PersistenceTypeToString(
200 const PersistenceType aPersistenceType
) {
201 const auto maybeString
= TypeTo_impl
<nsLiteralCString
>(aPersistenceType
);
202 if (maybeString
.isNothing()) {
203 BadPersistenceType();
205 return maybeString
.value();
208 Maybe
<PersistenceType
> PersistenceTypeFromString(const nsACString
& aString
,
210 return TypeFrom_impl(aString
);
213 PersistenceType
PersistenceTypeFromString(const nsACString
& aString
) {
214 const auto maybePersistenceType
= TypeFrom_impl(aString
);
215 if (maybePersistenceType
.isNothing()) {
216 BadPersistenceType();
218 return maybePersistenceType
.value();
221 StorageType
PersistenceTypeToStorageType(
222 const PersistenceType aPersistenceType
) {
223 const auto maybeStorageType
= TypeTo_impl
<StorageType
>(aPersistenceType
);
224 if (maybeStorageType
.isNothing()) {
225 BadPersistenceType();
227 return maybeStorageType
.value();
230 PersistenceType
PersistenceTypeFromStorageType(const StorageType aStorageType
) {
231 const auto maybePersistenceType
= TypeFrom_impl(aStorageType
);
232 if (maybePersistenceType
.isNothing()) {
233 BadPersistenceType();
235 return maybePersistenceType
.value();
238 Maybe
<PersistenceType
> PersistenceTypeFromInt32(const int32_t aInt32
,
240 return TypeFrom_impl(aInt32
);
243 Maybe
<PersistenceType
> PersistenceTypeFromFile(nsIFile
& aFile
,
245 return TypeFrom_impl(aFile
);
248 } // namespace mozilla::dom::quota