| OLD | NEW |
| 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_service.h" | 5 #include "chrome/browser/network_time/network_time_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/network_time/network_time_tracker.h" | 10 #include "chrome/browser/network_time/network_time_tracker.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 #include "components/user_prefs/pref_registry_syncable.h" | 14 #include "components/user_prefs/pref_registry_syncable.h" |
| 14 | 15 |
| 15 // static | 16 // static |
| 16 void NetworkTimeService::RegisterProfilePrefs( | 17 void NetworkTimeService::RegisterProfilePrefs( |
| 17 user_prefs::PrefRegistrySyncable* registry) { | 18 user_prefs::PrefRegistrySyncable* registry) { |
| 18 registry->RegisterDictionaryPref( | 19 registry->RegisterDictionaryPref( |
| 19 prefs::kNetworkTimeMapping, | 20 prefs::kNetworkTimeMapping, |
| 20 new base::DictionaryValue(), | 21 new base::DictionaryValue(), |
| 21 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 22 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 22 } | 23 } |
| 23 | 24 |
| 24 NetworkTimeService::NetworkTimeService(Profile* profile) | 25 NetworkTimeService::NetworkTimeService(Profile* profile) |
| 25 : profile_(profile) { | 26 : profile_(profile) { |
| 26 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 27 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 27 switches::kEnableNetworkTime)) { | 28 switches::kEnableNetworkTime)) { |
| 28 return; | 29 return; |
| 29 } | 30 } |
| 30 | 31 |
| 31 network_time_tracker_.reset(new NetworkTimeTracker); | |
| 32 network_time_tracker_->Start(); | |
| 33 | |
| 34 const base::DictionaryValue* time_mapping = | 32 const base::DictionaryValue* time_mapping = |
| 35 profile_->GetPrefs()->GetDictionary(prefs::kNetworkTimeMapping); | 33 profile_->GetPrefs()->GetDictionary(prefs::kNetworkTimeMapping); |
| 36 double local_time_js; | 34 double local_time_js; |
| 37 double network_time_js; | 35 double network_time_js; |
| 38 if (time_mapping->GetDouble("local", &local_time_js) && | 36 if (time_mapping->GetDouble("local", &local_time_js) && |
| 39 time_mapping->GetDouble("network", &network_time_js)) { | 37 time_mapping->GetDouble("network", &network_time_js)) { |
| 40 base::Time local_time_saved = base::Time::FromJsTime(local_time_js); | 38 base::Time local_time_saved = base::Time::FromJsTime(local_time_js); |
| 41 if (local_time_saved > base::Time::Now() || | 39 if (local_time_saved > base::Time::Now() || |
| 42 base::Time::Now() - local_time_saved > base::TimeDelta::FromDays(7)) { | 40 base::Time::Now() - local_time_saved > base::TimeDelta::FromDays(7)) { |
| 43 // Drop saved mapping if clock skew has changed or the data is too old. | 41 // Drop saved mapping if clock skew has changed or the data is too old. |
| 44 profile_->GetPrefs()->ClearPref(prefs::kNetworkTimeMapping); | 42 profile_->GetPrefs()->ClearPref(prefs::kNetworkTimeMapping); |
| 45 } else { | 43 } else { |
| 46 network_time_tracker_->InitFromSavedTime( | 44 g_browser_process->network_time_tracker()->InitFromSavedTime( |
| 47 NetworkTimeTracker::TimeMapping( | 45 NetworkTimeTracker::TimeMapping( |
| 48 local_time_saved, base::Time::FromJsTime(network_time_js))); | 46 local_time_saved, base::Time::FromJsTime(network_time_js))); |
| 49 } | 47 } |
| 50 } | 48 } |
| 51 } | 49 } |
| 52 | 50 |
| 53 NetworkTimeService::~NetworkTimeService() {} | 51 NetworkTimeService::~NetworkTimeService() {} |
| 54 | 52 |
| 55 void NetworkTimeService::Shutdown() { | 53 void NetworkTimeService::Shutdown() { |
| 56 if (!network_time_tracker_) | 54 if (g_browser_process->network_time_tracker()->received_network_time()) { |
| 57 return; | |
| 58 | |
| 59 if (network_time_tracker_->received_network_time()) { | |
| 60 // Update time mapping if tracker received time update from server, i.e. | 55 // Update time mapping if tracker received time update from server, i.e. |
| 61 // mapping is accurate. | 56 // mapping is accurate. |
| 62 base::Time local_now = base::Time::Now(); | 57 base::Time local_now = base::Time::Now(); |
| 63 base::Time network_now = GetNetworkTime(local_now); | 58 base::Time network_now = GetNetworkTime(local_now); |
| 64 base::DictionaryValue time_mapping; | 59 base::DictionaryValue time_mapping; |
| 65 time_mapping.SetDouble("local", local_now.ToJsTime()); | 60 time_mapping.SetDouble("local", local_now.ToJsTime()); |
| 66 time_mapping.SetDouble("network", network_now.ToJsTime()); | 61 time_mapping.SetDouble("network", network_now.ToJsTime()); |
| 67 profile_->GetPrefs()->Set(prefs::kNetworkTimeMapping, time_mapping); | 62 profile_->GetPrefs()->Set(prefs::kNetworkTimeMapping, time_mapping); |
| 68 } | 63 } |
| 69 } | 64 } |
| 70 | 65 |
| 71 base::Time NetworkTimeService::GetNetworkTime( | 66 base::Time NetworkTimeService::GetNetworkTime( |
| 72 const base::Time& local_time) { | 67 const base::Time& local_time) { |
| 73 if (!network_time_tracker_.get() || local_time.is_null() || | 68 if (local_time.is_null() || local_time.is_max()) |
| 74 local_time.is_max()) | |
| 75 return local_time; | 69 return local_time; |
| 76 | 70 |
| 77 base::Time network_time_now; | 71 base::Time network_time_now; |
| 78 if (!network_time_tracker_->GetNetworkTime(base::TimeTicks::Now(), | 72 if (!g_browser_process->network_time_tracker()->GetNetworkTime( |
| 79 &network_time_now, NULL)) { | 73 base::TimeTicks::Now(), &network_time_now, NULL)) { |
| 80 return local_time; | 74 return local_time; |
| 81 } | 75 } |
| 82 return local_time + (network_time_now - base::Time::Now()); | 76 return local_time + (network_time_now - base::Time::Now()); |
| 83 } | 77 } |
| 84 | 78 |
| 85 base::Time NetworkTimeService::GetNetworkNow() { | 79 base::Time NetworkTimeService::GetNetworkNow() { |
| 86 return GetNetworkTime(base::Time::Now()); | 80 return GetNetworkTime(base::Time::Now()); |
| 87 } | 81 } |
| OLD | NEW |