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