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 #ifndef CHROME_BROWSER_HISTORY_DOMAIN_DIVERSITY_REPORTER_H_
6 #define CHROME_BROWSER_HISTORY_DOMAIN_DIVERSITY_REPORTER_H_
7 
8 #include <vector>
9 
10 #include "base/scoped_observer.h"
11 #include "base/time/clock.h"
12 #include "components/history/core/browser/history_service.h"
13 #include "components/history/core/browser/history_service_observer.h"
14 
15 class PrefService;
16 
17 namespace user_prefs {
18 class PrefRegistrySyncable;
19 }
20 
21 // A profile keyed service responsible for scheduling periodic tasks to report
22 // domain diversity metrics.
23 class DomainDiversityReporter : public KeyedService,
24                                 public history::HistoryServiceObserver {
25  public:
26   DomainDiversityReporter(history::HistoryService* history_service,
27                           PrefService* prefs,
28                           base::Clock* clock);
29   ~DomainDiversityReporter() override;
30 
31   // Registers Profile preferences in |registry|.
32   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
33 
34   // Invokes ComputeDomainMetrics() if history backend is already loaded.
35   // Otherwise, use a HistoryServiceObserver to start ComputeDomainMetrics()
36   // as soon as the backend is loaded.
37   void MaybeComputeDomainMetrics();
38 
39   // Computes the domain diversity metric and emits histogram through callback,
40   // and schedules another domain metric computation task for 24 hours later.
41   void ComputeDomainMetrics();
42 
43   // Callback to emit histograms for domain metrics.
44   void ReportDomainMetrics(base::Time time_current_report_triggered,
45                            history::DomainDiversityResults result);
46 
47   // HistoryServiceObserver:
48   void OnHistoryServiceLoaded(
49       history::HistoryService* history_service) override;
50   void HistoryServiceBeingDeleted(
51       history::HistoryService* history_service) override;
52 
53   // KeyedService implementation.
Shutdown()54   void Shutdown() override {}
55 
56  private:
57   history::HistoryService* history_service_;
58   PrefService* prefs_;
59   base::Clock* clock_;
60 
61   ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
62       history_service_observer_;
63   base::CancelableTaskTracker cancelable_task_tracker_;
64 
65   base::WeakPtrFactory<DomainDiversityReporter> weak_ptr_factory_{this};
66 
67   DISALLOW_COPY_AND_ASSIGN(DomainDiversityReporter);
68 };
69 
70 #endif  // CHROME_BROWSER_HISTORY_DOMAIN_DIVERSITY_REPORTER_H_
71