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 "components/rappor/proto/rappor_metric.pb.h" | |
| 15 #include "components/rappor/rappor_pref_names.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Seconds before the initial log is generated. | |
| 20 const int kInitialLogIntervalSeconds = 15; | |
| 21 // Interval between ongoing logs. | |
| 22 const int kLogIntervalSeconds = 30 * 60; | |
| 23 | |
| 24 const char kServerUrl[] = "http://localhost:1234/rappor/v2"; | |
| 25 const char kMimeType[] = "application/vnd.chrome.uma"; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 namespace rappor { | |
| 30 | |
| 31 RapporService::RapporService() : uploader_(kServerUrl, kMimeType) {} | |
| 32 | |
| 33 RapporService::~RapporService() {} | |
| 34 | |
| 35 void RapporService::Start(PrefService* pref_service, | |
| 36 net::URLRequestContextGetter* request_context) { | |
| 37 GenerateRapporSecret(pref_service); | |
| 38 uploader_.SetRequestContext(request_context); | |
| 39 log_rotation_timer_.Start( | |
| 40 FROM_HERE, | |
| 41 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | |
| 42 this, | |
| 43 &RapporService::OnLogInterval); | |
| 44 } | |
| 45 | |
| 46 void RapporService::OnLogInterval() { | |
| 47 rappor_metrics_proto_.Clear(); | |
| 48 LogRapporMetrics(); | |
| 49 if (rappor_metrics_proto_.rappor_size() > 0) { | |
| 50 std::string log_text; | |
| 51 bool success = rappor_metrics_proto_.SerializeToString(&log_text); | |
| 52 DCHECK(success); | |
| 53 uploader_.QueueLog(log_text); | |
| 54 } | |
| 55 log_rotation_timer_.Start(FROM_HERE, | |
| 56 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | |
| 57 this, | |
| 58 &RapporService::OnLogInterval); | |
| 59 } | |
| 60 | |
| 61 // static | |
| 62 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 63 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | |
| 64 } | |
| 65 | |
| 66 void RapporService::GenerateRapporSecret(PrefService* pref_service) { | |
| 67 if (!rappor_secret_.empty()) | |
| 68 return; | |
| 69 std::string rappor_secret_64 = pref_service->GetString(prefs::kRapporSecret); | |
| 70 if (!rappor_secret_64.empty()) { | |
| 71 bool decoded = base::Base64Decode(rappor_secret_64, &rappor_secret_); | |
| 72 if (decoded) | |
| 73 return; | |
| 74 } | |
| 75 | |
| 76 rappor_secret_ = base::RandBytesAsString(128); | |
| 77 bool encoded = base::Base64Encode(rappor_secret_, &rappor_secret_64); | |
| 78 DCHECK(encoded); | |
| 79 pref_service->SetString(prefs::kRapporSecret, rappor_secret_64); | |
| 80 } | |
| 81 | |
| 82 void RapporService::LogRappor(const RapporMetric& rappor) { | |
| 83 RapporMetricsProto::RapporMetric* metric = rappor_metrics_proto_.add_rappor(); | |
| 84 metric->set_name_hash(base::Hash(rappor.parameters()->rappor_name)); | |
| 85 ByteVector bytes = rappor.GetReport(rappor_secret_); | |
| 86 std::string byte_string(bytes.begin(), bytes.end()); | |
| 87 metric->set_bits(byte_string); | |
| 88 } | |
| 89 | |
| 90 void RapporService::LogRapporMetrics() { | |
| 91 base::AutoLock auto_lock(lock_); | |
| 92 | |
| 93 for (RapporMap::iterator it = rappors_.begin(); rappors_.end() != it; ++it) { | |
| 94 const RapporMetric* rappor = it->second; | |
| 95 DCHECK_EQ(it->first, rappor->parameters()->rappor_name); | |
| 96 LogRappor(*rappor); | |
| 97 delete rappor; | |
|
Alexei Svitkine (slow)
2013/12/24 16:59:30
Instead of deleting things manually, use STLDelete
Steven Holte
2014/01/04 00:12:54
Done.
| |
| 98 } | |
| 99 rappors_.clear(); | |
| 100 } | |
| 101 | |
| 102 void RapporService::RecordSamples(const RapporParameters* parameters, | |
| 103 const std::vector<std::string>& samples) { | |
| 104 base::AutoLock auto_lock(lock_); | |
| 105 | |
| 106 RapporMetric* rappor = GetRapporMetric(parameters); | |
| 107 rappor->AddSamples(samples); | |
| 108 } | |
| 109 | |
| 110 RapporMetric* RapporService::GetRapporMetric( | |
| 111 const RapporParameters* parameters) { | |
| 112 RapporMap::iterator it = rappors_.find(parameters->rappor_name); | |
| 113 if (rappors_.end() != it) { | |
| 114 RapporMetric* rappor = it->second; | |
| 115 DCHECK_EQ(parameters, rappor->parameters()); | |
| 116 return rappor; | |
| 117 } | |
| 118 | |
| 119 RapporMetric* new_rappor = new RapporMetric(parameters); | |
| 120 rappors_[parameters->rappor_name] = new_rappor; | |
| 121 return new_rappor; | |
| 122 } | |
| 123 | |
| 124 } // namespace rappor | |
| OLD | NEW |