Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / toolkit / components / url-classifier / src / nsUrlClassifierUtils.h
blobb7c0db5860ccec14323a5a2aedf63b05ab03f0f3
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Url Classifier code
16 * The Initial Developer of the Original Code is
17 * Google Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifndef nsUrlClassifierUtils_h_
38 #define nsUrlClassifierUtils_h_
40 #include "nsAutoPtr.h"
41 #include "nsIUrlClassifierUtils.h"
42 #include "nsTArray.h"
43 #include "nsDataHashtable.h"
45 class nsUrlClassifierUtils : public nsIUrlClassifierUtils
47 private:
48 /**
49 * A fast, bit-vector map for ascii characters.
51 * Internally stores 256 bits in an array of 8 ints.
52 * Does quick bit-flicking to lookup needed characters.
54 class Charmap
56 public:
57 Charmap(PRUint32 b0, PRUint32 b1, PRUint32 b2, PRUint32 b3,
58 PRUint32 b4, PRUint32 b5, PRUint32 b6, PRUint32 b7)
60 mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3;
61 mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7;
64 /**
65 * Do a quick lookup to see if the letter is in the map.
67 PRBool Contains(unsigned char c) const
69 return mMap[c >> 5] & (1 << (c & 31));
72 private:
73 // Store the 256 bits in an 8 byte array.
74 PRUint32 mMap[8];
78 public:
79 nsUrlClassifierUtils();
80 ~nsUrlClassifierUtils() {}
82 NS_DECL_ISUPPORTS
83 NS_DECL_NSIURLCLASSIFIERUTILS
85 nsresult Init();
87 nsresult CanonicalizeHostname(const nsACString & hostname,
88 nsACString & _retval);
89 nsresult CanonicalizePath(const nsACString & url, nsACString & _retval);
91 // This function will encode all "special" characters in typical url encoding,
92 // that is %hh where h is a valid hex digit. The characters which are encoded
93 // by this function are any ascii characters under 32(control characters and
94 // space), 37(%), and anything 127 or above (special characters). Url is the
95 // string to encode, ret is the encoded string. Function returns true if
96 // ret != url.
97 PRBool SpecialEncode(const nsACString & url,
98 PRBool foldSlashes,
99 nsACString & _retval);
101 void ParseIPAddress(const nsACString & host, nsACString & _retval);
102 void CanonicalNum(const nsACString & num,
103 PRUint32 bytes,
104 PRBool allowOctal,
105 nsACString & _retval);
107 // Convert an urlsafe base64 string to a normal base64 string.
108 // This method will leave an already-normal base64 string alone.
109 static void UnUrlsafeBase64(nsACString & str);
111 // Takes an urlsafe-base64 encoded client key and gives back binary
112 // key data
113 static nsresult DecodeClientKey(const nsACString & clientKey,
114 nsACString & _retval);
115 private:
116 // Disallow copy constructor
117 nsUrlClassifierUtils(const nsUrlClassifierUtils&);
119 // Function to tell if we should encode a character.
120 PRBool ShouldURLEscape(const unsigned char c) const;
122 void CleanupHostname(const nsACString & host, nsACString & _retval);
124 nsAutoPtr<Charmap> mEscapeCharmap;
127 // An MRU list of fragments. This is used by the DB service to
128 // keep a set of known-clean fragments that don't need a database
129 // lookup.
130 class nsUrlClassifierFragmentSet
132 public:
133 nsUrlClassifierFragmentSet() : mFirst(nsnull), mLast(nsnull), mCapacity(16) {}
135 PRBool Init(PRUint32 maxEntries) {
136 mCapacity = maxEntries;
137 if (!mEntryStorage.SetCapacity(mCapacity))
138 return PR_FALSE;
140 if (!mEntries.Init())
141 return PR_FALSE;
143 return PR_TRUE;
146 PRBool Put(const nsACString &fragment) {
147 Entry *entry;
148 if (mEntries.Get(fragment, &entry)) {
149 // Remove this entry from the list, we'll add it back
150 // to the front.
151 UnlinkEntry(entry);
152 } else {
153 if (mEntryStorage.Length() < mEntryStorage.Capacity()) {
154 entry = mEntryStorage.AppendElement();
155 if (!entry)
156 return PR_FALSE;
157 } else {
158 // Reuse the oldest entry.
159 entry = mLast;
160 UnlinkEntry(entry);
161 mEntries.Remove(entry->mFragment);
163 entry->mFragment = fragment;
164 mEntries.Put(fragment, entry);
167 // Add the entry to the front of the list
168 entry->mPrev = nsnull;
169 entry->mNext = mFirst;
170 mFirst = entry;
171 if (!mLast) {
172 mLast = entry;
175 return PR_TRUE;
178 PRBool Has(const nsACString &fragment) {
179 return mEntries.Get(fragment, nsnull);
182 void Clear() {
183 mFirst = mLast = nsnull;
184 mEntries.Clear();
185 mEntryStorage.Clear();
186 mEntryStorage.SetCapacity(mCapacity);
189 private:
190 // One entry in the set. We maintain a doubly-linked list, with
191 // the most recently used entry at the front.
192 class Entry {
193 public:
194 Entry() : mNext(nsnull), mPrev(nsnull) {};
195 ~Entry() { }
197 Entry *mNext;
198 Entry *mPrev;
199 nsCString mFragment;
202 void UnlinkEntry(Entry *entry)
204 if (entry->mPrev)
205 entry->mPrev->mNext = entry->mNext;
206 else
207 mFirst = entry->mNext;
209 if (entry->mNext)
210 entry->mNext->mPrev = entry->mPrev;
211 else
212 mLast = entry->mPrev;
214 entry->mPrev = entry->mNext = nsnull;
217 // The newest entry in the cache.
218 Entry *mFirst;
219 // The oldest entry in the cache.
220 Entry *mLast;
222 // Max entries in the cache.
223 PRUint32 mCapacity;
225 // Storage for the entries in this set.
226 nsTArray<Entry> mEntryStorage;
228 // Entry lookup by fragment.
229 nsDataHashtable<nsCStringHashKey, Entry*> mEntries;
232 #endif // nsUrlClassifierUtils_h_