Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4631)

Unified Diff: chrome/browser/extensions/api/socket/tcp_socket.cc

Issue 10273016: Refactor the socket API to remove onEvent callback in socket.create() function. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix review issues Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/socket/tcp_socket.cc
diff --git a/chrome/browser/extensions/api/socket/tcp_socket.cc b/chrome/browser/extensions/api/socket/tcp_socket.cc
index cde2685a043646dd2e390ba2928748919a77fd6d..c47ad00b0a20682406a85603ea3c3026fdd5a960 100644
--- a/chrome/browser/extensions/api/socket/tcp_socket.cc
+++ b/chrome/browser/extensions/api/socket/tcp_socket.cc
@@ -38,23 +38,39 @@ TCPSocket::~TCPSocket() {
}
}
-int TCPSocket::Connect(const std::string& address, int port) {
- if (is_connected_)
- return net::ERR_CONNECTION_FAILED;
-
- net::AddressList address_list;
- if (!StringAndPortToAddressList(address, port, &address_list))
- return net::ERR_INVALID_ARGUMENT;
-
- socket_.reset(new net::TCPClientSocket(address_list, NULL,
- net::NetLog::Source()));
-
- int result = socket_->Connect(base::Bind(
- &TCPSocket::OnConnect, base::Unretained(this)));
- if (result == net::OK) {
- is_connected_ = true;
+void TCPSocket::Connect(const std::string& address,
+ int port,
+ const CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
+
+ if (!connect_callback_.is_null()) {
+ callback.Run(net::ERR_IO_PENDING);
miket_OOO 2012/05/03 17:08:13 Is there something like an "another IO operation a
Peng 2012/05/03 19:21:50 Linux returns -EALREADY for sock is connecting and
+ return;
}
- return result;
+ connect_callback_ = callback;
+
+ int result = net::ERR_CONNECTION_FAILED;
+ do {
+ if (is_connected_) {
+ result = net::ERR_CONNECTION_FAILED;
miket_OOO 2012/05/03 17:08:13 Might as well take advantage of the initialization
Peng 2012/05/03 19:21:50 Done.
+ break;
+ }
+
+ net::AddressList address_list;
+ if (!StringAndPortToAddressList(address, port, &address_list)) {
+ result = net::ERR_INVALID_ARGUMENT;
+ break;
+ }
+
+ socket_.reset(new net::TCPClientSocket(address_list, NULL,
+ net::NetLog::Source()));
+ connect_callback_ = callback;
+ result = socket_->Connect(base::Bind(
+ &TCPSocket::OnConnectComplete, base::Unretained(this)));
+ } while (0);
+
+ if (result != net::ERR_IO_PENDING)
+ OnConnectComplete(result);
}
void TCPSocket::Disconnect() {
@@ -67,42 +83,102 @@ int TCPSocket::Bind(const std::string& address, int port) {
return net::ERR_FAILED;
}
-int TCPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) {
- if (!socket_.get() || !socket_->IsConnected())
- return net::ERR_SOCKET_NOT_CONNECTED;
+void TCPSocket::Read(int count,
+ const ReadCompletionCallback& callback) {
+ DCHECK(!callback.is_null());
+
+ if (!read_callback_.is_null()) {
+ callback.Run(net::ERR_IO_PENDING, NULL);
miket_OOO 2012/05/03 17:08:13 Same comment as earlier.
Peng 2012/05/03 19:21:50 I can not find any better errors than IO_PENDING i
jeremya 2012/05/04 00:38:00 I don't think this system can ever return ERR_IO_P
+ return;
+ } else {
+ read_callback_ = callback;
+ }
- return socket_->Read(
- io_buffer.get(),
- io_buffer_len,
- base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer,
- static_cast<net::IPEndPoint*>(NULL)));
+ int result = net::ERR_FAILED;
+ scoped_refptr<net::IOBuffer> io_buffer;
+ do {
+ if (count < 0) {
+ result = net::ERR_INVALID_ARGUMENT;
+ break;
+ }
+
+ if (!socket_.get() || !socket_->IsConnected()) {
+ result = net::ERR_SOCKET_NOT_CONNECTED;
+ break;
+ }
+
+ io_buffer = new net::IOBuffer(count);
+ result = socket_->Read(io_buffer.get(), count,
+ base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this),
+ io_buffer));
+ } while (0);
+
+ if (result != net::ERR_IO_PENDING)
+ OnReadComplete(io_buffer, result);
}
-int TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) {
- if (!socket_.get() || !socket_->IsConnected())
- return net::ERR_SOCKET_NOT_CONNECTED;
+void TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer,
+ int byte_count,
+ const CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
+
+ if (!write_callback_.is_null()) {
+ // TODO(penghuang): Put requests in a pending queue to support multiple
+ // write calls.
+ callback.Run(net::ERR_IO_PENDING);
+ return;
+ } else {
+ write_callback_ = callback;
+ }
+
+ int result = net::ERR_FAILED;
+ do {
+ if (!socket_.get() || !socket_->IsConnected()) {
+ result = net::ERR_SOCKET_NOT_CONNECTED;
+ break;
+ }
- return socket_->Write(
- io_buffer.get(), byte_count,
- base::Bind(&Socket::OnWriteComplete, base::Unretained(this)));
+ result = socket_->Write(
+ io_buffer.get(), byte_count,
+ base::Bind(&TCPSocket::OnWriteComplete, base::Unretained(this)));
+ } while (0);
+
+ if (result != net::ERR_IO_PENDING)
+ OnWriteComplete(result);
}
-int TCPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer,
- int io_buffer_len,
- net::IPEndPoint* address) {
- return net::ERR_FAILED;
+void TCPSocket::RecvFrom(int count,
+ const RecvFromCompletionCallback& callback) {
+ callback.Run(net::ERR_FAILED, NULL, NULL, 0);
}
-int TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
- int byte_count,
- const std::string& address,
- int port) {
- return net::ERR_FAILED;
+void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
+ int byte_count,
+ const std::string& address,
+ int port,
+ const CompletionCallback& callback) {
+ callback.Run(net::ERR_FAILED);
}
-void TCPSocket::OnConnect(int result) {
+void TCPSocket::OnConnectComplete(int result) {
+ DCHECK(!connect_callback_.is_null());
+ DCHECK(!is_connected_);
is_connected_ = result == net::OK;
- event_notifier()->OnConnectComplete(result);
+ connect_callback_.Run(result);
+ connect_callback_.Reset();
+}
+
+void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer,
+ int result) {
+ DCHECK(!read_callback_.is_null());
+ read_callback_.Run(result, io_buffer);
+ read_callback_.Reset();
+}
+
+void TCPSocket::OnWriteComplete(int result) {
+ DCHECK(!write_callback_.is_null());
+ write_callback_.Run(result);
+ write_callback_.Reset();
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698