summaryrefslogtreecommitdiffstats
path: root/net/socket_stream
diff options
context:
space:
mode:
authorukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-17 07:29:54 +0000
committerukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-17 07:29:54 +0000
commitb4384a798aef3f2e52c34d733a9af2db792c6c1d (patch)
treef0bdc9e8f054e23481ac619cb38238ed59584626 /net/socket_stream
parentea477c25a06ac425257f1a94081aaca98929ab0f (diff)
downloadchromium_src-b4384a798aef3f2e52c34d733a9af2db792c6c1d.zip
chromium_src-b4384a798aef3f2e52c34d733a9af2db792c6c1d.tar.gz
chromium_src-b4384a798aef3f2e52c34d733a9af2db792c6c1d.tar.bz2
Refactor WebSocket throttling feature.
Protocol specific handling should be done in SocketStreamJob subclasss, so websocket throttling should be handled in WebSocketJob. Review URL: http://codereview.chromium.org/669157 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41818 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket_stream')
-rw-r--r--net/socket_stream/socket_stream.cc15
-rw-r--r--net/socket_stream/socket_stream.h9
-rw-r--r--net/socket_stream/socket_stream_throttle.cc80
-rw-r--r--net/socket_stream/socket_stream_throttle.h81
4 files changed, 9 insertions, 176 deletions
diff --git a/net/socket_stream/socket_stream.cc b/net/socket_stream/socket_stream.cc
index 03d7a9f..e46f306 100644
--- a/net/socket_stream/socket_stream.cc
+++ b/net/socket_stream/socket_stream.cc
@@ -26,7 +26,6 @@
#include "net/socket/socks_client_socket.h"
#include "net/socket/tcp_client_socket.h"
#include "net/socket_stream/socket_stream_metrics.h"
-#include "net/socket_stream/socket_stream_throttle.h"
#include "net/url_request/url_request.h"
static const int kMaxPendingSendAllowed = 32768; // 32 kilobytes.
@@ -59,16 +58,12 @@ SocketStream::SocketStream(const GURL& url, Delegate* delegate)
current_write_buf_(NULL),
write_buf_offset_(0),
write_buf_size_(0),
- throttle_(
- SocketStreamThrottle::GetSocketStreamThrottleForScheme(
- url.scheme())),
metrics_(new SocketStreamMetrics(url)) {
DCHECK(MessageLoop::current()) <<
"The current MessageLoop must exist";
DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) <<
"The current MessageLoop must be TYPE_IO";
DCHECK(delegate_);
- DCHECK(throttle_);
}
SocketStream::~SocketStream() {
@@ -235,7 +230,6 @@ void SocketStream::Finish(int result) {
if (delegate) {
delegate->OnClose(this);
}
- throttle_->OnClose(this);
Release();
}
@@ -275,13 +269,12 @@ int SocketStream::DidReceiveData(int result) {
net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_RECEIVED);
int len = result;
metrics_->OnRead(len);
- result = throttle_->OnRead(this, read_buf_->data(), len, &io_callback_);
if (delegate_) {
// Notify recevied data to delegate.
delegate_->OnReceivedData(this, read_buf_->data(), len);
}
read_buf_ = NULL;
- return result;
+ return OK;
}
int SocketStream::DidSendData(int result) {
@@ -289,8 +282,6 @@ int SocketStream::DidSendData(int result) {
net_log_.AddEvent(NetLog::TYPE_SOCKET_STREAM_SENT);
int len = result;
metrics_->OnWrite(len);
- result = throttle_->OnWrite(this, current_write_buf_->data(), len,
- &io_callback_);
current_write_buf_ = NULL;
if (delegate_)
delegate_->OnSentData(this, len);
@@ -309,7 +300,7 @@ int SocketStream::DidSendData(int result) {
} else {
write_buf_offset_ += len;
}
- return result;
+ return OK;
}
void SocketStream::OnIOCompleted(int result) {
@@ -490,7 +481,7 @@ int SocketStream::DoResolveHost() {
int SocketStream::DoResolveHostComplete(int result) {
if (result == OK) {
next_state_ = STATE_TCP_CONNECT;
- result = throttle_->OnStartOpenConnection(this, &io_callback_);
+ result = delegate_->OnStartOpenConnection(this, &io_callback_);
if (result == net::ERR_IO_PENDING)
metrics_->OnWaitConnection();
} else {
diff --git a/net/socket_stream/socket_stream.h b/net/socket_stream/socket_stream.h
index e8b250e..b8fd0bf 100644
--- a/net/socket_stream/socket_stream.h
+++ b/net/socket_stream/socket_stream.h
@@ -18,6 +18,7 @@
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_log.h"
+#include "net/base/net_errors.h"
#include "net/http/http_auth.h"
#include "net/http/http_auth_cache.h"
#include "net/http/http_auth_handler.h"
@@ -34,7 +35,6 @@ class HttpAuthHandlerFactory;
class SSLConfigService;
class SingleRequestHostResolver;
class SocketStreamMetrics;
-class SocketStreamThrottle;
// SocketStream is used to implement Web Sockets.
// It provides plain full-duplex stream with proxy and SSL support.
@@ -57,6 +57,11 @@ class SocketStream : public base::RefCountedThreadSafe<SocketStream> {
public:
virtual ~Delegate() {}
+ virtual int OnStartOpenConnection(SocketStream* socket,
+ CompletionCallback* callback) {
+ return OK;
+ }
+
// Called when socket stream has been connected. The socket stream accepts
// at most |max_pending_send_allowed| so that a client of the socket stream
// should keep track of how much it has pending and shouldn't go over
@@ -313,8 +318,6 @@ class SocketStream : public base::RefCountedThreadSafe<SocketStream> {
int write_buf_size_;
PendingDataQueue pending_write_bufs_;
- SocketStreamThrottle* throttle_;
-
scoped_ptr<SocketStreamMetrics> metrics_;
DISALLOW_COPY_AND_ASSIGN(SocketStream);
diff --git a/net/socket_stream/socket_stream_throttle.cc b/net/socket_stream/socket_stream_throttle.cc
deleted file mode 100644
index 6a1d20d..0000000
--- a/net/socket_stream/socket_stream_throttle.cc
+++ /dev/null
@@ -1,80 +0,0 @@
-// 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.
-
-#include <string>
-
-#include "net/socket_stream/socket_stream_throttle.h"
-
-#include "base/hash_tables.h"
-#include "base/singleton.h"
-#include "net/base/completion_callback.h"
-#include "net/socket_stream/socket_stream.h"
-
-namespace net {
-
-// Default SocketStreamThrottle. No throttling. Used for unknown URL scheme.
-class DefaultSocketStreamThrottle : public SocketStreamThrottle {
- private:
- DefaultSocketStreamThrottle() {}
- virtual ~DefaultSocketStreamThrottle() {}
- friend struct DefaultSingletonTraits<DefaultSocketStreamThrottle>;
-
- DISALLOW_COPY_AND_ASSIGN(DefaultSocketStreamThrottle);
-};
-
-class SocketStreamThrottleRegistry {
- public:
- SocketStreamThrottle* GetSocketStreamThrottleForScheme(
- const std::string& scheme);
-
- void RegisterSocketStreamThrottle(
- const std::string& scheme, SocketStreamThrottle* throttle);
-
- private:
- typedef base::hash_map<std::string, SocketStreamThrottle*> ThrottleMap;
-
- SocketStreamThrottleRegistry() {}
- ~SocketStreamThrottleRegistry() {}
- friend struct DefaultSingletonTraits<SocketStreamThrottleRegistry>;
-
- ThrottleMap throttles_;
-
- DISALLOW_COPY_AND_ASSIGN(SocketStreamThrottleRegistry);
-};
-
-SocketStreamThrottle*
-SocketStreamThrottleRegistry::GetSocketStreamThrottleForScheme(
- const std::string& scheme) {
- ThrottleMap::const_iterator found = throttles_.find(scheme);
- if (found == throttles_.end()) {
- SocketStreamThrottle* throttle =
- Singleton<DefaultSocketStreamThrottle>::get();
- throttles_[scheme] = throttle;
- return throttle;
- }
- return found->second;
-}
-
-void SocketStreamThrottleRegistry::RegisterSocketStreamThrottle(
- const std::string& scheme, SocketStreamThrottle* throttle) {
- throttles_[scheme] = throttle;
-}
-
-/* static */
-SocketStreamThrottle* SocketStreamThrottle::GetSocketStreamThrottleForScheme(
- const std::string& scheme) {
- SocketStreamThrottleRegistry* registry =
- Singleton<SocketStreamThrottleRegistry>::get();
- return registry->GetSocketStreamThrottleForScheme(scheme);
-}
-
-/* static */
-void SocketStreamThrottle::RegisterSocketStreamThrottle(
- const std::string& scheme, SocketStreamThrottle* throttle) {
- SocketStreamThrottleRegistry* registry =
- Singleton<SocketStreamThrottleRegistry>::get();
- registry->RegisterSocketStreamThrottle(scheme, throttle);
-}
-
-} // namespace net
diff --git a/net/socket_stream/socket_stream_throttle.h b/net/socket_stream/socket_stream_throttle.h
deleted file mode 100644
index 7726cbe..0000000
--- a/net/socket_stream/socket_stream_throttle.h
+++ /dev/null
@@ -1,81 +0,0 @@
-// 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_