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

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

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 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
(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";
26 const char kMimeType[] = "application/vnd.chrome.uma";
27
28 } // namespace
29
30 namespace rappor {
31
32 RapporService::RapporService() : uploader_(kServerUrl, kMimeType) {}
33
34 RapporService::~RapporService() {}
35
36 void RapporService::Start(PrefService* pref_service,
37 net::URLRequestContextGetter* request_context) {
38 GenerateRapporSecret(pref_service);
39 uploader_.SetRequestContext(request_context);
40 log_rotation_timer_.Start(
41 FROM_HERE,
42 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
43 this,
44 &RapporService::OnLogInterval);
45 }
46
47 void RapporService::OnLogInterval() {
48 rappor_metrics_proto_.Clear();
49 LogRapporMetrics();
50 if (rappor_metrics_proto_.rappor_size() > 0) {
51 std::string log_text;
52 bool success = rappor_metrics_proto_.SerializeToString(&log_text);
53 DCHECK(success);
54 uploader_.QueueLog(log_text);
55 }
56 log_rotation_timer_.Start(FROM_HERE,
57 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
58 this,
59 &RapporService::OnLogInterval);
60 }
61
62 // static
63 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
64 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
65 }
66
67 void RapporService::GenerateRapporSecret(PrefService* pref_service) {
68 if (!rappor_secret_.empty())
69 return;
70 std::string rappor_secret_64 = pref_service->GetString(prefs::kRapporSecret);
71 if (!rappor_secret_64.empty()) {
72 bool decoded = base::Base64Decode(rappor_secret_64, &rappor_secret_);
73 if (decoded)
74 return;
75 }
76
77 rappor_secret_ = base::RandBytesAsString(128);
78 bool encoded = base::Base64Encode(rappor_secret_, &rappor_secret_64);
79 DCHECK(encoded);
80 pref_service->SetString(prefs::kRapporSecret, rappor_secret_64);
81 }
82
83 void RapporService::LogRappor(const RapporMetric& rappor) {
84 RapporMetricsProto::RapporMetric* metric = rappor_metrics_proto_.add_rappor();
85 metric->set_name_hash(base::Hash(rappor.parameters()->rappor_name));
86 ByteVector bytes = rappor.GetReport(rappor_secret_);
87 std::string byte_string(bytes.begin(), bytes.end());
88 metric->set_bits(byte_string);
89 }
90
91 void RapporService::LogRapporMetrics() {
92 base::AutoLock auto_lock(lock_);
93
94 for (RapporMap::iterator it = rappors_.begin(); rappors_.end() != it; ++it) {
95 const RapporMetric* rappor = it->second;
96 DCHECK_EQ(it->first, rappor->parameters()->rappor_name);
97 LogRappor(*rappor);
98 }
99 STLDeleteContainerPairSecondPointers(rappors_.begin(), rappors_.end());
100 rappors_.clear();
101 }
102
103 void RapporService::RecordSamples(const RapporParameters& parameters,
104 const std::vector<std::string>& samples) {
105 base::AutoLock auto_lock(lock_);
106
107 RapporMetric* rappor = GetRapporMetric(parameters);
108 rappor->AddSamples(samples);
109 }
110
111 RapporMetric* RapporService::GetRapporMetric(
112 const RapporParameters& parameters) {
113 RapporMap::iterator it = rappors_.find(parameters.rappor_name);
114 if (rappors_.end() != it) {
115 RapporMetric* rappor = it->second;
116 DCHECK_EQ(parameters.rappor_name, rappor->parameters()->rappor_name);
117 DCHECK_EQ(parameters.bloom_filter_size_bytes,
118 rappor->parameters()->bloom_filter_size_bytes);
119 DCHECK_EQ(parameters.bloom_filter_hash_count,
120 rappor->parameters()->bloom_filter_hash_count);
121 DCHECK_EQ(parameters.fake_prob, rappor->parameters()->fake_prob);
122 DCHECK_EQ(parameters.fake_one_prob, rappor->parameters()->fake_one_prob);
123 DCHECK_EQ(parameters.one_coin_prob, rappor->parameters()->one_coin_prob);
124 DCHECK_EQ(parameters.zero_coin_prob, rappor->parameters()->zero_coin_prob);
125 return rappor;
126 }
127
128 RapporMetric* new_rappor = new RapporMetric(parameters);
129 rappors_[parameters.rappor_name] = new_rappor;
130 return new_rappor;
131 }
132
133 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698