• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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_TCP_CLIENT_SOCKET_POOL_H_
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "base/basictypes.h"
12 #include "base/ref_counted.h"
13 #include "base/scoped_ptr.h"
14 #include "base/time.h"
15 #include "base/timer.h"
16 #include "net/base/host_port_pair.h"
17 #include "net/base/host_resolver.h"
18 #include "net/socket/client_socket_pool_base.h"
19 #include "net/socket/client_socket_pool_histograms.h"
20 #include "net/socket/client_socket_pool.h"
21 
22 namespace net {
23 
24 class ClientSocketFactory;
25 
26 class TCPSocketParams : public base::RefCounted<TCPSocketParams> {
27  public:
28   TCPSocketParams(const HostPortPair& host_port_pair, RequestPriority priority,
29                   const GURL& referrer, bool disable_resolver_cache
30 #ifdef ANDROID
31                   , bool ignore_limits
32 #endif
33                  );
34 
35   // TODO(willchan): Update all unittests so we don't need this.
36   TCPSocketParams(const std::string& host, int port, RequestPriority priority,
37                   const GURL& referrer, bool disable_resolver_cache);
38 
destination()39   const HostResolver::RequestInfo& destination() const { return destination_; }
40 
41 #ifdef ANDROID
ignore_limits()42   bool ignore_limits() const { return ignore_limits_; }
43   // Gets the UID of the calling process
44   bool getUID(uid_t *uid) const;
45   void setUID(uid_t uid);
46 #endif
47 
48  private:
49   friend class base::RefCounted<TCPSocketParams>;
50   ~TCPSocketParams();
51 
52   void Initialize(RequestPriority priority, const GURL& referrer,
53                   bool disable_resolver_cache);
54 
55   HostResolver::RequestInfo destination_;
56 #ifdef ANDROID
57   bool ignore_limits_;
58   bool valid_uid_;
59   int calling_uid_;
60 #endif
61 
62   DISALLOW_COPY_AND_ASSIGN(TCPSocketParams);
63 };
64 
65 // TCPConnectJob handles the host resolution necessary for socket creation
66 // and the tcp connect.
67 class TCPConnectJob : public ConnectJob {
68  public:
69   TCPConnectJob(const std::string& group_name,
70                 const scoped_refptr<TCPSocketParams>& params,
71                 base::TimeDelta timeout_duration,
72                 ClientSocketFactory* client_socket_factory,
73                 HostResolver* host_resolver,
74                 Delegate* delegate,
75                 NetLog* net_log);
76   virtual ~TCPConnectJob();
77 
78   // ConnectJob methods.
79   virtual LoadState GetLoadState() const;
80 
81  private:
82   enum State {
83     STATE_RESOLVE_HOST,
84     STATE_RESOLVE_HOST_COMPLETE,
85     STATE_TCP_CONNECT,
86     STATE_TCP_CONNECT_COMPLETE,
87     STATE_NONE,
88   };
89 
90   void OnIOComplete(int result);
91 
92   // Runs the state transition loop.
93   int DoLoop(int result);
94 
95   int DoResolveHost();
96   int DoResolveHostComplete(int result);
97   int DoTCPConnect();
98   int DoTCPConnectComplete(int result);
99 
100   // Begins the host resolution and the TCP connect.  Returns OK on success
101   // and ERR_IO_PENDING if it cannot immediately service the request.
102   // Otherwise, it returns a net error code.
103   virtual int ConnectInternal();
104 
105   scoped_refptr<TCPSocketParams> params_;
106   ClientSocketFactory* const client_socket_factory_;
107   CompletionCallbackImpl<TCPConnectJob> callback_;
108   SingleRequestHostResolver resolver_;
109   AddressList addresses_;
110   State next_state_;
111 
112   // The time Connect() was called.
113   base::TimeTicks start_time_;
114 
115   // The time the connect was started (after DNS finished).
116   base::TimeTicks connect_start_time_;
117 
118   DISALLOW_COPY_AND_ASSIGN(TCPConnectJob);
119 };
120 
121 class TCPClientSocketPool : public ClientSocketPool {
122  public:
123   TCPClientSocketPool(
124       int max_sockets,
125       int max_sockets_per_group,
126       ClientSocketPoolHistograms* histograms,
127       HostResolver* host_resolver,
128       ClientSocketFactory* client_socket_factory,
129       NetLog* net_log);
130 
131   virtual ~TCPClientSocketPool();
132 
133   // ClientSocketPool methods:
134 
135   virtual int RequestSocket(const std::string& group_name,
136                             const void* resolve_info,
137                             RequestPriority priority,
138                             ClientSocketHandle* handle,
139                             CompletionCallback* callback,
140                             const BoundNetLog& net_log);
141 
142   virtual void RequestSockets(const std::string& group_name,
143                               const void* params,
144                               int num_sockets,
145                               const BoundNetLog& net_log);
146 
147   virtual void CancelRequest(const std::string& group_name,
148                              ClientSocketHandle* handle);
149 
150   virtual void ReleaseSocket(const std::string& group_name,
151                              ClientSocket* socket,
152                              int id);
153 
154   virtual void Flush();
155 
156   virtual void CloseIdleSockets();
157 
158   virtual int IdleSocketCount() const;
159 
160   virtual int IdleSocketCountInGroup(const std::string& group_name) const;
161 
162   virtual LoadState GetLoadState(const std::string& group_name,
163                                  const ClientSocketHandle* handle) const;
164 
165   virtual DictionaryValue* GetInfoAsValue(const std::string& name,
166                                           const std::string& type,
167                                           bool include_nested_pools) const;
168 
169   virtual base::TimeDelta ConnectionTimeout() const;
170 
171   virtual ClientSocketPoolHistograms* histograms() const;
172 
173  private:
174   typedef ClientSocketPoolBase<TCPSocketParams> PoolBase;
175 
176   class TCPConnectJobFactory
177       : public PoolBase::ConnectJobFactory {
178    public:
TCPConnectJobFactory(ClientSocketFactory * client_socket_factory,HostResolver * host_resolver,NetLog * net_log)179     TCPConnectJobFactory(ClientSocketFactory* client_socket_factory,
180                          HostResolver* host_resolver,
181                          NetLog* net_log)
182         : client_socket_factory_(client_socket_factory),
183           host_resolver_(host_resolver),
184           net_log_(net_log) {}
185 
~TCPConnectJobFactory()186     virtual ~TCPConnectJobFactory() {}
187 
188     // ClientSocketPoolBase::ConnectJobFactory methods.
189 
190     virtual ConnectJob* NewConnectJob(
191         const std::string& group_name,
192         const PoolBase::Request& request,
193         ConnectJob::Delegate* delegate) const;
194 
195     virtual base::TimeDelta ConnectionTimeout() const;
196 
197    private:
198     ClientSocketFactory* const client_socket_factory_;
199     HostResolver* const host_resolver_;
200     NetLog* net_log_;
201 
202     DISALLOW_COPY_AND_ASSIGN(TCPConnectJobFactory);
203   };
204 
205   PoolBase base_;
206 
207   DISALLOW_COPY_AND_ASSIGN(TCPClientSocketPool);
208 };
209 
210 REGISTER_SOCKET_PARAMS_FOR_POOL(TCPClientSocketPool, TCPSocketParams);
211 
212 }  // namespace net
213 
214 #endif  // NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_
215