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" | |
12 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 13 #include "net/base/captured_net_log_entry.h" |
15 #include "base/memory/scoped_ptr.h" | 14 #include "net/base/capturing_net_log_observer.h" |
16 #include "base/synchronization/lock.h" | |
17 #include "base/time/time.h" | |
18 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
19 | 16 |
20 namespace base { | |
21 class DictionaryValue; | |
22 class ListValue; | |
23 } | |
24 | |
25 namespace net { | 17 namespace net { |
26 | 18 |
27 // CapturingNetLog is a NetLog which instantiates Observer that saves messages | 19 // CapturingNetLog is convenience class which combines a NetLog and a |
28 // to a bounded buffer. It is intended for testing only, and is part of the | 20 // CapturingNetLogObserver. It is intended for testing only, and is part of the |
29 // net_test_support project. This is provided for convinience and compatilbility | 21 // net_test_support project. |
30 // with the old unittests. | |
31 class CapturingNetLog : public NetLog { | 22 class CapturingNetLog : public NetLog { |
32 public: | 23 public: |
33 struct CapturedEntry { | 24 // TODO(mmenke): Get rid of these. |
34 CapturedEntry(EventType type, | 25 typedef CapturedNetLogEntry CapturedEntry; |
35 const base::TimeTicks& time, | 26 typedef CapturedNetLogEntry::List CapturedEntryList; |
36 Source source, | |
37 EventPhase phase, | |
38 scoped_ptr<base::DictionaryValue> params); | |
39 // Copy constructor needed to store in a std::vector because of the | |
40 // scoped_ptr. | |
41 CapturedEntry(const CapturedEntry& entry); | |
42 | |
43 ~CapturedEntry(); | |
44 | |
45 // Equality operator needed to store in a std::vector because of the | |
46 // scoped_ptr. | |
47 CapturedEntry& operator=(const CapturedEntry& entry); | |
48 | |
49 // Attempt to retrieve an value of the specified type with the given name | |
50 // from |params|. Returns true on success, false on failure. Does not | |
51 // modify |value| on failure. | |
52 bool GetStringValue(const std::string& name, std::string* value) const; | |
53 bool GetIntegerValue(const std::string& name, int* value) const; | |
54 bool GetListValue(const std::string& name, base::ListValue** value) const; | |
55 | |
56 // Same as GetIntegerValue, but returns the error code associated with a | |
57 // log entry. | |
58 bool GetNetErrorCode(int* value) const; | |
59 | |
60 // Returns the parameters as a JSON string, or empty string if there are no | |
61 // parameters. | |
62 std::string GetParamsJson() const; | |
63 | |
64 EventType type; | |
65 base::TimeTicks time; | |
66 Source source; | |
67 EventPhase phase; | |
68 scoped_ptr<base::DictionaryValue> params; | |
69 }; | |
70 | |
71 // Ordered set of entries that were logged. | |
72 typedef std::vector<CapturedEntry> CapturedEntryList; | |
73 | 27 |
74 CapturingNetLog(); | 28 CapturingNetLog(); |
75 ~CapturingNetLog() override; | 29 ~CapturingNetLog() override; |
76 | 30 |
77 void SetLogLevel(LogLevel log_level); | 31 void SetLogLevel(LogLevel log_level); |
78 | 32 |
79 // Below methods are forwarded to capturing_net_log_observer_. | 33 // Below methods are forwarded to capturing_net_log_observer_. |
80 void GetEntries(CapturedEntryList* entry_list) const; | 34 void GetEntries(CapturedEntryList* entry_list) const; |
81 void GetEntriesForSource(Source source, CapturedEntryList* entry_list) const; | 35 void GetEntriesForSource(Source source, CapturedEntryList* entry_list) const; |
82 size_t GetSize() const; | 36 size_t GetSize() const; |
83 void Clear(); | 37 void Clear(); |
84 | 38 |
85 private: | 39 private: |
86 // Observer is an implementation of NetLog::ThreadSafeObserver | 40 CapturingNetLogObserver capturing_net_log_observer_; |
87 // that saves messages to a bounded buffer. It is intended for testing only, | |
88 // and is part of the net_test_support project. | |
89 class Observer : public NetLog::ThreadSafeObserver { | |
90 public: | |
91 Observer(); | |
92 ~Observer() override; | |
93 | |
94 // Returns the list of all entries in the log. | |
95 void GetEntries(CapturedEntryList* entry_list) const; | |
96 | |
97 // Fills |entry_list| with all entries in the log from the specified Source. | |
98 void GetEntriesForSource(Source source, | |
99 CapturedEntryList* entry_list) const; | |
100 | |
101 // Returns number of entries in the log. | |
102 size_t GetSize() const; | |
103 | |
104 void Clear(); | |
105 | |
106 private: | |
107 // ThreadSafeObserver implementation: | |
108 void OnAddEntry(const Entry& entry) override; | |
109 | |
110 // Needs to be "mutable" so can use it in GetEntries(). | |
111 mutable base::Lock lock_; | |
112 | |
113 CapturedEntryList captured_entries_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(Observer); | |
116 }; | |
117 | |
118 Observer capturing_net_log_observer_; | |
119 | 41 |
120 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); | 42 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); |
121 }; | 43 }; |
122 | 44 |
123 // Helper class that exposes a similar API as BoundNetLog, but uses a | 45 // Helper class that exposes a similar API as BoundNetLog, but uses a |
124 // CapturingNetLog rather than the more generic NetLog. | 46 // CapturingNetLog rather than the more generic NetLog. |
125 // | 47 // |
126 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the | 48 // A CapturingBoundNetLog can easily be converted to a BoundNetLog using the |
127 // bound() method. | 49 // bound() method. |
128 class CapturingBoundNetLog { | 50 class CapturingBoundNetLog { |
129 public: | 51 public: |
130 CapturingBoundNetLog(); | 52 CapturingBoundNetLog(); |
131 ~CapturingBoundNetLog(); | 53 ~CapturingBoundNetLog(); |
132 | 54 |
133 // The returned BoundNetLog is only valid while |this| is alive. | 55 // The returned BoundNetLog is only valid while |this| is alive. |
134 BoundNetLog bound() const { return net_log_; } | 56 BoundNetLog bound() const { return net_log_; } |
135 | 57 |
136 // Fills |entry_list| with all entries in the log. | 58 // Fills |entry_list| with all entries in the log. |
(...skipping 15 matching lines...) Expand all Loading... |
152 private: | 74 private: |
153 CapturingNetLog capturing_net_log_; | 75 CapturingNetLog capturing_net_log_; |
154 const BoundNetLog net_log_; | 76 const BoundNetLog net_log_; |
155 | 77 |
156 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); | 78 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); |
157 }; | 79 }; |
158 | 80 |
159 } // namespace net | 81 } // namespace net |
160 | 82 |
161 #endif // NET_BASE_CAPTURING_NET_LOG_H_ | 83 #endif // NET_BASE_CAPTURING_NET_LOG_H_ |
OLD | NEW |