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

Side by Side Diff: content/browser/debugger/devtools_netlog_observer.cc

Issue 10399083: Make NetLog take in callbacks that return Values (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix merge error Created 8 years, 6 months 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
« no previous file with comments | « content/browser/debugger/devtools_netlog_observer.h ('k') | net/base/capturing_net_log.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "content/browser/debugger/devtools_netlog_observer.h" 5 #include "content/browser/debugger/devtools_netlog_observer.h"
6 6
7 #include "base/string_tokenizer.h" 7 #include "base/string_tokenizer.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/content_browser_client.h" 11 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/common/resource_response.h" 12 #include "content/public/common/resource_response.h"
13 #include "net/base/load_flags.h" 13 #include "net/base/load_flags.h"
14 #include "net/http/http_net_log_params.h"
15 #include "net/http/http_response_headers.h" 14 #include "net/http/http_response_headers.h"
16 #include "net/http/http_util.h" 15 #include "net/http/http_util.h"
17 #include "net/url_request/url_request.h" 16 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_netlog_params.h" 17 #include "net/url_request/url_request_netlog_params.h"
19 #include "webkit/glue/resource_loader_bridge.h" 18 #include "webkit/glue/resource_loader_bridge.h"
20 19
21 using content::BrowserThread; 20 using content::BrowserThread;
22 21
23 const size_t kMaxNumEntries = 1000; 22 const size_t kMaxNumEntries = 1000;
24 23
25 DevToolsNetLogObserver* DevToolsNetLogObserver::instance_ = NULL; 24 DevToolsNetLogObserver* DevToolsNetLogObserver::instance_ = NULL;
26 25
27 DevToolsNetLogObserver::DevToolsNetLogObserver() { 26 DevToolsNetLogObserver::DevToolsNetLogObserver() {
28 } 27 }
29 28
30 DevToolsNetLogObserver::~DevToolsNetLogObserver() { 29 DevToolsNetLogObserver::~DevToolsNetLogObserver() {
31 } 30 }
32 31
33 DevToolsNetLogObserver::ResourceInfo* 32 DevToolsNetLogObserver::ResourceInfo*
34 DevToolsNetLogObserver::GetResourceInfo(uint32 id) { 33 DevToolsNetLogObserver::GetResourceInfo(uint32 id) {
35 RequestToInfoMap::iterator it = request_to_info_.find(id); 34 RequestToInfoMap::iterator it = request_to_info_.find(id);
36 if (it != request_to_info_.end()) 35 if (it != request_to_info_.end())
37 return it->second; 36 return it->second;
38 return NULL; 37 return NULL;
39 } 38 }
40 39
41 void DevToolsNetLogObserver::OnAddEntry(net::NetLog::EventType type, 40 void DevToolsNetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
42 const base::TimeTicks& time,
43 const net::NetLog::Source& source,
44 net::NetLog::EventPhase phase,
45 net::NetLog::EventParameters* params) {
46 // The events that the Observer is interested in only occur on the IO thread. 41 // The events that the Observer is interested in only occur on the IO thread.
47 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) 42 if (!BrowserThread::CurrentlyOn(BrowserThread::IO))
48 return; 43 return;
49 if (source.type == net::NetLog::SOURCE_URL_REQUEST) 44
50 OnAddURLRequestEntry(type, time, source, phase, params); 45 if (entry.source().type == net::NetLog::SOURCE_URL_REQUEST)
51 else if (source.type == net::NetLog::SOURCE_HTTP_STREAM_JOB) 46 OnAddURLRequestEntry(entry);
52 OnAddHTTPStreamJobEntry(type, time, source, phase, params); 47 else if (entry.source().type == net::NetLog::SOURCE_HTTP_STREAM_JOB)
53 else if (source.type == net::NetLog::SOURCE_SOCKET) 48 OnAddHTTPStreamJobEntry(entry);
54 OnAddSocketEntry(type, time, source, phase, params); 49 else if (entry.source().type == net::NetLog::SOURCE_SOCKET)
50 OnAddSocketEntry(entry);
55 } 51 }
56 52
57 void DevToolsNetLogObserver::OnAddURLRequestEntry( 53 void DevToolsNetLogObserver::OnAddURLRequestEntry(
58 net::NetLog::EventType type, 54 const net::NetLog::Entry& entry) {
59 const base::TimeTicks& time,
60 const net::NetLog::Source& source,
61 net::NetLog::EventPhase phase,
62 net::NetLog::EventParameters* params) {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
64 56
65 bool is_begin = phase == net::NetLog::PHASE_BEGIN; 57 bool is_begin = entry.phase() == net::NetLog::PHASE_BEGIN;
66 bool is_end = phase == net::NetLog::PHASE_END; 58 bool is_end = entry.phase() == net::NetLog::PHASE_END;
67 59
68 if (type == net::NetLog::TYPE_URL_REQUEST_START_JOB) { 60 if (entry.type() == net::NetLog::TYPE_URL_REQUEST_START_JOB) {
69 if (is_begin) { 61 if (is_begin) {
70 int load_flags = static_cast< 62 int load_flags;
71 net::URLRequestStartEventParameters*>(params)->load_flags(); 63 scoped_ptr<Value> event_param(entry.ParametersToValue());
64 if (!net::StartEventLoadFlagsFromEventParams(event_param.get(),
65 &load_flags)) {
66 return;
67 }
68
72 if (!(load_flags & net::LOAD_REPORT_RAW_HEADERS)) 69 if (!(load_flags & net::LOAD_REPORT_RAW_HEADERS))
73 return; 70 return;
74 71
75 if (request_to_info_.size() > kMaxNumEntries) { 72 if (request_to_info_.size() > kMaxNumEntries) {
76 LOG(WARNING) << "The raw headers observer url request count has grown " 73 LOG(WARNING) << "The raw headers observer url request count has grown "
77 "larger than expected, resetting"; 74 "larger than expected, resetting";
78 request_to_info_.clear(); 75 request_to_info_.clear();
79 } 76 }
80 77
81 request_to_info_[source.id] = new ResourceInfo(); 78 request_to_info_[entry.source().id] = new ResourceInfo();
82 79
83 if (request_to_encoded_data_length_.size() > kMaxNumEntries) { 80 if (request_to_encoded_data_length_.size() > kMaxNumEntries) {
84 LOG(WARNING) << "The encoded data length observer url request count " 81 LOG(WARNING) << "The encoded data length observer url request count "
85 "has grown larger than expected, resetting"; 82 "has grown larger than expected, resetting";
86 request_to_encoded_data_length_.clear(); 83 request_to_encoded_data_length_.clear();
87 } 84 }
88 85
89 request_to_encoded_data_length_[source.id] = 0; 86 request_to_encoded_data_length_[entry.source().id] = 0;
90 } 87 }
91 return; 88 return;
92 } else if (type == net::NetLog::TYPE_REQUEST_ALIVE) { 89 } else if (entry.type() == net::NetLog::TYPE_REQUEST_ALIVE) {
93 // Cleanup records based on the TYPE_REQUEST_ALIVE entry. 90 // Cleanup records based on the TYPE_REQUEST_ALIVE entry.
94 if (is_end) { 91 if (is_end) {
95 request_to_info_.erase(source.id); 92 request_to_info_.erase(entry.source().id);
96 request_to_encoded_data_length_.erase(source.id); 93 request_to_encoded_data_length_.erase(entry.source().id);
97 } 94 }
98 return; 95 return;
99 } 96 }
100 97
101 ResourceInfo* info = GetResourceInfo(source.id); 98 ResourceInfo* info = GetResourceInfo(entry.source().id);
102 if (!info) 99 if (!info)
103 return; 100 return;
104 101
105 switch (type) { 102 switch (entry.type()) {
106 case net::NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS: { 103 case net::NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS: {
107 net::NetLogHttpRequestParameter* request_parameter = 104 scoped_ptr<Value> event_params(entry.ParametersToValue());
108 static_cast<net::NetLogHttpRequestParameter*>(params); 105 std::string request_line;
109 const net::HttpRequestHeaders &request_headers = 106 net::HttpRequestHeaders request_headers;
110 request_parameter->GetHeaders(); 107
108 if (!net::HttpRequestHeaders::FromNetLogParam(event_params.get(),
109 &request_headers,
110 &request_line)) {
111 NOTREACHED();
112 }
111 113
112 // We need to clear headers in case the same url_request is reused for 114 // We need to clear headers in case the same url_request is reused for
113 // several http requests (e.g. see http://crbug.com/80157). 115 // several http requests (e.g. see http://crbug.com/80157).
114 info->request_headers.clear(); 116 info->request_headers.clear();
115 117
116 for (net::HttpRequestHeaders::Iterator it(request_headers); 118 for (net::HttpRequestHeaders::Iterator it(request_headers);
117 it.GetNext();) { 119 it.GetNext();) {
118 info->request_headers.push_back(std::make_pair(it.name(), 120 info->request_headers.push_back(std::make_pair(it.name(), it.value()));
119 it.value()));
120 } 121 }
121 info->request_headers_text = 122 info->request_headers_text = request_line + request_headers.ToString();
122 request_parameter->GetLine() +
123 request_parameter->GetHeaders().ToString();
124 break; 123 break;
125 } 124 }
126 case net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS: { 125 case net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS: {
127 const net::HttpResponseHeaders& response_headers = 126 scoped_ptr<Value> event_params(entry.ParametersToValue());
128 static_cast<net::NetLogHttpResponseParameter*>(params)->GetHeaders(); 127
129 info->http_status_code = response_headers.response_code(); 128 scoped_refptr<net::HttpResponseHeaders> response_headers;
130 info->http_status_text = response_headers.GetStatusText(); 129
130 if (!net::HttpResponseHeaders::FromNetLogParam(event_params.get(),
131 &response_headers)) {
132 NOTREACHED();
133 }
134
135 info->http_status_code = response_headers->response_code();
136 info->http_status_text = response_headers->GetStatusText();
131 std::string name, value; 137 std::string name, value;
132 138
133 // We need to clear headers in case the same url_request is reused for 139 // We need to clear headers in case the same url_request is reused for
134 // several http requests (e.g. see http://crbug.com/80157). 140 // several http requests (e.g. see http://crbug.com/80157).
135 info->response_headers.clear(); 141 info->response_headers.clear();
136 142
137 for (void* it = NULL; 143 for (void* it = NULL;
138 response_headers.EnumerateHeaderLines(&it, &name, &value); ) { 144 response_headers->EnumerateHeaderLines(&it, &name, &value); ) {
139 info->response_headers.push_back(std::make_pair(name, value)); 145 info->response_headers.push_back(std::make_pair(name, value));
140 } 146 }
141 info->response_headers_text = 147 info->response_headers_text =
142 net::HttpUtil::ConvertHeadersBackToHTTPResponse( 148 net::HttpUtil::ConvertHeadersBackToHTTPResponse(
143 response_headers.raw_headers()); 149 response_headers->raw_headers());
144 break; 150 break;
145 } 151 }
146 case net::NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB: { 152 case net::NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB: {
147 uint32 http_stream_job_id = static_cast<net::NetLogSourceParameter*>( 153 scoped_ptr<Value> event_params(entry.ParametersToValue());
148 params)->value().id; 154 net::NetLog::Source http_stream_job_source;
155 if (!net::NetLog::Source::FromEventParameters(event_params.get(),
156 &http_stream_job_source)) {
157 NOTREACHED();
158 break;
159 }
160
161 uint32 http_stream_job_id = http_stream_job_source.id;
149 HTTPStreamJobToSocketMap::iterator it = 162 HTTPStreamJobToSocketMap::iterator it =
150 http_stream_job_to_socket_.find(http_stream_job_id); 163 http_stream_job_to_socket_.find(http_stream_job_id);
151 if (it == http_stream_job_to_socket_.end()) 164 if (it == http_stream_job_to_socket_.end())
152 return; 165 return;
153 uint32 socket_id = it->second; 166 uint32 socket_id = it->second;
154 167
155 if (socket_to_request_.size() > kMaxNumEntries) { 168 if (socket_to_request_.size() > kMaxNumEntries) {
156 LOG(WARNING) << "The url request observer socket count has grown " 169 LOG(WARNING) << "The url request observer socket count has grown "
157 "larger than expected, resetting"; 170 "larger than expected, resetting";
158 socket_to_request_.clear(); 171 socket_to_request_.clear();
159 } 172 }
160 173
161 socket_to_request_[socket_id] = source.id; 174 socket_to_request_[socket_id] = entry.source().id;
162 http_stream_job_to_socket_.erase(http_stream_job_id); 175 http_stream_job_to_socket_.erase(http_stream_job_id);
163 break; 176 break;
164 } 177 }
165 default: 178 default:
166 break; 179 break;
167 } 180 }
168 } 181 }
169 182
170 void DevToolsNetLogObserver::OnAddHTTPStreamJobEntry( 183 void DevToolsNetLogObserver::OnAddHTTPStreamJobEntry(
171 net::NetLog::EventType type, 184 const net::NetLog::Entry& entry) {
172 const base::TimeTicks& time,
173 const net::NetLog::Source& source,
174 net::NetLog::EventPhase phase,
175 net::NetLog::EventParameters* params) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
177 186
178 if (type == net::NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET) { 187 if (entry.type() == net::NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET) {
179 uint32 socket_id = static_cast<net::NetLogSourceParameter*>( 188 scoped_ptr<Value> event_params(entry.ParametersToValue());
180 params)->value().id; 189 net::NetLog::Source socket_source;
190 if (!net::NetLog::Source::FromEventParameters(event_params.get(),
191 &socket_source)) {
192 NOTREACHED();
193 return;
194 }
181 195
182 // Prevents us from passively growing the memory unbounded in 196 // Prevents us from passively growing the memory unbounded in
183 // case something went wrong. Should not happen. 197 // case something went wrong. Should not happen.
184 if (http_stream_job_to_socket_.size() > kMaxNumEntries) { 198 if (http_stream_job_to_socket_.size() > kMaxNumEntries) {
185 LOG(WARNING) << "The load timing observer http stream job count " 199 LOG(WARNING) << "The load timing observer http stream job count "
186 "has grown larger than expected, resetting"; 200 "has grown larger than expected, resetting";
187 http_stream_job_to_socket_.clear(); 201 http_stream_job_to_socket_.clear();
188 } 202 }
189 http_stream_job_to_socket_[source.id] = socket_id; 203 http_stream_job_to_socket_[entry.source().id] = socket_source.id;
190 } 204 }
191 } 205 }
192 206
193 void DevToolsNetLogObserver::OnAddSocketEntry( 207 void DevToolsNetLogObserver::OnAddSocketEntry(
194 net::NetLog::EventType type, 208 const net::NetLog::Entry& entry) {
195 const base::TimeTicks& time,
196 const net::NetLog::Source& source,
197 net::NetLog::EventPhase phase,
198 net::NetLog::EventParameters* params) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
200 210
201 bool is_end = phase == net::NetLog::PHASE_END; 211 bool is_end = entry.phase() == net::NetLog::PHASE_END;
202 212
203 SocketToRequestMap::iterator it = socket_to_request_.find(source.id); 213 SocketToRequestMap::iterator it =
214 socket_to_request_.find(entry.source().id);
204 if (it == socket_to_request_.end()) 215 if (it == socket_to_request_.end())
205 return; 216 return;
206 uint32 request_id = it->second; 217 uint32 request_id = it->second;
207 218
208 if (type == net::NetLog::TYPE_SOCKET_IN_USE) { 219 if (entry.type() == net::NetLog::TYPE_SOCKET_IN_USE) {
209 if (is_end) 220 if (is_end)
210 socket_to_request_.erase(source.id); 221 socket_to_request_.erase(entry.source().id);
211 return; 222 return;
212 } 223 }
213 224
214 RequestToEncodedDataLengthMap::iterator encoded_data_length_it = 225 RequestToEncodedDataLengthMap::iterator encoded_data_length_it =
215 request_to_encoded_data_length_.find(request_id); 226 request_to_encoded_data_length_.find(request_id);
216 if (encoded_data_length_it == request_to_encoded_data_length_.end()) 227 if (encoded_data_length_it == request_to_encoded_data_length_.end())
217 return; 228 return;
218 229
219 if (net::NetLog::TYPE_SOCKET_BYTES_RECEIVED == type) { 230 if (net::NetLog::TYPE_SOCKET_BYTES_RECEIVED == entry.type()) {
220 int byte_count = 0; 231 int byte_count = 0;
221 scoped_ptr<Value> value(params->ToValue()); 232 scoped_ptr<Value> value(entry.ParametersToValue());
222 if (!value->IsType(Value::TYPE_DICTIONARY)) 233 if (!value->IsType(Value::TYPE_DICTIONARY))
223 return; 234 return;
224 235
225 DictionaryValue* dValue = static_cast<DictionaryValue*>(value.get()); 236 DictionaryValue* dValue = static_cast<DictionaryValue*>(value.get());
226 if (!dValue->GetInteger("byte_count", &byte_count)) 237 if (!dValue->GetInteger("byte_count", &byte_count))
227 return; 238 return;
228 239
229 encoded_data_length_it->second += byte_count; 240 encoded_data_length_it->second += byte_count;
230 } 241 }
231 } 242 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 298
288 RequestToEncodedDataLengthMap::iterator it = 299 RequestToEncodedDataLengthMap::iterator it =
289 dev_tools_net_log_observer->request_to_encoded_data_length_.find( 300 dev_tools_net_log_observer->request_to_encoded_data_length_.find(
290 source_id); 301 source_id);
291 if (it == dev_tools_net_log_observer->request_to_encoded_data_length_.end()) 302 if (it == dev_tools_net_log_observer->request_to_encoded_data_length_.end())
292 return -1; 303 return -1;
293 int encoded_data_length = it->second; 304 int encoded_data_length = it->second;
294 it->second = 0; 305 it->second = 0;
295 return encoded_data_length; 306 return encoded_data_length;
296 } 307 }
OLDNEW
« no previous file with comments | « content/browser/debugger/devtools_netlog_observer.h ('k') | net/base/capturing_net_log.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698