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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/alias.h" | 8 #include "base/debug/alias.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 } | 198 } |
199 | 199 |
200 NetLog* NetLog::ThreadSafeObserver::net_log() const { | 200 NetLog* NetLog::ThreadSafeObserver::net_log() const { |
201 return net_log_; | 201 return net_log_; |
202 } | 202 } |
203 | 203 |
204 void NetLog::ThreadSafeObserver::OnAddEntryData(const EntryData& entry_data) { | 204 void NetLog::ThreadSafeObserver::OnAddEntryData(const EntryData& entry_data) { |
205 OnAddEntry(Entry(&entry_data, capture_mode())); | 205 OnAddEntry(Entry(&entry_data, capture_mode())); |
206 } | 206 } |
207 | 207 |
208 NetLog::NetLog() | 208 NetLog::NetLog() : last_id_(0), is_capturing_(0) { |
209 : last_id_(0), | |
210 effective_capture_mode_int32_( | |
211 NetLogCaptureMode::None().ToInternalValue()) { | |
212 } | 209 } |
213 | 210 |
214 NetLog::~NetLog() { | 211 NetLog::~NetLog() { |
215 } | 212 } |
216 | 213 |
217 void NetLog::AddGlobalEntry(EventType type) { | 214 void NetLog::AddGlobalEntry(EventType type) { |
218 AddEntry(type, Source(NetLog::SOURCE_NONE, NextID()), NetLog::PHASE_NONE, | 215 AddEntry(type, Source(NetLog::SOURCE_NONE, NextID()), NetLog::PHASE_NONE, |
219 NULL); | 216 NULL); |
220 } | 217 } |
221 | 218 |
222 void NetLog::AddGlobalEntry( | 219 void NetLog::AddGlobalEntry( |
223 EventType type, | 220 EventType type, |
224 const NetLog::ParametersCallback& parameters_callback) { | 221 const NetLog::ParametersCallback& parameters_callback) { |
225 AddEntry(type, Source(NetLog::SOURCE_NONE, NextID()), NetLog::PHASE_NONE, | 222 AddEntry(type, Source(NetLog::SOURCE_NONE, NextID()), NetLog::PHASE_NONE, |
226 ¶meters_callback); | 223 ¶meters_callback); |
227 } | 224 } |
228 | 225 |
229 uint32 NetLog::NextID() { | 226 uint32 NetLog::NextID() { |
230 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); | 227 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); |
231 } | 228 } |
232 | 229 |
233 NetLogCaptureMode NetLog::GetCaptureMode() const { | 230 bool NetLog::IsCapturing() const { |
234 base::subtle::Atomic32 capture_mode = | 231 return base::subtle::NoBarrier_Load(&is_capturing_) != 0; |
235 base::subtle::NoBarrier_Load(&effective_capture_mode_int32_); | |
236 return NetLogCaptureMode::FromInternalValue(capture_mode); | |
237 } | 232 } |
238 | 233 |
239 void NetLog::DeprecatedAddObserver(NetLog::ThreadSafeObserver* observer, | 234 void NetLog::DeprecatedAddObserver(NetLog::ThreadSafeObserver* observer, |
240 NetLogCaptureMode capture_mode) { | 235 NetLogCaptureMode capture_mode) { |
241 DCHECK(capture_mode.enabled()); | |
242 | |
243 base::AutoLock lock(lock_); | 236 base::AutoLock lock(lock_); |
244 | 237 |
245 DCHECK(!observer->net_log_); | 238 DCHECK(!observer->net_log_); |
246 DCHECK(!observer->capture_mode_.enabled()); | |
247 observers_.AddObserver(observer); | 239 observers_.AddObserver(observer); |
248 observer->net_log_ = this; | 240 observer->net_log_ = this; |
249 observer->capture_mode_ = capture_mode; | 241 observer->capture_mode_ = capture_mode; |
250 UpdateCaptureMode(); | 242 UpdateIsCapturing(); |
251 } | 243 } |
252 | 244 |
253 void NetLog::SetObserverCaptureMode(NetLog::ThreadSafeObserver* observer, | 245 void NetLog::SetObserverCaptureMode(NetLog::ThreadSafeObserver* observer, |
254 NetLogCaptureMode capture_mode) { | 246 NetLogCaptureMode capture_mode) { |
255 DCHECK(capture_mode.enabled()); | |
256 base::AutoLock lock(lock_); | 247 base::AutoLock lock(lock_); |
257 | 248 |
258 DCHECK(observers_.HasObserver(observer)); | 249 DCHECK(observers_.HasObserver(observer)); |
259 DCHECK_EQ(this, observer->net_log_); | 250 DCHECK_EQ(this, observer->net_log_); |
260 DCHECK(observer->capture_mode_.enabled()); | |
261 observer->capture_mode_ = capture_mode; | 251 observer->capture_mode_ = capture_mode; |
262 UpdateCaptureMode(); | |
263 } | 252 } |
264 | 253 |
265 void NetLog::DeprecatedRemoveObserver(NetLog::ThreadSafeObserver* observer) { | 254 void NetLog::DeprecatedRemoveObserver(NetLog::ThreadSafeObserver* observer) { |
266 base::AutoLock lock(lock_); | 255 base::AutoLock lock(lock_); |
267 | 256 |
268 DCHECK(observers_.HasObserver(observer)); | 257 DCHECK(observers_.HasObserver(observer)); |
269 DCHECK_EQ(this, observer->net_log_); | 258 DCHECK_EQ(this, observer->net_log_); |
270 DCHECK(observer->capture_mode_.enabled()); | |
271 observers_.RemoveObserver(observer); | 259 observers_.RemoveObserver(observer); |
272 observer->net_log_ = NULL; | 260 observer->net_log_ = NULL; |
273 observer->capture_mode_ = NetLogCaptureMode(); | 261 observer->capture_mode_ = NetLogCaptureMode(); |
274 UpdateCaptureMode(); | 262 UpdateIsCapturing(); |
275 } | 263 } |
276 | 264 |
277 void NetLog::UpdateCaptureMode() { | 265 void NetLog::UpdateIsCapturing() { |
278 lock_.AssertAcquired(); | 266 lock_.AssertAcquired(); |
279 | 267 base::subtle::NoBarrier_Store(&is_capturing_, |
280 // Accumulate the capture mode of all the observers to find the maximum level. | 268 observers_.might_have_observers() ? 1 : 0); |
281 NetLogCaptureMode new_capture_mode = NetLogCaptureMode::None(); | |
282 ObserverListBase<ThreadSafeObserver>::Iterator it(&observers_); | |
283 ThreadSafeObserver* observer; | |
284 while ((observer = it.GetNext()) != NULL) { | |
285 new_capture_mode = | |
286 NetLogCaptureMode::Max(new_capture_mode, observer->capture_mode()); | |
287 } | |
288 base::subtle::NoBarrier_Store(&effective_capture_mode_int32_, | |
289 new_capture_mode.ToInternalValue()); | |
290 } | 269 } |
291 | 270 |
292 // static | 271 // static |
293 std::string NetLog::TickCountToString(const base::TimeTicks& time) { | 272 std::string NetLog::TickCountToString(const base::TimeTicks& time) { |
294 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); | 273 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); |
295 return base::Int64ToString(delta_time); | 274 return base::Int64ToString(delta_time); |
296 } | 275 } |
297 | 276 |
298 // static | 277 // static |
299 const char* NetLog::EventTypeToString(EventType event) { | 278 const char* NetLog::EventTypeToString(EventType event) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 NetLog::ParametersCallback NetLog::StringCallback(const char* name, | 362 NetLog::ParametersCallback NetLog::StringCallback(const char* name, |
384 const base::string16* value) { | 363 const base::string16* value) { |
385 DCHECK(value); | 364 DCHECK(value); |
386 return base::Bind(&NetLogString16Callback, name, value); | 365 return base::Bind(&NetLogString16Callback, name, value); |
387 } | 366 } |
388 | 367 |
389 void NetLog::AddEntry(EventType type, | 368 void NetLog::AddEntry(EventType type, |
390 const Source& source, | 369 const Source& source, |
391 EventPhase phase, | 370 EventPhase phase, |
392 const NetLog::ParametersCallback* parameters_callback) { | 371 const NetLog::ParametersCallback* parameters_callback) { |
393 if (!GetCaptureMode().enabled()) | 372 if (!IsCapturing()) |
394 return; | 373 return; |
395 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), | 374 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), |
396 parameters_callback); | 375 parameters_callback); |
397 | 376 |
398 // Notify all of the log observers. | 377 // Notify all of the log observers. |
399 base::AutoLock lock(lock_); | 378 base::AutoLock lock(lock_); |
400 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); | 379 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); |
401 } | 380 } |
402 | 381 |
403 BoundNetLog::~BoundNetLog() { | 382 BoundNetLog::~BoundNetLog() { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error)); | 452 EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error)); |
474 } | 453 } |
475 } | 454 } |
476 | 455 |
477 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, | 456 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, |
478 int byte_count, | 457 int byte_count, |
479 const char* bytes) const { | 458 const char* bytes) const { |
480 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); | 459 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); |
481 } | 460 } |
482 | 461 |
483 NetLogCaptureMode BoundNetLog::GetCaptureMode() const { | 462 bool BoundNetLog::IsCapturing() const { |
484 CrashIfInvalid(); | 463 CrashIfInvalid(); |
485 | 464 return net_log_ && net_log_->IsCapturing(); |
486 if (net_log_) | |
487 return net_log_->GetCaptureMode(); | |
488 return NetLogCaptureMode(); | |
489 } | 465 } |
490 | 466 |
491 // static | 467 // static |
492 BoundNetLog BoundNetLog::Make(NetLog* net_log, NetLog::SourceType source_type) { | 468 BoundNetLog BoundNetLog::Make(NetLog* net_log, NetLog::SourceType source_type) { |
493 if (!net_log) | 469 if (!net_log) |
494 return BoundNetLog(); | 470 return BoundNetLog(); |
495 | 471 |
496 NetLog::Source source(source_type, net_log->NextID()); | 472 NetLog::Source source(source_type, net_log->NextID()); |
497 return BoundNetLog(source, net_log); | 473 return BoundNetLog(source, net_log); |
498 } | 474 } |
499 | 475 |
500 void BoundNetLog::CrashIfInvalid() const { | 476 void BoundNetLog::CrashIfInvalid() const { |
501 Liveness liveness = liveness_; | 477 Liveness liveness = liveness_; |
502 | 478 |
503 if (liveness == ALIVE) | 479 if (liveness == ALIVE) |
504 return; | 480 return; |
505 | 481 |
506 base::debug::Alias(&liveness); | 482 base::debug::Alias(&liveness); |
507 CHECK_EQ(ALIVE, liveness); | 483 CHECK_EQ(ALIVE, liveness); |
508 } | 484 } |
509 | 485 |
510 } // namespace net | 486 } // namespace net |
OLD | NEW |