1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
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/
8 : *
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.
13 : *
14 : * The Original Code is Url Classifier code
15 : *
16 : * The Initial Developer of the Original Code is
17 : * the Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Dave Camp <dcamp@mozilla.com>
23 : * Gian-Carlo Pascutto <gpascutto@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef HashStore_h__
40 : #define HashStore_h__
41 :
42 : #include "Entries.h"
43 : #include "ChunkSet.h"
44 :
45 : #include "nsString.h"
46 : #include "nsTArray.h"
47 : #include "nsIFile.h"
48 : #include "nsIFileStreams.h"
49 : #include "nsCOMPtr.h"
50 :
51 : namespace mozilla {
52 : namespace safebrowsing {
53 :
54 132 : class TableUpdate {
55 : public:
56 133 : TableUpdate(const nsACString& aTable)
57 133 : : mTable(aTable), mLocalUpdate(false) {}
58 458 : const nsCString& TableName() const { return mTable; }
59 :
60 127 : bool Empty() const {
61 127 : return mAddChunks.Length() == 0 &&
62 24 : mSubChunks.Length() == 0 &&
63 9 : mAddExpirations.Length() == 0 &&
64 7 : mSubExpirations.Length() == 0 &&
65 5 : mAddPrefixes.Length() == 0 &&
66 5 : mSubPrefixes.Length() == 0 &&
67 5 : mAddCompletes.Length() == 0 &&
68 182 : mSubCompletes.Length() == 0;
69 : }
70 :
71 113 : void NewAddChunk(PRUint32 aChunk) { mAddChunks.Set(aChunk); }
72 19 : void NewSubChunk(PRUint32 aChunk) { mSubChunks.Set(aChunk); }
73 :
74 9 : void NewAddExpiration(PRUint32 aChunk) { mAddExpirations.Set(aChunk); }
75 6 : void NewSubExpiration(PRUint32 aChunk) { mSubExpirations.Set(aChunk); }
76 :
77 : void NewAddPrefix(PRUint32 aAddChunk, const Prefix& aPrefix);
78 : void NewSubPrefix(PRUint32 aAddChunk, const Prefix& aPprefix, PRUint32 aSubChunk);
79 : void NewAddComplete(PRUint32 aChunk, const Completion& aCompletion);
80 : void NewSubComplete(PRUint32 aAddChunk, const Completion& aCompletion,
81 : PRUint32 aSubChunk);
82 46 : void SetLocalUpdate(void) { mLocalUpdate = true; };
83 122 : bool IsLocalUpdate(void) { return mLocalUpdate; };
84 :
85 244 : ChunkSet& AddChunks() { return mAddChunks; }
86 244 : ChunkSet& SubChunks() { return mSubChunks; }
87 :
88 122 : ChunkSet& AddExpirations() { return mAddExpirations; }
89 122 : ChunkSet& SubExpirations() { return mSubExpirations; }
90 :
91 122 : AddPrefixArray& AddPrefixes() { return mAddPrefixes; }
92 122 : SubPrefixArray& SubPrefixes() { return mSubPrefixes; }
93 122 : AddCompleteArray& AddCompletes() { return mAddCompletes; }
94 122 : SubCompleteArray& SubCompletes() { return mSubCompletes; }
95 :
96 : private:
97 : nsCString mTable;
98 : // Update not from the remote server (no freshness)
99 : bool mLocalUpdate;
100 :
101 : ChunkSet mAddChunks;
102 : ChunkSet mSubChunks;
103 : ChunkSet mAddExpirations;
104 : ChunkSet mSubExpirations;
105 : AddPrefixArray mAddPrefixes;
106 : SubPrefixArray mSubPrefixes;
107 : AddCompleteArray mAddCompletes;
108 : SubCompleteArray mSubCompletes;
109 : };
110 :
111 : class HashStore {
112 : public:
113 : HashStore(const nsACString& aTableName, nsIFile* aStoreFile);
114 : ~HashStore();
115 :
116 670 : const nsCString& TableName() const { return mTableName; };
117 :
118 : nsresult Open();
119 : nsresult AugmentAdds(const nsTArray<PRUint32>& aPrefixes);
120 :
121 147 : ChunkSet& AddChunks() { return mAddChunks; }
122 147 : ChunkSet& SubChunks() { return mSubChunks; }
123 97 : AddPrefixArray& AddPrefixes() { return mAddPrefixes; }
124 97 : AddCompleteArray& AddCompletes() { return mAddCompletes; }
125 0 : SubPrefixArray& SubPrefixes() { return mSubPrefixes; }
126 0 : SubCompleteArray& SubCompletes() { return mSubCompletes; }
127 :
128 : // =======
129 : // Updates
130 : // =======
131 : // Begin the update process. Reads the store into memory.
132 : nsresult BeginUpdate();
133 :
134 : // Imports the data from a TableUpdate.
135 : nsresult ApplyUpdate(TableUpdate &aUpdate);
136 :
137 : // Process expired chunks
138 : nsresult Expire();
139 :
140 : // Rebuild the store, Incorporating all the applied updates.
141 : nsresult Rebuild();
142 :
143 : // Write the current state of the store to disk.
144 : // If you call between ApplyUpdate() and Rebuild(), you'll
145 : // have a mess on your hands.
146 : nsresult WriteFile();
147 :
148 : // Drop memory used during the update process.
149 : nsresult FinishUpdate();
150 :
151 : // Force the entire store in memory
152 : nsresult ReadEntireStore();
153 :
154 : private:
155 : void Clear();
156 : nsresult Reset();
157 :
158 : nsresult ReadHeader();
159 : nsresult SanityCheck(nsIFile* aStoreFile);
160 : nsresult CalculateChecksum(nsCAutoString& aChecksum, bool aChecksumPresent);
161 : nsresult CheckChecksum(nsIFile* aStoreFile);
162 : void UpdateHeader();
163 :
164 : nsresult EnsureChunkNumbers();
165 : nsresult ReadChunkNumbers();
166 : nsresult ReadHashes();
167 : nsresult ReadAddPrefixes();
168 : nsresult ReadSubPrefixes();
169 :
170 : nsresult WriteAddPrefixes(nsIOutputStream* aOut);
171 : nsresult WriteSubPrefixes(nsIOutputStream* aOut);
172 :
173 : nsresult ProcessSubs();
174 :
175 : struct Header {
176 : uint32 magic;
177 : uint32 version;
178 : uint32 numAddChunks;
179 : uint32 numSubChunks;
180 : uint32 numAddPrefixes;
181 : uint32 numSubPrefixes;
182 : uint32 numAddCompletes;
183 : uint32 numSubCompletes;
184 : };
185 :
186 : Header mHeader;
187 :
188 : nsCString mTableName;
189 : nsCOMPtr<nsIFile> mStoreDirectory;
190 :
191 : bool mInUpdate;
192 :
193 : nsCOMPtr<nsIInputStream> mInputStream;
194 :
195 : bool haveChunks;
196 : ChunkSet mAddChunks;
197 : ChunkSet mSubChunks;
198 :
199 : ChunkSet mAddExpirations;
200 : ChunkSet mSubExpirations;
201 :
202 : AddPrefixArray mAddPrefixes;
203 : AddCompleteArray mAddCompletes;
204 : SubPrefixArray mSubPrefixes;
205 : SubCompleteArray mSubCompletes;
206 : };
207 :
208 : }
209 : }
210 :
211 : #endif
|