OriginInfo.h (5043B)
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/. */ 6 7 #ifndef DOM_QUOTA_ORIGININFO_H_ 8 #define DOM_QUOTA_ORIGININFO_H_ 9 10 #include "Assertions.h" 11 #include "ClientUsageArray.h" 12 #include "mozilla/dom/quota/QuotaManager.h" 13 14 namespace mozilla::dom::quota { 15 16 class CanonicalQuotaObject; 17 class GroupInfo; 18 19 class OriginInfo final { 20 friend class CanonicalQuotaObject; 21 friend class GroupInfo; 22 friend class PersistOp; 23 friend class QuotaManager; 24 25 public: 26 OriginInfo(GroupInfo* aGroupInfo, const nsACString& aOrigin, 27 const nsACString& aStorageOrigin, bool aIsPrivate, 28 const ClientUsageArray& aClientUsages, uint64_t aUsage, 29 int64_t aAccessTime, int32_t aMaintenanceDate, bool aPersisted, 30 bool aDirectoryExists); 31 32 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OriginInfo) 33 34 GroupInfo* GetGroupInfo() const { return mGroupInfo; } 35 36 const nsCString& Origin() const { return mOrigin; } 37 38 int64_t LockedUsage() const { 39 AssertCurrentThreadOwnsQuotaMutex(); 40 41 #ifdef DEBUG 42 QuotaManager* quotaManager = QuotaManager::Get(); 43 MOZ_ASSERT(quotaManager); 44 45 uint64_t usage = 0; 46 for (Client::Type type : quotaManager->AllClientTypes()) { 47 AssertNoOverflow(usage, mClientUsages[type].valueOr(0)); 48 usage += mClientUsages[type].valueOr(0); 49 } 50 MOZ_ASSERT(mUsage == usage); 51 #endif 52 53 return mUsage; 54 } 55 56 int64_t LockedAccessTime() const { 57 AssertCurrentThreadOwnsQuotaMutex(); 58 59 return mAccessTime; 60 } 61 62 int32_t LockedMaintenanceDate() const { 63 AssertCurrentThreadOwnsQuotaMutex(); 64 65 return mMaintenanceDate; 66 } 67 68 bool LockedAccessed() const { 69 AssertCurrentThreadOwnsQuotaMutex(); 70 71 return mAccessed; 72 } 73 74 bool LockedPersisted() const { 75 AssertCurrentThreadOwnsQuotaMutex(); 76 77 return mPersisted; 78 } 79 80 bool IsExtensionOrigin() const { return mIsExtension; } 81 82 bool LockedDirectoryExists() const { 83 AssertCurrentThreadOwnsQuotaMutex(); 84 85 return mDirectoryExists; 86 } 87 88 OriginMetadata FlattenToOriginMetadata() const; 89 90 OriginStateMetadata LockedFlattenToOriginStateMetadata() const; 91 92 FullOriginMetadata LockedFlattenToFullOriginMetadata() const; 93 94 nsresult LockedBindToStatement(mozIStorageStatement* aStatement) const; 95 96 private: 97 // Private destructor, to discourage deletion outside of Release(): 98 ~OriginInfo() { 99 MOZ_COUNT_DTOR(OriginInfo); 100 101 MOZ_ASSERT(!mCanonicalQuotaObjects.Count()); 102 } 103 104 void LockedDecreaseUsage(Client::Type aClientType, int64_t aSize); 105 106 void LockedResetUsageForClient(Client::Type aClientType); 107 108 UsageInfo LockedGetUsageForClient(Client::Type aClientType); 109 110 void LockedUpdateAccessTime(int64_t aAccessTime) { 111 AssertCurrentThreadOwnsQuotaMutex(); 112 113 mAccessTime = aAccessTime; 114 if (!mAccessed) { 115 mAccessed = true; 116 } 117 } 118 119 void LockedUpdateMaintenanceDate(int32_t aMaintenanceDate) { 120 AssertCurrentThreadOwnsQuotaMutex(); 121 122 mMaintenanceDate = aMaintenanceDate; 123 } 124 125 void LockedUpdateAccessed() { 126 AssertCurrentThreadOwnsQuotaMutex(); 127 128 if (!mAccessed) { 129 mAccessed = true; 130 } 131 } 132 133 void LockedPersist(); 134 135 void LockedDirectoryCreated(); 136 137 nsTHashMap<nsStringHashKey, NotNull<CanonicalQuotaObject*>> 138 mCanonicalQuotaObjects; 139 ClientUsageArray mClientUsages; 140 GroupInfo* mGroupInfo; 141 const nsCString mOrigin; 142 const nsCString mStorageOrigin; 143 uint64_t mUsage; 144 int64_t mAccessTime; 145 int32_t mMaintenanceDate; 146 bool mIsPrivate; 147 bool mAccessed; 148 bool mPersisted; 149 const bool mIsExtension; 150 /** 151 * In some special cases like the LocalStorage client where it's possible to 152 * create a Quota-using representation but not actually write any data, we 153 * want to be able to track quota for an origin without creating its origin 154 * directory or the per-client files until they are actually needed to store 155 * data. In those cases, the OriginInfo will be created by 156 * InitQuotaForOrigin and the resulting mDirectoryExists will be false until 157 * the origin actually needs to be created. It is possible for mUsage to be 158 * greater than zero while mDirectoryExists is false, representing a state 159 * where a client like LocalStorage has reserved quota for disk writes, but 160 * has not yet flushed the data to disk. 161 */ 162 bool mDirectoryExists; 163 }; 164 165 class OriginInfoAccessTimeComparator { 166 public: 167 bool Equals(const NotNull<RefPtr<const OriginInfo>>& a, 168 const NotNull<RefPtr<const OriginInfo>>& b) const { 169 return a->LockedAccessTime() == b->LockedAccessTime(); 170 } 171 172 bool LessThan(const NotNull<RefPtr<const OriginInfo>>& a, 173 const NotNull<RefPtr<const OriginInfo>>& b) const { 174 return a->LockedAccessTime() < b->LockedAccessTime(); 175 } 176 }; 177 178 } // namespace mozilla::dom::quota 179 180 #endif // DOM_QUOTA_ORIGININFO_H_