summaryrefslogtreecommitdiffstats
path: root/dom/quota/UsageInfo.h
blob: 7f7bf5f85ae5e7d87b4260650fc52e818fcb2d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 <cstdint>
#include <utility>
#include "mozilla/CheckedInt.h"
#include "mozilla/Maybe.h"

namespace mozilla::dom::quota {

enum struct UsageKind { Database, File };

namespace detail {
inline void AddCapped(Maybe<uint64_t>& aValue, const Maybe<uint64_t> aDelta) {
  if (aDelta.isSome()) {
    CheckedUint64 value = aValue.valueOr(0);

    value += aDelta.value();

    aValue = Some(value.isValid() ? value.value() : UINT64_MAX);
  }
}

template <UsageKind Kind>
struct Usage {
  explicit Usage(Maybe<uint64_t> aValue = Nothing{}) : mValue(aValue) {}

  Maybe<uint64_t> 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<uint64_t> mValue;
};
}  // namespace detail

using DatabaseUsageType = detail::Usage<UsageKind::Database>;
using FileUsageType = detail::Usage<UsageKind::File>;

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<uint64_t> DatabaseUsage() const { return mDatabaseUsage.GetValue(); }

  Maybe<uint64_t> FileUsage() const { return mFileUsage.GetValue(); }

  Maybe<uint64_t> TotalUsage() const {
    Maybe<uint64_t> res = mDatabaseUsage.GetValue();
    detail::AddCapped(res, FileUsage());
    return res;
  }

 private:
  DatabaseUsageType mDatabaseUsage;
  FileUsageType mFileUsage;
};

}  // namespace mozilla::dom::quota

#endif  // mozilla_dom_quota_usageinfo_h__