OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 NET_BASE_CAPTURING_NET_LOG_H_ | 5 #ifndef NET_BASE_CAPTURING_NET_LOG_H_ |
6 #define NET_BASE_CAPTURING_NET_LOG_H_ | 6 #define NET_BASE_CAPTURING_NET_LOG_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 class DictionaryValue; | 21 class DictionaryValue; |
22 } | 22 } |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 // CapturingNetLog is an implementation of NetLog that saves messages to a | 26 // CapturingNetLog is a NetLog which instantiates Observer that saves messages |
27 // bounded buffer. It is intended for testing only, and is part of the | 27 // to a bounded buffer. It is intended for testing only, and is part of the |
28 // net_test_support project. | 28 // net_test_support project. This is provided for convinience and compatilbility |
| 29 // with the old unittests. |
29 class CapturingNetLog : public NetLog { | 30 class CapturingNetLog : public NetLog { |
30 public: | 31 public: |
31 struct CapturedEntry { | 32 struct CapturedEntry { |
32 CapturedEntry(EventType type, | 33 CapturedEntry(EventType type, |
33 const base::TimeTicks& time, | 34 const base::TimeTicks& time, |
34 Source source, | 35 Source source, |
35 EventPhase phase, | 36 EventPhase phase, |
36 scoped_ptr<base::DictionaryValue> params); | 37 scoped_ptr<base::DictionaryValue> params); |
37 // Copy constructor needed to store in a std::vector because of the | 38 // Copy constructor needed to store in a std::vector because of the |
38 // scoped_ptr. | 39 // scoped_ptr. |
(...skipping 25 matching lines...) Expand all Loading... |
64 EventPhase phase; | 65 EventPhase phase; |
65 scoped_ptr<base::DictionaryValue> params; | 66 scoped_ptr<base::DictionaryValue> params; |
66 }; | 67 }; |
67 | 68 |
68 // Ordered set of entries that were logged. | 69 // Ordered set of entries that were logged. |
69 typedef std::vector<CapturedEntry> CapturedEntryList; | 70 typedef std::vector<CapturedEntry> CapturedEntryList; |
70 | 71 |
71 CapturingNetLog(); | 72 CapturingNetLog(); |
72 virtual ~CapturingNetLog(); | 73 virtual ~CapturingNetLog(); |
73 | 74 |
74 // Returns the list of all entries in the log. | 75 void SetLogLevel(LogLevel log_level); |
| 76 |
| 77 // Below methods are forwarded to capturing_net_log_observer_. |
75 void GetEntries(CapturedEntryList* entry_list) const; | 78 void GetEntries(CapturedEntryList* entry_list) const; |
76 | 79 void GetEntriesForSource(Source source, CapturedEntryList* entry_list) const; |
77 // Fills |entry_list| with all entries in the log from the specified Source. | |
78 void GetEntriesForSource(NetLog::Source source, | |
79 CapturedEntryList* entry_list) const; | |
80 | |
81 // Returns number of entries in the log. | |
82 size_t GetSize() const; | 80 size_t GetSize() const; |
83 | |
84 void Clear(); | 81 void Clear(); |
85 | 82 |
86 void SetLogLevel(NetLog::LogLevel log_level); | 83 private: |
| 84 // Observer is an implementation of NetLog::ThreadSafeObserver |
| 85 // that saves messages to a bounded buffer. It is intended for testing only, |
| 86 // and is part of the net_test_support project. |
| 87 class Observer : public NetLog::ThreadSafeObserver { |
| 88 public: |
| 89 Observer(); |
| 90 virtual ~Observer(); |
87 | 91 |
88 // NetLog implementation: | 92 // Returns the list of all entries in the log. |
89 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; | 93 void GetEntries(CapturedEntryList* entry_list) const; |
90 virtual uint32 NextID() OVERRIDE; | |
91 virtual LogLevel GetLogLevel() const OVERRIDE; | |
92 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, | |
93 LogLevel log_level) OVERRIDE; | |
94 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, | |
95 LogLevel log_level) OVERRIDE; | |
96 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; | |
97 | 94 |
98 private: | 95 // Fills |entry_list| with all entries in the log from the specified Source. |
99 // Needs to be "mutable" so can use it in GetEntries(). | 96 void GetEntriesForSource(Source source, |
100 mutable base::Lock lock_; | 97 CapturedEntryList* entry_list) const; |
101 | 98 |
102 // Last assigned source ID. Incremented to get the next one. | 99 // Returns number of entries in the log. |
103 base::subtle::Atomic32 last_id_; | 100 size_t GetSize() const; |
104 | 101 |
105 CapturedEntryList captured_entries_; | 102 void Clear(); |
106 | 103 |
107 NetLog::LogLevel log_level_; | 104 private: |
| 105 // ThreadSafeObserver implementation: |
| 106 virtual void OnAddEntry(const Entry& entry) OVERRIDE; |
| 107 |
| 108 // Needs to be "mutable" so can use it in GetEntries(). |
| 109 mutable base::Lock lock_; |
| 110 |
| 111 CapturedEntryList captured_entries_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 114 }; |
| 115 |
| 116 Observer capturing_net_log_observer_; |
108 | 117 |
109 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); | 118 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); |
110 }; | 119 }; |
111 | 120 |
112 // Helper class that exposes a similar API as BoundNetLog, but uses a | 121 // Helper class that exposes a similar API as BoundNetLog, but uses a |
113 // CapturingNetLog rather than the more generic NetLog. | 122 // CapturingNetLog rather than the more generic NetLog. |
114 // | 123 // |
115 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the | 124 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the |
116 // bound() method. | 125 // bound() method. |
117 class CapturingBoundNetLog { | 126 class CapturingBoundNetLog { |
(...skipping 23 matching lines...) Expand all Loading... |
141 private: | 150 private: |
142 CapturingNetLog capturing_net_log_; | 151 CapturingNetLog capturing_net_log_; |
143 const BoundNetLog net_log_; | 152 const BoundNetLog net_log_; |
144 | 153 |
145 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); | 154 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); |
146 }; | 155 }; |
147 | 156 |
148 } // namespace net | 157 } // namespace net |
149 | 158 |
150 #endif // NET_BASE_CAPTURING_NET_LOG_H_ | 159 #endif // NET_BASE_CAPTURING_NET_LOG_H_ |
OLD | NEW |