1 // Copyright 2014 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 #ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/base/completion_callback.h"
11 #include "net/base/load_states.h"
12 #include "net/base/request_priority.h"
13 #include "net/http/http_transaction.h"
14 #include "net/websockets/websocket_handshake_stream_base.h"
16 class DevToolsNetworkController
;
17 class DevToolsNetworkInterceptor
;
21 class AuthCredentials
;
23 class HttpRequestHeaders
;
24 struct HttpRequestInfo
;
25 class HttpResponseInfo
;
26 class HttpNetworkSession
;
28 struct LoadTimingInfo
;
30 class X509Certificate
;
34 class DevToolsNetworkControllerHelper
;
37 // DevToolsNetworkTransaction is a wrapper for network transaction. All
38 // HttpTransaction methods are proxied to real transaction, but |callback|
39 // parameter is saved and replaced with proxy callback. Fail method should be
40 // used to simulate network outage. It runs saved callback (if any) with
41 // net::ERR_INTERNET_DISCONNECTED result value.
42 class DevToolsNetworkTransaction
: public net::HttpTransaction
{
44 static const char kDevToolsRequestInitiator
[];
45 static const char kDevToolsEmulateNetworkConditionsClientId
[];
47 DevToolsNetworkTransaction(
48 DevToolsNetworkController
* controller
,
49 scoped_ptr
<net::HttpTransaction
> network_transaction
);
51 ~DevToolsNetworkTransaction() override
;
53 const net::HttpRequestInfo
* request() const { return request_
; }
55 // Checks if request contains DevTools specific headers. Found values are
56 // remembered and corresponding keys are removed from headers.
57 void ProcessRequest();
59 bool failed() const { return failed_
; }
61 // Runs callback (if any) with net::ERR_INTERNET_DISCONNECTED result value.
64 int64_t throttled_byte_count() const { return throttled_byte_count_
; }
65 void DecreaseThrottledByteCount(int64_t delta
) {
66 throttled_byte_count_
-= delta
;
69 const std::string
& request_initiator() const { return request_initiator_
; }
71 const std::string
& client_id() const {
75 void FireThrottledCallback();
77 // HttpTransaction methods:
78 int Start(const net::HttpRequestInfo
* request
,
79 const net::CompletionCallback
& callback
,
80 const net::BoundNetLog
& net_log
) override
;
81 int RestartIgnoringLastError(
82 const net::CompletionCallback
& callback
) override
;
83 int RestartWithCertificate(net::X509Certificate
* client_cert
,
84 const net::CompletionCallback
& callback
) override
;
85 int RestartWithAuth(const net::AuthCredentials
& credentials
,
86 const net::CompletionCallback
& callback
) override
;
87 bool IsReadyToRestartForAuth() override
;
89 int Read(net::IOBuffer
* buf
,
91 const net::CompletionCallback
& callback
) override
;
92 void StopCaching() override
;
93 bool GetFullRequestHeaders(net::HttpRequestHeaders
* headers
) const override
;
94 int64
GetTotalReceivedBytes() const override
;
95 void DoneReading() override
;
96 const net::HttpResponseInfo
* GetResponseInfo() const override
;
97 net::LoadState
GetLoadState() const override
;
98 net::UploadProgress
GetUploadProgress() const override
;
99 void SetQuicServerInfo(net::QuicServerInfo
* quic_server_info
) override
;
100 bool GetLoadTimingInfo(net::LoadTimingInfo
* load_timing_info
) const override
;
101 void SetPriority(net::RequestPriority priority
) override
;
102 void SetWebSocketHandshakeStreamCreateHelper(
103 net::WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) override
;
104 void SetBeforeNetworkStartCallback(
105 const BeforeNetworkStartCallback
& callback
) override
;
106 void SetBeforeProxyHeadersSentCallback(
107 const BeforeProxyHeadersSentCallback
& callback
) override
;
108 int ResumeNetworkStart() override
;
111 friend class test::DevToolsNetworkControllerHelper
;
114 // Proxy callback handler. Runs saved callback.
115 void OnCallback(int result
);
117 DevToolsNetworkController
* controller_
;
118 base::WeakPtr
<DevToolsNetworkInterceptor
> interceptor_
;
120 // Modified request. Should be destructed after |network_transaction_|
121 scoped_ptr
<net::HttpRequestInfo
> custom_request_
;
123 // Real network transaction.
124 scoped_ptr
<net::HttpTransaction
> network_transaction_
;
126 const net::HttpRequestInfo
* request_
;
128 // True if Fail was already invoked.
131 // Value of "X-DevTools-Request-Initiator" request header.
132 std::string request_initiator_
;
134 // Value of "X-DevTools-Emulate-Network-Conditions-Client-Id" request header.
135 std::string client_id_
;
140 RESTART_IGNORING_LAST_ERROR
,
142 RESTART_WITH_CERTIFICATE
,
147 net::CompletionCallback callback
,
149 CallbackType callback_type
);
151 void Throttle(int result
);
153 int throttled_result_
;
154 int64_t throttled_byte_count_
;
155 CallbackType callback_type_
;
156 net::CompletionCallback proxy_callback_
;
157 net::CompletionCallback callback_
;
159 DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction
);
162 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_