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