Home | History | Annotate | Download | only in socket_stream
      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_THROTTLE_H_
      6 #define NET_SOCKET_STREAM_SOCKET_STREAM_THROTTLE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "net/base/completion_callback.h"
     12 #include "net/base/net_errors.h"
     13 
     14 namespace net {
     15 
     16 class SocketStream;
     17 
     18 // Abstract interface to throttle SocketStream per URL scheme.
     19 // Each URL scheme (protocol) could define own SocketStreamThrottle.
     20 // These methods will be called on IO thread.
     21 class SocketStreamThrottle {
     22  public:
     23   // Called when |socket| is about to open connection.
     24   // Returns net::OK if the connection can open now.
     25   // Returns net::ERR_IO_PENDING if the connection should wait.  In this case,
     26   // |callback| will be called when it's ready to open connection.
     27   virtual int OnStartOpenConnection(SocketStream* socket,
     28                                     CompletionCallback* callback) {
     29     // No throttle by default.
     30     return OK;
     31   }
     32 
     33   // Called when |socket| read |len| bytes of |data|.
     34   // May wake up another waiting socket.
     35   // Returns net::OK if |socket| can continue to run.
     36   // Returns net::ERR_IO_PENDING if |socket| should suspend to run.  In this
     37   // case, |callback| will be called when it's ready to resume running.
     38   virtual int OnRead(SocketStream* socket, const char* data, int len,
     39                      CompletionCallback* callback) {
     40     // No throttle by default.
     41     return OK;
     42   }
     43 
     44   // Called when |socket| wrote |len| bytes of |data|.
     45   // May wake up another waiting socket.
     46   // Returns net::OK if |socket| can continue to run.
     47   // Returns net::ERR_IO_PENDING if |socket| should suspend to run.  In this
     48   // case, |callback| will be called when it's ready to resume running.
     49   virtual int OnWrite(SocketStream* socket, const char* data, int len,
     50                       CompletionCallback* callback) {
     51     // No throttle by default.
     52     return OK;
     53   }
     54 
     55   // Called when |socket| is closed.
     56   // May wake up another waiting socket.
     57   virtual void OnClose(SocketStream* socket) {}
     58 
     59   // Gets SocketStreamThrottle for URL |scheme|.
     60   // Doesn't pass ownership of the SocketStreamThrottle.
     61   static SocketStreamThrottle* GetSocketStreamThrottleForScheme(
     62       const std::string& scheme);
     63 
     64   // Registers |throttle| for URL |scheme|.
     65   // Doesn't take ownership of |throttle|.  Typically |throttle| is
     66   // singleton instance.
     67   static void RegisterSocketStreamThrottle(
     68       const std::string& scheme,
     69       SocketStreamThrottle* throttle);
     70 
     71  protected:
     72   SocketStreamThrottle() {}
     73   virtual ~SocketStreamThrottle() {}
     74 
     75  private:
     76   DISALLOW_COPY_AND_ASSIGN(SocketStreamThrottle);
     77 };
     78 
     79 }  // namespace net
     80 
     81 #endif  // NET_SOCKET_STREAM_SOCKET_STREAM_THROTTLE_H_
     82