Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / quota / UsageInfo.h
blob7f7bf5f85ae5e7d87b4260650fc52e818fcb2d11
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 #ifndef mozilla_dom_quota_usageinfo_h__
8 #define mozilla_dom_quota_usageinfo_h__
10 #include <cstdint>
11 #include <utility>
12 #include "mozilla/CheckedInt.h"
13 #include "mozilla/Maybe.h"
15 namespace mozilla::dom::quota {
17 enum struct UsageKind { Database, File };
19 namespace detail {
20 inline void AddCapped(Maybe<uint64_t>& aValue, const Maybe<uint64_t> aDelta) {
21 if (aDelta.isSome()) {
22 CheckedUint64 value = aValue.valueOr(0);
24 value += aDelta.value();
26 aValue = Some(value.isValid() ? value.value() : UINT64_MAX);
30 template <UsageKind Kind>
31 struct Usage {
32 explicit Usage(Maybe<uint64_t> aValue = Nothing{}) : mValue(aValue) {}
34 Maybe<uint64_t> GetValue() const { return mValue; }
36 Usage& operator+=(const Usage aDelta) {
37 AddCapped(mValue, aDelta.mValue);
39 return *this;
42 Usage operator+(const Usage aDelta) const {
43 Usage res = *this;
44 res += aDelta;
45 return res;
48 private:
49 Maybe<uint64_t> mValue;
51 } // namespace detail
53 using DatabaseUsageType = detail::Usage<UsageKind::Database>;
54 using FileUsageType = detail::Usage<UsageKind::File>;
56 class UsageInfo final {
57 public:
58 UsageInfo() = default;
60 explicit UsageInfo(const DatabaseUsageType aUsage) : mDatabaseUsage(aUsage) {}
62 explicit UsageInfo(const FileUsageType aUsage) : mFileUsage(aUsage) {}
64 UsageInfo operator+(const UsageInfo& aUsageInfo) {
65 UsageInfo res = *this;
66 res += aUsageInfo;
67 return res;
70 UsageInfo& operator+=(const UsageInfo& aUsageInfo) {
71 mDatabaseUsage += aUsageInfo.mDatabaseUsage;
72 mFileUsage += aUsageInfo.mFileUsage;
73 return *this;
76 UsageInfo& operator+=(const DatabaseUsageType aUsage) {
77 mDatabaseUsage += aUsage;
78 return *this;
81 UsageInfo& operator+=(const FileUsageType aUsage) {
82 mFileUsage += aUsage;
83 return *this;
86 Maybe<uint64_t> DatabaseUsage() const { return mDatabaseUsage.GetValue(); }
88 Maybe<uint64_t> FileUsage() const { return mFileUsage.GetValue(); }
90 Maybe<uint64_t> TotalUsage() const {
91 Maybe<uint64_t> res = mDatabaseUsage.GetValue();
92 detail::AddCapped(res, FileUsage());
93 return res;
96 private:
97 DatabaseUsageType mDatabaseUsage;
98 FileUsageType mFileUsage;
101 } // namespace mozilla::dom::quota
103 #endif // mozilla_dom_quota_usageinfo_h__