Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/socket/tls_socket.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "chrome/browser/extensions/api/api_resource.h" | |
| 9 #include "net/base/address_list.h" | |
| 10 #include "net/base/ip_endpoint.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/base/rand_callback.h" | |
| 13 #include "net/socket/tcp_client_socket.h" | |
| 14 | |
| 15 namespace extensions { | |
| 16 | |
| 17 const char kTLSSocketTypeInvalidError[] = | |
| 18 "Cannot listen on a socket that's already connected."; | |
| 19 | |
| 20 TLSSocket::TLSSocket(net::StreamSocket* tls_socket, | |
| 21 net::TCPClientSocket* underlying_tcp_socket, | |
| 22 const std::string& owner_extension_id) | |
| 23 : Socket(owner_extension_id), | |
| 24 tls_socket_(tls_socket), | |
| 25 underlying_socket_(underlying_tcp_socket) { | |
| 26 } | |
| 27 | |
| 28 TLSSocket::~TLSSocket() { | |
| 29 Disconnect(); | |
| 30 } | |
| 31 | |
| 32 void TLSSocket::Connect(const std::string& address, | |
| 33 int port, | |
| 34 const CompletionCallback& callback) { | |
| 35 callback.Run(net::ERR_CONNECTION_FAILED); | |
| 36 } | |
| 37 | |
| 38 void TLSSocket::Disconnect() { | |
| 39 if (tls_socket_) { | |
| 40 tls_socket_->Disconnect(); | |
| 41 tls_socket_.reset(); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 int TLSSocket::Bind(const std::string& address, int port) { | |
| 46 return net::ERR_NOT_IMPLEMENTED; | |
| 47 } | |
| 48 | |
| 49 void TLSSocket::Read(int count, | |
| 50 const ReadCompletionCallback& callback) { | |
| 51 DCHECK(!callback.is_null()); | |
| 52 | |
| 53 if (!read_callback_.is_null()) { | |
| 54 callback.Run(net::ERR_IO_PENDING, NULL); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 if (count <= 0) { | |
| 59 callback.Run(net::ERR_INVALID_ARGUMENT, NULL); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 if (!tls_socket_.get() || !IsConnected()) { | |
| 64 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 read_callback_ = callback; | |
| 69 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count); | |
| 70 int result = tls_socket_->Read( | |
| 71 io_buffer.get(), count, base::Bind(&TLSSocket::OnReadComplete, | |
| 72 base::Unretained(this), | |
| 73 io_buffer)); | |
| 74 | |
| 75 if (result != net::ERR_IO_PENDING) | |
| 76 OnReadComplete(io_buffer, result); | |
|
Ryan Sleevi
2013/11/25 17:30:13
This is a dangerous pattern (see comments in the T
Lally Singh
2013/12/05 17:07:12
Fair enough on both counts. The existing APIs do
| |
| 77 } | |
| 78 | |
| 79 void TLSSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | |
| 80 int result) { | |
| 81 DCHECK(!read_callback_.is_null()); | |
| 82 read_callback_.Run(result, io_buffer); | |
| 83 read_callback_.Reset(); | |
| 84 } | |
| 85 | |
| 86 int TLSSocket::WriteImpl(net::IOBuffer* io_buffer, | |
| 87 int io_buffer_size, | |
| 88 const net::CompletionCallback& callback) { | |
| 89 if (!IsConnected()) | |
| 90 return net::ERR_SOCKET_NOT_CONNECTED; | |
| 91 else | |
| 92 return tls_socket_->Write(io_buffer, io_buffer_size, callback); | |
| 93 } | |
| 94 | |
| 95 void TLSSocket::RecvFrom(int count, | |
| 96 const RecvFromCompletionCallback& callback) { | |
| 97 callback.Run(net::ERR_FAILED, NULL, NULL, 0); | |
| 98 } | |
| 99 | |
| 100 void TLSSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, | |
| 101 int byte_count, | |
| 102 const std::string& address, | |
| 103 int port, | |
| 104 const CompletionCallback& callback) { | |
| 105 callback.Run(net::ERR_FAILED); | |
| 106 } | |
| 107 | |
| 108 bool TLSSocket::SetKeepAlive(bool enable, int delay) { | |
| 109 return IsConnected() && underlying_socket_->SetKeepAlive(enable, delay); | |
| 110 } | |
| 111 | |
| 112 bool TLSSocket::SetNoDelay(bool no_delay) { | |
| 113 return IsConnected() && underlying_socket_->SetNoDelay(no_delay); | |
| 114 } | |
| 115 | |
| 116 int TLSSocket::Listen(const std::string& address, int port, int backlog, | |
| 117 std::string* error_msg) { | |
| 118 *error_msg = kTLSSocketTypeInvalidError; | |
| 119 return net::ERR_NOT_IMPLEMENTED; | |
| 120 } | |
| 121 | |
| 122 void TLSSocket::Accept(const AcceptCompletionCallback &callback) { | |
| 123 callback.Run(net::ERR_FAILED, NULL); | |
| 124 } | |
| 125 | |
| 126 bool TLSSocket::IsConnected() { | |
| 127 return tls_socket_.get() && tls_socket_->IsConnected() && | |
| 128 underlying_socket_.get() && underlying_socket_->IsConnected(); | |
| 129 } | |
| 130 | |
| 131 bool TLSSocket::GetPeerAddress(net::IPEndPoint* address) { | |
| 132 return IsConnected() && underlying_socket_->GetPeerAddress(address); | |
| 133 } | |
| 134 | |
| 135 bool TLSSocket::GetLocalAddress(net::IPEndPoint* address) { | |
| 136 return IsConnected() && underlying_socket_->GetLocalAddress(address); | |
| 137 } | |
| 138 | |
| 139 Socket::SocketType TLSSocket::GetSocketType() const { | |
| 140 return Socket::TYPE_TCP; | |
| 141 } | |
| 142 | |
| 143 } // namespace extensions | |
| OLD | NEW |