Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/net/chrome_net_log.h

Issue 4118004: Update NetLog to be thread safe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Response to comments Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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::Observer functions may be called by an Observer's OnAddEntry()
24 // 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. 30 // TODO(eroman): Move this default observer out of ChromeNetLog.
eroman 2010/11/17 05:59:02 Could you do me a favor and delete this comment wh
25 // 31 //
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 {
eroman 2010/11/17 05:59:02 I see, you moved this out of PassiveLogCollector b
mmenke 2010/11/17 21:42:14 Reason I didn't do that is because EntryList is an
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 Observer {
eroman 2010/11/17 05:59:02 I suggest renaming this to "ThreadSafeObserver".
mmenke 2010/11/17 21:42:14 Done.
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.
33 // 61 //
34 // Typical observers should specify LOG_BASIC. 62 // Typical observers should specify LOG_BASIC.
35 // 63 //
36 // Observers that need to see the full granularity of events can 64 // Observers that need to see the full granularity of events can
37 // specify LOG_ALL. However doing so will have performance consequences, 65 // specify LOG_ALL. However doing so will have performance consequences,
38 // and may cause PassiveLogCollector to use more memory than anticiapted. 66 // and may cause PassiveLogCollector to use more memory than anticipated.
eroman 2010/11/17 05:59:02 thanks for fixing the typo!
67 //
68 // Observers will be called on the same thread an entry is added on,
69 // and are responsible for ensuring their own thread safety.
39 explicit Observer(LogLevel log_level); 70 explicit Observer(LogLevel log_level);
40 71
41 virtual ~Observer() {} 72 virtual ~Observer() {}
73
74 // It is illegal for an Observer to call any ChromeNetLog or
75 // ChromeNetLog::Observer functions in response to a call to OnAddEntry.
eroman 2010/11/17 05:59:02 I suggest additionaly re-iterating the threading m
42 virtual void OnAddEntry(EventType type, 76 virtual void OnAddEntry(EventType type,
43 const base::TimeTicks& time, 77 const base::TimeTicks& time,
44 const Source& source, 78 const Source& source,
45 EventPhase phase, 79 EventPhase phase,
46 EventParameters* params) = 0; 80 EventParameters* params) = 0;
47 LogLevel log_level() const; 81 LogLevel log_level() const;
48 protected: 82 protected:
49 void set_log_level(LogLevel log_level); 83 // |net_log| is the ChromeNetLog the Observer is currently observing,
84 // and cannot be NULL.
85 void SetLogLevel(LogLevel log_level, ChromeNetLog* net_log);
eroman 2010/11/17 05:59:02 Taking the ChromeNetLog* here isn't very intuitive
mmenke 2010/11/17 21:42:14 Done. Only thing I don't like about that is that
50 private: 86 private:
51 LogLevel log_level_; 87 LogLevel log_level_;
52 DISALLOW_COPY_AND_ASSIGN(Observer); 88 DISALLOW_COPY_AND_ASSIGN(Observer);
53 }; 89 };
54 90
55 ChromeNetLog(); 91 ChromeNetLog();
56 ~ChromeNetLog(); 92 ~ChromeNetLog();
57 93
58 // NetLog implementation: 94 // NetLog implementation:
59 virtual void AddEntry(EventType type, 95 virtual void AddEntry(EventType type,
60 const base::TimeTicks& time, 96 const base::TimeTicks& time,
61 const Source& source, 97 const Source& source,
62 EventPhase phase, 98 EventPhase phase,
63 EventParameters* params); 99 EventParameters* params);
64 virtual uint32 NextID(); 100 virtual uint32 NextID();
65 virtual LogLevel GetLogLevel() const; 101 virtual LogLevel GetLogLevel() const;
66 102
67 void AddObserver(Observer* observer); 103 void AddObserver(Observer* observer);
68 void RemoveObserver(Observer* observer); 104 void RemoveObserver(Observer* observer);
69 105
106 // Adds |observer| and writes all passively captured events to
107 // |passive_entries|. Guarantees that no events in |passive_entries| will be
108 // sent to |observer| and all future events that have yet been sent to the
109 // PassiveLogCollector will be sent to |observer|.
110 void AddObserverAndGetAllCapturedEvents(Observer* observer,
eroman 2010/11/17 05:59:02 I have a reputation for writing incredibly long fu
mmenke 2010/11/17 21:42:14 I'm fine with it, actually. Only thing I have aga
111 EntryList* passive_entries);
112
70 PassiveLogCollector* passive_collector() { 113 PassiveLogCollector* passive_collector() {
71 return passive_collector_.get(); 114 return passive_collector_.get();
72 } 115 }
73 116
74 LoadTimingObserver* load_timing_observer() { 117 LoadTimingObserver* load_timing_observer() {
75 return load_timing_observer_.get(); 118 return load_timing_observer_.get();
76 } 119 }
77 120
78 private: 121 private:
79 uint32 next_id_; 122 // Called whenever an Observer is added or removed, or changes its log level.
123 // Must have acquired |lock_| prior to calling.
124 void UpdateLogLevel_();
125
126 Lock lock_;
127
128 base::subtle::Atomic32 last_id_;
129 base::subtle::Atomic32 log_level_;
130
80 scoped_ptr<PassiveLogCollector> passive_collector_; 131 scoped_ptr<PassiveLogCollector> passive_collector_;
81 scoped_ptr<LoadTimingObserver> load_timing_observer_; 132 scoped_ptr<LoadTimingObserver> load_timing_observer_;
82 scoped_ptr<NetLogLogger> net_log_logger_; 133 scoped_ptr<NetLogLogger> net_log_logger_;
83 134
84 // Note that this needs to be "mutable" so we can iterate over the observer 135 ObserverList<Observer, true> observers_;
eroman 2010/11/17 05:59:02 Can you mention that |lock_| must be held whenever
85 // list in GetLogLevel().
86 mutable ObserverList<Observer, true> observers_;
87 136
88 DISALLOW_COPY_AND_ASSIGN(ChromeNetLog); 137 DISALLOW_COPY_AND_ASSIGN(ChromeNetLog);
89 }; 138 };
90 139
91 #endif // CHROME_BROWSER_NET_CHROME_NET_LOG_H_ 140 #endif // CHROME_BROWSER_NET_CHROME_NET_LOG_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698