| 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 <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/at_exit.h" | 16 #include "base/at_exit.h" |
| 17 #include "base/basictypes.h" | |
| 18 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 19 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 20 #include "base/format_macros.h" | 19 #include "base/format_macros.h" |
| 21 #include "base/i18n/time_formatting.h" | 20 #include "base/i18n/time_formatting.h" |
| 22 #include "base/json/json_writer.h" | 21 #include "base/json/json_writer.h" |
| 23 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "base/macros.h" |
| 24 #include "base/memory/ref_counted.h" | 24 #include "base/memory/ref_counted.h" |
| 25 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
| 26 #include "base/message_loop/message_loop.h" | 26 #include "base/message_loop/message_loop.h" |
| 27 #include "base/single_thread_task_runner.h" | 27 #include "base/single_thread_task_runner.h" |
| 28 #include "base/strings/string_number_conversions.h" | 28 #include "base/strings/string_number_conversions.h" |
| 29 #include "base/strings/utf_string_conversions.h" | 29 #include "base/strings/utf_string_conversions.h" |
| 30 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 31 #include "base/values.h" | 31 #include "base/values.h" |
| 32 #include "build/build_config.h" | 32 #include "build/build_config.h" |
| 33 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 47 #include "net/proxy/proxy_config.h" | 47 #include "net/proxy/proxy_config.h" |
| 48 #include "net/proxy/proxy_config_service_fixed.h" | 48 #include "net/proxy/proxy_config_service_fixed.h" |
| 49 #endif | 49 #endif |
| 50 | 50 |
| 51 using base::UTF16ToUTF8; | 51 using base::UTF16ToUTF8; |
| 52 | 52 |
| 53 namespace { | 53 namespace { |
| 54 | 54 |
| 55 // base::TimeTicks::Now() is documented to have a resolution of | 55 // base::TimeTicks::Now() is documented to have a resolution of |
| 56 // ~1-15ms. | 56 // ~1-15ms. |
| 57 const int64 kTicksResolutionMs = 15; | 57 const int64_t kTicksResolutionMs = 15; |
| 58 | 58 |
| 59 // For the sources that are supported (HTTP date headers, TLS | 59 // For the sources that are supported (HTTP date headers, TLS |
| 60 // handshake), the resolution of the server time is 1 second. | 60 // handshake), the resolution of the server time is 1 second. |
| 61 const int64 kServerTimeResolutionMs = 1000; | 61 const int64_t kServerTimeResolutionMs = 1000; |
| 62 | 62 |
| 63 // Assume base::Time::Now() has the same resolution as | 63 // Assume base::Time::Now() has the same resolution as |
| 64 // base::TimeTicks::Now(). | 64 // base::TimeTicks::Now(). |
| 65 // | 65 // |
| 66 // TODO(akalin): Figure out the real resolution. | 66 // TODO(akalin): Figure out the real resolution. |
| 67 const int64 kTimeResolutionMs = kTicksResolutionMs; | 67 const int64_t kTimeResolutionMs = kTicksResolutionMs; |
| 68 | 68 |
| 69 // Simply quits the current message loop when finished. Used to make | 69 // Simply quits the current message loop when finished. Used to make |
| 70 // URLFetcher synchronous. | 70 // URLFetcher synchronous. |
| 71 class QuitDelegate : public net::URLFetcherDelegate { | 71 class QuitDelegate : public net::URLFetcherDelegate { |
| 72 public: | 72 public: |
| 73 QuitDelegate() {} | 73 QuitDelegate() {} |
| 74 | 74 |
| 75 ~QuitDelegate() override {} | 75 ~QuitDelegate() override {} |
| 76 | 76 |
| 77 // net::URLFetcherDelegate implementation. | 77 // net::URLFetcherDelegate implementation. |
| 78 void OnURLFetchComplete(const net::URLFetcher* source) override { | 78 void OnURLFetchComplete(const net::URLFetcher* source) override { |
| 79 base::MessageLoop::current()->QuitWhenIdle(); | 79 base::MessageLoop::current()->QuitWhenIdle(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 82 void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 83 int64 current, | 83 int64_t current, |
| 84 int64 total) override { | 84 int64_t total) override { |
| 85 NOTREACHED(); | 85 NOTREACHED(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void OnURLFetchUploadProgress(const net::URLFetcher* source, | 88 void OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 89 int64 current, | 89 int64_t current, |
| 90 int64 total) override { | 90 int64_t total) override { |
| 91 NOTREACHED(); | 91 NOTREACHED(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 private: | 94 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(QuitDelegate); | 95 DISALLOW_COPY_AND_ASSIGN(QuitDelegate); |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 // NetLog::ThreadSafeObserver implementation that simply prints events | 98 // NetLog::ThreadSafeObserver implementation that simply prints events |
| 99 // to the logs. | 99 // to the logs. |
| 100 class PrintingLogObserver : public net::NetLog::ThreadSafeObserver { | 100 class PrintingLogObserver : public net::NetLog::ThreadSafeObserver { |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 main_loop.Run(); | 254 main_loop.Run(); |
| 255 | 255 |
| 256 const base::Time end_time = base::Time::Now(); | 256 const base::Time end_time = base::Time::Now(); |
| 257 const base::TimeTicks end_ticks = base::TimeTicks::Now(); | 257 const base::TimeTicks end_ticks = base::TimeTicks::Now(); |
| 258 | 258 |
| 259 std::printf( | 259 std::printf( |
| 260 "Request ended at %s (ticks = %" PRId64 ")\n", | 260 "Request ended at %s (ticks = %" PRId64 ")\n", |
| 261 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(end_time)).c_str(), | 261 UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(end_time)).c_str(), |
| 262 end_ticks.ToInternalValue()); | 262 end_ticks.ToInternalValue()); |
| 263 | 263 |
| 264 const int64 delta_ticks_internal = | 264 const int64_t delta_ticks_internal = |
| 265 end_ticks.ToInternalValue() - start_ticks.ToInternalValue(); | 265 end_ticks.ToInternalValue() - start_ticks.ToInternalValue(); |
| 266 const base::TimeDelta delta_ticks = end_ticks - start_ticks; | 266 const base::TimeDelta delta_ticks = end_ticks - start_ticks; |
| 267 | 267 |
| 268 std::printf( | 268 std::printf( |
| 269 "Request took %" PRId64 " ticks (%.2f ms)\n", | 269 "Request took %" PRId64 " ticks (%.2f ms)\n", |
| 270 delta_ticks_internal, delta_ticks.InMillisecondsF()); | 270 delta_ticks_internal, delta_ticks.InMillisecondsF()); |
| 271 | 271 |
| 272 const net::URLRequestStatus status = fetcher->GetStatus(); | 272 const net::URLRequestStatus status = fetcher->GetStatus(); |
| 273 if (status.status() != net::URLRequestStatus::SUCCESS) { | 273 if (status.status() != net::URLRequestStatus::SUCCESS) { |
| 274 LOG(ERROR) << "Request failed with error code: " | 274 LOG(ERROR) << "Request failed with error code: " |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 &skew, &skew_uncertainty); | 317 &skew, &skew_uncertainty); |
| 318 | 318 |
| 319 std::printf( | 319 std::printf( |
| 320 "An estimate for the local clock skew is %.2f ms with " | 320 "An estimate for the local clock skew is %.2f ms with " |
| 321 "uncertainty %.2f ms\n", | 321 "uncertainty %.2f ms\n", |
| 322 skew.InMillisecondsF(), | 322 skew.InMillisecondsF(), |
| 323 skew_uncertainty.InMillisecondsF()); | 323 skew_uncertainty.InMillisecondsF()); |
| 324 | 324 |
| 325 return EXIT_SUCCESS; | 325 return EXIT_SUCCESS; |
| 326 } | 326 } |
| OLD | NEW |