Port Android relocation packer to chromium build
[chromium-blink-merge.git] / net / spdy / spdy_proxy_client_socket.h
blob806af7397bff44e91f08e78cd841408b8d1c31e2
1 // Copyright (c) 2012 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 NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
6 #define NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
8 #include <list>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/base/load_timing_info.h"
17 #include "net/base/net_log.h"
18 #include "net/http/http_auth_controller.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/http/http_request_info.h"
21 #include "net/http/http_response_info.h"
22 #include "net/http/proxy_client_socket.h"
23 #include "net/spdy/spdy_http_stream.h"
24 #include "net/spdy/spdy_protocol.h"
25 #include "net/spdy/spdy_read_queue.h"
26 #include "net/spdy/spdy_session.h"
27 #include "net/spdy/spdy_stream.h"
30 class GURL;
32 namespace net {
34 class AddressList;
35 class HttpStream;
36 class IOBuffer;
37 class SpdyStream;
39 class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket,
40 public SpdyStream::Delegate {
41 public:
42 // Create a socket on top of the |spdy_stream| by sending a SYN_STREAM
43 // CONNECT frame for |endpoint|. After the SYN_REPLY is received,
44 // any data read/written to the socket will be transferred in data
45 // frames. This object will set itself as |spdy_stream|'s delegate.
46 SpdyProxyClientSocket(const base::WeakPtr<SpdyStream>& spdy_stream,
47 const std::string& user_agent,
48 const HostPortPair& endpoint,
49 const GURL& url,
50 const HostPortPair& proxy_server,
51 const BoundNetLog& source_net_log,
52 HttpAuthCache* auth_cache,
53 HttpAuthHandlerFactory* auth_handler_factory);
56 // On destruction Disconnect() is called.
57 ~SpdyProxyClientSocket() override;
59 // ProxyClientSocket methods:
60 const HttpResponseInfo* GetConnectResponseInfo() const override;
61 HttpStream* CreateConnectResponseStream() override;
62 const scoped_refptr<HttpAuthController>& GetAuthController() const override;
63 int RestartWithAuth(const CompletionCallback& callback) override;
64 bool IsUsingSpdy() const override;
65 NextProto GetProtocolNegotiated() const override;
67 // StreamSocket implementation.
68 int Connect(const CompletionCallback& callback) override;
69 void Disconnect() override;
70 bool IsConnected() const override;
71 bool IsConnectedAndIdle() const override;
72 const BoundNetLog& NetLog() const override;
73 void SetSubresourceSpeculation() override;
74 void SetOmniboxSpeculation() override;
75 bool WasEverUsed() const override;
76 bool UsingTCPFastOpen() const override;
77 bool WasNpnNegotiated() const override;
78 NextProto GetNegotiatedProtocol() const override;
79 bool GetSSLInfo(SSLInfo* ssl_info) override;
81 // Socket implementation.
82 int Read(IOBuffer* buf,
83 int buf_len,
84 const CompletionCallback& callback) override;
85 int Write(IOBuffer* buf,
86 int buf_len,
87 const CompletionCallback& callback) override;
88 int SetReceiveBufferSize(int32 size) override;
89 int SetSendBufferSize(int32 size) override;
90 int GetPeerAddress(IPEndPoint* address) const override;
91 int GetLocalAddress(IPEndPoint* address) const override;
93 // SpdyStream::Delegate implementation.
94 void OnRequestHeadersSent() override;
95 SpdyResponseHeadersStatus OnResponseHeadersUpdated(
96 const SpdyHeaderBlock& response_headers) override;
97 void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) override;
98 void OnDataSent() override;
99 void OnClose(int status) override;
101 private:
102 enum State {
103 STATE_DISCONNECTED,
104 STATE_GENERATE_AUTH_TOKEN,
105 STATE_GENERATE_AUTH_TOKEN_COMPLETE,
106 STATE_SEND_REQUEST,
107 STATE_SEND_REQUEST_COMPLETE,
108 STATE_READ_REPLY_COMPLETE,
109 STATE_OPEN,
110 STATE_CLOSED
113 void LogBlockedTunnelResponse() const;
115 // Calls |callback.Run(result)|. Used to run a callback posted to the
116 // message loop.
117 void RunCallback(const CompletionCallback& callback, int result) const;
119 void OnIOComplete(int result);
121 int DoLoop(int last_io_result);
122 int DoGenerateAuthToken();
123 int DoGenerateAuthTokenComplete(int result);
124 int DoSendRequest();
125 int DoSendRequestComplete(int result);
126 int DoReadReplyComplete(int result);
128 // Populates |user_buffer_| with as much read data as possible
129 // and returns the number of bytes read.
130 size_t PopulateUserReadBuffer(char* out, size_t len);
132 State next_state_;
134 // Pointer to the SPDY Stream that this sits on top of.
135 base::WeakPtr<SpdyStream> spdy_stream_;
137 // Stores the callback to the layer above, called on completing Read() or
138 // Connect().
139 CompletionCallback read_callback_;
140 // Stores the callback to the layer above, called on completing Write().
141 CompletionCallback write_callback_;
143 // CONNECT request and response.
144 HttpRequestInfo request_;
145 HttpResponseInfo response_;
147 // The hostname and port of the endpoint. This is not necessarily the one
148 // specified by the URL, due to Alternate-Protocol or fixed testing ports.
149 const HostPortPair endpoint_;
150 scoped_refptr<HttpAuthController> auth_;
152 // We buffer the response body as it arrives asynchronously from the stream.
153 SpdyReadQueue read_buffer_queue_;
155 // User provided buffer for the Read() response.
156 scoped_refptr<IOBuffer> user_buffer_;
157 size_t user_buffer_len_;
159 // User specified number of bytes to be written.
160 int write_buffer_len_;
162 // True if the transport socket has ever sent data.
163 bool was_ever_used_;
165 // Used only for redirects.
166 bool redirect_has_load_timing_info_;
167 LoadTimingInfo redirect_load_timing_info_;
169 const BoundNetLog net_log_;
171 // The default weak pointer factory.
172 base::WeakPtrFactory<SpdyProxyClientSocket> weak_factory_;
174 // Only used for posting write callbacks. Weak pointers created by this
175 // factory are invalidated in Disconnect().
176 base::WeakPtrFactory<SpdyProxyClientSocket> write_callback_weak_factory_;
178 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket);
181 } // namespace net
183 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_