Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1002)

Side by Side Diff: chrome/browser/safe_browsing/local_database_manager.cc

Issue 1848973004: Makes V4UpdateProtocolManager auto-schedule update fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_01_
Patch Set: git fetch && git pull && gclient sync Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/local_database_manager.h" 5 #include "chrome/browser/safe_browsing/local_database_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 16 matching lines...) Expand all
27 #include "chrome/browser/safe_browsing/protocol_manager.h" 27 #include "chrome/browser/safe_browsing/protocol_manager.h"
28 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 28 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
29 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 29 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
30 #include "chrome/browser/safe_browsing/ui_manager.h" 30 #include "chrome/browser/safe_browsing/ui_manager.h"
31 #include "chrome/common/chrome_constants.h" 31 #include "chrome/common/chrome_constants.h"
32 #include "chrome/common/chrome_paths.h" 32 #include "chrome/common/chrome_paths.h"
33 #include "chrome/common/chrome_switches.h" 33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
35 #include "components/prefs/pref_service.h" 35 #include "components/prefs/pref_service.h"
36 #include "components/safe_browsing_db/util.h" 36 #include "components/safe_browsing_db/util.h"
37 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
38 #include "content/public/browser/browser_thread.h" 37 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
40 #include "net/url_request/url_request_context_getter.h" 39 #include "net/url_request/url_request_context_getter.h"
41 #include "url/url_constants.h" 40 #include "url/url_constants.h"
42 41
43 using content::BrowserThread; 42 using content::BrowserThread;
44 43
45 namespace safe_browsing { 44 namespace safe_browsing {
46 45
47 namespace { 46 namespace {
(...skipping 12 matching lines...) Expand all
60 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_EMPTY; 59 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_EMPTY;
61 } else if (hit) { 60 } else if (hit) {
62 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_HIT; 61 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_HIT;
63 } else { 62 } else {
64 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_MISS; 63 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_MISS;
65 } 64 }
66 bool is_download = check_type == BINURL; 65 bool is_download = check_type == BINURL;
67 SafeBrowsingProtocolManager::RecordGetHashResult(is_download, result); 66 SafeBrowsingProtocolManager::RecordGetHashResult(is_download, result);
68 } 67 }
69 68
70 bool IsExpectedThreat( 69 bool IsExpectedThreat(const SBThreatType threat_type,
71 const SBThreatType threat_type, 70 const std::vector<SBThreatType>& expected_threats) {
72 const std::vector<SBThreatType>& expected_threats) {
73 return expected_threats.end() != std::find(expected_threats.begin(), 71 return expected_threats.end() != std::find(expected_threats.begin(),
74 expected_threats.end(), 72 expected_threats.end(),
75 threat_type); 73 threat_type);
76 } 74 }
77 75
78 // Returns threat level of the list. Lists with lower threat levels are more 76 // Returns threat level of the list. Lists with lower threat levels are more
79 // severe than lists with higher threat levels. Zero is the severest threat 77 // severe than lists with higher threat levels. Zero is the severest threat
80 // level possible. 78 // level possible.
81 int GetThreatSeverity(ListType threat) { 79 int GetThreatSeverity(ListType threat) {
82 switch (threat) { 80 switch (threat) {
83 case MALWARE: // Falls through. 81 case MALWARE: // Falls through.
84 case PHISH: // Falls through. 82 case PHISH: // Falls through.
85 case BINURL: // Falls through. 83 case BINURL: // Falls through.
86 case CSDWHITELIST: // Falls through. 84 case CSDWHITELIST: // Falls through.
87 case DOWNLOADWHITELIST: // Falls through. 85 case DOWNLOADWHITELIST: // Falls through.
88 case INCLUSIONWHITELIST: // Falls through. 86 case INCLUSIONWHITELIST: // Falls through.
89 case MODULEWHITELIST: // Falls through. 87 case MODULEWHITELIST: // Falls through.
90 case EXTENSIONBLACKLIST: // Falls through. 88 case EXTENSIONBLACKLIST: // Falls through.
91 case IPBLACKLIST: 89 case IPBLACKLIST:
92 return 0; 90 return 0;
93 case UNWANTEDURL: 91 case UNWANTEDURL:
94 // UNWANTEDURL is considered less severe than other threats. 92 // UNWANTEDURL is considered less severe than other threats.
95 return 1; 93 return 1;
96 case RESOURCEBLACKLIST: 94 case RESOURCEBLACKLIST:
97 // RESOURCEBLACKLIST is even less severe than UNWANTEDURL. 95 // RESOURCEBLACKLIST is even less severe than UNWANTEDURL.
98 return 2; 96 return 2;
99 case INVALID: 97 case INVALID:
100 return std::numeric_limits<int>::max(); 98 return std::numeric_limits<int>::max();
101 } 99 }
102 NOTREACHED(); 100 NOTREACHED();
103 return -1; 101 return -1;
104 } 102 }
105 103
106 // Return the severest list id from the results in |full_hashes| which matches 104 // Return the severest list id from the results in |full_hashes| which matches
107 // |hash|, or INVALID if none match. 105 // |hash|, or INVALID if none match.
108 ListType GetHashSeverestThreatListType( 106 ListType GetHashSeverestThreatListType(
109 const SBFullHash& hash, 107 const SBFullHash& hash,
110 const std::vector<SBFullHashResult>& full_hashes, 108 const std::vector<SBFullHashResult>& full_hashes,
111 size_t* index) { 109 size_t* index) {
112 ListType pending_threat = INVALID; 110 ListType pending_threat = INVALID;
113 int pending_threat_severity = GetThreatSeverity(INVALID); 111 int pending_threat_severity = GetThreatSeverity(INVALID);
114 for (size_t i = 0; i < full_hashes.size(); ++i) { 112 for (size_t i = 0; i < full_hashes.size(); ++i) {
115 if (SBFullHashEqual(hash, full_hashes[i].hash)) { 113 if (SBFullHashEqual(hash, full_hashes[i].hash)) {
116 const ListType threat = 114 const ListType threat = static_cast<ListType>(full_hashes[i].list_id);
117 static_cast<ListType>(full_hashes[i].list_id);
118 int threat_severity = GetThreatSeverity(threat); 115 int threat_severity = GetThreatSeverity(threat);
119 if (threat_severity < pending_threat_severity) { 116 if (threat_severity < pending_threat_severity) {
120 pending_threat = threat; 117 pending_threat = threat;
121 pending_threat_severity = threat_severity; 118 pending_threat_severity = threat_severity;
122 if (index) 119 if (index)
123 *index = i; 120 *index = i;
124 } 121 }
125 if (pending_threat_severity == 0) 122 if (pending_threat_severity == 0)
126 return pending_threat; 123 return pending_threat;
127 } 124 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 full_hashes(full_hashes), 207 full_hashes(full_hashes),
211 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE), 208 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE),
212 client(client), 209 client(client),
213 need_get_hash(false), 210 need_get_hash(false),
214 check_type(check_type), 211 check_type(check_type),
215 expected_threats(expected_threats) { 212 expected_threats(expected_threats) {
216 DCHECK_EQ(urls.empty(), !full_hashes.empty()) 213 DCHECK_EQ(urls.empty(), !full_hashes.empty())
217 << "Exactly one of urls and full_hashes must be set"; 214 << "Exactly one of urls and full_hashes must be set";
218 } 215 }
219 216
220 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::~SafeBrowsingCheck() { 217 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::~SafeBrowsingCheck() {}
221 }
222 218
223 void LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck:: 219 void LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::
224 OnSafeBrowsingResult() { 220 OnSafeBrowsingResult() {
225 DCHECK_CURRENTLY_ON(BrowserThread::IO); 221 DCHECK_CURRENTLY_ON(BrowserThread::IO);
226 222
227 DCHECK(client); 223 DCHECK(client);
228 DCHECK_EQ(urls.size(), url_results.size()); 224 DCHECK_EQ(urls.size(), url_results.size());
229 DCHECK_EQ(full_hashes.size(), full_hash_results.size()); 225 DCHECK_EQ(full_hashes.size(), full_hash_results.size());
230 if (!urls.empty()) { 226 if (!urls.empty()) {
231 DCHECK(full_hashes.empty()); 227 DCHECK(full_hashes.empty());
(...skipping 16 matching lines...) Expand all
248 url_hit_hash[0]); 244 url_hit_hash[0]);
249 break; 245 break;
250 default: 246 default:
251 NOTREACHED(); 247 NOTREACHED();
252 } 248 }
253 } else if (!full_hashes.empty()) { 249 } else if (!full_hashes.empty()) {
254 switch (check_type) { 250 switch (check_type) {
255 case EXTENSIONBLACKLIST: { 251 case EXTENSIONBLACKLIST: {
256 std::set<std::string> unsafe_extension_ids; 252 std::set<std::string> unsafe_extension_ids;
257 for (size_t i = 0; i < full_hashes.size(); ++i) { 253 for (size_t i = 0; i < full_hashes.size(); ++i) {
258 std::string extension_id = 254 std::string extension_id = SBFullHashToString(full_hashes[i]);
259 SBFullHashToString(full_hashes[i]);
260 if (full_hash_results[i] == SB_THREAT_TYPE_EXTENSION) 255 if (full_hash_results[i] == SB_THREAT_TYPE_EXTENSION)
261 unsafe_extension_ids.insert(extension_id); 256 unsafe_extension_ids.insert(extension_id);
262 } 257 }
263 client->OnCheckExtensionsResult(unsafe_extension_ids); 258 client->OnCheckExtensionsResult(unsafe_extension_ids);
264 break; 259 break;
265 } 260 }
266 default: 261 default:
267 NOTREACHED(); 262 NOTREACHED();
268 } 263 }
269 } else { 264 } else {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 return false; 331 return false;
337 } 332 }
338 333
339 bool LocalSafeBrowsingDatabaseManager::CanCheckResourceType( 334 bool LocalSafeBrowsingDatabaseManager::CanCheckResourceType(
340 content::ResourceType resource_type) const { 335 content::ResourceType resource_type) const {
341 // We check all types since most checks are fast. 336 // We check all types since most checks are fast.
342 return true; 337 return true;
343 } 338 }
344 339
345 bool LocalSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const { 340 bool LocalSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const {
346 return url.SchemeIs(url::kFtpScheme) || 341 return url.SchemeIs(url::kFtpScheme) || url.SchemeIs(url::kHttpScheme) ||
347 url.SchemeIs(url::kHttpScheme) ||
348 url.SchemeIs(url::kHttpsScheme); 342 url.SchemeIs(url::kHttpsScheme);
349 } 343 }
350 344
351 bool LocalSafeBrowsingDatabaseManager::CheckDownloadUrl( 345 bool LocalSafeBrowsingDatabaseManager::CheckDownloadUrl(
352 const std::vector<GURL>& url_chain, 346 const std::vector<GURL>& url_chain,
353 Client* client) { 347 Client* client) {
354 DCHECK_CURRENTLY_ON(BrowserThread::IO); 348 DCHECK_CURRENTLY_ON(BrowserThread::IO);
355 if (!enabled_ || !enable_download_protection_) 349 if (!enabled_ || !enable_download_protection_)
356 return true; 350 return true;
357 351
358 // We need to check the database for url prefix, and later may fetch the url 352 // We need to check the database for url prefix, and later may fetch the url
359 // from the safebrowsing backends. These need to be asynchronous. 353 // from the safebrowsing backends. These need to be asynchronous.
360 SafeBrowsingCheck* check = 354 SafeBrowsingCheck* check = new SafeBrowsingCheck(
361 new SafeBrowsingCheck(url_chain, 355 url_chain, std::vector<SBFullHash>(), client, BINURL,
362 std::vector<SBFullHash>(), 356 std::vector<SBThreatType>(1, SB_THREAT_TYPE_BINARY_MALWARE_URL));
363 client,
364 BINURL,
365 std::vector<SBThreatType>(1,
366 SB_THREAT_TYPE_BINARY_MALWARE_URL));
367 std::vector<SBPrefix> prefixes; 357 std::vector<SBPrefix> prefixes;
368 SafeBrowsingDatabase::GetDownloadUrlPrefixes(url_chain, &prefixes); 358 SafeBrowsingDatabase::GetDownloadUrlPrefixes(url_chain, &prefixes);
369 StartSafeBrowsingCheck( 359 StartSafeBrowsingCheck(
370 check, 360 check,
371 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread, 361 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread,
372 this, prefixes)); 362 this, prefixes));
373 return false; 363 return false;
374 } 364 }
375 365
376 bool LocalSafeBrowsingDatabaseManager::CheckExtensionIDs( 366 bool LocalSafeBrowsingDatabaseManager::CheckExtensionIDs(
377 const std::set<std::string>& extension_ids, Client* client) { 367 const std::set<std::string>& extension_ids,
368 Client* client) {
378 DCHECK_CURRENTLY_ON(BrowserThread::IO); 369 DCHECK_CURRENTLY_ON(BrowserThread::IO);
379 370
380 if (!enabled_ || !enable_extension_blacklist_) 371 if (!enabled_ || !enable_extension_blacklist_)
381 return true; 372 return true;
382 373
383 std::vector<SBFullHash> extension_id_hashes; 374 std::vector<SBFullHash> extension_id_hashes;
384 std::transform(extension_ids.begin(), extension_ids.end(), 375 std::transform(extension_ids.begin(), extension_ids.end(),
385 std::back_inserter(extension_id_hashes), 376 std::back_inserter(extension_id_hashes), StringToSBFullHash);
386 StringToSBFullHash);
387 std::vector<SBPrefix> prefixes; 377 std::vector<SBPrefix> prefixes;
388 for (const SBFullHash& hash : extension_id_hashes) 378 for (const SBFullHash& hash : extension_id_hashes)
389 prefixes.push_back(hash.prefix); 379 prefixes.push_back(hash.prefix);
390 380
391 SafeBrowsingCheck* check = new SafeBrowsingCheck( 381 SafeBrowsingCheck* check = new SafeBrowsingCheck(
392 std::vector<GURL>(), 382 std::vector<GURL>(), extension_id_hashes, client, EXTENSIONBLACKLIST,
393 extension_id_hashes,
394 client,
395 EXTENSIONBLACKLIST,
396 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION)); 383 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION));
397 StartSafeBrowsingCheck( 384 StartSafeBrowsingCheck(
398 check, 385 check,
399 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread, 386 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread,
400 this, prefixes)); 387 this, prefixes));
401 return false; 388 return false;
402 } 389 }
403 390
404 bool LocalSafeBrowsingDatabaseManager::CheckResourceUrl( 391 bool LocalSafeBrowsingDatabaseManager::CheckResourceUrl(const GURL& url,
405 const GURL& url, Client* client) { 392 Client* client) {
406 DCHECK_CURRENTLY_ON(BrowserThread::IO); 393 DCHECK_CURRENTLY_ON(BrowserThread::IO);
407 394
408 if (!enabled_ || !CanCheckUrl(url)) 395 if (!enabled_ || !CanCheckUrl(url))
409 return true; 396 return true;
410 397
411 std::vector<SBThreatType> expected_threats = 398 std::vector<SBThreatType> expected_threats = {
412 {SB_THREAT_TYPE_BLACKLISTED_RESOURCE}; 399 SB_THREAT_TYPE_BLACKLISTED_RESOURCE};
413 400
414 if (!MakeDatabaseAvailable()) { 401 if (!MakeDatabaseAvailable()) {
415 QueuedCheck queued_check(RESOURCEBLACKLIST, client, url, 402 QueuedCheck queued_check(RESOURCEBLACKLIST, client, url, expected_threats,
416 expected_threats, base::TimeTicks::Now()); 403 base::TimeTicks::Now());
417 queued_checks_.push_back(queued_check); 404 queued_checks_.push_back(queued_check);
418 return false; 405 return false;
419 } 406 }
420 407
421 SafeBrowsingCheck* check = 408 SafeBrowsingCheck* check =
422 new SafeBrowsingCheck({url}, std::vector<SBFullHash>(), client, 409 new SafeBrowsingCheck({url}, std::vector<SBFullHash>(), client,
423 RESOURCEBLACKLIST, expected_threats); 410 RESOURCEBLACKLIST, expected_threats);
424 411
425 std::vector<SBPrefix> prefixes; 412 std::vector<SBPrefix> prefixes;
426 SafeBrowsingDatabase::GetDownloadUrlPrefixes(check->urls, &prefixes); 413 SafeBrowsingDatabase::GetDownloadUrlPrefixes(check->urls, &prefixes);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 484
498 bool LocalSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() { 485 bool LocalSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() {
499 DCHECK_CURRENTLY_ON(BrowserThread::IO); 486 DCHECK_CURRENTLY_ON(BrowserThread::IO);
500 if (!enabled_ || !MakeDatabaseAvailable()) { 487 if (!enabled_ || !MakeDatabaseAvailable()) {
501 return true; 488 return true;
502 } 489 }
503 return database_->IsCsdWhitelistKillSwitchOn(); 490 return database_->IsCsdWhitelistKillSwitchOn();
504 } 491 }
505 492
506 bool LocalSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url, 493 bool LocalSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url,
507 Client* client) { 494 Client* client) {
508 DCHECK_CURRENTLY_ON(BrowserThread::IO); 495 DCHECK_CURRENTLY_ON(BrowserThread::IO);
509 if (!enabled_) 496 if (!enabled_)
510 return true; 497 return true;
511 498
512 if (!CanCheckUrl(url)) 499 if (!CanCheckUrl(url))
513 return true; 500 return true;
514 501
515 std::vector<SBThreatType> expected_threats; 502 std::vector<SBThreatType> expected_threats;
516 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); 503 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
517 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); 504 expected_threats.push_back(SB_THREAT_TYPE_URL_PHISHING);
518 expected_threats.push_back(SB_THREAT_TYPE_URL_UNWANTED); 505 expected_threats.push_back(SB_THREAT_TYPE_URL_UNWANTED);
519 506
520 const base::TimeTicks start = base::TimeTicks::Now(); 507 const base::TimeTicks start = base::TimeTicks::Now();
521 if (!MakeDatabaseAvailable()) { 508 if (!MakeDatabaseAvailable()) {
522 QueuedCheck queued_check(MALWARE, // or PHISH 509 QueuedCheck queued_check(MALWARE, // or PHISH
523 client, 510 client, url, expected_threats, start);
524 url,
525 expected_threats,
526 start);
527 queued_checks_.push_back(queued_check); 511 queued_checks_.push_back(queued_check);
528 return false; 512 return false;
529 } 513 }
530 514
531 // Cache hits should, in general, be the same for both (ignoring potential 515 // Cache hits should, in general, be the same for both (ignoring potential
532 // cache evictions in the second call for entries that were just about to be 516 // cache evictions in the second call for entries that were just about to be
533 // evicted in the first call). 517 // evicted in the first call).
534 // TODO(gab): Refactor SafeBrowsingDatabase to avoid depending on this here. 518 // TODO(gab): Refactor SafeBrowsingDatabase to avoid depending on this here.
535 std::vector<SBFullHashResult> cache_hits; 519 std::vector<SBFullHashResult> cache_hits;
536 520
537 std::vector<SBFullHash> full_hashes; 521 std::vector<SBFullHash> full_hashes;
538 UrlToFullHashes(url, false, &full_hashes); 522 UrlToFullHashes(url, false, &full_hashes);
539 523
540 std::vector<SBPrefix> browse_prefix_hits; 524 std::vector<SBPrefix> browse_prefix_hits;
541 bool browse_prefix_match = database_->ContainsBrowseHashes( 525 bool browse_prefix_match = database_->ContainsBrowseHashes(
542 full_hashes, &browse_prefix_hits, &cache_hits); 526 full_hashes, &browse_prefix_hits, &cache_hits);
543 527
544 std::vector<SBPrefix> unwanted_prefix_hits; 528 std::vector<SBPrefix> unwanted_prefix_hits;
545 std::vector<SBFullHashResult> unused_cache_hits; 529 std::vector<SBFullHashResult> unused_cache_hits;
546 bool unwanted_prefix_match = database_->ContainsUnwantedSoftwareHashes( 530 bool unwanted_prefix_match = database_->ContainsUnwantedSoftwareHashes(
547 full_hashes, &unwanted_prefix_hits, &unused_cache_hits); 531 full_hashes, &unwanted_prefix_hits, &unused_cache_hits);
548 532
549 // Merge the two pre-sorted prefix hits lists. 533 // Merge the two pre-sorted prefix hits lists.
550 // TODO(gab): Refactor SafeBrowsingDatabase for it to return this merged list 534 // TODO(gab): Refactor SafeBrowsingDatabase for it to return this merged list
551 // by default rather than building it here. 535 // by default rather than building it here.
552 std::vector<SBPrefix> prefix_hits(browse_prefix_hits.size() + 536 std::vector<SBPrefix> prefix_hits(browse_prefix_hits.size() +
553 unwanted_prefix_hits.size()); 537 unwanted_prefix_hits.size());
554 std::merge(browse_prefix_hits.begin(), 538 std::merge(browse_prefix_hits.begin(), browse_prefix_hits.end(),
555 browse_prefix_hits.end(), 539 unwanted_prefix_hits.begin(), unwanted_prefix_hits.end(),
556 unwanted_prefix_hits.begin(),
557 unwanted_prefix_hits.end(),
558 prefix_hits.begin()); 540 prefix_hits.begin());
559 prefix_hits.erase(std::unique(prefix_hits.begin(), prefix_hits.end()), 541 prefix_hits.erase(std::unique(prefix_hits.begin(), prefix_hits.end()),
560 prefix_hits.end()); 542 prefix_hits.end());
561 543
562 UMA_HISTOGRAM_TIMES("SB2.FilterCheck", base::TimeTicks::Now() - start); 544 UMA_HISTOGRAM_TIMES("SB2.FilterCheck", base::TimeTicks::Now() - start);
563 545
564 if (!browse_prefix_match && !unwanted_prefix_match) 546 if (!browse_prefix_match && !unwanted_prefix_match)
565 return true; // URL is okay. 547 return true; // URL is okay.
566 548
567 // Needs to be asynchronous, since we could be in the constructor of a 549 // Needs to be asynchronous, since we could be in the constructor of a
568 // ResourceDispatcherHost event handler which can't pause there. 550 // ResourceDispatcherHost event handler which can't pause there.
569 // This check will ping the Safe Browsing servers and get all lists which it 551 // This check will ping the Safe Browsing servers and get all lists which it
570 // matches. These lists will then be filtered against the |expected_threats| 552 // matches. These lists will then be filtered against the |expected_threats|
571 // and the result callback for MALWARE (which is the same as for PHISH and 553 // and the result callback for MALWARE (which is the same as for PHISH and
572 // UNWANTEDURL) will eventually be invoked with the final decision. 554 // UNWANTEDURL) will eventually be invoked with the final decision.
573 SafeBrowsingCheck* check = new SafeBrowsingCheck(std::vector<GURL>(1, url), 555 SafeBrowsingCheck* check = new SafeBrowsingCheck(
574 std::vector<SBFullHash>(), 556 std::vector<GURL>(1, url), std::vector<SBFullHash>(), client, MALWARE,
575 client, 557 expected_threats);
576 MALWARE,
577 expected_threats);
578 check->need_get_hash = cache_hits.empty(); 558 check->need_get_hash = cache_hits.empty();
579 check->prefix_hits.swap(prefix_hits); 559 check->prefix_hits.swap(prefix_hits);
580 check->cache_hits.swap(cache_hits); 560 check->cache_hits.swap(cache_hits);
581 checks_.insert(check); 561 checks_.insert(check);
582 562
583 BrowserThread::PostTask( 563 BrowserThread::PostTask(
584 BrowserThread::IO, FROM_HERE, 564 BrowserThread::IO, FROM_HERE,
585 base::Bind(&LocalSafeBrowsingDatabaseManager::OnCheckDone, this, check)); 565 base::Bind(&LocalSafeBrowsingDatabaseManager::OnCheckDone, this, check));
586 566
587 return false; 567 return false;
588 } 568 }
589 569
590 void LocalSafeBrowsingDatabaseManager::CancelCheck(Client* client) { 570 void LocalSafeBrowsingDatabaseManager::CancelCheck(Client* client) {
591 DCHECK_CURRENTLY_ON(BrowserThread::IO); 571 DCHECK_CURRENTLY_ON(BrowserThread::IO);
592 for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) { 572 for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) {
593 // We can't delete matching checks here because the db thread has a copy of 573 // We can't delete matching checks here because the db thread has a copy of
594 // the pointer. Instead, we simply NULL out the client, and when the db 574 // the pointer. Instead, we simply NULL out the client, and when the db
595 // thread calls us back, we'll clean up the check. 575 // thread calls us back, we'll clean up the check.
596 if ((*i)->client == client) 576 if ((*i)->client == client)
597 (*i)->client = NULL; 577 (*i)->client = NULL;
598 } 578 }
599 579
600 // Scan the queued clients store. Clients may be here if they requested a URL 580 // Scan the queued clients store. Clients may be here if they requested a URL
601 // check before the database has finished loading. 581 // check before the database has finished loading.
602 for (std::deque<QueuedCheck>::iterator it(queued_checks_.begin()); 582 for (std::deque<QueuedCheck>::iterator it(queued_checks_.begin());
603 it != queued_checks_.end(); ) { 583 it != queued_checks_.end();) {
604 // In this case it's safe to delete matches entirely since nothing has a 584 // In this case it's safe to delete matches entirely since nothing has a
605 // pointer to them. 585 // pointer to them.
606 if (it->client == client) 586 if (it->client == client)
607 it = queued_checks_.erase(it); 587 it = queued_checks_.erase(it);
608 else 588 else
609 ++it; 589 ++it;
610 } 590 }
611 } 591 }
612 592
613 void LocalSafeBrowsingDatabaseManager::HandleGetHashResults( 593 void LocalSafeBrowsingDatabaseManager::HandleGetHashResults(
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 DCHECK_CURRENTLY_ON(BrowserThread::IO); 633 DCHECK_CURRENTLY_ON(BrowserThread::IO);
654 DCHECK(enabled_); 634 DCHECK(enabled_);
655 DCHECK(!callback.is_null()); 635 DCHECK(!callback.is_null());
656 safe_browsing_task_runner_->PostTask( 636 safe_browsing_task_runner_->PostTask(
657 FROM_HERE, 637 FROM_HERE,
658 base::Bind(&LocalSafeBrowsingDatabaseManager::AddDatabaseChunks, this, 638 base::Bind(&LocalSafeBrowsingDatabaseManager::AddDatabaseChunks, this,
659 list, base::Passed(&chunks), callback)); 639 list, base::Passed(&chunks), callback));
660 } 640 }
661 641
662 void LocalSafeBrowsingDatabaseManager::DeleteChunks( 642 void LocalSafeBrowsingDatabaseManager::DeleteChunks(
663 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { 643 scoped_ptr<std::vector<SBChunkDelete>> chunk_deletes) {
664 DCHECK_CURRENTLY_ON(BrowserThread::IO); 644 DCHECK_CURRENTLY_ON(BrowserThread::IO);
665 DCHECK(enabled_); 645 DCHECK(enabled_);
666 safe_browsing_task_runner_->PostTask( 646 safe_browsing_task_runner_->PostTask(
667 FROM_HERE, 647 FROM_HERE,
668 base::Bind(&LocalSafeBrowsingDatabaseManager::DeleteDatabaseChunks, this, 648 base::Bind(&LocalSafeBrowsingDatabaseManager::DeleteDatabaseChunks, this,
669 base::Passed(&chunk_deletes))); 649 base::Passed(&chunk_deletes)));
670 } 650 }
671 651
672 void LocalSafeBrowsingDatabaseManager::UpdateStarted() { 652 void LocalSafeBrowsingDatabaseManager::UpdateStarted() {
673 DCHECK_CURRENTLY_ON(BrowserThread::IO); 653 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 LocalSafeBrowsingDatabaseManager::QueuedCheck::QueuedCheck( 722 LocalSafeBrowsingDatabaseManager::QueuedCheck::QueuedCheck(
743 const ListType check_type, 723 const ListType check_type,
744 Client* client, 724 Client* client,
745 const GURL& url, 725 const GURL& url,
746 const std::vector<SBThreatType>& expected_threats, 726 const std::vector<SBThreatType>& expected_threats,
747 const base::TimeTicks& start) 727 const base::TimeTicks& start)
748 : check_type(check_type), 728 : check_type(check_type),
749 client(client), 729 client(client),
750 url(url), 730 url(url),
751 expected_threats(expected_threats), 731 expected_threats(expected_threats),
752 start(start) { 732 start(start) {}
753 }
754 733
755 LocalSafeBrowsingDatabaseManager::QueuedCheck::QueuedCheck( 734 LocalSafeBrowsingDatabaseManager::QueuedCheck::QueuedCheck(
756 const QueuedCheck& other) = default; 735 const QueuedCheck& other) = default;
757 736
758 LocalSafeBrowsingDatabaseManager::QueuedCheck::~QueuedCheck() { 737 LocalSafeBrowsingDatabaseManager::QueuedCheck::~QueuedCheck() {}
759 }
760 738
761 void LocalSafeBrowsingDatabaseManager::DoStopOnIOThread() { 739 void LocalSafeBrowsingDatabaseManager::DoStopOnIOThread() {
762 DCHECK_CURRENTLY_ON(BrowserThread::IO); 740 DCHECK_CURRENTLY_ON(BrowserThread::IO);
763 741
764 if (!enabled_) 742 if (!enabled_)
765 return; 743 return;
766 744
767 enabled_ = false; 745 enabled_ = false;
768 746
769 // Delete queued checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'. 747 // Delete queued checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'.
770 while (!queued_checks_.empty()) { 748 while (!queued_checks_.empty()) {
771 QueuedCheck queued = queued_checks_.front(); 749 QueuedCheck queued = queued_checks_.front();
772 if (queued.client) { 750 if (queued.client) {
773 SafeBrowsingCheck sb_check(std::vector<GURL>(1, queued.url), 751 SafeBrowsingCheck sb_check(std::vector<GURL>(1, queued.url),
774 std::vector<SBFullHash>(), 752 std::vector<SBFullHash>(), queued.client,
775 queued.client, 753 queued.check_type, queued.expected_threats);
776 queued.check_type,
777 queued.expected_threats);
778 sb_check.OnSafeBrowsingResult(); 754 sb_check.OnSafeBrowsingResult();
779 } 755 }
780 queued_checks_.pop_front(); 756 queued_checks_.pop_front();
781 } 757 }
782 758
783 // Close the database. Cases to avoid: 759 // Close the database. Cases to avoid:
784 // * If |closing_database_| is true, continuing will queue up a second 760 // * If |closing_database_| is true, continuing will queue up a second
785 // request, |closing_database_| will be reset after handling the first 761 // request, |closing_database_| will be reset after handling the first
786 // request, and if any functions on the db thread recreate the database, we 762 // request, and if any functions on the db thread recreate the database, we
787 // could start using it on the IO thread and then have the second request 763 // could start using it on the IO thread and then have the second request
788 // handler delete it out from under us. 764 // handler delete it out from under us.
789 // * If |database_| is NULL, then either no creation request is in flight, in 765 // * If |database_| is NULL, then either no creation request is in flight, in
790 // which case we don't need to do anything, or one is in flight, in which 766 // which case we don't need to do anything, or one is in flight, in which
791 // case the database will be recreated before our deletion request is 767 // case the database will be recreated before our deletion request is
792 // handled, and could be used on the IO thread in that time period, leading 768 // handled, and could be used on the IO thread in that time period, leading
793 // to the same problem as above. 769 // to the same problem as above.
794 // Checking DatabaseAvailable() avoids both of these. 770 // Checking DatabaseAvailable() avoids both of these.
795 if (DatabaseAvailable()) { 771 if (DatabaseAvailable()) {
796 closing_database_ = true; 772 closing_database_ = true;
797 safe_browsing_task_runner_->PostTask( 773 safe_browsing_task_runner_->PostTask(
798 FROM_HERE, 774 FROM_HERE,
799 base::Bind(&LocalSafeBrowsingDatabaseManager::OnCloseDatabase, this)); 775 base::Bind(&LocalSafeBrowsingDatabaseManager::OnCloseDatabase, this));
800 } 776 }
801 777
802 // Delete pending checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'. 778 // Delete pending checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'.
803 // We have to do this after the db thread returns because methods on it can 779 // We have to do this after the db thread returns because methods on it can
804 // have copies of these pointers, so deleting them might lead to accessing 780 // have copies of these pointers, so deleting them might lead to accessing
805 // garbage. 781 // garbage.
806 for (CurrentChecks::iterator it = checks_.begin(); 782 for (CurrentChecks::iterator it = checks_.begin(); it != checks_.end();
807 it != checks_.end(); ++it) { 783 ++it) {
808 SafeBrowsingCheck* check = *it; 784 SafeBrowsingCheck* check = *it;
809 if (check->client) 785 if (check->client)
810 check->OnSafeBrowsingResult(); 786 check->OnSafeBrowsingResult();
811 } 787 }
812 STLDeleteElements(&checks_); 788 STLDeleteElements(&checks_);
813 789
814 gethash_requests_.clear(); 790 gethash_requests_.clear();
815 } 791 }
816 792
817 bool LocalSafeBrowsingDatabaseManager::DatabaseAvailable() const { 793 bool LocalSafeBrowsingDatabaseManager::DatabaseAvailable() const {
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 while (!queued_checks_.empty()) { 995 while (!queued_checks_.empty()) {
1020 QueuedCheck check = queued_checks_.front(); 996 QueuedCheck check = queued_checks_.front();
1021 DCHECK(!check.start.is_null()); 997 DCHECK(!check.start.is_null());
1022 LOCAL_HISTOGRAM_TIMES("SB.QueueDelay", 998 LOCAL_HISTOGRAM_TIMES("SB.QueueDelay",
1023 base::TimeTicks::Now() - check.start); 999 base::TimeTicks::Now() - check.start);
1024 // If CheckUrl() determines the URL is safe immediately, it doesn't call the 1000 // If CheckUrl() determines the URL is safe immediately, it doesn't call the
1025 // client's handler function (because normally it's being directly called by 1001 // client's handler function (because normally it's being directly called by
1026 // the client). Since we're not the client, we have to convey this result. 1002 // the client). Since we're not the client, we have to convey this result.
1027 if (check.client && CheckBrowseUrl(check.url, check.client)) { 1003 if (check.client && CheckBrowseUrl(check.url, check.client)) {
1028 SafeBrowsingCheck sb_check(std::vector<GURL>(1, check.url), 1004 SafeBrowsingCheck sb_check(std::vector<GURL>(1, check.url),
1029 std::vector<SBFullHash>(), 1005 std::vector<SBFullHash>(), check.client,
1030 check.client, 1006 check.check_type, check.expected_threats);
1031 check.check_type,
1032 check.expected_threats);
1033 sb_check.OnSafeBrowsingResult(); 1007 sb_check.OnSafeBrowsingResult();
1034 } 1008 }
1035 queued_checks_.pop_front(); 1009 queued_checks_.pop_front();
1036 } 1010 }
1037 } 1011 }
1038 1012
1039 void LocalSafeBrowsingDatabaseManager::AddDatabaseChunks( 1013 void LocalSafeBrowsingDatabaseManager::AddDatabaseChunks(
1040 const std::string& list_name, 1014 const std::string& list_name,
1041 scoped_ptr<std::vector<scoped_ptr<SBChunkData>>> chunks, 1015 scoped_ptr<std::vector<scoped_ptr<SBChunkData>>> chunks,
1042 AddChunksCallback callback) { 1016 AddChunksCallback callback) {
1043 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 1017 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
1044 if (chunks) 1018 if (chunks)
1045 GetDatabase()->InsertChunks(list_name, *chunks); 1019 GetDatabase()->InsertChunks(list_name, *chunks);
1046 BrowserThread::PostTask( 1020 BrowserThread::PostTask(
1047 BrowserThread::IO, FROM_HERE, 1021 BrowserThread::IO, FROM_HERE,
1048 base::Bind(&LocalSafeBrowsingDatabaseManager::OnAddChunksComplete, this, 1022 base::Bind(&LocalSafeBrowsingDatabaseManager::OnAddChunksComplete, this,
1049 callback)); 1023 callback));
1050 } 1024 }
1051 1025
1052 void LocalSafeBrowsingDatabaseManager::DeleteDatabaseChunks( 1026 void LocalSafeBrowsingDatabaseManager::DeleteDatabaseChunks(
1053 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { 1027 scoped_ptr<std::vector<SBChunkDelete>> chunk_deletes) {
1054 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 1028 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
1055 if (chunk_deletes) 1029 if (chunk_deletes)
1056 GetDatabase()->DeleteChunks(*chunk_deletes); 1030 GetDatabase()->DeleteChunks(*chunk_deletes);
1057 } 1031 }
1058 1032
1059 void LocalSafeBrowsingDatabaseManager::DatabaseUpdateFinished( 1033 void LocalSafeBrowsingDatabaseManager::DatabaseUpdateFinished(
1060 bool update_succeeded) { 1034 bool update_succeeded) {
1061 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 1035 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
1062 GetDatabase()->UpdateFinished(update_succeeded); 1036 GetDatabase()->UpdateFinished(update_succeeded);
1063 DCHECK(database_update_in_progress_); 1037 DCHECK(database_update_in_progress_);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 } 1121 }
1148 } 1122 }
1149 1123
1150 if (expected_full_hashes.empty()) { 1124 if (expected_full_hashes.empty()) {
1151 SafeBrowsingCheckDone(check); 1125 SafeBrowsingCheckDone(check);
1152 return false; 1126 return false;
1153 } 1127 }
1154 1128
1155 for (size_t i = 0; i < check->urls.size(); ++i) { 1129 for (size_t i = 0; i < check->urls.size(); ++i) {
1156 size_t threat_index; 1130 size_t threat_index;
1157 SBThreatType threat = GetUrlSeverestThreatType(check->urls[i], 1131 SBThreatType threat = GetUrlSeverestThreatType(
1158 expected_full_hashes, 1132 check->urls[i], expected_full_hashes, &threat_index);
1159 &threat_index);
1160 if (threat != SB_THREAT_TYPE_SAFE) { 1133 if (threat != SB_THREAT_TYPE_SAFE) {
1161 check->url_results[i] = threat; 1134 check->url_results[i] = threat;
1162 check->url_metadata[i] = expected_full_hashes[threat_index].metadata; 1135 check->url_metadata[i] = expected_full_hashes[threat_index].metadata;
1163 const SBFullHash& hash = expected_full_hashes[threat_index].hash; 1136 const SBFullHash& hash = expected_full_hashes[threat_index].hash;
1164 check->url_hit_hash[i] = std::string(hash.full_hash, 1137 check->url_hit_hash[i] =
1165 arraysize(hash.full_hash)); 1138 std::string(hash.full_hash, arraysize(hash.full_hash));
1166 is_threat = true; 1139 is_threat = true;
1167 } 1140 }
1168 } 1141 }
1169 1142
1170 for (size_t i = 0; i < check->full_hashes.size(); ++i) { 1143 for (size_t i = 0; i < check->full_hashes.size(); ++i) {
1171 SBThreatType threat = 1144 SBThreatType threat =
1172 GetHashSeverestThreatType(check->full_hashes[i], expected_full_hashes); 1145 GetHashSeverestThreatType(check->full_hashes[i], expected_full_hashes);
1173 if (threat != SB_THREAT_TYPE_SAFE) { 1146 if (threat != SB_THREAT_TYPE_SAFE) {
1174 check->full_hash_results[i] = threat; 1147 check->full_hash_results[i] = threat;
1175 is_threat = true; 1148 is_threat = true;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 FROM_HERE, base::Bind(&LocalSafeBrowsingDatabaseManager::TimeoutCallback, 1252 FROM_HERE, base::Bind(&LocalSafeBrowsingDatabaseManager::TimeoutCallback,
1280 check->weak_ptr_factory_->GetWeakPtr(), check), 1253 check->weak_ptr_factory_->GetWeakPtr(), check),
1281 check_timeout_); 1254 check_timeout_);
1282 } 1255 }
1283 1256
1284 bool LocalSafeBrowsingDatabaseManager::download_protection_enabled() const { 1257 bool LocalSafeBrowsingDatabaseManager::download_protection_enabled() const {
1285 return enable_download_protection_; 1258 return enable_download_protection_;
1286 } 1259 }
1287 1260
1288 } // namespace safe_browsing 1261 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/local_database_manager.h ('k') | components/safe_browsing_db/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698