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

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
« no previous file with comments | « components/rappor/rappor_pref_names.cc ('k') | components/rappor/rappor_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_event.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.
49 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer);
50
44 // Starts the periodic generation of reports and upload attempts. 51 // Starts the periodic generation of reports and upload attempts.
45 void Start(PrefService* pref_service, 52 void Start(net::URLRequestContextGetter* context,
46 net::URLRequestContextGetter* context,
47 bool metrics_enabled); 53 bool metrics_enabled);
48 54
49 // Records a sample of the rappor metric specified by |metric_name|. 55 // Records a sample of the rappor metric specified by |metric_name|.
50 // Creates and initializes the metric, if it doesn't yet exist. 56 // Creates and initializes the metric, if it doesn't yet exist.
51 void RecordSample(const std::string& metric_name, 57 void RecordSample(const std::string& metric_name,
52 RapporType type, 58 RapporType type,
53 const std::string& sample); 59 const std::string& sample);
54 60
55 // Sets the cohort value. For use by tests only. 61 // Sets the cohort value. For use by tests only.
56 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; } 62 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; }
57 63
58 // Sets the secret value. For use by tests only. 64 // Sets the secret value. For use by tests only.
59 void SetSecretForTesting(const std::string& secret) { secret_ = secret; } 65 void SetSecretForTesting(const std::string& secret) { secret_ = secret; }
60 66
61 // Registers the names of all of the preferences used by RapporService in the 67 // Registers the names of all of the preferences used by RapporService in the
62 // provided PrefRegistry. This should be called before calling Start(). 68 // provided PrefRegistry. This should be called before calling Start().
63 static void RegisterPrefs(PrefRegistrySimple* registry); 69 static void RegisterPrefs(PrefRegistrySimple* registry);
64 70
65 protected: 71 protected:
72 // Retrieves the cohort number this client was assigned to, generating it if
73 // doesn't already exist. The cohort should be persistent.
74 void LoadCohort();
75
76 // Retrieves the value for secret_ from preferences, generating it if doesn't
77 // already exist. The secret should be persistent, so that additional bits
78 // from the client do not get exposed over time.
79 void LoadSecret();
80
66 // Logs all of the collected metrics to the reports proto message and clears 81 // 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 82 // the internal map. Exposed for tests. Returns true if any metrics were
68 // recorded. 83 // recorded.
69 bool ExportMetrics(RapporReports* reports); 84 bool ExportMetrics(RapporReports* reports);
70 85
71 // Records a sample of the rappor metric specified by |parameters|. 86 // Records a sample of the rappor metric specified by |parameters|.
72 // Creates and initializes the metric, if it doesn't yet exist. 87 // Creates and initializes the metric, if it doesn't yet exist.
73 // Exposed for tests. 88 // Exposed for tests.
74 void RecordSampleInternal(const std::string& metric_name, 89 void RecordSampleInternal(const std::string& metric_name,
75 const RapporParameters& parameters, 90 const RapporParameters& parameters,
76 const std::string& sample); 91 const std::string& sample);
77 92
78 private: 93 private:
79 // Check if the service has been started successfully. 94 // Check if the service has been started successfully.
80 bool IsInitialized() const; 95 bool IsInitialized() const;
81 96
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 97 // Called whenever the logging interval elapses to generate a new log of
92 // reports and pass it to the uploader. 98 // reports and pass it to the uploader.
93 void OnLogInterval(); 99 void OnLogInterval();
94 100
95 // Finds a metric in the metrics_map_, creating it if it doesn't already 101 // Finds a metric in the metrics_map_, creating it if it doesn't already
96 // exist. 102 // exist.
97 RapporMetric* LookUpMetric(const std::string& metric_name, 103 RapporMetric* LookUpMetric(const std::string& metric_name,
98 const RapporParameters& parameters); 104 const RapporParameters& parameters);
99 105
106 // A weak pointer to the PrefService used to read and write preferences.
107 PrefService* pref_service_;
108
100 // Client-side secret used to generate fake bits. 109 // Client-side secret used to generate fake bits.
101 std::string secret_; 110 std::string secret_;
102 111
103 // The cohort this client is assigned to. -1 is uninitialized. 112 // The cohort this client is assigned to. -1 is uninitialized.
104 int32_t cohort_; 113 int32_t cohort_;
105 114
106 // Timer which schedules calls to OnLogInterval(). 115 // Timer which schedules calls to OnLogInterval().
107 base::OneShotTimer<RapporService> log_rotation_timer_; 116 base::OneShotTimer<RapporService> log_rotation_timer_;
108 117
118 // A daily event for collecting metrics once a day.
119 metrics::DailyEvent daily_event_;
120
109 // A private LogUploader instance for sending reports to the server. 121 // A private LogUploader instance for sending reports to the server.
110 scoped_ptr<LogUploader> uploader_; 122 scoped_ptr<LogUploader> uploader_;
111 123
112 // We keep all registered metrics in a map, from name to metric. 124 // We keep all registered metrics in a map, from name to metric.
113 // The map owns the metrics it contains. 125 // The map owns the metrics it contains.
114 std::map<std::string, RapporMetric*> metrics_map_; 126 std::map<std::string, RapporMetric*> metrics_map_;
115 127
116 DISALLOW_COPY_AND_ASSIGN(RapporService); 128 DISALLOW_COPY_AND_ASSIGN(RapporService);
117 }; 129 };
118 130
119 } // namespace rappor 131 } // namespace rappor
120 132
121 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ 133 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
OLDNEW
« no previous file with comments | « components/rappor/rappor_pref_names.cc ('k') | components/rappor/rappor_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698