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

Unified Diff: chrome/browser/metrics/variations/network_time_tracker.cc

Issue 12096096: Give access to a network time kept in the variation service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 2nd round of CR comments, new file and tests. Created 7 years, 10 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: chrome/browser/metrics/variations/network_time_tracker.cc
diff --git a/chrome/browser/metrics/variations/network_time_tracker.cc b/chrome/browser/metrics/variations/network_time_tracker.cc
new file mode 100644
index 0000000000000000000000000000000000000000..76830099d25978953787e0bb2b1b3033d2d466a1
--- /dev/null
+++ b/chrome/browser/metrics/variations/network_time_tracker.cc
@@ -0,0 +1,79 @@
+// 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 "chrome/browser/metrics/variations/network_time_tracker.h"
+
+#include "base/logging.h"
+
+namespace {
+
+// base::TimeTicks::Now() is documented to have a resolution of ~1-15ms.
+const int64 kTicksResolutionMs = 15;
+
+#ifndef NDEBUG
+// Checks that time is tracked correctly.
+bool ValidateTimeTracking(const NetworkTimeTracker& network_time_tracker,
Alexei Svitkine (slow) 2013/02/04 21:23:18 If you're passing in the NetworkTimeTracker object
MAD 2013/02/05 01:05:55 D'Ho! This evolved from something else... Makes se
+ const base::Time& new_network_time,
+ const base::TimeDelta& resolution,
+ const base::TimeDelta& latency) {
+ base::Time current_network_time;
+ base::TimeDelta uncertainty;
+ if (network_time_tracker.GetNetworkTime(&current_network_time,
+ &uncertainty)) {
+ // Account for new_network_time's own innaccuracy.
+ uncertainty +=
+ resolution + latency +
+ 2 * base::TimeDelta::FromMilliseconds(kTicksResolutionMs);
+ DVLOG(1) << current_network_time.ToInternalValue() << " VS "
+ << new_network_time.ToInternalValue() << ", should be within: "
+ << uncertainty.ToInternalValue();
+ return abs(current_network_time.ToInternalValue() -
+ new_network_time.ToInternalValue()) <=
+ uncertainty.ToInternalValue();
+ }
+ return true; // No time to track, so no tracking drift.
+}
+#endif // ifndef NDEBUG
+
+} // namespace
+
+NetworkTimeTracker::NetworkTimeTracker() {
+}
+
+NetworkTimeTracker::~NetworkTimeTracker() {
+}
+
+bool NetworkTimeTracker::GetNetworkTime(base::Time* network_time,
+ base::TimeDelta* uncertainty) const {
+ if (network_time_.is_null())
+ return false;
+ DCHECK(!network_time_ticks_.is_null());
+ DCHECK(network_time);
+ *network_time = network_time_ + (base::TimeTicks::Now() -
+ network_time_ticks_);
+ if (uncertainty)
+ *uncertainty = network_time_uncertainty_;
+ return true;
+}
+
+void NetworkTimeTracker::UpdateNetworkTime(const base::Time& network_time,
+ const base::TimeDelta& resolution,
+ const base::TimeDelta& latency) {
+ DCHECK(ValidateTimeTracking(*this, network_time, resolution, latency));
+ // Update network time on every request to limit dependency on ticks lag.
Alexei Svitkine (slow) 2013/02/04 21:23:18 Nit: add a blank line before this to make it more
MAD 2013/02/05 01:05:55 Done.
+ // TODO(mad): Find a heuristic to avoid augmenting the
+ // network_time_uncertainty_ too much by a particularly long latency.
+ // Maybe only update when we either improve accuracy or drifted too far
+ // from |network_time|.
+ network_time_ = network_time;
+ // Estimate that the time was set midway through the latency time.
+ network_time_ticks_ = base::TimeTicks::Now() - latency / 2;
+ // We can't assume a better time than the resolution of the given time
Alexei Svitkine (slow) 2013/02/04 21:23:18 Nit: add a blank line before this to make it more
MAD 2013/02/05 01:05:55 Done.
+ // and we involve 4 ticks value, each with their own uncertainty, 1 & 2
+ // are the ones used to compute the latency, 3 is the Now() above and 4
+ // will be the Now() used in GetNetworkTime().
+ network_time_uncertainty_ =
+ resolution + latency +
+ 4 * base::TimeDelta::FromMilliseconds(kTicksResolutionMs);
+}

Powered by Google App Engine
This is Rietveld 408576698