Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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/prefs/pref_registry_simple.h" | |
| 12 #include "base/prefs/pref_service.h" | |
| 13 #include "base/rand_util.h" | |
| 14 #include "base/stl_util.h" | |
| 15 #include "components/rappor/proto/rappor_metric.pb.h" | |
| 16 #include "components/rappor/rappor_pref_names.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Seconds before the initial log is generated. | |
| 21 const int kInitialLogIntervalSeconds = 15; | |
| 22 // Interval between ongoing logs. | |
| 23 const int kLogIntervalSeconds = 30 * 60; | |
| 24 | |
| 25 const char kServerUrl[] = "http://localhost:1234/rappor/v2"; | |
|
Ilya Sherman
2014/01/10 11:00:32
nit: Please update this, yadda yadda.
Steven Holte
2014/01/15 04:53:44
Updated this, assuming the collector will extend t
| |
| 26 const char kMimeType[] = "application/vnd.chrome.uma"; | |
|
Ilya Sherman
2014/01/10 11:00:32
nit: This too, probably, though I bet it doesn't r
Steven Holte
2014/01/15 04:53:44
It doesn't seem to matter, but I changed it.
| |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace rappor { | |
| 31 | |
| 32 RapporService::RapporService() : uploader_(NULL) {} | |
| 33 | |
| 34 RapporService::~RapporService() { | |
| 35 if (uploader_) | |
| 36 delete uploader_; | |
|
Ilya Sherman
2014/01/10 11:00:32
No no no. Please use a smart pointer instead.
Steven Holte
2014/01/15 04:53:44
Done.
| |
| 37 } | |
| 38 | |
| 39 void RapporService::Start(PrefService* pref_service, | |
| 40 net::URLRequestContextGetter* request_context) { | |
| 41 DCHECK(!uploader_); | |
| 42 GenerateRapporSecret(pref_service); | |
| 43 uploader_ = new LogUploader(kServerUrl, kMimeType, request_context); | |
| 44 log_rotation_timer_.Start( | |
| 45 FROM_HERE, | |
| 46 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | |
| 47 this, | |
| 48 &RapporService::OnLogInterval); | |
| 49 } | |
| 50 | |
| 51 void RapporService::OnLogInterval() { | |
| 52 DCHECK(uploader_); | |
| 53 rappor_metrics_proto_.Clear(); | |
| 54 LogRapporMetrics(); | |
| 55 if (rappor_metrics_proto_.rappor_size() > 0) { | |
| 56 std::string log_text; | |
| 57 bool success = rappor_metrics_proto_.SerializeToString(&log_text); | |
| 58 DCHECK(success); | |
| 59 uploader_->QueueLog(log_text); | |
| 60 } | |
| 61 log_rotation_timer_.Start(FROM_HERE, | |
| 62 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | |
| 63 this, | |
| 64 &RapporService::OnLogInterval); | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 69 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | |
| 70 } | |
| 71 | |
| 72 void RapporService::GenerateRapporSecret(PrefService* pref_service) { | |
| 73 if (!rappor_secret_.empty()) | |
| 74 return; | |
| 75 std::string rappor_secret_64 = pref_service->GetString(prefs::kRapporSecret); | |
|
Ilya Sherman
2014/01/10 11:00:32
nit: I think "_base64" would be clearer than just
Steven Holte
2014/01/15 04:53:44
Done.
| |
| 76 if (!rappor_secret_64.empty()) { | |
| 77 bool decoded = base::Base64Decode(rappor_secret_64, &rappor_secret_); | |
| 78 if (decoded) | |
| 79 return; | |
|
Ilya Sherman
2014/01/10 11:00:32
Is there a reason to expect that decoding might fa
Steven Holte
2014/01/15 04:53:44
Done.
| |
| 80 } | |
| 81 | |
| 82 rappor_secret_ = base::RandBytesAsString(128); | |
| 83 bool encoded = base::Base64Encode(rappor_secret_, &rappor_secret_64); | |
| 84 DCHECK(encoded); | |
| 85 pref_service->SetString(prefs::kRapporSecret, rappor_secret_64); | |
| 86 } | |
| 87 | |
| 88 void RapporService::LogRappor(const RapporMetric& rappor) { | |
| 89 RapporMetricsProto::RapporMetric* metric = rappor_metrics_proto_.add_rappor(); | |
| 90 metric->set_name_hash(base::Hash(rappor.parameters()->rappor_name)); | |
| 91 ByteVector bytes = rappor.GetReport(rappor_secret_); | |
| 92 std::string byte_string(bytes.begin(), bytes.end()); | |
| 93 metric->set_bits(byte_string); | |
| 94 } | |
| 95 | |
| 96 void RapporService::LogRapporMetrics() { | |
| 97 base::AutoLock auto_lock(lock_); | |
| 98 | |
| 99 for (RapporMap::iterator it = rappors_.begin(); rappors_.end() != it; ++it) { | |
| 100 const RapporMetric* rappor = it->second; | |
| 101 DCHECK_EQ(it->first, rappor->parameters()->rappor_name); | |
| 102 LogRappor(*rappor); | |
| 103 } | |
| 104 STLDeleteContainerPairSecondPointers(rappors_.begin(), rappors_.end()); | |
| 105 rappors_.clear(); | |
| 106 } | |
| 107 | |
| 108 void RapporService::RecordSamples(const RapporParameters& parameters, | |
| 109 const std::vector<std::string>& samples) { | |
| 110 base::AutoLock auto_lock(lock_); | |
| 111 | |
| 112 RapporMetric* rappor = GetRapporMetric(parameters); | |
| 113 rappor->AddSamples(samples); | |
| 114 } | |
| 115 | |
| 116 void RapporService::RecordUrl(const RapporParameters& parameters, | |
| 117 const GURL& url) { | |
| 118 std::vector<std::string> samples; | |
| 119 samples.push_back(url.spec()); | |
| 120 samples.push_back(url.host()); | |
| 121 // TODO(holte) break up host more | |
|
Ilya Sherman
2014/01/10 11:00:32
nit: "TODO(holte)" -> "TODO(holte):"
Steven Holte
2014/01/15 04:53:44
Done.
| |
| 122 samples.push_back(url.query()); | |
| 123 samples.push_back(url.path()); | |
| 124 RecordSamples(parameters, samples); | |
| 125 } | |
| 126 | |
| 127 RapporMetric* RapporService::GetRapporMetric( | |
|
Ilya Sherman
2014/01/10 11:00:32
Please return a scoped_ptr to indicate transfer of
Steven Holte
2014/01/15 04:53:44
There is no transfer of ownership here. This is a
| |
| 128 const RapporParameters& parameters) { | |
| 129 RapporMap::iterator it = rappors_.find(parameters.rappor_name); | |
| 130 if (rappors_.end() != it) { | |
| 131 RapporMetric* rappor = it->second; | |
| 132 DCHECK_EQ(parameters.rappor_name, rappor->parameters()->rappor_name); | |
| 133 DCHECK_EQ(parameters.bloom_filter_size_bytes, | |
| 134 rappor->parameters()->bloom_filter_size_bytes); | |
| 135 DCHECK_EQ(parameters.bloom_filter_hash_count, | |
| 136 rappor->parameters()->bloom_filter_hash_count); | |
| 137 DCHECK_EQ(parameters.fake_prob, rappor->parameters()->fake_prob); | |
| 138 DCHECK_EQ(parameters.fake_one_prob, rappor->parameters()->fake_one_prob); | |
| 139 DCHECK_EQ(parameters.one_coin_prob, rappor->parameters()->one_coin_prob); | |
| 140 DCHECK_EQ(parameters.zero_coin_prob, rappor->parameters()->zero_coin_prob); | |
|
Ilya Sherman
2014/01/10 11:00:32
nit: How about just DCHECK_EQ(parameters, rappor->
| |
| 141 return rappor; | |
| 142 } | |
| 143 | |
| 144 RapporMetric* new_rappor = new RapporMetric(parameters); | |
| 145 rappors_[parameters.rappor_name] = new_rappor; | |
| 146 return new_rappor; | |
| 147 } | |
| 148 | |
| 149 } // namespace rappor | |
| OLD | NEW |