From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- dom/quota/UsageInfo.h | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 dom/quota/UsageInfo.h (limited to 'dom/quota/UsageInfo.h') diff --git a/dom/quota/UsageInfo.h b/dom/quota/UsageInfo.h new file mode 100644 index 0000000000..7f7bf5f85a --- /dev/null +++ b/dom/quota/UsageInfo.h @@ -0,0 +1,103 @@ +/* -*- 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/. */ + +#ifndef mozilla_dom_quota_usageinfo_h__ +#define mozilla_dom_quota_usageinfo_h__ + +#include +#include +#include "mozilla/CheckedInt.h" +#include "mozilla/Maybe.h" + +namespace mozilla::dom::quota { + +enum struct UsageKind { Database, File }; + +namespace detail { +inline void AddCapped(Maybe& aValue, const Maybe aDelta) { + if (aDelta.isSome()) { + CheckedUint64 value = aValue.valueOr(0); + + value += aDelta.value(); + + aValue = Some(value.isValid() ? value.value() : UINT64_MAX); + } +} + +template +struct Usage { + explicit Usage(Maybe aValue = Nothing{}) : mValue(aValue) {} + + Maybe GetValue() const { return mValue; } + + Usage& operator+=(const Usage aDelta) { + AddCapped(mValue, aDelta.mValue); + + return *this; + } + + Usage operator+(const Usage aDelta) const { + Usage res = *this; + res += aDelta; + return res; + } + + private: + Maybe mValue; +}; +} // namespace detail + +using DatabaseUsageType = detail::Usage; +using FileUsageType = detail::Usage; + +class UsageInfo final { + public: + UsageInfo() = default; + + explicit UsageInfo(const DatabaseUsageType aUsage) : mDatabaseUsage(aUsage) {} + + explicit UsageInfo(const FileUsageType aUsage) : mFileUsage(aUsage) {} + + UsageInfo operator+(const UsageInfo& aUsageInfo) { + UsageInfo res = *this; + res += aUsageInfo; + return res; + } + + UsageInfo& operator+=(const UsageInfo& aUsageInfo) { + mDatabaseUsage += aUsageInfo.mDatabaseUsage; + mFileUsage += aUsageInfo.mFileUsage; + return *this; + } + + UsageInfo& operator+=(const DatabaseUsageType aUsage) { + mDatabaseUsage += aUsage; + return *this; + } + + UsageInfo& operator+=(const FileUsageType aUsage) { + mFileUsage += aUsage; + return *this; + } + + Maybe DatabaseUsage() const { return mDatabaseUsage.GetValue(); } + + Maybe FileUsage() const { return mFileUsage.GetValue(); } + + Maybe TotalUsage() const { + Maybe res = mDatabaseUsage.GetValue(); + detail::AddCapped(res, FileUsage()); + return res; + } + + private: + DatabaseUsageType mDatabaseUsage; + FileUsageType mFileUsage; +}; + +} // namespace mozilla::dom::quota + +#endif // mozilla_dom_quota_usageinfo_h__ -- cgit v1.2.3