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

Side by Side Diff: chrome/browser/network_time/network_time_tracker.cc

Issue 291333009: network_time: Remove NetworkTimeService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/network_time/network_time_tracker.h" 5 #include "chrome/browser/network_time/network_time_tracker.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/command_line.h"
8 #include "base/i18n/time_formatting.h" 9 #include "base/i18n/time_formatting.h"
9 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/tick_clock.h" 14 #include "base/time/tick_clock.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
12 17
13 namespace { 18 namespace {
14 19
15 // Clock resolution is platform dependent. 20 // Clock resolution is platform dependent.
16 #if defined(OS_WIN) 21 #if defined(OS_WIN)
17 const int64 kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs; 22 const int64 kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs;
18 #else 23 #else
19 const int64 kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms. 24 const int64 kTicksResolutionMs = 1; // Assume 1ms for non-windows platforms.
20 #endif 25 #endif
21 26
22 // Number of time measurements performed in a given network time calculation. 27 // Number of time measurements performed in a given network time calculation.
23 const int kNumTimeMeasurements = 5; 28 const int kNumTimeMeasurements = 5;
24 29
25 } // namespace 30 } // namespace
26 31
27 NetworkTimeTracker::TimeMapping::TimeMapping(base::Time local_time, 32 // static
28 base::Time network_time) 33 void NetworkTimeTracker::RegisterPrefs(PrefRegistrySimple* registry) {
29 : local_time(local_time), 34 registry->RegisterDictionaryPref(prefs::kNetworkTimeMapping,
30 network_time(network_time) {} 35 new base::DictionaryValue());
36 }
31 37
32 NetworkTimeTracker::NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock) 38 NetworkTimeTracker::NetworkTimeTracker(scoped_ptr<base::TickClock> tick_clock,
39 PrefService* pref_service)
33 : tick_clock_(tick_clock.Pass()), 40 : tick_clock_(tick_clock.Pass()),
41 pref_service_(pref_service),
34 received_network_time_(false) { 42 received_network_time_(false) {
43 if (CommandLine::ForCurrentProcess()->HasSwitch(
44 switches::kEnableNetworkTime)) {
haitaol1 2014/05/27 23:09:31 This switch is probably not needed. You can turn o
hashimoto 2014/05/28 11:25:52 Done.
45 const base::DictionaryValue* time_mapping =
46 pref_service_->GetDictionary(prefs::kNetworkTimeMapping);
47 double local_time_js = 0;
48 double network_time_js = 0;
49 if (time_mapping->GetDouble("local", &local_time_js) &&
50 time_mapping->GetDouble("network", &network_time_js)) {
51 base::Time local_time_saved = base::Time::FromJsTime(local_time_js);
52 base::Time local_time_now = base::Time::Now();
53 if (local_time_saved > local_time_now ||
54 local_time_now - local_time_saved > base::TimeDelta::FromDays(7)) {
55 // Drop saved mapping if clock skew has changed or the data is too old.
56 pref_service_->ClearPref(prefs::kNetworkTimeMapping);
57 } else {
58 network_time_ = base::Time::FromJsTime(network_time_js) +
59 (local_time_now - local_time_saved);
60 network_time_ticks_ = base::TimeTicks::Now();
61 }
62 }
63 }
35 } 64 }
36 65
37 NetworkTimeTracker::~NetworkTimeTracker() { 66 NetworkTimeTracker::~NetworkTimeTracker() {
38 DCHECK(thread_checker_.CalledOnValidThread()); 67 DCHECK(thread_checker_.CalledOnValidThread());
39 } 68 }
40 69
41 void NetworkTimeTracker::InitFromSavedTime(const TimeMapping& saved) {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 if (!network_time_.is_null() || saved.local_time.is_null() ||
44 saved.network_time.is_null())
45 return;
46
47 base::Time local_time_now = base::Time::Now();
48 if (local_time_now < saved.local_time) {
49 DLOG(WARNING) << "Can't initialize because clock skew has changed.";
50 return;
51 }
52
53 network_time_ = saved.network_time + (local_time_now - saved.local_time);
54 network_time_ticks_ = base::TimeTicks::Now();
55 }
56
57 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time, 70 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time,
58 base::TimeDelta resolution, 71 base::TimeDelta resolution,
59 base::TimeDelta latency, 72 base::TimeDelta latency,
60 base::TimeTicks post_time) { 73 base::TimeTicks post_time) {
61 DCHECK(thread_checker_.CalledOnValidThread()); 74 DCHECK(thread_checker_.CalledOnValidThread());
62 DVLOG(1) << "Network time updating to " 75 DVLOG(1) << "Network time updating to "
63 << base::UTF16ToUTF8( 76 << base::UTF16ToUTF8(
64 base::TimeFormatFriendlyDateAndTime(network_time)); 77 base::TimeFormatFriendlyDateAndTime(network_time));
65 // Update network time on every request to limit dependency on ticks lag. 78 // Update network time on every request to limit dependency on ticks lag.
66 // TODO(mad): Find a heuristic to avoid augmenting the 79 // TODO(mad): Find a heuristic to avoid augmenting the
(...skipping 11 matching lines...) Expand all
78 // Can't assume a better time than the resolution of the given time 91 // Can't assume a better time than the resolution of the given time
79 // and 5 ticks measurements are involved, each with their own uncertainty. 92 // and 5 ticks measurements are involved, each with their own uncertainty.
80 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when 93 // 1 & 2 are the ones used to compute the latency, 3 is the Now() from when
81 // this task was posted, 4 is the Now() above and 5 will be the Now() used in 94 // this task was posted, 4 is the Now() above and 5 will be the Now() used in
82 // GetNetworkTime(). 95 // GetNetworkTime().
83 network_time_uncertainty_ = 96 network_time_uncertainty_ =
84 resolution + latency + kNumTimeMeasurements * 97 resolution + latency + kNumTimeMeasurements *
85 base::TimeDelta::FromMilliseconds(kTicksResolutionMs); 98 base::TimeDelta::FromMilliseconds(kTicksResolutionMs);
86 99
87 received_network_time_ = true; 100 received_network_time_ = true;
101
102 base::Time network_time_now;
103 if (CommandLine::ForCurrentProcess()->HasSwitch(
haitaol1 2014/05/27 23:09:31 same here.
hashimoto 2014/05/28 11:25:52 Done.
104 switches::kEnableNetworkTime) &&
105 GetNetworkTime(base::TimeTicks::Now(), &network_time_now, NULL)) {
106 // Update time mapping if tracker received time update from server, i.e.
107 // mapping is accurate.
108 base::Time local_now = base::Time::Now();
109 base::DictionaryValue time_mapping;
110 time_mapping.SetDouble("local", local_now.ToJsTime());
111 time_mapping.SetDouble("network", network_time_now.ToJsTime());
112 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping);
113 }
88 } 114 }
89 115
90 bool NetworkTimeTracker::GetNetworkTime(base::TimeTicks time_ticks, 116 bool NetworkTimeTracker::GetNetworkTime(base::TimeTicks time_ticks,
91 base::Time* network_time, 117 base::Time* network_time,
92 base::TimeDelta* uncertainty) const { 118 base::TimeDelta* uncertainty) const {
93 DCHECK(thread_checker_.CalledOnValidThread()); 119 DCHECK(thread_checker_.CalledOnValidThread());
94 DCHECK(network_time); 120 DCHECK(network_time);
95 if (network_time_.is_null()) 121 if (network_time_.is_null())
96 return false; 122 return false;
97 DCHECK(!network_time_ticks_.is_null()); 123 DCHECK(!network_time_ticks_.is_null());
98 *network_time = network_time_ + (time_ticks - network_time_ticks_); 124 *network_time = network_time_ + (time_ticks - network_time_ticks_);
99 if (uncertainty) 125 if (uncertainty)
100 *uncertainty = network_time_uncertainty_; 126 *uncertainty = network_time_uncertainty_;
101 return true; 127 return true;
102 } 128 }
OLDNEW
« no previous file with comments | « chrome/browser/network_time/network_time_tracker.h ('k') | chrome/browser/network_time/network_time_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698