|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=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 mozilla_dom_quota_quotaobject_h__ |
|
8 #define mozilla_dom_quota_quotaobject_h__ |
|
9 |
|
10 #include "mozilla/dom/quota/QuotaCommon.h" |
|
11 |
|
12 #include "nsDataHashtable.h" |
|
13 |
|
14 #include "PersistenceType.h" |
|
15 |
|
16 BEGIN_QUOTA_NAMESPACE |
|
17 |
|
18 class GroupInfo; |
|
19 class GroupInfoPair; |
|
20 class OriginInfo; |
|
21 class QuotaManager; |
|
22 |
|
23 class QuotaObject |
|
24 { |
|
25 friend class OriginInfo; |
|
26 friend class QuotaManager; |
|
27 |
|
28 public: |
|
29 void |
|
30 AddRef(); |
|
31 |
|
32 void |
|
33 Release(); |
|
34 |
|
35 void |
|
36 UpdateSize(int64_t aSize); |
|
37 |
|
38 bool |
|
39 MaybeAllocateMoreSpace(int64_t aOffset, int32_t aCount); |
|
40 |
|
41 private: |
|
42 QuotaObject(OriginInfo* aOriginInfo, const nsAString& aPath, int64_t aSize) |
|
43 : mOriginInfo(aOriginInfo), mPath(aPath), mSize(aSize) |
|
44 { |
|
45 MOZ_COUNT_CTOR(QuotaObject); |
|
46 } |
|
47 |
|
48 ~QuotaObject() |
|
49 { |
|
50 MOZ_COUNT_DTOR(QuotaObject); |
|
51 } |
|
52 |
|
53 already_AddRefed<QuotaObject> |
|
54 LockedAddRef() |
|
55 { |
|
56 AssertCurrentThreadOwnsQuotaMutex(); |
|
57 |
|
58 ++mRefCnt; |
|
59 |
|
60 nsRefPtr<QuotaObject> result = dont_AddRef(this); |
|
61 return result.forget(); |
|
62 } |
|
63 |
|
64 mozilla::ThreadSafeAutoRefCnt mRefCnt; |
|
65 |
|
66 OriginInfo* mOriginInfo; |
|
67 nsString mPath; |
|
68 int64_t mSize; |
|
69 }; |
|
70 |
|
71 class OriginInfo MOZ_FINAL |
|
72 { |
|
73 friend class GroupInfo; |
|
74 friend class QuotaManager; |
|
75 friend class QuotaObject; |
|
76 |
|
77 public: |
|
78 OriginInfo(GroupInfo* aGroupInfo, const nsACString& aOrigin, uint64_t aLimit, |
|
79 uint64_t aUsage, int64_t aAccessTime) |
|
80 : mGroupInfo(aGroupInfo), mOrigin(aOrigin), mLimit(aLimit), mUsage(aUsage), |
|
81 mAccessTime(aAccessTime) |
|
82 { |
|
83 MOZ_COUNT_CTOR(OriginInfo); |
|
84 } |
|
85 |
|
86 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OriginInfo) |
|
87 |
|
88 int64_t |
|
89 AccessTime() const |
|
90 { |
|
91 return mAccessTime; |
|
92 } |
|
93 |
|
94 private: |
|
95 // Private destructor, to discourage deletion outside of Release(): |
|
96 ~OriginInfo() |
|
97 { |
|
98 MOZ_COUNT_DTOR(OriginInfo); |
|
99 } |
|
100 |
|
101 void |
|
102 LockedDecreaseUsage(int64_t aSize); |
|
103 |
|
104 void |
|
105 LockedUpdateAccessTime(int64_t aAccessTime) |
|
106 { |
|
107 AssertCurrentThreadOwnsQuotaMutex(); |
|
108 |
|
109 mAccessTime = aAccessTime; |
|
110 } |
|
111 |
|
112 void |
|
113 LockedClearOriginInfos() |
|
114 { |
|
115 AssertCurrentThreadOwnsQuotaMutex(); |
|
116 |
|
117 mQuotaObjects.EnumerateRead(ClearOriginInfoCallback, nullptr); |
|
118 } |
|
119 |
|
120 static PLDHashOperator |
|
121 ClearOriginInfoCallback(const nsAString& aKey, |
|
122 QuotaObject* aValue, void* aUserArg); |
|
123 |
|
124 nsDataHashtable<nsStringHashKey, QuotaObject*> mQuotaObjects; |
|
125 |
|
126 GroupInfo* mGroupInfo; |
|
127 nsCString mOrigin; |
|
128 uint64_t mLimit; |
|
129 uint64_t mUsage; |
|
130 int64_t mAccessTime; |
|
131 }; |
|
132 |
|
133 class OriginInfoLRUComparator |
|
134 { |
|
135 public: |
|
136 bool |
|
137 Equals(const OriginInfo* a, const OriginInfo* b) const |
|
138 { |
|
139 return |
|
140 a && b ? a->AccessTime() == b->AccessTime() : !a && !b ? true : false; |
|
141 } |
|
142 |
|
143 bool |
|
144 LessThan(const OriginInfo* a, const OriginInfo* b) const |
|
145 { |
|
146 return a && b ? a->AccessTime() < b->AccessTime() : b ? true : false; |
|
147 } |
|
148 }; |
|
149 |
|
150 class GroupInfo MOZ_FINAL |
|
151 { |
|
152 friend class GroupInfoPair; |
|
153 friend class OriginInfo; |
|
154 friend class QuotaManager; |
|
155 friend class QuotaObject; |
|
156 |
|
157 public: |
|
158 GroupInfo(PersistenceType aPersistenceType, const nsACString& aGroup) |
|
159 : mPersistenceType(aPersistenceType), mGroup(aGroup), mUsage(0) |
|
160 { |
|
161 MOZ_COUNT_CTOR(GroupInfo); |
|
162 } |
|
163 |
|
164 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GroupInfo) |
|
165 |
|
166 bool |
|
167 IsForPersistentStorage() const |
|
168 { |
|
169 return mPersistenceType == PERSISTENCE_TYPE_PERSISTENT; |
|
170 } |
|
171 |
|
172 bool |
|
173 IsForTemporaryStorage() const |
|
174 { |
|
175 return mPersistenceType == PERSISTENCE_TYPE_TEMPORARY; |
|
176 } |
|
177 |
|
178 private: |
|
179 // Private destructor, to discourage deletion outside of Release(): |
|
180 ~GroupInfo() |
|
181 { |
|
182 MOZ_COUNT_DTOR(GroupInfo); |
|
183 } |
|
184 |
|
185 already_AddRefed<OriginInfo> |
|
186 LockedGetOriginInfo(const nsACString& aOrigin); |
|
187 |
|
188 void |
|
189 LockedAddOriginInfo(OriginInfo* aOriginInfo); |
|
190 |
|
191 void |
|
192 LockedRemoveOriginInfo(const nsACString& aOrigin); |
|
193 |
|
194 void |
|
195 LockedRemoveOriginInfos(); |
|
196 |
|
197 void |
|
198 LockedRemoveOriginInfosForPattern(const nsACString& aPattern); |
|
199 |
|
200 bool |
|
201 LockedHasOriginInfos() |
|
202 { |
|
203 AssertCurrentThreadOwnsQuotaMutex(); |
|
204 |
|
205 return !mOriginInfos.IsEmpty(); |
|
206 } |
|
207 |
|
208 nsTArray<nsRefPtr<OriginInfo> > mOriginInfos; |
|
209 |
|
210 PersistenceType mPersistenceType; |
|
211 nsCString mGroup; |
|
212 uint64_t mUsage; |
|
213 }; |
|
214 |
|
215 class GroupInfoPair |
|
216 { |
|
217 friend class QuotaManager; |
|
218 |
|
219 public: |
|
220 GroupInfoPair() |
|
221 { |
|
222 MOZ_COUNT_CTOR(GroupInfoPair); |
|
223 } |
|
224 |
|
225 ~GroupInfoPair() |
|
226 { |
|
227 MOZ_COUNT_DTOR(GroupInfoPair); |
|
228 } |
|
229 |
|
230 private: |
|
231 already_AddRefed<GroupInfo> |
|
232 LockedGetGroupInfo(PersistenceType aPersistenceType) |
|
233 { |
|
234 AssertCurrentThreadOwnsQuotaMutex(); |
|
235 |
|
236 nsRefPtr<GroupInfo> groupInfo = |
|
237 GetGroupInfoForPersistenceType(aPersistenceType); |
|
238 return groupInfo.forget(); |
|
239 } |
|
240 |
|
241 void |
|
242 LockedSetGroupInfo(GroupInfo* aGroupInfo) |
|
243 { |
|
244 AssertCurrentThreadOwnsQuotaMutex(); |
|
245 |
|
246 nsRefPtr<GroupInfo>& groupInfo = |
|
247 GetGroupInfoForPersistenceType(aGroupInfo->mPersistenceType); |
|
248 groupInfo = aGroupInfo; |
|
249 } |
|
250 |
|
251 void |
|
252 LockedClearGroupInfo(PersistenceType aPersistenceType) |
|
253 { |
|
254 AssertCurrentThreadOwnsQuotaMutex(); |
|
255 |
|
256 nsRefPtr<GroupInfo>& groupInfo = |
|
257 GetGroupInfoForPersistenceType(aPersistenceType); |
|
258 groupInfo = nullptr; |
|
259 } |
|
260 |
|
261 bool |
|
262 LockedHasGroupInfos() |
|
263 { |
|
264 AssertCurrentThreadOwnsQuotaMutex(); |
|
265 |
|
266 return mPersistentStorageGroupInfo || mTemporaryStorageGroupInfo; |
|
267 } |
|
268 |
|
269 nsRefPtr<GroupInfo>& |
|
270 GetGroupInfoForPersistenceType(PersistenceType aPersistenceType); |
|
271 |
|
272 nsRefPtr<GroupInfo> mPersistentStorageGroupInfo; |
|
273 nsRefPtr<GroupInfo> mTemporaryStorageGroupInfo; |
|
274 }; |
|
275 |
|
276 END_QUOTA_NAMESPACE |
|
277 |
|
278 #endif // mozilla_dom_quota_quotaobject_h__ |