Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: net/socket_stream/socket_stream.h

Issue 243077: Add net/socket_stream. (Closed)
Patch Set: Rename SocketStreamJob to SocketStream Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 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.
4
5 #ifndef NET_SOCKET_STREAM_SOCKET_STREAM_H_
6 #define NET_SOCKET_STREAM_SOCKET_STREAM_H_
7
8 #include <deque>
9 #include <string>
10 #include <vector>
11
12 #include "base/linked_ptr.h"
13 #include "base/ref_counted.h"
14 #include "base/scoped_ptr.h"
15 #include "base/task.h"
16 #include "net/base/address_list.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/io_buffer.h"
19 #include "net/proxy/proxy_service.h"
20 #include "net/socket/tcp_client_socket.h"
21 #include "net/url_request/url_request_context.h"
22
23 namespace net {
24
25 class ClientSocketFactory;
26 class HostResolver;
27 class SSLConfigService;
28 class SingleRequestHostResolver;
29
30 class SocketStream : public base::RefCountedThreadSafe<SocketStream> {
wtc 2009/10/22 23:33:18 I forgot to email you this suggestion: I believe t
31 public:
32 // Derive from this class and add your own data members to associate extra
33 // information with a SocketStream. Use GetUserData(key) and
34 // SetUserData(key, data).
35 class UserData {
36 public:
37 UserData() {}
38 virtual ~UserData() {}
39 };
40
41 class Delegate {
42 public:
43 virtual ~Delegate() {}
44
45 // Called when socket stream has been connected. The socket stream accepts
46 // at most |max_pending_send_allowed| so that a client of the socket stream
47 // should keep track of how much it has pending and shouldn't go over
48 // |max_pending_send_allowed| bytes.
49 virtual void OnConnected(SocketStream* socket,
50 int max_pending_send_allowed) = 0;
51
52 // Called when |amount_sent| bytes of data are sent.
53 virtual void OnSentData(SocketStream* socket,
54 int amount_sent) = 0;
55
56 // Called when |len| bytes of |data| are received.
57 virtual void OnReceivedData(SocketStream* socket,
58 const char* data, int len) = 0;
59
60 // Called when the socket stream has been closed.
61 virtual void OnClose(SocketStream* socket) = 0;
62 };
63
64 SocketStream(const GURL& url, Delegate* delegate);
65
66 // The user data allows the clients to associate data with this job.
67 // Multiple user data values can be stored under different keys.
68 // This job will TAKE OWNERSHIP of the given data pointer, and will
69 // delete the object if it is changed or the job is destroyed.
70 UserData* GetUserData(const void* key) const;
71 void SetUserData(const void* key, UserData* data);
72
73 const GURL& url() const { return url_; }
74 Delegate* delegate() const { return delegate_; }
75 int max_pending_send_allowed() const { return max_pending_send_allowed_; }
76
77 URLRequestContext* context() const { return context_.get(); }
78 void set_context(URLRequestContext* context);
79
80 // Opens the connection on the IO thread.
81 // Once the connection is established, calls delegate's OnConnected.
82 void Connect();
83
84 // Requests to send |len| bytes of |data| on the connection.
85 // Returns true if |data| is buffered in the job.
86 // Returns false if size of buffered data would exceeds
87 // |max_pending_send_allowed_| and |data| is not sent at all.
88 bool SendData(const char* data, int len);
89
90 // Requests to close the connection.
91 // Once the connection is closed, calls delegate's OnClose.
92 void Close();
93
94 // Detach delegate. Call before delegate is deleted.
95 // Once delegate is detached, close the socket stream and never call delegate
96 // back.
97 void DetachDelegate();
98
99 // Sets an alternative HostResolver. For testing purposes only.
100 void SetHostResolver(HostResolver* host_resolver);
101
102 // Sets an alternative ClientSocketFactory. Doesn't take ownership of
103 // |factory|. For testing purposes only.
104 void SetClientSocketFactory(ClientSocketFactory* factory);
105
106 private:
107 class RequestHeaders : public IOBuffer {
108 public:
109 RequestHeaders() : IOBuffer() {}
110 ~RequestHeaders() { data_ = NULL; }
111
112 void SetDataOffset(size_t offset) {
113 data_ = const_cast<char*>(headers_.data()) + offset;
114 }
115 std::string headers_;
116 };
117
118 class ResponseHeaders : public IOBuffer {
119 public:
120 ResponseHeaders() : IOBuffer() {}
121 ~ResponseHeaders() { data_ = NULL; }
122
123 void SetDataOffset(size_t offset) { data_ = headers_.get() + offset; }
124 char* headers() const { return headers_.get(); }
125 void Reset() { headers_.reset(); }
126 void Realloc(size_t new_size);
127
128 private:
129 scoped_ptr_malloc<char> headers_;
130 };
131
132 enum State {
133 STATE_NONE,
134 STATE_RESOLVE_PROXY,
135 STATE_RESOLVE_PROXY_COMPLETE,
136 STATE_RESOLVE_HOST,
137 STATE_RESOLVE_HOST_COMPLETE,
138 STATE_TCP_CONNECT,
139 STATE_TCP_CONNECT_COMPLETE,
140 STATE_WRITE_TUNNEL_HEADERS,
141 STATE_WRITE_TUNNEL_HEADERS_COMPLETE,
142 STATE_READ_TUNNEL_HEADERS,
143 STATE_READ_TUNNEL_HEADERS_COMPLETE,
144 STATE_SOCKS_CONNECT,
145 STATE_SOCKS_CONNECT_COMPLETE,
146 STATE_SSL_CONNECT,
147 STATE_SSL_CONNECT_COMPLETE,
148 STATE_CONNECTION_ESTABLISHED,
149 STATE_READ_WRITE
150 };
151
152 enum ProxyMode {
153 kDirectConnection, // If using a direct connection
154 kTunnelProxy, // If using a tunnel (CONNECT method as HTTPS)
155 kSOCKSProxy, // If using a SOCKS proxy
156 };
157
158 typedef std::deque< scoped_refptr<IOBufferWithSize> > PendingDataQueue;
159 friend class base::RefCountedThreadSafe<SocketStream>;
160 ~SocketStream();
161
162 // Finish the job. Once finished, calls OnClose of delegate, and no more
163 // notifications will be sent to delegate.
164 void Finish();
165
166 void DidEstablishConnection();
167 void DidReceiveData(int result);
168 void DidSendData(int result);
169
170 void OnIOCompleted(int result);
171 void OnReadCompleted(int result);
172 void OnWriteCompleted(int result);
173
174 int DoLoop(int result);
175
176 int DoResolveProxy();
177 int DoResolveProxyComplete(int result);
178 int DoResolveHost();
179 int DoResolveHostComplete(int result);
180 int DoTcpConnect();
181 int DoTcpConnectComplete(int result);
182 int DoWriteTunnelHeaders();
183 int DoWriteTunnelHeadersComplete(int result);
184 int DoReadTunnelHeaders();
185 int DoReadTunnelHeadersComplete(int result);
186 int DoSOCKSConnect();
187 int DoSOCKSConnectComplete(int result);
188 int DoSSLConnect();
189 int DoSSLConnectComplete(int result);
190 int DoReadWrite(int result);
191
192 int HandleCertificateError(int result);
193
194 bool is_secure() const;
195 SSLConfigService* ssl_config_service() const;
196 ProxyService* proxy_service() const;
197
198 GURL url_;
199 Delegate* delegate_;
200 int max_pending_send_allowed_;
201 scoped_refptr<URLRequestContext> context_;
202
203 typedef std::map<const void*, linked_ptr<UserData> > UserDataMap;
204 UserDataMap user_data_;
205
206 State next_state_;
207 scoped_refptr<HostResolver> host_resolver_;
208 ClientSocketFactory* factory_;
209
210 ProxyMode proxy_mode_;
211
212 ProxyService::PacRequest* pac_request_;
213 ProxyInfo proxy_info_;
214
215 scoped_refptr<RequestHeaders> tunnel_request_headers_;
216 size_t tunnel_request_headers_bytes_sent_;
217 scoped_refptr<ResponseHeaders> tunnel_response_headers_;
218 int tunnel_response_headers_capacity_;
219 int tunnel_response_headers_len_;
220
221 // Use the same number as HttpNetworkTransaction::kMaxHeaderBufSize.
222 enum { kMaxTunnelResponseHeadersSize = 32768 }; // 32 kilobytes.
223
224 scoped_ptr<SingleRequestHostResolver> resolver_;
225 AddressList addresses_;
226 scoped_ptr<ClientSocket> socket_;
227
228 SSLConfig ssl_config_;
229
230 CompletionCallbackImpl<SocketStream> io_callback_;
231 CompletionCallbackImpl<SocketStream> read_callback_;
232 CompletionCallbackImpl<SocketStream> write_callback_;
233
234 scoped_refptr<IOBuffer> read_buf_;
235 int read_buf_size_;
236
237 // Total amount of buffer (|write_buf_size_| - |write_buf_offset_| +
238 // sum of size of |pending_write_bufs_|) should not exceed
239 // |max_pending_send_allowed_|.
240 // |write_buf_| holds requested data and |current_write_buf_| is used
241 // for Write operation, that is, |current_write_buf_| is
242 // |write_buf_| + |write_buf_offset_|.
243 scoped_refptr<IOBuffer> write_buf_;
244 scoped_refptr<ReusedIOBuffer> current_write_buf_;
245 int write_buf_offset_;
246 int write_buf_size_;
247 PendingDataQueue pending_write_bufs_;
248
249 DISALLOW_COPY_AND_ASSIGN(SocketStream);
250 };
251
252 } // namespace net
253
254 #endif // NET_SOCKET_STREAM_SOCKET_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698