1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/quota/QuotaObject.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,278 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_quota_quotaobject_h__ 1.11 +#define mozilla_dom_quota_quotaobject_h__ 1.12 + 1.13 +#include "mozilla/dom/quota/QuotaCommon.h" 1.14 + 1.15 +#include "nsDataHashtable.h" 1.16 + 1.17 +#include "PersistenceType.h" 1.18 + 1.19 +BEGIN_QUOTA_NAMESPACE 1.20 + 1.21 +class GroupInfo; 1.22 +class GroupInfoPair; 1.23 +class OriginInfo; 1.24 +class QuotaManager; 1.25 + 1.26 +class QuotaObject 1.27 +{ 1.28 + friend class OriginInfo; 1.29 + friend class QuotaManager; 1.30 + 1.31 +public: 1.32 + void 1.33 + AddRef(); 1.34 + 1.35 + void 1.36 + Release(); 1.37 + 1.38 + void 1.39 + UpdateSize(int64_t aSize); 1.40 + 1.41 + bool 1.42 + MaybeAllocateMoreSpace(int64_t aOffset, int32_t aCount); 1.43 + 1.44 +private: 1.45 + QuotaObject(OriginInfo* aOriginInfo, const nsAString& aPath, int64_t aSize) 1.46 + : mOriginInfo(aOriginInfo), mPath(aPath), mSize(aSize) 1.47 + { 1.48 + MOZ_COUNT_CTOR(QuotaObject); 1.49 + } 1.50 + 1.51 + ~QuotaObject() 1.52 + { 1.53 + MOZ_COUNT_DTOR(QuotaObject); 1.54 + } 1.55 + 1.56 + already_AddRefed<QuotaObject> 1.57 + LockedAddRef() 1.58 + { 1.59 + AssertCurrentThreadOwnsQuotaMutex(); 1.60 + 1.61 + ++mRefCnt; 1.62 + 1.63 + nsRefPtr<QuotaObject> result = dont_AddRef(this); 1.64 + return result.forget(); 1.65 + } 1.66 + 1.67 + mozilla::ThreadSafeAutoRefCnt mRefCnt; 1.68 + 1.69 + OriginInfo* mOriginInfo; 1.70 + nsString mPath; 1.71 + int64_t mSize; 1.72 +}; 1.73 + 1.74 +class OriginInfo MOZ_FINAL 1.75 +{ 1.76 + friend class GroupInfo; 1.77 + friend class QuotaManager; 1.78 + friend class QuotaObject; 1.79 + 1.80 +public: 1.81 + OriginInfo(GroupInfo* aGroupInfo, const nsACString& aOrigin, uint64_t aLimit, 1.82 + uint64_t aUsage, int64_t aAccessTime) 1.83 + : mGroupInfo(aGroupInfo), mOrigin(aOrigin), mLimit(aLimit), mUsage(aUsage), 1.84 + mAccessTime(aAccessTime) 1.85 + { 1.86 + MOZ_COUNT_CTOR(OriginInfo); 1.87 + } 1.88 + 1.89 + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OriginInfo) 1.90 + 1.91 + int64_t 1.92 + AccessTime() const 1.93 + { 1.94 + return mAccessTime; 1.95 + } 1.96 + 1.97 +private: 1.98 + // Private destructor, to discourage deletion outside of Release(): 1.99 + ~OriginInfo() 1.100 + { 1.101 + MOZ_COUNT_DTOR(OriginInfo); 1.102 + } 1.103 + 1.104 + void 1.105 + LockedDecreaseUsage(int64_t aSize); 1.106 + 1.107 + void 1.108 + LockedUpdateAccessTime(int64_t aAccessTime) 1.109 + { 1.110 + AssertCurrentThreadOwnsQuotaMutex(); 1.111 + 1.112 + mAccessTime = aAccessTime; 1.113 + } 1.114 + 1.115 + void 1.116 + LockedClearOriginInfos() 1.117 + { 1.118 + AssertCurrentThreadOwnsQuotaMutex(); 1.119 + 1.120 + mQuotaObjects.EnumerateRead(ClearOriginInfoCallback, nullptr); 1.121 + } 1.122 + 1.123 + static PLDHashOperator 1.124 + ClearOriginInfoCallback(const nsAString& aKey, 1.125 + QuotaObject* aValue, void* aUserArg); 1.126 + 1.127 + nsDataHashtable<nsStringHashKey, QuotaObject*> mQuotaObjects; 1.128 + 1.129 + GroupInfo* mGroupInfo; 1.130 + nsCString mOrigin; 1.131 + uint64_t mLimit; 1.132 + uint64_t mUsage; 1.133 + int64_t mAccessTime; 1.134 +}; 1.135 + 1.136 +class OriginInfoLRUComparator 1.137 +{ 1.138 +public: 1.139 + bool 1.140 + Equals(const OriginInfo* a, const OriginInfo* b) const 1.141 + { 1.142 + return 1.143 + a && b ? a->AccessTime() == b->AccessTime() : !a && !b ? true : false; 1.144 + } 1.145 + 1.146 + bool 1.147 + LessThan(const OriginInfo* a, const OriginInfo* b) const 1.148 + { 1.149 + return a && b ? a->AccessTime() < b->AccessTime() : b ? true : false; 1.150 + } 1.151 +}; 1.152 + 1.153 +class GroupInfo MOZ_FINAL 1.154 +{ 1.155 + friend class GroupInfoPair; 1.156 + friend class OriginInfo; 1.157 + friend class QuotaManager; 1.158 + friend class QuotaObject; 1.159 + 1.160 +public: 1.161 + GroupInfo(PersistenceType aPersistenceType, const nsACString& aGroup) 1.162 + : mPersistenceType(aPersistenceType), mGroup(aGroup), mUsage(0) 1.163 + { 1.164 + MOZ_COUNT_CTOR(GroupInfo); 1.165 + } 1.166 + 1.167 + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GroupInfo) 1.168 + 1.169 + bool 1.170 + IsForPersistentStorage() const 1.171 + { 1.172 + return mPersistenceType == PERSISTENCE_TYPE_PERSISTENT; 1.173 + } 1.174 + 1.175 + bool 1.176 + IsForTemporaryStorage() const 1.177 + { 1.178 + return mPersistenceType == PERSISTENCE_TYPE_TEMPORARY; 1.179 + } 1.180 + 1.181 +private: 1.182 + // Private destructor, to discourage deletion outside of Release(): 1.183 + ~GroupInfo() 1.184 + { 1.185 + MOZ_COUNT_DTOR(GroupInfo); 1.186 + } 1.187 + 1.188 + already_AddRefed<OriginInfo> 1.189 + LockedGetOriginInfo(const nsACString& aOrigin); 1.190 + 1.191 + void 1.192 + LockedAddOriginInfo(OriginInfo* aOriginInfo); 1.193 + 1.194 + void 1.195 + LockedRemoveOriginInfo(const nsACString& aOrigin); 1.196 + 1.197 + void 1.198 + LockedRemoveOriginInfos(); 1.199 + 1.200 + void 1.201 + LockedRemoveOriginInfosForPattern(const nsACString& aPattern); 1.202 + 1.203 + bool 1.204 + LockedHasOriginInfos() 1.205 + { 1.206 + AssertCurrentThreadOwnsQuotaMutex(); 1.207 + 1.208 + return !mOriginInfos.IsEmpty(); 1.209 + } 1.210 + 1.211 + nsTArray<nsRefPtr<OriginInfo> > mOriginInfos; 1.212 + 1.213 + PersistenceType mPersistenceType; 1.214 + nsCString mGroup; 1.215 + uint64_t mUsage; 1.216 +}; 1.217 + 1.218 +class GroupInfoPair 1.219 +{ 1.220 + friend class QuotaManager; 1.221 + 1.222 +public: 1.223 + GroupInfoPair() 1.224 + { 1.225 + MOZ_COUNT_CTOR(GroupInfoPair); 1.226 + } 1.227 + 1.228 + ~GroupInfoPair() 1.229 + { 1.230 + MOZ_COUNT_DTOR(GroupInfoPair); 1.231 + } 1.232 + 1.233 +private: 1.234 + already_AddRefed<GroupInfo> 1.235 + LockedGetGroupInfo(PersistenceType aPersistenceType) 1.236 + { 1.237 + AssertCurrentThreadOwnsQuotaMutex(); 1.238 + 1.239 + nsRefPtr<GroupInfo> groupInfo = 1.240 + GetGroupInfoForPersistenceType(aPersistenceType); 1.241 + return groupInfo.forget(); 1.242 + } 1.243 + 1.244 + void 1.245 + LockedSetGroupInfo(GroupInfo* aGroupInfo) 1.246 + { 1.247 + AssertCurrentThreadOwnsQuotaMutex(); 1.248 + 1.249 + nsRefPtr<GroupInfo>& groupInfo = 1.250 + GetGroupInfoForPersistenceType(aGroupInfo->mPersistenceType); 1.251 + groupInfo = aGroupInfo; 1.252 + } 1.253 + 1.254 + void 1.255 + LockedClearGroupInfo(PersistenceType aPersistenceType) 1.256 + { 1.257 + AssertCurrentThreadOwnsQuotaMutex(); 1.258 + 1.259 + nsRefPtr<GroupInfo>& groupInfo = 1.260 + GetGroupInfoForPersistenceType(aPersistenceType); 1.261 + groupInfo = nullptr; 1.262 + } 1.263 + 1.264 + bool 1.265 + LockedHasGroupInfos() 1.266 + { 1.267 + AssertCurrentThreadOwnsQuotaMutex(); 1.268 + 1.269 + return mPersistentStorageGroupInfo || mTemporaryStorageGroupInfo; 1.270 + } 1.271 + 1.272 + nsRefPtr<GroupInfo>& 1.273 + GetGroupInfoForPersistenceType(PersistenceType aPersistenceType); 1.274 + 1.275 + nsRefPtr<GroupInfo> mPersistentStorageGroupInfo; 1.276 + nsRefPtr<GroupInfo> mTemporaryStorageGroupInfo; 1.277 +}; 1.278 + 1.279 +END_QUOTA_NAMESPACE 1.280 + 1.281 +#endif // mozilla_dom_quota_quotaobject_h__