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__
12 #include "mozilla/CheckedInt.h"
13 #include "mozilla/Maybe.h"
15 namespace mozilla::dom::quota
{
17 enum struct UsageKind
{ Database
, File
};
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
>
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
);
42 Usage
operator+(const Usage aDelta
) const {
49 Maybe
<uint64_t> mValue
;
53 using DatabaseUsageType
= detail::Usage
<UsageKind::Database
>;
54 using FileUsageType
= detail::Usage
<UsageKind::File
>;
56 class UsageInfo final
{
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;
70 UsageInfo
& operator+=(const UsageInfo
& aUsageInfo
) {
71 mDatabaseUsage
+= aUsageInfo
.mDatabaseUsage
;
72 mFileUsage
+= aUsageInfo
.mFileUsage
;
76 UsageInfo
& operator+=(const DatabaseUsageType aUsage
) {
77 mDatabaseUsage
+= aUsage
;
81 UsageInfo
& operator+=(const FileUsageType aUsage
) {
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());
97 DatabaseUsageType mDatabaseUsage
;
98 FileUsageType mFileUsage
;
101 } // namespace mozilla::dom::quota
103 #endif // mozilla_dom_quota_usageinfo_h__