• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FLIP_FLIP_SESSION_POOL_H_
6 #define NET_FLIP_FLIP_SESSION_POOL_H_
7 
8 #include <map>
9 #include <list>
10 #include <string>
11 
12 #include "base/ref_counted.h"
13 #include "base/scoped_ptr.h"
14 #include "net/base/host_resolver.h"
15 
16 namespace net {
17 
18 class ClientSocketHandle;
19 class FlipSession;
20 class HttpNetworkSession;
21 
22 // This is a very simple pool for open FlipSessions.
23 // TODO(mbelshe): Make this production ready.
24 class FlipSessionPool : public base::RefCounted<FlipSessionPool> {
25  public:
26   FlipSessionPool();
27 
28   // Either returns an existing FlipSession or creates a new FlipSession for
29   // use.
30   scoped_refptr<FlipSession> Get(
31       const HostResolver::RequestInfo& info, HttpNetworkSession* session);
32 
33   // Builds a FlipSession from an existing socket.  Users should try calling
34   // Get() first to use an existing FlipSession so we don't get multiple
35   // FlipSessions per domain.  Note that ownership of |connection| is
36   // transferred from the caller to the FlipSession.
37   scoped_refptr<FlipSession> GetFlipSessionFromSocket(
38       const HostResolver::RequestInfo& info,
39       HttpNetworkSession* session,
40       ClientSocketHandle* connection);
41 
42   // TODO(willchan): Consider renaming to HasReusableSession, since perhaps we
43   // should be creating a new session.
44   bool HasSession(const HostResolver::RequestInfo& info) const;
45 
46   // Close all Flip Sessions; used for debugging.
47   void CloseAllSessions();
48 
49  private:
50   friend class base::RefCounted<FlipSessionPool>;
51   friend class FlipSession;  // Needed for Remove().
52   friend class FlipSessionPoolPeer;  // For testing.
53 
54   typedef std::list<scoped_refptr<FlipSession> > FlipSessionList;
55   typedef std::map<std::string, FlipSessionList*> FlipSessionsMap;
56 
57   virtual ~FlipSessionPool();
58 
59   // Removes a FlipSession from the FlipSessionPool.
60   void Remove(const scoped_refptr<FlipSession>& session);
61 
62   // Helper functions for manipulating the lists.
63   FlipSessionList* AddSessionList(const std::string& domain);
64   FlipSessionList* GetSessionList(const std::string& domain);
65   const FlipSessionList* GetSessionList(const std::string& domain) const;
66   void RemoveSessionList(const std::string& domain);
67 
68   // This is our weak session pool - one session per domain.
69   FlipSessionsMap sessions_;
70 
71   DISALLOW_COPY_AND_ASSIGN(FlipSessionPool);
72 };
73 
74 }  // namespace net
75 
76 #endif  // NET_FLIP_FLIP_SESSION_POOL_H_
77