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

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

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 #include "components/rappor/rappor_service.h" 5 #include "components/rappor/rappor_service.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_registry_simple.h" 9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 11 matching lines...) Expand all
22 22
23 namespace { 23 namespace {
24 24
25 // Seconds before the initial log is generated. 25 // Seconds before the initial log is generated.
26 const int kInitialLogIntervalSeconds = 15; 26 const int kInitialLogIntervalSeconds = 15;
27 // Interval between ongoing logs. 27 // Interval between ongoing logs.
28 const int kLogIntervalSeconds = 30 * 60; 28 const int kLogIntervalSeconds = 30 * 60;
29 29
30 const char kMimeType[] = "application/vnd.chrome.rappor"; 30 const char kMimeType[] = "application/vnd.chrome.rappor";
31 31
32 const char kRapporDailyIntervalHistogram[] =
33 "Rappor.DailyInterval.IntervalType";
34
32 // Constants for the RAPPOR rollout field trial. 35 // Constants for the RAPPOR rollout field trial.
33 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; 36 const char kRapporRolloutFieldTrialName[] = "RapporRollout";
34 37
35 // Constant for the finch parameter name for the server URL 38 // Constant for the finch parameter name for the server URL
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; 39 const char kRapporRolloutServerUrlParam[] = "ServerUrl";
37 40
38 // Constant for the finch parameter name for the server URL 41 // Constant for the finch parameter name for the server URL
39 const char kRapporRolloutRequireUmaParam[] = "RequireUma"; 42 const char kRapporRolloutRequireUmaParam[] = "RequireUma";
40 43
41 // The rappor server's URL. 44 // The rappor server's URL.
(...skipping 20 matching lines...) Expand all
62 16 /* Bloom filter size bytes */, 65 16 /* Bloom filter size bytes */,
63 2 /* Bloom filter hash count */, 66 2 /* Bloom filter hash count */,
64 rappor::PROBABILITY_50 /* Fake data probability */, 67 rappor::PROBABILITY_50 /* Fake data probability */,
65 rappor::PROBABILITY_50 /* Fake one probability */, 68 rappor::PROBABILITY_50 /* Fake one probability */,
66 rappor::PROBABILITY_75 /* One coin probability */, 69 rappor::PROBABILITY_75 /* One coin probability */,
67 rappor::PROBABILITY_25 /* Zero coin probability */}, 70 rappor::PROBABILITY_25 /* Zero coin probability */},
68 }; 71 };
69 72
70 } // namespace 73 } // namespace
71 74
72 RapporService::RapporService() : cohort_(-1) {} 75 RapporService::RapporService(PrefService* pref_service)
76 : pref_service_(pref_service),
77 cohort_(-1),
78 daily_interval_(pref_service,
79 prefs::kRapporLastDailySample,
80 kRapporDailyIntervalHistogram) {
81 }
73 82
74 RapporService::~RapporService() { 83 RapporService::~RapporService() {
75 STLDeleteValues(&metrics_map_); 84 STLDeleteValues(&metrics_map_);
76 } 85 }
77 86
78 void RapporService::Start(PrefService* pref_service, 87 void RapporService::AddDailyObserver(
79 net::URLRequestContextGetter* request_context, 88 scoped_ptr<metrics::DailyInterval::Observer> observer) {
89 daily_interval_.AddObserver(observer.Pass());
90 }
91
92 void RapporService::Start(net::URLRequestContextGetter* request_context,
80 bool metrics_enabled) { 93 bool metrics_enabled) {
81 const GURL server_url = GetServerUrl(metrics_enabled); 94 const GURL server_url = GetServerUrl(metrics_enabled);
82 if (!server_url.is_valid()) { 95 if (!server_url.is_valid()) {
83 DVLOG(1) << server_url.spec() << " is invalid. " 96 DVLOG(1) << server_url.spec() << " is invalid. "
84 << "RapporService not started."; 97 << "RapporService not started.";
85 return; 98 return;
86 } 99 }
87 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); 100 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec();
88 DCHECK(!uploader_); 101 DCHECK(!uploader_);
89 LoadSecret(pref_service); 102 LoadSecret();
90 LoadCohort(pref_service); 103 LoadCohort();
91 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); 104 uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
92 log_rotation_timer_.Start( 105 log_rotation_timer_.Start(
93 FROM_HERE, 106 FROM_HERE,
94 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), 107 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
95 this, 108 this,
96 &RapporService::OnLogInterval); 109 &RapporService::OnLogInterval);
97 } 110 }
98 111
99 void RapporService::OnLogInterval() { 112 void RapporService::OnLogInterval() {
100 DCHECK(uploader_); 113 DCHECK(uploader_);
101 DVLOG(2) << "RapporService::OnLogInterval"; 114 DVLOG(2) << "RapporService::OnLogInterval";
115 daily_interval_.CheckInterval();
102 RapporReports reports; 116 RapporReports reports;
103 if (ExportMetrics(&reports)) { 117 if (ExportMetrics(&reports)) {
104 std::string log_text; 118 std::string log_text;
105 bool success = reports.SerializeToString(&log_text); 119 bool success = reports.SerializeToString(&log_text);
106 DCHECK(success); 120 DCHECK(success);
107 DVLOG(1) << "RapporService sending a report of " 121 DVLOG(1) << "RapporService sending a report of "
108 << reports.report_size() << " value(s)."; 122 << reports.report_size() << " value(s).";
109 uploader_->QueueLog(log_text); 123 uploader_->QueueLog(log_text);
110 } 124 }
111 log_rotation_timer_.Start(FROM_HERE, 125 log_rotation_timer_.Start(FROM_HERE,
112 base::TimeDelta::FromSeconds(kLogIntervalSeconds), 126 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
113 this, 127 this,
114 &RapporService::OnLogInterval); 128 &RapporService::OnLogInterval);
115 } 129 }
116 130
117 // static 131 // static
118 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { 132 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
119 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); 133 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
120 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); 134 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
121 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); 135 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
136 metrics::DailyInterval::RegisterPref(registry,
137 prefs::kRapporLastDailySample);
122 } 138 }
123 139
124 void RapporService::LoadCohort(PrefService* pref_service) { 140 void RapporService::LoadCohort() {
125 DCHECK(!IsInitialized()); 141 DCHECK(!IsInitialized());
126 // Ignore and delete old cohort parameter. 142 // Ignore and delete old cohort parameter.
127 pref_service->ClearPref(prefs::kRapporCohortDeprecated); 143 pref_service_->ClearPref(prefs::kRapporCohortDeprecated);
128 144
129 cohort_ = pref_service->GetInteger(prefs::kRapporCohortSeed); 145 cohort_ = pref_service_->GetInteger(prefs::kRapporCohortSeed);
130 // If the user is already assigned to a valid cohort, we're done. 146 // If the user is already assigned to a valid cohort, we're done.
131 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) 147 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts)
132 return; 148 return;
133 149
134 // This is the first time the client has started the service (or their 150 // This is the first time the client has started the service (or their
135 // preferences were corrupted). Randomly assign them to a cohort. 151 // preferences were corrupted). Randomly assign them to a cohort.
136 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); 152 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts);
137 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_; 153 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_;
138 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort_); 154 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort_);
139 } 155 }
140 156
141 void RapporService::LoadSecret(PrefService* pref_service) { 157 void RapporService::LoadSecret() {
142 DCHECK(secret_.empty()); 158 DCHECK(secret_.empty());
143 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret); 159 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret);
144 if (!secret_base64.empty()) { 160 if (!secret_base64.empty()) {
145 bool decoded = base::Base64Decode(secret_base64, &secret_); 161 bool decoded = base::Base64Decode(secret_base64, &secret_);
146 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) 162 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
147 return; 163 return;
148 // If the preference fails to decode, or is the wrong size, it must be 164 // If the preference fails to decode, or is the wrong size, it must be
149 // corrupt, so continue as though it didn't exist yet and generate a new 165 // corrupt, so continue as though it didn't exist yet and generate a new
150 // one. 166 // one.
151 } 167 }
152 168
153 DVLOG(2) << "Generated a new Rappor secret."; 169 DVLOG(2) << "Generated a new Rappor secret.";
154 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); 170 secret_ = HmacByteVectorGenerator::GenerateEntropyInput();
155 base::Base64Encode(secret_, &secret_base64); 171 base::Base64Encode(secret_, &secret_base64);
156 pref_service->SetString(prefs::kRapporSecret, secret_base64); 172 pref_service_->SetString(prefs::kRapporSecret, secret_base64);
157 } 173 }
158 174
159 bool RapporService::ExportMetrics(RapporReports* reports) { 175 bool RapporService::ExportMetrics(RapporReports* reports) {
160 if (metrics_map_.empty()) 176 if (metrics_map_.empty())
161 return false; 177 return false;
162 178
163 DCHECK_GE(cohort_, 0); 179 DCHECK_GE(cohort_, 0);
164 reports->set_cohort(cohort_); 180 reports->set_cohort(cohort_);
165 181
166 for (std::map<std::string, RapporMetric*>::const_iterator it = 182 for (std::map<std::string, RapporMetric*>::const_iterator it =
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); 228 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
213 return metric; 229 return metric;
214 } 230 }
215 231
216 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); 232 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
217 metrics_map_[metric_name] = new_metric; 233 metrics_map_[metric_name] = new_metric;
218 return new_metric; 234 return new_metric;
219 } 235 }
220 236
221 } // namespace rappor 237 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698