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
|
// 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.
#include "chrome/browser/debugger/devtools_netlog_observer.h"
#include "base/string_util.h"
#include "chrome/browser/io_thread.h"
#include "chrome/common/resource_response.h"
#include "net/base/load_flags.h"
#include "net/http/http_net_log_params.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_netlog_params.h"
#include "webkit/glue/resource_loader_bridge.h"
const size_t kMaxNumEntries = 1000;
DevToolsNetLogObserver* DevToolsNetLogObserver::instance_ = NULL;
DevToolsNetLogObserver::DevToolsNetLogObserver(ChromeNetLog* chrome_net_log)
: ChromeNetLog::Observer(net::NetLog::LOG_ALL_BUT_BYTES),
chrome_net_log_(chrome_net_log) {
chrome_net_log_->AddObserver(this);
}
DevToolsNetLogObserver::~DevToolsNetLogObserver() {
chrome_net_log_->RemoveObserver(this);
}
DevToolsNetLogObserver::ResourceInfo*
DevToolsNetLogObserver::GetResourceInfo(uint32 id) {
RequestToInfoMap::iterator it = request_to_info_.find(id);
if (it != request_to_info_.end())
return it->second;
return NULL;
}
void DevToolsNetLogObserver::OnAddEntry(net::NetLog::EventType type,
const base::TimeTicks& time,
const net::NetLog::Source& source,
net::NetLog::EventPhase phase,
net::NetLog::EventParameters* params) {
if (type == net::NetLog::TYPE_URL_REQUEST_START_JOB) {
if (phase != net::NetLog::PHASE_BEGIN)
return;
int load_flags = static_cast<URLRequestStartEventParameters*>(params)->
load_flags();
if (!(load_flags & net::LOAD_REPORT_RAW_HEADERS))
return;
if (request_to_info_.size() > kMaxNumEntries) {
LOG(WARNING) << "The raw headers observer url request count has grown "
"larger than expected, resetting";
request_to_info_.clear();
}
scoped_refptr<ResourceInfo> new_record(new ResourceInfo());
// We may encounter multiple PHASE_BEGIN for same resource in case of
// redirect -- if so, replace the old record to avoid keeping headers
// from different requests.
std::pair<RequestToInfoMap::iterator, bool> inserted =
request_to_info_.insert(std::make_pair(source.id, new_record));
if (!inserted.second)
inserted.first->second = new_record;
return;
}
if (type == net::NetLog::TYPE_REQUEST_ALIVE &&
phase == net::NetLog::PHASE_END) {
request_to_info_.erase(source.id);
return;
}
if (type != net::NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS &&
type != net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS)
return;
ResourceInfo* info = GetResourceInfo(source.id);
if (!info)
return;
switch (type) {
case net::NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS: {
const net::HttpRequestHeaders &request_headers =
static_cast<net::NetLogHttpRequestParameter*>(params)->GetHeaders();
for (net::HttpRequestHeaders::Iterator it(request_headers);
it.GetNext();) {
info->request_headers.push_back(std::make_pair(it.name(),
it.value()));
}
break;
}
case net::NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS: {
const net::HttpResponseHeaders& response_headers =
static_cast<net::NetLogHttpResponseParameter*>(params)->GetHeaders();
info->http_status_code = response_headers.response_code();
info->http_status_text = response_headers.GetStatusText();
std::string name, value;
for (void* it = NULL;
response_headers.EnumerateHeaderLines(&it, &name, &value); ) {
info->response_headers.push_back(std::make_pair(name, value));
}
break;
}
default:
break;
}
}
void DevToolsNetLogObserver::Attach(IOThread* io_thread) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(!instance_);
instance_ = new DevToolsNetLogObserver(io_thread->globals()->net_log.get());
}
void DevToolsNetLogObserver::Detach() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(instance_);
delete instance_;
instance_ = NULL;
}
DevToolsNetLogObserver* DevToolsNetLogObserver::GetInstance() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
return instance_;
}
// static
void DevToolsNetLogObserver::PopulateResponseInfo(URLRequest* request,
ResourceResponse* response) {
if (!(request->load_flags() & net::LOAD_REPORT_RAW_HEADERS))
return;
uint32 source_id = request->net_log().source().id;
DevToolsNetLogObserver* dev_tools_net_log_observer =
DevToolsNetLogObserver::GetInstance();
if (!dev_tools_net_log_observer)
return;
response->response_head.devtools_info =
dev_tools_net_log_observer->GetResourceInfo(source_id);
}
|