[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 5 | #ifndef NET_SPDY_SPDY_SESSION_POOL_H_ |
| 6 | #define NET_SPDY_SPDY_SESSION_POOL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | #include <list> |
| 11 | #include <string> |
| 12 | |
[email protected] | 367ead4 | 2010-02-26 00:15:21 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 14 | #include "base/ref_counted.h" |
| 15 | #include "base/scoped_ptr.h" |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 16 | #include "net/base/host_port_pair.h" |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 18 | #include "net/base/network_change_notifier.h" |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 23 | class BoundNetLog; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 24 | class ClientSocketHandle; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 25 | class HttpNetworkSession; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 26 | class SpdySession; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 27 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 28 | // This is a very simple pool for open SpdySessions. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 29 | // TODO(mbelshe): Make this production ready. |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 30 | class SpdySessionPool |
| 31 | : public base::RefCounted<SpdySessionPool>, |
| 32 | public NetworkChangeNotifier::Observer { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 33 | public: |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 34 | SpdySessionPool(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 35 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 36 | // Either returns an existing SpdySession or creates a new SpdySession for |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 37 | // use. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 38 | scoped_refptr<SpdySession> Get( |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 39 | const HostPortPair& host_port_pair, HttpNetworkSession* session, |
| 40 | const BoundNetLog& net_log); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 41 | |
[email protected] | 4a5e178 | 2010-03-05 20:49:02 | [diff] [blame] | 42 | // Set the maximum concurrent sessions per domain. |
| 43 | static void set_max_sessions_per_domain(int max) { |
| 44 | if (max >= 1) |
| 45 | g_max_sessions_per_domain = max; |
| 46 | } |
| 47 | |
[email protected] | 5fe524e | 2010-02-20 00:43:22 | [diff] [blame] | 48 | // Builds a SpdySession from an existing SSL socket. Users should try |
| 49 | // calling Get() first to use an existing SpdySession so we don't get |
| 50 | // multiple SpdySessions per domain. Note that ownership of |connection| is |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 51 | // transferred from the caller to the SpdySession. |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 52 | // |certificate_error_code| is used to indicate the certificate error |
| 53 | // encountered when connecting the SSL socket. OK means there was no error. |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 54 | // For testing, setting is_secure to false allows Spdy to connect with a |
| 55 | // pre-existing TCP socket. |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 56 | // Returns OK on success, and the |spdy_session| will be provided. |
| 57 | // Returns an error on failure, and |spdy_session| will be NULL. |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 58 | net::Error GetSpdySessionFromSocket( |
[email protected] | 367ead4 | 2010-02-26 00:15:21 | [diff] [blame] | 59 | const HostPortPair& host_port_pair, |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 60 | HttpNetworkSession* session, |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 61 | ClientSocketHandle* connection, |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 62 | const BoundNetLog& net_log, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 63 | int certificate_error_code, |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 64 | scoped_refptr<SpdySession>* spdy_session, |
| 65 | bool is_secure); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 66 | |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 67 | // TODO(willchan): Consider renaming to HasReusableSession, since perhaps we |
| 68 | // should be creating a new session. |
[email protected] | 367ead4 | 2010-02-26 00:15:21 | [diff] [blame] | 69 | bool HasSession(const HostPortPair& host_port_pair)const; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 70 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 71 | // Close all Spdy Sessions; used for debugging. |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 72 | void CloseAllSessions(); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 73 | |
[email protected] | 6c6ea17 | 2010-07-27 20:04:03 | [diff] [blame^] | 74 | // Removes a SpdySession from the SpdySessionPool. This should only be called |
| 75 | // by SpdySession, because otherwise session->state_ is not set to CLOSED. |
[email protected] | 807c01a | 2010-04-21 16:57:45 | [diff] [blame] | 76 | void Remove(const scoped_refptr<SpdySession>& session); |
| 77 | |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 78 | // NetworkChangeNotifier::Observer methods: |
| 79 | |
| 80 | // We flush all idle sessions and release references to the active ones so |
| 81 | // they won't get re-used. The active ones will either complete successfully |
| 82 | // or error out due to the IP address change. |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 83 | virtual void OnIPAddressChanged(); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 84 | |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 85 | private: |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 86 | friend class base::RefCounted<SpdySessionPool>; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 87 | friend class SpdySessionPoolPeer; // For testing. |
[email protected] | cb54b3b2 | 2010-06-03 16:28:55 | [diff] [blame] | 88 | friend class SpdyNetworkTransactionTest; // For testing. |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 89 | FRIEND_TEST(SpdyNetworkTransactionTest, WindowUpdateOverflow); |
[email protected] | 9f7c4fd | 2009-11-24 18:50:15 | [diff] [blame] | 90 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 91 | typedef std::list<scoped_refptr<SpdySession> > SpdySessionList; |
[email protected] | 367ead4 | 2010-02-26 00:15:21 | [diff] [blame] | 92 | typedef std::map<HostPortPair, SpdySessionList*> SpdySessionsMap; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 93 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 94 | virtual ~SpdySessionPool(); |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 95 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 96 | // Helper functions for manipulating the lists. |
[email protected] | 367ead4 | 2010-02-26 00:15:21 | [diff] [blame] | 97 | SpdySessionList* AddSessionList(const HostPortPair& host_port_pair); |
| 98 | SpdySessionList* GetSessionList(const HostPortPair& host_port_pair); |
| 99 | const SpdySessionList* GetSessionList( |
| 100 | const HostPortPair& host_port_pair) const; |
| 101 | void RemoveSessionList(const HostPortPair& host_port_pair); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 102 | // Releases the SpdySessionPool reference to all sessions. Will result in all |
| 103 | // idle sessions being deleted, and the active sessions from being reused, so |
| 104 | // they will be deleted once all active streams belonging to that session go |
| 105 | // away. |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 106 | void ClearSessions(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 107 | |
| 108 | // This is our weak session pool - one session per domain. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 109 | SpdySessionsMap sessions_; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 110 | |
[email protected] | 4a5e178 | 2010-03-05 20:49:02 | [diff] [blame] | 111 | static int g_max_sessions_per_domain; |
| 112 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(SpdySessionPool); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace net |
| 117 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 118 | #endif // NET_SPDY_SPDY_SESSION_POOL_H_ |