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 #include "net/log/net_log.h" | 5 #include "net/log/net_log.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 new base::DictionaryValue()); | 103 new base::DictionaryValue()); |
104 event_params->SetString(name, *value); | 104 event_params->SetString(name, *value); |
105 return std::move(event_params); | 105 return std::move(event_params); |
106 } | 106 } |
107 | 107 |
108 } // namespace | 108 } // namespace |
109 | 109 |
110 // LoadTimingInfo requires this be 0. | 110 // LoadTimingInfo requires this be 0. |
111 const uint32_t NetLog::Source::kInvalidId = 0; | 111 const uint32_t NetLog::Source::kInvalidId = 0; |
112 | 112 |
113 NetLog::Source::Source() : type(SOURCE_NONE), id(kInvalidId) { | 113 NetLog::Source::Source() : type(NetLogSourceType::NONE), id(kInvalidId) { |
114 } | 114 } |
115 | 115 |
116 NetLog::Source::Source(SourceType type, uint32_t id) : type(type), id(id) {} | 116 NetLog::Source::Source(NetLogSourceType type, uint32_t id) |
| 117 : type(type), id(id) {} |
117 | 118 |
118 bool NetLog::Source::IsValid() const { | 119 bool NetLog::Source::IsValid() const { |
119 return id != kInvalidId; | 120 return id != kInvalidId; |
120 } | 121 } |
121 | 122 |
122 void NetLog::Source::AddToEventParameters( | 123 void NetLog::Source::AddToEventParameters( |
123 base::DictionaryValue* event_params) const { | 124 base::DictionaryValue* event_params) const { |
124 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 125 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
125 dict->SetInteger("type", static_cast<int>(type)); | 126 dict->SetInteger("type", static_cast<int>(type)); |
126 dict->SetInteger("id", static_cast<int>(id)); | 127 dict->SetInteger("id", static_cast<int>(id)); |
127 event_params->Set("source_dependency", std::move(dict)); | 128 event_params->Set("source_dependency", std::move(dict)); |
128 } | 129 } |
129 | 130 |
130 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const { | 131 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const { |
131 return base::Bind(&SourceEventParametersCallback, *this); | 132 return base::Bind(&SourceEventParametersCallback, *this); |
132 } | 133 } |
133 | 134 |
134 // static | 135 // static |
135 bool NetLog::Source::FromEventParameters(base::Value* event_params, | 136 bool NetLog::Source::FromEventParameters(base::Value* event_params, |
136 Source* source) { | 137 Source* source) { |
137 base::DictionaryValue* dict = NULL; | 138 base::DictionaryValue* dict = NULL; |
138 base::DictionaryValue* source_dict = NULL; | 139 base::DictionaryValue* source_dict = NULL; |
139 int source_id = -1; | 140 int source_id = -1; |
140 int source_type = NetLog::SOURCE_COUNT; | 141 int source_type = static_cast<int>(NetLogSourceType::COUNT); |
141 if (!event_params || !event_params->GetAsDictionary(&dict) || | 142 if (!event_params || !event_params->GetAsDictionary(&dict) || |
142 !dict->GetDictionary("source_dependency", &source_dict) || | 143 !dict->GetDictionary("source_dependency", &source_dict) || |
143 !source_dict->GetInteger("id", &source_id) || | 144 !source_dict->GetInteger("id", &source_id) || |
144 !source_dict->GetInteger("type", &source_type)) { | 145 !source_dict->GetInteger("type", &source_type)) { |
145 *source = Source(); | 146 *source = Source(); |
146 return false; | 147 return false; |
147 } | 148 } |
148 | 149 |
149 DCHECK_GE(source_id, 0); | 150 DCHECK_GE(source_id, 0); |
150 DCHECK_LT(source_type, NetLog::SOURCE_COUNT); | 151 DCHECK_LT(source_type, static_cast<int>(NetLogSourceType::COUNT)); |
151 *source = Source(static_cast<SourceType>(source_type), source_id); | 152 *source = Source(static_cast<NetLogSourceType>(source_type), source_id); |
152 return true; | 153 return true; |
153 } | 154 } |
154 | 155 |
155 std::unique_ptr<base::Value> NetLog::Entry::ToValue() const { | 156 std::unique_ptr<base::Value> NetLog::Entry::ToValue() const { |
156 std::unique_ptr<base::DictionaryValue> entry_dict( | 157 std::unique_ptr<base::DictionaryValue> entry_dict( |
157 new base::DictionaryValue()); | 158 new base::DictionaryValue()); |
158 | 159 |
159 entry_dict->SetString("time", TickCountToString(data_->time)); | 160 entry_dict->SetString("time", TickCountToString(data_->time)); |
160 | 161 |
161 // Set the entry source. | 162 // Set the entry source. |
(...skipping 17 matching lines...) Expand all Loading... |
179 | 180 |
180 return std::move(entry_dict); | 181 return std::move(entry_dict); |
181 } | 182 } |
182 | 183 |
183 std::unique_ptr<base::Value> NetLog::Entry::ParametersToValue() const { | 184 std::unique_ptr<base::Value> NetLog::Entry::ParametersToValue() const { |
184 if (data_->parameters_callback) | 185 if (data_->parameters_callback) |
185 return data_->parameters_callback->Run(capture_mode_); | 186 return data_->parameters_callback->Run(capture_mode_); |
186 return nullptr; | 187 return nullptr; |
187 } | 188 } |
188 | 189 |
189 NetLog::EntryData::EntryData(EventType type, | 190 NetLog::EntryData::EntryData(NetLogEventType type, |
190 Source source, | 191 Source source, |
191 EventPhase phase, | 192 NetLogEventPhase phase, |
192 base::TimeTicks time, | 193 base::TimeTicks time, |
193 const ParametersCallback* parameters_callback) | 194 const ParametersCallback* parameters_callback) |
194 : type(type), | 195 : type(type), |
195 source(source), | 196 source(source), |
196 phase(phase), | 197 phase(phase), |
197 time(time), | 198 time(time), |
198 parameters_callback(parameters_callback) { | 199 parameters_callback(parameters_callback) { |
199 } | 200 } |
200 | 201 |
201 NetLog::EntryData::~EntryData() { | 202 NetLog::EntryData::~EntryData() { |
(...skipping 28 matching lines...) Expand all Loading... |
230 void NetLog::ThreadSafeObserver::OnAddEntryData(const EntryData& entry_data) { | 231 void NetLog::ThreadSafeObserver::OnAddEntryData(const EntryData& entry_data) { |
231 OnAddEntry(Entry(&entry_data, capture_mode())); | 232 OnAddEntry(Entry(&entry_data, capture_mode())); |
232 } | 233 } |
233 | 234 |
234 NetLog::NetLog() : last_id_(0), is_capturing_(0) { | 235 NetLog::NetLog() : last_id_(0), is_capturing_(0) { |
235 } | 236 } |
236 | 237 |
237 NetLog::~NetLog() { | 238 NetLog::~NetLog() { |
238 } | 239 } |
239 | 240 |
240 void NetLog::AddGlobalEntry(EventType type) { | 241 void NetLog::AddGlobalEntry(NetLogEventType type) { |
241 AddEntry(type, Source(NetLog::SOURCE_NONE, NextID()), NetLog::PHASE_NONE, | 242 AddEntry(type, Source(NetLogSourceType::NONE, NextID()), |
242 NULL); | 243 NetLogEventPhase::NONE, NULL); |
243 } | 244 } |
244 | 245 |
245 void NetLog::AddGlobalEntry( | 246 void NetLog::AddGlobalEntry( |
246 EventType type, | 247 NetLogEventType type, |
247 const NetLog::ParametersCallback& parameters_callback) { | 248 const NetLog::ParametersCallback& parameters_callback) { |
248 AddEntry(type, Source(NetLog::SOURCE_NONE, NextID()), NetLog::PHASE_NONE, | 249 AddEntry(type, Source(NetLogSourceType::NONE, NextID()), |
249 ¶meters_callback); | 250 NetLogEventPhase::NONE, ¶meters_callback); |
250 } | 251 } |
251 | 252 |
252 uint32_t NetLog::NextID() { | 253 uint32_t NetLog::NextID() { |
253 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); | 254 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); |
254 } | 255 } |
255 | 256 |
256 bool NetLog::IsCapturing() const { | 257 bool NetLog::IsCapturing() const { |
257 return base::subtle::NoBarrier_Load(&is_capturing_) != 0; | 258 return base::subtle::NoBarrier_Load(&is_capturing_) != 0; |
258 } | 259 } |
259 | 260 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 observers_.might_have_observers() ? 1 : 0); | 295 observers_.might_have_observers() ? 1 : 0); |
295 } | 296 } |
296 | 297 |
297 // static | 298 // static |
298 std::string NetLog::TickCountToString(const base::TimeTicks& time) { | 299 std::string NetLog::TickCountToString(const base::TimeTicks& time) { |
299 int64_t delta_time = (time - base::TimeTicks()).InMilliseconds(); | 300 int64_t delta_time = (time - base::TimeTicks()).InMilliseconds(); |
300 return base::Int64ToString(delta_time); | 301 return base::Int64ToString(delta_time); |
301 } | 302 } |
302 | 303 |
303 // static | 304 // static |
304 const char* NetLog::EventTypeToString(EventType event) { | 305 const char* NetLog::EventTypeToString(NetLogEventType event) { |
305 switch (event) { | 306 switch (event) { |
306 #define EVENT_TYPE(label) \ | 307 #define EVENT_TYPE(label) \ |
307 case TYPE_##label: \ | 308 case NetLogEventType::label: \ |
308 return #label; | 309 return #label; |
309 #include "net/log/net_log_event_type_list.h" | 310 #include "net/log/net_log_event_type_list.h" |
310 #undef EVENT_TYPE | 311 #undef EVENT_TYPE |
311 default: | 312 default: |
312 NOTREACHED(); | 313 NOTREACHED(); |
313 return NULL; | 314 return NULL; |
314 } | 315 } |
315 } | 316 } |
316 | 317 |
317 // static | 318 // static |
318 base::Value* NetLog::GetEventTypesAsValue() { | 319 base::Value* NetLog::GetEventTypesAsValue() { |
319 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 320 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
320 for (int i = 0; i < EVENT_COUNT; ++i) { | 321 for (int i = 0; i < static_cast<int>(NetLogEventType::COUNT); ++i) { |
321 dict->SetInteger(EventTypeToString(static_cast<EventType>(i)), i); | 322 dict->SetInteger(EventTypeToString(static_cast<NetLogEventType>(i)), i); |
322 } | 323 } |
323 return dict.release(); | 324 return dict.release(); |
324 } | 325 } |
325 | 326 |
326 // static | 327 // static |
327 const char* NetLog::SourceTypeToString(SourceType source) { | 328 const char* NetLog::SourceTypeToString(NetLogSourceType source) { |
328 switch (source) { | 329 switch (source) { |
329 #define SOURCE_TYPE(label) \ | 330 #define SOURCE_TYPE(label) \ |
330 case SOURCE_##label: \ | 331 case NetLogSourceType::label: \ |
331 return #label; | 332 return #label; |
332 #include "net/log/net_log_source_type_list.h" | 333 #include "net/log/net_log_source_type_list.h" |
333 #undef SOURCE_TYPE | 334 #undef SOURCE_TYPE |
334 default: | 335 default: |
335 NOTREACHED(); | 336 NOTREACHED(); |
336 return NULL; | 337 return NULL; |
337 } | 338 } |
338 } | 339 } |
339 | 340 |
340 // static | 341 // static |
341 base::Value* NetLog::GetSourceTypesAsValue() { | 342 base::Value* NetLog::GetSourceTypesAsValue() { |
342 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 343 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
343 for (int i = 0; i < SOURCE_COUNT; ++i) { | 344 for (int i = 0; i < static_cast<int>(NetLogSourceType::COUNT); ++i) { |
344 dict->SetInteger(SourceTypeToString(static_cast<SourceType>(i)), i); | 345 dict->SetInteger(SourceTypeToString(static_cast<NetLogSourceType>(i)), i); |
345 } | 346 } |
346 return dict.release(); | 347 return dict.release(); |
347 } | 348 } |
348 | 349 |
349 // static | 350 // static |
350 const char* NetLog::EventPhaseToString(EventPhase phase) { | 351 const char* NetLog::EventPhaseToString(NetLogEventPhase phase) { |
351 switch (phase) { | 352 switch (phase) { |
352 case PHASE_BEGIN: | 353 case NetLogEventPhase::BEGIN: |
353 return "PHASE_BEGIN"; | 354 return "PHASE_BEGIN"; |
354 case PHASE_END: | 355 case NetLogEventPhase::END: |
355 return "PHASE_END"; | 356 return "PHASE_END"; |
356 case PHASE_NONE: | 357 case NetLogEventPhase::NONE: |
357 return "PHASE_NONE"; | 358 return "PHASE_NONE"; |
358 } | 359 } |
359 NOTREACHED(); | 360 NOTREACHED(); |
360 return NULL; | 361 return NULL; |
361 } | 362 } |
362 | 363 |
363 // static | 364 // static |
364 NetLog::ParametersCallback NetLog::BoolCallback(const char* name, bool value) { | 365 NetLog::ParametersCallback NetLog::BoolCallback(const char* name, bool value) { |
365 return base::Bind(&NetLogBoolCallback, name, value); | 366 return base::Bind(&NetLogBoolCallback, name, value); |
366 } | 367 } |
(...skipping 23 matching lines...) Expand all Loading... |
390 return base::Bind(&NetLogCharStringCallback, name, value); | 391 return base::Bind(&NetLogCharStringCallback, name, value); |
391 } | 392 } |
392 | 393 |
393 // static | 394 // static |
394 NetLog::ParametersCallback NetLog::StringCallback(const char* name, | 395 NetLog::ParametersCallback NetLog::StringCallback(const char* name, |
395 const base::string16* value) { | 396 const base::string16* value) { |
396 DCHECK(value); | 397 DCHECK(value); |
397 return base::Bind(&NetLogString16Callback, name, value); | 398 return base::Bind(&NetLogString16Callback, name, value); |
398 } | 399 } |
399 | 400 |
400 void NetLog::AddEntry(EventType type, | 401 void NetLog::AddEntry(NetLogEventType type, |
401 const Source& source, | 402 const Source& source, |
402 EventPhase phase, | 403 NetLogEventPhase phase, |
403 const NetLog::ParametersCallback* parameters_callback) { | 404 const NetLog::ParametersCallback* parameters_callback) { |
404 if (!IsCapturing()) | 405 if (!IsCapturing()) |
405 return; | 406 return; |
406 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), | 407 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), |
407 parameters_callback); | 408 parameters_callback); |
408 | 409 |
409 // Notify all of the log observers. | 410 // Notify all of the log observers. |
410 base::AutoLock lock(lock_); | 411 base::AutoLock lock(lock_); |
411 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); | 412 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); |
412 } | 413 } |
413 | 414 |
414 BoundNetLog::~BoundNetLog() { | 415 BoundNetLog::~BoundNetLog() { |
415 liveness_ = DEAD; | 416 liveness_ = DEAD; |
416 } | 417 } |
417 | 418 |
418 void BoundNetLog::AddEntry(NetLog::EventType type, | 419 void BoundNetLog::AddEntry(NetLogEventType type, |
419 NetLog::EventPhase phase) const { | 420 NetLogEventPhase phase) const { |
420 CrashIfInvalid(); | 421 CrashIfInvalid(); |
421 | 422 |
422 if (!net_log_) | 423 if (!net_log_) |
423 return; | 424 return; |
424 net_log_->AddEntry(type, source_, phase, NULL); | 425 net_log_->AddEntry(type, source_, phase, NULL); |
425 } | 426 } |
426 | 427 |
427 void BoundNetLog::AddEntry( | 428 void BoundNetLog::AddEntry( |
428 NetLog::EventType type, | 429 NetLogEventType type, |
429 NetLog::EventPhase phase, | 430 NetLogEventPhase phase, |
430 const NetLog::ParametersCallback& get_parameters) const { | 431 const NetLog::ParametersCallback& get_parameters) const { |
431 CrashIfInvalid(); | 432 CrashIfInvalid(); |
432 | 433 |
433 if (!net_log_) | 434 if (!net_log_) |
434 return; | 435 return; |
435 net_log_->AddEntry(type, source_, phase, &get_parameters); | 436 net_log_->AddEntry(type, source_, phase, &get_parameters); |
436 } | 437 } |
437 | 438 |
438 void BoundNetLog::AddEvent(NetLog::EventType type) const { | 439 void BoundNetLog::AddEvent(NetLogEventType type) const { |
439 AddEntry(type, NetLog::PHASE_NONE); | 440 AddEntry(type, NetLogEventPhase::NONE); |
440 } | 441 } |
441 | 442 |
442 void BoundNetLog::AddEvent( | 443 void BoundNetLog::AddEvent( |
443 NetLog::EventType type, | 444 NetLogEventType type, |
444 const NetLog::ParametersCallback& get_parameters) const { | 445 const NetLog::ParametersCallback& get_parameters) const { |
445 AddEntry(type, NetLog::PHASE_NONE, get_parameters); | 446 AddEntry(type, NetLogEventPhase::NONE, get_parameters); |
446 } | 447 } |
447 | 448 |
448 void BoundNetLog::BeginEvent(NetLog::EventType type) const { | 449 void BoundNetLog::BeginEvent(NetLogEventType type) const { |
449 AddEntry(type, NetLog::PHASE_BEGIN); | 450 AddEntry(type, NetLogEventPhase::BEGIN); |
450 } | 451 } |
451 | 452 |
452 void BoundNetLog::BeginEvent( | 453 void BoundNetLog::BeginEvent( |
453 NetLog::EventType type, | 454 NetLogEventType type, |
454 const NetLog::ParametersCallback& get_parameters) const { | 455 const NetLog::ParametersCallback& get_parameters) const { |
455 AddEntry(type, NetLog::PHASE_BEGIN, get_parameters); | 456 AddEntry(type, NetLogEventPhase::BEGIN, get_parameters); |
456 } | 457 } |
457 | 458 |
458 void BoundNetLog::EndEvent(NetLog::EventType type) const { | 459 void BoundNetLog::EndEvent(NetLogEventType type) const { |
459 AddEntry(type, NetLog::PHASE_END); | 460 AddEntry(type, NetLogEventPhase::END); |
460 } | 461 } |
461 | 462 |
462 void BoundNetLog::EndEvent( | 463 void BoundNetLog::EndEvent( |
463 NetLog::EventType type, | 464 NetLogEventType type, |
464 const NetLog::ParametersCallback& get_parameters) const { | 465 const NetLog::ParametersCallback& get_parameters) const { |
465 AddEntry(type, NetLog::PHASE_END, get_parameters); | 466 AddEntry(type, NetLogEventPhase::END, get_parameters); |
466 } | 467 } |
467 | 468 |
468 void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type, | 469 void BoundNetLog::AddEventWithNetErrorCode(NetLogEventType event_type, |
469 int net_error) const { | 470 int net_error) const { |
470 DCHECK_NE(ERR_IO_PENDING, net_error); | 471 DCHECK_NE(ERR_IO_PENDING, net_error); |
471 if (net_error >= 0) { | 472 if (net_error >= 0) { |
472 AddEvent(event_type); | 473 AddEvent(event_type); |
473 } else { | 474 } else { |
474 AddEvent(event_type, NetLog::IntCallback("net_error", net_error)); | 475 AddEvent(event_type, NetLog::IntCallback("net_error", net_error)); |
475 } | 476 } |
476 } | 477 } |
477 | 478 |
478 void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type, | 479 void BoundNetLog::EndEventWithNetErrorCode(NetLogEventType event_type, |
479 int net_error) const { | 480 int net_error) const { |
480 DCHECK_NE(ERR_IO_PENDING, net_error); | 481 DCHECK_NE(ERR_IO_PENDING, net_error); |
481 if (net_error >= 0) { | 482 if (net_error >= 0) { |
482 EndEvent(event_type); | 483 EndEvent(event_type); |
483 } else { | 484 } else { |
484 EndEvent(event_type, NetLog::IntCallback("net_error", net_error)); | 485 EndEvent(event_type, NetLog::IntCallback("net_error", net_error)); |
485 } | 486 } |
486 } | 487 } |
487 | 488 |
488 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, | 489 void BoundNetLog::AddByteTransferEvent(NetLogEventType event_type, |
489 int byte_count, | 490 int byte_count, |
490 const char* bytes) const { | 491 const char* bytes) const { |
491 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); | 492 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); |
492 } | 493 } |
493 | 494 |
494 bool BoundNetLog::IsCapturing() const { | 495 bool BoundNetLog::IsCapturing() const { |
495 CrashIfInvalid(); | 496 CrashIfInvalid(); |
496 return net_log_ && net_log_->IsCapturing(); | 497 return net_log_ && net_log_->IsCapturing(); |
497 } | 498 } |
498 | 499 |
499 // static | 500 // static |
500 BoundNetLog BoundNetLog::Make(NetLog* net_log, NetLog::SourceType source_type) { | 501 BoundNetLog BoundNetLog::Make(NetLog* net_log, NetLogSourceType source_type) { |
501 if (!net_log) | 502 if (!net_log) |
502 return BoundNetLog(); | 503 return BoundNetLog(); |
503 | 504 |
504 NetLog::Source source(source_type, net_log->NextID()); | 505 NetLog::Source source(source_type, net_log->NextID()); |
505 return BoundNetLog(source, net_log); | 506 return BoundNetLog(source, net_log); |
506 } | 507 } |
507 | 508 |
508 void BoundNetLog::CrashIfInvalid() const { | 509 void BoundNetLog::CrashIfInvalid() const { |
509 Liveness liveness = liveness_; | 510 Liveness liveness = liveness_; |
510 | 511 |
511 if (liveness == ALIVE) | 512 if (liveness == ALIVE) |
512 return; | 513 return; |
513 | 514 |
514 base::debug::Alias(&liveness); | 515 base::debug::Alias(&liveness); |
515 CHECK_EQ(ALIVE, liveness); | 516 CHECK_EQ(ALIVE, liveness); |
516 } | 517 } |
517 | 518 |
518 } // namespace net | 519 } // namespace net |
OLD | NEW |