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.h" | |
| 16 #include "components/rappor/rappor_pref_names.h" | |
| 17 #include "components/rappor/rappor_recorder.h" | |
| 18 #include "components/rappor/rappor_reporter.h" | |
| 19 | |
| 20 using base::TimeDelta; | |
|
Alexei Svitkine (slow)
2013/12/11 15:27:05
Nit: Remove this and just use the base:: prefix in
Steven Holte
2013/12/11 20:31:26
Done.
| |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Seconds before the initial log is generated. | |
| 25 const int kInitialLogIntervalSeconds = 15; | |
| 26 // Interval between ongoing logs. | |
| 27 const int kLogIntervalSeconds = 30 * 60; | |
| 28 | |
| 29 const char kServerUrl[] = "http://localhost:1234/rappor/v2"; | |
| 30 const char kMimeType[] = "application/vnd.chrome.uma"; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 namespace rappor { | |
| 35 | |
| 36 // TODO(holte): Get rid of global | |
| 37 base::LazyInstance<RapporService>::Leaky g_rappor_service = | |
| 38 LAZY_INSTANCE_INITIALIZER; | |
| 39 | |
| 40 RapporService::RapporService() : uploader_(kServerUrl, kMimeType) { | |
| 41 } | |
| 42 | |
| 43 RapporService::~RapporService() {} | |
| 44 | |
| 45 void RapporService::Start(PrefService* pref_service, | |
| 46 net::URLRequestContextGetter* request_context) { | |
| 47 GenerateRapporSecret(pref_service); | |
| 48 uploader_.SetRequestContext(request_context); | |
| 49 log_rotation_timer_.Start(FROM_HERE, | |
| 50 TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | |
| 51 this, &RapporService::OnLogInterval); | |
| 52 } | |
| 53 | |
| 54 void RapporService::OnLogInterval() { | |
| 55 rappor_metrics_proto_.Clear(); | |
| 56 RecordRapporMetrics(); | |
| 57 if (rappor_metrics_proto_.rappor_size() > 0) { | |
| 58 std::string log_text; | |
| 59 rappor_metrics_proto_.SerializeToString(&log_text); | |
| 60 uploader_.QueueLog(log_text); | |
| 61 } | |
| 62 log_rotation_timer_.Start(FROM_HERE, | |
| 63 TimeDelta::FromSeconds(kLogIntervalSeconds), | |
| 64 this, &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); | |
| 76 if (!rappor_secret_64.empty()) { | |
| 77 bool decoded = base::Base64Decode(rappor_secret_64, &rappor_secret_); | |
| 78 if (decoded) { | |
| 79 return; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 rappor_secret_ = base::RandBytesAsString(128); | |
| 84 bool encoded = base::Base64Encode(rappor_secret_, &rappor_secret_64); | |
| 85 DCHECK(encoded); | |
| 86 pref_service->SetString(prefs::kRapporSecret, rappor_secret_64); | |
| 87 } | |
| 88 | |
| 89 void RapporService::RecordRapporMetric(const std::string& metric_name, | |
| 90 const std::vector<uint8_t> bytes) { | |
| 91 RapporMetricsProto::RapporMetricProto* metric = | |
| 92 rappor_metrics_proto_.add_rappor(); | |
| 93 metric->set_name_hash(base::Hash(metric_name)); | |
| 94 std::string byte_string(bytes.begin(), bytes.end()); | |
| 95 metric->set_bits(byte_string); | |
| 96 } | |
| 97 | |
| 98 void RapporService::RecordRapporMetrics() { | |
| 99 RapporReporter rappor_reporter(rappor_secret_); | |
| 100 | |
| 101 RapporRecorder::Rappors rappors; | |
| 102 g_rappor_recorder.Get().GetRappors(&rappors); | |
| 103 for (RapporRecorder::Rappors::const_iterator it = rappors.begin(); | |
| 104 rappors.end() != it; | |
| 105 ++it) { | |
| 106 const Rappor& rappor = **it; | |
| 107 std::vector<uint8_t> bytes = rappor_reporter.GetReport(rappor); | |
| 108 RecordRapporMetric(rappor.rappor_name(), bytes); | |
| 109 } | |
| 110 g_rappor_recorder.Get().ClearRappors(); | |
| 111 } | |
| 112 | |
| 113 } // namespace rappor | |
| OLD | NEW |