Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1641)

Side by Side Diff: net/tools/get_server_time/get_server_time.cc

Issue 292063002: Create a TrivialURLRequestContextGetter to shim an existing URLRequestContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd up to r272911 Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/domain_reliability/monitor.cc ('k') | net/url_request/url_request_context_getter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 scoped_ptr<net::URLRequestContext> url_request_context(
262 233 BuildURLRequestContext(&net_log));
234 fetcher->SetRequestContext(
235 // Since there's only a single thread, there's no need to worry
236 // about when the URLRequestContext gets created.
237 // The URLFetcher will take a reference on the object, and hence
238 // implicitly take ownership.
239 new net::TrivialURLRequestContextGetter(url_request_context.get(),
240 main_loop.message_loop_proxy()));
263 const base::Time start_time = base::Time::Now(); 241 const base::Time start_time = base::Time::Now();
264 const base::TimeTicks start_ticks = base::TimeTicks::Now(); 242 const base::TimeTicks start_ticks = base::TimeTicks::Now();
265 243
266 fetcher->Start(); 244 fetcher->Start();
267 std::printf( 245 std::printf(
268 "Request started at %s (ticks = %" PRId64 ")\n", 246 "Request started at %s (ticks = %" PRId64 ")\n",
269 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(start_time)).c_str(), 247 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(start_time)).c_str(),
270 start_ticks.ToInternalValue()); 248 start_ticks.ToInternalValue());
271 249
272 // |delegate| quits |main_loop| when the request is done. 250 // |delegate| quits |main_loop| when the request is done.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 &skew, &skew_uncertainty); 314 &skew, &skew_uncertainty);
337 315
338 std::printf( 316 std::printf(
339 "An estimate for the local clock skew is %.2f ms with " 317 "An estimate for the local clock skew is %.2f ms with "
340 "uncertainty %.2f ms\n", 318 "uncertainty %.2f ms\n",
341 skew.InMillisecondsF(), 319 skew.InMillisecondsF(),
342 skew_uncertainty.InMillisecondsF()); 320 skew_uncertainty.InMillisecondsF());
343 321
344 return EXIT_SUCCESS; 322 return EXIT_SUCCESS;
345 } 323 }
OLDNEW
« no previous file with comments | « components/domain_reliability/monitor.cc ('k') | net/url_request/url_request_context_getter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698