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
|
// 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.
#ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/base/completion_callback.h"
#include "net/base/load_states.h"
#include "net/base/request_priority.h"
#include "net/http/http_transaction.h"
#include "net/websockets/websocket_handshake_stream_base.h"
class DevToolsNetworkController;
class DevToolsNetworkInterceptor;
class GURL;
namespace net {
class AuthCredentials;
class BoundNetLog;
class HttpRequestHeaders;
struct HttpRequestInfo;
class HttpResponseInfo;
class HttpNetworkSession;
class IOBuffer;
struct LoadTimingInfo;
class UploadProgress;
class X509Certificate;
} // namespace net
namespace test {
class DevToolsNetworkControllerHelper;
}
// DevToolsNetworkTransaction is a wrapper for network transaction. All
// HttpTransaction methods are proxied to real transaction, but |callback|
// parameter is saved and replaced with proxy callback. Fail method should be
// used to simulate network outage. It runs saved callback (if any) with
// net::ERR_INTERNET_DISCONNECTED result value.
class DevToolsNetworkTransaction : public net::HttpTransaction {
public:
static const char kDevToolsRequestInitiator[];
static const char kDevToolsEmulateNetworkConditionsClientId[];
DevToolsNetworkTransaction(
DevToolsNetworkController* controller,
scoped_ptr<net::HttpTransaction> network_transaction);
~DevToolsNetworkTransaction() override;
const net::HttpRequestInfo* request() const { return request_; }
// Checks if request contains DevTools specific headers. Found values are
// remembered and corresponding keys are removed from headers.
void ProcessRequest();
bool failed() const { return failed_; }
// Runs callback (if any) with net::ERR_INTERNET_DISCONNECTED result value.
void Fail();
int64_t throttled_byte_count() const { return throttled_byte_count_; }
void DecreaseThrottledByteCount(int64_t delta) {
throttled_byte_count_ -= delta;
}
const std::string& request_initiator() const { return request_initiator_; }
const std::string& client_id() const {
return client_id_;
}
void FireThrottledCallback();
// HttpTransaction methods:
int Start(const net::HttpRequestInfo* request,
const net::CompletionCallback& callback,
const net::BoundNetLog& net_log) override;
int RestartIgnoringLastError(
const net::CompletionCallback& callback) override;
int RestartWithCertificate(net::X509Certificate* client_cert,
const net::CompletionCallback& callback) override;
int RestartWithAuth(const net::AuthCredentials& credentials,
const net::CompletionCallback& callback) override;
bool IsReadyToRestartForAuth() override;
int Read(net::IOBuffer* buf,
int buf_len,
const net::CompletionCallback& callback) override;
void StopCaching() override;
bool GetFullRequestHeaders(net::HttpRequestHeaders* headers) const override;
int64 GetTotalReceivedBytes() const override;
void DoneReading() override;
const net::HttpResponseInfo* GetResponseInfo() const override;
net::LoadState GetLoadState() const override;
net::UploadProgress GetUploadProgress() const override;
void SetQuicServerInfo(net::QuicServerInfo* quic_server_info) override;
bool GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
void SetPriority(net::RequestPriority priority) override;
void SetWebSocketHandshakeStreamCreateHelper(
net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) override;
void SetBeforeNetworkStartCallback(
const BeforeNetworkStartCallback& callback) override;
void SetBeforeProxyHeadersSentCallback(
const BeforeProxyHeadersSentCallback& callback) override;
int ResumeNetworkStart() override;
protected:
friend class test::DevToolsNetworkControllerHelper;
private:
// Proxy callback handler. Runs saved callback.
void OnCallback(int result);
DevToolsNetworkController* controller_;
base::WeakPtr<DevToolsNetworkInterceptor> interceptor_;
// Modified request. Should be destructed after |network_transaction_|
scoped_ptr<net::HttpRequestInfo> custom_request_;
// Real network transaction.
scoped_ptr<net::HttpTransaction> network_transaction_;
const net::HttpRequestInfo* request_;
// True if Start was already invoked.
bool started_;
// True if Fail was already invoked.
bool failed_;
// Value of "X-DevTools-Request-Initiator" request header.
std::string request_initiator_;
// Value of "X-DevTools-Emulate-Network-Conditions-Client-Id" request header.
std::string client_id_;
enum CallbackType {
NONE,
READ,
RESTART_IGNORING_LAST_ERROR,
RESTART_WITH_AUTH,
RESTART_WITH_CERTIFICATE,
START
};
int SetupCallback(
net::CompletionCallback callback,
int result,
CallbackType callback_type);
void Throttle(int result);
int throttled_result_;
int64_t throttled_byte_count_;
CallbackType callback_type_;
net::CompletionCallback proxy_callback_;
net::CompletionCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction);
};
#endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
|