/ios/chrome/browser/history/domain_diversity_reporter_factory.mm

https://github.com/chromium/chromium · Objective C++ · 83 lines · 62 code · 15 blank · 6 comment · 3 complexity · 79f7d3db84c5383797565cc4a64e8967 MD5 · raw file

  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ios/chrome/browser/history/domain_diversity_reporter_factory.h"
  5. #import "base/bind.h"
  6. #import "base/no_destructor.h"
  7. #import "base/time/default_clock.h"
  8. #import "build/build_config.h"
  9. #import "components/history/metrics/domain_diversity_reporter.h"
  10. #import "components/keyed_service/core/service_access_type.h"
  11. #import "components/keyed_service/ios/browser_state_dependency_manager.h"
  12. #import "components/pref_registry/pref_registry_syncable.h"
  13. #import "components/prefs/pref_service.h"
  14. #import "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
  15. #import "ios/chrome/browser/browser_state/chrome_browser_state.h"
  16. #import "ios/chrome/browser/history/history_service_factory.h"
  17. #if !defined(__has_feature) || !__has_feature(objc_arc)
  18. #error "This file requires ARC support."
  19. #endif
  20. // static
  21. DomainDiversityReporter* DomainDiversityReporterFactory::GetForBrowserState(
  22. web::BrowserState* browser_state) {
  23. return static_cast<DomainDiversityReporter*>(
  24. GetInstance()->GetServiceForBrowserState(browser_state, /*create=*/true));
  25. }
  26. // static
  27. DomainDiversityReporterFactory* DomainDiversityReporterFactory::GetInstance() {
  28. static base::NoDestructor<DomainDiversityReporterFactory> instance;
  29. return instance.get();
  30. }
  31. DomainDiversityReporterFactory::DomainDiversityReporterFactory()
  32. : BrowserStateKeyedServiceFactory(
  33. "DomainDiversityReporter",
  34. BrowserStateDependencyManager::GetInstance()) {
  35. DependsOn(ios::HistoryServiceFactory::GetInstance());
  36. }
  37. DomainDiversityReporterFactory::~DomainDiversityReporterFactory() = default;
  38. std::unique_ptr<KeyedService>
  39. DomainDiversityReporterFactory::BuildServiceInstanceFor(
  40. web::BrowserState* browser_state) const {
  41. ChromeBrowserState* chrome_browser_state =
  42. ChromeBrowserState::FromBrowserState(browser_state);
  43. if (chrome_browser_state->IsOffTheRecord())
  44. return nullptr;
  45. history::HistoryService* history_service =
  46. ios::HistoryServiceFactory::GetForBrowserState(
  47. chrome_browser_state, ServiceAccessType::EXPLICIT_ACCESS);
  48. // Only build DomainDiversityReporter service with a valid |history_service|.
  49. if (!history_service)
  50. return nullptr;
  51. return std::make_unique<DomainDiversityReporter>(
  52. history_service, chrome_browser_state->GetPrefs(),
  53. base::DefaultClock::GetInstance());
  54. }
  55. void DomainDiversityReporterFactory::RegisterBrowserStatePrefs(
  56. user_prefs::PrefRegistrySyncable* registry) {
  57. DomainDiversityReporter::RegisterProfilePrefs(registry);
  58. }
  59. web::BrowserState* DomainDiversityReporterFactory::GetBrowserStateToUse(
  60. web::BrowserState* context) const {
  61. return GetBrowserStateRedirectedInIncognito(context);
  62. }
  63. bool DomainDiversityReporterFactory::ServiceIsNULLWhileTesting() const {
  64. return true;
  65. }
  66. bool DomainDiversityReporterFactory::ServiceIsCreatedWithBrowserState() const {
  67. return true;
  68. }