| 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 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 SafeBrowsingURLRequestContextGetter::~SafeBrowsingURLRequestContextGetter() {} | 250 SafeBrowsingURLRequestContextGetter::~SafeBrowsingURLRequestContextGetter() {} |
| 251 | 251 |
| 252 // static | 252 // static |
| 253 SafeBrowsingServiceFactory* SafeBrowsingService::factory_ = NULL; | 253 SafeBrowsingServiceFactory* SafeBrowsingService::factory_ = NULL; |
| 254 | 254 |
| 255 // The default SafeBrowsingServiceFactory. Global, made a singleton so we | 255 // The default SafeBrowsingServiceFactory. Global, made a singleton so we |
| 256 // don't leak it. | 256 // don't leak it. |
| 257 class SafeBrowsingServiceFactoryImpl : public SafeBrowsingServiceFactory { | 257 class SafeBrowsingServiceFactoryImpl : public SafeBrowsingServiceFactory { |
| 258 public: | 258 public: |
| 259 SafeBrowsingService* CreateSafeBrowsingService() override { | 259 SafeBrowsingService* CreateSafeBrowsingService() override { |
| 260 return new SafeBrowsingService(); | 260 return new SafeBrowsingService( |
| 261 safe_browsing::V4FeatureList::IsLocalDatabaseManagerEnabled(), |
| 262 safe_browsing::V4FeatureList::IsV4OnlyEnabled()); |
| 261 } | 263 } |
| 262 | 264 |
| 263 private: | 265 private: |
| 264 friend struct base::DefaultLazyInstanceTraits<SafeBrowsingServiceFactoryImpl>; | 266 friend struct base::DefaultLazyInstanceTraits<SafeBrowsingServiceFactoryImpl>; |
| 265 | 267 |
| 266 SafeBrowsingServiceFactoryImpl() { } | 268 SafeBrowsingServiceFactoryImpl() { } |
| 267 | 269 |
| 268 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactoryImpl); | 270 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactoryImpl); |
| 269 }; | 271 }; |
| 270 | 272 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 285 } | 287 } |
| 286 | 288 |
| 287 | 289 |
| 288 // static | 290 // static |
| 289 SafeBrowsingService* SafeBrowsingService::CreateSafeBrowsingService() { | 291 SafeBrowsingService* SafeBrowsingService::CreateSafeBrowsingService() { |
| 290 if (!factory_) | 292 if (!factory_) |
| 291 factory_ = g_safe_browsing_service_factory_impl.Pointer(); | 293 factory_ = g_safe_browsing_service_factory_impl.Pointer(); |
| 292 return factory_->CreateSafeBrowsingService(); | 294 return factory_->CreateSafeBrowsingService(); |
| 293 } | 295 } |
| 294 | 296 |
| 295 SafeBrowsingService::SafeBrowsingService() | 297 SafeBrowsingService::SafeBrowsingService(bool v4_enabled, bool use_v4_only) |
| 296 : services_delegate_(ServicesDelegate::Create(this)), | 298 : services_delegate_(ServicesDelegate::Create(this)), |
| 297 estimated_extended_reporting_by_prefs_(SBER_LEVEL_OFF), | 299 estimated_extended_reporting_by_prefs_(SBER_LEVEL_OFF), |
| 298 enabled_(false), | 300 enabled_(false), |
| 299 enabled_by_prefs_(false), | 301 enabled_by_prefs_(false), |
| 300 enabled_v4_only_(safe_browsing::V4FeatureList::IsV4OnlyEnabled()) {} | 302 use_v4_only_(use_v4_only), |
| 303 v4_enabled_(v4_enabled) {} |
| 301 | 304 |
| 302 SafeBrowsingService::~SafeBrowsingService() { | 305 SafeBrowsingService::~SafeBrowsingService() { |
| 303 // We should have already been shut down. If we're still enabled, then the | 306 // We should have already been shut down. If we're still enabled, then the |
| 304 // database isn't going to be closed properly, which could lead to corruption. | 307 // database isn't going to be closed properly, which could lead to corruption. |
| 305 DCHECK(!enabled_); | 308 DCHECK(!enabled_); |
| 306 } | 309 } |
| 307 | 310 |
| 308 void SafeBrowsingService::Initialize() { | 311 void SafeBrowsingService::Initialize() { |
| 309 // Ensure FileTypePolicies's Singleton is instantiated during startup. | 312 // Ensure FileTypePolicies's Singleton is instantiated during startup. |
| 310 // This guarantees we'll log UMA metrics about its state. | 313 // This guarantees we'll log UMA metrics about its state. |
| 311 FileTypePolicies::GetInstance(); | 314 FileTypePolicies::GetInstance(); |
| 312 | 315 |
| 313 url_request_context_getter_ = new SafeBrowsingURLRequestContextGetter( | 316 url_request_context_getter_ = new SafeBrowsingURLRequestContextGetter( |
| 314 g_browser_process->system_request_context()); | 317 g_browser_process->system_request_context()); |
| 315 | 318 |
| 316 ui_manager_ = CreateUIManager(); | 319 ui_manager_ = CreateUIManager(); |
| 317 | 320 |
| 318 if (!enabled_v4_only_) { | 321 if (!use_v4_only_) { |
| 319 database_manager_ = CreateDatabaseManager(); | 322 database_manager_ = CreateDatabaseManager(); |
| 320 } | 323 } |
| 321 | 324 |
| 322 if (base::FeatureList::IsEnabled( | 325 if (base::FeatureList::IsEnabled( |
| 323 SafeBrowsingNavigationObserverManager::kDownloadAttribution)) { | 326 SafeBrowsingNavigationObserverManager::kDownloadAttribution)) { |
| 324 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); | 327 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); |
| 325 } | 328 } |
| 326 | 329 |
| 327 services_delegate_->Initialize(); | 330 services_delegate_->Initialize(v4_enabled_); |
| 328 services_delegate_->InitializeCsdService(url_request_context_getter_.get()); | 331 services_delegate_->InitializeCsdService(url_request_context_getter_.get()); |
| 329 | 332 |
| 330 // Track the safe browsing preference of existing profiles. | 333 // Track the safe browsing preference of existing profiles. |
| 331 // The SafeBrowsingService will be started if any existing profile has the | 334 // The SafeBrowsingService will be started if any existing profile has the |
| 332 // preference enabled. It will also listen for updates to the preferences. | 335 // preference enabled. It will also listen for updates to the preferences. |
| 333 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 336 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 334 if (profile_manager) { | 337 if (profile_manager) { |
| 335 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | 338 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); |
| 336 // TODO(felt): I believe this for-loop is dead code. Confirm this and | 339 // TODO(felt): I believe this for-loop is dead code. Confirm this and |
| 337 // remove in a future CL. See https://codereview.chromium.org/1341533002/ | 340 // remove in a future CL. See https://codereview.chromium.org/1341533002/ |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 url_request_context_getter_->DisableQuicOnIOThread(); | 411 url_request_context_getter_->DisableQuicOnIOThread(); |
| 409 } | 412 } |
| 410 | 413 |
| 411 const scoped_refptr<SafeBrowsingUIManager>& | 414 const scoped_refptr<SafeBrowsingUIManager>& |
| 412 SafeBrowsingService::ui_manager() const { | 415 SafeBrowsingService::ui_manager() const { |
| 413 return ui_manager_; | 416 return ui_manager_; |
| 414 } | 417 } |
| 415 | 418 |
| 416 const scoped_refptr<SafeBrowsingDatabaseManager>& | 419 const scoped_refptr<SafeBrowsingDatabaseManager>& |
| 417 SafeBrowsingService::database_manager() const { | 420 SafeBrowsingService::database_manager() const { |
| 418 return enabled_v4_only_ ? v4_local_database_manager() : database_manager_; | 421 return use_v4_only_ ? v4_local_database_manager() : database_manager_; |
| 419 } | 422 } |
| 420 | 423 |
| 421 scoped_refptr<SafeBrowsingNavigationObserverManager> | 424 scoped_refptr<SafeBrowsingNavigationObserverManager> |
| 422 SafeBrowsingService::navigation_observer_manager() { | 425 SafeBrowsingService::navigation_observer_manager() { |
| 423 return navigation_observer_manager_; | 426 return navigation_observer_manager_; |
| 424 } | 427 } |
| 425 | 428 |
| 426 SafeBrowsingProtocolManager* SafeBrowsingService::protocol_manager() const { | 429 SafeBrowsingProtocolManager* SafeBrowsingService::protocol_manager() const { |
| 427 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 430 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 428 #if defined(SAFE_BROWSING_DB_LOCAL) | 431 #if defined(SAFE_BROWSING_DB_LOCAL) |
| 429 DCHECK(!enabled_v4_only_); | 432 DCHECK(!use_v4_only_); |
| 430 return protocol_manager_.get(); | 433 return protocol_manager_.get(); |
| 431 #else | 434 #else |
| 432 return nullptr; | 435 return nullptr; |
| 433 #endif | 436 #endif |
| 434 } | 437 } |
| 435 | 438 |
| 436 SafeBrowsingPingManager* SafeBrowsingService::ping_manager() const { | 439 SafeBrowsingPingManager* SafeBrowsingService::ping_manager() const { |
| 437 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 440 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 438 return ping_manager_.get(); | 441 return ping_manager_.get(); |
| 439 } | 442 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 #endif // defined(OS_WIN) | 552 #endif // defined(OS_WIN) |
| 550 | 553 |
| 551 return client_name; | 554 return client_name; |
| 552 } | 555 } |
| 553 | 556 |
| 554 // Any tests that create a DatabaseManager that isn't derived from | 557 // Any tests that create a DatabaseManager that isn't derived from |
| 555 // LocalSafeBrowsingDatabaseManager should override this to return NULL. | 558 // LocalSafeBrowsingDatabaseManager should override this to return NULL. |
| 556 SafeBrowsingProtocolManagerDelegate* | 559 SafeBrowsingProtocolManagerDelegate* |
| 557 SafeBrowsingService::GetProtocolManagerDelegate() { | 560 SafeBrowsingService::GetProtocolManagerDelegate() { |
| 558 #if defined(SAFE_BROWSING_DB_LOCAL) | 561 #if defined(SAFE_BROWSING_DB_LOCAL) |
| 559 DCHECK(!enabled_v4_only_); | 562 DCHECK(!use_v4_only_); |
| 560 return static_cast<LocalSafeBrowsingDatabaseManager*>( | 563 return static_cast<LocalSafeBrowsingDatabaseManager*>( |
| 561 database_manager_.get()); | 564 database_manager_.get()); |
| 562 #else | 565 #else |
| 563 NOTREACHED(); | 566 NOTREACHED(); |
| 564 return NULL; | 567 return NULL; |
| 565 #endif | 568 #endif |
| 566 } | 569 } |
| 567 | 570 |
| 568 void SafeBrowsingService::StartOnIOThread( | 571 void SafeBrowsingService::StartOnIOThread( |
| 569 net::URLRequestContextGetter* url_request_context_getter) { | 572 net::URLRequestContextGetter* url_request_context_getter) { |
| 570 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 573 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 571 if (enabled_) | 574 if (enabled_) |
| 572 return; | 575 return; |
| 573 enabled_ = true; | 576 enabled_ = true; |
| 574 | 577 |
| 575 SafeBrowsingProtocolConfig config = GetProtocolConfig(); | 578 SafeBrowsingProtocolConfig config = GetProtocolConfig(); |
| 576 V4ProtocolConfig v4_config = GetV4ProtocolConfig(); | 579 V4ProtocolConfig v4_config = GetV4ProtocolConfig(); |
| 577 | 580 |
| 578 services_delegate_->StartOnIOThread(url_request_context_getter, v4_config); | 581 services_delegate_->StartOnIOThread(url_request_context_getter, v4_config); |
| 579 | 582 |
| 580 #if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) | 583 #if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) |
| 581 if (!enabled_v4_only_) { | 584 if (!use_v4_only_) { |
| 582 DCHECK(database_manager_.get()); | 585 DCHECK(database_manager_.get()); |
| 583 database_manager_->StartOnIOThread(url_request_context_getter, v4_config); | 586 database_manager_->StartOnIOThread(url_request_context_getter, v4_config); |
| 584 } | 587 } |
| 585 #endif | 588 #endif |
| 586 | 589 |
| 587 #if defined(SAFE_BROWSING_DB_LOCAL) | 590 #if defined(SAFE_BROWSING_DB_LOCAL) |
| 588 if (!enabled_v4_only_) { | 591 if (!use_v4_only_) { |
| 589 SafeBrowsingProtocolManagerDelegate* protocol_manager_delegate = | 592 SafeBrowsingProtocolManagerDelegate* protocol_manager_delegate = |
| 590 GetProtocolManagerDelegate(); | 593 GetProtocolManagerDelegate(); |
| 591 if (protocol_manager_delegate) { | 594 if (protocol_manager_delegate) { |
| 592 protocol_manager_ = SafeBrowsingProtocolManager::Create( | 595 protocol_manager_ = SafeBrowsingProtocolManager::Create( |
| 593 protocol_manager_delegate, url_request_context_getter, config); | 596 protocol_manager_delegate, url_request_context_getter, config); |
| 594 protocol_manager_->Initialize(); | 597 protocol_manager_->Initialize(); |
| 595 } | 598 } |
| 596 } | 599 } |
| 597 #endif | 600 #endif |
| 598 | 601 |
| 599 DCHECK(!ping_manager_); | 602 DCHECK(!ping_manager_); |
| 600 ping_manager_ = SafeBrowsingPingManager::Create( | 603 ping_manager_ = SafeBrowsingPingManager::Create( |
| 601 url_request_context_getter, config); | 604 url_request_context_getter, config); |
| 602 } | 605 } |
| 603 | 606 |
| 604 void SafeBrowsingService::StopOnIOThread(bool shutdown) { | 607 void SafeBrowsingService::StopOnIOThread(bool shutdown) { |
| 605 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 608 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 606 | 609 |
| 607 #if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) | 610 #if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE) |
| 608 if (!enabled_v4_only_) { | 611 if (!use_v4_only_) { |
| 609 database_manager_->StopOnIOThread(shutdown); | 612 database_manager_->StopOnIOThread(shutdown); |
| 610 } | 613 } |
| 611 #endif | 614 #endif |
| 612 ui_manager_->StopOnIOThread(shutdown); | 615 ui_manager_->StopOnIOThread(shutdown); |
| 613 | 616 |
| 614 services_delegate_->StopOnIOThread(shutdown); | 617 services_delegate_->StopOnIOThread(shutdown); |
| 615 | 618 |
| 616 if (enabled_) { | 619 if (enabled_) { |
| 617 enabled_ = false; | 620 enabled_ = false; |
| 618 | 621 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 ping_manager()->ReportThreatDetails(report); | 762 ping_manager()->ReportThreatDetails(report); |
| 760 } | 763 } |
| 761 | 764 |
| 762 void SafeBrowsingService::ProcessResourceRequest( | 765 void SafeBrowsingService::ProcessResourceRequest( |
| 763 const ResourceRequestInfo& request) { | 766 const ResourceRequestInfo& request) { |
| 764 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 767 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 765 services_delegate_->ProcessResourceRequest(&request); | 768 services_delegate_->ProcessResourceRequest(&request); |
| 766 } | 769 } |
| 767 | 770 |
| 768 } // namespace safe_browsing | 771 } // namespace safe_browsing |
| OLD | NEW |