Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / devtools / devtools_netlog_observer.cc
blob27ce58ae49bca77083db4b55605d85e566cf8555
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
3 // found in the LICENSE file.
5 #include "content/browser/devtools/devtools_netlog_observer.h"
7 #include "base/strings/string_util.h"
8 #include "base/values.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/content_browser_client.h"
11 #include "content/public/common/resource_response.h"
12 #include "net/base/load_flags.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/http/http_util.h"
15 #include "net/spdy/spdy_header_block.h"
16 #include "net/url_request/url_request.h"
17 #include "net/url_request/url_request_netlog_params.h"
19 namespace content {
20 const size_t kMaxNumEntries = 1000;
22 DevToolsNetLogObserver* DevToolsNetLogObserver::instance_ = NULL;
24 DevToolsNetLogObserver::DevToolsNetLogObserver() {
27 DevToolsNetLogObserver::~DevToolsNetLogObserver() {
30 DevToolsNetLogObserver::ResourceInfo*
31 DevToolsNetLogObserver::GetResourceInfo(uint32 id) {
32 RequestToInfoMap::iterator it = request_to_info_.find(id);
33 if (it != request_to_info_.end())
34 return it->second.get();
35 return NULL;
38 void DevToolsNetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
39 // The events that the Observer is interested in only occur on the IO thread.
40 if (!BrowserThread::CurrentlyOn(BrowserThread::IO))
41 return;
43 if (entry.source().type == net::NetLog::SOURCE_URL_REQUEST)
44 OnAddURLRequestEntry(entry);
47 void DevToolsNetLogObserver::OnAddURLRequestEntry(
48 const net::NetLog::Entry& entry) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51 bool is_begin = entry.phase() == net::NetLog::PHASE_BEGIN;
52 bool is_end = entry.phase() == net::NetLog::PHASE_END;
54 if (entry.type() == net::NetLog::TYPE_URL_REQUEST_START_JOB) {
55 if (is_begin) {
56 int load_flags;
57 scoped_ptr<base::Value> event_param(entry.ParametersToValue());
58 if (!net::StartEventLoadFlagsFromEventParams(event_param.get(),
59 &load_flags)) {
60 return;
63 if (!(load_flags & net::LOAD_REPORT_RAW_HEADERS))
64 return;
66 if (request_to_info_.size() > kMaxNumEntries) {
67 LOG(WARNING) << "The raw headers observer url request count has grown "
68 "larger than expected, resetting";
69 request_to_info_.clear();
72 request_to_info_[entry.source().id] = new ResourceInfo();
74 return;
75 } else if (entry.type() == net::NetLog::TYPE_REQUEST_ALIVE) {
76 // Cleanup records based on the TYPE_REQUEST_ALIVE entry.
77 if (is_end)
78 request_to_info_.erase(entry.source().id);
79 return;
82 ResourceInfo* info = GetResourceInfo(entry.source().id);
83 if (!info)
84 return;
86 switch (entry.type()) {
87 case net::NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS: {
88 scoped_ptr<base::Value> event_params(entry.ParametersToValue());
89 std::string request_line;
90 net::HttpRequestHeaders request_headers;
92 if (!net::HttpRequestHeaders::FromNetLogParam(event_params.get(),
93 &request_headers,
94 &request_line)) {
95 NOTREACHED();
98 // We need to clear headers in case the same url_request is reused for
99 // several http requests (e.g. see http://crbug.com/80157).
100 info->request_headers.clear();
102 for (net::HttpRequestHeaders::Iterator it(request_headers);
103 it.GetNext();) {
104 info->request_headers.push_back(std::make_pair(it.name(), it.value()));
106 info->request_headers_text = request_line + request_headers.ToString();
107 break;
109 case net::NetLog::TYPE_HTTP_TRANSACTION_SPDY_SEND_REQUEST_HEADERS: {
110 scoped_ptr<base::Value> event_params(entry.ParametersToValue());
111 net::SpdyHeaderBlock request_headers;
113 if (!net::SpdyHeaderBlockFromNetLogParam(event_params.get(),
114 &request_headers)) {
115 NOTREACHED();
118 // We need to clear headers in case the same url_request is reused for
119 // several http requests (e.g. see http://crbug.com/80157).
120 info->request_headers.clear();
122 for (net::SpdyHeaderBlock::const_iterator it = request_headers.begin();
123 it != request_headers.end(); ++it) {
124 info->request_headers.push_back(std::make_pair(it->first, it->second));
126 info->request_headers_text = "";
127 break;
129 case net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS: {
130 scoped_ptr<base::Value> event_params(entry.ParametersToValue());
132 scoped_refptr<net::HttpResponseHeaders> response_headers;
134 if (!net::HttpResponseHeaders::FromNetLogParam(event_params.get(),
135 &response_headers)) {
136 NOTREACHED();
139 info->http_status_code = response_headers->response_code();
140 info->http_status_text = response_headers->GetStatusText();
141 std::string name, value;
143 // We need to clear headers in case the same url_request is reused for
144 // several http requests (e.g. see http://crbug.com/80157).
145 info->response_headers.clear();
147 for (void* it = NULL;
148 response_headers->EnumerateHeaderLines(&it, &name, &value); ) {
149 info->response_headers.push_back(std::make_pair(name, value));
152 if (!info->request_headers_text.empty()) {
153 info->response_headers_text =
154 net::HttpUtil::ConvertHeadersBackToHTTPResponse(
155 response_headers->raw_headers());
156 } else {
157 // SPDY request.
158 info->response_headers_text = "";
160 break;
162 default:
163 break;
167 void DevToolsNetLogObserver::Attach() {
168 DCHECK(!instance_);
169 net::NetLog* net_log = GetContentClient()->browser()->GetNetLog();
170 if (net_log) {
171 instance_ = new DevToolsNetLogObserver();
172 net_log->AddThreadSafeObserver(instance_, net::NetLog::LOG_ALL_BUT_BYTES);
176 void DevToolsNetLogObserver::Detach() {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
179 if (instance_) {
180 // Safest not to do this in the destructor to maintain thread safety across
181 // refactorings.
182 instance_->net_log()->RemoveThreadSafeObserver(instance_);
183 delete instance_;
184 instance_ = NULL;
188 DevToolsNetLogObserver* DevToolsNetLogObserver::GetInstance() {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
191 return instance_;
194 // static
195 void DevToolsNetLogObserver::PopulateResponseInfo(
196 net::URLRequest* request,
197 ResourceResponse* response) {
198 if (!(request->load_flags() & net::LOAD_REPORT_RAW_HEADERS))
199 return;
201 uint32 source_id = request->net_log().source().id;
202 DevToolsNetLogObserver* dev_tools_net_log_observer =
203 DevToolsNetLogObserver::GetInstance();
204 if (dev_tools_net_log_observer == NULL)
205 return;
206 response->head.devtools_info =
207 dev_tools_net_log_observer->GetResourceInfo(source_id);
210 } // namespace content