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

Unified Diff: components/rappor/rappor_service.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: RapporMetric 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 side-by-side diff with in-line comments
Download patch
Index: components/rappor/rappor_service.cc
diff --git a/components/rappor/rappor_service.cc b/components/rappor/rappor_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aa71a4baf01611fe0fe6bc17aa1656d7507c44c2
--- /dev/null
+++ b/components/rappor/rappor_service.cc
@@ -0,0 +1,124 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/rappor/rappor_service.h"
+
+#include <cstdlib>
+
+#include "base/base64.h"
+#include "base/hash.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+#include "base/rand_util.h"
+#include "components/rappor/proto/rappor_metric.pb.h"
+#include "components/rappor/rappor_pref_names.h"
+
+namespace {
+
+// Seconds before the initial log is generated.
+const int kInitialLogIntervalSeconds = 15;
+// Interval between ongoing logs.
+const int kLogIntervalSeconds = 30 * 60;
+
+const char kServerUrl[] = "http://localhost:1234/rappor/v2";
+const char kMimeType[] = "application/vnd.chrome.uma";
+
+} // namespace
+
+namespace rappor {
+
+RapporService::RapporService() : uploader_(kServerUrl, kMimeType) {}
+
+RapporService::~RapporService() {}
+
+void RapporService::Start(PrefService* pref_service,
+ net::URLRequestContextGetter* request_context) {
+ GenerateRapporSecret(pref_service);
+ uploader_.SetRequestContext(request_context);
+ log_rotation_timer_.Start(
+ FROM_HERE,
+ base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
+ this,
+ &RapporService::OnLogInterval);
+}
+
+void RapporService::OnLogInterval() {
+ rappor_metrics_proto_.Clear();
+ LogRapporMetrics();
+ if (rappor_metrics_proto_.rappor_size() > 0) {
+ std::string log_text;
+ bool success = rappor_metrics_proto_.SerializeToString(&log_text);
+ DCHECK(success);
+ uploader_.QueueLog(log_text);
+ }
+ log_rotation_timer_.Start(FROM_HERE,
+ base::TimeDelta::FromSeconds(kLogIntervalSeconds),
+ this,
+ &RapporService::OnLogInterval);
+}
+
+// static
+void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
+ registry->RegisterStringPref(prefs::kRapporSecret, std::string());
+}
+
+void RapporService::GenerateRapporSecret(PrefService* pref_service) {
+ if (!rappor_secret_.empty())
+ return;
+ std::string rappor_secret_64 = pref_service->GetString(prefs::kRapporSecret);
+ if (!rappor_secret_64.empty()) {
+ bool decoded = base::Base64Decode(rappor_secret_64, &rappor_secret_);
+ if (decoded)
+ return;
+ }
+
+ rappor_secret_ = base::RandBytesAsString(128);
+ bool encoded = base::Base64Encode(rappor_secret_, &rappor_secret_64);
+ DCHECK(encoded);
+ pref_service->SetString(prefs::kRapporSecret, rappor_secret_64);
+}
+
+void RapporService::LogRappor(const RapporMetric& rappor) {
+ RapporMetricsProto::RapporMetric* metric = rappor_metrics_proto_.add_rappor();
+ metric->set_name_hash(base::Hash(rappor.parameters()->rappor_name));
+ ByteVector bytes = rappor.GetReport(rappor_secret_);
+ std::string byte_string(bytes.begin(), bytes.end());
+ metric->set_bits(byte_string);
+}
+
+void RapporService::LogRapporMetrics() {
+ base::AutoLock auto_lock(lock_);
+
+ for (RapporMap::iterator it = rappors_.begin(); rappors_.end() != it; ++it) {
+ const RapporMetric* rappor = it->second;
+ DCHECK_EQ(it->first, rappor->parameters()->rappor_name);
+ LogRappor(*rappor);
+ 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.
+ }
+ rappors_.clear();
+}
+
+void RapporService::RecordSamples(const RapporParameters* parameters,
+ const std::vector<std::string>& samples) {
+ base::AutoLock auto_lock(lock_);
+
+ RapporMetric* rappor = GetRapporMetric(parameters);
+ rappor->AddSamples(samples);
+}
+
+RapporMetric* RapporService::GetRapporMetric(
+ const RapporParameters* parameters) {
+ RapporMap::iterator it = rappors_.find(parameters->rappor_name);
+ if (rappors_.end() != it) {
+ RapporMetric* rappor = it->second;
+ DCHECK_EQ(parameters, rappor->parameters());
+ return rappor;
+ }
+
+ RapporMetric* new_rappor = new RapporMetric(parameters);
+ rappors_[parameters->rappor_name] = new_rappor;
+ return new_rappor;
+}
+
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698