Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_network_transaction.h
blobd50b6013c9175f30b4557a3cb31ba1315f03f87b
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 <stdint.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/load_states.h"
14 #include "net/base/request_priority.h"
15 #include "net/http/http_transaction.h"
16 #include "net/websockets/websocket_handshake_stream_base.h"
18 class DevToolsNetworkController;
19 class DevToolsNetworkInterceptor;
20 class GURL;
22 namespace net {
23 class AuthCredentials;
24 class BoundNetLog;
25 class HttpRequestHeaders;
26 struct HttpRequestInfo;
27 class HttpResponseInfo;
28 class HttpNetworkSession;
29 class IOBuffer;
30 struct LoadTimingInfo;
31 class UploadProgress;
32 class X509Certificate;
33 } // namespace net
35 namespace test {
36 class DevToolsNetworkControllerHelper;
39 // DevToolsNetworkTransaction is a wrapper for network transaction. All
40 // HttpTransaction methods are proxied to real transaction, but |callback|
41 // parameter is saved and replaced with proxy callback. Fail method should be
42 // used to simulate network outage. It runs saved callback (if any) with
43 // net::ERR_INTERNET_DISCONNECTED result value.
44 class DevToolsNetworkTransaction : public net::HttpTransaction {
45 public:
46 static const char kDevToolsRequestInitiator[];
47 static const char kDevToolsEmulateNetworkConditionsClientId[];
49 DevToolsNetworkTransaction(
50 DevToolsNetworkController* controller,
51 scoped_ptr<net::HttpTransaction> network_transaction);
53 ~DevToolsNetworkTransaction() override;
55 const net::HttpRequestInfo* request() const { return request_; }
57 // Checks if request contains DevTools specific headers. Found values are
58 // remembered and corresponding keys are removed from headers.
59 void ProcessRequest();
61 bool failed() const { return failed_; }
63 // Runs callback (if any) with net::ERR_INTERNET_DISCONNECTED result value.
64 void Fail();
66 int64_t throttled_byte_count() const { return throttled_byte_count_; }
67 void DecreaseThrottledByteCount(int64_t delta) {
68 throttled_byte_count_ -= delta;
71 const std::string& request_initiator() const { return request_initiator_; }
73 const std::string& client_id() const {
74 return client_id_;
77 void FireThrottledCallback();
79 // HttpTransaction methods:
80 int Start(const net::HttpRequestInfo* request,
81 const net::CompletionCallback& callback,
82 const net::BoundNetLog& net_log) override;
83 int RestartIgnoringLastError(
84 const net::CompletionCallback& callback) override;
85 int RestartWithCertificate(net::X509Certificate* client_cert,
86 const net::CompletionCallback& callback) override;
87 int RestartWithAuth(const net::AuthCredentials& credentials,
88 const net::CompletionCallback& callback) override;
89 bool IsReadyToRestartForAuth() override;
91 int Read(net::IOBuffer* buf,
92 int buf_len,
93 const net::CompletionCallback& callback) override;
94 void StopCaching() override;
95 bool GetFullRequestHeaders(net::HttpRequestHeaders* headers) const override;
96 int64 GetTotalReceivedBytes() const override;
97 int64_t GetTotalSentBytes() const override;
98 void DoneReading() override;
99 const net::HttpResponseInfo* GetResponseInfo() const override;
100 net::LoadState GetLoadState() const override;
101 net::UploadProgress GetUploadProgress() const override;
102 void SetQuicServerInfo(net::QuicServerInfo* quic_server_info) override;
103 bool GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
104 void SetPriority(net::RequestPriority priority) override;
105 void SetWebSocketHandshakeStreamCreateHelper(
106 net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) override;
107 void SetBeforeNetworkStartCallback(
108 const BeforeNetworkStartCallback& callback) override;
109 void SetBeforeProxyHeadersSentCallback(
110 const BeforeProxyHeadersSentCallback& callback) override;
111 int ResumeNetworkStart() override;
112 void GetConnectionAttempts(net::ConnectionAttempts* out) const override;
114 protected:
115 friend class test::DevToolsNetworkControllerHelper;
117 private:
118 // Proxy callback handler. Runs saved callback.
119 void OnCallback(int result);
121 DevToolsNetworkController* controller_;
122 base::WeakPtr<DevToolsNetworkInterceptor> interceptor_;
124 // Modified request. Should be destructed after |network_transaction_|
125 scoped_ptr<net::HttpRequestInfo> custom_request_;
127 // Real network transaction.
128 scoped_ptr<net::HttpTransaction> network_transaction_;
130 const net::HttpRequestInfo* request_;
132 // True if Fail was already invoked.
133 bool failed_;
135 // Value of "X-DevTools-Request-Initiator" request header.
136 std::string request_initiator_;
138 // Value of "X-DevTools-Emulate-Network-Conditions-Client-Id" request header.
139 std::string client_id_;
141 enum CallbackType {
142 NONE,
143 READ,
144 RESTART_IGNORING_LAST_ERROR,
145 RESTART_WITH_AUTH,
146 RESTART_WITH_CERTIFICATE,
147 START
150 int SetupCallback(
151 net::CompletionCallback callback,
152 int result,
153 CallbackType callback_type);
155 void Throttle(int result);
157 int throttled_result_;
158 int64_t throttled_byte_count_;
159 CallbackType callback_type_;
160 net::CompletionCallback proxy_callback_;
161 net::CompletionCallback callback_;
163 DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction);
166 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_TRANSACTION_H_