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

Side by Side Diff: net/base/net_log.cc

Issue 10399083: Make NetLog take in callbacks that return Values (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix merge error Created 8 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 | « net/base/net_log.h ('k') | net/base/net_log_unittest.cc » ('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 #include "net/base/net_log.h" 5 #include "net/base/net_log.h"
6 6
7 #include "base/bind.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
9 #include "base/time.h" 10 #include "base/time.h"
10 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
13 14
14 namespace net { 15 namespace net {
15 16
16 namespace { 17 namespace {
17 18
18 // Parameters for logging data transferred events. Includes bytes transferred 19 // Returns parameters for logging data transferred events. Includes number of
19 // and, if |bytes| is not NULL, the bytes themselves. 20 // bytes transferred and, if the log level indicates bytes should be logged and
20 class NetLogBytesTransferredParameter : public NetLog::EventParameters { 21 // |byte_count| > 0, the bytes themselves. The bytes are hex-encoded, since
21 public: 22 // base::StringValue only supports UTF-8.
22 NetLogBytesTransferredParameter(int byte_count, const char* bytes); 23 Value* BytesTransferredCallback(int byte_count,
23 24 const char* bytes,
24 virtual Value* ToValue() const; 25 NetLog::LogLevel log_level) {
25
26 protected:
27 virtual ~NetLogBytesTransferredParameter() {}
28
29 private:
30 const int byte_count_;
31 std::string hex_encoded_bytes_;
32 bool has_bytes_;
33 };
34
35 NetLogBytesTransferredParameter::NetLogBytesTransferredParameter(
36 int byte_count, const char* transferred_bytes)
37 : byte_count_(byte_count),
38 has_bytes_(false) {
39 if (transferred_bytes) {
40 hex_encoded_bytes_ = base::HexEncode(transferred_bytes, byte_count);
41 has_bytes_ = true;
42 }
43 }
44
45 Value* NetLogBytesTransferredParameter::ToValue() const {
46 DictionaryValue* dict = new DictionaryValue(); 26 DictionaryValue* dict = new DictionaryValue();
47 dict->SetInteger("byte_count", byte_count_); 27 dict->SetInteger("byte_count", byte_count);
48 if (has_bytes_ && byte_count_ > 0) 28 if (NetLog::IsLoggingBytes(log_level) && byte_count > 0)
49 dict->SetString("hex_encoded_bytes", hex_encoded_bytes_); 29 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count));
50 return dict; 30 return dict;
51 } 31 }
52 32
33 Value* EventParametersCallback(
34 const scoped_refptr<NetLog::EventParameters>& params,
35 NetLog::LogLevel /* log_level */) {
36 if (!params.get())
37 return NULL;
38 return params->ToValue();
39 }
40
41 Value* SourceEventParametersCallback(const NetLog::Source source,
42 NetLog::LogLevel /* log_level */) {
43 DictionaryValue* event_params = new DictionaryValue();
44 source.AddToEventParameters(event_params);
45 return event_params;
46 }
47
48 Value* SingleIntegerCallback(const char* name,
49 int value,
50 NetLog::LogLevel /* log_level */) {
51 if (!value)
52 return NULL;
53 DictionaryValue* event_params = new DictionaryValue();
54 event_params->SetInteger(name, value);
55 return event_params;
56 }
57
53 } // namespace 58 } // namespace
54 59
55 Value* NetLog::Source::ToValue() const { 60 Value* NetLog::Source::ToValue() const {
56 DictionaryValue* dict = new DictionaryValue(); 61 DictionaryValue* dict = new DictionaryValue();
57 dict->SetInteger("type", static_cast<int>(type)); 62 dict->SetInteger("type", static_cast<int>(type));
58 dict->SetInteger("id", static_cast<int>(id)); 63 dict->SetInteger("id", static_cast<int>(id));
59 return dict; 64 return dict;
60 } 65 }
61 66
67 void NetLog::Source::AddToEventParameters(DictionaryValue* event_params) const {
68 DictionaryValue* dict = new DictionaryValue();
69 dict->SetInteger("type", static_cast<int>(type));
70 dict->SetInteger("id", static_cast<int>(id));
71 event_params->Set("source_dependency", dict);
72 }
73
74 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const {
75 return base::Bind(&SourceEventParametersCallback, *this);
76 }
77
78 // static
79 bool NetLog::Source::FromEventParameters(Value* event_params, Source* source) {
80 DictionaryValue* dict;
81 DictionaryValue* source_dict;
82 int source_id;
83 int source_type;
84 if (!event_params ||
85 !event_params->GetAsDictionary(&dict) ||
86 !dict->GetDictionary("source_dependency", &source_dict) ||
87 !source_dict->GetInteger("id", &source_id) ||
88 !source_dict->GetInteger("type", &source_type)) {
89 *source = Source();
90 return false;
91 }
92
93 DCHECK_LE(0, source_id);
94 DCHECK_LT(source_type, NetLog::SOURCE_COUNT);
95 *source = Source(static_cast<SourceType>(source_type), source_id);
96 return true;
97 }
98
99 Value* NetLog::Entry::ToValue() const {
100 DictionaryValue* entry_dict(new DictionaryValue());
101
102 entry_dict->SetString("time", TickCountToString(base::TimeTicks::Now()));
103
104 // Set the entry source.
105 DictionaryValue* source_dict = new DictionaryValue();
106 source_dict->SetInteger("id", source_.id);
107 source_dict->SetInteger("type", static_cast<int>(source_.type));
108 entry_dict->Set("source", source_dict);
109
110 // Set the event info.
111 entry_dict->SetInteger("type", static_cast<int>(type_));
112 entry_dict->SetInteger("phase", static_cast<int>(phase_));
113
114 // Set the event-specific parameters.
115 if (parameters_callback_) {
116 Value* value = parameters_callback_->Run(log_level_);
117 if (value)
118 entry_dict->Set("params", value);
119 }
120
121 return entry_dict;
122 }
123
124 Value* NetLog::Entry::ParametersToValue() const {
125 if (parameters_callback_)
126 return parameters_callback_->Run(log_level_);
127 return NULL;
128 }
129
130 NetLog::Entry::Entry(
131 EventType type,
132 Source source,
133 EventPhase phase,
134 const ParametersCallback* parameters_callback,
135 LogLevel log_level)
136 : type_(type),
137 source_(source),
138 phase_(phase),
139 parameters_callback_(parameters_callback),
140 log_level_(log_level) {
141 };
142
143 NetLog::Entry::~Entry() {
144 }
145
62 NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_BASIC), 146 NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_BASIC),
63 net_log_(NULL) { 147 net_log_(NULL) {
64 } 148 }
65 149
66 NetLog::ThreadSafeObserver::~ThreadSafeObserver() { 150 NetLog::ThreadSafeObserver::~ThreadSafeObserver() {
67 // Make sure we aren't watching a NetLog on destruction. Because the NetLog 151 // Make sure we aren't watching a NetLog on destruction. Because the NetLog
68 // may pass events to each observer on multiple threads, we cannot safely 152 // may pass events to each observer on multiple threads, we cannot safely
69 // stop watching a NetLog automatically from a parent class. 153 // stop watching a NetLog automatically from a parent class.
70 DCHECK(!net_log_); 154 DCHECK(!net_log_);
71 } 155 }
72 156
73 NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const { 157 NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const {
74 DCHECK(net_log_); 158 DCHECK(net_log_);
75 return log_level_; 159 return log_level_;
76 } 160 }
77 161
78 NetLog* NetLog::ThreadSafeObserver::net_log() const { 162 NetLog* NetLog::ThreadSafeObserver::net_log() const {
79 return net_log_; 163 return net_log_;
80 } 164 }
81 165
166 void NetLog::AddGlobalEntry(EventType type) {
167 AddEntry(type,
168 Source(net::NetLog::SOURCE_NONE, NextID()),
169 net::NetLog::PHASE_NONE,
170 NULL);
171 }
172
173 void NetLog::AddGlobalEntry(
174 EventType type,
175 const NetLog::ParametersCallback& parameters_callback) {
176 AddEntry(type,
177 Source(net::NetLog::SOURCE_NONE, NextID()),
178 net::NetLog::PHASE_NONE,
179 &parameters_callback);
180 }
181
82 void NetLog::AddGlobalEntry(EventType type, 182 void NetLog::AddGlobalEntry(EventType type,
83 const scoped_refptr<EventParameters>& params) { 183 const scoped_refptr<EventParameters>& params) {
184 ParametersCallback callback = base::Bind(&EventParametersCallback, params);
84 AddEntry(type, 185 AddEntry(type,
85 Source(net::NetLog::SOURCE_NONE, this->NextID()), 186 Source(net::NetLog::SOURCE_NONE, NextID()),
86 net::NetLog::PHASE_NONE, 187 net::NetLog::PHASE_NONE,
87 params); 188 &callback);
88 } 189 }
89 190
90 // static 191 // static
91 std::string NetLog::TickCountToString(const base::TimeTicks& time) { 192 std::string NetLog::TickCountToString(const base::TimeTicks& time) {
92 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); 193 int64 delta_time = (time - base::TimeTicks()).InMilliseconds();
93 return base::Int64ToString(delta_time); 194 return base::Int64ToString(delta_time);
94 } 195 }
95 196
96 // static 197 // static
97 const char* NetLog::EventTypeToString(EventType event) { 198 const char* NetLog::EventTypeToString(EventType event) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 case PHASE_END: 244 case PHASE_END:
144 return "PHASE_END"; 245 return "PHASE_END";
145 case PHASE_NONE: 246 case PHASE_NONE:
146 return "PHASE_NONE"; 247 return "PHASE_NONE";
147 } 248 }
148 NOTREACHED(); 249 NOTREACHED();
149 return NULL; 250 return NULL;
150 } 251 }
151 252
152 // static 253 // static
153 Value* NetLog::EntryToDictionaryValue(NetLog::EventType type, 254 bool NetLog::IsLoggingBytes(LogLevel log_level) {
154 const base::TimeTicks& time, 255 return log_level == NetLog::LOG_ALL;
155 const NetLog::Source& source, 256 }
156 NetLog::EventPhase phase,
157 NetLog::EventParameters* params,
158 bool use_strings) {
159 DictionaryValue* entry_dict = new DictionaryValue();
160 257
161 entry_dict->SetString("time", TickCountToString(time)); 258 // static
259 bool NetLog::IsLoggingAllEvents(LogLevel log_level) {
260 return log_level <= NetLog::LOG_ALL_BUT_BYTES;
261 }
162 262
163 // Set the entry source. 263 // static
164 DictionaryValue* source_dict = new DictionaryValue(); 264 NetLog::ParametersCallback NetLog::IntegerCallback(const char* name,
165 source_dict->SetInteger("id", source.id); 265 int value) {
166 if (!use_strings) { 266 return base::Bind(&SingleIntegerCallback, name, value);
167 source_dict->SetInteger("type", static_cast<int>(source.type));
168 } else {
169 source_dict->SetString("type",
170 NetLog::SourceTypeToString(source.type));
171 }
172 entry_dict->Set("source", source_dict);
173
174 // Set the event info.
175 if (!use_strings) {
176 entry_dict->SetInteger("type", static_cast<int>(type));
177 entry_dict->SetInteger("phase", static_cast<int>(phase));
178 } else {
179 entry_dict->SetString("type", NetLog::EventTypeToString(type));
180 entry_dict->SetString("phase", NetLog::EventPhaseToString(phase));
181 }
182
183 // Set the event-specific parameters.
184 if (params)
185 entry_dict->Set("params", params->ToValue());
186
187 return entry_dict;
188 } 267 }
189 268
190 void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) { 269 void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) {
191 DCHECK(!observer->net_log_); 270 DCHECK(!observer->net_log_);
192 observer->net_log_ = this; 271 observer->net_log_ = this;
193 observer->log_level_ = log_level; 272 observer->log_level_ = log_level;
194 } 273 }
195 274
196 void NetLog::OnSetObserverLogLevel(ThreadSafeObserver* observer, 275 void NetLog::OnSetObserverLogLevel(ThreadSafeObserver* observer,
197 LogLevel log_level) { 276 LogLevel log_level) {
198 DCHECK_EQ(this, observer->net_log_); 277 DCHECK_EQ(this, observer->net_log_);
199 observer->log_level_ = log_level; 278 observer->log_level_ = log_level;
200 } 279 }
201 280
202 void NetLog::OnRemoveObserver(ThreadSafeObserver* observer) { 281 void NetLog::OnRemoveObserver(ThreadSafeObserver* observer) {
203 DCHECK_EQ(this, observer->net_log_); 282 DCHECK_EQ(this, observer->net_log_);
204 observer->net_log_ = NULL; 283 observer->net_log_ = NULL;
205 } 284 }
206 285
286 void NetLog::AddEntry(EventType type,
287 const Source& source,
288 EventPhase phase,
289 const NetLog::ParametersCallback* parameters_callback) {
290 Entry entry(type, source, phase, parameters_callback, GetLogLevel());
291 OnAddEntry(entry);
292 }
293
294 void BoundNetLog::AddEntry(NetLog::EventType type,
295 NetLog::EventPhase phase) const {
296 if (!net_log_)
297 return;
298 net_log_->AddEntry(type, source_, phase, NULL);
299 }
300
301 void BoundNetLog::AddEntry(
302 NetLog::EventType type,
303 NetLog::EventPhase phase,
304 const NetLog::ParametersCallback& get_parameters) const {
305 if (!net_log_)
306 return;
307 net_log_->AddEntry(type, source_, phase, &get_parameters);
308 }
309
310 void BoundNetLog::AddEvent(NetLog::EventType type) const {
311 AddEntry(type, NetLog::PHASE_NONE);
312 }
313
314 void BoundNetLog::AddEvent(
315 NetLog::EventType type,
316 const NetLog::ParametersCallback& get_parameters) const {
317 AddEntry(type, NetLog::PHASE_NONE, get_parameters);
318 }
319
320 void BoundNetLog::BeginEvent(NetLog::EventType type) const {
321 AddEntry(type, NetLog::PHASE_BEGIN);
322 }
323
324 void BoundNetLog::BeginEvent(
325 NetLog::EventType type,
326 const NetLog::ParametersCallback& get_parameters) const {
327 AddEntry(type, NetLog::PHASE_BEGIN, get_parameters);
328 }
329
330 void BoundNetLog::EndEvent(NetLog::EventType type) const {
331 AddEntry(type, NetLog::PHASE_END);
332 }
333
334 void BoundNetLog::EndEvent(
335 NetLog::EventType type,
336 const NetLog::ParametersCallback& get_parameters) const {
337 AddEntry(type, NetLog::PHASE_END, get_parameters);
338 }
339
207 void BoundNetLog::AddEntry( 340 void BoundNetLog::AddEntry(
208 NetLog::EventType type, 341 NetLog::EventType type,
209 NetLog::EventPhase phase, 342 NetLog::EventPhase phase,
210 const scoped_refptr<NetLog::EventParameters>& params) const { 343 const scoped_refptr<NetLog::EventParameters>& params) const {
211 if (net_log_) 344 if (!net_log_)
212 net_log_->AddEntry(type, source_, phase, params); 345 return;
346 NetLog::ParametersCallback callback =
347 base::Bind(&EventParametersCallback, params);
348 net_log_->AddEntry(type, source_, phase, &callback);
213 } 349 }
214 350
215 void BoundNetLog::AddEvent( 351 void BoundNetLog::AddEvent(
216 NetLog::EventType event_type, 352 NetLog::EventType event_type,
217 const scoped_refptr<NetLog::EventParameters>& params) const { 353 const scoped_refptr<NetLog::EventParameters>& params) const {
218 AddEntry(event_type, NetLog::PHASE_NONE, params); 354 AddEntry(event_type, NetLog::PHASE_NONE, params);
219 } 355 }
220 356
221 void BoundNetLog::BeginEvent( 357 void BoundNetLog::BeginEvent(
222 NetLog::EventType event_type, 358 NetLog::EventType event_type,
223 const scoped_refptr<NetLog::EventParameters>& params) const { 359 const scoped_refptr<NetLog::EventParameters>& params) const {
224 AddEntry(event_type, NetLog::PHASE_BEGIN, params); 360 AddEntry(event_type, NetLog::PHASE_BEGIN, params);
225 } 361 }
226 362
227 void BoundNetLog::EndEvent( 363 void BoundNetLog::EndEvent(
228 NetLog::EventType event_type, 364 NetLog::EventType event_type,
229 const scoped_refptr<NetLog::EventParameters>& params) const { 365 const scoped_refptr<NetLog::EventParameters>& params) const {
230 AddEntry(event_type, NetLog::PHASE_END, params); 366 AddEntry(event_type, NetLog::PHASE_END, params);
231 } 367 }
232 368
233 void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type, 369 void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type,
234 int net_error) const { 370 int net_error) const {
235 DCHECK_GT(0, net_error); 371 DCHECK_GT(0, net_error);
236 DCHECK_NE(ERR_IO_PENDING, net_error); 372 DCHECK_NE(ERR_IO_PENDING, net_error);
237 AddEvent( 373 AddEvent(event_type, NetLog::IntegerCallback("net_error", net_error));
238 event_type,
239 make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error)));
240 } 374 }
241 375
242 void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type, 376 void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type,
243 int net_error) const { 377 int net_error) const {
244 DCHECK_NE(ERR_IO_PENDING, net_error); 378 DCHECK_NE(ERR_IO_PENDING, net_error);
245 if (net_error >= 0) { 379 EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error));
246 EndEvent(event_type, NULL);
247 } else {
248 EndEvent(
249 event_type,
250 make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error)));
251 }
252 } 380 }
253 381
254 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, 382 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type,
255 int byte_count, 383 int byte_count,
256 const char* bytes) const { 384 const char* bytes) const {
257 scoped_refptr<NetLog::EventParameters> params; 385 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes));
258 if (IsLoggingBytes()) {
259 params = new NetLogBytesTransferredParameter(byte_count, bytes);
260 } else {
261 params = new NetLogBytesTransferredParameter(byte_count, NULL);
262 }
263 AddEvent(event_type, params);
264 } 386 }
265 387
266 NetLog::LogLevel BoundNetLog::GetLogLevel() const { 388 NetLog::LogLevel BoundNetLog::GetLogLevel() const {
267 if (net_log_) 389 if (net_log_)
268 return net_log_->GetLogLevel(); 390 return net_log_->GetLogLevel();
269 return NetLog::LOG_BASIC; 391 return NetLog::LOG_BASIC;
270 } 392 }
271 393
272 bool BoundNetLog::IsLoggingBytes() const { 394 bool BoundNetLog::IsLoggingBytes() const {
273 return GetLogLevel() == NetLog::LOG_ALL; 395 return NetLog::IsLoggingBytes(GetLogLevel());
274 } 396 }
275 397
276 bool BoundNetLog::IsLoggingAllEvents() const { 398 bool BoundNetLog::IsLoggingAllEvents() const {
277 return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES; 399 return NetLog::IsLoggingAllEvents(GetLogLevel());
278 } 400 }
279 401
280 // static 402 // static
281 BoundNetLog BoundNetLog::Make(NetLog* net_log, 403 BoundNetLog BoundNetLog::Make(NetLog* net_log,
282 NetLog::SourceType source_type) { 404 NetLog::SourceType source_type) {
283 if (!net_log) 405 if (!net_log)
284 return BoundNetLog(); 406 return BoundNetLog();
285 407
286 NetLog::Source source(source_type, net_log->NextID()); 408 NetLog::Source source(source_type, net_log->NextID());
287 return BoundNetLog(source, net_log); 409 return BoundNetLog(source, net_log);
(...skipping 19 matching lines...) Expand all
307 return dict; 429 return dict;
308 } 430 }
309 431
310 Value* NetLogSourceParameter::ToValue() const { 432 Value* NetLogSourceParameter::ToValue() const {
311 DictionaryValue* dict = new DictionaryValue(); 433 DictionaryValue* dict = new DictionaryValue();
312 if (value_.is_valid()) 434 if (value_.is_valid())
313 dict->Set(name_, value_.ToValue()); 435 dict->Set(name_, value_.ToValue());
314 return dict; 436 return dict;
315 } 437 }
316 438
317 ScopedNetLogEvent::ScopedNetLogEvent(
318 const BoundNetLog& net_log,
319 NetLog::EventType event_type,
320 const scoped_refptr<NetLog::EventParameters>& params)
321 : net_log_(net_log),
322 event_type_(event_type) {
323 net_log_.BeginEvent(event_type, params);
324 }
325
326 ScopedNetLogEvent::~ScopedNetLogEvent() {
327 net_log_.EndEvent(event_type_, end_event_params_);
328 }
329
330 void ScopedNetLogEvent::SetEndEventParameters(
331 const scoped_refptr<NetLog::EventParameters>& end_event_params) {
332 DCHECK(!end_event_params_.get());
333 end_event_params_ = end_event_params;
334 }
335
336 const BoundNetLog& ScopedNetLogEvent::net_log() const {
337 return net_log_;
338 }
339
340 } // namespace net 439 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_log.h ('k') | net/base/net_log_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698