blob: f02a85a3fe65e48d13738531627496c854a78b18 [file] [log] [blame]
[email protected]2a172e42014-02-21 04:06:101// 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.
4
5#ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
6#define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
7
[email protected]ccb49262014-03-26 04:10:178#include <map>
[email protected]2a172e42014-02-21 04:06:109#include <string>
10
11#include "base/basictypes.h"
[email protected]ccb49262014-03-26 04:10:1712#include "base/macros.h"
13#include "base/memory/scoped_ptr.h"
[email protected]2a172e42014-02-21 04:06:1014#include "base/timer/timer.h"
[email protected]2a172e42014-02-21 04:06:1015
16class PrefRegistrySimple;
[email protected]ccb49262014-03-26 04:10:1717class PrefService;
18
19namespace net {
20class URLRequestContextGetter;
21}
[email protected]2a172e42014-02-21 04:06:1022
23namespace rappor {
24
[email protected]ccb49262014-03-26 04:10:1725class LogUploader;
26class RapporMetric;
27class RapporReports;
28struct RapporParameters;
29
[email protected]2a172e42014-02-21 04:06:1030// The type of data stored in a metric.
31enum RapporType {
[email protected]ccb49262014-03-26 04:10:1732 // For sampling the eTLD+1 of a URL.
[email protected]2a172e42014-02-21 04:06:1033 ETLD_PLUS_ONE_RAPPOR_TYPE = 0,
34 NUM_RAPPOR_TYPES
35};
36
37// This class provides an interface for recording samples for rappor metrics,
38// and periodically generates and uploads reports based on the collected data.
39class RapporService {
40 public:
41 RapporService();
42 virtual ~RapporService();
43
44 // Starts the periodic generation of reports and upload attempts.
[email protected]f861eca2014-08-07 20:02:3845 void Start(PrefService* pref_service,
46 net::URLRequestContextGetter* context,
47 bool metrics_enabled);
[email protected]2a172e42014-02-21 04:06:1048
49 // Records a sample of the rappor metric specified by |metric_name|.
50 // Creates and initializes the metric, if it doesn't yet exist.
51 void RecordSample(const std::string& metric_name,
52 RapporType type,
53 const std::string& sample);
54
55 // Sets the cohort value. For use by tests only.
56 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; }
57
58 // Sets the secret value. For use by tests only.
59 void SetSecretForTesting(const std::string& secret) { secret_ = secret; }
60
61 // Registers the names of all of the preferences used by RapporService in the
62 // provided PrefRegistry. This should be called before calling Start().
63 static void RegisterPrefs(PrefRegistrySimple* registry);
64
65 protected:
[email protected]ccb49262014-03-26 04:10:1766 // Logs all of the collected metrics to the reports proto message and clears
67 // the internal map. Exposed for tests. Returns true if any metrics were
68 // recorded.
[email protected]2a172e42014-02-21 04:06:1069 bool ExportMetrics(RapporReports* reports);
70
71 // Records a sample of the rappor metric specified by |parameters|.
72 // Creates and initializes the metric, if it doesn't yet exist.
73 // Exposed for tests.
74 void RecordSampleInternal(const std::string& metric_name,
75 const RapporParameters& parameters,
76 const std::string& sample);
77
78 private:
79 // Check if the service has been started successfully.
80 bool IsInitialized() const;
81
82 // Retrieves the cohort number this client was assigned to, generating it if
83 // doesn't already exist. The cohort should be persistent.
84 void LoadCohort(PrefService* pref_service);
85
86 // Retrieves the value for secret_ from preferences, generating it if doesn't
87 // already exist. The secret should be persistent, so that additional bits
88 // from the client do not get exposed over time.
89 void LoadSecret(PrefService* pref_service);
90
91 // Called whenever the logging interval elapses to generate a new log of
92 // reports and pass it to the uploader.
93 void OnLogInterval();
94
95 // Finds a metric in the metrics_map_, creating it if it doesn't already
96 // exist.
97 RapporMetric* LookUpMetric(const std::string& metric_name,
98 const RapporParameters& parameters);
99
100 // Client-side secret used to generate fake bits.
101 std::string secret_;
102
103 // The cohort this client is assigned to. -1 is uninitialized.
104 int32_t cohort_;
105
[email protected]ccb49262014-03-26 04:10:17106 // Timer which schedules calls to OnLogInterval().
[email protected]2a172e42014-02-21 04:06:10107 base::OneShotTimer<RapporService> log_rotation_timer_;
108
109 // A private LogUploader instance for sending reports to the server.
110 scoped_ptr<LogUploader> uploader_;
111
112 // We keep all registered metrics in a map, from name to metric.
113 // The map owns the metrics it contains.
114 std::map<std::string, RapporMetric*> metrics_map_;
115
116 DISALLOW_COPY_AND_ASSIGN(RapporService);
117};
118
119} // namespace rappor
120
121#endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_