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

Unified Diff: components/metrics/daily_interval.cc

Issue 511623002: Add a mechanism for collecting Rappor samples on a daily interval. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: components/metrics/daily_interval.cc
diff --git a/components/metrics/daily_interval.cc b/components/metrics/daily_interval.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8bb5d6f6bf911afb3acbb0cc012c220629c84215
--- /dev/null
+++ b/components/metrics/daily_interval.cc
@@ -0,0 +1,66 @@
+// Copyright 2014 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/metrics/daily_interval.h"
+
+#include "base/i18n/time_formatting.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+
+namespace metrics {
+
+DailyInterval::DailyInterval(PrefService* pref_service, const char* pref_name)
+ : pref_service_(pref_service), pref_name_(pref_name) {
+}
+
+DailyInterval::~DailyInterval() {
+}
+
+// static
+void DailyInterval::RegisterPref(PrefRegistrySimple* registry,
+ const char* pref_name) {
+ registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue());
+}
+
+void DailyInterval::AddObserver(DailyObserver* observer) {
+ DVLOG(2) << "DailyInterval observer added.";
+ DCHECK(last_fired_.is_null());
+ observers_.AddObserver(observer);
+}
+
+void DailyInterval::CheckInterval() {
+ base::Time now = base::Time::Now();
+ if (last_fired_.is_null()) {
+ // The first time we call CheckInterval, we read the time stored in prefs.
+ last_fired_ = base::Time::FromInternalValue(
+ pref_service_->GetInt64(pref_name_));
+ DVLOG(1) << "DailyInterval time loaded: "
+ << base::TimeFormatShortDateAndTime(last_fired_);
+ if (last_fired_.is_null()) {
+ DVLOG(1) << "DailyInterval first run.";
+ OnInterval(now);
+ return;
+ }
+ }
+ int days_elapsed = (now - last_fired_).InDays();
+ if (days_elapsed > 1) {
+ DVLOG(1) << "DailyInterval day elapsed.";
+ OnInterval(now);
+ } else if (days_elapsed <= -1) {
+ // The "last fired" time is more than a day in the future, so the clock
+ // must have been changed.
+ DVLOG(1) << "DailyInterval clock change detected.";
jwd 2014/08/29 15:24:15 It might be nice to log an uma metric here, to see
Steven Holte 2014/09/03 19:33:44 Done.
+ OnInterval(now);
+ }
+}
+
+void DailyInterval::OnInterval(base::Time now) {
+ DCHECK(!now.is_null());
+ last_fired_ = now;
+ pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue());
+ FOR_EACH_OBSERVER(
+ DailyObserver, observers_, OnDailyInterval());
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698