summaryrefslogtreecommitdiffstats
path: root/net/socket_stream/socket_stream_throttle.h
blob: 7726cbe4e19c5afe9098a7b08d1120cac8e3a6d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_SOCKET_STREAM_SOCKET_STREAM_THROTTLE_H_
#define NET_SOCKET_STREAM_SOCKET_STREAM_THROTTLE_H_

#include <string>

#include "base/basictypes.h"
#include "net/base/completion_callback.h"
#include "net/base/net_errors.h"

namespace net {

class SocketStream;

// Abstract interface to throttle SocketStream per URL scheme.
// Each URL scheme (protocol) could define own SocketStreamThrottle.
// These methods will be called on IO thread.
class SocketStreamThrottle {
 public:
  // Called when |socket| is about to open connection.
  // Returns net::OK if the connection can open now.
  // Returns net::ERR_IO_PENDING if the connection should wait.  In this case,
  // |callback| will be called when it's ready to open connection.
  virtual int OnStartOpenConnection(SocketStream* socket,
                                    CompletionCallback* callback) {
    // No throttle by default.
    return OK;
  }

  // Called when |socket| read |len| bytes of |data|.
  // May wake up another waiting socket.
  // Returns net::OK if |socket| can continue to run.
  // Returns net::ERR_IO_PENDING if |socket| should suspend to run.  In this
  // case, |callback| will be called when it's ready to resume running.
  virtual int OnRead(SocketStream* socket, const char* data, int len,
                     CompletionCallback* callback) {
    // No throttle by default.
    return OK;
  }

  // Called when |socket| wrote |len| bytes of |data|.
  // May wake up another waiting socket.
  // Returns net::OK if |socket| can continue to run.
  // Returns net::ERR_IO_PENDING if |socket| should suspend to run.  In this
  // case, |callback| will be called when it's ready to resume running.
  virtual int OnWrite(SocketStream* socket, const char* data, int len,
                      CompletionCallback* callback) {
    // No throttle by default.
    return OK;
  }

  // Called when |socket| is closed.
  // May wake up another waiting socket.
  virtual void OnClose(SocketStream* socket) {}

  // Gets SocketStreamThrottle for URL |scheme|.
  // Doesn't pass ownership of the SocketStreamThrottle.
  static SocketStreamThrottle* GetSocketStreamThrottleForScheme(
      const std::string& scheme);

  // Registers |throttle| for URL |scheme|.
  // Doesn't take ownership of |throttle|.  Typically |throttle| is
  // singleton instance.
  static void RegisterSocketStreamThrottle(
      const std::string& scheme,
      SocketStreamThrottle* throttle);

 protected:
  SocketStreamThrottle() {}
  virtual ~SocketStreamThrottle() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(SocketStreamThrottle);
};

}  // namespace net

#endif  // NET_SOCKET_STREAM_SOCKET_STREAM_THROTTLE_H_