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_
10 #include "base/basictypes.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "components/rappor/log_uploader.h"
15 #include "components/rappor/proto/rappor_metric.pb.h"
16 #include "components/rappor/rappor_metric.h"
18 class PrefRegistrySimple
;
22 // The type of data stored in a metric.
24 ETLD_PLUS_ONE_RAPPOR_TYPE
= 0,
28 // This class provides an interface for recording samples for rappor metrics,
29 // and periodically generates and uploads reports based on the collected data.
33 virtual ~RapporService();
35 // Starts the periodic generation of reports and upload attempts.
36 void Start(PrefService
* pref_service
, net::URLRequestContextGetter
* context
);
38 // Records a sample of the rappor metric specified by |metric_name|.
39 // Creates and initializes the metric, if it doesn't yet exist.
40 void RecordSample(const std::string
& metric_name
,
42 const std::string
& sample
);
44 // Sets the cohort value. For use by tests only.
45 void SetCohortForTesting(uint32_t cohort
) { cohort_
= cohort
; }
47 // Sets the secret value. For use by tests only.
48 void SetSecretForTesting(const std::string
& secret
) { secret_
= secret
; }
50 // Registers the names of all of the preferences used by RapporService in the
51 // provided PrefRegistry. This should be called before calling Start().
52 static void RegisterPrefs(PrefRegistrySimple
* registry
);
55 // Logs all of the collected metrics to the reports proto message. Exposed
56 // for tests. Returns true if any metrics were recorded.
57 bool ExportMetrics(RapporReports
* reports
);
59 // Records a sample of the rappor metric specified by |parameters|.
60 // Creates and initializes the metric, if it doesn't yet exist.
62 void RecordSampleInternal(const std::string
& metric_name
,
63 const RapporParameters
& parameters
,
64 const std::string
& sample
);
67 // Check if the service has been started successfully.
68 bool IsInitialized() const;
70 // Retrieves the cohort number this client was assigned to, generating it if
71 // doesn't already exist. The cohort should be persistent.
72 void LoadCohort(PrefService
* pref_service
);
74 // Retrieves the value for secret_ from preferences, generating it if doesn't
75 // already exist. The secret should be persistent, so that additional bits
76 // from the client do not get exposed over time.
77 void LoadSecret(PrefService
* pref_service
);
79 // Called whenever the logging interval elapses to generate a new log of
80 // reports and pass it to the uploader.
83 // Finds a metric in the metrics_map_, creating it if it doesn't already
85 RapporMetric
* LookUpMetric(const std::string
& metric_name
,
86 const RapporParameters
& parameters
);
88 // Client-side secret used to generate fake bits.
91 // The cohort this client is assigned to. -1 is uninitialized.
94 // Timer which schedules calls to OnLogInterval()
95 base::OneShotTimer
<RapporService
> log_rotation_timer_
;
97 // A private LogUploader instance for sending reports to the server.
98 scoped_ptr
<LogUploader
> uploader_
;
100 // We keep all registered metrics in a map, from name to metric.
101 // The map owns the metrics it contains.
102 std::map
<std::string
, RapporMetric
*> metrics_map_
;
104 DISALLOW_COPY_AND_ASSIGN(RapporService
);
107 } // namespace rappor
109 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_