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_PASSIVE_LOG_COLLECTOR_H_ | 5 #ifndef CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ |
6 #define CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ | 6 #define CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| 11 #include "base/time.h" |
| 12 #include "base/ref_counted.h" |
11 #include "chrome/browser/net/chrome_net_log.h" | 13 #include "chrome/browser/net/chrome_net_log.h" |
12 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
13 | 15 |
14 class PassiveLogCollector : public ChromeNetLog::Observer { | 16 class PassiveLogCollector : public ChromeNetLog::Observer { |
15 public: | 17 public: |
| 18 // This structure encapsulates all of the parameters of a captured event, |
| 19 // including an "order" field that identifies when it was captured relative |
| 20 // to other events. |
| 21 struct Entry { |
| 22 Entry(int order, |
| 23 net::NetLog::EventType type, |
| 24 const base::TimeTicks& time, |
| 25 net::NetLog::Source source, |
| 26 net::NetLog::EventPhase phase, |
| 27 net::NetLog::EventParameters* extra_parameters) |
| 28 : order(order), type(type), time(time), source(source), phase(phase), |
| 29 extra_parameters(extra_parameters) { |
| 30 } |
| 31 |
| 32 int order; |
| 33 net::NetLog::EventType type; |
| 34 base::TimeTicks time; |
| 35 net::NetLog::Source source; |
| 36 net::NetLog::EventPhase phase; |
| 37 scoped_refptr<net::NetLog::EventParameters> extra_parameters; |
| 38 }; |
| 39 |
| 40 typedef std::vector<Entry> EntryList; |
| 41 |
16 struct RequestInfo { | 42 struct RequestInfo { |
17 RequestInfo() : num_entries_truncated(0) {} | 43 RequestInfo() : num_entries_truncated(0) {} |
18 std::string url; | 44 std::string url; |
19 net::CapturingNetLog::EntryList entries; | 45 EntryList entries; |
20 size_t num_entries_truncated; | 46 size_t num_entries_truncated; |
21 }; | 47 }; |
22 | 48 |
23 typedef std::vector<RequestInfo> RequestInfoList; | 49 typedef std::vector<RequestInfo> RequestInfoList; |
24 | 50 |
25 // This class stores and manages the passively logged information for | 51 // This class stores and manages the passively logged information for |
26 // URLRequests/SocketStreams/ConnectJobs. | 52 // URLRequests/SocketStreams/ConnectJobs. |
27 class RequestTrackerBase { | 53 class RequestTrackerBase { |
28 public: | 54 public: |
29 explicit RequestTrackerBase(size_t max_graveyard_size); | 55 explicit RequestTrackerBase(size_t max_graveyard_size); |
30 | 56 |
31 void OnAddEntry(const net::CapturingNetLog::Entry& entry); | 57 void OnAddEntry(const Entry& entry); |
32 | 58 |
33 RequestInfoList GetLiveRequests() const; | 59 RequestInfoList GetLiveRequests() const; |
34 void ClearRecentlyDeceased(); | 60 void ClearRecentlyDeceased(); |
35 RequestInfoList GetRecentlyDeceased() const; | 61 RequestInfoList GetRecentlyDeceased() const; |
36 void SetUnbounded(bool unbounded); | 62 void SetUnbounded(bool unbounded); |
37 | 63 |
38 bool IsUnbounded() const { return is_unbounded_; } | 64 bool IsUnbounded() const { return is_unbounded_; } |
39 | 65 |
40 void Clear(); | 66 void Clear(); |
41 | 67 |
| 68 // Appends all the captured entries to |out|. The ordering is undefined. |
| 69 void AppendAllEntries(EntryList* out) const; |
| 70 |
42 const RequestInfo* GetRequestInfoFromGraveyard(int id) const; | 71 const RequestInfo* GetRequestInfoFromGraveyard(int id) const; |
43 | 72 |
44 protected: | 73 protected: |
45 enum Action { | 74 enum Action { |
46 ACTION_NONE, | 75 ACTION_NONE, |
47 ACTION_DELETE, | 76 ACTION_DELETE, |
48 ACTION_MOVE_TO_GRAVEYARD, | 77 ACTION_MOVE_TO_GRAVEYARD, |
49 }; | 78 }; |
50 | 79 |
51 // Updates |out_info| with the information from |entry|. Returns an action | 80 // Updates |out_info| with the information from |entry|. Returns an action |
52 // to perform for this map entry on completion. | 81 // to perform for this map entry on completion. |
53 virtual Action DoAddEntry(const net::CapturingNetLog::Entry& entry, | 82 virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info) = 0; |
54 RequestInfo* out_info) = 0; | |
55 | 83 |
56 bool is_unbounded() const { return is_unbounded_; } | 84 bool is_unbounded() const { return is_unbounded_; } |
57 | 85 |
58 private: | 86 private: |
59 typedef base::hash_map<int, RequestInfo> SourceIDToInfoMap; | 87 typedef base::hash_map<int, RequestInfo> SourceIDToInfoMap; |
60 | 88 |
61 void RemoveFromLiveRequests(int source_id); | 89 void RemoveFromLiveRequests(int source_id); |
62 void InsertIntoGraveyard(const RequestInfo& info); | 90 void InsertIntoGraveyard(const RequestInfo& info); |
63 | 91 |
64 SourceIDToInfoMap live_requests_; | 92 SourceIDToInfoMap live_requests_; |
65 size_t max_graveyard_size_; | 93 size_t max_graveyard_size_; |
66 size_t next_graveyard_index_; | 94 size_t next_graveyard_index_; |
67 RequestInfoList graveyard_; | 95 RequestInfoList graveyard_; |
68 bool is_unbounded_; | 96 bool is_unbounded_; |
69 | 97 |
70 DISALLOW_COPY_AND_ASSIGN(RequestTrackerBase); | 98 DISALLOW_COPY_AND_ASSIGN(RequestTrackerBase); |
71 }; | 99 }; |
72 | 100 |
73 // Specialization of RequestTrackerBase for handling ConnectJobs. | 101 // Specialization of RequestTrackerBase for handling ConnectJobs. |
74 class ConnectJobTracker : public RequestTrackerBase { | 102 class ConnectJobTracker : public RequestTrackerBase { |
75 public: | 103 public: |
76 static const size_t kMaxGraveyardSize; | 104 static const size_t kMaxGraveyardSize; |
77 | 105 |
78 ConnectJobTracker(); | 106 ConnectJobTracker(); |
79 | 107 |
80 protected: | 108 protected: |
81 virtual Action DoAddEntry(const net::CapturingNetLog::Entry& entry, | 109 virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info); |
82 RequestInfo* out_info); | |
83 private: | 110 private: |
84 DISALLOW_COPY_AND_ASSIGN(ConnectJobTracker); | 111 DISALLOW_COPY_AND_ASSIGN(ConnectJobTracker); |
85 }; | 112 }; |
86 | 113 |
87 // Specialization of RequestTrackerBase for handling URLRequest/SocketStream. | 114 // Specialization of RequestTrackerBase for handling URLRequest/SocketStream. |
88 class RequestTracker : public RequestTrackerBase { | 115 class RequestTracker : public RequestTrackerBase { |
89 public: | 116 public: |
90 static const size_t kMaxGraveyardSize; | 117 static const size_t kMaxGraveyardSize; |
91 static const size_t kMaxGraveyardURLSize; | 118 static const size_t kMaxGraveyardURLSize; |
92 | 119 |
93 explicit RequestTracker(ConnectJobTracker* connect_job_tracker); | 120 explicit RequestTracker(ConnectJobTracker* connect_job_tracker); |
94 | 121 |
95 protected: | 122 protected: |
96 virtual Action DoAddEntry(const net::CapturingNetLog::Entry& entry, | 123 virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info); |
97 RequestInfo* out_info); | |
98 | 124 |
99 private: | 125 private: |
100 // Searches through |connect_job_tracker_| for information on the | 126 // Searches through |connect_job_tracker_| for information on the |
101 // ConnectJob specified in |entry|, and appends it to |live_entry|. | 127 // ConnectJob specified in |entry|, and appends it to |live_entry|. |
102 void AddConnectJobInfo(const net::CapturingNetLog::Entry& entry, | 128 void AddConnectJobInfo(const Entry& entry, RequestInfo* live_entry); |
103 RequestInfo* live_entry); | |
104 | 129 |
105 ConnectJobTracker* connect_job_tracker_; | 130 ConnectJobTracker* connect_job_tracker_; |
106 | 131 |
107 DISALLOW_COPY_AND_ASSIGN(RequestTracker); | 132 DISALLOW_COPY_AND_ASSIGN(RequestTracker); |
108 }; | 133 }; |
109 | 134 |
110 // Tracks the log entries for the last seen SOURCE_INIT_PROXY_RESOLVER. | 135 // Tracks the log entries for the last seen SOURCE_INIT_PROXY_RESOLVER. |
111 class InitProxyResolverTracker { | 136 class InitProxyResolverTracker { |
112 public: | 137 public: |
113 InitProxyResolverTracker(); | 138 InitProxyResolverTracker(); |
114 | 139 |
115 void OnAddEntry(const net::CapturingNetLog::Entry& entry); | 140 void OnAddEntry(const Entry& entry); |
116 | 141 |
117 const net::CapturingNetLog::EntryList& entries() const { | 142 const EntryList& entries() const { |
118 return entries_; | 143 return entries_; |
119 } | 144 } |
120 | 145 |
121 private: | 146 private: |
122 net::CapturingNetLog::EntryList entries_; | 147 EntryList entries_; |
123 DISALLOW_COPY_AND_ASSIGN(InitProxyResolverTracker); | 148 DISALLOW_COPY_AND_ASSIGN(InitProxyResolverTracker); |
124 }; | 149 }; |
125 | 150 |
126 PassiveLogCollector(); | 151 PassiveLogCollector(); |
127 ~PassiveLogCollector(); | 152 ~PassiveLogCollector(); |
128 | 153 |
129 // Observer implementation: | 154 // Observer implementation: |
130 virtual void OnAddEntry(net::NetLog::EventType type, | 155 virtual void OnAddEntry(net::NetLog::EventType type, |
131 const base::TimeTicks& time, | 156 const base::TimeTicks& time, |
132 const net::NetLog::Source& source, | 157 const net::NetLog::Source& source, |
133 net::NetLog::EventPhase phase, | 158 net::NetLog::EventPhase phase, |
134 net::NetLog::EventParameters* extra_parameters); | 159 net::NetLog::EventParameters* extra_parameters); |
135 | 160 |
136 // Clears all of the passively logged data. | 161 // Clears all of the passively logged data. |
137 void Clear(); | 162 void Clear(); |
138 | 163 |
139 RequestTracker* url_request_tracker() { | 164 RequestTracker* url_request_tracker() { |
140 return &url_request_tracker_; | 165 return &url_request_tracker_; |
141 } | 166 } |
142 | 167 |
143 RequestTracker* socket_stream_tracker() { | 168 RequestTracker* socket_stream_tracker() { |
144 return &socket_stream_tracker_; | 169 return &socket_stream_tracker_; |
145 } | 170 } |
146 | 171 |
147 InitProxyResolverTracker* init_proxy_resolver_tracker() { | 172 InitProxyResolverTracker* init_proxy_resolver_tracker() { |
148 return &init_proxy_resolver_tracker_; | 173 return &init_proxy_resolver_tracker_; |
149 } | 174 } |
150 | 175 |
| 176 // Fills |out| with the full list of events that have been passively |
| 177 // captured. The list is ordered by capture time. |
| 178 void GetAllCapturedEvents(EntryList* out) const; |
| 179 |
151 private: | 180 private: |
152 ConnectJobTracker connect_job_tracker_; | 181 ConnectJobTracker connect_job_tracker_; |
153 RequestTracker url_request_tracker_; | 182 RequestTracker url_request_tracker_; |
154 RequestTracker socket_stream_tracker_; | 183 RequestTracker socket_stream_tracker_; |
155 InitProxyResolverTracker init_proxy_resolver_tracker_; | 184 InitProxyResolverTracker init_proxy_resolver_tracker_; |
156 | 185 |
| 186 // The count of how many events have flowed through this log. Used to set the |
| 187 // "order" field on captured events. |
| 188 int num_events_seen_; |
| 189 |
157 DISALLOW_COPY_AND_ASSIGN(PassiveLogCollector); | 190 DISALLOW_COPY_AND_ASSIGN(PassiveLogCollector); |
158 }; | 191 }; |
159 | 192 |
160 #endif // CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ | 193 #endif // CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_ |
OLD | NEW |