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), has_observers_(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::HasObservers() const { |
234 base::subtle::Atomic32 capture_mode = | 231 base::subtle::Atomic32 has_observers = |
235 base::subtle::NoBarrier_Load(&effective_capture_mode_int32_); | 232 base::subtle::NoBarrier_Load(&has_observers_); |
236 return NetLogCaptureMode::FromInternalValue(capture_mode); | 233 return has_observers; |
237 } | 234 } |
238 | 235 |
239 void NetLog::DeprecatedAddObserver(NetLog::ThreadSafeObserver* observer, | 236 void NetLog::DeprecatedAddObserver(NetLog::ThreadSafeObserver* observer, |
240 NetLogCaptureMode capture_mode) { | 237 NetLogCaptureMode capture_mode) { |
241 DCHECK(capture_mode.enabled()); | 238 DCHECK(capture_mode.enabled()); |
242 | 239 |
243 base::AutoLock lock(lock_); | 240 base::AutoLock lock(lock_); |
244 | 241 |
245 DCHECK(!observer->net_log_); | 242 DCHECK(!observer->net_log_); |
246 DCHECK(!observer->capture_mode_.enabled()); | 243 DCHECK(!observer->capture_mode_.enabled()); |
247 observers_.AddObserver(observer); | 244 observers_.AddObserver(observer); |
248 observer->net_log_ = this; | 245 observer->net_log_ = this; |
249 observer->capture_mode_ = capture_mode; | 246 observer->capture_mode_ = capture_mode; |
250 UpdateCaptureMode(); | 247 UpdateHasObservers(); |
251 } | 248 } |
252 | 249 |
253 void NetLog::SetObserverCaptureMode(NetLog::ThreadSafeObserver* observer, | 250 void NetLog::SetObserverCaptureMode(NetLog::ThreadSafeObserver* observer, |
254 NetLogCaptureMode capture_mode) { | 251 NetLogCaptureMode capture_mode) { |
255 DCHECK(capture_mode.enabled()); | 252 DCHECK(capture_mode.enabled()); |
256 base::AutoLock lock(lock_); | 253 base::AutoLock lock(lock_); |
257 | 254 |
258 DCHECK(observers_.HasObserver(observer)); | 255 DCHECK(observers_.HasObserver(observer)); |
259 DCHECK_EQ(this, observer->net_log_); | 256 DCHECK_EQ(this, observer->net_log_); |
260 DCHECK(observer->capture_mode_.enabled()); | 257 DCHECK(observer->capture_mode_.enabled()); |
261 observer->capture_mode_ = capture_mode; | 258 observer->capture_mode_ = capture_mode; |
mmenke
2015/05/05 19:45:15
Do we ever explicitly disallow observers from watc
eroman
2015/05/05 22:54:37
I updated the CL and just removed NetLogCaptureMod
| |
262 UpdateCaptureMode(); | |
263 } | 259 } |
264 | 260 |
265 void NetLog::DeprecatedRemoveObserver(NetLog::ThreadSafeObserver* observer) { | 261 void NetLog::DeprecatedRemoveObserver(NetLog::ThreadSafeObserver* observer) { |
266 base::AutoLock lock(lock_); | 262 base::AutoLock lock(lock_); |
267 | 263 |
268 DCHECK(observers_.HasObserver(observer)); | 264 DCHECK(observers_.HasObserver(observer)); |
269 DCHECK_EQ(this, observer->net_log_); | 265 DCHECK_EQ(this, observer->net_log_); |
270 DCHECK(observer->capture_mode_.enabled()); | 266 DCHECK(observer->capture_mode_.enabled()); |
271 observers_.RemoveObserver(observer); | 267 observers_.RemoveObserver(observer); |
272 observer->net_log_ = NULL; | 268 observer->net_log_ = NULL; |
273 observer->capture_mode_ = NetLogCaptureMode(); | 269 observer->capture_mode_ = NetLogCaptureMode(); |
274 UpdateCaptureMode(); | 270 UpdateHasObservers(); |
275 } | 271 } |
276 | 272 |
277 void NetLog::UpdateCaptureMode() { | 273 void NetLog::UpdateHasObservers() { |
278 lock_.AssertAcquired(); | 274 lock_.AssertAcquired(); |
279 | 275 base::subtle::NoBarrier_Store( |
280 // Accumulate the capture mode of all the observers to find the maximum level. | 276 &has_observers_, static_cast<int>(observers_.might_have_observers())); |
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 } | 277 } |
291 | 278 |
292 // static | 279 // static |
293 std::string NetLog::TickCountToString(const base::TimeTicks& time) { | 280 std::string NetLog::TickCountToString(const base::TimeTicks& time) { |
294 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); | 281 int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); |
295 return base::Int64ToString(delta_time); | 282 return base::Int64ToString(delta_time); |
296 } | 283 } |
297 | 284 |
298 // static | 285 // static |
299 const char* NetLog::EventTypeToString(EventType event) { | 286 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, | 370 NetLog::ParametersCallback NetLog::StringCallback(const char* name, |
384 const base::string16* value) { | 371 const base::string16* value) { |
385 DCHECK(value); | 372 DCHECK(value); |
386 return base::Bind(&NetLogString16Callback, name, value); | 373 return base::Bind(&NetLogString16Callback, name, value); |
387 } | 374 } |
388 | 375 |
389 void NetLog::AddEntry(EventType type, | 376 void NetLog::AddEntry(EventType type, |
390 const Source& source, | 377 const Source& source, |
391 EventPhase phase, | 378 EventPhase phase, |
392 const NetLog::ParametersCallback* parameters_callback) { | 379 const NetLog::ParametersCallback* parameters_callback) { |
393 if (!GetCaptureMode().enabled()) | 380 if (!HasObservers()) |
394 return; | 381 return; |
395 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), | 382 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), |
396 parameters_callback); | 383 parameters_callback); |
397 | 384 |
398 // Notify all of the log observers. | 385 // Notify all of the log observers. |
399 base::AutoLock lock(lock_); | 386 base::AutoLock lock(lock_); |
400 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); | 387 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); |
401 } | 388 } |
402 | 389 |
403 BoundNetLog::~BoundNetLog() { | 390 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)); | 460 EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error)); |
474 } | 461 } |
475 } | 462 } |
476 | 463 |
477 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, | 464 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, |
478 int byte_count, | 465 int byte_count, |
479 const char* bytes) const { | 466 const char* bytes) const { |
480 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); | 467 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); |
481 } | 468 } |
482 | 469 |
483 NetLogCaptureMode BoundNetLog::GetCaptureMode() const { | 470 bool BoundNetLog::HasObservers() const { |
484 CrashIfInvalid(); | 471 CrashIfInvalid(); |
485 | 472 return net_log_ && net_log_->HasObservers(); |
486 if (net_log_) | |
487 return net_log_->GetCaptureMode(); | |
488 return NetLogCaptureMode(); | |
489 } | 473 } |
490 | 474 |
491 // static | 475 // static |
492 BoundNetLog BoundNetLog::Make(NetLog* net_log, NetLog::SourceType source_type) { | 476 BoundNetLog BoundNetLog::Make(NetLog* net_log, NetLog::SourceType source_type) { |
493 if (!net_log) | 477 if (!net_log) |
494 return BoundNetLog(); | 478 return BoundNetLog(); |
495 | 479 |
496 NetLog::Source source(source_type, net_log->NextID()); | 480 NetLog::Source source(source_type, net_log->NextID()); |
497 return BoundNetLog(source, net_log); | 481 return BoundNetLog(source, net_log); |
498 } | 482 } |
499 | 483 |
500 void BoundNetLog::CrashIfInvalid() const { | 484 void BoundNetLog::CrashIfInvalid() const { |
501 Liveness liveness = liveness_; | 485 Liveness liveness = liveness_; |
502 | 486 |
503 if (liveness == ALIVE) | 487 if (liveness == ALIVE) |
504 return; | 488 return; |
505 | 489 |
506 base::debug::Alias(&liveness); | 490 base::debug::Alias(&liveness); |
507 CHECK_EQ(ALIVE, liveness); | 491 CHECK_EQ(ALIVE, liveness); |
508 } | 492 } |
509 | 493 |
510 } // namespace net | 494 } // namespace net |
OLD | NEW |