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/. */
10 #include "mozilla/ipc/BackgroundParent.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/dom/quota/QuotaManager.h"
14 namespace mozilla::dom::quota
{
16 using mozilla::ipc::AssertIsOnBackgroundThread
;
17 using mozilla::ipc::IsOnBackgroundThread
;
21 const char kIDBPrefix
= 'I';
22 const char kDOMCachePrefix
= 'C';
23 const char kSDBPrefix
= 'S';
24 const char kFILESYSTEMPrefix
= 'F';
25 const char kLSPrefix
= 'L';
27 template <Client::Type type
>
28 struct ClientTypeTraits
;
31 struct ClientTypeTraits
<Client::Type::IDB
> {
33 static void To(T
& aData
) {
34 aData
.AssignLiteral(IDB_DIRECTORY_NAME
);
37 static void To(char& aData
) { aData
= kIDBPrefix
; }
40 static bool From(const T
& aData
) {
41 return aData
.EqualsLiteral(IDB_DIRECTORY_NAME
);
44 static bool From(char aData
) { return aData
== kIDBPrefix
; }
48 struct ClientTypeTraits
<Client::Type::DOMCACHE
> {
50 static void To(T
& aData
) {
51 aData
.AssignLiteral(DOMCACHE_DIRECTORY_NAME
);
54 static void To(char& aData
) { aData
= kDOMCachePrefix
; }
57 static bool From(const T
& aData
) {
58 return aData
.EqualsLiteral(DOMCACHE_DIRECTORY_NAME
);
61 static bool From(char aData
) { return aData
== kDOMCachePrefix
; }
65 struct ClientTypeTraits
<Client::Type::SDB
> {
67 static void To(T
& aData
) {
68 aData
.AssignLiteral(SDB_DIRECTORY_NAME
);
71 static void To(char& aData
) { aData
= kSDBPrefix
; }
74 static bool From(const T
& aData
) {
75 return aData
.EqualsLiteral(SDB_DIRECTORY_NAME
);
78 static bool From(char aData
) { return aData
== kSDBPrefix
; }
82 struct ClientTypeTraits
<Client::Type::FILESYSTEM
> {
84 static void To(T
& aData
) {
85 aData
.AssignLiteral(FILESYSTEM_DIRECTORY_NAME
);
88 static void To(char& aData
) { aData
= kFILESYSTEMPrefix
; }
91 static bool From(const T
& aData
) {
92 return aData
.EqualsLiteral(FILESYSTEM_DIRECTORY_NAME
);
95 static bool From(char aData
) { return aData
== kFILESYSTEMPrefix
; }
99 struct ClientTypeTraits
<Client::Type::LS
> {
100 template <typename T
>
101 static void To(T
& aData
) {
102 aData
.AssignLiteral(LS_DIRECTORY_NAME
);
105 static void To(char& aData
) { aData
= kLSPrefix
; }
107 template <typename T
>
108 static bool From(const T
& aData
) {
109 return aData
.EqualsLiteral(LS_DIRECTORY_NAME
);
112 static bool From(char aData
) { return aData
== kLSPrefix
; }
115 template <typename T
>
116 bool TypeTo_impl(Client::Type aType
, T
& aData
) {
119 ClientTypeTraits
<Client::Type::IDB
>::To(aData
);
122 case Client::DOMCACHE
:
123 ClientTypeTraits
<Client::Type::DOMCACHE
>::To(aData
);
127 ClientTypeTraits
<Client::Type::SDB
>::To(aData
);
130 case Client::FILESYSTEM
:
131 ClientTypeTraits
<Client::Type::FILESYSTEM
>::To(aData
);
135 if (CachedNextGenLocalStorageEnabled()) {
136 ClientTypeTraits
<Client::Type::LS
>::To(aData
);
141 case Client::TYPE_MAX
:
146 MOZ_CRASH("Should never get here!");
149 template <typename T
>
150 bool TypeFrom_impl(const T
& aData
, Client::Type
& aType
) {
151 if (ClientTypeTraits
<Client::Type::IDB
>::From(aData
)) {
156 if (ClientTypeTraits
<Client::Type::DOMCACHE
>::From(aData
)) {
157 aType
= Client::DOMCACHE
;
161 if (ClientTypeTraits
<Client::Type::SDB
>::From(aData
)) {
166 if (ClientTypeTraits
<Client::Type::FILESYSTEM
>::From(aData
)) {
167 aType
= Client::FILESYSTEM
;
171 if (CachedNextGenLocalStorageEnabled() &&
172 ClientTypeTraits
<Client::Type::LS
>::From(aData
)) {
180 void BadType() { MOZ_CRASH("Bad client type value!"); }
185 bool Client::IsValidType(Type aType
) {
188 case Client::DOMCACHE
:
190 case Client::FILESYSTEM
:
194 if (CachedNextGenLocalStorageEnabled()) {
205 bool Client::TypeToText(Type aType
, nsAString
& aText
, const fallible_t
&) {
207 if (!TypeTo_impl(aType
, text
)) {
215 nsAutoString
Client::TypeToString(Type aType
) {
217 if (!TypeTo_impl(aType
, res
)) {
224 nsAutoCString
Client::TypeToText(Type aType
) {
226 if (!TypeTo_impl(aType
, res
)) {
233 bool Client::TypeFromText(const nsAString
& aText
, Type
& aType
,
236 if (!TypeFrom_impl(aText
, type
)) {
244 Client::Type
Client::TypeFromText(const nsACString
& aText
) {
246 if (!TypeFrom_impl(aText
, type
)) {
253 char Client::TypeToPrefix(Type aType
) {
255 if (!TypeTo_impl(aType
, prefix
)) {
262 bool Client::TypeFromPrefix(char aPrefix
, Type
& aType
, const fallible_t
&) {
264 if (!TypeFrom_impl(aPrefix
, type
)) {
271 bool Client::InitiateShutdownWorkThreads() {
272 AssertIsOnBackgroundThread();
274 QuotaManager::MaybeRecordQuotaClientShutdownStep(GetType(), "starting"_ns
);
278 return IsShutdownCompleted();
281 void Client::FinalizeShutdownWorkThreads() {
282 QuotaManager::MaybeRecordQuotaClientShutdownStep(GetType(), "completed"_ns
);
288 bool Client::IsShuttingDownOnBackgroundThread() {
289 MOZ_ASSERT(IsOnBackgroundThread());
290 return QuotaManager::IsShuttingDown();
294 bool Client::IsShuttingDownOnNonBackgroundThread() {
295 MOZ_ASSERT(!IsOnBackgroundThread());
296 return QuotaManager::IsShuttingDown();
299 } // namespace mozilla::dom::quota