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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ClientUsageArray.h"
9 #include "mozilla/dom/quota/QuotaManager.h"
10 #include "mozilla/dom/quota/ResultExtensions.h"
12 namespace mozilla::dom::quota
{
14 void ClientUsageArray::Serialize(nsACString
& aText
) const {
15 QuotaManager
* quotaManager
= QuotaManager::Get();
16 MOZ_ASSERT(quotaManager
);
20 for (Client::Type type
: quotaManager
->AllClientTypes()) {
21 const Maybe
<uint64_t>& clientUsage
= ElementAt(type
);
22 if (clientUsage
.isSome()) {
29 aText
.Append(Client::TypeToPrefix(type
));
30 aText
.AppendInt(clientUsage
.value());
35 nsresult
ClientUsageArray::Deserialize(const nsACString
& aText
) {
36 for (const auto& token
:
37 nsCCharSeparatedTokenizerTemplate
<NS_TokenizerIgnoreNothing
>(aText
, ' ')
39 QM_TRY(OkIf(token
.Length() >= 2), NS_ERROR_FAILURE
);
41 Client::Type clientType
;
42 QM_TRY(OkIf(Client::TypeFromPrefix(token
.First(), clientType
, fallible
)),
46 const uint64_t usage
= Substring(token
, 1).ToInteger64(&rv
);
47 QM_TRY(MOZ_TO_RESULT(rv
));
49 ElementAt(clientType
) = Some(usage
);
55 } // namespace mozilla::dom::quota