Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_NET_CHROME_NET_LOG_H_ | 5 #ifndef CHROME_BROWSER_NET_CHROME_NET_LOG_H_ |
| 6 #define CHROME_BROWSER_NET_CHROME_NET_LOG_H_ | 6 #define CHROME_BROWSER_NET_CHROME_NET_LOG_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/atomicops.h" | |
| 9 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| 10 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "chrome/browser/browser_thread.h" | |
| 11 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 12 | 14 |
| 13 class LoadTimingObserver; | 15 class LoadTimingObserver; |
| 14 class NetLogLogger; | 16 class NetLogLogger; |
| 15 class PassiveLogCollector; | 17 class PassiveLogCollector; |
| 16 | 18 |
| 17 // ChromeNetLog is an implementation of NetLog that dispatches network log | 19 // ChromeNetLog is an implementation of NetLog that dispatches network log |
| 18 // messages to a list of observers. | 20 // messages to a list of observers. |
| 19 // | 21 // |
| 22 // All methods are threadsafe, with the exception that no ChromeNetLog or | |
| 23 // ChromeNetLog::ThreadSafeObserver functions may be called by an observer's | |
| 24 // OnAddEntry() method. | |
| 25 // | |
| 20 // By default, ChromeNetLog will attach the observer PassiveLogCollector which | 26 // By default, ChromeNetLog will attach the observer PassiveLogCollector which |
| 21 // will keep track of recent request information (which used when displaying | 27 // will keep track of recent request information (which used when displaying |
| 22 // the about:net-internals page). | 28 // the about:net-internals page). |
| 23 // | 29 // |
| 24 // TODO(eroman): Move this default observer out of ChromeNetLog. | |
| 25 // | |
| 26 class ChromeNetLog : public net::NetLog { | 30 class ChromeNetLog : public net::NetLog { |
| 27 public: | 31 public: |
| 32 // This structure encapsulates all of the parameters of an event, | |
| 33 // including an "order" field that identifies when it was captured relative | |
| 34 // to other events. | |
| 35 struct Entry { | |
| 36 Entry(uint32 order, | |
| 37 net::NetLog::EventType type, | |
| 38 const base::TimeTicks& time, | |
| 39 net::NetLog::Source source, | |
| 40 net::NetLog::EventPhase phase, | |
| 41 net::NetLog::EventParameters* params); | |
| 42 ~Entry(); | |
| 43 | |
| 44 uint32 order; | |
| 45 net::NetLog::EventType type; | |
| 46 base::TimeTicks time; | |
| 47 net::NetLog::Source source; | |
| 48 net::NetLog::EventPhase phase; | |
| 49 scoped_refptr<net::NetLog::EventParameters> params; | |
| 50 }; | |
| 51 | |
| 52 typedef std::vector<Entry> EntryList; | |
| 53 | |
| 28 // Interface for observing the events logged by the network stack. | 54 // Interface for observing the events logged by the network stack. |
| 29 class Observer { | 55 class ThreadSafeObserver { |
| 30 public: | 56 public: |
| 31 // Constructs an observer that wants to see network events, with | 57 // Constructs an observer that wants to see network events, with |
| 32 // the specified minimum event granularity. | 58 // the specified minimum event granularity. A ThreadSafeObserver can only |
| 59 // observe a single ChromeNetLog at a time. | |
| 33 // | 60 // |
| 34 // Typical observers should specify LOG_BASIC. | 61 // Typical observers should specify LOG_BASIC. |
| 35 // | 62 // |
| 36 // Observers that need to see the full granularity of events can | 63 // Observers that need to see the full granularity of events can |
| 37 // specify LOG_ALL. However doing so will have performance consequences, | 64 // specify LOG_ALL. However doing so will have performance consequences, |
| 38 // and may cause PassiveLogCollector to use more memory than anticiapted. | 65 // and may cause PassiveLogCollector to use more memory than anticipated. |
| 39 explicit Observer(LogLevel log_level); | 66 // |
| 67 // Observers will be called on the same thread an entry is added on, | |
| 68 // and are responsible for ensuring their own thread safety. | |
| 69 explicit ThreadSafeObserver(LogLevel log_level); | |
| 40 | 70 |
| 41 virtual ~Observer() {} | 71 virtual ~ThreadSafeObserver(); |
| 72 | |
| 73 // This method will be called on the thread that the event occurs on. It | |
| 74 // is the responsibility of the Observer to handle it in a threadsafe | |
| 75 // manner. | |
| 76 // | |
| 77 // It is illegal for an Observer to call any ChromeNetLog or | |
|
eroman
2010/11/18 18:04:03
nit: "an Observer" --> either:
"an observer" or
mmenke
2010/11/23 16:48:45
Thought I caught all of those.
| |
| 78 // ChromeNetLog::ThreadSafeObserver functions in response to a call to | |
| 79 // OnAddEntry. | |
| 42 virtual void OnAddEntry(EventType type, | 80 virtual void OnAddEntry(EventType type, |
| 43 const base::TimeTicks& time, | 81 const base::TimeTicks& time, |
| 44 const Source& source, | 82 const Source& source, |
| 45 EventPhase phase, | 83 EventPhase phase, |
| 46 EventParameters* params) = 0; | 84 EventParameters* params) = 0; |
| 47 LogLevel log_level() const; | 85 LogLevel log_level() const; |
| 48 protected: | 86 |
| 49 void set_log_level(LogLevel log_level); | 87 // This must only be called by ChromeNetLog. |
| 88 void set_net_log(ChromeNetLog* net_log); | |
|
eroman
2010/11/18 18:04:03
Can you make it private? (outter classes have dire
mmenke
2010/11/23 16:48:45
I tried that initially, but was getting a compile
| |
| 89 ChromeNetLog* net_log(); | |
| 90 protected: | |
| 91 // Can only be called when actively observing a ChromeNetLog. | |
| 92 void SetLogLevel(LogLevel log_level); | |
| 50 private: | 93 private: |
| 94 // ChromeNetLog currently being observed, if any. Set by ChromeNetLog's | |
| 95 // AddObserver and RemoveObserver methods. | |
| 96 ChromeNetLog* net_log_; | |
| 97 | |
| 51 LogLevel log_level_; | 98 LogLevel log_level_; |
| 52 DISALLOW_COPY_AND_ASSIGN(Observer); | 99 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); |
| 53 }; | 100 }; |
| 54 | 101 |
| 55 ChromeNetLog(); | 102 ChromeNetLog(); |
| 56 ~ChromeNetLog(); | 103 ~ChromeNetLog(); |
| 57 | 104 |
| 58 // NetLog implementation: | 105 // NetLog implementation: |
| 59 virtual void AddEntry(EventType type, | 106 virtual void AddEntry(EventType type, |
| 60 const base::TimeTicks& time, | 107 const base::TimeTicks& time, |
| 61 const Source& source, | 108 const Source& source, |
| 62 EventPhase phase, | 109 EventPhase phase, |
| 63 EventParameters* params); | 110 EventParameters* params); |
| 64 virtual uint32 NextID(); | 111 virtual uint32 NextID(); |
| 65 virtual LogLevel GetLogLevel() const; | 112 virtual LogLevel GetLogLevel() const; |
| 66 | 113 |
| 67 void AddObserver(Observer* observer); | 114 void AddObserver(ThreadSafeObserver* observer); |
| 68 void RemoveObserver(Observer* observer); | 115 void RemoveObserver(ThreadSafeObserver* observer); |
| 69 | 116 |
| 70 PassiveLogCollector* passive_collector() { | 117 // Adds |observer| and writes all passively captured events to |
| 71 return passive_collector_.get(); | 118 // |passive_entries|. Guarantees that no events in |passive_entries| will be |
| 72 } | 119 // sent to |observer| and all future events that have yet been sent to the |
| 120 // PassiveLogCollector will be sent to |observer|. | |
| 121 void AddObserverAndGetAllPassivelyCapturedEvents(ThreadSafeObserver* observer, | |
| 122 EntryList* passive_entries); | |
| 123 | |
| 124 void ClearAllPassivelyCapturedEvents(); | |
| 73 | 125 |
| 74 LoadTimingObserver* load_timing_observer() { | 126 LoadTimingObserver* load_timing_observer() { |
| 75 return load_timing_observer_.get(); | 127 return load_timing_observer_.get(); |
| 76 } | 128 } |
| 77 | 129 |
| 78 private: | 130 private: |
| 79 uint32 next_id_; | 131 // Called whenever an Observer is added or removed, or changes its log level. |
|
eroman
2010/11/18 18:04:03
same comment regarding the capitlized "Observer".
| |
| 132 // Must have acquired |lock_| prior to calling. | |
| 133 void UpdateLogLevel_(); | |
| 134 | |
| 135 Lock lock_; | |
| 136 | |
| 137 base::subtle::Atomic32 last_id_; | |
|
eroman
2010/11/18 18:04:03
Could you add a brief comment for this? (I realize
| |
| 138 base::subtle::Atomic32 log_level_; | |
| 139 | |
| 140 // Not threadsafe. Must only be used when |lock_| is acquired. | |
| 80 scoped_ptr<PassiveLogCollector> passive_collector_; | 141 scoped_ptr<PassiveLogCollector> passive_collector_; |
| 142 | |
| 81 scoped_ptr<LoadTimingObserver> load_timing_observer_; | 143 scoped_ptr<LoadTimingObserver> load_timing_observer_; |
| 82 scoped_ptr<NetLogLogger> net_log_logger_; | 144 scoped_ptr<NetLogLogger> net_log_logger_; |
| 83 | 145 |
| 84 // Note that this needs to be "mutable" so we can iterate over the observer | 146 // |lock_| must be acquired whenever reading or writing to this. |
| 85 // list in GetLogLevel(). | 147 ObserverList<ThreadSafeObserver, true> observers_; |
| 86 mutable ObserverList<Observer, true> observers_; | |
| 87 | 148 |
| 88 DISALLOW_COPY_AND_ASSIGN(ChromeNetLog); | 149 DISALLOW_COPY_AND_ASSIGN(ChromeNetLog); |
| 89 }; | 150 }; |
| 90 | 151 |
| 91 #endif // CHROME_BROWSER_NET_CHROME_NET_LOG_H_ | 152 #endif // CHROME_BROWSER_NET_CHROME_NET_LOG_H_ |
| OLD | NEW |