OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/net/chrome_net_log.h" | 5 #include "chrome/browser/net/chrome_net_log.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/browser/browser_thread.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/net/load_timing_observer.h" | 13 #include "chrome/browser/net/load_timing_observer.h" |
14 #include "chrome/browser/net/net_log_logger.h" | 14 #include "chrome/browser/net/net_log_logger.h" |
15 #include "chrome/browser/net/passive_log_collector.h" | 15 #include "chrome/browser/net/passive_log_collector.h" |
16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
17 | 17 |
18 ChromeNetLog::Observer::Observer(LogLevel log_level) : log_level_(log_level) {} | 18 ChromeNetLog::ThreadSafeObserver::ThreadSafeObserver(LogLevel log_level) |
| 19 : net_log_(NULL), |
| 20 log_level_(log_level) { |
| 21 } |
19 | 22 |
20 net::NetLog::LogLevel ChromeNetLog::Observer::log_level() const { | 23 ChromeNetLog::ThreadSafeObserver::~ThreadSafeObserver() { |
| 24 DCHECK(!net_log_); |
| 25 } |
| 26 |
| 27 net::NetLog::LogLevel ChromeNetLog::ThreadSafeObserver::log_level() const { |
21 return log_level_; | 28 return log_level_; |
22 } | 29 } |
23 | 30 |
24 void ChromeNetLog::Observer::set_log_level(net::NetLog::LogLevel log_level) { | 31 void ChromeNetLog::ThreadSafeObserver::AssertNetLogLockAcquired() const { |
25 log_level_ = log_level; | 32 if (net_log_) |
| 33 net_log_->lock_.AssertAcquired(); |
26 } | 34 } |
27 | 35 |
| 36 void ChromeNetLog::ThreadSafeObserver::SetLogLevel( |
| 37 net::NetLog::LogLevel log_level) { |
| 38 DCHECK(net_log_); |
| 39 AutoLock lock(net_log_->lock_); |
| 40 log_level_ = log_level; |
| 41 net_log_->UpdateLogLevel_(); |
| 42 } |
| 43 |
| 44 ChromeNetLog::Entry::Entry(uint32 order, |
| 45 net::NetLog::EventType type, |
| 46 const base::TimeTicks& time, |
| 47 net::NetLog::Source source, |
| 48 net::NetLog::EventPhase phase, |
| 49 net::NetLog::EventParameters* params) |
| 50 : order(order), |
| 51 type(type), |
| 52 time(time), |
| 53 source(source), |
| 54 phase(phase), |
| 55 params(params) { |
| 56 } |
| 57 |
| 58 ChromeNetLog::Entry::~Entry() {} |
| 59 |
28 ChromeNetLog::ChromeNetLog() | 60 ChromeNetLog::ChromeNetLog() |
29 : next_id_(1), | 61 : last_id_(0), |
| 62 log_level_(LOG_BASIC), |
30 passive_collector_(new PassiveLogCollector), | 63 passive_collector_(new PassiveLogCollector), |
31 load_timing_observer_(new LoadTimingObserver) { | 64 load_timing_observer_(new LoadTimingObserver) { |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
33 AddObserver(passive_collector_.get()); | 65 AddObserver(passive_collector_.get()); |
34 AddObserver(load_timing_observer_.get()); | 66 AddObserver(load_timing_observer_.get()); |
35 | 67 |
36 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 68 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
37 if (command_line.HasSwitch(switches::kLogNetLog)) { | 69 if (command_line.HasSwitch(switches::kLogNetLog)) { |
38 net_log_logger_.reset(new NetLogLogger()); | 70 net_log_logger_.reset(new NetLogLogger()); |
39 AddObserver(net_log_logger_.get()); | 71 AddObserver(net_log_logger_.get()); |
40 } | 72 } |
41 } | 73 } |
42 | 74 |
43 ChromeNetLog::~ChromeNetLog() { | 75 ChromeNetLog::~ChromeNetLog() { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
45 RemoveObserver(passive_collector_.get()); | 76 RemoveObserver(passive_collector_.get()); |
46 RemoveObserver(load_timing_observer_.get()); | 77 RemoveObserver(load_timing_observer_.get()); |
47 if (net_log_logger_.get()) { | 78 if (net_log_logger_.get()) { |
48 RemoveObserver(net_log_logger_.get()); | 79 RemoveObserver(net_log_logger_.get()); |
49 } | 80 } |
50 } | 81 } |
51 | 82 |
52 void ChromeNetLog::AddEntry(EventType type, | 83 void ChromeNetLog::AddEntry(EventType type, |
53 const base::TimeTicks& time, | 84 const base::TimeTicks& time, |
54 const Source& source, | 85 const Source& source, |
55 EventPhase phase, | 86 EventPhase phase, |
56 EventParameters* params) { | 87 EventParameters* params) { |
57 // This must be invoked when we're on the IO thread, or if the IO thread's | 88 AutoLock lock(lock_); |
58 // message loop isn't valid. The later can happen if this is invoked when the | |
59 // IOThread is shuting down the MessageLoop. | |
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) || | |
61 !BrowserThread::IsMessageLoopValid(BrowserThread::IO)); | |
62 | 89 |
63 // Notify all of the log observers. | 90 // Notify all of the log observers. |
64 FOR_EACH_OBSERVER(Observer, observers_, | 91 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, |
65 OnAddEntry(type, time, source, phase, params)); | 92 OnAddEntry(type, time, source, phase, params)); |
66 } | 93 } |
67 | 94 |
68 uint32 ChromeNetLog::NextID() { | 95 uint32 ChromeNetLog::NextID() { |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 96 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); |
70 return next_id_++; | |
71 } | 97 } |
72 | 98 |
73 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const { | 99 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 100 base::subtle::Atomic32 log_level = base::subtle::NoBarrier_Load(&log_level_); |
| 101 return static_cast<net::NetLog::LogLevel>(log_level); |
| 102 } |
| 103 |
| 104 void ChromeNetLog::AddObserver(ThreadSafeObserver* observer) { |
| 105 AutoLock lock(lock_); |
| 106 AddObserverWhileLockHeld(observer); |
| 107 } |
| 108 |
| 109 void ChromeNetLog::RemoveObserver(ThreadSafeObserver* observer) { |
| 110 AutoLock lock(lock_); |
| 111 DCHECK_EQ(observer->net_log_, this); |
| 112 observer->net_log_ = NULL; |
| 113 observers_.RemoveObserver(observer); |
| 114 UpdateLogLevel_(); |
| 115 } |
| 116 |
| 117 void ChromeNetLog::AddObserverAndGetAllPassivelyCapturedEvents( |
| 118 ThreadSafeObserver* observer, EntryList* passive_entries) { |
| 119 AutoLock lock(lock_); |
| 120 AddObserverWhileLockHeld(observer); |
| 121 passive_collector_->GetAllCapturedEvents(passive_entries); |
| 122 } |
| 123 |
| 124 void ChromeNetLog::GetAllPassivelyCapturedEvents(EntryList* passive_entries) { |
| 125 AutoLock lock(lock_); |
| 126 passive_collector_->GetAllCapturedEvents(passive_entries); |
| 127 } |
| 128 |
| 129 void ChromeNetLog::ClearAllPassivelyCapturedEvents() { |
| 130 AutoLock lock(lock_); |
| 131 passive_collector_->Clear(); |
| 132 } |
| 133 |
| 134 void ChromeNetLog::UpdateLogLevel_() { |
| 135 lock_.AssertAcquired(); |
75 | 136 |
76 // Look through all the observers and find the finest granularity | 137 // Look through all the observers and find the finest granularity |
77 // log level (higher values of the enum imply *lower* log levels). | 138 // log level (higher values of the enum imply *lower* log levels). |
78 LogLevel log_level = LOG_BASIC; | 139 LogLevel new_log_level = LOG_BASIC; |
79 ObserverListBase<Observer>::Iterator it(observers_); | 140 ObserverListBase<ThreadSafeObserver>::Iterator it(observers_); |
80 Observer* observer; | 141 ThreadSafeObserver* observer; |
81 while ((observer = it.GetNext()) != NULL) { | 142 while ((observer = it.GetNext()) != NULL) { |
82 log_level = std::min(log_level, observer->log_level()); | 143 new_log_level = std::min(new_log_level, observer->log_level()); |
83 } | 144 } |
84 return log_level; | 145 base::subtle::NoBarrier_Store(&log_level_, new_log_level); |
85 } | 146 } |
86 | 147 |
87 void ChromeNetLog::AddObserver(Observer* observer) { | 148 void ChromeNetLog::AddObserverWhileLockHeld(ThreadSafeObserver* observer) { |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 149 DCHECK(!observer->net_log_); |
| 150 observer->net_log_ = this; |
89 observers_.AddObserver(observer); | 151 observers_.AddObserver(observer); |
| 152 UpdateLogLevel_(); |
90 } | 153 } |
91 | |
92 void ChromeNetLog::RemoveObserver(Observer* observer) { | |
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
94 observers_.RemoveObserver(observer); | |
95 } | |
OLD | NEW |