|
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_patternmatcher_h__ |
|
8 #define mozilla_dom_quota_patternmatcher_h__ |
|
9 |
|
10 #include "mozilla/dom/quota/QuotaCommon.h" |
|
11 |
|
12 #include "ArrayCluster.h" |
|
13 #include "Utilities.h" |
|
14 |
|
15 BEGIN_QUOTA_NAMESPACE |
|
16 |
|
17 template <class ValueType, class BaseType = ArrayCluster<nsIOfflineStorage*> > |
|
18 class StorageMatcher : public ValueType |
|
19 { |
|
20 typedef StorageMatcher<ValueType, BaseType> SelfType; |
|
21 |
|
22 struct Closure |
|
23 { |
|
24 Closure(SelfType& aSelf) |
|
25 : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(nullptr) |
|
26 { } |
|
27 |
|
28 Closure(SelfType& aSelf, const nsACString& aPattern) |
|
29 : mSelf(aSelf), mPattern(aPattern), mIndexes(nullptr) |
|
30 { } |
|
31 |
|
32 Closure(SelfType& aSelf, const nsTArray<uint32_t>* aIndexes) |
|
33 : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(aIndexes) |
|
34 { } |
|
35 |
|
36 Closure(SelfType& aSelf, const nsACString& aPattern, |
|
37 const nsTArray<uint32_t>* aIndexes) |
|
38 : mSelf(aSelf), mPattern(aPattern), mIndexes(aIndexes) |
|
39 { } |
|
40 |
|
41 SelfType& mSelf; |
|
42 const nsACString& mPattern; |
|
43 const nsTArray<uint32_t>* mIndexes; |
|
44 }; |
|
45 |
|
46 public: |
|
47 template <class T, class U, class V> |
|
48 void |
|
49 Find(const nsBaseHashtable<T, U, V>& aHashtable, |
|
50 const nsACString& aPattern) |
|
51 { |
|
52 SelfType::Clear(); |
|
53 |
|
54 Closure closure(*this, aPattern); |
|
55 aHashtable.EnumerateRead(SelfType::MatchPattern, &closure); |
|
56 } |
|
57 |
|
58 template <class T, class U, class V> |
|
59 void |
|
60 Find(const nsBaseHashtable<T, U, V>& aHashtable, |
|
61 const nsTArray<uint32_t>* aIndexes) |
|
62 { |
|
63 SelfType::Clear(); |
|
64 |
|
65 Closure closure(*this, aIndexes); |
|
66 aHashtable.EnumerateRead(SelfType::MatchIndexes, &closure); |
|
67 } |
|
68 |
|
69 template <class T, class U, class V> |
|
70 void |
|
71 Find(const nsBaseHashtable<T, U, V>& aHashtable, |
|
72 uint32_t aIndex) |
|
73 { |
|
74 nsAutoTArray<uint32_t, 1> indexes; |
|
75 indexes.AppendElement(aIndex); |
|
76 |
|
77 Find(aHashtable, &indexes); |
|
78 } |
|
79 |
|
80 template <class T, class U, class V> |
|
81 void |
|
82 Find(const nsBaseHashtable<T, U, V>& aHashtable, |
|
83 const nsACString& aPattern, |
|
84 const nsTArray<uint32_t>* aIndexes) |
|
85 { |
|
86 SelfType::Clear(); |
|
87 |
|
88 Closure closure(*this, aPattern, aIndexes); |
|
89 aHashtable.EnumerateRead(SelfType::MatchPatternAndIndexes, &closure); |
|
90 } |
|
91 |
|
92 template <class T, class U, class V> |
|
93 void |
|
94 Find(const nsBaseHashtable<T, U, V>& aHashtable, |
|
95 const nsACString& aPattern, |
|
96 uint32_t aIndex) |
|
97 { |
|
98 nsAutoTArray<uint32_t, 1> indexes; |
|
99 indexes.AppendElement(aIndex); |
|
100 |
|
101 Find(aHashtable, aPattern, &indexes); |
|
102 } |
|
103 |
|
104 template <class T, class U, class V> |
|
105 void |
|
106 Find(const nsBaseHashtable<T, U, V>& aHashtable) |
|
107 { |
|
108 SelfType::Clear(); |
|
109 |
|
110 Closure closure(*this); |
|
111 aHashtable.EnumerateRead(SelfType::MatchAll, &closure); |
|
112 } |
|
113 |
|
114 private: |
|
115 static PLDHashOperator |
|
116 MatchPattern(const nsACString& aKey, |
|
117 BaseType* aValue, |
|
118 void* aUserArg) |
|
119 { |
|
120 MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!"); |
|
121 MOZ_ASSERT(aValue, "Null pointer!"); |
|
122 MOZ_ASSERT(aUserArg, "Null pointer!"); |
|
123 |
|
124 Closure* closure = static_cast<Closure*>(aUserArg); |
|
125 |
|
126 if (PatternMatchesOrigin(closure->mPattern, aKey)) { |
|
127 aValue->AppendElementsTo(closure->mSelf); |
|
128 } |
|
129 |
|
130 return PL_DHASH_NEXT; |
|
131 } |
|
132 |
|
133 static PLDHashOperator |
|
134 MatchIndexes(const nsACString& aKey, |
|
135 BaseType* aValue, |
|
136 void* aUserArg) |
|
137 { |
|
138 MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!"); |
|
139 MOZ_ASSERT(aValue, "Null pointer!"); |
|
140 MOZ_ASSERT(aUserArg, "Null pointer!"); |
|
141 |
|
142 Closure* closure = static_cast<Closure*>(aUserArg); |
|
143 |
|
144 for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) { |
|
145 aValue->AppendElementsTo(closure->mIndexes->ElementAt(index), |
|
146 closure->mSelf); |
|
147 } |
|
148 |
|
149 return PL_DHASH_NEXT; |
|
150 } |
|
151 |
|
152 static PLDHashOperator |
|
153 MatchPatternAndIndexes(const nsACString& aKey, |
|
154 BaseType* aValue, |
|
155 void* aUserArg) |
|
156 { |
|
157 MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!"); |
|
158 MOZ_ASSERT(aValue, "Null pointer!"); |
|
159 MOZ_ASSERT(aUserArg, "Null pointer!"); |
|
160 |
|
161 Closure* closure = static_cast<Closure*>(aUserArg); |
|
162 |
|
163 if (PatternMatchesOrigin(closure->mPattern, aKey)) { |
|
164 for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) { |
|
165 aValue->AppendElementsTo(closure->mIndexes->ElementAt(index), |
|
166 closure->mSelf); |
|
167 } |
|
168 } |
|
169 |
|
170 return PL_DHASH_NEXT; |
|
171 } |
|
172 |
|
173 static PLDHashOperator |
|
174 MatchAll(const nsACString& aKey, |
|
175 BaseType* aValue, |
|
176 void* aUserArg) |
|
177 { |
|
178 MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!"); |
|
179 MOZ_ASSERT(aValue, "Null pointer!"); |
|
180 MOZ_ASSERT(aUserArg, "Null pointer!"); |
|
181 |
|
182 Closure* closure = static_cast<Closure*>(aUserArg); |
|
183 aValue->AppendElementsTo(closure->mSelf); |
|
184 |
|
185 return PL_DHASH_NEXT; |
|
186 } |
|
187 }; |
|
188 |
|
189 END_QUOTA_NAMESPACE |
|
190 |
|
191 #endif // mozilla_dom_quota_patternmatcher_h__ |