| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/api_resource.h" | 7 #include "chrome/browser/extensions/api/api_resource.h" |
| 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
| 9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 10 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 APIResourceEventNotifier* event_notifier) { | 31 APIResourceEventNotifier* event_notifier) { |
| 32 return new TCPSocket(tcp_client_socket, event_notifier); | 32 return new TCPSocket(tcp_client_socket, event_notifier); |
| 33 } | 33 } |
| 34 | 34 |
| 35 TCPSocket::~TCPSocket() { | 35 TCPSocket::~TCPSocket() { |
| 36 if (is_connected_) { | 36 if (is_connected_) { |
| 37 Disconnect(); | 37 Disconnect(); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 int TCPSocket::Connect(const std::string& address, int port) { | 41 void TCPSocket::Connect(const std::string& address, |
| 42 if (is_connected_) | 42 int port, |
| 43 return net::ERR_CONNECTION_FAILED; | 43 const CompletionCallback& callback) { |
| 44 DCHECK(!callback.is_null()); |
| 44 | 45 |
| 45 net::AddressList address_list; | 46 if (!connect_callback_.is_null()) { |
| 46 if (!StringAndPortToAddressList(address, port, &address_list)) | 47 callback.Run(net::ERR_CONNECTION_FAILED); |
| 47 return net::ERR_INVALID_ARGUMENT; | 48 return; |
| 49 } |
| 50 connect_callback_ = callback; |
| 48 | 51 |
| 49 socket_.reset(new net::TCPClientSocket(address_list, NULL, | 52 int result = net::ERR_CONNECTION_FAILED; |
| 50 net::NetLog::Source())); | 53 do { |
| 54 if (is_connected_) |
| 55 break; |
| 51 | 56 |
| 52 int result = socket_->Connect(base::Bind( | 57 net::AddressList address_list; |
| 53 &TCPSocket::OnConnect, base::Unretained(this))); | 58 if (!StringAndPortToAddressList(address, port, &address_list)) { |
| 54 if (result == net::OK) { | 59 result = net::ERR_ADDRESS_INVALID; |
| 55 is_connected_ = true; | 60 break; |
| 56 } | 61 } |
| 57 return result; | 62 |
| 63 socket_.reset(new net::TCPClientSocket(address_list, NULL, |
| 64 net::NetLog::Source())); |
| 65 connect_callback_ = callback; |
| 66 result = socket_->Connect(base::Bind( |
| 67 &TCPSocket::OnConnectComplete, base::Unretained(this))); |
| 68 } while (false); |
| 69 |
| 70 if (result != net::ERR_IO_PENDING) |
| 71 OnConnectComplete(result); |
| 58 } | 72 } |
| 59 | 73 |
| 60 void TCPSocket::Disconnect() { | 74 void TCPSocket::Disconnect() { |
| 61 is_connected_ = false; | 75 is_connected_ = false; |
| 62 socket_->Disconnect(); | 76 socket_->Disconnect(); |
| 63 } | 77 } |
| 64 | 78 |
| 65 int TCPSocket::Bind(const std::string& address, int port) { | 79 int TCPSocket::Bind(const std::string& address, int port) { |
| 66 // TODO(penghuang): Supports bind for tcp? | 80 // TODO(penghuang): Supports bind for tcp? |
| 67 return net::ERR_FAILED; | 81 return net::ERR_FAILED; |
| 68 } | 82 } |
| 69 | 83 |
| 70 int TCPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) { | 84 void TCPSocket::Read(int count, |
| 71 if (!socket_.get() || !socket_->IsConnected()) | 85 const ReadCompletionCallback& callback) { |
| 72 return net::ERR_SOCKET_NOT_CONNECTED; | 86 DCHECK(!callback.is_null()); |
| 73 | 87 |
| 74 return socket_->Read( | 88 if (!read_callback_.is_null()) { |
| 75 io_buffer.get(), | 89 callback.Run(net::ERR_IO_PENDING, NULL); |
| 76 io_buffer_len, | 90 return; |
| 77 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, | 91 } else { |
| 78 static_cast<net::IPEndPoint*>(NULL))); | 92 read_callback_ = callback; |
| 93 } |
| 94 |
| 95 int result = net::ERR_FAILED; |
| 96 scoped_refptr<net::IOBuffer> io_buffer; |
| 97 do { |
| 98 if (count < 0) { |
| 99 result = net::ERR_INVALID_ARGUMENT; |
| 100 break; |
| 101 } |
| 102 |
| 103 if (!socket_.get() || !socket_->IsConnected()) { |
| 104 result = net::ERR_SOCKET_NOT_CONNECTED; |
| 105 break; |
| 106 } |
| 107 |
| 108 io_buffer = new net::IOBuffer(count); |
| 109 result = socket_->Read(io_buffer.get(), count, |
| 110 base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this), |
| 111 io_buffer)); |
| 112 } while (false); |
| 113 |
| 114 if (result != net::ERR_IO_PENDING) |
| 115 OnReadComplete(io_buffer, result); |
| 79 } | 116 } |
| 80 | 117 |
| 81 int TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { | 118 void TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, |
| 82 if (!socket_.get() || !socket_->IsConnected()) | 119 int byte_count, |
| 83 return net::ERR_SOCKET_NOT_CONNECTED; | 120 const CompletionCallback& callback) { |
| 121 DCHECK(!callback.is_null()); |
| 84 | 122 |
| 85 return socket_->Write( | 123 if (!write_callback_.is_null()) { |
| 86 io_buffer.get(), byte_count, | 124 // TODO(penghuang): Put requests in a pending queue to support multiple |
| 87 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); | 125 // write calls. |
| 126 callback.Run(net::ERR_IO_PENDING); |
| 127 return; |
| 128 } else { |
| 129 write_callback_ = callback; |
| 130 } |
| 131 |
| 132 int result = net::ERR_FAILED; |
| 133 do { |
| 134 if (!socket_.get() || !socket_->IsConnected()) { |
| 135 result = net::ERR_SOCKET_NOT_CONNECTED; |
| 136 break; |
| 137 } |
| 138 |
| 139 result = socket_->Write( |
| 140 io_buffer.get(), byte_count, |
| 141 base::Bind(&TCPSocket::OnWriteComplete, base::Unretained(this))); |
| 142 } while (false); |
| 143 |
| 144 if (result != net::ERR_IO_PENDING) |
| 145 OnWriteComplete(result); |
| 88 } | 146 } |
| 89 | 147 |
| 90 int TCPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, | 148 void TCPSocket::RecvFrom(int count, |
| 91 int io_buffer_len, | 149 const RecvFromCompletionCallback& callback) { |
| 92 net::IPEndPoint* address) { | 150 callback.Run(net::ERR_FAILED, NULL, NULL, 0); |
| 93 return net::ERR_FAILED; | |
| 94 } | 151 } |
| 95 | 152 |
| 96 int TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 153 void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 97 int byte_count, | 154 int byte_count, |
| 98 const std::string& address, | 155 const std::string& address, |
| 99 int port) { | 156 int port, |
| 100 return net::ERR_FAILED; | 157 const CompletionCallback& callback) { |
| 158 callback.Run(net::ERR_FAILED); |
| 101 } | 159 } |
| 102 | 160 |
| 103 void TCPSocket::OnConnect(int result) { | 161 void TCPSocket::OnConnectComplete(int result) { |
| 162 DCHECK(!connect_callback_.is_null()); |
| 163 DCHECK(!is_connected_); |
| 104 is_connected_ = result == net::OK; | 164 is_connected_ = result == net::OK; |
| 105 event_notifier()->OnConnectComplete(result); | 165 connect_callback_.Run(result); |
| 166 connect_callback_.Reset(); |
| 167 } |
| 168 |
| 169 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, |
| 170 int result) { |
| 171 DCHECK(!read_callback_.is_null()); |
| 172 read_callback_.Run(result, io_buffer); |
| 173 read_callback_.Reset(); |
| 174 } |
| 175 |
| 176 void TCPSocket::OnWriteComplete(int result) { |
| 177 DCHECK(!write_callback_.is_null()); |
| 178 write_callback_.Run(result); |
| 179 write_callback_.Reset(); |
| 106 } | 180 } |
| 107 | 181 |
| 108 } // namespace extensions | 182 } // namespace extensions |
| OLD | NEW |