1 // Copyright 2014 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.
5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/timer/timer.h"
16 #include "components/metrics/daily_event.h"
17 #include "components/rappor/rappor_parameters.h"
19 class PrefRegistrySimple
;
23 class URLRequestContextGetter
;
32 // The type of data stored in a metric.
34 // For sampling the eTLD+1 of a URL.
35 ETLD_PLUS_ONE_RAPPOR_TYPE
= 0,
40 // This class provides an interface for recording samples for rappor metrics,
41 // and periodically generates and uploads reports based on the collected data.
44 // Constructs a RapporService.
45 // Calling code is responsible for ensuring that the lifetime of
46 // |pref_service| is longer than the lifetime of RapporService.
47 // |is_incognito_callback| will be called to test if incognito mode is active.
48 RapporService(PrefService
* pref_service
,
49 const base::Callback
<bool(void)> is_incognito_callback
);
50 virtual ~RapporService();
52 // Add an observer for collecting daily metrics.
53 void AddDailyObserver(scoped_ptr
<metrics::DailyEvent::Observer
> observer
);
55 // Starts the periodic generation of reports and upload attempts.
56 // |metrics enabled| should be true if UMA users have opted in.
57 void Start(net::URLRequestContextGetter
* context
,
58 bool metrics_enabled
);
60 // Records a sample of the rappor metric specified by |metric_name|.
61 // Creates and initializes the metric, if it doesn't yet exist.
62 void RecordSample(const std::string
& metric_name
,
64 const std::string
& sample
);
66 // Registers the names of all of the preferences used by RapporService in the
67 // provided PrefRegistry. This should be called before calling Start().
68 static void RegisterPrefs(PrefRegistrySimple
* registry
);
71 // Initializes the state of the RapporService.
72 void Initialize(int32_t cohort
,
73 const std::string
& secret
,
74 const ReportingLevel
& reporting_level
);
76 // Retrieves the cohort number this client was assigned to, generating it if
77 // doesn't already exist. The cohort should be persistent.
80 // Retrieves the value for secret_ from preferences, generating it if doesn't
81 // already exist. The secret should be persistent, so that additional bits
82 // from the client do not get exposed over time.
83 std::string
LoadSecret();
85 // Logs all of the collected metrics to the reports proto message and clears
86 // the internal map. Exposed for tests. Returns true if any metrics were
88 bool ExportMetrics(RapporReports
* reports
);
90 // Records a sample of the rappor metric specified by |parameters|.
91 // Creates and initializes the metric, if it doesn't yet exist.
93 void RecordSampleInternal(const std::string
& metric_name
,
94 const RapporParameters
& parameters
,
95 const std::string
& sample
);
98 // Check if the service has been started successfully.
99 bool IsInitialized() const;
101 // Called whenever the logging interval elapses to generate a new log of
102 // reports and pass it to the uploader.
103 void OnLogInterval();
105 // Finds a metric in the metrics_map_, creating it if it doesn't already
107 RapporMetric
* LookUpMetric(const std::string
& metric_name
,
108 const RapporParameters
& parameters
);
110 // A weak pointer to the PrefService used to read and write preferences.
111 PrefService
* pref_service_
;
113 // A callback for testing if incognito mode is active;
114 const base::Callback
<bool(void)> is_incognito_callback_
;
116 // Client-side secret used to generate fake bits.
119 // The cohort this client is assigned to. -1 is uninitialized.
122 // Timer which schedules calls to OnLogInterval().
123 base::OneShotTimer
<RapporService
> log_rotation_timer_
;
125 // A daily event for collecting metrics once a day.
126 metrics::DailyEvent daily_event_
;
128 // A private LogUploader instance for sending reports to the server.
129 scoped_ptr
<LogUploader
> uploader_
;
131 // What reporting level of metrics are being reported.
132 ReportingLevel reporting_level_
;
134 // We keep all registered metrics in a map, from name to metric.
135 // The map owns the metrics it contains.
136 std::map
<std::string
, RapporMetric
*> metrics_map_
;
138 DISALLOW_COPY_AND_ASSIGN(RapporService
);
141 } // namespace rappor
143 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_