| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/pepper/pepper_tcp_server_socket.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "content/browser/renderer_host/pepper/pepper_message_filter.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "net/base/ip_endpoint.h" | |
| 13 #include "net/base/net_errors.h" | |
| 14 #include "net/socket/tcp_client_socket.h" | |
| 15 #include "net/socket/tcp_server_socket.h" | |
| 16 #include "ppapi/c/pp_errors.h" | |
| 17 #include "ppapi/proxy/ppapi_messages.h" | |
| 18 #include "ppapi/shared_impl/private/net_address_private_impl.h" | |
| 19 | |
| 20 using ppapi::NetAddressPrivateImpl; | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 PepperTCPServerSocket::PepperTCPServerSocket( | |
| 25 PepperMessageFilter* manager, | |
| 26 int32 routing_id, | |
| 27 uint32 plugin_dispatcher_id, | |
| 28 PP_Resource socket_resource, | |
| 29 uint32 socket_id) | |
| 30 : manager_(manager), | |
| 31 routing_id_(routing_id), | |
| 32 plugin_dispatcher_id_(plugin_dispatcher_id), | |
| 33 socket_resource_(socket_resource), | |
| 34 socket_id_(socket_id), | |
| 35 state_(BEFORE_LISTENING) { | |
| 36 DCHECK(manager); | |
| 37 } | |
| 38 | |
| 39 PepperTCPServerSocket::~PepperTCPServerSocket() { | |
| 40 } | |
| 41 | |
| 42 void PepperTCPServerSocket::Listen(const PP_NetAddress_Private& addr, | |
| 43 int32 backlog) { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 45 | |
| 46 net::IPAddressNumber address; | |
| 47 int port; | |
| 48 if (state_ != BEFORE_LISTENING || | |
| 49 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address, &port)) { | |
| 50 CancelListenRequest(); | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 state_ = LISTEN_IN_PROGRESS; | |
| 55 | |
| 56 socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source())); | |
| 57 int result = socket_->Listen(net::IPEndPoint(address, port), backlog); | |
| 58 if (result != net::ERR_IO_PENDING) | |
| 59 OnListenCompleted(result); | |
| 60 } | |
| 61 | |
| 62 void PepperTCPServerSocket::Accept(int32 tcp_client_socket_routing_id) { | |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 64 | |
| 65 if (state_ != LISTENING) { | |
| 66 SendAcceptACKError(); | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 state_ = ACCEPT_IN_PROGRESS; | |
| 71 | |
| 72 int result = socket_->Accept( | |
| 73 &socket_buffer_, | |
| 74 base::Bind(&PepperTCPServerSocket::OnAcceptCompleted, | |
| 75 base::Unretained(this), | |
| 76 tcp_client_socket_routing_id)); | |
| 77 if (result != net::ERR_IO_PENDING) | |
| 78 OnAcceptCompleted(tcp_client_socket_routing_id, result); | |
| 79 } | |
| 80 | |
| 81 void PepperTCPServerSocket::CancelListenRequest() { | |
| 82 manager_->Send(new PpapiMsg_PPBTCPServerSocket_ListenACK( | |
| 83 routing_id_, | |
| 84 plugin_dispatcher_id_, | |
| 85 socket_resource_, | |
| 86 0, | |
| 87 NetAddressPrivateImpl::kInvalidNetAddress, | |
| 88 PP_ERROR_FAILED)); | |
| 89 BrowserThread::PostTask( | |
| 90 BrowserThread::IO, | |
| 91 FROM_HERE, | |
| 92 base::Bind(&PepperMessageFilter::RemoveTCPServerSocket, manager_, | |
| 93 socket_id_)); | |
| 94 } | |
| 95 | |
| 96 void PepperTCPServerSocket::SendAcceptACKError() { | |
| 97 manager_->Send(new PpapiMsg_PPBTCPServerSocket_AcceptACK( | |
| 98 routing_id_, | |
| 99 plugin_dispatcher_id_, | |
| 100 socket_id_, | |
| 101 0, | |
| 102 NetAddressPrivateImpl::kInvalidNetAddress, | |
| 103 NetAddressPrivateImpl::kInvalidNetAddress)); | |
| 104 } | |
| 105 | |
| 106 void PepperTCPServerSocket::OnListenCompleted(int result) { | |
| 107 DCHECK(state_ == LISTEN_IN_PROGRESS && socket_.get()); | |
| 108 | |
| 109 if (result != net::OK) { | |
| 110 CancelListenRequest(); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 net::IPEndPoint end_point; | |
| 115 PP_NetAddress_Private addr; | |
| 116 if (socket_->GetLocalAddress(&end_point) != net::OK || | |
| 117 !NetAddressPrivateImpl::IPEndPointToNetAddress(end_point.address(), | |
| 118 end_point.port(), | |
| 119 &addr)) { | |
| 120 CancelListenRequest(); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 manager_->Send(new PpapiMsg_PPBTCPServerSocket_ListenACK( | |
| 125 routing_id_, | |
| 126 plugin_dispatcher_id_, | |
| 127 socket_resource_, | |
| 128 socket_id_, | |
| 129 addr, | |
| 130 PP_OK)); | |
| 131 state_ = LISTENING; | |
| 132 } | |
| 133 | |
| 134 void PepperTCPServerSocket::OnAcceptCompleted( | |
| 135 int32 tcp_client_socket_routing_id, | |
| 136 int result) { | |
| 137 DCHECK(state_ == ACCEPT_IN_PROGRESS && socket_buffer_.get()); | |
| 138 | |
| 139 if (result != net::OK) { | |
| 140 SendAcceptACKError(); | |
| 141 } else { | |
| 142 scoped_ptr<net::StreamSocket> socket(socket_buffer_.release()); | |
| 143 | |
| 144 net::IPEndPoint ip_end_point_local; | |
| 145 net::IPEndPoint ip_end_point_remote; | |
| 146 PP_NetAddress_Private local_addr = | |
| 147 NetAddressPrivateImpl::kInvalidNetAddress; | |
| 148 PP_NetAddress_Private remote_addr = | |
| 149 NetAddressPrivateImpl::kInvalidNetAddress; | |
| 150 | |
| 151 if (socket->GetLocalAddress(&ip_end_point_local) != net::OK || | |
| 152 !NetAddressPrivateImpl::IPEndPointToNetAddress( | |
| 153 ip_end_point_local.address(), | |
| 154 ip_end_point_local.port(), | |
| 155 &local_addr) || | |
| 156 socket->GetPeerAddress(&ip_end_point_remote) != net::OK || | |
| 157 !NetAddressPrivateImpl::IPEndPointToNetAddress( | |
| 158 ip_end_point_remote.address(), | |
| 159 ip_end_point_remote.port(), | |
| 160 &remote_addr)) { | |
| 161 SendAcceptACKError(); | |
| 162 } else { | |
| 163 uint32 accepted_socket_id = | |
| 164 manager_->AddAcceptedTCPSocket(tcp_client_socket_routing_id, | |
| 165 plugin_dispatcher_id_, | |
| 166 socket.release()); | |
| 167 if (accepted_socket_id != 0) { | |
| 168 manager_->Send(new PpapiMsg_PPBTCPServerSocket_AcceptACK( | |
| 169 routing_id_, | |
| 170 plugin_dispatcher_id_, | |
| 171 socket_id_, | |
| 172 accepted_socket_id, | |
| 173 local_addr, | |
| 174 remote_addr)); | |
| 175 } else { | |
| 176 SendAcceptACKError(); | |
| 177 } | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 state_ = LISTENING; | |
| 182 } | |
| 183 | |
| 184 } // namespace content | |
| OLD | NEW |