• 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 #include <string>
6 
7 #include "net/socket_stream/socket_stream_throttle.h"
8 
9 #include "base/hash_tables.h"
10 #include "base/singleton.h"
11 #include "net/base/completion_callback.h"
12 #include "net/socket_stream/socket_stream.h"
13 
14 namespace net {
15 
16 // Default SocketStreamThrottle.  No throttling. Used for unknown URL scheme.
17 class DefaultSocketStreamThrottle : public SocketStreamThrottle {
18  private:
DefaultSocketStreamThrottle()19   DefaultSocketStreamThrottle() {}
~DefaultSocketStreamThrottle()20   virtual ~DefaultSocketStreamThrottle() {}
21   friend struct DefaultSingletonTraits<DefaultSocketStreamThrottle>;
22 
23   DISALLOW_COPY_AND_ASSIGN(DefaultSocketStreamThrottle);
24 };
25 
26 class SocketStreamThrottleRegistry {
27  public:
28   SocketStreamThrottle* GetSocketStreamThrottleForScheme(
29       const std::string& scheme);
30 
31   void RegisterSocketStreamThrottle(
32       const std::string& scheme, SocketStreamThrottle* throttle);
33 
34  private:
35   typedef base::hash_map<std::string, SocketStreamThrottle*> ThrottleMap;
36 
SocketStreamThrottleRegistry()37   SocketStreamThrottleRegistry() {}
~SocketStreamThrottleRegistry()38   ~SocketStreamThrottleRegistry() {}
39   friend struct DefaultSingletonTraits<SocketStreamThrottleRegistry>;
40 
41   ThrottleMap throttles_;
42 
43   DISALLOW_COPY_AND_ASSIGN(SocketStreamThrottleRegistry);
44 };
45 
46 SocketStreamThrottle*
GetSocketStreamThrottleForScheme(const std::string & scheme)47 SocketStreamThrottleRegistry::GetSocketStreamThrottleForScheme(
48     const std::string& scheme) {
49   ThrottleMap::const_iterator found = throttles_.find(scheme);
50   if (found == throttles_.end()) {
51     SocketStreamThrottle* throttle =
52         Singleton<DefaultSocketStreamThrottle>::get();
53     throttles_[scheme] = throttle;
54     return throttle;
55   }
56   return found->second;
57 }
58 
RegisterSocketStreamThrottle(const std::string & scheme,SocketStreamThrottle * throttle)59 void SocketStreamThrottleRegistry::RegisterSocketStreamThrottle(
60     const std::string& scheme, SocketStreamThrottle* throttle) {
61   throttles_[scheme] = throttle;
62 }
63 
64 /* static */
GetSocketStreamThrottleForScheme(const std::string & scheme)65 SocketStreamThrottle* SocketStreamThrottle::GetSocketStreamThrottleForScheme(
66     const std::string& scheme) {
67   SocketStreamThrottleRegistry* registry =
68       Singleton<SocketStreamThrottleRegistry>::get();
69   return registry->GetSocketStreamThrottleForScheme(scheme);
70 }
71 
72 /* static */
RegisterSocketStreamThrottle(const std::string & scheme,SocketStreamThrottle * throttle)73 void SocketStreamThrottle::RegisterSocketStreamThrottle(
74     const std::string& scheme, SocketStreamThrottle* throttle) {
75   SocketStreamThrottleRegistry* registry =
76       Singleton<SocketStreamThrottleRegistry>::get();
77   registry->RegisterSocketStreamThrottle(scheme, throttle);
78 }
79 
80 }  // namespace net
81