tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ClientUsageArray.cpp (1601B)


      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/. */
      6 
      7 #include "ClientUsageArray.h"
      8 
      9 #include "mozilla/dom/quota/QuotaManager.h"
     10 #include "mozilla/dom/quota/ResultExtensions.h"
     11 
     12 namespace mozilla::dom::quota {
     13 
     14 void ClientUsageArray::Serialize(nsACString& aText) const {
     15  QuotaManager* quotaManager = QuotaManager::Get();
     16  MOZ_ASSERT(quotaManager);
     17 
     18  bool first = true;
     19 
     20  for (Client::Type type : quotaManager->AllClientTypes()) {
     21    const Maybe<uint64_t>& clientUsage = (*this)[type];
     22    if (clientUsage.isSome()) {
     23      if (first) {
     24        first = false;
     25      } else {
     26        aText.Append(" ");
     27      }
     28 
     29      aText.Append(Client::TypeToPrefix(type));
     30      aText.AppendInt(clientUsage.value());
     31    }
     32  }
     33 }
     34 
     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);
     40 
     41    Client::Type clientType;
     42    QM_TRY(OkIf(Client::TypeFromPrefix(token.First(), clientType, fallible)),
     43           NS_ERROR_FAILURE);
     44 
     45    nsresult rv;
     46    const uint64_t usage = Substring(token, 1).ToInteger64(&rv);
     47    QM_TRY(MOZ_TO_RESULT(rv));
     48 
     49    (*this)[clientType] = Some(usage);
     50  }
     51 
     52  return NS_OK;
     53 }
     54 
     55 }  // namespace mozilla::dom::quota