OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "crypto/sha2.h" | 25 #include "crypto/sha2.h" |
26 #include "net/base/ip_address_number.h" | 26 #include "net/base/ip_address_number.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
30 #include "base/mac/mac_util.h" | 30 #include "base/mac/mac_util.h" |
31 #endif | 31 #endif |
32 | 32 |
33 using content::BrowserThread; | 33 using content::BrowserThread; |
34 using safe_browsing::PrefixSet; | |
35 using safe_browsing::PrefixSetBuilder; | |
36 | 34 |
37 namespace { | 35 namespace { |
38 | 36 |
37 using safe_browsing::SafeBrowsingStore; | |
38 using safe_browsing::SBCachedFullHashResult; | |
39 using safe_browsing::SBPrefix; | |
40 | |
39 // Filename suffix for the bloom filter. | 41 // Filename suffix for the bloom filter. |
40 const base::FilePath::CharType kBloomFilterFileSuffix[] = | 42 const base::FilePath::CharType kBloomFilterFileSuffix[] = |
41 FILE_PATH_LITERAL(" Filter 2"); | 43 FILE_PATH_LITERAL(" Filter 2"); |
42 // Filename suffix for the prefix set. | 44 // Filename suffix for the prefix set. |
43 const base::FilePath::CharType kPrefixSetFileSuffix[] = | 45 const base::FilePath::CharType kPrefixSetFileSuffix[] = |
44 FILE_PATH_LITERAL(" Prefix Set"); | 46 FILE_PATH_LITERAL(" Prefix Set"); |
45 // Filename suffix for download store. | 47 // Filename suffix for download store. |
46 const base::FilePath::CharType kDownloadDBFile[] = | 48 const base::FilePath::CharType kDownloadDBFile[] = |
47 FILE_PATH_LITERAL(" Download"); | 49 FILE_PATH_LITERAL(" Download"); |
48 // Filename suffix for client-side phishing detection whitelist store. | 50 // Filename suffix for client-side phishing detection whitelist store. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 // to match against the csd whitelist. E.g., if the path-prefix /foo is on the | 118 // to match against the csd whitelist. E.g., if the path-prefix /foo is on the |
117 // whitelist it should also match /foo/bar which is not the case for all the | 119 // whitelist it should also match /foo/bar which is not the case for all the |
118 // other lists. We'll also always add a pattern for the empty path. | 120 // other lists. We'll also always add a pattern for the empty path. |
119 // TODO(shess): This function is almost the same as | 121 // TODO(shess): This function is almost the same as |
120 // |CompareFullHashes()| in safe_browsing_util.cc, except that code | 122 // |CompareFullHashes()| in safe_browsing_util.cc, except that code |
121 // does an early exit on match. Since match should be the infrequent | 123 // does an early exit on match. Since match should be the infrequent |
122 // case (phishing or malware found), consider combining this function | 124 // case (phishing or malware found), consider combining this function |
123 // with that one. | 125 // with that one. |
124 void UrlToFullHashes(const GURL& url, | 126 void UrlToFullHashes(const GURL& url, |
125 bool include_whitelist_hashes, | 127 bool include_whitelist_hashes, |
126 std::vector<SBFullHash>* full_hashes) { | 128 std::vector<safe_browsing::SBFullHash>* full_hashes) { |
127 std::vector<std::string> hosts; | 129 std::vector<std::string> hosts; |
128 if (url.HostIsIPAddress()) { | 130 if (url.HostIsIPAddress()) { |
129 hosts.push_back(url.host()); | 131 hosts.push_back(url.host()); |
130 } else { | 132 } else { |
131 safe_browsing::GenerateHostsToCheck(url, &hosts); | 133 safe_browsing::GenerateHostsToCheck(url, &hosts); |
132 } | 134 } |
133 | 135 |
134 std::vector<std::string> paths; | 136 std::vector<std::string> paths; |
135 safe_browsing::GeneratePathsToCheck(url, &paths); | 137 safe_browsing::GeneratePathsToCheck(url, &paths); |
136 | 138 |
137 for (size_t i = 0; i < hosts.size(); ++i) { | 139 for (size_t i = 0; i < hosts.size(); ++i) { |
138 for (size_t j = 0; j < paths.size(); ++j) { | 140 for (size_t j = 0; j < paths.size(); ++j) { |
139 const std::string& path = paths[j]; | 141 const std::string& path = paths[j]; |
140 full_hashes->push_back(safe_browsing::SBFullHashForString( | 142 full_hashes->push_back( |
141 hosts[i] + path)); | 143 safe_browsing::SBFullHashForString(hosts[i] + path)); |
142 | 144 |
143 // We may have /foo as path-prefix in the whitelist which should | 145 // We may have /foo as path-prefix in the whitelist which should |
144 // also match with /foo/bar and /foo?bar. Hence, for every path | 146 // also match with /foo/bar and /foo?bar. Hence, for every path |
145 // that ends in '/' we also add the path without the slash. | 147 // that ends in '/' we also add the path without the slash. |
146 if (include_whitelist_hashes && | 148 if (include_whitelist_hashes && path.size() > 1 && |
147 path.size() > 1 && | |
148 path[path.size() - 1] == '/') { | 149 path[path.size() - 1] == '/') { |
149 full_hashes->push_back( | 150 full_hashes->push_back(safe_browsing::SBFullHashForString( |
150 safe_browsing::SBFullHashForString( | 151 hosts[i] + path.substr(0, path.size() - 1))); |
151 hosts[i] + path.substr(0, path.size() - 1))); | |
152 } | 152 } |
153 } | 153 } |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 // Helper function to compare addprefixes in |store| with |prefixes|. | 157 // Helper function to compare addprefixes in |store| with |prefixes|. |
158 // The |list_bit| indicates which list (url or hash) to compare. | 158 // The |list_bit| indicates which list (url or hash) to compare. |
159 // | 159 // |
160 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain | 160 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain |
161 // the actual matching prefixes. | 161 // the actual matching prefixes. |
162 bool MatchAddPrefixes(SafeBrowsingStore* store, | 162 bool MatchAddPrefixes(SafeBrowsingStore* store, |
163 int list_bit, | 163 int list_bit, |
164 const std::vector<SBPrefix>& prefixes, | 164 const std::vector<SBPrefix>& prefixes, |
165 std::vector<SBPrefix>* prefix_hits) { | 165 std::vector<SBPrefix>* prefix_hits) { |
166 prefix_hits->clear(); | 166 prefix_hits->clear(); |
167 bool found_match = false; | 167 bool found_match = false; |
168 | 168 |
169 SBAddPrefixes add_prefixes; | 169 safe_browsing::SBAddPrefixes add_prefixes; |
170 store->GetAddPrefixes(&add_prefixes); | 170 store->GetAddPrefixes(&add_prefixes); |
171 for (SBAddPrefixes::const_iterator iter = add_prefixes.begin(); | 171 for (safe_browsing::SBAddPrefixes::const_iterator iter = add_prefixes.begin(); |
172 iter != add_prefixes.end(); ++iter) { | 172 iter != add_prefixes.end(); ++iter) { |
173 for (size_t j = 0; j < prefixes.size(); ++j) { | 173 for (size_t j = 0; j < prefixes.size(); ++j) { |
174 const SBPrefix& prefix = prefixes[j]; | 174 const SBPrefix& prefix = prefixes[j]; |
175 if (prefix == iter->prefix && | 175 if (prefix == iter->prefix && GetListIdBit(iter->chunk_id) == list_bit) { |
176 GetListIdBit(iter->chunk_id) == list_bit) { | |
177 prefix_hits->push_back(prefix); | 176 prefix_hits->push_back(prefix); |
178 found_match = true; | 177 found_match = true; |
179 } | 178 } |
180 } | 179 } |
181 } | 180 } |
182 return found_match; | 181 return found_match; |
183 } | 182 } |
184 | 183 |
185 // This function generates a chunk range string for |chunks|. It | 184 // This function generates a chunk range string for |chunks|. It |
186 // outputs one chunk range string per list and writes it to the | 185 // outputs one chunk range string per list and writes it to the |
187 // |list_ranges| vector. We expect |list_ranges| to already be of the | 186 // |list_ranges| vector. We expect |list_ranges| to already be of the |
188 // right size. E.g., if |chunks| contains chunks with two different | 187 // right size. E.g., if |chunks| contains chunks with two different |
189 // list ids then |list_ranges| must contain two elements. | 188 // list ids then |list_ranges| must contain two elements. |
190 void GetChunkRanges(const std::vector<int>& chunks, | 189 void GetChunkRanges(const std::vector<int>& chunks, |
191 std::vector<std::string>* list_ranges) { | 190 std::vector<std::string>* list_ranges) { |
192 // Since there are 2 possible list ids, there must be exactly two | 191 // Since there are 2 possible list ids, there must be exactly two |
193 // list ranges. Even if the chunk data should only contain one | 192 // list ranges. Even if the chunk data should only contain one |
194 // line, this code has to somehow handle corruption. | 193 // line, this code has to somehow handle corruption. |
195 DCHECK_EQ(2U, list_ranges->size()); | 194 DCHECK_EQ(2U, list_ranges->size()); |
196 | 195 |
197 std::vector<std::vector<int> > decoded_chunks(list_ranges->size()); | 196 std::vector<std::vector<int>> decoded_chunks(list_ranges->size()); |
198 for (std::vector<int>::const_iterator iter = chunks.begin(); | 197 for (std::vector<int>::const_iterator iter = chunks.begin(); |
199 iter != chunks.end(); ++iter) { | 198 iter != chunks.end(); ++iter) { |
200 int mod_list_id = GetListIdBit(*iter); | 199 int mod_list_id = GetListIdBit(*iter); |
201 DCHECK_GE(mod_list_id, 0); | 200 DCHECK_GE(mod_list_id, 0); |
202 DCHECK_LT(static_cast<size_t>(mod_list_id), decoded_chunks.size()); | 201 DCHECK_LT(static_cast<size_t>(mod_list_id), decoded_chunks.size()); |
203 decoded_chunks[mod_list_id].push_back(DecodeChunkId(*iter)); | 202 decoded_chunks[mod_list_id].push_back(DecodeChunkId(*iter)); |
204 } | 203 } |
205 for (size_t i = 0; i < decoded_chunks.size(); ++i) { | 204 for (size_t i = 0; i < decoded_chunks.size(); ++i) { |
206 ChunksToRangeString(decoded_chunks[i], &((*list_ranges)[i])); | 205 safe_browsing::ChunksToRangeString(decoded_chunks[i], &((*list_ranges)[i])); |
207 } | 206 } |
208 } | 207 } |
209 | 208 |
210 // Helper function to create chunk range lists for Browse related | 209 // Helper function to create chunk range lists for Browse related |
211 // lists. | 210 // lists. |
212 void UpdateChunkRanges(SafeBrowsingStore* store, | 211 void UpdateChunkRanges(SafeBrowsingStore* store, |
213 const std::vector<std::string>& listnames, | 212 const std::vector<std::string>& listnames, |
214 std::vector<SBListChunkRanges>* lists) { | 213 std::vector<safe_browsing::SBListChunkRanges>* lists) { |
215 if (!store) | 214 if (!store) |
216 return; | 215 return; |
217 | 216 |
218 DCHECK_GT(listnames.size(), 0U); | 217 DCHECK_GT(listnames.size(), 0U); |
219 DCHECK_LE(listnames.size(), 2U); | 218 DCHECK_LE(listnames.size(), 2U); |
220 std::vector<int> add_chunks; | 219 std::vector<int> add_chunks; |
221 std::vector<int> sub_chunks; | 220 std::vector<int> sub_chunks; |
222 store->GetAddChunks(&add_chunks); | 221 store->GetAddChunks(&add_chunks); |
223 store->GetSubChunks(&sub_chunks); | 222 store->GetSubChunks(&sub_chunks); |
224 | 223 |
225 // Always decode 2 ranges, even if only the first one is expected. | 224 // Always decode 2 ranges, even if only the first one is expected. |
226 // The loop below will only load as many into |lists| as |listnames| | 225 // The loop below will only load as many into |lists| as |listnames| |
227 // indicates. | 226 // indicates. |
228 std::vector<std::string> adds(2); | 227 std::vector<std::string> adds(2); |
229 std::vector<std::string> subs(2); | 228 std::vector<std::string> subs(2); |
230 GetChunkRanges(add_chunks, &adds); | 229 GetChunkRanges(add_chunks, &adds); |
231 GetChunkRanges(sub_chunks, &subs); | 230 GetChunkRanges(sub_chunks, &subs); |
232 | 231 |
233 for (size_t i = 0; i < listnames.size(); ++i) { | 232 for (size_t i = 0; i < listnames.size(); ++i) { |
234 const std::string& listname = listnames[i]; | 233 const std::string& listname = listnames[i]; |
235 DCHECK_EQ(safe_browsing::GetListId(listname) % 2, | 234 DCHECK_EQ(safe_browsing::GetListId(listname) % 2, static_cast<int>(i % 2)); |
236 static_cast<int>(i % 2)); | 235 DCHECK_NE(safe_browsing::GetListId(listname), safe_browsing::INVALID); |
237 DCHECK_NE(safe_browsing::GetListId(listname), | 236 lists->push_back(safe_browsing::SBListChunkRanges(listname)); |
238 safe_browsing::INVALID); | |
239 lists->push_back(SBListChunkRanges(listname)); | |
240 lists->back().adds.swap(adds[i]); | 237 lists->back().adds.swap(adds[i]); |
241 lists->back().subs.swap(subs[i]); | 238 lists->back().subs.swap(subs[i]); |
242 } | 239 } |
243 } | 240 } |
244 | 241 |
245 void UpdateChunkRangesForLists(SafeBrowsingStore* store, | 242 void UpdateChunkRangesForLists( |
246 const std::string& listname0, | 243 SafeBrowsingStore* store, |
247 const std::string& listname1, | 244 const std::string& listname0, |
248 std::vector<SBListChunkRanges>* lists) { | 245 const std::string& listname1, |
246 std::vector<safe_browsing::SBListChunkRanges>* lists) { | |
249 std::vector<std::string> listnames; | 247 std::vector<std::string> listnames; |
250 listnames.push_back(listname0); | 248 listnames.push_back(listname0); |
251 listnames.push_back(listname1); | 249 listnames.push_back(listname1); |
252 UpdateChunkRanges(store, listnames, lists); | 250 UpdateChunkRanges(store, listnames, lists); |
253 } | 251 } |
254 | 252 |
255 void UpdateChunkRangesForList(SafeBrowsingStore* store, | 253 void UpdateChunkRangesForList( |
256 const std::string& listname, | 254 SafeBrowsingStore* store, |
257 std::vector<SBListChunkRanges>* lists) { | 255 const std::string& listname, |
256 std::vector<safe_browsing::SBListChunkRanges>* lists) { | |
258 UpdateChunkRanges(store, std::vector<std::string>(1, listname), lists); | 257 UpdateChunkRanges(store, std::vector<std::string>(1, listname), lists); |
259 } | 258 } |
260 | 259 |
261 // This code always checks for non-zero file size. This helper makes | 260 // This code always checks for non-zero file size. This helper makes |
262 // that less verbose. | 261 // that less verbose. |
263 int64 GetFileSizeOrZero(const base::FilePath& file_path) { | 262 int64 GetFileSizeOrZero(const base::FilePath& file_path) { |
264 int64 size_64; | 263 int64 size_64; |
265 if (!base::GetFileSize(file_path, &size_64)) | 264 if (!base::GetFileSize(file_path, &size_64)) |
266 return 0; | 265 return 0; |
267 return size_64; | 266 return size_64; |
268 } | 267 } |
269 | 268 |
270 // Helper for PrefixSetContainsUrlHashes(). Returns true if an un-expired match | 269 // Helper for PrefixSetContainsUrlHashes(). Returns true if an un-expired match |
271 // for |full_hash| is found in |cache|, with any matches appended to |results| | 270 // for |full_hash| is found in |cache|, with any matches appended to |results| |
272 // (true can be returned with zero matches). |expire_base| is used to check the | 271 // (true can be returned with zero matches). |expire_base| is used to check the |
273 // cache lifetime of matches, expired matches will be discarded from |cache|. | 272 // cache lifetime of matches, expired matches will be discarded from |cache|. |
274 bool GetCachedFullHash(std::map<SBPrefix, SBCachedFullHashResult>* cache, | 273 bool GetCachedFullHash(std::map<SBPrefix, SBCachedFullHashResult>* cache, |
275 const SBFullHash& full_hash, | 274 const safe_browsing::SBFullHash& full_hash, |
276 const base::Time& expire_base, | 275 const base::Time& expire_base, |
277 std::vector<SBFullHashResult>* results) { | 276 std::vector<safe_browsing::SBFullHashResult>* results) { |
278 // First check if there is a valid cached result for this prefix. | 277 // First check if there is a valid cached result for this prefix. |
279 std::map<SBPrefix, SBCachedFullHashResult>::iterator | 278 std::map<SBPrefix, SBCachedFullHashResult>::iterator citer = |
280 citer = cache->find(full_hash.prefix); | 279 cache->find(full_hash.prefix); |
281 if (citer == cache->end()) | 280 if (citer == cache->end()) |
282 return false; | 281 return false; |
283 | 282 |
284 // Remove expired entries. | 283 // Remove expired entries. |
285 SBCachedFullHashResult& cached_result = citer->second; | 284 SBCachedFullHashResult& cached_result = citer->second; |
286 if (cached_result.expire_after <= expire_base) { | 285 if (cached_result.expire_after <= expire_base) { |
287 cache->erase(citer); | 286 cache->erase(citer); |
288 return false; | 287 return false; |
289 } | 288 } |
290 | 289 |
291 // Find full-hash matches. | 290 // Find full-hash matches. |
292 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; | 291 std::vector<safe_browsing::SBFullHashResult>& cached_hashes = |
292 cached_result.full_hashes; | |
293 for (size_t i = 0; i < cached_hashes.size(); ++i) { | 293 for (size_t i = 0; i < cached_hashes.size(); ++i) { |
294 if (safe_browsing::SBFullHashEqual(full_hash, cached_hashes[i].hash)) | 294 if (SBFullHashEqual(full_hash, cached_hashes[i].hash)) |
295 results->push_back(cached_hashes[i]); | 295 results->push_back(cached_hashes[i]); |
296 } | 296 } |
297 | 297 |
298 return true; | 298 return true; |
299 } | 299 } |
300 | 300 |
301 SafeBrowsingStoreFile* CreateStore( | 301 safe_browsing::SafeBrowsingStoreFile* CreateStore( |
302 bool enable, | 302 bool enable, |
303 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 303 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
304 if (!enable) | 304 if (!enable) |
305 return nullptr; | 305 return nullptr; |
306 return new SafeBrowsingStoreFile(task_runner); | 306 return new safe_browsing::SafeBrowsingStoreFile(task_runner); |
307 } | 307 } |
308 | 308 |
309 } // namespace | 309 } // namespace |
310 | 310 |
311 namespace safe_browsing { | |
Nathan Parker
2015/11/07 01:15:13
Similar here: this can encompase the whole file, w
vakh (old account. dont use)
2015/11/11 18:59:53
Done.
| |
312 | |
311 // The default SafeBrowsingDatabaseFactory. | 313 // The default SafeBrowsingDatabaseFactory. |
312 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { | 314 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { |
313 public: | 315 public: |
314 SafeBrowsingDatabase* CreateSafeBrowsingDatabase( | 316 SafeBrowsingDatabase* CreateSafeBrowsingDatabase( |
315 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 317 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
316 bool enable_download_protection, | 318 bool enable_download_protection, |
317 bool enable_client_side_whitelist, | 319 bool enable_client_side_whitelist, |
318 bool enable_download_whitelist, | 320 bool enable_download_whitelist, |
319 bool enable_extension_blacklist, | 321 bool enable_extension_blacklist, |
320 bool enable_ip_blacklist, | 322 bool enable_ip_blacklist, |
321 bool enable_unwanted_software_list) override { | 323 bool enable_unwanted_software_list) override { |
322 return new SafeBrowsingDatabaseNew( | 324 return new SafeBrowsingDatabaseNew( |
323 db_task_runner, CreateStore(true, db_task_runner), // browse_store | 325 db_task_runner, CreateStore(true, db_task_runner), // browse_store |
324 CreateStore(enable_download_protection, db_task_runner), | 326 CreateStore(enable_download_protection, db_task_runner), |
325 CreateStore(enable_client_side_whitelist, db_task_runner), | 327 CreateStore(enable_client_side_whitelist, db_task_runner), |
326 CreateStore(enable_download_whitelist, db_task_runner), | 328 CreateStore(enable_download_whitelist, db_task_runner), |
327 CreateStore(true, db_task_runner), // inclusion_whitelist_store | 329 CreateStore(true, db_task_runner), // inclusion_whitelist_store |
328 CreateStore(enable_extension_blacklist, db_task_runner), | 330 CreateStore(enable_extension_blacklist, db_task_runner), |
329 CreateStore(enable_ip_blacklist, db_task_runner), | 331 CreateStore(enable_ip_blacklist, db_task_runner), |
330 CreateStore(enable_unwanted_software_list, db_task_runner)); | 332 CreateStore(enable_unwanted_software_list, db_task_runner)); |
331 } | 333 } |
332 | 334 |
333 SafeBrowsingDatabaseFactoryImpl() { } | 335 SafeBrowsingDatabaseFactoryImpl() {} |
334 | 336 |
335 private: | 337 private: |
336 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactoryImpl); | 338 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactoryImpl); |
337 }; | 339 }; |
338 | 340 |
339 // static | 341 // static |
340 SafeBrowsingDatabaseFactory* SafeBrowsingDatabase::factory_ = NULL; | 342 SafeBrowsingDatabaseFactory* SafeBrowsingDatabase::factory_ = NULL; |
341 | 343 |
342 // Factory method, should be called on the Safe Browsing sequenced task runner, | 344 // Factory method, should be called on the Safe Browsing sequenced task runner, |
343 // which is also passed to the function as |current_task_runner|. | 345 // which is also passed to the function as |current_task_runner|. |
(...skipping 11 matching lines...) Expand all Loading... | |
355 DCHECK(current_task_runner->RunsTasksOnCurrentThread()); | 357 DCHECK(current_task_runner->RunsTasksOnCurrentThread()); |
356 if (!factory_) | 358 if (!factory_) |
357 factory_ = new SafeBrowsingDatabaseFactoryImpl(); | 359 factory_ = new SafeBrowsingDatabaseFactoryImpl(); |
358 return factory_->CreateSafeBrowsingDatabase( | 360 return factory_->CreateSafeBrowsingDatabase( |
359 current_task_runner, enable_download_protection, | 361 current_task_runner, enable_download_protection, |
360 enable_client_side_whitelist, enable_download_whitelist, | 362 enable_client_side_whitelist, enable_download_whitelist, |
361 enable_extension_blacklist, enable_ip_blacklist, | 363 enable_extension_blacklist, enable_ip_blacklist, |
362 enable_unwanted_software_list); | 364 enable_unwanted_software_list); |
363 } | 365 } |
364 | 366 |
365 SafeBrowsingDatabase::~SafeBrowsingDatabase() { | 367 SafeBrowsingDatabase::~SafeBrowsingDatabase() {} |
366 } | |
367 | 368 |
368 // static | 369 // static |
369 base::FilePath SafeBrowsingDatabase::BrowseDBFilename( | 370 base::FilePath SafeBrowsingDatabase::BrowseDBFilename( |
370 const base::FilePath& db_base_filename) { | 371 const base::FilePath& db_base_filename) { |
371 return base::FilePath(db_base_filename.value() + kBrowseDBFile); | 372 return base::FilePath(db_base_filename.value() + kBrowseDBFile); |
372 } | 373 } |
373 | 374 |
374 // static | 375 // static |
375 base::FilePath SafeBrowsingDatabase::DownloadDBFilename( | 376 base::FilePath SafeBrowsingDatabase::DownloadDBFilename( |
376 const base::FilePath& db_base_filename) { | 377 const base::FilePath& db_base_filename) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 UrlToFullHashes(urls[i], false, &full_hashes); | 441 UrlToFullHashes(urls[i], false, &full_hashes); |
441 | 442 |
442 for (size_t i = 0; i < full_hashes.size(); ++i) | 443 for (size_t i = 0; i < full_hashes.size(); ++i) |
443 prefixes->push_back(full_hashes[i].prefix); | 444 prefixes->push_back(full_hashes[i].prefix); |
444 } | 445 } |
445 | 446 |
446 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { | 447 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { |
447 // Stores are not thread safe. | 448 // Stores are not thread safe. |
448 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 449 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
449 | 450 |
450 if (list_id == safe_browsing::PHISH || | 451 if (list_id == PHISH || list_id == MALWARE) { |
451 list_id == safe_browsing::MALWARE) { | |
452 return browse_store_.get(); | 452 return browse_store_.get(); |
453 } else if (list_id == safe_browsing::BINURL) { | 453 } else if (list_id == BINURL) { |
454 return download_store_.get(); | 454 return download_store_.get(); |
455 } else if (list_id == safe_browsing::CSDWHITELIST) { | 455 } else if (list_id == CSDWHITELIST) { |
456 return csd_whitelist_store_.get(); | 456 return csd_whitelist_store_.get(); |
457 } else if (list_id == safe_browsing::DOWNLOADWHITELIST) { | 457 } else if (list_id == DOWNLOADWHITELIST) { |
458 return download_whitelist_store_.get(); | 458 return download_whitelist_store_.get(); |
459 } else if (list_id == safe_browsing::INCLUSIONWHITELIST) { | 459 } else if (list_id == INCLUSIONWHITELIST) { |
460 return inclusion_whitelist_store_.get(); | 460 return inclusion_whitelist_store_.get(); |
461 } else if (list_id == safe_browsing::EXTENSIONBLACKLIST) { | 461 } else if (list_id == EXTENSIONBLACKLIST) { |
462 return extension_blacklist_store_.get(); | 462 return extension_blacklist_store_.get(); |
463 } else if (list_id == safe_browsing::IPBLACKLIST) { | 463 } else if (list_id == IPBLACKLIST) { |
464 return ip_blacklist_store_.get(); | 464 return ip_blacklist_store_.get(); |
465 } else if (list_id == safe_browsing::UNWANTEDURL) { | 465 } else if (list_id == UNWANTEDURL) { |
466 return unwanted_software_store_.get(); | 466 return unwanted_software_store_.get(); |
467 } | 467 } |
468 return NULL; | 468 return NULL; |
469 } | 469 } |
470 | 470 |
471 // static | 471 // static |
472 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { | 472 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { |
473 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, | 473 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, |
474 FAILURE_DATABASE_MAX); | 474 FAILURE_DATABASE_MAX); |
475 } | 475 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
600 } | 600 } |
601 | 601 |
602 ThreadSafeStateManager* outer_; | 602 ThreadSafeStateManager* outer_; |
603 base::AutoLock transaction_lock_; | 603 base::AutoLock transaction_lock_; |
604 | 604 |
605 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); | 605 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); |
606 }; | 606 }; |
607 | 607 |
608 SafeBrowsingDatabaseNew::ThreadSafeStateManager::ThreadSafeStateManager( | 608 SafeBrowsingDatabaseNew::ThreadSafeStateManager::ThreadSafeStateManager( |
609 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) | 609 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) |
610 : db_task_runner_(db_task_runner) { | 610 : db_task_runner_(db_task_runner) {} |
611 } | |
612 | 611 |
613 SafeBrowsingDatabaseNew::ThreadSafeStateManager::~ThreadSafeStateManager() { | 612 SafeBrowsingDatabaseNew::ThreadSafeStateManager::~ThreadSafeStateManager() {} |
614 } | |
615 | 613 |
616 SafeBrowsingDatabaseNew::DatabaseStateManager::DatabaseStateManager( | 614 SafeBrowsingDatabaseNew::DatabaseStateManager::DatabaseStateManager( |
617 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) | 615 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) |
618 : db_task_runner_(db_task_runner), | 616 : db_task_runner_(db_task_runner), |
619 corruption_detected_(false), | 617 corruption_detected_(false), |
620 change_detected_(false) { | 618 change_detected_(false) {} |
621 } | |
622 | 619 |
623 SafeBrowsingDatabaseNew::DatabaseStateManager::~DatabaseStateManager() { | 620 SafeBrowsingDatabaseNew::DatabaseStateManager::~DatabaseStateManager() {} |
624 } | |
625 | 621 |
626 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> | 622 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> |
627 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginReadTransaction() { | 623 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginReadTransaction() { |
628 return make_scoped_ptr( | 624 return make_scoped_ptr( |
629 new ReadTransaction(this, ReadTransaction::AutoLockRequirement::LOCK)); | 625 new ReadTransaction(this, ReadTransaction::AutoLockRequirement::LOCK)); |
630 } | 626 } |
631 | 627 |
632 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> SafeBrowsingDatabaseNew:: | 628 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> SafeBrowsingDatabaseNew:: |
633 ThreadSafeStateManager::BeginReadTransactionNoLockOnMainTaskRunner() { | 629 ThreadSafeStateManager::BeginReadTransactionNoLockOnMainTaskRunner() { |
634 return make_scoped_ptr(new ReadTransaction( | 630 return make_scoped_ptr(new ReadTransaction( |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
909 | 905 |
910 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( | 906 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( |
911 const std::vector<SBPrefix>& prefixes, | 907 const std::vector<SBPrefix>& prefixes, |
912 std::vector<SBPrefix>* prefix_hits) { | 908 std::vector<SBPrefix>* prefix_hits) { |
913 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 909 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
914 | 910 |
915 // Ignore this check when download checking is not enabled. | 911 // Ignore this check when download checking is not enabled. |
916 if (!download_store_.get()) | 912 if (!download_store_.get()) |
917 return false; | 913 return false; |
918 | 914 |
919 return MatchAddPrefixes(download_store_.get(), | 915 return MatchAddPrefixes(download_store_.get(), BINURL % 2, prefixes, |
920 safe_browsing::BINURL % 2, | |
921 prefixes, | |
922 prefix_hits); | 916 prefix_hits); |
923 } | 917 } |
924 | 918 |
925 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { | 919 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { |
926 std::vector<SBFullHash> full_hashes; | 920 std::vector<SBFullHash> full_hashes; |
927 UrlToFullHashes(url, true, &full_hashes); | 921 UrlToFullHashes(url, true, &full_hashes); |
928 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 922 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
929 } | 923 } |
930 | 924 |
931 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedUrl(const GURL& url) { | 925 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedUrl(const GURL& url) { |
(...skipping 10 matching lines...) Expand all Loading... | |
942 | 936 |
943 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( | 937 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( |
944 const std::vector<SBPrefix>& prefixes, | 938 const std::vector<SBPrefix>& prefixes, |
945 std::vector<SBPrefix>* prefix_hits) { | 939 std::vector<SBPrefix>* prefix_hits) { |
946 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 940 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
947 | 941 |
948 if (!extension_blacklist_store_) | 942 if (!extension_blacklist_store_) |
949 return false; | 943 return false; |
950 | 944 |
951 return MatchAddPrefixes(extension_blacklist_store_.get(), | 945 return MatchAddPrefixes(extension_blacklist_store_.get(), |
952 safe_browsing::EXTENSIONBLACKLIST % 2, | 946 EXTENSIONBLACKLIST % 2, prefixes, prefix_hits); |
953 prefixes, | |
954 prefix_hits); | |
955 } | 947 } |
956 | 948 |
957 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { | 949 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { |
958 net::IPAddressNumber ip_number; | 950 net::IPAddressNumber ip_number; |
959 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) | 951 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) |
960 return false; | 952 return false; |
961 if (ip_number.size() == net::kIPv4AddressSize) | 953 if (ip_number.size() == net::kIPv4AddressSize) |
962 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); | 954 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); |
963 if (ip_number.size() != net::kIPv6AddressSize) | 955 if (ip_number.size() != net::kIPv6AddressSize) |
964 return false; // better safe than sorry. | 956 return false; // better safe than sorry. |
(...skipping 17 matching lines...) Expand all Loading... | |
982 if (it->second.count(hash) > 0) { | 974 if (it->second.count(hash) > 0) { |
983 return true; | 975 return true; |
984 } | 976 } |
985 } | 977 } |
986 return false; | 978 return false; |
987 } | 979 } |
988 | 980 |
989 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( | 981 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( |
990 const std::string& str) { | 982 const std::string& str) { |
991 std::vector<SBFullHash> hashes; | 983 std::vector<SBFullHash> hashes; |
992 hashes.push_back(safe_browsing::SBFullHashForString(str)); | 984 hashes.push_back(SBFullHashForString(str)); |
993 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); | 985 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); |
994 } | 986 } |
995 | 987 |
996 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( | 988 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( |
997 SBWhitelistId whitelist_id, | 989 SBWhitelistId whitelist_id, |
998 const std::vector<SBFullHash>& hashes) { | 990 const std::vector<SBFullHash>& hashes) { |
999 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); | 991 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); |
1000 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); | 992 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); |
1001 if (whitelist->second) | 993 if (whitelist->second) |
1002 return true; | 994 return true; |
1003 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); | 995 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); |
1004 it != hashes.end(); ++it) { | 996 it != hashes.end(); ++it) { |
1005 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), | 997 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), |
1006 *it, safe_browsing::SBFullHashLess)) { | 998 *it, SBFullHashLess)) { |
1007 return true; | 999 return true; |
1008 } | 1000 } |
1009 } | 1001 } |
1010 return false; | 1002 return false; |
1011 } | 1003 } |
1012 | 1004 |
1013 // Helper to insert add-chunk entries. | 1005 // Helper to insert add-chunk entries. |
1014 void SafeBrowsingDatabaseNew::InsertAddChunk( | 1006 void SafeBrowsingDatabaseNew::InsertAddChunk(SafeBrowsingStore* store, |
1015 SafeBrowsingStore* store, | 1007 const ListType list_id, |
1016 const safe_browsing::ListType list_id, | 1008 const SBChunkData& chunk_data) { |
1017 const SBChunkData& chunk_data) { | |
1018 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1009 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1019 DCHECK(store); | 1010 DCHECK(store); |
1020 | 1011 |
1021 // The server can give us a chunk that we already have because | 1012 // The server can give us a chunk that we already have because |
1022 // it's part of a range. Don't add it again. | 1013 // it's part of a range. Don't add it again. |
1023 const int chunk_id = chunk_data.ChunkNumber(); | 1014 const int chunk_id = chunk_data.ChunkNumber(); |
1024 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1015 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1025 if (store->CheckAddChunk(encoded_chunk_id)) | 1016 if (store->CheckAddChunk(encoded_chunk_id)) |
1026 return; | 1017 return; |
1027 | 1018 |
1028 store->SetAddChunk(encoded_chunk_id); | 1019 store->SetAddChunk(encoded_chunk_id); |
1029 if (chunk_data.IsPrefix()) { | 1020 if (chunk_data.IsPrefix()) { |
1030 const size_t c = chunk_data.PrefixCount(); | 1021 const size_t c = chunk_data.PrefixCount(); |
1031 for (size_t i = 0; i < c; ++i) { | 1022 for (size_t i = 0; i < c; ++i) { |
1032 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); | 1023 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); |
1033 } | 1024 } |
1034 } else { | 1025 } else { |
1035 const size_t c = chunk_data.FullHashCount(); | 1026 const size_t c = chunk_data.FullHashCount(); |
1036 for (size_t i = 0; i < c; ++i) { | 1027 for (size_t i = 0; i < c; ++i) { |
1037 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); | 1028 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); |
1038 } | 1029 } |
1039 } | 1030 } |
1040 } | 1031 } |
1041 | 1032 |
1042 // Helper to insert sub-chunk entries. | 1033 // Helper to insert sub-chunk entries. |
1043 void SafeBrowsingDatabaseNew::InsertSubChunk( | 1034 void SafeBrowsingDatabaseNew::InsertSubChunk(SafeBrowsingStore* store, |
1044 SafeBrowsingStore* store, | 1035 const ListType list_id, |
1045 const safe_browsing::ListType list_id, | 1036 const SBChunkData& chunk_data) { |
1046 const SBChunkData& chunk_data) { | |
1047 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1037 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1048 DCHECK(store); | 1038 DCHECK(store); |
1049 | 1039 |
1050 // The server can give us a chunk that we already have because | 1040 // The server can give us a chunk that we already have because |
1051 // it's part of a range. Don't add it again. | 1041 // it's part of a range. Don't add it again. |
1052 const int chunk_id = chunk_data.ChunkNumber(); | 1042 const int chunk_id = chunk_data.ChunkNumber(); |
1053 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1043 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1054 if (store->CheckSubChunk(encoded_chunk_id)) | 1044 if (store->CheckSubChunk(encoded_chunk_id)) |
1055 return; | 1045 return; |
1056 | 1046 |
(...skipping 21 matching lines...) Expand all Loading... | |
1078 const std::string& list_name, | 1068 const std::string& list_name, |
1079 const std::vector<SBChunkData*>& chunks) { | 1069 const std::vector<SBChunkData*>& chunks) { |
1080 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1070 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1081 | 1071 |
1082 if (db_state_manager_.corruption_detected() || chunks.empty()) | 1072 if (db_state_manager_.corruption_detected() || chunks.empty()) |
1083 return; | 1073 return; |
1084 | 1074 |
1085 const base::TimeTicks before = base::TimeTicks::Now(); | 1075 const base::TimeTicks before = base::TimeTicks::Now(); |
1086 | 1076 |
1087 // TODO(shess): The caller should just pass list_id. | 1077 // TODO(shess): The caller should just pass list_id. |
1088 const safe_browsing::ListType list_id = | 1078 const ListType list_id = GetListId(list_name); |
1089 safe_browsing::GetListId(list_name); | |
1090 | 1079 |
1091 SafeBrowsingStore* store = GetStore(list_id); | 1080 SafeBrowsingStore* store = GetStore(list_id); |
1092 if (!store) return; | 1081 if (!store) |
1082 return; | |
1093 | 1083 |
1094 db_state_manager_.set_change_detected(); | 1084 db_state_manager_.set_change_detected(); |
1095 | 1085 |
1096 // TODO(shess): I believe that the list is always add or sub. Can this use | 1086 // TODO(shess): I believe that the list is always add or sub. Can this use |
1097 // that productively? | 1087 // that productively? |
1098 store->BeginChunk(); | 1088 store->BeginChunk(); |
1099 for (size_t i = 0; i < chunks.size(); ++i) { | 1089 for (size_t i = 0; i < chunks.size(); ++i) { |
1100 if (chunks[i]->IsAdd()) { | 1090 if (chunks[i]->IsAdd()) { |
1101 InsertAddChunk(store, list_id, *chunks[i]); | 1091 InsertAddChunk(store, list_id, *chunks[i]); |
1102 } else if (chunks[i]->IsSub()) { | 1092 } else if (chunks[i]->IsSub()) { |
1103 InsertSubChunk(store, list_id, *chunks[i]); | 1093 InsertSubChunk(store, list_id, *chunks[i]); |
1104 } else { | 1094 } else { |
1105 NOTREACHED(); | 1095 NOTREACHED(); |
1106 } | 1096 } |
1107 } | 1097 } |
1108 store->FinishChunk(); | 1098 store->FinishChunk(); |
1109 | 1099 |
1110 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before); | 1100 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before); |
1111 } | 1101 } |
1112 | 1102 |
1113 void SafeBrowsingDatabaseNew::DeleteChunks( | 1103 void SafeBrowsingDatabaseNew::DeleteChunks( |
1114 const std::vector<SBChunkDelete>& chunk_deletes) { | 1104 const std::vector<SBChunkDelete>& chunk_deletes) { |
1115 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1105 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1116 | 1106 |
1117 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) | 1107 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) |
1118 return; | 1108 return; |
1119 | 1109 |
1120 const std::string& list_name = chunk_deletes.front().list_name; | 1110 const std::string& list_name = chunk_deletes.front().list_name; |
1121 const safe_browsing::ListType list_id = | 1111 const ListType list_id = GetListId(list_name); |
1122 safe_browsing::GetListId(list_name); | |
1123 | 1112 |
1124 SafeBrowsingStore* store = GetStore(list_id); | 1113 SafeBrowsingStore* store = GetStore(list_id); |
1125 if (!store) return; | 1114 if (!store) |
1115 return; | |
1126 | 1116 |
1127 db_state_manager_.set_change_detected(); | 1117 db_state_manager_.set_change_detected(); |
1128 | 1118 |
1129 for (size_t i = 0; i < chunk_deletes.size(); ++i) { | 1119 for (size_t i = 0; i < chunk_deletes.size(); ++i) { |
1130 std::vector<int> chunk_numbers; | 1120 std::vector<int> chunk_numbers; |
1131 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); | 1121 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); |
1132 for (size_t j = 0; j < chunk_numbers.size(); ++j) { | 1122 for (size_t j = 0; j < chunk_numbers.size(); ++j) { |
1133 const int encoded_chunk_id = EncodeChunkId(chunk_numbers[j], list_id); | 1123 const int encoded_chunk_id = EncodeChunkId(chunk_numbers[j], list_id); |
1134 if (chunk_deletes[i].is_sub_del) | 1124 if (chunk_deletes[i].is_sub_del) |
1135 store->DeleteSubChunk(encoded_chunk_id); | 1125 store->DeleteSubChunk(encoded_chunk_id); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1215 if (unwanted_software_store_ && !unwanted_software_store_->BeginUpdate()) { | 1205 if (unwanted_software_store_ && !unwanted_software_store_->BeginUpdate()) { |
1216 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); | 1206 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); |
1217 HandleCorruptDatabase(); | 1207 HandleCorruptDatabase(); |
1218 return false; | 1208 return false; |
1219 } | 1209 } |
1220 | 1210 |
1221 // Cached fullhash results must be cleared on every database update (whether | 1211 // Cached fullhash results must be cleared on every database update (whether |
1222 // successful or not). | 1212 // successful or not). |
1223 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); | 1213 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); |
1224 | 1214 |
1225 UpdateChunkRangesForLists(browse_store_.get(), | 1215 UpdateChunkRangesForLists(browse_store_.get(), kMalwareList, kPhishingList, |
1226 safe_browsing::kMalwareList, | |
1227 safe_browsing::kPhishingList, | |
1228 lists); | 1216 lists); |
1229 | 1217 |
1230 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been | 1218 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been |
1231 // deprecated. Code to delete the list from the store shows ~15k hits/day as | 1219 // deprecated. Code to delete the list from the store shows ~15k hits/day as |
1232 // of Feb 2014, so it has been removed. Everything _should_ be resilient to | 1220 // of Feb 2014, so it has been removed. Everything _should_ be resilient to |
1233 // extra data of that sort. | 1221 // extra data of that sort. |
1234 UpdateChunkRangesForList(download_store_.get(), | 1222 UpdateChunkRangesForList(download_store_.get(), kBinUrlList, lists); |
1235 safe_browsing::kBinUrlList, lists); | |
1236 | 1223 |
1237 UpdateChunkRangesForList(csd_whitelist_store_.get(), | 1224 UpdateChunkRangesForList(csd_whitelist_store_.get(), kCsdWhiteList, lists); |
1238 safe_browsing::kCsdWhiteList, lists); | |
1239 | 1225 |
1240 UpdateChunkRangesForList(download_whitelist_store_.get(), | 1226 UpdateChunkRangesForList(download_whitelist_store_.get(), kDownloadWhiteList, |
1241 safe_browsing::kDownloadWhiteList, lists); | 1227 lists); |
1242 | 1228 |
1243 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), | 1229 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), |
1244 safe_browsing::kInclusionWhitelist, lists); | 1230 kInclusionWhitelist, lists); |
1245 | 1231 |
1246 UpdateChunkRangesForList(extension_blacklist_store_.get(), | 1232 UpdateChunkRangesForList(extension_blacklist_store_.get(), |
1247 safe_browsing::kExtensionBlacklist, lists); | 1233 kExtensionBlacklist, lists); |
1248 | 1234 |
1249 UpdateChunkRangesForList(ip_blacklist_store_.get(), | 1235 UpdateChunkRangesForList(ip_blacklist_store_.get(), kIPBlacklist, lists); |
1250 safe_browsing::kIPBlacklist, lists); | |
1251 | 1236 |
1252 UpdateChunkRangesForList(unwanted_software_store_.get(), | 1237 UpdateChunkRangesForList(unwanted_software_store_.get(), kUnwantedUrlList, |
1253 safe_browsing::kUnwantedUrlList, | |
1254 lists); | 1238 lists); |
1255 | 1239 |
1256 db_state_manager_.reset_corruption_detected(); | 1240 db_state_manager_.reset_corruption_detected(); |
1257 db_state_manager_.reset_change_detected(); | 1241 db_state_manager_.reset_change_detected(); |
1258 return true; | 1242 return true; |
1259 } | 1243 } |
1260 | 1244 |
1261 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { | 1245 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { |
1262 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1246 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1263 | 1247 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1432 // Measure the amount of IO during the filter build. | 1416 // Measure the amount of IO during the filter build. |
1433 base::IoCounters io_before, io_after; | 1417 base::IoCounters io_before, io_after; |
1434 base::ProcessHandle handle = base::GetCurrentProcessHandle(); | 1418 base::ProcessHandle handle = base::GetCurrentProcessHandle(); |
1435 scoped_ptr<base::ProcessMetrics> metric( | 1419 scoped_ptr<base::ProcessMetrics> metric( |
1436 #if !defined(OS_MACOSX) | 1420 #if !defined(OS_MACOSX) |
1437 base::ProcessMetrics::CreateProcessMetrics(handle) | 1421 base::ProcessMetrics::CreateProcessMetrics(handle) |
1438 #else | 1422 #else |
1439 // Getting stats only for the current process is enough, so NULL is fine. | 1423 // Getting stats only for the current process is enough, so NULL is fine. |
1440 base::ProcessMetrics::CreateProcessMetrics(handle, NULL) | 1424 base::ProcessMetrics::CreateProcessMetrics(handle, NULL) |
1441 #endif | 1425 #endif |
1442 ); | 1426 ); |
1443 | 1427 |
1444 // IoCounters are currently not supported on Mac, and may not be | 1428 // IoCounters are currently not supported on Mac, and may not be |
1445 // available for Linux, so we check the result and only show IO | 1429 // available for Linux, so we check the result and only show IO |
1446 // stats if they are available. | 1430 // stats if they are available. |
1447 const bool got_counters = metric->GetIOCounters(&io_before); | 1431 const bool got_counters = metric->GetIOCounters(&io_before); |
1448 | 1432 |
1449 const base::TimeTicks before = base::TimeTicks::Now(); | 1433 const base::TimeTicks before = base::TimeTicks::Now(); |
1450 | 1434 |
1451 // TODO(shess): Perhaps refactor to let builder accumulate full hashes on the | 1435 // TODO(shess): Perhaps refactor to let builder accumulate full hashes on the |
1452 // fly? Other clients use the SBAddFullHash vector, but AFAICT they only use | 1436 // fly? Other clients use the SBAddFullHash vector, but AFAICT they only use |
(...skipping 26 matching lines...) Expand all Loading... | |
1479 new_prefix_set.Pass()); | 1463 new_prefix_set.Pass()); |
1480 | 1464 |
1481 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); | 1465 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); |
1482 | 1466 |
1483 WritePrefixSet(db_filename, prefix_set_id, write_failure_type); | 1467 WritePrefixSet(db_filename, prefix_set_id, write_failure_type); |
1484 | 1468 |
1485 // Gather statistics. | 1469 // Gather statistics. |
1486 if (got_counters && metric->GetIOCounters(&io_after)) { | 1470 if (got_counters && metric->GetIOCounters(&io_after)) { |
1487 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", | 1471 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", |
1488 static_cast<int>(io_after.ReadTransferCount - | 1472 static_cast<int>(io_after.ReadTransferCount - |
1489 io_before.ReadTransferCount) / 1024); | 1473 io_before.ReadTransferCount) / |
1474 1024); | |
1490 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", | 1475 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", |
1491 static_cast<int>(io_after.WriteTransferCount - | 1476 static_cast<int>(io_after.WriteTransferCount - |
1492 io_before.WriteTransferCount) / 1024); | 1477 io_before.WriteTransferCount) / |
1478 1024); | |
1493 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", | 1479 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", |
1494 static_cast<int>(io_after.ReadOperationCount - | 1480 static_cast<int>(io_after.ReadOperationCount - |
1495 io_before.ReadOperationCount)); | 1481 io_before.ReadOperationCount)); |
1496 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1482 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
1497 static_cast<int>(io_after.WriteOperationCount - | 1483 static_cast<int>(io_after.WriteOperationCount - |
1498 io_before.WriteOperationCount)); | 1484 io_before.WriteOperationCount)); |
1499 } | 1485 } |
1500 | 1486 |
1501 RecordFileSizeHistogram(db_filename); | 1487 RecordFileSizeHistogram(db_filename); |
1502 | 1488 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1602 // if something looks wrong. | 1588 // if something looks wrong. |
1603 | 1589 |
1604 const bool r1 = browse_store_->Delete(); | 1590 const bool r1 = browse_store_->Delete(); |
1605 if (!r1) | 1591 if (!r1) |
1606 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1592 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1607 | 1593 |
1608 const bool r2 = download_store_.get() ? download_store_->Delete() : true; | 1594 const bool r2 = download_store_.get() ? download_store_->Delete() : true; |
1609 if (!r2) | 1595 if (!r2) |
1610 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1596 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1611 | 1597 |
1612 const bool r3 = csd_whitelist_store_.get() ? | 1598 const bool r3 = |
1613 csd_whitelist_store_->Delete() : true; | 1599 csd_whitelist_store_.get() ? csd_whitelist_store_->Delete() : true; |
1614 if (!r3) | 1600 if (!r3) |
1615 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1601 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1616 | 1602 |
1617 const bool r4 = download_whitelist_store_.get() ? | 1603 const bool r4 = download_whitelist_store_.get() |
1618 download_whitelist_store_->Delete() : true; | 1604 ? download_whitelist_store_->Delete() |
1605 : true; | |
1619 if (!r4) | 1606 if (!r4) |
1620 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1607 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1621 | 1608 |
1622 const bool r5 = inclusion_whitelist_store_.get() ? | 1609 const bool r5 = inclusion_whitelist_store_.get() |
1623 inclusion_whitelist_store_->Delete() : true; | 1610 ? inclusion_whitelist_store_->Delete() |
1611 : true; | |
1624 if (!r5) | 1612 if (!r5) |
1625 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1613 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1626 | 1614 |
1627 const base::FilePath browse_filename = | 1615 const base::FilePath browse_filename = |
1628 BrowseDBFilename(db_state_manager_.filename_base()); | 1616 BrowseDBFilename(db_state_manager_.filename_base()); |
1629 const base::FilePath bloom_filter_filename = | 1617 const base::FilePath bloom_filter_filename = |
1630 BloomFilterForFilename(browse_filename); | 1618 BloomFilterForFilename(browse_filename); |
1631 const bool r6 = base::DeleteFile(bloom_filter_filename, false); | 1619 const bool r6 = base::DeleteFile(bloom_filter_filename, false); |
1632 if (!r6) | 1620 if (!r6) |
1633 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); | 1621 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1697 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1685 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1698 return; | 1686 return; |
1699 } | 1687 } |
1700 | 1688 |
1701 std::vector<SBFullHash> new_whitelist; | 1689 std::vector<SBFullHash> new_whitelist; |
1702 new_whitelist.reserve(full_hashes.size()); | 1690 new_whitelist.reserve(full_hashes.size()); |
1703 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); | 1691 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); |
1704 it != full_hashes.end(); ++it) { | 1692 it != full_hashes.end(); ++it) { |
1705 new_whitelist.push_back(it->full_hash); | 1693 new_whitelist.push_back(it->full_hash); |
1706 } | 1694 } |
1707 std::sort(new_whitelist.begin(), new_whitelist.end(), | 1695 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess); |
1708 safe_browsing::SBFullHashLess); | |
1709 | 1696 |
1710 SBFullHash kill_switch = safe_browsing::SBFullHashForString( | 1697 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl); |
1711 kWhitelistKillSwitchUrl); | |
1712 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), | 1698 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), |
1713 kill_switch, safe_browsing::SBFullHashLess)) { | 1699 kill_switch, SBFullHashLess)) { |
1714 // The kill switch is whitelisted hence we whitelist all URLs. | 1700 // The kill switch is whitelisted hence we whitelist all URLs. |
1715 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1701 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1716 } else { | 1702 } else { |
1717 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, | 1703 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, |
1718 &new_whitelist); | 1704 &new_whitelist); |
1719 } | 1705 } |
1720 } | 1706 } |
1721 | 1707 |
1722 void SafeBrowsingDatabaseNew::LoadIpBlacklist( | 1708 void SafeBrowsingDatabaseNew::LoadIpBlacklist( |
1723 const std::vector<SBAddFullHash>& full_hashes) { | 1709 const std::vector<SBAddFullHash>& full_hashes) { |
1724 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1710 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1725 | 1711 |
1726 IPBlacklist new_blacklist; | 1712 IPBlacklist new_blacklist; |
1727 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); | 1713 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); |
1728 it != full_hashes.end(); | 1714 it != full_hashes.end(); ++it) { |
1729 ++it) { | |
1730 const char* full_hash = it->full_hash.full_hash; | 1715 const char* full_hash = it->full_hash.full_hash; |
1731 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash)); | 1716 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash)); |
1732 // The format of the IP blacklist is: | 1717 // The format of the IP blacklist is: |
1733 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes. | 1718 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes. |
1734 std::string hashed_ip_prefix(full_hash, base::kSHA1Length); | 1719 std::string hashed_ip_prefix(full_hash, base::kSHA1Length); |
1735 size_t prefix_size = static_cast<uint8>(full_hash[base::kSHA1Length]); | 1720 size_t prefix_size = static_cast<uint8>(full_hash[base::kSHA1Length]); |
1736 if (prefix_size > kMaxIpPrefixSize || prefix_size < kMinIpPrefixSize) { | 1721 if (prefix_size > kMaxIpPrefixSize || prefix_size < kMinIpPrefixSize) { |
1737 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_INVALID); | 1722 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_INVALID); |
1738 new_blacklist.clear(); // Load empty blacklist. | 1723 new_blacklist.clear(); // Load empty blacklist. |
1739 break; | 1724 break; |
(...skipping 13 matching lines...) Expand all Loading... | |
1753 << " prefix_size:" << prefix_size | 1738 << " prefix_size:" << prefix_size |
1754 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), | 1739 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), |
1755 hashed_ip_prefix.size()); | 1740 hashed_ip_prefix.size()); |
1756 new_blacklist[mask].insert(hashed_ip_prefix); | 1741 new_blacklist[mask].insert(hashed_ip_prefix); |
1757 } | 1742 } |
1758 | 1743 |
1759 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); | 1744 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); |
1760 } | 1745 } |
1761 | 1746 |
1762 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { | 1747 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { |
1763 SBFullHash malware_kill_switch = safe_browsing::SBFullHashForString( | 1748 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); |
1764 kMalwareIPKillSwitchUrl); | |
1765 std::vector<SBFullHash> full_hashes; | 1749 std::vector<SBFullHash> full_hashes; |
1766 full_hashes.push_back(malware_kill_switch); | 1750 full_hashes.push_back(malware_kill_switch); |
1767 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 1751 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
1768 } | 1752 } |
1769 | 1753 |
1770 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1754 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1771 return state_manager_.BeginReadTransaction() | 1755 return state_manager_.BeginReadTransaction() |
1772 ->GetSBWhitelist(SBWhitelistId::CSD) | 1756 ->GetSBWhitelist(SBWhitelistId::CSD) |
1773 ->second; | 1757 ->second; |
1774 } | 1758 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1827 else | 1811 else |
1828 NOTREACHED(); // Add support for new lists above. | 1812 NOTREACHED(); // Add support for new lists above. |
1829 | 1813 |
1830 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | 1814 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. |
1831 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | 1815 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
1832 histogram_name, 1, 1000000, 50, | 1816 histogram_name, 1, 1000000, 50, |
1833 base::HistogramBase::kUmaTargetedHistogramFlag); | 1817 base::HistogramBase::kUmaTargetedHistogramFlag); |
1834 | 1818 |
1835 histogram_pointer->Add(file_size_kilobytes); | 1819 histogram_pointer->Add(file_size_kilobytes); |
1836 } | 1820 } |
1821 | |
1822 } // namespace safe_browsing | |
OLD | NEW |