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

Unified Diff: chrome/browser/metrics/variations/variations_service.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: Addressed CR comments Created 7 years, 11 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/variations_service.cc
diff --git a/chrome/browser/metrics/variations/variations_service.cc b/chrome/browser/metrics/variations/variations_service.cc
index 82b751050c7f361dc0c06ddddebff08bb3699da9..a828b4af11e27ead12d07b53ddc5b1f5d29dd46c 100644
--- a/chrome/browser/metrics/variations/variations_service.cc
+++ b/chrome/browser/metrics/variations/variations_service.cc
@@ -25,6 +25,7 @@
#include "net/base/load_flags.h"
#include "net/base/network_change_notifier.h"
#include "net/http/http_response_headers.h"
+#include "net/http/http_status_code.h"
#include "net/http/http_util.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"
@@ -41,6 +42,15 @@ const int kMaxRetrySeedFetch = 5;
// Time between seed fetches, in hours.
const int kSeedFetchPeriodHours = 5;
+// TODO(mad): To be moved to NetworkTimeService when available.
+// base::TimeTicks::Now() is documented to have a resolution of
+// ~1-15ms.
+const int64 kTicksResolutionMs = 15;
+
+// For the sources that are supported (HTTP date headers, TLS
+// handshake), the resolution of the server time is 1 second.
+const int64 kServerTimeResolutionMs = 1000;
+
// Maps Study_Channel enum values to corresponding chrome::VersionInfo::Channel
// enum values.
chrome::VersionInfo::Channel ConvertStudyChannelToVersionChannel(
@@ -187,6 +197,11 @@ void VariationsService::StartRepeatedVariationsSeedFetch() {
this, &VariationsService::FetchVariationsSeed);
}
+bool VariationsService::GetNetworkTime(base::Time* network_time,
+ base::TimeDelta* uncertainty) const {
+ return network_service_.GetNetworkTime(network_time, uncertainty);
+}
+
#if defined(OS_WIN)
void VariationsService::StartGoogleUpdateRegistrySync() {
registry_syncer_.RequestRegistrySync();
@@ -256,17 +271,26 @@ void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) {
}
// Log the response code.
+ const int response_code = request->GetResponseCode();
UMA_HISTOGRAM_CUSTOM_ENUMERATION("Variations.SeedFetchResponseCode",
- net::HttpUtil::MapStatusCodeForHistogram(request->GetResponseCode()),
+ net::HttpUtil::MapStatusCodeForHistogram(response_code),
net::HttpUtil::GetStatusCodesForHistogram());
const base::TimeDelta latency =
base::TimeTicks::Now() - last_request_started_time_;
- if (request->GetResponseCode() != 200) {
- DVLOG(1) << "Variations server request returned non-200 response code: "
- << request->GetResponseCode();
- if (request->GetResponseCode() == 304)
+ base::Time response_date;
+ if (response_code == net::HTTP_OK ||
+ response_code == net::HTTP_NOT_MODIFIED) {
+ bool success = request->GetResponseHeaders()->GetDateValue(&response_date);
+ DCHECK(success || response_date.is_null());
+
+ if (!response_date.is_null())
+ network_service_.SetNetworkTime(response_date, latency);
+ } else if (response_code != net::HTTP_OK) {
+ DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
+ << response_code;
+ if (response_code == net::HTTP_NOT_MODIFIED)
UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchNotModifiedLatency", latency);
else
UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchOtherLatency", latency);
@@ -278,10 +302,6 @@ void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) {
bool success = request->GetResponseAsString(&seed_data);
DCHECK(success);
- base::Time response_date;
- success = request->GetResponseHeaders()->GetDateValue(&response_date);
- DCHECK(success || response_date.is_null());
-
StoreSeedData(seed_data, response_date, g_browser_process->local_state());
}
@@ -565,4 +585,55 @@ void VariationsService::CreateTrialFromStudy(const Study& study,
trial->Disable();
}
+bool VariationsService::NetworkTimeService::GetNetworkTime(
+ base::Time* network_time, base::TimeDelta* uncertainty) const {
Alexei Svitkine (slow) 2013/02/01 22:18:08 Each param on a separate line.
MAD 2013/02/04 21:07:43 Done.
+ if (network_time_.is_null())
+ return false;
+ DCHECK(!network_time_ticks_.is_null());
+ DCHECK(network_time);
+ *network_time = network_time_ + (base::TimeTicks::Now() -
Alexei Svitkine (slow) 2013/02/01 22:18:08 Nit: No need for ()s.
MAD 2013/02/04 21:07:43 Actually, we get a compile error without them: er
+ network_time_ticks_);
+ if (uncertainty)
+ *uncertainty = network_time_uncertainty_;
+ return true;
+}
+
+void VariationsService::NetworkTimeService::SetNetworkTime(
+ const base::Time& network_time, const base::TimeDelta& latency) {
+#ifndef NDEBUG
+ if (!network_time_.is_null()) {
Alexei Svitkine (slow) 2013/02/01 22:18:08 Can you make a separate function for this check co
MAD 2013/02/04 21:07:43 Done.
+ // Check that we are tracking time correctly.
+ base::Time current_network_time;
+ base::TimeDelta uncertainty;
+ bool success = GetNetworkTime(&current_network_time, &uncertainty);
+ DCHECK(success);
+ // Account for network_time's own innaccuracy.
+ uncertainty +=
+ base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs) +
+ latency + 2 * base::TimeDelta::FromMilliseconds(kTicksResolutionMs);
+ DCHECK(current_network_time >= network_time - uncertainty);
+ DCHECK(current_network_time <= network_time + uncertainty);
+ DVLOG(1) << current_network_time.ToInternalValue() << " VS "
+ << network_time.ToInternalValue() << ", is within: "
+ << uncertainty.ToInternalValue();
+ }
+#endif // ifndef NDEBUG
+ // Update network time on every request to limit dependency on ticks lag.
+ // 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 server time
+ // 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_ =
+ base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs) +
+ latency + 4 * base::TimeDelta::FromMilliseconds(kTicksResolutionMs);
+
+}
+
} // namespace chrome_variations

Powered by Google App Engine
This is Rietveld 408576698