OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // if (db.UpdateStarted(&lists)) { | 126 // if (db.UpdateStarted(&lists)) { |
127 // // Do something with |lists|. | 127 // // Do something with |lists|. |
128 // | 128 // |
129 // // Process add/sub commands. | 129 // // Process add/sub commands. |
130 // db.InsertChunks(list_name, chunks); | 130 // db.InsertChunks(list_name, chunks); |
131 // | 131 // |
132 // // Process adddel/subdel commands. | 132 // // Process adddel/subdel commands. |
133 // db.DeleteChunks(chunks_deletes); | 133 // db.DeleteChunks(chunks_deletes); |
134 // | 134 // |
135 // // If passed true, processes the collected chunk info and | 135 // // If passed true, processes the collected chunk info and |
136 // // rebuilds the bloom filter. If passed false, rolls everything | 136 // // rebuilds the filter. If passed false, rolls everything |
137 // // back. | 137 // // back. |
138 // db.UpdateFinished(success); | 138 // db.UpdateFinished(success); |
139 // } | 139 // } |
140 // | 140 // |
141 // If UpdateStarted() returns true, the caller MUST eventually call | 141 // If UpdateStarted() returns true, the caller MUST eventually call |
142 // UpdateFinished(). If it returns false, the caller MUST NOT call | 142 // UpdateFinished(). If it returns false, the caller MUST NOT call |
143 // the other functions. | 143 // the other functions. |
144 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; | 144 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) = 0; |
145 virtual void InsertChunks(const std::string& list_name, | 145 virtual void InsertChunks(const std::string& list_name, |
146 const SBChunkList& chunks) = 0; | 146 const SBChunkList& chunks) = 0; |
147 virtual void DeleteChunks( | 147 virtual void DeleteChunks( |
148 const std::vector<SBChunkDelete>& chunk_deletes) = 0; | 148 const std::vector<SBChunkDelete>& chunk_deletes) = 0; |
149 virtual void UpdateFinished(bool update_succeeded) = 0; | 149 virtual void UpdateFinished(bool update_succeeded) = 0; |
150 | 150 |
151 // Store the results of a GetHash response. In the case of empty results, we | 151 // Store the results of a GetHash response. In the case of empty results, we |
152 // cache the prefixes until the next update so that we don't have to issue | 152 // cache the prefixes until the next update so that we don't have to issue |
153 // further GetHash requests we know will be empty. | 153 // further GetHash requests we know will be empty. |
154 virtual void CacheHashResults( | 154 virtual void CacheHashResults( |
155 const std::vector<SBPrefix>& prefixes, | 155 const std::vector<SBPrefix>& prefixes, |
156 const std::vector<SBFullHashResult>& full_hits) = 0; | 156 const std::vector<SBFullHashResult>& full_hits) = 0; |
157 | 157 |
158 // The name of the bloom-filter file for the given database file. | 158 // The name of the bloom-filter file for the given database file. |
159 static FilePath BloomFilterForFilename(const FilePath& db_filename); | 159 static FilePath BloomFilterForFilename(const FilePath& db_filename); |
160 | 160 |
| 161 // The name of the prefix set file for the given database file. |
| 162 static FilePath PrefixSetForFilename(const FilePath& db_filename); |
| 163 |
161 // Filename for malware and phishing URL database. | 164 // Filename for malware and phishing URL database. |
162 static FilePath BrowseDBFilename(const FilePath& db_base_filename); | 165 static FilePath BrowseDBFilename(const FilePath& db_base_filename); |
163 | 166 |
164 // Filename for download URL and download binary hash database. | 167 // Filename for download URL and download binary hash database. |
165 static FilePath DownloadDBFilename(const FilePath& db_base_filename); | 168 static FilePath DownloadDBFilename(const FilePath& db_base_filename); |
166 | 169 |
167 // Filename for client-side phishing detection whitelist databsae. | 170 // Filename for client-side phishing detection whitelist databsae. |
168 static FilePath CsdWhitelistDBFilename( | 171 static FilePath CsdWhitelistDBFilename( |
169 const FilePath& csd_whitelist_base_filename); | 172 const FilePath& csd_whitelist_base_filename); |
170 | 173 |
(...skipping 11 matching lines...) Expand all Loading... |
182 FAILURE_DATABASE_FILTER_MISSING, | 185 FAILURE_DATABASE_FILTER_MISSING, |
183 FAILURE_DATABASE_FILTER_READ, | 186 FAILURE_DATABASE_FILTER_READ, |
184 FAILURE_DATABASE_FILTER_WRITE, | 187 FAILURE_DATABASE_FILTER_WRITE, |
185 FAILURE_DATABASE_FILTER_DELETE, | 188 FAILURE_DATABASE_FILTER_DELETE, |
186 FAILURE_DATABASE_STORE_MISSING, | 189 FAILURE_DATABASE_STORE_MISSING, |
187 FAILURE_DATABASE_STORE_DELETE, | 190 FAILURE_DATABASE_STORE_DELETE, |
188 FAILURE_DOWNLOAD_DATABASE_UPDATE_BEGIN, | 191 FAILURE_DOWNLOAD_DATABASE_UPDATE_BEGIN, |
189 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH, | 192 FAILURE_DOWNLOAD_DATABASE_UPDATE_FINISH, |
190 FAILURE_WHITELIST_DATABASE_UPDATE_BEGIN, | 193 FAILURE_WHITELIST_DATABASE_UPDATE_BEGIN, |
191 FAILURE_WHITELIST_DATABASE_UPDATE_FINISH, | 194 FAILURE_WHITELIST_DATABASE_UPDATE_FINISH, |
| 195 FAILURE_DATABASE_PREFIX_SET_MISSING, |
| 196 FAILURE_DATABASE_PREFIX_SET_READ, |
| 197 FAILURE_DATABASE_PREFIX_SET_WRITE, |
| 198 FAILURE_DATABASE_PREFIX_SET_DELETE, |
| 199 |
192 // Memory space for histograms is determined by the max. ALWAYS | 200 // Memory space for histograms is determined by the max. ALWAYS |
193 // ADD NEW VALUES BEFORE THIS ONE. | 201 // ADD NEW VALUES BEFORE THIS ONE. |
194 FAILURE_DATABASE_MAX | 202 FAILURE_DATABASE_MAX |
195 }; | 203 }; |
196 | 204 |
197 static void RecordFailure(FailureType failure_type); | 205 static void RecordFailure(FailureType failure_type); |
198 | 206 |
199 private: | 207 private: |
200 // The factory used to instantiate a SafeBrowsingDatabase object. | 208 // The factory used to instantiate a SafeBrowsingDatabase object. |
201 // Useful for tests, so they can provide their own implementation of | 209 // Useful for tests, so they can provide their own implementation of |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 bool ContainsWhitelistedHashes(const SBWhitelist& whitelist, | 268 bool ContainsWhitelistedHashes(const SBWhitelist& whitelist, |
261 const std::vector<SBFullHash>& hashes); | 269 const std::vector<SBFullHash>& hashes); |
262 | 270 |
263 // Return the browse_store_, download_store_, download_whitelist_store or | 271 // Return the browse_store_, download_store_, download_whitelist_store or |
264 // csd_whitelist_store_ based on list_id. | 272 // csd_whitelist_store_ based on list_id. |
265 SafeBrowsingStore* GetStore(int list_id); | 273 SafeBrowsingStore* GetStore(int list_id); |
266 | 274 |
267 // Deletes the files on disk. | 275 // Deletes the files on disk. |
268 bool Delete(); | 276 bool Delete(); |
269 | 277 |
270 // Load the bloom filter off disk, or generates one if it doesn't exist. | 278 // Load the prefix set or bloom filter off disk, if available. |
271 void LoadBloomFilter(); | 279 void LoadBloomFilterOrPrefixSet(); |
272 | 280 |
273 // Writes the current bloom filter to disk. | 281 // Writes the current prefix set to disk. |
274 void WriteBloomFilter(); | 282 void WritePrefixSet(); |
275 | 283 |
276 // Loads the given full-length hashes to the given whitelist. If the number | 284 // Loads the given full-length hashes to the given whitelist. If the number |
277 // of hashes is too large or if the kill switch URL is on the whitelist | 285 // of hashes is too large or if the kill switch URL is on the whitelist |
278 // we will whitelist everything. | 286 // we will whitelist everything. |
279 void LoadWhitelist(const std::vector<SBAddFullHash>& full_hashes, | 287 void LoadWhitelist(const std::vector<SBAddFullHash>& full_hashes, |
280 SBWhitelist* whitelist); | 288 SBWhitelist* whitelist); |
281 | 289 |
282 // Call this method if an error occured with the given whitelist. This will | 290 // Call this method if an error occured with the given whitelist. This will |
283 // result in all lookups to the whitelist to return true. | 291 // result in all lookups to the whitelist to return true. |
284 void WhitelistEverything(SBWhitelist* whitelist); | 292 void WhitelistEverything(SBWhitelist* whitelist); |
(...skipping 27 matching lines...) Expand all Loading... |
312 // matching prefixes. | 320 // matching prefixes. |
313 bool MatchDownloadAddPrefixes(int list_bit, | 321 bool MatchDownloadAddPrefixes(int list_bit, |
314 const std::vector<SBPrefix>& prefixes, | 322 const std::vector<SBPrefix>& prefixes, |
315 std::vector<SBPrefix>* prefix_hits); | 323 std::vector<SBPrefix>* prefix_hits); |
316 | 324 |
317 // Used to verify that various calls are made from the thread the | 325 // Used to verify that various calls are made from the thread the |
318 // object was created on. | 326 // object was created on. |
319 MessageLoop* creation_loop_; | 327 MessageLoop* creation_loop_; |
320 | 328 |
321 // Lock for protecting access to variables that may be used on the | 329 // Lock for protecting access to variables that may be used on the |
322 // IO thread. This includes |browse_bloom_filter_|, |full_browse_hashes_|, | 330 // IO thread. This includes |browse_bloom_filter_|, |prefix_set_|, |
323 // |pending_browse_hashes_|, |prefix_miss_cache_|, |csd_whitelist_|, and | 331 // |full_browse_hashes_|, |pending_browse_hashes_|, |
324 // |csd_whitelist_all_urls_|. | 332 // |prefix_miss_cache_|, |csd_whitelist_|, and |csd_whitelist_all_urls_|. |
325 base::Lock lookup_lock_; | 333 base::Lock lookup_lock_; |
326 | 334 |
327 // Underlying persistent store for chunk data. | 335 // Underlying persistent store for chunk data. |
328 // For browsing related (phishing and malware URLs) chunks and prefixes. | 336 // For browsing related (phishing and malware URLs) chunks and prefixes. |
329 FilePath browse_filename_; | 337 FilePath browse_filename_; |
330 scoped_ptr<SafeBrowsingStore> browse_store_; | 338 scoped_ptr<SafeBrowsingStore> browse_store_; |
331 | 339 |
332 // For download related (download URL and binary hash) chunks and prefixes. | 340 // For download related (download URL and binary hash) chunks and prefixes. |
333 FilePath download_filename_; | 341 FilePath download_filename_; |
334 scoped_ptr<SafeBrowsingStore> download_store_; | 342 scoped_ptr<SafeBrowsingStore> download_store_; |
335 | 343 |
336 // For the client-side phishing detection whitelist chunks and full-length | 344 // For the client-side phishing detection whitelist chunks and full-length |
337 // hashes. This list only contains 256 bit hashes. | 345 // hashes. This list only contains 256 bit hashes. |
338 FilePath csd_whitelist_filename_; | 346 FilePath csd_whitelist_filename_; |
339 scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; | 347 scoped_ptr<SafeBrowsingStore> csd_whitelist_store_; |
340 | 348 |
341 // For the download whitelist chunks and full-length hashes. This list only | 349 // For the download whitelist chunks and full-length hashes. This list only |
342 // contains 256 bit hashes. | 350 // contains 256 bit hashes. |
343 FilePath download_whitelist_filename_; | 351 FilePath download_whitelist_filename_; |
344 scoped_ptr<SafeBrowsingStore> download_whitelist_store_; | 352 scoped_ptr<SafeBrowsingStore> download_whitelist_store_; |
345 | 353 |
346 SBWhitelist csd_whitelist_; | 354 SBWhitelist csd_whitelist_; |
347 SBWhitelist download_whitelist_; | 355 SBWhitelist download_whitelist_; |
348 | 356 |
349 // Bloom filter generated from the add-prefixes in |browse_store_|. | 357 // Bloom filter generated from the add-prefixes in |browse_store_|. |
350 // Only browse_store_ requires the BloomFilter for fast query. | 358 // Only browse_store_ requires the BloomFilter for fast query. |
| 359 // TODO(shess): Do not use, being replaced by prefix_set_. |
351 FilePath bloom_filter_filename_; | 360 FilePath bloom_filter_filename_; |
352 scoped_refptr<BloomFilter> browse_bloom_filter_; | 361 scoped_refptr<BloomFilter> browse_bloom_filter_; |
353 | 362 |
354 // Cached browse store related full-hash items, ordered by prefix for | 363 // Cached browse store related full-hash items, ordered by prefix for |
355 // efficient scanning. | 364 // efficient scanning. |
356 // |full_browse_hashes_| are items from |browse_store_|, | 365 // |full_browse_hashes_| are items from |browse_store_|, |
357 // |pending_browse_hashes_| are items from |CacheHashResults()|, which | 366 // |pending_browse_hashes_| are items from |CacheHashResults()|, which |
358 // will be pushed to the store on the next update. | 367 // will be pushed to the store on the next update. |
359 std::vector<SBAddFullHash> full_browse_hashes_; | 368 std::vector<SBAddFullHash> full_browse_hashes_; |
360 std::vector<SBAddFullHash> pending_browse_hashes_; | 369 std::vector<SBAddFullHash> pending_browse_hashes_; |
361 | 370 |
362 // Cache of prefixes that returned empty results (no full hash | 371 // Cache of prefixes that returned empty results (no full hash |
363 // match) to |CacheHashResults()|. Cached to prevent asking for | 372 // match) to |CacheHashResults()|. Cached to prevent asking for |
364 // them every time. Cleared on next update. | 373 // them every time. Cleared on next update. |
365 std::set<SBPrefix> prefix_miss_cache_; | 374 std::set<SBPrefix> prefix_miss_cache_; |
366 | 375 |
367 // Used to schedule resetting the database because of corruption. | 376 // Used to schedule resetting the database because of corruption. |
368 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; | 377 base::WeakPtrFactory<SafeBrowsingDatabaseNew> reset_factory_; |
369 | 378 |
370 // Set if corruption is detected during the course of an update. | 379 // Set if corruption is detected during the course of an update. |
371 // Causes the update functions to fail with no side effects, until | 380 // Causes the update functions to fail with no side effects, until |
372 // the next call to |UpdateStarted()|. | 381 // the next call to |UpdateStarted()|. |
373 bool corruption_detected_; | 382 bool corruption_detected_; |
374 | 383 |
375 // Set to true if any chunks are added or deleted during an update. | 384 // Set to true if any chunks are added or deleted during an update. |
376 // Used to optimize away database update. | 385 // Used to optimize away database update. |
377 bool change_detected_; | 386 bool change_detected_; |
378 | 387 |
379 // Used to check if a prefix was in the database. | 388 // Used to check if a prefix was in the database. |
| 389 FilePath prefix_set_filename_; |
380 scoped_ptr<safe_browsing::PrefixSet> prefix_set_; | 390 scoped_ptr<safe_browsing::PrefixSet> prefix_set_; |
381 }; | 391 }; |
382 | 392 |
383 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ | 393 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_DATABASE_H_ |
OLD | NEW |