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

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 7 years 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 "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 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 kServerUrl[] = "http://localhost:1234/rappor/v2";
28 const char kMimeType[] = "application/vnd.chrome.uma";
29
30 } // namespace
31
32 namespace rappor {
33
34 RapporService::RapporService() : uploader_(kServerUrl, kMimeType) {}
35
36 RapporService::~RapporService() {}
37
38 void RapporService::Start(PrefService* pref_service,
39 net::URLRequestContextGetter* request_context) {
40 GenerateRapporSecret(pref_service);
41 uploader_.SetRequestContext(request_context);
42 log_rotation_timer_.Start(
43 FROM_HERE,
44 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
45 this,
46 &RapporService::OnLogInterval);
47 }
48
49 void RapporService::OnLogInterval() {
50 rappor_metrics_proto_.Clear();
51 RecordRapporMetrics();
52 if (rappor_metrics_proto_.rappor_size() > 0) {
53 std::string log_text;
54 rappor_metrics_proto_.SerializeToString(&log_text);
55 uploader_.QueueLog(log_text);
56 }
57 log_rotation_timer_.Start(FROM_HERE,
58 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
59 this,
60 &RapporService::OnLogInterval);
61 }
62
63 // static
64 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
65 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
66 }
67
68 void RapporService::GenerateRapporSecret(PrefService* pref_service) {
69 if (!rappor_secret_.empty())
70 return;
71 std::string rappor_secret_64 = pref_service->GetString(prefs::kRapporSecret);
72 if (!rappor_secret_64.empty()) {
73 bool decoded = base::Base64Decode(rappor_secret_64, &rappor_secret_);
74 if (decoded) {
75 return;
76 }
77 }
78
79 rappor_secret_ = base::RandBytesAsString(128);
80 bool encoded = base::Base64Encode(rappor_secret_, &rappor_secret_64);
81 DCHECK(encoded);
82 pref_service->SetString(prefs::kRapporSecret, rappor_secret_64);
83 }
84
85 void RapporService::RecordRapporMetric(const std::string& metric_name,
86 const std::vector<uint8_t> bytes) {
87 RapporMetricsProto::RapporMetricProto* metric =
88 rappor_metrics_proto_.add_rappor();
89 metric->set_name_hash(base::Hash(metric_name));
90 std::string byte_string(bytes.begin(), bytes.end());
91 metric->set_bits(byte_string);
92 }
93
94 void RapporService::RecordRapporMetrics() {
95 RapporReporter rappor_reporter(rappor_secret_);
96
97 RapporRecorder::Rappors rappors;
98 g_rappor_recorder.Get().GetRappors(&rappors);
99 for (RapporRecorder::Rappors::const_iterator it = rappors.begin();
100 rappors.end() != it;
101 ++it) {
102 const Rappor& rappor = **it;
103 std::vector<uint8_t> bytes = rappor_reporter.GetReport(rappor);
104 RecordRapporMetric(rappor.rappor_name(), bytes);
105 }
106 g_rappor_recorder.Get().ClearRappors();
107 }
108
109 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698