Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / dom / quota / ClientUsageArray.cpp
blobf0c8095b1c1b16709eca456373e35501665e6d1a
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);
18 bool first = true;
20 for (Client::Type type : quotaManager->AllClientTypes()) {
21 const Maybe<uint64_t>& clientUsage = ElementAt(type);
22 if (clientUsage.isSome()) {
23 if (first) {
24 first = false;
25 } else {
26 aText.Append(" ");
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, ' ')
38 .ToRange()) {
39 QM_TRY(OkIf(token.Length() >= 2), NS_ERROR_FAILURE);
41 Client::Type clientType;
42 QM_TRY(OkIf(Client::TypeFromPrefix(token.First(), clientType, fallible)),
43 NS_ERROR_FAILURE);
45 nsresult rv;
46 const uint64_t usage = Substring(token, 1).ToInteger64(&rv);
47 QM_TRY(MOZ_TO_RESULT(rv));
49 ElementAt(clientType) = Some(usage);
52 return NS_OK;
55 } // namespace mozilla::dom::quota