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

Unified 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 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..ba115c642b98ffc9ec12679c76f426825a0ce658
--- /dev/null
+++ b/components/rappor/rappor_service.cc
@@ -0,0 +1,109 @@
+// 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.h"
+#include "components/rappor/rappor_pref_names.h"
+#include "components/rappor/rappor_recorder.h"
+#include "components/rappor/rappor_reporter.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();
+ RecordRapporMetrics();
+ if (rappor_metrics_proto_.rappor_size() > 0) {
+ std::string log_text;
+ rappor_metrics_proto_.SerializeToString(&log_text);
+ 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::RecordRapporMetric(const std::string& metric_name,
+ const std::vector<uint8_t> bytes) {
+ RapporMetricsProto::RapporMetricProto* metric =
+ rappor_metrics_proto_.add_rappor();
+ metric->set_name_hash(base::Hash(metric_name));
+ std::string byte_string(bytes.begin(), bytes.end());
+ metric->set_bits(byte_string);
+}
+
+void RapporService::RecordRapporMetrics() {
+ RapporReporter rappor_reporter(rappor_secret_);
+
+ RapporRecorder::Rappors rappors;
+ g_rappor_recorder.Get().GetRappors(&rappors);
+ for (RapporRecorder::Rappors::const_iterator it = rappors.begin();
+ rappors.end() != it;
+ ++it) {
+ const Rappor& rappor = **it;
+ std::vector<uint8_t> bytes = rappor_reporter.GetReport(rappor);
+ RecordRapporMetric(rappor.rappor_name(), bytes);
+ }
+ g_rappor_recorder.Get().ClearRappors();
+}
+
+} // namespace rappor

Powered by Google App Engine
This is Rietveld 408576698