diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-03 19:03:06 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-03 19:03:06 +0000 |
commit | 8b62f8e537893c76633cc94e2327c2c2bfc340ee (patch) | |
tree | 9f38f00402039886c0b306a193f87b6c9d127b3d /net | |
parent | df248dc033de8f3b48f5d2c4dc49d739537c8ad4 (diff) | |
download | chromium_src-8b62f8e537893c76633cc94e2327c2c2bfc340ee.zip chromium_src-8b62f8e537893c76633cc94e2327c2c2bfc340ee.tar.gz chromium_src-8b62f8e537893c76633cc94e2327c2c2bfc340ee.tar.bz2 |
Remove ref-counting from StreamListenSocket
Changes APIs for StreamListenSocket, TCPListenSocket, UnixDomainSocket and derived test classes to use scoped_ptr instead of scoped_refptr.
BUG=263963
Review URL: https://chromiumcodereview.appspot.com/20142003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220992 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
21 files changed, 126 insertions, 131 deletions
diff --git a/net/server/http_connection.cc b/net/server/http_connection.cc index d964cb0..d433012 100644 --- a/net/server/http_connection.cc +++ b/net/server/http_connection.cc @@ -29,21 +29,17 @@ void HttpConnection::Send(const HttpServerResponseInfo& response) { Send(response.Serialize()); } -HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock) +HttpConnection::HttpConnection(HttpServer* server, + scoped_ptr<StreamListenSocket> sock) : server_(server), - socket_(sock) { + socket_(sock.Pass()) { id_ = last_id_++; } HttpConnection::~HttpConnection() { - DetachSocket(); server_->delegate_->OnClose(id_); } -void HttpConnection::DetachSocket() { - socket_ = NULL; -} - void HttpConnection::Shift(int num_bytes) { recv_data_ = recv_data_.substr(num_bytes); } diff --git a/net/server/http_connection.h b/net/server/http_connection.h index b0e3766..17faa46 100644 --- a/net/server/http_connection.h +++ b/net/server/http_connection.h @@ -8,7 +8,6 @@ #include <string> #include "base/basictypes.h" -#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "net/http/http_status_code.h" @@ -36,12 +35,10 @@ class HttpConnection { friend class HttpServer; static int last_id_; - HttpConnection(HttpServer* server, StreamListenSocket* sock); - - void DetachSocket(); + HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); HttpServer* server_; - scoped_refptr<StreamListenSocket> socket_; + scoped_ptr<StreamListenSocket> socket_; scoped_ptr<WebSocket> web_socket_; std::string recv_data_; int id_; diff --git a/net/server/http_server.cc b/net/server/http_server.cc index 373025c..a51feb8 100644 --- a/net/server/http_server.cc +++ b/net/server/http_server.cc @@ -95,10 +95,11 @@ int HttpServer::GetLocalAddress(IPEndPoint* address) { } void HttpServer::DidAccept(StreamListenSocket* server, - StreamListenSocket* socket) { - HttpConnection* connection = new HttpConnection(this, socket); + scoped_ptr<StreamListenSocket> socket) { + HttpConnection* connection = new HttpConnection(this, socket.Pass()); id_to_connection_[connection->id()] = connection; - socket_to_connection_[socket] = connection; + // TODO(szym): Fix socket access. Make HttpConnection the Delegate. + socket_to_connection_[connection->socket_.get()] = connection; } void HttpServer::DidRead(StreamListenSocket* socket, @@ -180,7 +181,6 @@ void HttpServer::DidClose(StreamListenSocket* socket) { HttpServer::~HttpServer() { STLDeleteContainerPairSecondPointers( id_to_connection_.begin(), id_to_connection_.end()); - server_ = NULL; } // diff --git a/net/server/http_server.h b/net/server/http_server.h index f434575..51bec95 100644 --- a/net/server/http_server.h +++ b/net/server/http_server.h @@ -9,7 +9,7 @@ #include <map> #include "base/basictypes.h" -#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" #include "net/http/http_status_code.h" #include "net/socket/stream_listen_socket.h" @@ -65,7 +65,7 @@ class HttpServer : public StreamListenSocket::Delegate, // ListenSocketDelegate virtual void DidAccept(StreamListenSocket* server, - StreamListenSocket* socket) OVERRIDE; + scoped_ptr<StreamListenSocket> socket) OVERRIDE; virtual void DidRead(StreamListenSocket* socket, const char* data, int len) OVERRIDE; @@ -89,7 +89,7 @@ class HttpServer : public StreamListenSocket::Delegate, HttpConnection* FindConnection(StreamListenSocket* socket); HttpServer::Delegate* delegate_; - scoped_refptr<StreamListenSocket> server_; + scoped_ptr<StreamListenSocket> server_; typedef std::map<int, HttpConnection*> IdToConnectionMap; IdToConnectionMap id_to_connection_; typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap; diff --git a/net/server/http_server_unittest.cc b/net/server/http_server_unittest.cc index 48a2ce7..ede5066 100644 --- a/net/server/http_server_unittest.cc +++ b/net/server/http_server_unittest.cc @@ -276,9 +276,9 @@ class MockStreamListenSocket : public StreamListenSocket { } // namespace TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { - scoped_refptr<StreamListenSocket> socket( - new MockStreamListenSocket(server_.get())); - server_->DidAccept(NULL, socket.get()); + StreamListenSocket* socket = + new MockStreamListenSocket(server_.get()); + server_->DidAccept(NULL, make_scoped_ptr(socket)); std::string body("body"); std::string request = base::StringPrintf( "GET /test HTTP/1.1\r\n" @@ -286,9 +286,9 @@ TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { "Content-Length: %" PRIuS "\r\n\r\n%s", body.length(), body.c_str()); - server_->DidRead(socket.get(), request.c_str(), request.length() - 2); + server_->DidRead(socket, request.c_str(), request.length() - 2); ASSERT_EQ(0u, requests_.size()); - server_->DidRead(socket.get(), request.c_str() + request.length() - 2, 2); + server_->DidRead(socket, request.c_str() + request.length() - 2, 2); ASSERT_EQ(1u, requests_.size()); ASSERT_EQ(body, requests_[0].data); } diff --git a/net/socket/stream_listen_socket.h b/net/socket/stream_listen_socket.h index 4964b92..9825a4e 100644 --- a/net/socket/stream_listen_socket.h +++ b/net/socket/stream_listen_socket.h @@ -30,6 +30,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" #include "net/base/net_export.h" #include "net/socket/socket_descriptor.h" @@ -38,7 +39,7 @@ namespace net { class IPEndPoint; class NET_EXPORT StreamListenSocket - : public base::RefCountedThreadSafe<StreamListenSocket>, + : #if defined(OS_WIN) public base::win::ObjectWatcher::Delegate { #elif defined(OS_POSIX) @@ -46,16 +47,17 @@ class NET_EXPORT StreamListenSocket #endif public: + virtual ~StreamListenSocket(); + // TODO(erikkay): this delegate should really be split into two parts // to split up the listener from the connected socket. Perhaps this class // should be split up similarly. class Delegate { public: // |server| is the original listening Socket, connection is the new - // Socket that was created. Ownership of |connection| is transferred - // to the delegate with this call. + // Socket that was created. virtual void DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) = 0; + scoped_ptr<StreamListenSocket> connection) = 0; virtual void DidRead(StreamListenSocket* connection, const char* data, int len) = 0; @@ -82,7 +84,6 @@ class NET_EXPORT StreamListenSocket }; StreamListenSocket(SocketDescriptor s, Delegate* del); - virtual ~StreamListenSocket(); SocketDescriptor AcceptSocket(); virtual void Accept() = 0; @@ -100,7 +101,6 @@ class NET_EXPORT StreamListenSocket Delegate* const socket_delegate_; private: - friend class base::RefCountedThreadSafe<StreamListenSocket>; friend class TransportClientSocketTest; void SendInternal(const char* bytes, int len); @@ -139,7 +139,7 @@ class NET_EXPORT StreamListenSocketFactory { virtual ~StreamListenSocketFactory() {} // Returns a new instance of StreamListenSocket or NULL if an error occurred. - virtual scoped_refptr<StreamListenSocket> CreateAndListen( + virtual scoped_ptr<StreamListenSocket> CreateAndListen( StreamListenSocket::Delegate* delegate) const = 0; }; diff --git a/net/socket/tcp_listen_socket.cc b/net/socket/tcp_listen_socket.cc index 0ece37f..223abee 100644 --- a/net/socket/tcp_listen_socket.cc +++ b/net/socket/tcp_listen_socket.cc @@ -30,14 +30,14 @@ using std::string; namespace net { // static -scoped_refptr<TCPListenSocket> TCPListenSocket::CreateAndListen( +scoped_ptr<TCPListenSocket> TCPListenSocket::CreateAndListen( const string& ip, int port, StreamListenSocket::Delegate* del) { SocketDescriptor s = CreateAndBind(ip, port); if (s == kInvalidSocket) - return NULL; - scoped_refptr<TCPListenSocket> sock(new TCPListenSocket(s, del)); + return scoped_ptr<TCPListenSocket>(); + scoped_ptr<TCPListenSocket> sock(new TCPListenSocket(s, del)); sock->Listen(); - return sock; + return sock.Pass(); } TCPListenSocket::TCPListenSocket(SocketDescriptor s, @@ -101,13 +101,13 @@ void TCPListenSocket::Accept() { SocketDescriptor conn = AcceptSocket(); if (conn == kInvalidSocket) return; - scoped_refptr<TCPListenSocket> sock( + scoped_ptr<TCPListenSocket> sock( new TCPListenSocket(conn, socket_delegate_)); // It's up to the delegate to AddRef if it wants to keep it around. #if defined(OS_POSIX) sock->WatchSocket(WAITING_READ); #endif - socket_delegate_->DidAccept(this, sock.get()); + socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); } TCPListenSocketFactory::TCPListenSocketFactory(const string& ip, int port) @@ -117,9 +117,10 @@ TCPListenSocketFactory::TCPListenSocketFactory(const string& ip, int port) TCPListenSocketFactory::~TCPListenSocketFactory() {} -scoped_refptr<StreamListenSocket> TCPListenSocketFactory::CreateAndListen( +scoped_ptr<StreamListenSocket> TCPListenSocketFactory::CreateAndListen( StreamListenSocket::Delegate* delegate) const { - return TCPListenSocket::CreateAndListen(ip_, port_, delegate); + return TCPListenSocket::CreateAndListen(ip_, port_, delegate) + .PassAs<StreamListenSocket>(); } } // namespace net diff --git a/net/socket/tcp_listen_socket.h b/net/socket/tcp_listen_socket.h index 1756975..54a91de 100644 --- a/net/socket/tcp_listen_socket.h +++ b/net/socket/tcp_listen_socket.h @@ -8,19 +8,19 @@ #include <string> #include "base/basictypes.h" -#include "base/memory/ref_counted.h" #include "net/base/net_export.h" #include "net/socket/socket_descriptor.h" #include "net/socket/stream_listen_socket.h" namespace net { -// Implements a TCP socket. Note that this is ref counted. +// Implements a TCP socket. class NET_EXPORT TCPListenSocket : public StreamListenSocket { public: + virtual ~TCPListenSocket(); // Listen on port for the specified IP address. Use 127.0.0.1 to only // accept local connections. - static scoped_refptr<TCPListenSocket> CreateAndListen( + static scoped_ptr<TCPListenSocket> CreateAndListen( const std::string& ip, int port, StreamListenSocket::Delegate* del); // Get raw TCP socket descriptor bound to ip:port. @@ -31,10 +31,7 @@ class NET_EXPORT TCPListenSocket : public StreamListenSocket { int* port); protected: - friend class scoped_refptr<TCPListenSocket>; - TCPListenSocket(SocketDescriptor s, StreamListenSocket::Delegate* del); - virtual ~TCPListenSocket(); // Implements StreamListenSocket::Accept. virtual void Accept() OVERRIDE; @@ -50,7 +47,7 @@ class NET_EXPORT TCPListenSocketFactory : public StreamListenSocketFactory { virtual ~TCPListenSocketFactory(); // StreamListenSocketFactory overrides. - virtual scoped_refptr<StreamListenSocket> CreateAndListen( + virtual scoped_ptr<StreamListenSocket> CreateAndListen( StreamListenSocket::Delegate* delegate) const OVERRIDE; private: diff --git a/net/socket/tcp_listen_socket_unittest.cc b/net/socket/tcp_listen_socket_unittest.cc index 9589ce2..b122c61 100644 --- a/net/socket/tcp_listen_socket_unittest.cc +++ b/net/socket/tcp_listen_socket_unittest.cc @@ -26,8 +26,6 @@ static const int kDefaultTimeoutMs = 5000; TCPListenSocketTester::TCPListenSocketTester() : loop_(NULL), - server_(NULL), - connection_(NULL), cv_(&lock_), server_port_(0) {} @@ -121,17 +119,14 @@ int TCPListenSocketTester::ClearTestSocket() { } void TCPListenSocketTester::Shutdown() { - connection_->Release(); - connection_ = NULL; - server_->Release(); - server_ = NULL; + connection_.reset(); + server_.reset(); ReportAction(TCPListenSocketTestAction(ACTION_SHUTDOWN)); } void TCPListenSocketTester::Listen() { server_ = DoListen(); ASSERT_TRUE(server_.get()); - server_->AddRef(); // The server's port will be needed to open the client socket. IPEndPoint local_address; @@ -241,10 +236,10 @@ bool TCPListenSocketTester::Send(SocketDescriptor sock, return true; } -void TCPListenSocketTester::DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) { - connection_ = connection; - connection_->AddRef(); +void TCPListenSocketTester::DidAccept( + StreamListenSocket* server, + scoped_ptr<StreamListenSocket> connection) { + connection_ = connection.Pass(); ReportAction(TCPListenSocketTestAction(ACTION_ACCEPT)); } @@ -261,7 +256,7 @@ void TCPListenSocketTester::DidClose(StreamListenSocket* sock) { TCPListenSocketTester::~TCPListenSocketTester() {} -scoped_refptr<TCPListenSocket> TCPListenSocketTester::DoListen() { +scoped_ptr<TCPListenSocket> TCPListenSocketTester::DoListen() { // Let the OS pick a free port. return TCPListenSocket::CreateAndListen(kLoopback, 0, this); } diff --git a/net/socket/tcp_listen_socket_unittest.h b/net/socket/tcp_listen_socket_unittest.h index 93adbd5..1bc31a8 100644 --- a/net/socket/tcp_listen_socket_unittest.h +++ b/net/socket/tcp_listen_socket_unittest.h @@ -91,15 +91,15 @@ class TCPListenSocketTester : // StreamListenSocket::Delegate: virtual void DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) OVERRIDE; + scoped_ptr<StreamListenSocket> connection) OVERRIDE; virtual void DidRead(StreamListenSocket* connection, const char* data, int len) OVERRIDE; virtual void DidClose(StreamListenSocket* sock) OVERRIDE; scoped_ptr<base::Thread> thread_; base::MessageLoopForIO* loop_; - scoped_refptr<TCPListenSocket> server_; - StreamListenSocket* connection_; + scoped_ptr<TCPListenSocket> server_; + scoped_ptr<StreamListenSocket> connection_; TCPListenSocketTestAction last_action_; SocketDescriptor test_socket_; @@ -113,7 +113,7 @@ class TCPListenSocketTester : virtual ~TCPListenSocketTester(); - virtual scoped_refptr<TCPListenSocket> DoListen(); + virtual scoped_ptr<TCPListenSocket> DoListen(); // Getters/setters for |server_port_|. They use |lock_| for thread safety. int GetServerPort(); diff --git a/net/socket/transport_client_socket_unittest.cc b/net/socket/transport_client_socket_unittest.cc index 9282c20..5548b27 100644 --- a/net/socket/transport_client_socket_unittest.cc +++ b/net/socket/transport_client_socket_unittest.cc @@ -48,8 +48,9 @@ class TransportClientSocketTest // Implement StreamListenSocket::Delegate methods virtual void DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) OVERRIDE { - connected_sock_ = reinterpret_cast<TCPListenSocket*>(connection); + scoped_ptr<StreamListenSocket> connection) OVERRIDE { + connected_sock_.reset( + static_cast<TCPListenSocket*>(connection.release())); } virtual void DidRead(StreamListenSocket*, const char* str, int len) OVERRIDE { // TODO(dkegel): this might not be long enough to tickle some bugs. @@ -65,7 +66,7 @@ class TransportClientSocketTest void CloseServerSocket() { // delete the connected_sock_, which will close it. - connected_sock_ = NULL; + connected_sock_.reset(); } void PauseServerReads() { @@ -94,8 +95,8 @@ class TransportClientSocketTest scoped_ptr<StreamSocket> sock_; private: - scoped_refptr<TCPListenSocket> listen_sock_; - scoped_refptr<TCPListenSocket> connected_sock_; + scoped_ptr<TCPListenSocket> listen_sock_; + scoped_ptr<TCPListenSocket> connected_sock_; bool close_server_socket_on_next_send_; }; @@ -103,7 +104,7 @@ void TransportClientSocketTest::SetUp() { ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); // Find a free port to listen on - scoped_refptr<TCPListenSocket> sock; + scoped_ptr<TCPListenSocket> sock; int port; // Range of ports to listen on. Shouldn't need to try many. const int kMinPort = 10100; @@ -117,7 +118,7 @@ void TransportClientSocketTest::SetUp() { break; } ASSERT_TRUE(sock.get() != NULL); - listen_sock_ = sock; + listen_sock_ = sock.Pass(); listen_port_ = port; AddressList addr; diff --git a/net/socket/unix_domain_socket_posix.cc b/net/socket/unix_domain_socket_posix.cc index 9166024..2b781d5 100644 --- a/net/socket/unix_domain_socket_posix.cc +++ b/net/socket/unix_domain_socket_posix.cc @@ -54,7 +54,7 @@ UnixDomainSocket::AuthCallback UnixDomainSocket::NoAuthentication() { } // static -UnixDomainSocket* UnixDomainSocket::CreateAndListenInternal( +scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListenInternal( const std::string& path, const std::string& fallback_path, StreamListenSocket::Delegate* del, @@ -64,14 +64,15 @@ UnixDomainSocket* UnixDomainSocket::CreateAndListenInternal( if (s == kInvalidSocket && !fallback_path.empty()) s = CreateAndBind(fallback_path, use_abstract_namespace); if (s == kInvalidSocket) - return NULL; - UnixDomainSocket* sock = new UnixDomainSocket(s, del, auth_callback); + return scoped_ptr<UnixDomainSocket>(); + scoped_ptr<UnixDomainSocket> sock( + new UnixDomainSocket(s, del, auth_callback)); sock->Listen(); - return sock; + return sock.Pass(); } // static -scoped_refptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen( +scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen( const std::string& path, StreamListenSocket::Delegate* del, const AuthCallback& auth_callback) { @@ -80,14 +81,14 @@ scoped_refptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen( #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) // static -scoped_refptr<UnixDomainSocket> +scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListenWithAbstractNamespace( const std::string& path, const std::string& fallback_path, StreamListenSocket::Delegate* del, const AuthCallback& auth_callback) { - return make_scoped_refptr( - CreateAndListenInternal(path, fallback_path, del, auth_callback, true)); + return + CreateAndListenInternal(path, fallback_path, del, auth_callback, true); } #endif @@ -148,11 +149,11 @@ void UnixDomainSocket::Accept() { LOG(ERROR) << "close() error"; return; } - scoped_refptr<UnixDomainSocket> sock( + scoped_ptr<UnixDomainSocket> sock( new UnixDomainSocket(conn, socket_delegate_, auth_callback_)); // It's up to the delegate to AddRef if it wants to keep it around. sock->WatchSocket(WAITING_READ); - socket_delegate_->DidAccept(this, sock.get()); + socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); } UnixDomainSocketFactory::UnixDomainSocketFactory( @@ -163,10 +164,10 @@ UnixDomainSocketFactory::UnixDomainSocketFactory( UnixDomainSocketFactory::~UnixDomainSocketFactory() {} -scoped_refptr<StreamListenSocket> UnixDomainSocketFactory::CreateAndListen( +scoped_ptr<StreamListenSocket> UnixDomainSocketFactory::CreateAndListen( StreamListenSocket::Delegate* delegate) const { return UnixDomainSocket::CreateAndListen( - path_, delegate, auth_callback_); + path_, delegate, auth_callback_).PassAs<StreamListenSocket>(); } #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) @@ -182,11 +183,12 @@ UnixDomainSocketWithAbstractNamespaceFactory( UnixDomainSocketWithAbstractNamespaceFactory:: ~UnixDomainSocketWithAbstractNamespaceFactory() {} -scoped_refptr<StreamListenSocket> +scoped_ptr<StreamListenSocket> UnixDomainSocketWithAbstractNamespaceFactory::CreateAndListen( StreamListenSocket::Delegate* delegate) const { return UnixDomainSocket::CreateAndListenWithAbstractNamespace( - path_, fallback_path_, delegate, auth_callback_); + path_, fallback_path_, delegate, auth_callback_) + .PassAs<StreamListenSocket>(); } #endif diff --git a/net/socket/unix_domain_socket_posix.h b/net/socket/unix_domain_socket_posix.h index 2ef0680..98d0c11 100644 --- a/net/socket/unix_domain_socket_posix.h +++ b/net/socket/unix_domain_socket_posix.h @@ -10,7 +10,6 @@ #include "base/basictypes.h" #include "base/callback_forward.h" #include "base/compiler_specific.h" -#include "base/memory/ref_counted.h" #include "build/build_config.h" #include "net/base/net_export.h" #include "net/socket/stream_listen_socket.h" @@ -26,6 +25,8 @@ namespace net { // Unix Domain Socket Implementation. Supports abstract namespaces on Linux. class NET_EXPORT UnixDomainSocket : public StreamListenSocket { public: + virtual ~UnixDomainSocket(); + // Callback that returns whether the already connected client, identified by // its process |user_id| and |group_id|, is allowed to keep the connection // open. Note that the socket is closed immediately in case the callback @@ -38,7 +39,7 @@ class NET_EXPORT UnixDomainSocket : public StreamListenSocket { // Note that the returned UnixDomainSocket instance does not take ownership of // |del|. - static scoped_refptr<UnixDomainSocket> CreateAndListen( + static scoped_ptr<UnixDomainSocket> CreateAndListen( const std::string& path, StreamListenSocket::Delegate* del, const AuthCallback& auth_callback); @@ -47,7 +48,7 @@ class NET_EXPORT UnixDomainSocket : public StreamListenSocket { // Same as above except that the created socket uses the abstract namespace // which is a Linux-only feature. If |fallback_path| is not empty, // make the second attempt with the provided fallback name. - static scoped_refptr<UnixDomainSocket> CreateAndListenWithAbstractNamespace( + static scoped_ptr<UnixDomainSocket> CreateAndListenWithAbstractNamespace( const std::string& path, const std::string& fallback_path, StreamListenSocket::Delegate* del, @@ -58,9 +59,8 @@ class NET_EXPORT UnixDomainSocket : public StreamListenSocket { UnixDomainSocket(SocketDescriptor s, StreamListenSocket::Delegate* del, const AuthCallback& auth_callback); - virtual ~UnixDomainSocket(); - static UnixDomainSocket* CreateAndListenInternal( + static scoped_ptr<UnixDomainSocket> CreateAndListenInternal( const std::string& path, const std::string& fallback_path, StreamListenSocket::Delegate* del, @@ -87,7 +87,7 @@ class NET_EXPORT UnixDomainSocketFactory : public StreamListenSocketFactory { virtual ~UnixDomainSocketFactory(); // StreamListenSocketFactory: - virtual scoped_refptr<StreamListenSocket> CreateAndListen( + virtual scoped_ptr<StreamListenSocket> CreateAndListen( StreamListenSocket::Delegate* delegate) const OVERRIDE; protected: @@ -111,7 +111,7 @@ class NET_EXPORT UnixDomainSocketWithAbstractNamespaceFactory virtual ~UnixDomainSocketWithAbstractNamespaceFactory(); // UnixDomainSocketFactory: - virtual scoped_refptr<StreamListenSocket> CreateAndListen( + virtual scoped_ptr<StreamListenSocket> CreateAndListen( StreamListenSocket::Delegate* delegate) const OVERRIDE; private: diff --git a/net/socket/unix_domain_socket_posix_unittest.cc b/net/socket/unix_domain_socket_posix_unittest.cc index 65fda04..f062d27 100644 --- a/net/socket/unix_domain_socket_posix_unittest.cc +++ b/net/socket/unix_domain_socket_posix_unittest.cc @@ -103,9 +103,9 @@ class TestListenSocketDelegate : public StreamListenSocket::Delegate { : event_manager_(event_manager) {} virtual void DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) OVERRIDE { + scoped_ptr<StreamListenSocket> connection) OVERRIDE { LOG(ERROR) << __PRETTY_FUNCTION__; - connection_ = connection; + connection_ = connection.Pass(); Notify(EVENT_ACCEPT); } @@ -139,7 +139,7 @@ class TestListenSocketDelegate : public StreamListenSocket::Delegate { } const scoped_refptr<EventManager> event_manager_; - scoped_refptr<StreamListenSocket> connection_; + scoped_ptr<StreamListenSocket> connection_; base::Lock mutex_; string data_; }; @@ -173,7 +173,7 @@ class UnixDomainSocketTestHelper : public testing::Test { virtual void TearDown() OVERRIDE { DeleteSocketFile(); - socket_ = NULL; + socket_.reset(); socket_delegate_.reset(); event_manager_ = NULL; } @@ -222,7 +222,7 @@ class UnixDomainSocketTestHelper : public testing::Test { const bool allow_user_; scoped_refptr<EventManager> event_manager_; scoped_ptr<TestListenSocketDelegate> socket_delegate_; - scoped_refptr<UnixDomainSocket> socket_; + scoped_ptr<UnixDomainSocket> socket_; }; class UnixDomainSocketTest : public UnixDomainSocketTestHelper { @@ -265,7 +265,7 @@ TEST_F(UnixDomainSocketTestWithInvalidPath, } TEST_F(UnixDomainSocketTest, TestFallbackName) { - scoped_refptr<UnixDomainSocket> existing_socket = + scoped_ptr<UnixDomainSocket> existing_socket = UnixDomainSocket::CreateAndListenWithAbstractNamespace( file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); EXPECT_FALSE(existing_socket.get() == NULL); @@ -281,7 +281,6 @@ TEST_F(UnixDomainSocketTest, TestFallbackName) { socket_delegate_.get(), MakeAuthCallback()); EXPECT_FALSE(socket_.get() == NULL); - existing_socket = NULL; } #endif diff --git a/net/test/embedded_test_server/embedded_test_server.cc b/net/test/embedded_test_server/embedded_test_server.cc index d9196e1..db840db 100644 --- a/net/test/embedded_test_server/embedded_test_server.cc +++ b/net/test/embedded_test_server/embedded_test_server.cc @@ -156,7 +156,7 @@ void EmbeddedTestServer::InitializeOnIOThread() { if (socket_descriptor == kInvalidSocket) return; - listen_socket_ = new HttpListenSocket(socket_descriptor, this); + listen_socket_.reset(new HttpListenSocket(socket_descriptor, this)); listen_socket_->Listen(); IPEndPoint address; @@ -171,7 +171,7 @@ void EmbeddedTestServer::InitializeOnIOThread() { void EmbeddedTestServer::ShutdownOnIOThread() { DCHECK(io_thread_->BelongsToCurrentThread()); - listen_socket_ = NULL; // Release the listen socket. + listen_socket_.reset(); STLDeleteContainerPairSecondPointers(connections_.begin(), connections_.end()); connections_.clear(); @@ -224,15 +224,17 @@ void EmbeddedTestServer::RegisterRequestHandler( request_handlers_.push_back(callback); } -void EmbeddedTestServer::DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) { +void EmbeddedTestServer::DidAccept( + StreamListenSocket* server, + scoped_ptr<StreamListenSocket> connection) { DCHECK(io_thread_->BelongsToCurrentThread()); HttpConnection* http_connection = new HttpConnection( - connection, + connection.Pass(), base::Bind(&EmbeddedTestServer::HandleRequest, weak_factory_.GetWeakPtr())); - connections_[connection] = http_connection; + // TODO(szym): Make HttpConnection the StreamListenSocket delegate. + connections_[http_connection->socket_.get()] = http_connection; } void EmbeddedTestServer::DidRead(StreamListenSocket* connection, diff --git a/net/test/embedded_test_server/embedded_test_server.h b/net/test/embedded_test_server/embedded_test_server.h index 879c4a9..6a5203f 100644 --- a/net/test/embedded_test_server/embedded_test_server.h +++ b/net/test/embedded_test_server/embedded_test_server.h @@ -33,10 +33,10 @@ class HttpListenSocket : public TCPListenSocket { public: HttpListenSocket(const SocketDescriptor socket_descriptor, StreamListenSocket::Delegate* delegate); + virtual ~HttpListenSocket(); virtual void Listen(); private: - virtual ~HttpListenSocket(); base::ThreadChecker thread_checker_; }; @@ -137,7 +137,7 @@ class EmbeddedTestServer : public StreamListenSocket::Delegate { // StreamListenSocket::Delegate overrides: virtual void DidAccept(StreamListenSocket* server, - StreamListenSocket* connection) OVERRIDE; + scoped_ptr<StreamListenSocket> connection) OVERRIDE; virtual void DidRead(StreamListenSocket* connection, const char* data, int length) OVERRIDE; @@ -147,7 +147,7 @@ class EmbeddedTestServer : public StreamListenSocket::Delegate { scoped_refptr<base::SingleThreadTaskRunner> io_thread_; - scoped_refptr<HttpListenSocket> listen_socket_; + scoped_ptr<HttpListenSocket> listen_socket_; int port_; GURL base_url_; diff --git a/net/test/embedded_test_server/http_connection.cc b/net/test/embedded_test_server/http_connection.cc index 8b5317e..b7eab2eb 100644 --- a/net/test/embedded_test_server/http_connection.cc +++ b/net/test/embedded_test_server/http_connection.cc @@ -10,9 +10,9 @@ namespace net { namespace test_server { -HttpConnection::HttpConnection(StreamListenSocket* socket, +HttpConnection::HttpConnection(scoped_ptr<StreamListenSocket> socket, const HandleRequestCallback& callback) - : socket_(socket), + : socket_(socket.Pass()), callback_(callback) { } diff --git a/net/test/embedded_test_server/http_connection.h b/net/test/embedded_test_server/http_connection.h index da93534..870d122 100644 --- a/net/test/embedded_test_server/http_connection.h +++ b/net/test/embedded_test_server/http_connection.h @@ -7,7 +7,6 @@ #include "base/basictypes.h" #include "base/callback.h" -#include "base/memory/ref_counted.h" #include "base/strings/string_piece.h" #include "net/test/embedded_test_server/http_request.h" @@ -30,7 +29,7 @@ typedef base::Callback<void(HttpConnection* connection, // If a valid request is parsed, then |callback_| is invoked. class HttpConnection { public: - HttpConnection(StreamListenSocket* socket, + HttpConnection(scoped_ptr<StreamListenSocket> socket, const HandleRequestCallback& callback); ~HttpConnection(); @@ -45,7 +44,7 @@ class HttpConnection { // called. void ReceiveData(const base::StringPiece& data); - scoped_refptr<StreamListenSocket> socket_; + scoped_ptr<StreamListenSocket> socket_; const HandleRequestCallback callback_; HttpRequestParser request_parser_; diff --git a/net/tools/fetch/http_listen_socket.cc b/net/tools/fetch/http_listen_socket.cc index c8e1b1b..410a0ba 100644 --- a/net/tools/fetch/http_listen_socket.cc +++ b/net/tools/fetch/http_listen_socket.cc @@ -4,11 +4,10 @@ #include "net/tools/fetch/http_listen_socket.h" -#include <map> - #include "base/compiler_specific.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" +#include "base/stl_util.h" #include "base/strings/string_number_conversions.h" #include "net/tools/fetch/http_server_request_info.h" #include "net/tools/fetch/http_server_response_info.h" @@ -20,6 +19,7 @@ HttpListenSocket::HttpListenSocket(net::SocketDescriptor s, } HttpListenSocket::~HttpListenSocket() { + STLDeleteElements(&connections_); } void HttpListenSocket::Accept() { @@ -28,15 +28,14 @@ void HttpListenSocket::Accept() { if (conn == net::kInvalidSocket) { // TODO } else { - scoped_refptr<HttpListenSocket> sock( + scoped_ptr<StreamListenSocket> sock( new HttpListenSocket(conn, delegate_)); - // It's up to the delegate to AddRef if it wants to keep it around. - DidAccept(this, sock.get()); + DidAccept(this, sock.Pass()); } } // static -scoped_refptr<HttpListenSocket> HttpListenSocket::CreateAndListen( +scoped_ptr<HttpListenSocket> HttpListenSocket::CreateAndListen( const std::string& ip, int port, HttpListenSocket::Delegate* delegate) { @@ -44,11 +43,11 @@ scoped_refptr<HttpListenSocket> HttpListenSocket::CreateAndListen( if (s == net::kInvalidSocket) { // TODO (ibrar): error handling. } else { - scoped_refptr<HttpListenSocket> serv = new HttpListenSocket(s, delegate); + scoped_ptr<HttpListenSocket> serv(new HttpListenSocket(s, delegate)); serv->Listen(); - return serv; + return serv.Pass(); } - return NULL; + return scoped_ptr<HttpListenSocket>(); } // @@ -180,9 +179,10 @@ HttpServerRequestInfo* HttpListenSocket::ParseHeaders() { return NULL; } -void HttpListenSocket::DidAccept(net::StreamListenSocket* server, - net::StreamListenSocket* connection) { - connection->AddRef(); +void HttpListenSocket::DidAccept( + net::StreamListenSocket* server, + scoped_ptr<net::StreamListenSocket> connection) { + connections_.insert(connection.release()); } void HttpListenSocket::DidRead(net::StreamListenSocket* connection, @@ -199,7 +199,9 @@ void HttpListenSocket::DidRead(net::StreamListenSocket* connection, } void HttpListenSocket::DidClose(net::StreamListenSocket* sock) { - sock->Release(); + size_t count = connections_.erase(sock); + DCHECK_EQ(1u, count); + delete sock; } // Convert the numeric status code to a string. diff --git a/net/tools/fetch/http_listen_socket.h b/net/tools/fetch/http_listen_socket.h index 246948b..e0a58c0 100644 --- a/net/tools/fetch/http_listen_socket.h +++ b/net/tools/fetch/http_listen_socket.h @@ -5,6 +5,8 @@ #ifndef NET_BASE_TOOLS_HTTP_LISTEN_SOCKET_H_ #define NET_BASE_TOOLS_HTTP_LISTEN_SOCKET_H_ +#include <set> + #include "base/message_loop/message_loop.h" #include "net/socket/stream_listen_socket.h" #include "net/socket/tcp_listen_socket.h" @@ -25,7 +27,9 @@ class HttpListenSocket : public net::TCPListenSocket, virtual ~Delegate() {} }; - static scoped_refptr<HttpListenSocket> CreateAndListen( + virtual ~HttpListenSocket(); + + static scoped_ptr<HttpListenSocket> CreateAndListen( const std::string& ip, int port, HttpListenSocket::Delegate* delegate); // Send a server response. @@ -33,8 +37,9 @@ class HttpListenSocket : public net::TCPListenSocket, void Respond(HttpServerResponseInfo* info, std::string& data); // StreamListenSocket::Delegate. - virtual void DidAccept(net::StreamListenSocket* server, - net::StreamListenSocket* connection) OVERRIDE; + virtual void DidAccept( + net::StreamListenSocket* server, + scoped_ptr<net::StreamListenSocket> connection) OVERRIDE; virtual void DidRead(net::StreamListenSocket* connection, const char* data, int len) OVERRIDE; virtual void DidClose(net::StreamListenSocket* sock) OVERRIDE; @@ -44,13 +49,10 @@ class HttpListenSocket : public net::TCPListenSocket, virtual void Accept() OVERRIDE; private: - friend class base::RefCountedThreadSafe<net::StreamListenSocket>; - static const int kReadBufSize = 16 * 1024; // Must run in the IO thread. HttpListenSocket(net::SocketDescriptor s, HttpListenSocket::Delegate* del); - virtual ~HttpListenSocket(); // Expects the raw data to be stored in recv_data_. If parsing is successful, // will remove the data parsed from recv_data_, leaving only the unused @@ -60,6 +62,8 @@ class HttpListenSocket : public net::TCPListenSocket, HttpListenSocket::Delegate* const delegate_; std::string recv_data_; + std::set<StreamListenSocket*> connections_; + DISALLOW_COPY_AND_ASSIGN(HttpListenSocket); }; diff --git a/net/tools/fetch/http_session.h b/net/tools/fetch/http_session.h index 7d87e05..b0266f2a 100644 --- a/net/tools/fetch/http_session.h +++ b/net/tools/fetch/http_session.h @@ -19,7 +19,7 @@ class HttpSession : HttpListenSocket::Delegate { HttpServerRequestInfo* info) OVERRIDE; private: - scoped_refptr<HttpListenSocket> socket_; + scoped_ptr<HttpListenSocket> socket_; DISALLOW_COPY_AND_ASSIGN(HttpSession); }; |