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). |
11 | 11 |
12 #include <cstdio> | 12 #include <cstdio> |
13 #include <cstdlib> | 13 #include <cstdlib> |
| 14 #include <memory> |
14 #include <string> | 15 #include <string> |
15 | 16 |
16 #include "base/at_exit.h" | 17 #include "base/at_exit.h" |
17 #include "base/command_line.h" | 18 #include "base/command_line.h" |
18 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
19 #include "base/format_macros.h" | 20 #include "base/format_macros.h" |
20 #include "base/i18n/time_formatting.h" | 21 #include "base/i18n/time_formatting.h" |
21 #include "base/json/json_writer.h" | 22 #include "base/json/json_writer.h" |
22 #include "base/logging.h" | 23 #include "base/logging.h" |
23 #include "base/macros.h" | 24 #include "base/macros.h" |
| 25 #include "base/memory/ptr_util.h" |
24 #include "base/memory/ref_counted.h" | 26 #include "base/memory/ref_counted.h" |
25 #include "base/memory/scoped_ptr.h" | |
26 #include "base/message_loop/message_loop.h" | 27 #include "base/message_loop/message_loop.h" |
27 #include "base/single_thread_task_runner.h" | 28 #include "base/single_thread_task_runner.h" |
28 #include "base/strings/string_number_conversions.h" | 29 #include "base/strings/string_number_conversions.h" |
29 #include "base/strings/utf_string_conversions.h" | 30 #include "base/strings/utf_string_conversions.h" |
30 #include "base/time/time.h" | 31 #include "base/time/time.h" |
31 #include "base/values.h" | 32 #include "base/values.h" |
32 #include "build/build_config.h" | 33 #include "build/build_config.h" |
33 #include "net/base/net_errors.h" | 34 #include "net/base/net_errors.h" |
34 #include "net/http/http_response_headers.h" | 35 #include "net/http/http_response_headers.h" |
35 #include "net/log/net_log.h" | 36 #include "net/log/net_log.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // to VLOG(1). | 113 // to VLOG(1). |
113 if (!VLOG_IS_ON(1)) | 114 if (!VLOG_IS_ON(1)) |
114 return; | 115 return; |
115 | 116 |
116 const char* const source_type = | 117 const char* const source_type = |
117 net::NetLog::SourceTypeToString(entry.source().type); | 118 net::NetLog::SourceTypeToString(entry.source().type); |
118 const char* const event_type = | 119 const char* const event_type = |
119 net::NetLog::EventTypeToString(entry.type()); | 120 net::NetLog::EventTypeToString(entry.type()); |
120 const char* const event_phase = | 121 const char* const event_phase = |
121 net::NetLog::EventPhaseToString(entry.phase()); | 122 net::NetLog::EventPhaseToString(entry.phase()); |
122 scoped_ptr<base::Value> params(entry.ParametersToValue()); | 123 std::unique_ptr<base::Value> params(entry.ParametersToValue()); |
123 std::string params_str; | 124 std::string params_str; |
124 if (params.get()) { | 125 if (params.get()) { |
125 base::JSONWriter::Write(*params, ¶ms_str); | 126 base::JSONWriter::Write(*params, ¶ms_str); |
126 params_str.insert(0, ": "); | 127 params_str.insert(0, ": "); |
127 } | 128 } |
128 | 129 |
129 VLOG(1) << source_type << "(" << entry.source().id << "): " | 130 VLOG(1) << source_type << "(" << entry.source().id << "): " |
130 << event_type << ": " << event_phase << params_str; | 131 << event_type << ": " << event_phase << params_str; |
131 } | 132 } |
132 | 133 |
133 private: | 134 private: |
134 DISALLOW_COPY_AND_ASSIGN(PrintingLogObserver); | 135 DISALLOW_COPY_AND_ASSIGN(PrintingLogObserver); |
135 }; | 136 }; |
136 | 137 |
137 // Builds a URLRequestContext assuming there's only a single loop. | 138 // Builds a URLRequestContext assuming there's only a single loop. |
138 scoped_ptr<net::URLRequestContext> | 139 std::unique_ptr<net::URLRequestContext> BuildURLRequestContext( |
139 BuildURLRequestContext(net::NetLog* net_log) { | 140 net::NetLog* net_log) { |
140 net::URLRequestContextBuilder builder; | 141 net::URLRequestContextBuilder builder; |
141 #if defined(OS_LINUX) | 142 #if defined(OS_LINUX) |
142 // On Linux, use a fixed ProxyConfigService, since the default one | 143 // On Linux, use a fixed ProxyConfigService, since the default one |
143 // depends on glib. | 144 // depends on glib. |
144 // | 145 // |
145 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. | 146 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. |
146 builder.set_proxy_config_service( | 147 builder.set_proxy_config_service( |
147 make_scoped_ptr(new net::ProxyConfigServiceFixed(net::ProxyConfig()))); | 148 base::WrapUnique(new net::ProxyConfigServiceFixed(net::ProxyConfig()))); |
148 #endif | 149 #endif |
149 scoped_ptr<net::URLRequestContext> context(builder.Build()); | 150 std::unique_ptr<net::URLRequestContext> context(builder.Build()); |
150 context->set_net_log(net_log); | 151 context->set_net_log(net_log); |
151 return context; | 152 return context; |
152 } | 153 } |
153 | 154 |
154 // Assuming that the time |server_time| was received from a server, | 155 // Assuming that the time |server_time| was received from a server, |
155 // that the request for the server was started on |start_ticks|, and | 156 // that the request for the server was started on |start_ticks|, and |
156 // that it ended on |end_ticks|, fills |server_now| with an estimate | 157 // that it ended on |end_ticks|, fills |server_now| with an estimate |
157 // of the current time and |server_now_uncertainty| with a | 158 // of the current time and |server_now_uncertainty| with a |
158 // conservative estimate of the uncertainty. | 159 // conservative estimate of the uncertainty. |
159 void EstimateServerTimeNow(base::Time server_time, | 160 void EstimateServerTimeNow(base::Time server_time, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // The declaration order for net_log and printing_log_observer is | 224 // The declaration order for net_log and printing_log_observer is |
224 // important. The destructor of PrintingLogObserver removes itself | 225 // important. The destructor of PrintingLogObserver removes itself |
225 // from net_log, so net_log must be available for entire lifetime of | 226 // from net_log, so net_log must be available for entire lifetime of |
226 // printing_log_observer. | 227 // printing_log_observer. |
227 net::NetLog net_log; | 228 net::NetLog net_log; |
228 PrintingLogObserver printing_log_observer; | 229 PrintingLogObserver printing_log_observer; |
229 net_log.DeprecatedAddObserver(&printing_log_observer, | 230 net_log.DeprecatedAddObserver(&printing_log_observer, |
230 net::NetLogCaptureMode::IncludeSocketBytes()); | 231 net::NetLogCaptureMode::IncludeSocketBytes()); |
231 | 232 |
232 QuitDelegate delegate; | 233 QuitDelegate delegate; |
233 scoped_ptr<net::URLFetcher> fetcher = | 234 std::unique_ptr<net::URLFetcher> fetcher = |
234 net::URLFetcher::Create(url, net::URLFetcher::HEAD, &delegate); | 235 net::URLFetcher::Create(url, net::URLFetcher::HEAD, &delegate); |
235 scoped_ptr<net::URLRequestContext> url_request_context( | 236 std::unique_ptr<net::URLRequestContext> url_request_context( |
236 BuildURLRequestContext(&net_log)); | 237 BuildURLRequestContext(&net_log)); |
237 fetcher->SetRequestContext( | 238 fetcher->SetRequestContext( |
238 // Since there's only a single thread, there's no need to worry | 239 // Since there's only a single thread, there's no need to worry |
239 // about when the URLRequestContext gets created. | 240 // about when the URLRequestContext gets created. |
240 // The URLFetcher will take a reference on the object, and hence | 241 // The URLFetcher will take a reference on the object, and hence |
241 // implicitly take ownership. | 242 // implicitly take ownership. |
242 new net::TrivialURLRequestContextGetter(url_request_context.get(), | 243 new net::TrivialURLRequestContextGetter(url_request_context.get(), |
243 main_loop.task_runner())); | 244 main_loop.task_runner())); |
244 const base::Time start_time = base::Time::Now(); | 245 const base::Time start_time = base::Time::Now(); |
245 const base::TimeTicks start_ticks = base::TimeTicks::Now(); | 246 const base::TimeTicks start_ticks = base::TimeTicks::Now(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 &skew, &skew_uncertainty); | 318 &skew, &skew_uncertainty); |
318 | 319 |
319 std::printf( | 320 std::printf( |
320 "An estimate for the local clock skew is %.2f ms with " | 321 "An estimate for the local clock skew is %.2f ms with " |
321 "uncertainty %.2f ms\n", | 322 "uncertainty %.2f ms\n", |
322 skew.InMillisecondsF(), | 323 skew.InMillisecondsF(), |
323 skew_uncertainty.InMillisecondsF()); | 324 skew_uncertainty.InMillisecondsF()); |
324 | 325 |
325 return EXIT_SUCCESS; | 326 return EXIT_SUCCESS; |
326 } | 327 } |
OLD | NEW |