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