This source file includes following definitions.
- UpdateNetworkTime
- AddObserver
#include "net/base/network_time_notifier.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/i18n/time_formatting.h"
#include "base/location.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
namespace {
#if defined(OS_WIN)
const int64 kTicksResolutionMs = base::Time::kMinLowResolutionThresholdMs;
#else
const int64 kTicksResolutionMs = 1;
#endif
const int kNumTimeMeasurements = 5;
}
namespace net {
NetworkTimeNotifier::NetworkTimeNotifier(
scoped_ptr<base::TickClock> tick_clock) {
tick_clock_ = tick_clock.Pass();
}
NetworkTimeNotifier::~NetworkTimeNotifier() {}
void NetworkTimeNotifier::UpdateNetworkTime(const base::Time& network_time,
const base::TimeDelta& resolution,
const base::TimeDelta& latency,
const base::TimeTicks& post_time) {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "Network time updating to "
<< base::UTF16ToUTF8(
base::TimeFormatFriendlyDateAndTime(network_time));
network_time_ = network_time;
base::TimeTicks now = tick_clock_->NowTicks();
base::TimeDelta task_delay = now - post_time;
network_time_ticks_ = now - task_delay - latency / 2;
network_time_uncertainty_ =
resolution + latency + kNumTimeMeasurements *
base::TimeDelta::FromMilliseconds(kTicksResolutionMs);
for (size_t i = 0; i < observers_.size(); ++i) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(observers_[i],
network_time_,
network_time_ticks_,
network_time_uncertainty_));
}
}
void NetworkTimeNotifier::AddObserver(
const ObserverCallback& observer_callback) {
DCHECK(thread_checker_.CalledOnValidThread());
observers_.push_back(observer_callback);
if (!network_time_.is_null()) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(observer_callback,
network_time_,
network_time_ticks_,
network_time_uncertainty_));
}
}
}