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

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

Issue 1110723002: Split to SafeBrowsingDatabaseManager into Local* and Remote*. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to review. Tweak comments and list initializer. Created 5 years, 7 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/database_manager.h" 5 #include "chrome/browser/safe_browsing/local_database_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/debug/leak_tracker.h" 13 #include "base/debug/leak_tracker.h"
14 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 return SB_THREAT_TYPE_EXTENSION; 159 return SB_THREAT_TYPE_EXTENSION;
160 default: 160 default:
161 DVLOG(1) << "Unknown safe browsing list id " << list_type; 161 DVLOG(1) << "Unknown safe browsing list id " << list_type;
162 return SB_THREAT_TYPE_SAFE; 162 return SB_THREAT_TYPE_SAFE;
163 } 163 }
164 } 164 }
165 165
166 } // namespace 166 } // namespace
167 167
168 // static 168 // static
169 SBThreatType SafeBrowsingDatabaseManager::GetHashSeverestThreatType( 169 SBThreatType LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType(
170 const SBFullHash& hash, 170 const SBFullHash& hash,
171 const std::vector<SBFullHashResult>& full_hashes) { 171 const std::vector<SBFullHashResult>& full_hashes) {
172 return GetThreatTypeFromListType( 172 return GetThreatTypeFromListType(
173 GetHashSeverestThreatListType(hash, full_hashes, NULL)); 173 GetHashSeverestThreatListType(hash, full_hashes, NULL));
174 } 174 }
175 175
176 // static 176 // static
177 SBThreatType SafeBrowsingDatabaseManager::GetUrlSeverestThreatType( 177 SBThreatType LocalSafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
178 const GURL& url, 178 const GURL& url,
179 const std::vector<SBFullHashResult>& full_hashes, 179 const std::vector<SBFullHashResult>& full_hashes,
180 size_t* index) { 180 size_t* index) {
181 return GetThreatTypeFromListType( 181 return GetThreatTypeFromListType(
182 GetUrlSeverestThreatListType(url, full_hashes, index)); 182 GetUrlSeverestThreatListType(url, full_hashes, index));
183 } 183 }
184 184
185 SafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck( 185 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck(
186 const std::vector<GURL>& urls, 186 const std::vector<GURL>& urls,
187 const std::vector<SBFullHash>& full_hashes, 187 const std::vector<SBFullHash>& full_hashes,
188 Client* client, 188 Client* client,
189 safe_browsing_util::ListType check_type, 189 safe_browsing_util::ListType check_type,
190 const std::vector<SBThreatType>& expected_threats) 190 const std::vector<SBThreatType>& expected_threats)
191 : urls(urls), 191 : urls(urls),
192 url_results(urls.size(), SB_THREAT_TYPE_SAFE), 192 url_results(urls.size(), SB_THREAT_TYPE_SAFE),
193 url_metadata(urls.size()), 193 url_metadata(urls.size()),
194 full_hashes(full_hashes), 194 full_hashes(full_hashes),
195 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE), 195 full_hash_results(full_hashes.size(), SB_THREAT_TYPE_SAFE),
196 client(client), 196 client(client),
197 need_get_hash(false), 197 need_get_hash(false),
198 check_type(check_type), 198 check_type(check_type),
199 expected_threats(expected_threats) { 199 expected_threats(expected_threats) {
200 DCHECK_EQ(urls.empty(), !full_hashes.empty()) 200 DCHECK_EQ(urls.empty(), !full_hashes.empty())
201 << "Exactly one of urls and full_hashes must be set"; 201 << "Exactly one of urls and full_hashes must be set";
202 } 202 }
203 203
204 SafeBrowsingDatabaseManager::SafeBrowsingCheck::~SafeBrowsingCheck() {} 204 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::~SafeBrowsingCheck() {
205 }
205 206
206 void SafeBrowsingDatabaseManager::Client::OnSafeBrowsingResult( 207 void LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck::
207 const SafeBrowsingCheck& check) { 208 OnSafeBrowsingResult() {
208 DCHECK_CURRENTLY_ON(BrowserThread::IO); 209 DCHECK_CURRENTLY_ON(BrowserThread::IO);
209 210
210 DCHECK_EQ(check.urls.size(), check.url_results.size()); 211 DCHECK(client);
211 DCHECK_EQ(check.full_hashes.size(), check.full_hash_results.size()); 212 DCHECK_EQ(urls.size(), url_results.size());
212 if (!check.urls.empty()) { 213 DCHECK_EQ(full_hashes.size(), full_hash_results.size());
213 DCHECK(check.full_hashes.empty()); 214 if (!urls.empty()) {
214 switch (check.check_type) { 215 DCHECK(full_hashes.empty());
216 switch (check_type) {
215 case safe_browsing_util::MALWARE: 217 case safe_browsing_util::MALWARE:
216 case safe_browsing_util::PHISH: 218 case safe_browsing_util::PHISH:
217 case safe_browsing_util::UNWANTEDURL: 219 case safe_browsing_util::UNWANTEDURL:
218 DCHECK_EQ(1u, check.urls.size()); 220 DCHECK_EQ(1u, urls.size());
219 OnCheckBrowseUrlResult( 221 client->OnCheckBrowseUrlResult(urls[0], url_results[0],
220 check.urls[0], check.url_results[0], check.url_metadata[0]); 222 url_metadata[0]);
221 break; 223 break;
222 case safe_browsing_util::BINURL: 224 case safe_browsing_util::BINURL:
223 DCHECK_EQ(check.urls.size(), check.url_results.size()); 225 DCHECK_EQ(urls.size(), url_results.size());
224 OnCheckDownloadUrlResult( 226 client->OnCheckDownloadUrlResult(
225 check.urls, 227 urls, *std::max_element(url_results.begin(), url_results.end()));
226 *std::max_element(check.url_results.begin(),
227 check.url_results.end()));
228 break; 228 break;
229 default: 229 default:
230 NOTREACHED(); 230 NOTREACHED();
231 } 231 }
232 } else if (!check.full_hashes.empty()) { 232 } else if (!full_hashes.empty()) {
233 switch (check.check_type) { 233 switch (check_type) {
234 case safe_browsing_util::EXTENSIONBLACKLIST: { 234 case safe_browsing_util::EXTENSIONBLACKLIST: {
235 std::set<std::string> unsafe_extension_ids; 235 std::set<std::string> unsafe_extension_ids;
236 for (size_t i = 0; i < check.full_hashes.size(); ++i) { 236 for (size_t i = 0; i < full_hashes.size(); ++i) {
237 std::string extension_id = 237 std::string extension_id =
238 safe_browsing_util::SBFullHashToString(check.full_hashes[i]); 238 safe_browsing_util::SBFullHashToString(full_hashes[i]);
239 if (check.full_hash_results[i] == SB_THREAT_TYPE_EXTENSION) 239 if (full_hash_results[i] == SB_THREAT_TYPE_EXTENSION)
240 unsafe_extension_ids.insert(extension_id); 240 unsafe_extension_ids.insert(extension_id);
241 } 241 }
242 OnCheckExtensionsResult(unsafe_extension_ids); 242 client->OnCheckExtensionsResult(unsafe_extension_ids);
243 break; 243 break;
244 } 244 }
245 default: 245 default:
246 NOTREACHED(); 246 NOTREACHED();
247 } 247 }
248 } else { 248 } else {
249 NOTREACHED(); 249 NOTREACHED();
250 } 250 }
251 } 251 }
252 252
253 SafeBrowsingDatabaseManager::SafeBrowsingDatabaseManager( 253 LocalSafeBrowsingDatabaseManager::LocalSafeBrowsingDatabaseManager(
254 const scoped_refptr<SafeBrowsingService>& service) 254 const scoped_refptr<SafeBrowsingService>& service)
255 : sb_service_(service), 255 : sb_service_(service),
256 database_(NULL), 256 database_(NULL),
257 enabled_(false), 257 enabled_(false),
258 enable_download_protection_(false), 258 enable_download_protection_(false),
259 enable_csd_whitelist_(false), 259 enable_csd_whitelist_(false),
260 enable_download_whitelist_(false), 260 enable_download_whitelist_(false),
261 enable_extension_blacklist_(false), 261 enable_extension_blacklist_(false),
262 enable_ip_blacklist_(false), 262 enable_ip_blacklist_(false),
263 enable_unwanted_software_blacklist_(false), 263 enable_unwanted_software_blacklist_(false),
(...skipping 29 matching lines...) Expand all
293 // The client-side IP blacklist feature is tightly integrated with client-side 293 // The client-side IP blacklist feature is tightly integrated with client-side
294 // phishing protection for now. 294 // phishing protection for now.
295 enable_ip_blacklist_ = enable_csd_whitelist_; 295 enable_ip_blacklist_ = enable_csd_whitelist_;
296 296
297 // The UwS blacklist feature is controlled by a flag for M40. 297 // The UwS blacklist feature is controlled by a flag for M40.
298 enable_unwanted_software_blacklist_ = 298 enable_unwanted_software_blacklist_ =
299 safe_browsing_util::GetUnwantedTrialGroup() > safe_browsing_util::UWS_OFF; 299 safe_browsing_util::GetUnwantedTrialGroup() > safe_browsing_util::UWS_OFF;
300 #endif 300 #endif
301 } 301 }
302 302
303 SafeBrowsingDatabaseManager::~SafeBrowsingDatabaseManager() { 303 LocalSafeBrowsingDatabaseManager::~LocalSafeBrowsingDatabaseManager() {
304 // The DCHECK is disabled due to crbug.com/438754. 304 // The DCHECK is disabled due to crbug.com/438754.
305 // DCHECK_CURRENTLY_ON(BrowserThread::UI); 305 // DCHECK_CURRENTLY_ON(BrowserThread::UI);
306 306
307 // We should have already been shut down. If we're still enabled, then the 307 // We should have already been shut down. If we're still enabled, then the
308 // database isn't going to be closed properly, which could lead to corruption. 308 // database isn't going to be closed properly, which could lead to corruption.
309 DCHECK(!enabled_); 309 DCHECK(!enabled_);
310 } 310 }
311 311
312 bool SafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const { 312 bool LocalSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const {
313 return url.SchemeIs(url::kFtpScheme) || 313 return url.SchemeIs(url::kFtpScheme) ||
314 url.SchemeIs(url::kHttpScheme) || 314 url.SchemeIs(url::kHttpScheme) ||
315 url.SchemeIs(url::kHttpsScheme); 315 url.SchemeIs(url::kHttpsScheme);
316 } 316 }
317 317
318 bool SafeBrowsingDatabaseManager::CheckDownloadUrl( 318 bool LocalSafeBrowsingDatabaseManager::CheckDownloadUrl(
319 const std::vector<GURL>& url_chain, 319 const std::vector<GURL>& url_chain,
320 Client* client) { 320 Client* client) {
321 DCHECK_CURRENTLY_ON(BrowserThread::IO); 321 DCHECK_CURRENTLY_ON(BrowserThread::IO);
322 if (!enabled_ || !enable_download_protection_) 322 if (!enabled_ || !enable_download_protection_)
323 return true; 323 return true;
324 324
325 // We need to check the database for url prefix, and later may fetch the url 325 // We need to check the database for url prefix, and later may fetch the url
326 // from the safebrowsing backends. These need to be asynchronous. 326 // from the safebrowsing backends. These need to be asynchronous.
327 SafeBrowsingCheck* check = 327 SafeBrowsingCheck* check =
328 new SafeBrowsingCheck(url_chain, 328 new SafeBrowsingCheck(url_chain,
329 std::vector<SBFullHash>(), 329 std::vector<SBFullHash>(),
330 client, 330 client,
331 safe_browsing_util::BINURL, 331 safe_browsing_util::BINURL,
332 std::vector<SBThreatType>(1, 332 std::vector<SBThreatType>(1,
333 SB_THREAT_TYPE_BINARY_MALWARE_URL)); 333 SB_THREAT_TYPE_BINARY_MALWARE_URL));
334 std::vector<SBPrefix> prefixes; 334 std::vector<SBPrefix> prefixes;
335 SafeBrowsingDatabase::GetDownloadUrlPrefixes(url_chain, &prefixes); 335 SafeBrowsingDatabase::GetDownloadUrlPrefixes(url_chain, &prefixes);
336 StartSafeBrowsingCheck( 336 StartSafeBrowsingCheck(
337 check, 337 check,
338 base::Bind(&SafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread, this, 338 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread,
339 prefixes)); 339 this, prefixes));
340 return false; 340 return false;
341 } 341 }
342 342
343 bool SafeBrowsingDatabaseManager::CheckExtensionIDs( 343 bool LocalSafeBrowsingDatabaseManager::CheckExtensionIDs(
344 const std::set<std::string>& extension_ids, 344 const std::set<std::string>& extension_ids,
345 Client* client) { 345 Client* client) {
346 DCHECK_CURRENTLY_ON(BrowserThread::IO); 346 DCHECK_CURRENTLY_ON(BrowserThread::IO);
347 347
348 if (!enabled_ || !enable_extension_blacklist_) 348 if (!enabled_ || !enable_extension_blacklist_)
349 return true; 349 return true;
350 350
351 std::vector<SBFullHash> extension_id_hashes; 351 std::vector<SBFullHash> extension_id_hashes;
352 std::transform(extension_ids.begin(), extension_ids.end(), 352 std::transform(extension_ids.begin(), extension_ids.end(),
353 std::back_inserter(extension_id_hashes), 353 std::back_inserter(extension_id_hashes),
354 safe_browsing_util::StringToSBFullHash); 354 safe_browsing_util::StringToSBFullHash);
355 std::vector<SBPrefix> prefixes; 355 std::vector<SBPrefix> prefixes;
356 for (const SBFullHash& hash : extension_id_hashes) 356 for (const SBFullHash& hash : extension_id_hashes)
357 prefixes.push_back(hash.prefix); 357 prefixes.push_back(hash.prefix);
358 358
359 SafeBrowsingCheck* check = new SafeBrowsingCheck( 359 SafeBrowsingCheck* check = new SafeBrowsingCheck(
360 std::vector<GURL>(), 360 std::vector<GURL>(),
361 extension_id_hashes, 361 extension_id_hashes,
362 client, 362 client,
363 safe_browsing_util::EXTENSIONBLACKLIST, 363 safe_browsing_util::EXTENSIONBLACKLIST,
364 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION)); 364 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION));
365 StartSafeBrowsingCheck( 365 StartSafeBrowsingCheck(
366 check, 366 check,
367 base::Bind(&SafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread, 367 base::Bind(&LocalSafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread,
368 this, prefixes)); 368 this, prefixes));
369 return false; 369 return false;
370 } 370 }
371 371
372 bool SafeBrowsingDatabaseManager::MatchMalwareIP( 372 bool LocalSafeBrowsingDatabaseManager::MatchMalwareIP(
373 const std::string& ip_address) { 373 const std::string& ip_address) {
374 DCHECK_CURRENTLY_ON(BrowserThread::IO); 374 DCHECK_CURRENTLY_ON(BrowserThread::IO);
375 if (!enabled_ || !enable_ip_blacklist_ || !MakeDatabaseAvailable()) { 375 if (!enabled_ || !enable_ip_blacklist_ || !MakeDatabaseAvailable()) {
376 return false; // Fail open. 376 return false; // Fail open.
377 } 377 }
378 return database_->ContainsMalwareIP(ip_address); 378 return database_->ContainsMalwareIP(ip_address);
379 } 379 }
380 380
381 bool SafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { 381 bool LocalSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) {
382 DCHECK_CURRENTLY_ON(BrowserThread::IO); 382 DCHECK_CURRENTLY_ON(BrowserThread::IO);
383 if (!enabled_ || !enable_csd_whitelist_ || !MakeDatabaseAvailable()) { 383 if (!enabled_ || !enable_csd_whitelist_ || !MakeDatabaseAvailable()) {
384 // There is something funky going on here -- for example, perhaps the user 384 // There is something funky going on here -- for example, perhaps the user
385 // has not restarted since enabling metrics reporting, so we haven't 385 // has not restarted since enabling metrics reporting, so we haven't
386 // enabled the csd whitelist yet. Just to be safe we return true in this 386 // enabled the csd whitelist yet. Just to be safe we return true in this
387 // case. 387 // case.
388 return true; 388 return true;
389 } 389 }
390 return database_->ContainsCsdWhitelistedUrl(url); 390 return database_->ContainsCsdWhitelistedUrl(url);
391 } 391 }
392 392
393 bool SafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl(const GURL& url) { 393 bool LocalSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl(
394 const GURL& url) {
394 DCHECK_CURRENTLY_ON(BrowserThread::IO); 395 DCHECK_CURRENTLY_ON(BrowserThread::IO);
395 if (!enabled_ || !enable_download_whitelist_ || !MakeDatabaseAvailable()) { 396 if (!enabled_ || !enable_download_whitelist_ || !MakeDatabaseAvailable()) {
396 return true; 397 return true;
397 } 398 }
398 return database_->ContainsDownloadWhitelistedUrl(url); 399 return database_->ContainsDownloadWhitelistedUrl(url);
399 } 400 }
400 401
401 bool SafeBrowsingDatabaseManager::MatchDownloadWhitelistString( 402 bool LocalSafeBrowsingDatabaseManager::MatchDownloadWhitelistString(
402 const std::string& str) { 403 const std::string& str) {
403 DCHECK_CURRENTLY_ON(BrowserThread::IO); 404 DCHECK_CURRENTLY_ON(BrowserThread::IO);
404 if (!enabled_ || !enable_download_whitelist_ || !MakeDatabaseAvailable()) { 405 if (!enabled_ || !enable_download_whitelist_ || !MakeDatabaseAvailable()) {
405 return true; 406 return true;
406 } 407 }
407 return database_->ContainsDownloadWhitelistedString(str); 408 return database_->ContainsDownloadWhitelistedString(str);
408 } 409 }
409 410
410 bool SafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl(const GURL& url) { 411 bool LocalSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl(
412 const GURL& url) {
411 DCHECK_CURRENTLY_ON(BrowserThread::IO); 413 DCHECK_CURRENTLY_ON(BrowserThread::IO);
412 if (!enabled_ || !MakeDatabaseAvailable()) 414 if (!enabled_ || !MakeDatabaseAvailable())
413 return true; 415 return true;
414 return database_->ContainsInclusionWhitelistedUrl(url); 416 return database_->ContainsInclusionWhitelistedUrl(url);
415 } 417 }
416 418
417 bool SafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() { 419 bool LocalSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() {
418 DCHECK_CURRENTLY_ON(BrowserThread::IO); 420 DCHECK_CURRENTLY_ON(BrowserThread::IO);
419 if (!enabled_ || !MakeDatabaseAvailable()) { 421 if (!enabled_ || !MakeDatabaseAvailable()) {
420 return true; 422 return true;
421 } 423 }
422 return database_->IsMalwareIPMatchKillSwitchOn(); 424 return database_->IsMalwareIPMatchKillSwitchOn();
423 } 425 }
424 426
425 bool SafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() { 427 bool LocalSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() {
426 DCHECK_CURRENTLY_ON(BrowserThread::IO); 428 DCHECK_CURRENTLY_ON(BrowserThread::IO);
427 if (!enabled_ || !MakeDatabaseAvailable()) { 429 if (!enabled_ || !MakeDatabaseAvailable()) {
428 return true; 430 return true;
429 } 431 }
430 return database_->IsCsdWhitelistKillSwitchOn(); 432 return database_->IsCsdWhitelistKillSwitchOn();
431 } 433 }
432 434
433 bool SafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url, 435 bool LocalSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url,
434 Client* client) { 436 Client* client) {
435 DCHECK_CURRENTLY_ON(BrowserThread::IO); 437 DCHECK_CURRENTLY_ON(BrowserThread::IO);
436 if (!enabled_) 438 if (!enabled_)
437 return true; 439 return true;
438 440
439 if (!CanCheckUrl(url)) 441 if (!CanCheckUrl(url))
440 return true; 442 return true;
441 443
442 std::vector<SBThreatType> expected_threats; 444 std::vector<SBThreatType> expected_threats;
443 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); 445 expected_threats.push_back(SB_THREAT_TYPE_URL_MALWARE);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 client, 501 client,
500 safe_browsing_util::MALWARE, 502 safe_browsing_util::MALWARE,
501 expected_threats); 503 expected_threats);
502 check->need_get_hash = cache_hits.empty(); 504 check->need_get_hash = cache_hits.empty();
503 check->prefix_hits.swap(prefix_hits); 505 check->prefix_hits.swap(prefix_hits);
504 check->cache_hits.swap(cache_hits); 506 check->cache_hits.swap(cache_hits);
505 checks_.insert(check); 507 checks_.insert(check);
506 508
507 BrowserThread::PostTask( 509 BrowserThread::PostTask(
508 BrowserThread::IO, FROM_HERE, 510 BrowserThread::IO, FROM_HERE,
509 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); 511 base::Bind(&LocalSafeBrowsingDatabaseManager::OnCheckDone, this, check));
510 512
511 return false; 513 return false;
512 } 514 }
513 515
514 void SafeBrowsingDatabaseManager::CancelCheck(Client* client) { 516 void LocalSafeBrowsingDatabaseManager::CancelCheck(Client* client) {
515 DCHECK_CURRENTLY_ON(BrowserThread::IO); 517 DCHECK_CURRENTLY_ON(BrowserThread::IO);
516 for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) { 518 for (CurrentChecks::iterator i = checks_.begin(); i != checks_.end(); ++i) {
517 // We can't delete matching checks here because the db thread has a copy of 519 // We can't delete matching checks here because the db thread has a copy of
518 // the pointer. Instead, we simply NULL out the client, and when the db 520 // the pointer. Instead, we simply NULL out the client, and when the db
519 // thread calls us back, we'll clean up the check. 521 // thread calls us back, we'll clean up the check.
520 if ((*i)->client == client) 522 if ((*i)->client == client)
521 (*i)->client = NULL; 523 (*i)->client = NULL;
522 } 524 }
523 525
524 // Scan the queued clients store. Clients may be here if they requested a URL 526 // Scan the queued clients store. Clients may be here if they requested a URL
525 // check before the database has finished loading. 527 // check before the database has finished loading.
526 for (std::deque<QueuedCheck>::iterator it(queued_checks_.begin()); 528 for (std::deque<QueuedCheck>::iterator it(queued_checks_.begin());
527 it != queued_checks_.end(); ) { 529 it != queued_checks_.end(); ) {
528 // In this case it's safe to delete matches entirely since nothing has a 530 // In this case it's safe to delete matches entirely since nothing has a
529 // pointer to them. 531 // pointer to them.
530 if (it->client == client) 532 if (it->client == client)
531 it = queued_checks_.erase(it); 533 it = queued_checks_.erase(it);
532 else 534 else
533 ++it; 535 ++it;
534 } 536 }
535 } 537 }
536 538
537 void SafeBrowsingDatabaseManager::HandleGetHashResults( 539 void LocalSafeBrowsingDatabaseManager::HandleGetHashResults(
538 SafeBrowsingCheck* check, 540 SafeBrowsingCheck* check,
539 const std::vector<SBFullHashResult>& full_hashes, 541 const std::vector<SBFullHashResult>& full_hashes,
540 const base::TimeDelta& cache_lifetime) { 542 const base::TimeDelta& cache_lifetime) {
541 DCHECK_CURRENTLY_ON(BrowserThread::IO); 543 DCHECK_CURRENTLY_ON(BrowserThread::IO);
542 544
543 if (!enabled_) 545 if (!enabled_)
544 return; 546 return;
545 547
546 // If the service has been shut down, |check| should have been deleted. 548 // If the service has been shut down, |check| should have been deleted.
547 DCHECK(checks_.find(check) != checks_.end()); 549 DCHECK(checks_.find(check) != checks_.end());
548 550
549 // |start| is set before calling |GetFullHash()|, which should be 551 // |start| is set before calling |GetFullHash()|, which should be
550 // the only path which gets to here. 552 // the only path which gets to here.
551 DCHECK(!check->start.is_null()); 553 DCHECK(!check->start.is_null());
552 UMA_HISTOGRAM_LONG_TIMES("SB2.Network", 554 UMA_HISTOGRAM_LONG_TIMES("SB2.Network",
553 base::TimeTicks::Now() - check->start); 555 base::TimeTicks::Now() - check->start);
554 556
555 std::vector<SBPrefix> prefixes = check->prefix_hits; 557 std::vector<SBPrefix> prefixes = check->prefix_hits;
556 OnHandleGetHashResults(check, full_hashes); // 'check' is deleted here. 558 OnHandleGetHashResults(check, full_hashes); // 'check' is deleted here.
557 559
558 // Cache the GetHash results. 560 // Cache the GetHash results.
559 if (cache_lifetime != base::TimeDelta() && MakeDatabaseAvailable()) 561 if (cache_lifetime != base::TimeDelta() && MakeDatabaseAvailable())
560 database_->CacheHashResults(prefixes, full_hashes, cache_lifetime); 562 database_->CacheHashResults(prefixes, full_hashes, cache_lifetime);
561 } 563 }
562 564
563 void SafeBrowsingDatabaseManager::GetChunks(GetChunksCallback callback) { 565 void LocalSafeBrowsingDatabaseManager::GetChunks(GetChunksCallback callback) {
564 DCHECK_CURRENTLY_ON(BrowserThread::IO); 566 DCHECK_CURRENTLY_ON(BrowserThread::IO);
565 DCHECK(enabled_); 567 DCHECK(enabled_);
566 DCHECK(!callback.is_null()); 568 DCHECK(!callback.is_null());
567 safe_browsing_task_runner_->PostTask( 569 safe_browsing_task_runner_->PostTask(
568 FROM_HERE, 570 FROM_HERE,
569 base::Bind(&SafeBrowsingDatabaseManager::GetAllChunksFromDatabase, this, 571 base::Bind(&LocalSafeBrowsingDatabaseManager::GetAllChunksFromDatabase,
570 callback)); 572 this, callback));
571 } 573 }
572 574
573 void SafeBrowsingDatabaseManager::AddChunks( 575 void LocalSafeBrowsingDatabaseManager::AddChunks(
574 const std::string& list, 576 const std::string& list,
575 scoped_ptr<ScopedVector<SBChunkData> > chunks, 577 scoped_ptr<ScopedVector<SBChunkData> > chunks,
576 AddChunksCallback callback) { 578 AddChunksCallback callback) {
577 DCHECK_CURRENTLY_ON(BrowserThread::IO); 579 DCHECK_CURRENTLY_ON(BrowserThread::IO);
578 DCHECK(enabled_); 580 DCHECK(enabled_);
579 DCHECK(!callback.is_null()); 581 DCHECK(!callback.is_null());
580 safe_browsing_task_runner_->PostTask( 582 safe_browsing_task_runner_->PostTask(
581 FROM_HERE, base::Bind(&SafeBrowsingDatabaseManager::AddDatabaseChunks, 583 FROM_HERE,
582 this, list, base::Passed(&chunks), callback)); 584 base::Bind(&LocalSafeBrowsingDatabaseManager::AddDatabaseChunks, this,
585 list, base::Passed(&chunks), callback));
583 } 586 }
584 587
585 void SafeBrowsingDatabaseManager::DeleteChunks( 588 void LocalSafeBrowsingDatabaseManager::DeleteChunks(
586 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { 589 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) {
587 DCHECK_CURRENTLY_ON(BrowserThread::IO); 590 DCHECK_CURRENTLY_ON(BrowserThread::IO);
588 DCHECK(enabled_); 591 DCHECK(enabled_);
589 safe_browsing_task_runner_->PostTask( 592 safe_browsing_task_runner_->PostTask(
590 FROM_HERE, base::Bind(&SafeBrowsingDatabaseManager::DeleteDatabaseChunks, 593 FROM_HERE,
591 this, base::Passed(&chunk_deletes))); 594 base::Bind(&LocalSafeBrowsingDatabaseManager::DeleteDatabaseChunks, this,
595 base::Passed(&chunk_deletes)));
592 } 596 }
593 597
594 void SafeBrowsingDatabaseManager::UpdateStarted() { 598 void LocalSafeBrowsingDatabaseManager::UpdateStarted() {
595 DCHECK_CURRENTLY_ON(BrowserThread::IO); 599 DCHECK_CURRENTLY_ON(BrowserThread::IO);
596 DCHECK(enabled_); 600 DCHECK(enabled_);
597 DCHECK(!update_in_progress_); 601 DCHECK(!update_in_progress_);
598 update_in_progress_ = true; 602 update_in_progress_ = true;
599 } 603 }
600 604
601 void SafeBrowsingDatabaseManager::UpdateFinished(bool update_succeeded) { 605 void LocalSafeBrowsingDatabaseManager::UpdateFinished(bool update_succeeded) {
602 DCHECK_CURRENTLY_ON(BrowserThread::IO); 606 DCHECK_CURRENTLY_ON(BrowserThread::IO);
603 DCHECK(enabled_); 607 DCHECK(enabled_);
604 if (update_in_progress_) { 608 if (update_in_progress_) {
605 update_in_progress_ = false; 609 update_in_progress_ = false;
606 safe_browsing_task_runner_->PostTask( 610 safe_browsing_task_runner_->PostTask(
607 FROM_HERE, 611 FROM_HERE,
608 base::Bind(&SafeBrowsingDatabaseManager::DatabaseUpdateFinished, this, 612 base::Bind(&LocalSafeBrowsingDatabaseManager::DatabaseUpdateFinished,
609 update_succeeded)); 613 this, update_succeeded));
610 } 614 }
611 } 615 }
612 616
613 void SafeBrowsingDatabaseManager::ResetDatabase() { 617 void LocalSafeBrowsingDatabaseManager::ResetDatabase() {
614 DCHECK_CURRENTLY_ON(BrowserThread::IO); 618 DCHECK_CURRENTLY_ON(BrowserThread::IO);
615 DCHECK(enabled_); 619 DCHECK(enabled_);
616 safe_browsing_task_runner_->PostTask( 620 safe_browsing_task_runner_->PostTask(
617 FROM_HERE, 621 FROM_HERE,
618 base::Bind(&SafeBrowsingDatabaseManager::OnResetDatabase, this)); 622 base::Bind(&LocalSafeBrowsingDatabaseManager::OnResetDatabase, this));
619 } 623 }
620 624
621 void SafeBrowsingDatabaseManager::StartOnIOThread() { 625 void LocalSafeBrowsingDatabaseManager::StartOnIOThread() {
622 DCHECK_CURRENTLY_ON(BrowserThread::IO); 626 DCHECK_CURRENTLY_ON(BrowserThread::IO);
623 if (enabled_) 627 if (enabled_)
624 return; 628 return;
625 629
626 // Only get a new task runner if there isn't one already. If the service has 630 // Only get a new task runner if there isn't one already. If the service has
627 // previously been started and stopped, a task runner could already exist. 631 // previously been started and stopped, a task runner could already exist.
628 if (!safe_browsing_task_runner_) { 632 if (!safe_browsing_task_runner_) {
629 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); 633 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
630 safe_browsing_task_runner_ = 634 safe_browsing_task_runner_ =
631 pool->GetSequencedTaskRunnerWithShutdownBehavior( 635 pool->GetSequencedTaskRunnerWithShutdownBehavior(
632 pool->GetSequenceToken(), 636 pool->GetSequenceToken(),
633 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 637 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
634 } 638 }
635 639
636 enabled_ = true; 640 enabled_ = true;
637 641
638 MakeDatabaseAvailable(); 642 MakeDatabaseAvailable();
639 } 643 }
640 644
641 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { 645 void LocalSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) {
642 DCHECK_CURRENTLY_ON(BrowserThread::IO); 646 DCHECK_CURRENTLY_ON(BrowserThread::IO);
643 647
644 DoStopOnIOThread(); 648 DoStopOnIOThread();
645 if (shutdown) { 649 if (shutdown) {
646 sb_service_ = NULL; 650 sb_service_ = NULL;
647 } 651 }
648 } 652 }
649 653
650 void SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished( 654 void LocalSafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished(
651 bool update_succeeded) { 655 bool update_succeeded) {
652 DCHECK_CURRENTLY_ON(BrowserThread::UI); 656 DCHECK_CURRENTLY_ON(BrowserThread::UI);
653 content::NotificationService::current()->Notify( 657 content::NotificationService::current()->Notify(
654 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 658 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
655 content::Source<SafeBrowsingDatabaseManager>(this), 659 content::Source<SafeBrowsingDatabaseManager>(this),
656 content::Details<bool>(&update_succeeded)); 660 content::Details<bool>(&update_succeeded));
657 } 661 }
658 662
659 SafeBrowsingDatabaseManager::QueuedCheck::QueuedCheck( 663 LocalSafeBrowsingDatabaseManager::QueuedCheck::QueuedCheck(
660 const safe_browsing_util::ListType check_type, 664 const safe_browsing_util::ListType check_type,
661 Client* client, 665 Client* client,
662 const GURL& url, 666 const GURL& url,
663 const std::vector<SBThreatType>& expected_threats, 667 const std::vector<SBThreatType>& expected_threats,
664 const base::TimeTicks& start) 668 const base::TimeTicks& start)
665 : check_type(check_type), 669 : check_type(check_type),
666 client(client), 670 client(client),
667 url(url), 671 url(url),
668 expected_threats(expected_threats), 672 expected_threats(expected_threats),
669 start(start) { 673 start(start) {
670 } 674 }
671 675
672 SafeBrowsingDatabaseManager::QueuedCheck::~QueuedCheck() { 676 LocalSafeBrowsingDatabaseManager::QueuedCheck::~QueuedCheck() {
673 } 677 }
674 678
675 void SafeBrowsingDatabaseManager::DoStopOnIOThread() { 679 void LocalSafeBrowsingDatabaseManager::DoStopOnIOThread() {
676 DCHECK_CURRENTLY_ON(BrowserThread::IO); 680 DCHECK_CURRENTLY_ON(BrowserThread::IO);
677 681
678 if (!enabled_) 682 if (!enabled_)
679 return; 683 return;
680 684
681 enabled_ = false; 685 enabled_ = false;
682 686
683 // Delete queued checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'. 687 // Delete queued checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'.
684 while (!queued_checks_.empty()) { 688 while (!queued_checks_.empty()) {
685 QueuedCheck queued = queued_checks_.front(); 689 QueuedCheck queued = queued_checks_.front();
686 if (queued.client) { 690 if (queued.client) {
687 SafeBrowsingCheck sb_check(std::vector<GURL>(1, queued.url), 691 SafeBrowsingCheck sb_check(std::vector<GURL>(1, queued.url),
688 std::vector<SBFullHash>(), 692 std::vector<SBFullHash>(),
689 queued.client, 693 queued.client,
690 queued.check_type, 694 queued.check_type,
691 queued.expected_threats); 695 queued.expected_threats);
692 queued.client->OnSafeBrowsingResult(sb_check); 696 sb_check.OnSafeBrowsingResult();
693 } 697 }
694 queued_checks_.pop_front(); 698 queued_checks_.pop_front();
695 } 699 }
696 700
697 // Close the database. Cases to avoid: 701 // Close the database. Cases to avoid:
698 // * If |closing_database_| is true, continuing will queue up a second 702 // * If |closing_database_| is true, continuing will queue up a second
699 // request, |closing_database_| will be reset after handling the first 703 // request, |closing_database_| will be reset after handling the first
700 // request, and if any functions on the db thread recreate the database, we 704 // request, and if any functions on the db thread recreate the database, we
701 // could start using it on the IO thread and then have the second request 705 // could start using it on the IO thread and then have the second request
702 // handler delete it out from under us. 706 // handler delete it out from under us.
703 // * If |database_| is NULL, then either no creation request is in flight, in 707 // * If |database_| is NULL, then either no creation request is in flight, in
704 // which case we don't need to do anything, or one is in flight, in which 708 // which case we don't need to do anything, or one is in flight, in which
705 // case the database will be recreated before our deletion request is 709 // case the database will be recreated before our deletion request is
706 // handled, and could be used on the IO thread in that time period, leading 710 // handled, and could be used on the IO thread in that time period, leading
707 // to the same problem as above. 711 // to the same problem as above.
708 // Checking DatabaseAvailable() avoids both of these. 712 // Checking DatabaseAvailable() avoids both of these.
709 if (DatabaseAvailable()) { 713 if (DatabaseAvailable()) {
710 closing_database_ = true; 714 closing_database_ = true;
711 safe_browsing_task_runner_->PostTask( 715 safe_browsing_task_runner_->PostTask(
712 FROM_HERE, 716 FROM_HERE,
713 base::Bind(&SafeBrowsingDatabaseManager::OnCloseDatabase, this)); 717 base::Bind(&LocalSafeBrowsingDatabaseManager::OnCloseDatabase, this));
714 } 718 }
715 719
716 // Delete pending checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'. 720 // Delete pending checks, calling back any clients with 'SB_THREAT_TYPE_SAFE'.
717 // We have to do this after the db thread returns because methods on it can 721 // We have to do this after the db thread returns because methods on it can
718 // have copies of these pointers, so deleting them might lead to accessing 722 // have copies of these pointers, so deleting them might lead to accessing
719 // garbage. 723 // garbage.
720 for (CurrentChecks::iterator it = checks_.begin(); 724 for (CurrentChecks::iterator it = checks_.begin();
721 it != checks_.end(); ++it) { 725 it != checks_.end(); ++it) {
722 SafeBrowsingCheck* check = *it; 726 SafeBrowsingCheck* check = *it;
723 if (check->client) 727 if (check->client)
724 check->client->OnSafeBrowsingResult(*check); 728 check->OnSafeBrowsingResult();
725 } 729 }
726 STLDeleteElements(&checks_); 730 STLDeleteElements(&checks_);
727 731
728 gethash_requests_.clear(); 732 gethash_requests_.clear();
729 } 733 }
730 734
731 bool SafeBrowsingDatabaseManager::DatabaseAvailable() const { 735 bool LocalSafeBrowsingDatabaseManager::DatabaseAvailable() const {
732 base::AutoLock lock(database_lock_); 736 base::AutoLock lock(database_lock_);
733 return !closing_database_ && (database_ != NULL); 737 return !closing_database_ && (database_ != NULL);
734 } 738 }
735 739
736 bool SafeBrowsingDatabaseManager::MakeDatabaseAvailable() { 740 bool LocalSafeBrowsingDatabaseManager::MakeDatabaseAvailable() {
737 DCHECK_CURRENTLY_ON(BrowserThread::IO); 741 DCHECK_CURRENTLY_ON(BrowserThread::IO);
738 DCHECK(enabled_); 742 DCHECK(enabled_);
739 if (DatabaseAvailable()) 743 if (DatabaseAvailable())
740 return true; 744 return true;
741 safe_browsing_task_runner_->PostTask( 745 safe_browsing_task_runner_->PostTask(
742 FROM_HERE, 746 FROM_HERE, base::Bind(base::IgnoreResult(
743 base::Bind(base::IgnoreResult(&SafeBrowsingDatabaseManager::GetDatabase), 747 &LocalSafeBrowsingDatabaseManager::GetDatabase),
744 this)); 748 this));
745 return false; 749 return false;
746 } 750 }
747 751
748 SafeBrowsingDatabase* SafeBrowsingDatabaseManager::GetDatabase() { 752 SafeBrowsingDatabase* LocalSafeBrowsingDatabaseManager::GetDatabase() {
749 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 753 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
750 754
751 if (database_) 755 if (database_)
752 return database_; 756 return database_;
753 startup_metric_utils::ScopedSlowStartupUMA 757 startup_metric_utils::ScopedSlowStartupUMA
754 scoped_timer("Startup.SlowStartupSafeBrowsingGetDatabase"); 758 scoped_timer("Startup.SlowStartupSafeBrowsingGetDatabase");
755 const base::TimeTicks before = base::TimeTicks::Now(); 759 const base::TimeTicks before = base::TimeTicks::Now();
756 760
757 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create( 761 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create(
758 safe_browsing_task_runner_, enable_download_protection_, 762 safe_browsing_task_runner_, enable_download_protection_,
759 enable_csd_whitelist_, enable_download_whitelist_, 763 enable_csd_whitelist_, enable_download_whitelist_,
760 enable_extension_blacklist_, enable_ip_blacklist_, 764 enable_extension_blacklist_, enable_ip_blacklist_,
761 enable_unwanted_software_blacklist_); 765 enable_unwanted_software_blacklist_);
762 766
763 database->Init(SafeBrowsingService::GetBaseFilename()); 767 database->Init(SafeBrowsingService::GetBaseFilename());
764 { 768 {
765 // Acquiring the lock here guarantees correct ordering between the writes to 769 // Acquiring the lock here guarantees correct ordering between the writes to
766 // the new database object above, and the setting of |database_| below. 770 // the new database object above, and the setting of |database_| below.
767 base::AutoLock lock(database_lock_); 771 base::AutoLock lock(database_lock_);
768 database_ = database; 772 database_ = database;
769 } 773 }
770 774
771 BrowserThread::PostTask( 775 BrowserThread::PostTask(
772 BrowserThread::IO, FROM_HERE, 776 BrowserThread::IO, FROM_HERE,
773 base::Bind(&SafeBrowsingDatabaseManager::DatabaseLoadComplete, this)); 777 base::Bind(&LocalSafeBrowsingDatabaseManager::DatabaseLoadComplete,
778 this));
774 779
775 UMA_HISTOGRAM_TIMES("SB2.DatabaseOpen", base::TimeTicks::Now() - before); 780 UMA_HISTOGRAM_TIMES("SB2.DatabaseOpen", base::TimeTicks::Now() - before);
776 return database_; 781 return database_;
777 } 782 }
778 783
779 void SafeBrowsingDatabaseManager::OnCheckDone(SafeBrowsingCheck* check) { 784 void LocalSafeBrowsingDatabaseManager::OnCheckDone(SafeBrowsingCheck* check) {
780 DCHECK_CURRENTLY_ON(BrowserThread::IO); 785 DCHECK_CURRENTLY_ON(BrowserThread::IO);
781 786
782 if (!enabled_) 787 if (!enabled_)
783 return; 788 return;
784 789
785 // If the service has been shut down, |check| should have been deleted. 790 // If the service has been shut down, |check| should have been deleted.
786 DCHECK(checks_.find(check) != checks_.end()); 791 DCHECK(checks_.find(check) != checks_.end());
787 792
788 if (check->client && check->need_get_hash) { 793 if (check->client && check->need_get_hash) {
789 // We have a partial match so we need to query Google for the full hash. 794 // We have a partial match so we need to query Google for the full hash.
(...skipping 20 matching lines...) Expand all
810 } 815 }
811 816
812 // Reset the start time so that we can measure the network time without the 817 // Reset the start time so that we can measure the network time without the
813 // database time. 818 // database time.
814 check->start = base::TimeTicks::Now(); 819 check->start = base::TimeTicks::Now();
815 // Note: If |this| is deleted or stopped, the protocol_manager will 820 // Note: If |this| is deleted or stopped, the protocol_manager will
816 // be destroyed as well - hence it's OK to do unretained in this case. 821 // be destroyed as well - hence it's OK to do unretained in this case.
817 bool is_download = check->check_type == safe_browsing_util::BINURL; 822 bool is_download = check->check_type == safe_browsing_util::BINURL;
818 sb_service_->protocol_manager()->GetFullHash( 823 sb_service_->protocol_manager()->GetFullHash(
819 check->prefix_hits, 824 check->prefix_hits,
820 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashResults, 825 base::Bind(&LocalSafeBrowsingDatabaseManager::HandleGetHashResults,
821 base::Unretained(this), 826 base::Unretained(this),
822 check), 827 check),
823 is_download); 828 is_download);
824 } else { 829 } else {
825 // We may have cached results for previous GetHash queries. Since 830 // We may have cached results for previous GetHash queries. Since
826 // this data comes from cache, don't histogram hits. 831 // this data comes from cache, don't histogram hits.
827 HandleOneCheck(check, check->cache_hits); 832 HandleOneCheck(check, check->cache_hits);
828 } 833 }
829 } 834 }
830 835
831 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( 836 void LocalSafeBrowsingDatabaseManager::GetAllChunksFromDatabase(
832 GetChunksCallback callback) { 837 GetChunksCallback callback) {
833 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 838 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
834 839
835 bool database_error = true; 840 bool database_error = true;
836 std::vector<SBListChunkRanges> lists; 841 std::vector<SBListChunkRanges> lists;
837 DCHECK(!database_update_in_progress_); 842 DCHECK(!database_update_in_progress_);
838 database_update_in_progress_ = true; 843 database_update_in_progress_ = true;
839 GetDatabase(); // This guarantees that |database_| is non-NULL. 844 GetDatabase(); // This guarantees that |database_| is non-NULL.
840 if (database_->UpdateStarted(&lists)) { 845 if (database_->UpdateStarted(&lists)) {
841 database_error = false; 846 database_error = false;
842 } else { 847 } else {
843 database_->UpdateFinished(false); 848 database_->UpdateFinished(false);
844 } 849 }
845 850
846 BrowserThread::PostTask( 851 BrowserThread::PostTask(
847 BrowserThread::IO, FROM_HERE, 852 BrowserThread::IO, FROM_HERE,
848 base::Bind(&SafeBrowsingDatabaseManager::OnGetAllChunksFromDatabase, 853 base::Bind(&LocalSafeBrowsingDatabaseManager::OnGetAllChunksFromDatabase,
849 this, lists, database_error, callback)); 854 this, lists, database_error, callback));
850 } 855 }
851 856
852 void SafeBrowsingDatabaseManager::OnGetAllChunksFromDatabase( 857 void LocalSafeBrowsingDatabaseManager::OnGetAllChunksFromDatabase(
853 const std::vector<SBListChunkRanges>& lists, bool database_error, 858 const std::vector<SBListChunkRanges>& lists, bool database_error,
854 GetChunksCallback callback) { 859 GetChunksCallback callback) {
855 DCHECK_CURRENTLY_ON(BrowserThread::IO); 860 DCHECK_CURRENTLY_ON(BrowserThread::IO);
856 if (enabled_) 861 if (enabled_)
857 callback.Run(lists, database_error); 862 callback.Run(lists, database_error);
858 } 863 }
859 864
860 void SafeBrowsingDatabaseManager::OnAddChunksComplete( 865 void LocalSafeBrowsingDatabaseManager::OnAddChunksComplete(
861 AddChunksCallback callback) { 866 AddChunksCallback callback) {
862 DCHECK_CURRENTLY_ON(BrowserThread::IO); 867 DCHECK_CURRENTLY_ON(BrowserThread::IO);
863 if (enabled_) 868 if (enabled_)
864 callback.Run(); 869 callback.Run();
865 } 870 }
866 871
867 void SafeBrowsingDatabaseManager::DatabaseLoadComplete() { 872 void LocalSafeBrowsingDatabaseManager::DatabaseLoadComplete() {
868 DCHECK_CURRENTLY_ON(BrowserThread::IO); 873 DCHECK_CURRENTLY_ON(BrowserThread::IO);
869 if (!enabled_) 874 if (!enabled_)
870 return; 875 return;
871 876
872 LOCAL_HISTOGRAM_COUNTS("SB.QueueDepth", queued_checks_.size()); 877 LOCAL_HISTOGRAM_COUNTS("SB.QueueDepth", queued_checks_.size());
873 if (queued_checks_.empty()) 878 if (queued_checks_.empty())
874 return; 879 return;
875 880
876 // If the database isn't already available, calling CheckUrl() in the loop 881 // If the database isn't already available, calling CheckUrl() in the loop
877 // below will add the check back to the queue, and we'll infinite-loop. 882 // below will add the check back to the queue, and we'll infinite-loop.
878 DCHECK(DatabaseAvailable()); 883 DCHECK(DatabaseAvailable());
879 while (!queued_checks_.empty()) { 884 while (!queued_checks_.empty()) {
880 QueuedCheck check = queued_checks_.front(); 885 QueuedCheck check = queued_checks_.front();
881 DCHECK(!check.start.is_null()); 886 DCHECK(!check.start.is_null());
882 LOCAL_HISTOGRAM_TIMES("SB.QueueDelay", 887 LOCAL_HISTOGRAM_TIMES("SB.QueueDelay",
883 base::TimeTicks::Now() - check.start); 888 base::TimeTicks::Now() - check.start);
884 // If CheckUrl() determines the URL is safe immediately, it doesn't call the 889 // If CheckUrl() determines the URL is safe immediately, it doesn't call the
885 // client's handler function (because normally it's being directly called by 890 // client's handler function (because normally it's being directly called by
886 // the client). Since we're not the client, we have to convey this result. 891 // the client). Since we're not the client, we have to convey this result.
887 if (check.client && CheckBrowseUrl(check.url, check.client)) { 892 if (check.client && CheckBrowseUrl(check.url, check.client)) {
888 SafeBrowsingCheck sb_check(std::vector<GURL>(1, check.url), 893 SafeBrowsingCheck sb_check(std::vector<GURL>(1, check.url),
889 std::vector<SBFullHash>(), 894 std::vector<SBFullHash>(),
890 check.client, 895 check.client,
891 check.check_type, 896 check.check_type,
892 check.expected_threats); 897 check.expected_threats);
893 check.client->OnSafeBrowsingResult(sb_check); 898 sb_check.OnSafeBrowsingResult();
894 } 899 }
895 queued_checks_.pop_front(); 900 queued_checks_.pop_front();
896 } 901 }
897 } 902 }
898 903
899 void SafeBrowsingDatabaseManager::AddDatabaseChunks( 904 void LocalSafeBrowsingDatabaseManager::AddDatabaseChunks(
900 const std::string& list_name, 905 const std::string& list_name,
901 scoped_ptr<ScopedVector<SBChunkData> > chunks, 906 scoped_ptr<ScopedVector<SBChunkData> > chunks,
902 AddChunksCallback callback) { 907 AddChunksCallback callback) {
903 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 908 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
904 if (chunks) 909 if (chunks)
905 GetDatabase()->InsertChunks(list_name, chunks->get()); 910 GetDatabase()->InsertChunks(list_name, chunks->get());
906 BrowserThread::PostTask( 911 BrowserThread::PostTask(
907 BrowserThread::IO, FROM_HERE, 912 BrowserThread::IO, FROM_HERE,
908 base::Bind(&SafeBrowsingDatabaseManager::OnAddChunksComplete, this, 913 base::Bind(&LocalSafeBrowsingDatabaseManager::OnAddChunksComplete, this,
909 callback)); 914 callback));
910 } 915 }
911 916
912 void SafeBrowsingDatabaseManager::DeleteDatabaseChunks( 917 void LocalSafeBrowsingDatabaseManager::DeleteDatabaseChunks(
913 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) { 918 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) {
914 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 919 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
915 if (chunk_deletes) 920 if (chunk_deletes)
916 GetDatabase()->DeleteChunks(*chunk_deletes); 921 GetDatabase()->DeleteChunks(*chunk_deletes);
917 } 922 }
918 923
919 void SafeBrowsingDatabaseManager::DatabaseUpdateFinished( 924 void LocalSafeBrowsingDatabaseManager::DatabaseUpdateFinished(
920 bool update_succeeded) { 925 bool update_succeeded) {
921 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 926 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
922 GetDatabase()->UpdateFinished(update_succeeded); 927 GetDatabase()->UpdateFinished(update_succeeded);
923 DCHECK(database_update_in_progress_); 928 DCHECK(database_update_in_progress_);
924 database_update_in_progress_ = false; 929 database_update_in_progress_ = false;
925 BrowserThread::PostTask( 930 BrowserThread::PostTask(
926 BrowserThread::UI, FROM_HERE, 931 BrowserThread::UI, FROM_HERE,
927 base::Bind(&SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished, 932 base::Bind(
928 this, update_succeeded)); 933 &LocalSafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished, this,
934 update_succeeded));
929 } 935 }
930 936
931 void SafeBrowsingDatabaseManager::OnCloseDatabase() { 937 void LocalSafeBrowsingDatabaseManager::OnCloseDatabase() {
932 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 938 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
933 DCHECK(closing_database_); 939 DCHECK(closing_database_);
934 940
935 // Because |closing_database_| is true, nothing on the IO thread will be 941 // Because |closing_database_| is true, nothing on the IO thread will be
936 // accessing the database, so it's safe to delete and then NULL the pointer. 942 // accessing the database, so it's safe to delete and then NULL the pointer.
937 delete database_; 943 delete database_;
938 database_ = NULL; 944 database_ = NULL;
939 945
940 // Acquiring the lock here guarantees correct ordering between the resetting 946 // Acquiring the lock here guarantees correct ordering between the resetting
941 // of |database_| above and of |closing_database_| below, which ensures there 947 // of |database_| above and of |closing_database_| below, which ensures there
942 // won't be a window during which the IO thread falsely believes the database 948 // won't be a window during which the IO thread falsely believes the database
943 // is available. 949 // is available.
944 base::AutoLock lock(database_lock_); 950 base::AutoLock lock(database_lock_);
945 closing_database_ = false; 951 closing_database_ = false;
946 } 952 }
947 953
948 void SafeBrowsingDatabaseManager::OnResetDatabase() { 954 void LocalSafeBrowsingDatabaseManager::OnResetDatabase() {
949 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 955 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
950 956
951 GetDatabase()->ResetDatabase(); 957 GetDatabase()->ResetDatabase();
952 } 958 }
953 959
954 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( 960 void LocalSafeBrowsingDatabaseManager::OnHandleGetHashResults(
955 SafeBrowsingCheck* check, 961 SafeBrowsingCheck* check,
956 const std::vector<SBFullHashResult>& full_hashes) { 962 const std::vector<SBFullHashResult>& full_hashes) {
957 DCHECK_CURRENTLY_ON(BrowserThread::IO); 963 DCHECK_CURRENTLY_ON(BrowserThread::IO);
958 safe_browsing_util::ListType check_type = check->check_type; 964 safe_browsing_util::ListType check_type = check->check_type;
959 SBPrefix prefix = check->prefix_hits[0]; 965 SBPrefix prefix = check->prefix_hits[0];
960 GetHashRequests::iterator it = gethash_requests_.find(prefix); 966 GetHashRequests::iterator it = gethash_requests_.find(prefix);
961 if (check->prefix_hits.size() > 1 || it == gethash_requests_.end()) { 967 if (check->prefix_hits.size() > 1 || it == gethash_requests_.end()) {
962 const bool hit = HandleOneCheck(check, full_hashes); 968 const bool hit = HandleOneCheck(check, full_hashes);
963 RecordGetHashCheckStatus(hit, check_type, full_hashes); 969 RecordGetHashCheckStatus(hit, check_type, full_hashes);
964 return; 970 return;
965 } 971 }
966 972
967 // Call back all interested parties, noting if any has a hit. 973 // Call back all interested parties, noting if any has a hit.
968 GetHashRequestors& requestors = it->second; 974 GetHashRequestors& requestors = it->second;
969 bool hit = false; 975 bool hit = false;
970 for (GetHashRequestors::iterator r = requestors.begin(); 976 for (GetHashRequestors::iterator r = requestors.begin();
971 r != requestors.end(); ++r) { 977 r != requestors.end(); ++r) {
972 if (HandleOneCheck(*r, full_hashes)) 978 if (HandleOneCheck(*r, full_hashes))
973 hit = true; 979 hit = true;
974 } 980 }
975 RecordGetHashCheckStatus(hit, check_type, full_hashes); 981 RecordGetHashCheckStatus(hit, check_type, full_hashes);
976 982
977 gethash_requests_.erase(it); 983 gethash_requests_.erase(it);
978 } 984 }
979 985
980 bool SafeBrowsingDatabaseManager::HandleOneCheck( 986 bool LocalSafeBrowsingDatabaseManager::HandleOneCheck(
981 SafeBrowsingCheck* check, 987 SafeBrowsingCheck* check,
982 const std::vector<SBFullHashResult>& full_hashes) { 988 const std::vector<SBFullHashResult>& full_hashes) {
983 DCHECK_CURRENTLY_ON(BrowserThread::IO); 989 DCHECK_CURRENTLY_ON(BrowserThread::IO);
984 DCHECK(check); 990 DCHECK(check);
985 991
986 bool is_threat = false; 992 bool is_threat = false;
987 993
988 // TODO(shess): GetHashSeverestThreadListType() contains a loop, 994 // TODO(shess): GetHashSeverestThreadListType() contains a loop,
989 // GetUrlSeverestThreatListType() a loop around that loop. Having another 995 // GetUrlSeverestThreatListType() a loop around that loop. Having another
990 // loop out here concerns me. It is likely that SAFE is an expected outcome, 996 // loop out here concerns me. It is likely that SAFE is an expected outcome,
(...skipping 29 matching lines...) Expand all
1020 IsExpectedThreat(threat, check->expected_threats)) { 1026 IsExpectedThreat(threat, check->expected_threats)) {
1021 check->full_hash_results[i] = threat; 1027 check->full_hash_results[i] = threat;
1022 is_threat = true; 1028 is_threat = true;
1023 } 1029 }
1024 } 1030 }
1025 1031
1026 SafeBrowsingCheckDone(check); 1032 SafeBrowsingCheckDone(check);
1027 return is_threat; 1033 return is_threat;
1028 } 1034 }
1029 1035
1030 void SafeBrowsingDatabaseManager::OnAsyncCheckDone( 1036 void LocalSafeBrowsingDatabaseManager::OnAsyncCheckDone(
1031 SafeBrowsingCheck* check, 1037 SafeBrowsingCheck* check,
1032 const std::vector<SBPrefix>& prefix_hits) { 1038 const std::vector<SBPrefix>& prefix_hits) {
1033 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1039 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1034 DCHECK(enable_download_protection_); 1040 DCHECK(enable_download_protection_);
1035 1041
1036 check->prefix_hits = prefix_hits; 1042 check->prefix_hits = prefix_hits;
1037 if (check->prefix_hits.empty()) { 1043 if (check->prefix_hits.empty()) {
1038 SafeBrowsingCheckDone(check); 1044 SafeBrowsingCheckDone(check);
1039 } else { 1045 } else {
1040 check->need_get_hash = true; 1046 check->need_get_hash = true;
1041 OnCheckDone(check); 1047 OnCheckDone(check);
1042 } 1048 }
1043 } 1049 }
1044 1050
1045 std::vector<SBPrefix> SafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread( 1051 std::vector<SBPrefix>
1052 LocalSafeBrowsingDatabaseManager::CheckDownloadUrlOnSBThread(
1046 const std::vector<SBPrefix>& prefixes) { 1053 const std::vector<SBPrefix>& prefixes) {
1047 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 1054 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
1048 DCHECK(enable_download_protection_); 1055 DCHECK(enable_download_protection_);
1049 1056
1050 std::vector<SBPrefix> prefix_hits; 1057 std::vector<SBPrefix> prefix_hits;
1051 const bool result = 1058 const bool result =
1052 database_->ContainsDownloadUrlPrefixes(prefixes, &prefix_hits); 1059 database_->ContainsDownloadUrlPrefixes(prefixes, &prefix_hits);
1053 DCHECK_EQ(result, !prefix_hits.empty()); 1060 DCHECK_EQ(result, !prefix_hits.empty());
1054 return prefix_hits; 1061 return prefix_hits;
1055 } 1062 }
1056 1063
1057 std::vector<SBPrefix> SafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread( 1064 std::vector<SBPrefix>
1065 LocalSafeBrowsingDatabaseManager::CheckExtensionIDsOnSBThread(
1058 const std::vector<SBPrefix>& prefixes) { 1066 const std::vector<SBPrefix>& prefixes) {
1059 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread()); 1067 DCHECK(safe_browsing_task_runner_->RunsTasksOnCurrentThread());
1060 1068
1061 std::vector<SBPrefix> prefix_hits; 1069 std::vector<SBPrefix> prefix_hits;
1062 const bool result = 1070 const bool result =
1063 database_->ContainsExtensionPrefixes(prefixes, &prefix_hits); 1071 database_->ContainsExtensionPrefixes(prefixes, &prefix_hits);
1064 DCHECK_EQ(result, !prefix_hits.empty()); 1072 DCHECK_EQ(result, !prefix_hits.empty());
1065 return prefix_hits; 1073 return prefix_hits;
1066 } 1074 }
1067 1075
1068 void SafeBrowsingDatabaseManager::TimeoutCallback(SafeBrowsingCheck* check) { 1076 void LocalSafeBrowsingDatabaseManager::TimeoutCallback(
1077 SafeBrowsingCheck* check) {
1069 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1078 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1070 DCHECK(check); 1079 DCHECK(check);
1071 1080
1072 if (!enabled_) 1081 if (!enabled_)
1073 return; 1082 return;
1074 1083
1075 DCHECK(checks_.find(check) != checks_.end()); 1084 DCHECK(checks_.find(check) != checks_.end());
1076 if (check->client) { 1085 if (check->client) {
1077 check->client->OnSafeBrowsingResult(*check); 1086 check->OnSafeBrowsingResult();
1078 check->client = NULL; 1087 check->client = NULL;
1079 } 1088 }
1080 } 1089 }
1081 1090
1082 void SafeBrowsingDatabaseManager::SafeBrowsingCheckDone( 1091 void LocalSafeBrowsingDatabaseManager::SafeBrowsingCheckDone(
1083 SafeBrowsingCheck* check) { 1092 SafeBrowsingCheck* check) {
1084 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1093 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1085 DCHECK(check); 1094 DCHECK(check);
1086 1095
1087 if (!enabled_) 1096 if (!enabled_)
1088 return; 1097 return;
1089 1098
1090 DVLOG(1) << "SafeBrowsingCheckDone"; 1099 DVLOG(1) << "SafeBrowsingCheckDone";
1091 DCHECK(checks_.find(check) != checks_.end()); 1100 DCHECK(checks_.find(check) != checks_.end());
1092 if (check->client) 1101 if (check->client)
1093 check->client->OnSafeBrowsingResult(*check); 1102 check->OnSafeBrowsingResult();
1094 checks_.erase(check); 1103 checks_.erase(check);
1095 delete check; 1104 delete check;
1096 } 1105 }
1097 1106
1098 void SafeBrowsingDatabaseManager::StartSafeBrowsingCheck( 1107 void LocalSafeBrowsingDatabaseManager::StartSafeBrowsingCheck(
1099 SafeBrowsingCheck* check, 1108 SafeBrowsingCheck* check,
1100 const base::Callback<std::vector<SBPrefix>(void)>& task) { 1109 const base::Callback<std::vector<SBPrefix>(void)>& task) {
1101 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1102 check->weak_ptr_factory_.reset( 1111 check->weak_ptr_factory_.reset(
1103 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); 1112 new base::WeakPtrFactory<LocalSafeBrowsingDatabaseManager>(this));
1104 checks_.insert(check); 1113 checks_.insert(check);
1105 1114
1106 base::PostTaskAndReplyWithResult( 1115 base::PostTaskAndReplyWithResult(
1107 safe_browsing_task_runner_.get(), FROM_HERE, task, 1116 safe_browsing_task_runner_.get(), FROM_HERE, task,
1108 base::Bind(&SafeBrowsingDatabaseManager::OnAsyncCheckDone, 1117 base::Bind(&LocalSafeBrowsingDatabaseManager::OnAsyncCheckDone,
1109 check->weak_ptr_factory_->GetWeakPtr(), check)); 1118 check->weak_ptr_factory_->GetWeakPtr(), check));
1110 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, 1119 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
1111 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, 1120 base::Bind(&LocalSafeBrowsingDatabaseManager::TimeoutCallback,
1112 check->weak_ptr_factory_->GetWeakPtr(), check), 1121 check->weak_ptr_factory_->GetWeakPtr(), check),
1113 check_timeout_); 1122 check_timeout_);
1114 } 1123 }
1124
1125 bool LocalSafeBrowsingDatabaseManager::download_protection_enabled() const {
1126 return enable_download_protection_;
1127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698