Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / dom / quota / OriginInfo.h
blob2ce9c7ab831c01d5bec967327390d3bb4ebddb7a
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 DOM_QUOTA_ORIGININFO_H_
8 #define DOM_QUOTA_ORIGININFO_H_
10 #include "Assertions.h"
11 #include "ClientUsageArray.h"
12 #include "mozilla/dom/quota/QuotaManager.h"
14 namespace mozilla::dom::quota {
16 class CanonicalQuotaObject;
17 class GroupInfo;
19 class OriginInfo final {
20 friend class CanonicalQuotaObject;
21 friend class GroupInfo;
22 friend class QuotaManager;
24 public:
25 OriginInfo(GroupInfo* aGroupInfo, const nsACString& aOrigin,
26 const nsACString& aStorageOrigin, bool aIsPrivate,
27 const ClientUsageArray& aClientUsages, uint64_t aUsage,
28 int64_t aAccessTime, bool aPersisted, bool aDirectoryExists);
30 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OriginInfo)
32 GroupInfo* GetGroupInfo() const { return mGroupInfo; }
34 const nsCString& Origin() const { return mOrigin; }
36 int64_t LockedUsage() const {
37 AssertCurrentThreadOwnsQuotaMutex();
39 #ifdef DEBUG
40 QuotaManager* quotaManager = QuotaManager::Get();
41 MOZ_ASSERT(quotaManager);
43 uint64_t usage = 0;
44 for (Client::Type type : quotaManager->AllClientTypes()) {
45 AssertNoOverflow(usage, mClientUsages[type].valueOr(0));
46 usage += mClientUsages[type].valueOr(0);
48 MOZ_ASSERT(mUsage == usage);
49 #endif
51 return mUsage;
54 int64_t LockedAccessTime() const {
55 AssertCurrentThreadOwnsQuotaMutex();
57 return mAccessTime;
60 bool LockedPersisted() const {
61 AssertCurrentThreadOwnsQuotaMutex();
63 return mPersisted;
66 OriginMetadata FlattenToOriginMetadata() const;
68 FullOriginMetadata LockedFlattenToFullOriginMetadata() const;
70 nsresult LockedBindToStatement(mozIStorageStatement* aStatement) const;
72 private:
73 // Private destructor, to discourage deletion outside of Release():
74 ~OriginInfo() {
75 MOZ_COUNT_DTOR(OriginInfo);
77 MOZ_ASSERT(!mCanonicalQuotaObjects.Count());
80 void LockedDecreaseUsage(Client::Type aClientType, int64_t aSize);
82 void LockedResetUsageForClient(Client::Type aClientType);
84 UsageInfo LockedGetUsageForClient(Client::Type aClientType);
86 void LockedUpdateAccessTime(int64_t aAccessTime) {
87 AssertCurrentThreadOwnsQuotaMutex();
89 mAccessTime = aAccessTime;
90 if (!mAccessed) {
91 mAccessed = true;
95 void LockedPersist();
97 bool IsExtensionOrigin() { return mIsExtension; }
99 nsTHashMap<nsStringHashKey, NotNull<CanonicalQuotaObject*>>
100 mCanonicalQuotaObjects;
101 ClientUsageArray mClientUsages;
102 GroupInfo* mGroupInfo;
103 const nsCString mOrigin;
104 const nsCString mStorageOrigin;
105 bool mIsExtension;
106 uint64_t mUsage;
107 int64_t mAccessTime;
108 bool mIsPrivate;
109 bool mAccessed;
110 bool mPersisted;
112 * In some special cases like the LocalStorage client where it's possible to
113 * create a Quota-using representation but not actually write any data, we
114 * want to be able to track quota for an origin without creating its origin
115 * directory or the per-client files until they are actually needed to store
116 * data. In those cases, the OriginInfo will be created by
117 * EnsureQuotaForOrigin and the resulting mDirectoryExists will be false until
118 * the origin actually needs to be created. It is possible for mUsage to be
119 * greater than zero while mDirectoryExists is false, representing a state
120 * where a client like LocalStorage has reserved quota for disk writes, but
121 * has not yet flushed the data to disk.
123 bool mDirectoryExists;
126 class OriginInfoAccessTimeComparator {
127 public:
128 bool Equals(const NotNull<RefPtr<const OriginInfo>>& a,
129 const NotNull<RefPtr<const OriginInfo>>& b) const {
130 return a->LockedAccessTime() == b->LockedAccessTime();
133 bool LessThan(const NotNull<RefPtr<const OriginInfo>>& a,
134 const NotNull<RefPtr<const OriginInfo>>& b) const {
135 return a->LockedAccessTime() < b->LockedAccessTime();
139 } // namespace mozilla::dom::quota
141 #endif // DOM_QUOTA_ORIGININFO_H_