| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This is a small utility that snarfs the server time from the | 5 // This is a small utility that snarfs the server time from the |
| 6 // response headers of an http/https HEAD request and compares it to | 6 // response headers of an http/https HEAD request and compares it to |
| 7 // the local time. | 7 // the local time. |
| 8 // | 8 // |
| 9 // TODO(akalin): Also snarf the server time from the TLS handshake, if | 9 // TODO(akalin): Also snarf the server time from the TLS handshake, if |
| 10 // any (http://crbug.com/146090). | 10 // any (http://crbug.com/146090). |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // | 143 // |
| 144 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. | 144 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. |
| 145 builder.set_proxy_config_service( | 145 builder.set_proxy_config_service( |
| 146 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 146 new net::ProxyConfigServiceFixed(net::ProxyConfig())); |
| 147 #endif | 147 #endif |
| 148 scoped_ptr<net::URLRequestContext> context(builder.Build()); | 148 scoped_ptr<net::URLRequestContext> context(builder.Build()); |
| 149 context->set_net_log(net_log); | 149 context->set_net_log(net_log); |
| 150 return context.Pass(); | 150 return context.Pass(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 class SingleThreadRequestContextGetter : public net::URLRequestContextGetter { | |
| 154 public: | |
| 155 // Since there's only a single thread, there's no need to worry | |
| 156 // about when |context_| gets created. | |
| 157 SingleThreadRequestContextGetter( | |
| 158 net::NetLog* net_log, | |
| 159 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) | |
| 160 : context_(BuildURLRequestContext(net_log)), | |
| 161 main_task_runner_(main_task_runner) {} | |
| 162 | |
| 163 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { | |
| 164 return context_.get(); | |
| 165 } | |
| 166 | |
| 167 virtual scoped_refptr<base::SingleThreadTaskRunner> | |
| 168 GetNetworkTaskRunner() const OVERRIDE { | |
| 169 return main_task_runner_; | |
| 170 } | |
| 171 | |
| 172 private: | |
| 173 virtual ~SingleThreadRequestContextGetter() {} | |
| 174 | |
| 175 const scoped_ptr<net::URLRequestContext> context_; | |
| 176 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
| 177 }; | |
| 178 | |
| 179 // Assuming that the time |server_time| was received from a server, | 153 // Assuming that the time |server_time| was received from a server, |
| 180 // that the request for the server was started on |start_ticks|, and | 154 // that the request for the server was started on |start_ticks|, and |
| 181 // that it ended on |end_ticks|, fills |server_now| with an estimate | 155 // that it ended on |end_ticks|, fills |server_now| with an estimate |
| 182 // of the current time and |server_now_uncertainty| with a | 156 // of the current time and |server_now_uncertainty| with a |
| 183 // conservative estimate of the uncertainty. | 157 // conservative estimate of the uncertainty. |
| 184 void EstimateServerTimeNow(base::Time server_time, | 158 void EstimateServerTimeNow(base::Time server_time, |
| 185 base::TimeTicks start_ticks, | 159 base::TimeTicks start_ticks, |
| 186 base::TimeTicks end_ticks, | 160 base::TimeTicks end_ticks, |
| 187 base::Time* server_now, | 161 base::Time* server_now, |
| 188 base::TimeDelta* server_now_uncertainty) { | 162 base::TimeDelta* server_now_uncertainty) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 // which causes the DNS resolution to abort. It's simpler to just | 218 // which causes the DNS resolution to abort. It's simpler to just |
| 245 // not instantiate one, since only a single request is sent anyway. | 219 // not instantiate one, since only a single request is sent anyway. |
| 246 | 220 |
| 247 // The declaration order for net_log and printing_log_observer is | 221 // The declaration order for net_log and printing_log_observer is |
| 248 // important. The destructor of PrintingLogObserver removes itself | 222 // important. The destructor of PrintingLogObserver removes itself |
| 249 // from net_log, so net_log must be available for entire lifetime of | 223 // from net_log, so net_log must be available for entire lifetime of |
| 250 // printing_log_observer. | 224 // printing_log_observer. |
| 251 net::NetLog net_log; | 225 net::NetLog net_log; |
| 252 PrintingLogObserver printing_log_observer; | 226 PrintingLogObserver printing_log_observer; |
| 253 net_log.AddThreadSafeObserver(&printing_log_observer, net::NetLog::LOG_ALL); | 227 net_log.AddThreadSafeObserver(&printing_log_observer, net::NetLog::LOG_ALL); |
| 254 scoped_refptr<SingleThreadRequestContextGetter> context_getter( | |
| 255 new SingleThreadRequestContextGetter(&net_log, | |
| 256 main_loop.message_loop_proxy())); | |
| 257 | 228 |
| 258 QuitDelegate delegate; | 229 QuitDelegate delegate; |
| 259 scoped_ptr<net::URLFetcher> fetcher( | 230 scoped_ptr<net::URLFetcher> fetcher( |
| 260 net::URLFetcher::Create(url, net::URLFetcher::HEAD, &delegate)); | 231 net::URLFetcher::Create(url, net::URLFetcher::HEAD, &delegate)); |
| 261 fetcher->SetRequestContext(context_getter.get()); | 232 fetcher->SetRequestContext( |
| 262 | 233 // Since there's only a single thread, there's no need to worry |
| 234 // about when the URLRequestContext gets created. |
| 235 // The URLFetcher will take a reference on the object, and hence |
| 236 // implicitly take ownership. |
| 237 new net::TrivialURLRequestContextGetter(BuildURLRequestContext(&net_log), |
| 238 main_loop.message_loop_proxy())); |
| 263 const base::Time start_time = base::Time::Now(); | 239 const base::Time start_time = base::Time::Now(); |
| 264 const base::TimeTicks start_ticks = base::TimeTicks::Now(); | 240 const base::TimeTicks start_ticks = base::TimeTicks::Now(); |
| 265 | 241 |
| 266 fetcher->Start(); | 242 fetcher->Start(); |
| 267 std::printf( | 243 std::printf( |
| 268 "Request started at %s (ticks = %" PRId64 ")\n", | 244 "Request started at %s (ticks = %" PRId64 ")\n", |
| 269 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(start_time)).c_str(), | 245 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(start_time)).c_str(), |
| 270 start_ticks.ToInternalValue()); | 246 start_ticks.ToInternalValue()); |
| 271 | 247 |
| 272 // |delegate| quits |main_loop| when the request is done. | 248 // |delegate| quits |main_loop| when the request is done. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 &skew, &skew_uncertainty); | 312 &skew, &skew_uncertainty); |
| 337 | 313 |
| 338 std::printf( | 314 std::printf( |
| 339 "An estimate for the local clock skew is %.2f ms with " | 315 "An estimate for the local clock skew is %.2f ms with " |
| 340 "uncertainty %.2f ms\n", | 316 "uncertainty %.2f ms\n", |
| 341 skew.InMillisecondsF(), | 317 skew.InMillisecondsF(), |
| 342 skew_uncertainty.InMillisecondsF()); | 318 skew_uncertainty.InMillisecondsF()); |
| 343 | 319 |
| 344 return EXIT_SUCCESS; | 320 return EXIT_SUCCESS; |
| 345 } | 321 } |
| OLD | NEW |