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

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

Issue 16137008: Refactor net::NetLog to provide implementation of observer pattern, not just the interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: private Observer Created 7 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
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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 96
97 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source, 97 virtual void OnURLFetchUploadProgress(const net::URLFetcher* source,
98 int64 current, int64 total) OVERRIDE { 98 int64 current, int64 total) OVERRIDE {
99 NOTREACHED(); 99 NOTREACHED();
100 } 100 }
101 101
102 private: 102 private:
103 DISALLOW_COPY_AND_ASSIGN(QuitDelegate); 103 DISALLOW_COPY_AND_ASSIGN(QuitDelegate);
104 }; 104 };
105 105
106 // NetLog implementation that simply prints events to the logs. 106 // NetLog::ThreadSafeObserver implementation that simply prints events
107 class PrintingLog : public net::NetLog { 107 // to the logs.
108 class PrintingLogObserver : public net::NetLog::ThreadSafeObserver {
108 public: 109 public:
109 PrintingLog() : next_id_(1) {} 110 PrintingLogObserver() {}
110 111
111 virtual ~PrintingLog() {} 112 virtual ~PrintingLogObserver() {
113 // This is guaranteed to be safe as this program is single threaded.
114 net_log()->RemoveThreadSafeObserver(this);
115 }
112 116
113 // NetLog implementation: 117 // NetLog::ThreadSafeObserver implementation:
114 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE { 118 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE {
115 // The log level of the entry is unknown, so just assume it maps 119 // The log level of the entry is unknown, so just assume it maps
116 // to VLOG(1). 120 // to VLOG(1).
117 if (!VLOG_IS_ON(1)) 121 if (!VLOG_IS_ON(1))
118 return; 122 return;
119 123
120 const char* const source_type = 124 const char* const source_type =
121 net::NetLog::SourceTypeToString(entry.source().type); 125 net::NetLog::SourceTypeToString(entry.source().type);
122 const char* const event_type = 126 const char* const event_type =
123 net::NetLog::EventTypeToString(entry.type()); 127 net::NetLog::EventTypeToString(entry.type());
124 const char* const event_phase = 128 const char* const event_phase =
125 net::NetLog::EventPhaseToString(entry.phase()); 129 net::NetLog::EventPhaseToString(entry.phase());
126 scoped_ptr<base::Value> params(entry.ParametersToValue()); 130 scoped_ptr<base::Value> params(entry.ParametersToValue());
127 std::string params_str; 131 std::string params_str;
128 if (params.get()) { 132 if (params.get()) {
129 base::JSONWriter::Write(params.get(), &params_str); 133 base::JSONWriter::Write(params.get(), &params_str);
130 params_str.insert(0, ": "); 134 params_str.insert(0, ": ");
131 } 135 }
132 136
133 VLOG(1) << source_type << "(" << entry.source().id << "): " 137 VLOG(1) << source_type << "(" << entry.source().id << "): "
134 << event_type << ": " << event_phase << params_str; 138 << event_type << ": " << event_phase << params_str;
135 } 139 }
136 140
137 virtual uint32 NextID() OVERRIDE {
138 return next_id_++;
139 }
140
141 virtual LogLevel GetLogLevel() const OVERRIDE {
142 const int vlog_level = logging::GetVlogLevel(__FILE__);
143 if (vlog_level <= 0) {
144 return LOG_BASIC;
145 }
146 if (vlog_level == 1) {
147 return LOG_ALL_BUT_BYTES;
148 }
149 return LOG_ALL;
150 }
151
152 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer,
153 LogLevel log_level) OVERRIDE {
154 NOTIMPLEMENTED();
155 }
156
157 virtual void SetObserverLogLevel(ThreadSafeObserver* observer,
158 LogLevel log_level) OVERRIDE {
159 NOTIMPLEMENTED();
160 }
161
162 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE {
163 NOTIMPLEMENTED();
164 }
165
166 private: 141 private:
167 uint32 next_id_; 142 DISALLOW_COPY_AND_ASSIGN(PrintingLogObserver);
168
169 DISALLOW_COPY_AND_ASSIGN(PrintingLog);
170 }; 143 };
171 144
172 // Builds a URLRequestContext assuming there's only a single loop. 145 // Builds a URLRequestContext assuming there's only a single loop.
173 scoped_ptr<net::URLRequestContext> BuildURLRequestContext() { 146 scoped_ptr<net::URLRequestContext>
147 BuildURLRequestContext(net::NetLog* net_log) {
174 net::URLRequestContextBuilder builder; 148 net::URLRequestContextBuilder builder;
175 #if defined(OS_LINUX) 149 #if defined(OS_LINUX)
176 // On Linux, use a fixed ProxyConfigService, since the default one 150 // On Linux, use a fixed ProxyConfigService, since the default one
177 // depends on glib. 151 // depends on glib.
178 // 152 //
179 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. 153 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed.
180 builder.set_proxy_config_service( 154 builder.set_proxy_config_service(
181 new net::ProxyConfigServiceFixed(net::ProxyConfig())); 155 new net::ProxyConfigServiceFixed(net::ProxyConfig()));
182 #endif 156 #endif
183 scoped_ptr<net::URLRequestContext> context(builder.Build()); 157 scoped_ptr<net::URLRequestContext> context(builder.Build());
184 context->set_net_log(new PrintingLog()); 158 context->set_net_log(net_log);
185 return context.Pass(); 159 return context.Pass();
186 } 160 }
187 161
188 class SingleThreadRequestContextGetter : public net::URLRequestContextGetter { 162 class SingleThreadRequestContextGetter : public net::URLRequestContextGetter {
189 public: 163 public:
190 // Since there's only a single thread, there's no need to worry 164 // Since there's only a single thread, there's no need to worry
191 // about when |context_| gets created. 165 // about when |context_| gets created.
192 explicit SingleThreadRequestContextGetter( 166 SingleThreadRequestContextGetter(
167 net::NetLog* net_log,
193 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) 168 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner)
194 : context_(BuildURLRequestContext()), 169 : context_(BuildURLRequestContext(net_log)),
195 main_task_runner_(main_task_runner) {} 170 main_task_runner_(main_task_runner) {}
196 171
197 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { 172 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
198 return context_.get(); 173 return context_.get();
199 } 174 }
200 175
201 virtual scoped_refptr<base::SingleThreadTaskRunner> 176 virtual scoped_refptr<base::SingleThreadTaskRunner>
202 GetNetworkTaskRunner() const OVERRIDE { 177 GetNetworkTaskRunner() const OVERRIDE {
203 return main_task_runner_; 178 return main_task_runner_;
204 } 179 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 return EXIT_FAILURE; 248 return EXIT_FAILURE;
274 } 249 }
275 250
276 base::MessageLoopForIO main_loop; 251 base::MessageLoopForIO main_loop;
277 252
278 // NOTE: A NetworkChangeNotifier could be instantiated here, but 253 // NOTE: A NetworkChangeNotifier could be instantiated here, but
279 // that interferes with the request that will be sent; some 254 // that interferes with the request that will be sent; some
280 // implementations always send out an OnIPAddressChanged() message, 255 // implementations always send out an OnIPAddressChanged() message,
281 // which causes the DNS resolution to abort. It's simpler to just 256 // which causes the DNS resolution to abort. It's simpler to just
282 // not instantiate one, since only a single request is sent anyway. 257 // not instantiate one, since only a single request is sent anyway.
283 258 net::NetLog net_log;
259 PrintingLogObserver printing_log_observer;
mmenke 2013/06/05 16:22:14 Suggest commenting that this order is important fo
kouhei (in TOK) 2013/06/06 00:49:40 Done.
260 net_log.AddThreadSafeObserver(&printing_log_observer, net::NetLog::LOG_ALL);
284 scoped_refptr<SingleThreadRequestContextGetter> context_getter( 261 scoped_refptr<SingleThreadRequestContextGetter> context_getter(
285 new SingleThreadRequestContextGetter(main_loop.message_loop_proxy())); 262 new SingleThreadRequestContextGetter(&net_log,
263 main_loop.message_loop_proxy()));
286 264
287 QuitDelegate delegate; 265 QuitDelegate delegate;
288 scoped_ptr<net::URLFetcher> fetcher( 266 scoped_ptr<net::URLFetcher> fetcher(
289 net::URLFetcher::Create(url, net::URLFetcher::HEAD, &delegate)); 267 net::URLFetcher::Create(url, net::URLFetcher::HEAD, &delegate));
290 fetcher->SetRequestContext(context_getter.get()); 268 fetcher->SetRequestContext(context_getter.get());
291 269
292 const base::Time start_time = base::Time::Now(); 270 const base::Time start_time = base::Time::Now();
293 const base::TimeTicks start_ticks = base::TimeTicks::Now(); 271 const base::TimeTicks start_ticks = base::TimeTicks::Now();
294 272
295 fetcher->Start(); 273 fetcher->Start();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 &skew, &skew_uncertainty); 343 &skew, &skew_uncertainty);
366 344
367 std::printf( 345 std::printf(
368 "An estimate for the local clock skew is %.2f ms with " 346 "An estimate for the local clock skew is %.2f ms with "
369 "uncertainty %.2f ms\n", 347 "uncertainty %.2f ms\n",
370 skew.InMillisecondsF(), 348 skew.InMillisecondsF(),
371 skew_uncertainty.InMillisecondsF()); 349 skew_uncertainty.InMillisecondsF());
372 350
373 return EXIT_SUCCESS; 351 return EXIT_SUCCESS;
374 } 352 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698