Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
| 24 #include "base/trace_event/trace_event.h" | 24 #include "base/trace_event/trace_event.h" |
| 25 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 26 #include "chrome/browser/browser_process.h" | 26 #include "chrome/browser/browser_process.h" |
| 27 #include "chrome/browser/chrome_notification_types.h" | 27 #include "chrome/browser/chrome_notification_types.h" |
| 28 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/browser/profiles/profile_manager.h" | 29 #include "chrome/browser/profiles/profile_manager.h" |
| 30 #include "chrome/browser/safe_browsing/chrome_password_protection_service.h" | |
| 30 #include "chrome/browser/safe_browsing/ping_manager.h" | 31 #include "chrome/browser/safe_browsing/ping_manager.h" |
| 31 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" | 32 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" |
| 32 #include "chrome/browser/safe_browsing/ui_manager.h" | 33 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 33 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
| 34 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
| 35 #include "chrome/common/pref_names.h" | 36 #include "chrome/common/pref_names.h" |
| 36 #include "chrome/common/safe_browsing/file_type_policies.h" | 37 #include "chrome/common/safe_browsing/file_type_policies.h" |
| 37 #include "components/prefs/pref_change_registrar.h" | 38 #include "components/prefs/pref_change_registrar.h" |
| 38 #include "components/prefs/pref_service.h" | 39 #include "components/prefs/pref_service.h" |
| 39 #include "components/safe_browsing/common/safebrowsing_constants.h" | 40 #include "components/safe_browsing/common/safebrowsing_constants.h" |
| 40 #include "components/safe_browsing/common/safebrowsing_switches.h" | 41 #include "components/safe_browsing/common/safebrowsing_switches.h" |
| 41 #include "components/safe_browsing/password_protection/password_protection_servi ce.h" | |
| 42 #include "components/safe_browsing_db/database_manager.h" | 42 #include "components/safe_browsing_db/database_manager.h" |
| 43 #include "components/safe_browsing_db/v4_feature_list.h" | 43 #include "components/safe_browsing_db/v4_feature_list.h" |
| 44 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | 44 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
| 45 #include "components/safe_browsing_db/v4_local_database_manager.h" | 45 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 46 #include "content/public/browser/browser_thread.h" | 46 #include "content/public/browser/browser_thread.h" |
| 47 #include "content/public/browser/cookie_store_factory.h" | 47 #include "content/public/browser/cookie_store_factory.h" |
| 48 #include "content/public/browser/notification_service.h" | 48 #include "content/public/browser/notification_service.h" |
| 49 #include "content/public/browser/resource_request_info.h" | 49 #include "content/public/browser/resource_request_info.h" |
| 50 #include "google_apis/google_api_keys.h" | 50 #include "google_apis/google_api_keys.h" |
| 51 #include "net/cookies/cookie_store.h" | 51 #include "net/cookies/cookie_store.h" |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 } | 325 } |
| 326 | 326 |
| 327 if (base::FeatureList::IsEnabled( | 327 if (base::FeatureList::IsEnabled( |
| 328 SafeBrowsingNavigationObserverManager::kDownloadAttribution)) { | 328 SafeBrowsingNavigationObserverManager::kDownloadAttribution)) { |
| 329 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); | 329 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); |
| 330 } | 330 } |
| 331 | 331 |
| 332 services_delegate_->Initialize(v4_enabled_); | 332 services_delegate_->Initialize(v4_enabled_); |
| 333 services_delegate_->InitializeCsdService(url_request_context_getter_.get()); | 333 services_delegate_->InitializeCsdService(url_request_context_getter_.get()); |
| 334 | 334 |
| 335 // TODO(jialiul): When PasswordProtectionService does more than reporting UMA, | |
| 336 // we need to add finch trial to gate its functionality. | |
| 337 password_protection_service_ = base::MakeUnique<PasswordProtectionService>( | |
| 338 database_manager(), url_request_context()); | |
| 339 | |
| 340 // Track the safe browsing preference of existing profiles. | |
| 341 // The SafeBrowsingService will be started if any existing profile has the | |
| 342 // preference enabled. It will also listen for updates to the preferences. | |
| 343 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 344 if (profile_manager) { | |
| 345 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | |
| 346 // TODO(felt): I believe this for-loop is dead code. Confirm this and | |
| 347 // remove in a future CL. See https://codereview.chromium.org/1341533002/ | |
| 348 DCHECK_EQ(0u, profiles.size()); | |
| 349 for (size_t i = 0; i < profiles.size(); ++i) { | |
| 350 if (profiles[i]->IsOffTheRecord()) | |
| 351 continue; | |
| 352 AddPrefService(profiles[i]->GetPrefs()); | |
| 353 } | |
| 354 } | |
| 355 | |
| 356 // Track profile creation and destruction. | 335 // Track profile creation and destruction. |
| 357 prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, | 336 profiles_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, |
| 358 content::NotificationService::AllSources()); | 337 content::NotificationService::AllSources()); |
| 359 prefs_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 338 profiles_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 360 content::NotificationService::AllSources()); | 339 content::NotificationService::AllSources()); |
| 361 | 340 |
| 362 // Register all the delayed analysis to the incident reporting service. | 341 // Register all the delayed analysis to the incident reporting service. |
| 363 RegisterAllDelayedAnalysis(); | 342 RegisterAllDelayedAnalysis(); |
| 364 } | 343 } |
| 365 | 344 |
| 366 void SafeBrowsingService::ShutDown() { | 345 void SafeBrowsingService::ShutDown() { |
| 367 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 346 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 368 shutdown_callback_list_.Notify(); | 347 shutdown_callback_list_.Notify(); |
| 369 | 348 |
| 370 // Delete the PrefChangeRegistrars, whose dtors also unregister |this| as an | 349 // Delete the PrefChangeRegistrars, whose dtors also unregister |this| as an |
| 371 // observer of the preferences. | 350 // observer of the preferences. |
| 372 prefs_map_.clear(); | 351 prefs_map_.clear(); |
| 373 | 352 |
| 353 // Delete the ChromePasswordProtectionService instances. | |
| 354 password_protection_service_map_.clear(); | |
| 355 | |
| 374 // Remove Profile creation/destruction observers. | 356 // Remove Profile creation/destruction observers. |
| 375 prefs_registrar_.RemoveAll(); | 357 profiles_registrar_.RemoveAll(); |
| 376 | 358 |
| 377 Stop(true); | 359 Stop(true); |
| 378 | 360 |
| 379 password_protection_service_.reset(); | |
| 380 | |
| 381 services_delegate_->ShutdownServices(); | 361 services_delegate_->ShutdownServices(); |
| 382 | 362 |
| 383 // Since URLRequestContextGetters are refcounted, can't count on clearing | 363 // Since URLRequestContextGetters are refcounted, can't count on clearing |
| 384 // |url_request_context_getter_| to delete it, so need to shut it down first, | 364 // |url_request_context_getter_| to delete it, so need to shut it down first, |
| 385 // which will cancel any requests that are currently using it, and prevent | 365 // which will cancel any requests that are currently using it, and prevent |
| 386 // new requests from using it as well. | 366 // new requests from using it as well. |
| 387 BrowserThread::PostNonNestableTask( | 367 BrowserThread::PostNonNestableTask( |
| 388 BrowserThread::IO, FROM_HERE, | 368 BrowserThread::IO, FROM_HERE, |
| 389 base::Bind(&SafeBrowsingURLRequestContextGetter::ServiceShuttingDown, | 369 base::Bind(&SafeBrowsingURLRequestContextGetter::ServiceShuttingDown, |
| 390 url_request_context_getter_)); | 370 url_request_context_getter_)); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 SafeBrowsingPingManager* SafeBrowsingService::ping_manager() const { | 428 SafeBrowsingPingManager* SafeBrowsingService::ping_manager() const { |
| 449 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 429 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 450 return ping_manager_.get(); | 430 return ping_manager_.get(); |
| 451 } | 431 } |
| 452 | 432 |
| 453 const scoped_refptr<SafeBrowsingDatabaseManager>& | 433 const scoped_refptr<SafeBrowsingDatabaseManager>& |
| 454 SafeBrowsingService::v4_local_database_manager() const { | 434 SafeBrowsingService::v4_local_database_manager() const { |
| 455 return services_delegate_->v4_local_database_manager(); | 435 return services_delegate_->v4_local_database_manager(); |
| 456 } | 436 } |
| 457 | 437 |
| 458 PasswordProtectionService* SafeBrowsingService::password_protection_service() { | 438 PasswordProtectionService* SafeBrowsingService::GetPasswordProtectionService( |
| 459 return password_protection_service_.get(); | 439 Profile* profile) { |
| 440 DCHECK(profile); | |
| 441 auto it = password_protection_service_map_.find(profile); | |
| 442 DCHECK(it != password_protection_service_map_.end()); | |
| 443 return it->second.get(); | |
| 460 } | 444 } |
| 461 | 445 |
| 462 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> | 446 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
| 463 SafeBrowsingService::CreatePreferenceValidationDelegate( | 447 SafeBrowsingService::CreatePreferenceValidationDelegate( |
| 464 Profile* profile) const { | 448 Profile* profile) const { |
| 465 return services_delegate_->CreatePreferenceValidationDelegate(profile); | 449 return services_delegate_->CreatePreferenceValidationDelegate(profile); |
| 466 } | 450 } |
| 467 | 451 |
| 468 void SafeBrowsingService::RegisterDelayedAnalysisCallback( | 452 void SafeBrowsingService::RegisterDelayedAnalysisCallback( |
| 469 const DelayedAnalysisCallback& callback) { | 453 const DelayedAnalysisCallback& callback) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 base::Bind(&SafeBrowsingService::StopOnIOThread, this, shutdown)); | 635 base::Bind(&SafeBrowsingService::StopOnIOThread, this, shutdown)); |
| 652 } | 636 } |
| 653 | 637 |
| 654 void SafeBrowsingService::Observe(int type, | 638 void SafeBrowsingService::Observe(int type, |
| 655 const content::NotificationSource& source, | 639 const content::NotificationSource& source, |
| 656 const content::NotificationDetails& details) { | 640 const content::NotificationDetails& details) { |
| 657 switch (type) { | 641 switch (type) { |
| 658 case chrome::NOTIFICATION_PROFILE_CREATED: { | 642 case chrome::NOTIFICATION_PROFILE_CREATED: { |
| 659 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 643 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 660 Profile* profile = content::Source<Profile>(source).ptr(); | 644 Profile* profile = content::Source<Profile>(source).ptr(); |
| 645 CreatePasswordProtectionService(profile); | |
| 661 if (!profile->IsOffTheRecord()) | 646 if (!profile->IsOffTheRecord()) |
| 662 AddPrefService(profile->GetPrefs()); | 647 AddPrefService(profile->GetPrefs()); |
| 663 break; | 648 break; |
| 664 } | 649 } |
| 665 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 650 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 666 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 651 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 667 Profile* profile = content::Source<Profile>(source).ptr(); | 652 Profile* profile = content::Source<Profile>(source).ptr(); |
| 653 RemovePasswordProtectionService(profile); | |
| 668 if (!profile->IsOffTheRecord()) | 654 if (!profile->IsOffTheRecord()) |
| 669 RemovePrefService(profile->GetPrefs()); | 655 RemovePrefService(profile->GetPrefs()); |
| 670 break; | 656 break; |
| 671 } | 657 } |
| 672 default: | 658 default: |
| 673 NOTREACHED(); | 659 NOTREACHED(); |
| 674 } | 660 } |
| 675 } | 661 } |
| 676 | 662 |
| 677 void SafeBrowsingService::AddPrefService(PrefService* pref_service) { | 663 void SafeBrowsingService::AddPrefService(PrefService* pref_service) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 768 if (ping_manager()) | 754 if (ping_manager()) |
| 769 ping_manager()->ReportThreatDetails(report); | 755 ping_manager()->ReportThreatDetails(report); |
| 770 } | 756 } |
| 771 | 757 |
| 772 void SafeBrowsingService::ProcessResourceRequest( | 758 void SafeBrowsingService::ProcessResourceRequest( |
| 773 const ResourceRequestInfo& request) { | 759 const ResourceRequestInfo& request) { |
| 774 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 760 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 775 services_delegate_->ProcessResourceRequest(&request); | 761 services_delegate_->ProcessResourceRequest(&request); |
| 776 } | 762 } |
| 777 | 763 |
| 764 void SafeBrowsingService::CreatePasswordProtectionService(Profile* profile) { | |
| 765 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 766 DCHECK(profile); | |
| 767 auto it = password_protection_service_map_.find(profile); | |
| 768 // Return early if there is already a ChromePasswordProtectionService instance | |
| 769 // created for this profile. | |
| 770 if (it != password_protection_service_map_.end() && it->second) | |
|
Nathan Parker
2017/03/30 21:38:12
should this be an error?
Jialiu Lin
2017/03/30 23:23:05
Right, it is impossible to create the same profile
| |
| 771 return; | |
| 772 | |
| 773 std::unique_ptr<ChromePasswordProtectionService> service = | |
| 774 base::MakeUnique<ChromePasswordProtectionService>(this, profile); | |
| 775 password_protection_service_map_[profile] = std::move(service); | |
| 776 } | |
| 777 | |
| 778 void SafeBrowsingService::RemovePasswordProtectionService(Profile* profile) { | |
| 779 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 780 DCHECK(profile); | |
| 781 auto it = password_protection_service_map_.find(profile); | |
| 782 if (it != password_protection_service_map_.end()) | |
| 783 password_protection_service_map_.erase(it); | |
|
Nathan Parker
2017/03/30 21:38:12
Also, maybe an error if it's not there. Could be a
Jialiu Lin
2017/03/30 23:23:05
Done.
| |
| 784 } | |
| 785 | |
| 778 } // namespace safe_browsing | 786 } // namespace safe_browsing |
| OLD | NEW |