Bug 1700051: part 35) Reduce accessibility of `mSoftText.mDOMMapping` to `private...
[gecko.git] / dom / quota / PersistenceType.cpp
blob2078c9e249de3fd7a41b6029abc3d27e685feeef
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"
9 #include <utility>
10 #include "nsIFile.h"
11 #include "nsLiteralString.h"
12 #include "nsString.h"
14 namespace mozilla::dom::quota {
16 namespace {
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;
35 template <>
36 struct PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT> {
37 template <typename T>
38 static T To();
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;
57 template <>
58 nsLiteralCString
59 PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<nsLiteralCString>() {
60 return kPersistentCString;
63 template <>
64 StorageType
65 PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<StorageType>() {
66 return StorageType::Persistent;
69 template <>
70 struct PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY> {
71 template <typename T>
72 static T To();
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;
91 template <>
92 nsLiteralCString
93 PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<nsLiteralCString>() {
94 return kTemporaryCString;
97 template <>
98 StorageType
99 PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<StorageType>() {
100 return StorageType::Temporary;
103 template <>
104 struct PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT> {
105 template <typename T>
106 static T To();
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;
125 template <>
126 nsLiteralCString
127 PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::To<nsLiteralCString>() {
128 return kDefaultCString;
131 template <>
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>());
148 default:
149 return Nothing();
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);
167 return Nothing();
170 void BadPersistenceType() { MOZ_CRASH("Bad persistence type value!"); }
172 } // namespace
174 bool IsValidPersistenceType(const PersistenceType aPersistenceType) {
175 switch (aPersistenceType) {
176 case PERSISTENCE_TYPE_PERSISTENT:
177 case PERSISTENCE_TYPE_TEMPORARY:
178 case PERSISTENCE_TYPE_DEFAULT:
179 return true;
181 default:
182 return false;
186 bool IsBestEffortPersistenceType(const PersistenceType aPersistenceType) {
187 switch (aPersistenceType) {
188 case PERSISTENCE_TYPE_TEMPORARY:
189 case PERSISTENCE_TYPE_DEFAULT:
190 return true;
192 case PERSISTENCE_TYPE_PERSISTENT:
193 case PERSISTENCE_TYPE_INVALID:
194 default:
195 return false;
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,
209 const fallible_t&) {
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,
239 const fallible_t&) {
240 return TypeFrom_impl(aInt32);
243 Maybe<PersistenceType> PersistenceTypeFromFile(nsIFile& aFile,
244 const fallible_t&) {
245 return TypeFrom_impl(aFile);
248 } // namespace mozilla::dom::quota