1 // Copyright 2019 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 
5 #include "chrome/browser/history/domain_diversity_reporter_factory.h"
6 
7 #include <string>
8 
9 #include "base/bind.h"
10 #include "base/time/default_clock.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/history/domain_diversity_reporter.h"
13 #include "chrome/browser/history/history_service_factory.h"
14 #include "chrome/browser/profiles/incognito_helpers.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "components/pref_registry/pref_registry_syncable.h"
18 #include "components/prefs/pref_service.h"
19 
20 // static
GetForProfile(Profile * profile)21 DomainDiversityReporter* DomainDiversityReporterFactory::GetForProfile(
22     Profile* profile) {
23   return static_cast<DomainDiversityReporter*>(
24       GetInstance()->GetServiceForBrowserContext(profile, true));
25 }
26 
27 // static
GetInstance()28 DomainDiversityReporterFactory* DomainDiversityReporterFactory::GetInstance() {
29   return base::Singleton<DomainDiversityReporterFactory>::get();
30 }
31 
32 // static
BuildInstanceFor(content::BrowserContext * context)33 std::unique_ptr<KeyedService> DomainDiversityReporterFactory::BuildInstanceFor(
34     content::BrowserContext* context) {
35   Profile* profile = static_cast<Profile*>(context);
36 
37   history::HistoryService* history_service =
38       HistoryServiceFactory::GetForProfile(profile,
39                                            ServiceAccessType::EXPLICIT_ACCESS);
40 
41   return std::make_unique<DomainDiversityReporter>(
42       history_service, profile->GetPrefs(), base::DefaultClock::GetInstance());
43 }
44 
DomainDiversityReporterFactory()45 DomainDiversityReporterFactory::DomainDiversityReporterFactory()
46     : BrowserContextKeyedServiceFactory(
47           "DomainDiversityReporter",
48           BrowserContextDependencyManager::GetInstance()) {
49   DependsOn(HistoryServiceFactory::GetInstance());
50 }
51 
52 DomainDiversityReporterFactory::~DomainDiversityReporterFactory() = default;
53 
BuildServiceInstanceFor(content::BrowserContext * profile) const54 KeyedService* DomainDiversityReporterFactory::BuildServiceInstanceFor(
55     content::BrowserContext* profile) const {
56   return BuildInstanceFor(static_cast<Profile*>(profile)).release();
57 }
58 
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)59 void DomainDiversityReporterFactory::RegisterProfilePrefs(
60     user_prefs::PrefRegistrySyncable* registry) {
61   DomainDiversityReporter::RegisterProfilePrefs(registry);
62 }
63 
GetBrowserContextToUse(content::BrowserContext * context) const64 content::BrowserContext* DomainDiversityReporterFactory::GetBrowserContextToUse(
65     content::BrowserContext* context) const {
66   return chrome::GetBrowserContextRedirectedInIncognito(context);
67 }
68 
ServiceIsNULLWhileTesting() const69 bool DomainDiversityReporterFactory::ServiceIsNULLWhileTesting() const {
70   return true;
71 }
72 
ServiceIsCreatedWithBrowserContext() const73 bool DomainDiversityReporterFactory::ServiceIsCreatedWithBrowserContext()
74     const {
75   return true;
76 }
77