1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_
#define CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_
#include <string>
#include <vector>
#include "base/hash_tables.h"
#include "base/ref_counted.h"
#include "base/time.h"
#include "chrome/browser/net/chrome_net_log.h"
#include "net/base/net_log.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
class PassiveLogCollector : public ChromeNetLog::Observer {
public:
// This structure encapsulates all of the parameters of a captured event,
// including an "order" field that identifies when it was captured relative
// to other events.
struct Entry {
Entry(uint32 order,
net::NetLog::EventType type,
const base::TimeTicks& time,
net::NetLog::Source source,
net::NetLog::EventPhase phase,
net::NetLog::EventParameters* params)
: order(order), type(type), time(time), source(source), phase(phase),
params(params) {
}
uint32 order;
net::NetLog::EventType type;
base::TimeTicks time;
net::NetLog::Source source;
net::NetLog::EventPhase phase;
scoped_refptr<net::NetLog::EventParameters> params;
};
typedef std::vector<Entry> EntryList;
struct RequestInfo {
RequestInfo()
: source_id(net::NetLog::Source::kInvalidId),
num_entries_truncated(0),
total_bytes_transmitted(0),
total_bytes_received(0),
bytes_transmitted(0),
bytes_received(0),
last_tx_rx_position(0) {}
// Returns the URL that corresponds with this source. This is
// only meaningful for certain source types (URL_REQUEST, SOCKET_STREAM).
// For the rest, it will return an empty string.
std::string GetURL() const;
uint32 source_id;
EntryList entries;
size_t num_entries_truncated;
net::NetLog::Source subordinate_source;
// Only used in SocketTracker.
uint64 total_bytes_transmitted;
uint64 total_bytes_received;
uint64 bytes_transmitted;
uint64 bytes_received;
uint32 last_tx_rx_position; // The |order| of the last Tx or Rx entry.
base::TimeTicks last_tx_rx_time; // The |time| of the last Tx or Rx entry.
};
typedef std::vector<RequestInfo> RequestInfoList;
// This class stores and manages the passively logged information for
// URLRequests/SocketStreams/ConnectJobs.
class RequestTrackerBase {
public:
explicit RequestTrackerBase(size_t max_graveyard_size);
void OnAddEntry(const Entry& entry);
RequestInfoList GetLiveRequests() const;
void ClearRecentlyDeceased();
RequestInfoList GetRecentlyDeceased() const;
void Clear();
// Appends all the captured entries to |out|. The ordering is undefined.
void AppendAllEntries(EntryList* out) const;
protected:
enum Action {
ACTION_NONE,
ACTION_DELETE,
ACTION_MOVE_TO_GRAVEYARD,
};
// Finds a request, either in the live entries or the graveyard and returns
// it.
RequestInfo* GetRequestInfo(uint32 id);
// When GetLiveRequests() is called, RequestTrackerBase calls this method
// for each entry after adding it to the list which will be returned
// to the caller.
virtual void OnLiveRequest(RequestInfo* info) const {}
private:
typedef base::hash_map<uint32, RequestInfo> SourceIDToInfoMap;
// Updates |out_info| with the information from |entry|. Returns an action
// to perform for this map entry on completion.
virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info) = 0;
void RemoveFromLiveRequests(uint32 source_id);
void InsertIntoGraveyard(const RequestInfo& info);
SourceIDToInfoMap live_requests_;
size_t max_graveyard_size_;
size_t next_graveyard_index_;
RequestInfoList graveyard_;
DISALLOW_COPY_AND_ASSIGN(RequestTrackerBase);
};
// Specialization of RequestTrackerBase for handling ConnectJobs.
class ConnectJobTracker : public RequestTrackerBase {
public:
static const size_t kMaxGraveyardSize;
ConnectJobTracker();
void AppendLogEntries(RequestInfo* out_info, uint32 connect_id);
protected:
virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info);
private:
DISALLOW_COPY_AND_ASSIGN(ConnectJobTracker);
};
// Specialization of RequestTrackerBase for handling Sockets.
class SocketTracker : public RequestTrackerBase {
public:
static const size_t kMaxGraveyardSize;
SocketTracker();
void AppendLogEntries(RequestInfo* out_info, uint32 socket_id, bool clear);
protected:
virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info);
private:
void ClearInfo(RequestInfo* info);
DISALLOW_COPY_AND_ASSIGN(SocketTracker);
};
// Specialization of RequestTrackerBase for handling URLRequest/SocketStream.
class RequestTracker : public RequestTrackerBase {
public:
static const size_t kMaxGraveyardSize;
RequestTracker(ConnectJobTracker* connect_job_tracker,
SocketTracker* socket_tracker);
void IntegrateSubordinateSource(RequestInfo* info,
bool clear_entries) const;
protected:
virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info);
virtual void OnLiveRequest(RequestInfo* info) const {
IntegrateSubordinateSource(info, false);
}
private:
ConnectJobTracker* connect_job_tracker_;
SocketTracker* socket_tracker_;
DISALLOW_COPY_AND_ASSIGN(RequestTracker);
};
// Tracks the log entries for the last seen SOURCE_INIT_PROXY_RESOLVER.
class InitProxyResolverTracker {
public:
InitProxyResolverTracker();
void OnAddEntry(const Entry& entry);
const EntryList& entries() const {
return entries_;
}
private:
EntryList entries_;
DISALLOW_COPY_AND_ASSIGN(InitProxyResolverTracker);
};
// Tracks the log entries for the last seen SOURCE_SPDY_SESSION.
class SpdySessionTracker : public RequestTrackerBase {
public:
static const size_t kMaxGraveyardSize;
SpdySessionTracker();
protected:
virtual Action DoAddEntry(const Entry& entry, RequestInfo* out_info);
private:
DISALLOW_COPY_AND_ASSIGN(SpdySessionTracker);
};
PassiveLogCollector();
~PassiveLogCollector();
// Observer implementation:
virtual void OnAddEntry(net::NetLog::EventType type,
const base::TimeTicks& time,
const net::NetLog::Source& source,
net::NetLog::EventPhase phase,
net::NetLog::EventParameters* params);
// Clears all of the passively logged data.
void Clear();
RequestTracker* url_request_tracker() {
return &url_request_tracker_;
}
RequestTracker* socket_stream_tracker() {
return &socket_stream_tracker_;
}
InitProxyResolverTracker* init_proxy_resolver_tracker() {
return &init_proxy_resolver_tracker_;
}
SpdySessionTracker* spdy_session_tracker() {
return &spdy_session_tracker_;
}
// Fills |out| with the full list of events that have been passively
// captured. The list is ordered by capture time.
void GetAllCapturedEvents(EntryList* out) const;
private:
FRIEND_TEST(PassiveLogCollectorTest, LostConnectJob);
FRIEND_TEST(PassiveLogCollectorTest, LostSocket);
ConnectJobTracker connect_job_tracker_;
SocketTracker socket_tracker_;
RequestTracker url_request_tracker_;
RequestTracker socket_stream_tracker_;
InitProxyResolverTracker init_proxy_resolver_tracker_;
SpdySessionTracker spdy_session_tracker_;
// The count of how many events have flowed through this log. Used to set the
// "order" field on captured events.
uint32 num_events_seen_;
DISALLOW_COPY_AND_ASSIGN(PassiveLogCollector);
};
#endif // CHROME_BROWSER_NET_PASSIVE_LOG_COLLECTOR_H_
|