1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/network_time_tracker.h"
#include "base/sequenced_task_runner.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/io_thread.h"
namespace {
// Helper functions for interacting with the NetworkTimeNotifier.
// Registration happens as follows (assuming tracker lives on thread N):
// | Thread N | | UI thread| | IO Thread |
// Start
// RegisterObserverOnUIThread
// RegisterObserverOnIOThread
// NetworkTimeNotifier::AddObserver
// after which updates to the notifier and the subsequent observer calls
// happen as follows (assuming the network time update comes from the same
// thread):
// | Thread N | | UI thread| | IO Thread |
// UpdateNetworkNotifier
// UpdateNetworkNotifierOnIOThread
// NetworkTimeNotifier::UpdateNetworkTime
// OnNetworkTimeUpdatedOnIOThread
// OnNetworkTimeUpdated
void RegisterObserverOnIOThread(
IOThread* io_thread,
const net::NetworkTimeNotifier::ObserverCallback& observer_callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
io_thread->globals()->network_time_notifier->AddObserver(observer_callback);
}
void RegisterObserverOnUIThread(
const net::NetworkTimeNotifier::ObserverCallback& observer_callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&RegisterObserverOnIOThread,
g_browser_process->io_thread(),
observer_callback));
}
void UpdateNetworkNotifierOnIOThread(IOThread* io_thread,
const base::Time& network_time,
const base::TimeDelta& resolution,
const base::TimeDelta& latency,
const base::TimeTicks& post_time) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
io_thread->globals()->network_time_notifier->UpdateNetworkTime(
network_time, resolution, latency, post_time);
}
void UpdateNetworkNotifier(IOThread* io_thread,
const base::Time& network_time,
const base::TimeDelta& resolution,
const base::TimeDelta& latency) {
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(&UpdateNetworkNotifierOnIOThread,
io_thread,
network_time,
resolution,
latency,
base::TimeTicks::Now()));
}
void OnNetworkTimeUpdatedOnIOThread(
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
const net::NetworkTimeNotifier::ObserverCallback& observer_callback,
const base::Time& network_time,
const base::TimeTicks& network_time_ticks,
const base::TimeDelta& network_time_uncertainty) {
task_runner->PostTask(
FROM_HERE,
base::Bind(observer_callback,
network_time,
network_time_ticks,
network_time_uncertainty));
}
} // namespace
NetworkTimeTracker::NetworkTimeTracker()
: weak_ptr_factory_(this) {
}
NetworkTimeTracker::~NetworkTimeTracker() {
}
void NetworkTimeTracker::Start() {
DCHECK(thread_checker_.CalledOnValidThread());
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&RegisterObserverOnUIThread,
BuildObserverCallback()));
}
bool NetworkTimeTracker::GetNetworkTime(const base::TimeTicks& time_ticks,
base::Time* network_time,
base::TimeDelta* uncertainty) const {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(network_time);
if (network_time_.is_null())
return false;
DCHECK(!network_time_ticks_.is_null());
*network_time = network_time_ + (time_ticks - network_time_ticks_);
if (uncertainty)
*uncertainty = network_time_uncertainty_;
return true;
}
// static
// Note: UpdateNetworkNotifier is exposed via callback because getting the IO
// thread pointer must be done on the UI thread, while components that provide
// network time updates may live on other threads.
NetworkTimeTracker::UpdateCallback
NetworkTimeTracker::BuildNotifierUpdateCallback() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
return base::Bind(&UpdateNetworkNotifier,
g_browser_process->io_thread());
}
net::NetworkTimeNotifier::ObserverCallback
NetworkTimeTracker::BuildObserverCallback() {
return base::Bind(&OnNetworkTimeUpdatedOnIOThread,
MessageLoop::current()->message_loop_proxy(),
base::Bind(&NetworkTimeTracker::OnNetworkTimeUpdate,
weak_ptr_factory_.GetWeakPtr()));
}
void NetworkTimeTracker::OnNetworkTimeUpdate(
const base::Time& network_time,
const base::TimeTicks& network_time_ticks,
const base::TimeDelta& network_time_uncertainty) {
DCHECK(thread_checker_.CalledOnValidThread());
network_time_ = network_time;
network_time_ticks_ = network_time_ticks;
network_time_uncertainty_ = network_time_uncertainty;
}
|