OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/rappor/rappor_service.h" | |
6 | |
7 #include <cstdlib> | |
8 | |
9 #include "base/base64.h" | |
10 #include "base/hash.h" | |
11 #include "base/metrics/field_trial.h" | |
12 #include "base/prefs/pref_registry_simple.h" | |
13 #include "base/prefs/pref_service.h" | |
14 #include "base/rand_util.h" | |
15 #include "base/stl_util.h" | |
16 #include "components/rappor/proto/rappor_metric.pb.h" | |
17 #include "components/rappor/rappor_pref_names.h" | |
18 #include "components/variations/variations_associated_data.h" | |
19 | |
20 namespace { | |
21 | |
22 // Seconds before the initial log is generated. | |
23 const int kInitialLogIntervalSeconds = 15; | |
24 // Interval between ongoing logs. | |
25 const int kLogIntervalSeconds = 30 * 60; | |
26 | |
27 const char kMimeType[] = "application/vnd.chrome.rappor"; | |
28 | |
29 // Constants for the RAPPOR rollout field trial. | |
30 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; | |
31 | |
32 // Constant for the finch parameter name for the server URL | |
33 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | |
34 | |
35 std::string GetServerUrl() { | |
Alexei Svitkine (slow)
2014/01/21 20:30:39
Return a GURL. Also, make LogUploader() take a GUR
Steven Holte
2014/01/21 21:05:32
Done.
| |
36 return chrome_variations::GetVariationParamValue( | |
37 kRapporRolloutFieldTrialName, | |
38 kRapporRolloutServerUrlParam); | |
39 } | |
40 | |
41 bool FieldTrialIsActive() { | |
42 return GetServerUrl() != ""; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 namespace rappor { | |
48 | |
49 RapporService::RapporService() {} | |
50 | |
51 RapporService::~RapporService() {} | |
52 | |
53 void RapporService::Start(PrefService* pref_service, | |
54 net::URLRequestContextGetter* request_context) { | |
55 if (!FieldTrialIsActive()) | |
Alexei Svitkine (slow)
2014/01/21 20:30:39
Remove this and just do:
GURL server_url = GetSer
Steven Holte
2014/01/21 21:05:32
Done.
| |
56 return; | |
57 DCHECK(!uploader_); | |
58 GetSecret(pref_service); | |
59 uploader_.reset(new LogUploader(GetServerUrl(), kMimeType, request_context)); | |
60 log_rotation_timer_.Start( | |
61 FROM_HERE, | |
62 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | |
63 this, | |
64 &RapporService::OnLogInterval); | |
65 } | |
66 | |
67 void RapporService::OnLogInterval() { | |
68 DCHECK(uploader_); | |
69 RapporReports reports; | |
70 LogMetrics(&reports); | |
71 if (reports.report_size() > 0) { | |
72 std::string log_text; | |
73 bool success = reports.SerializeToString(&log_text); | |
74 DCHECK(success); | |
75 uploader_->QueueLog(log_text); | |
76 } | |
77 log_rotation_timer_.Start(FROM_HERE, | |
78 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | |
79 this, | |
80 &RapporService::OnLogInterval); | |
81 } | |
82 | |
83 // static | |
84 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | |
85 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | |
86 } | |
87 | |
88 void RapporService::GetSecret(PrefService* pref_service) { | |
89 if (!secret_.empty()) | |
90 return; | |
91 std::string secret_base64 = | |
92 pref_service->GetString(prefs::kRapporSecret); | |
93 if (!secret_base64.empty()) { | |
94 bool decoded = base::Base64Decode(secret_base64, &secret_); | |
95 if (decoded) | |
96 return; | |
97 // If the preference fails to decode, it must be corrupt, so continue as | |
98 // though it didn't exist yet and generate a new one. | |
99 } | |
100 | |
101 secret_ = base::RandBytesAsString(128); | |
102 base::Base64Encode(secret_, &secret_base64); | |
103 pref_service->SetString(prefs::kRapporSecret, secret_base64); | |
104 } | |
105 | |
106 void RapporService::LogMetric(const RapporMetric& metric, | |
107 RapporReports::Report* report) { | |
108 report->set_name_hash(base::Hash(metric.parameters()->rappor_name)); | |
109 ByteVector bytes = metric.GetReport(secret_); | |
110 std::string byte_string(bytes.begin(), bytes.end()); | |
111 report->set_bits(byte_string); | |
112 } | |
113 | |
114 void RapporService::LogMetrics(RapporReports* reports) { | |
115 base::AutoLock auto_lock(lock_); | |
116 | |
117 for (std::map<std::string, RapporMetric*>::iterator it = metrics_map_.begin(); | |
118 metrics_map_.end() != it; | |
119 ++it) { | |
120 const RapporMetric* metric = it->second; | |
121 DCHECK_EQ(it->first, metric->parameters()->rappor_name); | |
122 RapporReports::Report* report = reports->add_report(); | |
123 LogMetric(*metric, report); | |
124 } | |
125 STLDeleteContainerPairSecondPointers( | |
126 metrics_map_.begin(), metrics_map_.end()); | |
127 metrics_map_.clear(); | |
128 } | |
129 | |
130 void RapporService::RecordSamples(const RapporParameters& parameters, | |
131 const std::vector<std::string>& samples) { | |
132 if (!FieldTrialIsActive()) | |
133 return; | |
134 RecordSamplesInternal(parameters, samples); | |
135 } | |
136 | |
137 void RapporService::RecordUrl(const RapporParameters& parameters, | |
138 const GURL& url) { | |
139 if (!FieldTrialIsActive()) | |
140 return; | |
141 std::vector<std::string> samples; | |
142 samples.push_back(url.spec()); | |
143 samples.push_back(url.host()); | |
144 // TODO(holte): break up host more | |
145 samples.push_back(url.query()); | |
146 samples.push_back(url.path()); | |
147 RecordSamplesInternal(parameters, samples); | |
148 } | |
149 | |
150 void RapporService::RecordSamplesInternal( | |
151 const RapporParameters& parameters, | |
152 const std::vector<std::string>& samples) { | |
153 base::AutoLock auto_lock(lock_); | |
154 | |
155 RapporMetric* metric = LookupMetric(parameters); | |
156 metric->AddSamples(samples); | |
157 } | |
158 | |
159 RapporMetric* RapporService::LookupMetric( | |
160 const RapporParameters& parameters) { | |
161 std::map<std::string, RapporMetric*>::iterator it = | |
162 metrics_map_.find(parameters.rappor_name); | |
163 if (metrics_map_.end() != it) { | |
164 RapporMetric* metric = it->second; | |
165 DCHECK_EQ(parameters, *metric->parameters()); | |
166 return metric; | |
167 } | |
168 | |
169 RapporMetric* new_metric = new RapporMetric(parameters); | |
170 metrics_map_[parameters.rappor_name] = new_metric; | |
171 return new_metric; | |
172 } | |
173 | |
174 } // namespace rappor | |
OLD | NEW |