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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// Copyright 2014 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/devtools/devtools_network_transaction.h"
#include "base/profiler/scoped_tracker.h"
#include "chrome/browser/devtools/devtools_network_controller.h"
#include "chrome/browser/devtools/devtools_network_interceptor.h"
#include "net/base/net_errors.h"
#include "net/base/upload_progress.h"
#include "net/http/http_network_transaction.h"
#include "net/http/http_request_info.h"
// Keep in sync with kDevToolsRequestInitiator and
// kDevToolsEmulateNetworkConditionsClientId defined in
// InspectorResourceAgent.cpp.
const char DevToolsNetworkTransaction::kDevToolsRequestInitiator[] =
"X-DevTools-Request-Initiator";
const char
DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId[] =
"X-DevTools-Emulate-Network-Conditions-Client-Id";
DevToolsNetworkTransaction::DevToolsNetworkTransaction(
DevToolsNetworkController* controller,
scoped_ptr<net::HttpTransaction> network_transaction)
: controller_(controller),
network_transaction_(network_transaction.Pass()),
request_(NULL),
failed_(false),
throttled_byte_count_(0),
callback_type_(NONE),
proxy_callback_(base::Bind(&DevToolsNetworkTransaction::OnCallback,
base::Unretained(this))) {
DCHECK(controller);
}
DevToolsNetworkTransaction::~DevToolsNetworkTransaction() {
if (interceptor_)
interceptor_->RemoveTransaction(this);
}
void DevToolsNetworkTransaction::Throttle(int result) {
throttled_result_ = result;
if (callback_type_ == START)
throttled_byte_count_ += network_transaction_->GetTotalReceivedBytes();
if (result > 0)
throttled_byte_count_ += result;
if (interceptor_)
interceptor_->ThrottleTransaction(this, callback_type_ == START);
}
void DevToolsNetworkTransaction::OnCallback(int rv) {
// TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
tracked_objects::ScopedTracker tracking_profile(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"424359 DevToolsNetworkTransaction::OnCallback"));
if (failed_)
return;
DCHECK(!callback_.is_null());
if (callback_type_ == START || callback_type_ == READ) {
if (interceptor_ && interceptor_->ShouldThrottle(this)) {
Throttle(rv);
return;
}
}
net::CompletionCallback callback = callback_;
callback_.Reset();
callback_type_ = NONE;
callback.Run(rv);
}
int DevToolsNetworkTransaction::SetupCallback(
net::CompletionCallback callback,
int result,
CallbackType callback_type) {
DCHECK(callback_type_ == NONE);
if (result == net::ERR_IO_PENDING) {
callback_type_ = callback_type;
callback_ = callback;
return result;
}
if (!interceptor_ || !interceptor_->ShouldThrottle(this))
return result;
// Only START and READ operation throttling is supported.
if (callback_type != START && callback_type != READ)
return result;
// In case of error |throttled_byte_count_| is unknown.
if (result < 0)
return result;
// URLRequestJob relies on synchronous end-of-stream notification.
if (callback_type == READ && result == 0)
return result;
callback_type_ = callback_type;
callback_ = callback;
Throttle(result);
return net::ERR_IO_PENDING;
}
void DevToolsNetworkTransaction::Fail() {
DCHECK(request_);
DCHECK(!failed_);
failed_ = true;
network_transaction_->SetBeforeNetworkStartCallback(
BeforeNetworkStartCallback());
if (callback_.is_null())
return;
net::CompletionCallback callback = callback_;
callback_.Reset();
callback_type_ = NONE;
callback.Run(net::ERR_INTERNET_DISCONNECTED);
}
int DevToolsNetworkTransaction::Start(
const net::HttpRequestInfo* request,
const net::CompletionCallback& callback,
const net::BoundNetLog& net_log) {
DCHECK(request);
request_ = request;
interceptor_ = controller_->GetInterceptor(this);
interceptor_->AddTransaction(this);
if (interceptor_->ShouldFail(this)) {
failed_ = true;
network_transaction_->SetBeforeNetworkStartCallback(
BeforeNetworkStartCallback());
return net::ERR_INTERNET_DISCONNECTED;
}
int rv = network_transaction_->Start(request_, proxy_callback_, net_log);
return SetupCallback(callback, rv, START);
}
void DevToolsNetworkTransaction::ProcessRequest() {
DCHECK(request_);
bool has_devtools_client_id = request_->extra_headers.HasHeader(
kDevToolsEmulateNetworkConditionsClientId);
bool has_devtools_request_initiator = request_->extra_headers.HasHeader(
kDevToolsRequestInitiator);
if (!has_devtools_client_id && !has_devtools_request_initiator)
return;
custom_request_.reset(new net::HttpRequestInfo(*request_));
if (has_devtools_client_id) {
custom_request_->extra_headers.GetHeader(
kDevToolsEmulateNetworkConditionsClientId, &client_id_);
custom_request_->extra_headers.RemoveHeader(
kDevToolsEmulateNetworkConditionsClientId);
}
if (has_devtools_request_initiator) {
custom_request_->extra_headers.GetHeader(
kDevToolsRequestInitiator, &request_initiator_);
custom_request_->extra_headers.RemoveHeader(kDevToolsRequestInitiator);
}
request_ = custom_request_.get();
}
int DevToolsNetworkTransaction::RestartIgnoringLastError(
const net::CompletionCallback& callback) {
if (failed_)
return net::ERR_INTERNET_DISCONNECTED;
int rv = network_transaction_->RestartIgnoringLastError(proxy_callback_);
return SetupCallback(callback, rv, RESTART_IGNORING_LAST_ERROR);
}
int DevToolsNetworkTransaction::RestartWithCertificate(
net::X509Certificate* client_cert,
const net::CompletionCallback& callback) {
if (failed_)
return net::ERR_INTERNET_DISCONNECTED;
int rv = network_transaction_->RestartWithCertificate(
client_cert, proxy_callback_);
return SetupCallback(callback, rv, RESTART_WITH_CERTIFICATE);
}
int DevToolsNetworkTransaction::RestartWithAuth(
const net::AuthCredentials& credentials,
const net::CompletionCallback& callback) {
if (failed_)
return net::ERR_INTERNET_DISCONNECTED;
int rv = network_transaction_->RestartWithAuth(credentials, proxy_callback_);
return SetupCallback(callback, rv, RESTART_WITH_AUTH);
}
bool DevToolsNetworkTransaction::IsReadyToRestartForAuth() {
return network_transaction_->IsReadyToRestartForAuth();
}
int DevToolsNetworkTransaction::Read(
net::IOBuffer* buf,
int buf_len,
const net::CompletionCallback& callback) {
if (failed_)
return net::ERR_INTERNET_DISCONNECTED;
int rv = network_transaction_->Read(buf, buf_len, proxy_callback_);
return SetupCallback(callback, rv, READ);
}
void DevToolsNetworkTransaction::StopCaching() {
network_transaction_->StopCaching();
}
bool DevToolsNetworkTransaction::GetFullRequestHeaders(
net::HttpRequestHeaders* headers) const {
return network_transaction_->GetFullRequestHeaders(headers);
}
int64 DevToolsNetworkTransaction::GetTotalReceivedBytes() const {
return network_transaction_->GetTotalReceivedBytes();
}
void DevToolsNetworkTransaction::DoneReading() {
network_transaction_->DoneReading();
}
const net::HttpResponseInfo*
DevToolsNetworkTransaction::GetResponseInfo() const {
return network_transaction_->GetResponseInfo();
}
net::LoadState DevToolsNetworkTransaction::GetLoadState() const {
return network_transaction_->GetLoadState();
}
net::UploadProgress DevToolsNetworkTransaction::GetUploadProgress() const {
return network_transaction_->GetUploadProgress();
}
void DevToolsNetworkTransaction::SetQuicServerInfo(
net::QuicServerInfo* quic_server_info) {
network_transaction_->SetQuicServerInfo(quic_server_info);
}
bool DevToolsNetworkTransaction::GetLoadTimingInfo(
net::LoadTimingInfo* load_timing_info) const {
return network_transaction_->GetLoadTimingInfo(load_timing_info);
}
void DevToolsNetworkTransaction::SetPriority(net::RequestPriority priority) {
network_transaction_->SetPriority(priority);
}
void DevToolsNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
network_transaction_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
}
void DevToolsNetworkTransaction::SetBeforeNetworkStartCallback(
const BeforeNetworkStartCallback& callback) {
network_transaction_->SetBeforeNetworkStartCallback(callback);
}
void DevToolsNetworkTransaction::SetBeforeProxyHeadersSentCallback(
const BeforeProxyHeadersSentCallback& callback) {
network_transaction_->SetBeforeProxyHeadersSentCallback(callback);
}
int DevToolsNetworkTransaction::ResumeNetworkStart() {
if (failed_)
return net::ERR_INTERNET_DISCONNECTED;
return network_transaction_->ResumeNetworkStart();
}
void DevToolsNetworkTransaction::FireThrottledCallback() {
DCHECK(!callback_.is_null());
DCHECK(callback_type_ == READ || callback_type_ == START);
net::CompletionCallback callback = callback_;
callback_.Reset();
callback_type_ = NONE;
callback.Run(throttled_result_);
}
|