Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: components/rappor/rappor_service.h

Issue 511623002: Add a mechanism for collecting Rappor samples on a daily interval. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ 5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ 6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/timer/timer.h" 14 #include "base/timer/timer.h"
15 #include "components/metrics/daily_interval.h"
15 16
16 class PrefRegistrySimple; 17 class PrefRegistrySimple;
17 class PrefService; 18 class PrefService;
18 19
19 namespace net { 20 namespace net {
20 class URLRequestContextGetter; 21 class URLRequestContextGetter;
21 } 22 }
22 23
23 namespace rappor { 24 namespace rappor {
24 25
25 class LogUploader; 26 class LogUploader;
26 class RapporMetric; 27 class RapporMetric;
27 class RapporReports; 28 class RapporReports;
28 struct RapporParameters; 29 struct RapporParameters;
29 30
30 // The type of data stored in a metric. 31 // The type of data stored in a metric.
31 enum RapporType { 32 enum RapporType {
32 // For sampling the eTLD+1 of a URL. 33 // For sampling the eTLD+1 of a URL.
33 ETLD_PLUS_ONE_RAPPOR_TYPE = 0, 34 ETLD_PLUS_ONE_RAPPOR_TYPE = 0,
34 NUM_RAPPOR_TYPES 35 NUM_RAPPOR_TYPES
35 }; 36 };
36 37
37 // This class provides an interface for recording samples for rappor metrics, 38 // This class provides an interface for recording samples for rappor metrics,
38 // and periodically generates and uploads reports based on the collected data. 39 // and periodically generates and uploads reports based on the collected data.
39 class RapporService { 40 class RapporService {
40 public: 41 public:
41 RapporService(); 42 // Constructs a RapporService.
43 // Calling code is responsible for ensuring that the lifetime of
44 // |pref_service| is longer than the lifetime of RapporService.
45 explicit RapporService(PrefService* pref_service);
42 virtual ~RapporService(); 46 virtual ~RapporService();
43 47
48 // Add an observer for collecting daily metrics. The RapporService will
49 // take ownership of all observers.
Alexei Svitkine (slow) 2014/09/15 17:50:38 If ownership is taken, pass the param by scoped_pt
Steven Holte 2014/09/15 20:17:10 Done.
50 void AddDailyObserver(metrics::DailyObserver* observer);
51
44 // Starts the periodic generation of reports and upload attempts. 52 // Starts the periodic generation of reports and upload attempts.
45 void Start(PrefService* pref_service, 53 void Start(net::URLRequestContextGetter* context,
46 net::URLRequestContextGetter* context,
47 bool metrics_enabled); 54 bool metrics_enabled);
48 55
49 // Records a sample of the rappor metric specified by |metric_name|. 56 // Records a sample of the rappor metric specified by |metric_name|.
50 // Creates and initializes the metric, if it doesn't yet exist. 57 // Creates and initializes the metric, if it doesn't yet exist.
51 void RecordSample(const std::string& metric_name, 58 void RecordSample(const std::string& metric_name,
52 RapporType type, 59 RapporType type,
53 const std::string& sample); 60 const std::string& sample);
54 61
55 // Sets the cohort value. For use by tests only. 62 // Sets the cohort value. For use by tests only.
56 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; } 63 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; }
57 64
58 // Sets the secret value. For use by tests only. 65 // Sets the secret value. For use by tests only.
59 void SetSecretForTesting(const std::string& secret) { secret_ = secret; } 66 void SetSecretForTesting(const std::string& secret) { secret_ = secret; }
60 67
61 // Registers the names of all of the preferences used by RapporService in the 68 // Registers the names of all of the preferences used by RapporService in the
62 // provided PrefRegistry. This should be called before calling Start(). 69 // provided PrefRegistry. This should be called before calling Start().
63 static void RegisterPrefs(PrefRegistrySimple* registry); 70 static void RegisterPrefs(PrefRegistrySimple* registry);
64 71
65 protected: 72 protected:
73 // Retrieves the cohort number this client was assigned to, generating it if
74 // doesn't already exist. The cohort should be persistent.
75 void LoadCohort();
76
77 // Retrieves the value for secret_ from preferences, generating it if doesn't
78 // already exist. The secret should be persistent, so that additional bits
79 // from the client do not get exposed over time.
80 void LoadSecret();
81
66 // Logs all of the collected metrics to the reports proto message and clears 82 // 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 83 // the internal map. Exposed for tests. Returns true if any metrics were
68 // recorded. 84 // recorded.
69 bool ExportMetrics(RapporReports* reports); 85 bool ExportMetrics(RapporReports* reports);
70 86
71 // Records a sample of the rappor metric specified by |parameters|. 87 // Records a sample of the rappor metric specified by |parameters|.
72 // Creates and initializes the metric, if it doesn't yet exist. 88 // Creates and initializes the metric, if it doesn't yet exist.
73 // Exposed for tests. 89 // Exposed for tests.
74 void RecordSampleInternal(const std::string& metric_name, 90 void RecordSampleInternal(const std::string& metric_name,
75 const RapporParameters& parameters, 91 const RapporParameters& parameters,
76 const std::string& sample); 92 const std::string& sample);
77 93
78 private: 94 private:
79 // Check if the service has been started successfully. 95 // Check if the service has been started successfully.
80 bool IsInitialized() const; 96 bool IsInitialized() const;
81 97
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 98 // Called whenever the logging interval elapses to generate a new log of
92 // reports and pass it to the uploader. 99 // reports and pass it to the uploader.
93 void OnLogInterval(); 100 void OnLogInterval();
94 101
95 // Finds a metric in the metrics_map_, creating it if it doesn't already 102 // Finds a metric in the metrics_map_, creating it if it doesn't already
96 // exist. 103 // exist.
97 RapporMetric* LookUpMetric(const std::string& metric_name, 104 RapporMetric* LookUpMetric(const std::string& metric_name,
98 const RapporParameters& parameters); 105 const RapporParameters& parameters);
99 106
107 // A weak pointer to the PrefService used to read and write preferences.
108 PrefService* pref_service_;
109
100 // Client-side secret used to generate fake bits. 110 // Client-side secret used to generate fake bits.
101 std::string secret_; 111 std::string secret_;
102 112
103 // The cohort this client is assigned to. -1 is uninitialized. 113 // The cohort this client is assigned to. -1 is uninitialized.
104 int32_t cohort_; 114 int32_t cohort_;
105 115
106 // Timer which schedules calls to OnLogInterval(). 116 // Timer which schedules calls to OnLogInterval().
107 base::OneShotTimer<RapporService> log_rotation_timer_; 117 base::OneShotTimer<RapporService> log_rotation_timer_;
108 118
119 // A daily interval tracker for collecting metrics once a day.
120 metrics::DailyInterval daily_interval_;
121
109 // A private LogUploader instance for sending reports to the server. 122 // A private LogUploader instance for sending reports to the server.
110 scoped_ptr<LogUploader> uploader_; 123 scoped_ptr<LogUploader> uploader_;
111 124
112 // We keep all registered metrics in a map, from name to metric. 125 // We keep all registered metrics in a map, from name to metric.
113 // The map owns the metrics it contains. 126 // The map owns the metrics it contains.
114 std::map<std::string, RapporMetric*> metrics_map_; 127 std::map<std::string, RapporMetric*> metrics_map_;
115 128
116 DISALLOW_COPY_AND_ASSIGN(RapporService); 129 DISALLOW_COPY_AND_ASSIGN(RapporService);
117 }; 130 };
118 131
119 } // namespace rappor 132 } // namespace rappor
120 133
121 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ 134 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698