Chromium Code Reviews| 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 #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 // This will also clear headers in the case the same url_request is reused | |
| 109 // for several http requests (e.g. see http://crbug.com/80157). | |
|
eroman
2012/06/06 21:27:11
Is this a new comment or moved from somewhere?
mmenke
2012/06/07 19:20:46
It was moved from below, but then I brought it bac
| |
| 110 if (!net::HttpRequestHeaders::FromNetLogParam(event_params.get(), | |
| 111 &request_headers, | |
| 112 &request_line)) { | |
| 113 NOTREACHED(); | |
| 114 } | |
| 111 | 115 |
| 112 // We need to clear headers in case the same url_request is reused for | 116 // 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). | 117 // several http requests (e.g. see http://crbug.com/80157). |
| 114 info->request_headers.clear(); | 118 info->request_headers.clear(); |
| 115 | 119 |
| 116 for (net::HttpRequestHeaders::Iterator it(request_headers); | 120 for (net::HttpRequestHeaders::Iterator it(request_headers); |
| 117 it.GetNext();) { | 121 it.GetNext();) { |
| 118 info->request_headers.push_back(std::make_pair(it.name(), | 122 info->request_headers.push_back(std::make_pair(it.name(), it.value())); |
| 119 it.value())); | |
| 120 } | 123 } |
| 121 info->request_headers_text = | 124 info->request_headers_text = request_line + request_headers.ToString(); |
| 122 request_parameter->GetLine() + | |
| 123 request_parameter->GetHeaders().ToString(); | |
| 124 break; | 125 break; |
| 125 } | 126 } |
| 126 case net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS: { | 127 case net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS: { |
| 127 const net::HttpResponseHeaders& response_headers = | 128 scoped_ptr<Value> event_params(entry.ParametersToValue()); |
| 128 static_cast<net::NetLogHttpResponseParameter*>(params)->GetHeaders(); | 129 |
| 129 info->http_status_code = response_headers.response_code(); | 130 scoped_refptr<net::HttpResponseHeaders> response_headers( |
| 130 info->http_status_text = response_headers.GetStatusText(); | 131 net::HttpResponseHeaders::FromNetLogParam(event_params.get())); |
| 132 | |
| 133 if (!response_headers) | |
| 134 NOTREACHED(); | |
|
eroman
2012/06/06 21:27:11
Please put curly braces.
Perhaps this works as is
mmenke
2012/06/07 19:20:46
Done. It does work, but your concern is reasonabl
| |
| 135 | |
| 136 info->http_status_code = response_headers->response_code(); | |
| 137 info->http_status_text = response_headers->GetStatusText(); | |
| 131 std::string name, value; | 138 std::string name, value; |
| 132 | 139 |
| 133 // We need to clear headers in case the same url_request is reused for | 140 // 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). | 141 // several http requests (e.g. see http://crbug.com/80157). |
| 135 info->response_headers.clear(); | 142 info->response_headers.clear(); |
| 136 | 143 |
| 137 for (void* it = NULL; | 144 for (void* it = NULL; |
| 138 response_headers.EnumerateHeaderLines(&it, &name, &value); ) { | 145 response_headers->EnumerateHeaderLines(&it, &name, &value); ) { |
| 139 info->response_headers.push_back(std::make_pair(name, value)); | 146 info->response_headers.push_back(std::make_pair(name, value)); |
| 140 } | 147 } |
| 141 info->response_headers_text = | 148 info->response_headers_text = |
| 142 net::HttpUtil::ConvertHeadersBackToHTTPResponse( | 149 net::HttpUtil::ConvertHeadersBackToHTTPResponse( |
| 143 response_headers.raw_headers()); | 150 response_headers->raw_headers()); |
| 144 break; | 151 break; |
| 145 } | 152 } |
| 146 case net::NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB: { | 153 case net::NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB: { |
| 147 uint32 http_stream_job_id = static_cast<net::NetLogSourceParameter*>( | 154 scoped_ptr<Value> event_params(entry.ParametersToValue()); |
| 148 params)->value().id; | 155 net::NetLog::Source http_stream_job_source; |
| 156 if (!net::NetLog::Source::FromEventParameters(event_params.get(), | |
| 157 &http_stream_job_source)) { | |
| 158 NOTREACHED(); | |
| 159 break; | |
| 160 } | |
| 161 | |
| 162 uint32 http_stream_job_id = http_stream_job_source.id; | |
| 149 HTTPStreamJobToSocketMap::iterator it = | 163 HTTPStreamJobToSocketMap::iterator it = |
| 150 http_stream_job_to_socket_.find(http_stream_job_id); | 164 http_stream_job_to_socket_.find(http_stream_job_id); |
| 151 if (it == http_stream_job_to_socket_.end()) | 165 if (it == http_stream_job_to_socket_.end()) |
| 152 return; | 166 return; |
| 153 uint32 socket_id = it->second; | 167 uint32 socket_id = it->second; |
| 154 | 168 |
| 155 if (socket_to_request_.size() > kMaxNumEntries) { | 169 if (socket_to_request_.size() > kMaxNumEntries) { |
| 156 LOG(WARNING) << "The url request observer socket count has grown " | 170 LOG(WARNING) << "The url request observer socket count has grown " |
| 157 "larger than expected, resetting"; | 171 "larger than expected, resetting"; |
| 158 socket_to_request_.clear(); | 172 socket_to_request_.clear(); |
| 159 } | 173 } |
| 160 | 174 |
| 161 socket_to_request_[socket_id] = source.id; | 175 socket_to_request_[socket_id] = entry.source().id; |
| 162 http_stream_job_to_socket_.erase(http_stream_job_id); | 176 http_stream_job_to_socket_.erase(http_stream_job_id); |
| 163 break; | 177 break; |
| 164 } | 178 } |
| 165 default: | 179 default: |
| 166 break; | 180 break; |
| 167 } | 181 } |
| 168 } | 182 } |
| 169 | 183 |
| 170 void DevToolsNetLogObserver::OnAddHTTPStreamJobEntry( | 184 void DevToolsNetLogObserver::OnAddHTTPStreamJobEntry( |
| 171 net::NetLog::EventType type, | 185 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)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 177 | 187 |
| 178 if (type == net::NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET) { | 188 if (entry.type() == net::NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET) { |
| 179 uint32 socket_id = static_cast<net::NetLogSourceParameter*>( | 189 scoped_ptr<Value> event_params(entry.ParametersToValue()); |
| 180 params)->value().id; | 190 net::NetLog::Source socket_source; |
| 191 if (!net::NetLog::Source::FromEventParameters(event_params.get(), | |
| 192 &socket_source)) { | |
| 193 NOTREACHED(); | |
| 194 return; | |
| 195 } | |
| 181 | 196 |
| 182 // Prevents us from passively growing the memory unbounded in | 197 // Prevents us from passively growing the memory unbounded in |
| 183 // case something went wrong. Should not happen. | 198 // case something went wrong. Should not happen. |
| 184 if (http_stream_job_to_socket_.size() > kMaxNumEntries) { | 199 if (http_stream_job_to_socket_.size() > kMaxNumEntries) { |
| 185 LOG(WARNING) << "The load timing observer http stream job count " | 200 LOG(WARNING) << "The load timing observer http stream job count " |
| 186 "has grown larger than expected, resetting"; | 201 "has grown larger than expected, resetting"; |
| 187 http_stream_job_to_socket_.clear(); | 202 http_stream_job_to_socket_.clear(); |
| 188 } | 203 } |
| 189 http_stream_job_to_socket_[source.id] = socket_id; | 204 http_stream_job_to_socket_[entry.source().id] = socket_source.id; |
| 190 } | 205 } |
| 191 } | 206 } |
| 192 | 207 |
| 193 void DevToolsNetLogObserver::OnAddSocketEntry( | 208 void DevToolsNetLogObserver::OnAddSocketEntry( |
| 194 net::NetLog::EventType type, | 209 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)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 200 | 211 |
| 201 bool is_end = phase == net::NetLog::PHASE_END; | 212 bool is_end = entry.phase() == net::NetLog::PHASE_END; |
| 202 | 213 |
| 203 SocketToRequestMap::iterator it = socket_to_request_.find(source.id); | 214 SocketToRequestMap::iterator it = |
| 215 socket_to_request_.find(entry.source().id); | |
| 204 if (it == socket_to_request_.end()) | 216 if (it == socket_to_request_.end()) |
| 205 return; | 217 return; |
| 206 uint32 request_id = it->second; | 218 uint32 request_id = it->second; |
| 207 | 219 |
| 208 if (type == net::NetLog::TYPE_SOCKET_IN_USE) { | 220 if (entry.type() == net::NetLog::TYPE_SOCKET_IN_USE) { |
| 209 if (is_end) | 221 if (is_end) |
| 210 socket_to_request_.erase(source.id); | 222 socket_to_request_.erase(entry.source().id); |
| 211 return; | 223 return; |
| 212 } | 224 } |
| 213 | 225 |
| 214 RequestToEncodedDataLengthMap::iterator encoded_data_length_it = | 226 RequestToEncodedDataLengthMap::iterator encoded_data_length_it = |
| 215 request_to_encoded_data_length_.find(request_id); | 227 request_to_encoded_data_length_.find(request_id); |
| 216 if (encoded_data_length_it == request_to_encoded_data_length_.end()) | 228 if (encoded_data_length_it == request_to_encoded_data_length_.end()) |
| 217 return; | 229 return; |
| 218 | 230 |
| 219 if (net::NetLog::TYPE_SOCKET_BYTES_RECEIVED == type) { | 231 if (net::NetLog::TYPE_SOCKET_BYTES_RECEIVED == entry.type()) { |
| 220 int byte_count = 0; | 232 int byte_count = 0; |
| 221 scoped_ptr<Value> value(params->ToValue()); | 233 scoped_ptr<Value> value(entry.ParametersToValue()); |
| 222 if (!value->IsType(Value::TYPE_DICTIONARY)) | 234 if (!value->IsType(Value::TYPE_DICTIONARY)) |
| 223 return; | 235 return; |
| 224 | 236 |
| 225 DictionaryValue* dValue = static_cast<DictionaryValue*>(value.get()); | 237 DictionaryValue* dValue = static_cast<DictionaryValue*>(value.get()); |
| 226 if (!dValue->GetInteger("byte_count", &byte_count)) | 238 if (!dValue->GetInteger("byte_count", &byte_count)) |
| 227 return; | 239 return; |
| 228 | 240 |
| 229 encoded_data_length_it->second += byte_count; | 241 encoded_data_length_it->second += byte_count; |
| 230 } | 242 } |
| 231 } | 243 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 | 299 |
| 288 RequestToEncodedDataLengthMap::iterator it = | 300 RequestToEncodedDataLengthMap::iterator it = |
| 289 dev_tools_net_log_observer->request_to_encoded_data_length_.find( | 301 dev_tools_net_log_observer->request_to_encoded_data_length_.find( |
| 290 source_id); | 302 source_id); |
| 291 if (it == dev_tools_net_log_observer->request_to_encoded_data_length_.end()) | 303 if (it == dev_tools_net_log_observer->request_to_encoded_data_length_.end()) |
| 292 return -1; | 304 return -1; |
| 293 int encoded_data_length = it->second; | 305 int encoded_data_length = it->second; |
| 294 it->second = 0; | 306 it->second = 0; |
| 295 return encoded_data_length; | 307 return encoded_data_length; |
| 296 } | 308 } |
| OLD | NEW |