blob: 9dd517304308727178391461035932f830dde54f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "PersistenceType.h"
#include <utility>
#include "nsIFile.h"
#include "nsLiteralString.h"
#include "nsString.h"
namespace mozilla::dom::quota {
namespace {
constexpr auto kPersistentCString = "persistent"_ns;
constexpr auto kTemporaryCString = "temporary"_ns;
constexpr auto kDefaultCString = "default"_ns;
constexpr auto kPrivateCString = "private"_ns;
constexpr auto kPermanentString = u"permanent"_ns;
constexpr auto kTemporaryString = u"temporary"_ns;
constexpr auto kDefaultString = u"default"_ns;
constexpr auto kPrivateString = u"private"_ns;
static_assert(PERSISTENCE_TYPE_PERSISTENT == 0 &&
PERSISTENCE_TYPE_TEMPORARY == 1 &&
PERSISTENCE_TYPE_DEFAULT == 2 &&
PERSISTENCE_TYPE_PRIVATE == 3 &&
PERSISTENCE_TYPE_INVALID == 4,
"Incorrect enum values!");
template <PersistenceType type>
struct PersistenceTypeTraits;
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kPersistentCString;
}
static bool From(const int32_t aInt32) { return aInt32 == 0; }
static bool From(nsIFile& aFile) {
nsAutoString leafName;
MOZ_ALWAYS_SUCCEEDS(aFile.GetLeafName(leafName));
return leafName == kPermanentString;
}
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<nsLiteralCString>() {
return kPersistentCString;
}
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kTemporaryCString;
}
static bool From(const int32_t aInt32) { return aInt32 == 1; }
static bool From(nsIFile& aFile) {
nsAutoString leafName;
MOZ_ALWAYS_SUCCEEDS(aFile.GetLeafName(leafName));
return leafName == kTemporaryString;
}
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<nsLiteralCString>() {
return kTemporaryCString;
}
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kDefaultCString;
}
static bool From(const int32_t aInt32) { return aInt32 == 2; }
static bool From(nsIFile& aFile) {
nsAutoString leafName;
MOZ_ALWAYS_SUCCEEDS(aFile.GetLeafName(leafName));
return leafName == kDefaultString;
}
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::To<nsLiteralCString>() {
return kDefaultCString;
}
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_PRIVATE> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kPrivateCString;
}
static bool From(const int32_t aInt32) { return aInt32 == 3; }
static bool From(nsIFile& aFile) {
nsAutoString leafName;
MOZ_ALWAYS_SUCCEEDS(aFile.GetLeafName(leafName));
return leafName == kPrivateString;
}
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_PRIVATE>::To<nsLiteralCString>() {
return kPrivateCString;
}
template <typename T>
Maybe<T> TypeTo_impl(const PersistenceType aPersistenceType) {
switch (aPersistenceType) {
case PERSISTENCE_TYPE_PERSISTENT:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<T>());
case PERSISTENCE_TYPE_TEMPORARY:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<T>());
case PERSISTENCE_TYPE_DEFAULT:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::To<T>());
case PERSISTENCE_TYPE_PRIVATE:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_PRIVATE>::To<T>());
default:
return Nothing();
}
}
template <typename T>
Maybe<PersistenceType> TypeFrom_impl(T& aData) {
if (PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::From(aData)) {
return Some(PERSISTENCE_TYPE_PERSISTENT);
}
if (PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::From(aData)) {
return Some(PERSISTENCE_TYPE_TEMPORARY);
}
if (PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::From(aData)) {
return Some(PERSISTENCE_TYPE_DEFAULT);
}
if (PersistenceTypeTraits<PERSISTENCE_TYPE_PRIVATE>::From(aData)) {
return Some(PERSISTENCE_TYPE_PRIVATE);
}
return Nothing();
}
void BadPersistenceType() { MOZ_CRASH("Bad persistence type value!"); }
} // namespace
bool IsValidPersistenceType(const PersistenceType aPersistenceType) {
switch (aPersistenceType) {
case PERSISTENCE_TYPE_PERSISTENT:
case PERSISTENCE_TYPE_TEMPORARY:
case PERSISTENCE_TYPE_DEFAULT:
case PERSISTENCE_TYPE_PRIVATE:
return true;
default:
return false;
}
}
bool IsBestEffortPersistenceType(const PersistenceType aPersistenceType) {
switch (aPersistenceType) {
case PERSISTENCE_TYPE_TEMPORARY:
case PERSISTENCE_TYPE_DEFAULT:
case PERSISTENCE_TYPE_PRIVATE:
return true;
case PERSISTENCE_TYPE_PERSISTENT:
case PERSISTENCE_TYPE_INVALID:
default:
return false;
}
}
nsLiteralCString PersistenceTypeToString(
const PersistenceType aPersistenceType) {
const auto maybeString = TypeTo_impl<nsLiteralCString>(aPersistenceType);
if (maybeString.isNothing()) {
BadPersistenceType();
}
return maybeString.value();
}
Maybe<PersistenceType> PersistenceTypeFromString(const nsACString& aString,
const fallible_t&) {
return TypeFrom_impl(aString);
}
PersistenceType PersistenceTypeFromString(const nsACString& aString) {
const auto maybePersistenceType = TypeFrom_impl(aString);
if (maybePersistenceType.isNothing()) {
BadPersistenceType();
}
return maybePersistenceType.value();
}
Maybe<PersistenceType> PersistenceTypeFromInt32(const int32_t aInt32,
const fallible_t&) {
return TypeFrom_impl(aInt32);
}
Maybe<PersistenceType> PersistenceTypeFromFile(nsIFile& aFile,
const fallible_t&) {
return TypeFrom_impl(aFile);
}
std::array<PersistenceType, 2> ComplementaryPersistenceTypes(
const PersistenceType aPersistenceType) {
MOZ_ASSERT(aPersistenceType == PERSISTENCE_TYPE_DEFAULT ||
aPersistenceType == PERSISTENCE_TYPE_TEMPORARY ||
aPersistenceType == PERSISTENCE_TYPE_PRIVATE);
if (aPersistenceType == PERSISTENCE_TYPE_TEMPORARY) {
return {PERSISTENCE_TYPE_DEFAULT, PERSISTENCE_TYPE_PRIVATE};
}
if (aPersistenceType == PERSISTENCE_TYPE_DEFAULT) {
return {PERSISTENCE_TYPE_TEMPORARY, PERSISTENCE_TYPE_PRIVATE};
}
return {PERSISTENCE_TYPE_DEFAULT, PERSISTENCE_TYPE_TEMPORARY};
}
} // namespace mozilla::dom::quota
|