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 "content/browser/renderer_host/pepper/pepper_udp_socket.h" | 5 #include "content/browser/renderer_host/pepper/pepper_udp_socket_private_shared.
h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "content/browser/renderer_host/pepper/pepper_message_filter.h" | |
12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
15 #include "net/udp/udp_server_socket.h" | 14 #include "net/udp/udp_server_socket.h" |
16 #include "ppapi/proxy/ppapi_messages.h" | 15 #include "ppapi/c/private/ppb_net_address_private.h" |
| 16 #include "ppapi/c/private/ppb_udp_socket_private.h" |
17 #include "ppapi/shared_impl/private/net_address_private_impl.h" | 17 #include "ppapi/shared_impl/private/net_address_private_impl.h" |
18 | 18 |
19 using ppapi::NetAddressPrivateImpl; | 19 using ppapi::NetAddressPrivateImpl; |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 PepperUDPSocket::PepperUDPSocket( | 23 PepperUDPSocketPrivateShared::PepperUDPSocketPrivateShared() |
24 PepperMessageFilter* manager, | 24 : allow_address_reuse_(false), |
25 int32 routing_id, | 25 allow_broadcast_(false), |
26 uint32 plugin_dispatcher_id, | 26 closed_(false), |
27 uint32 socket_id) | 27 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
28 : manager_(manager), | |
29 routing_id_(routing_id), | |
30 plugin_dispatcher_id_(plugin_dispatcher_id), | |
31 socket_id_(socket_id), | |
32 allow_address_reuse_(false), | |
33 allow_broadcast_(false) { | |
34 DCHECK(manager); | |
35 } | 28 } |
36 | 29 |
37 PepperUDPSocket::~PepperUDPSocket() { | 30 PepperUDPSocketPrivateShared::~PepperUDPSocketPrivateShared() { |
38 // Make sure there are no further callbacks from socket_. | 31 // Make sure there are no further callbacks from socket_. |
39 if (socket_.get()) | 32 if (socket_.get()) |
40 socket_->Close(); | 33 socket_->Close(); |
41 } | 34 } |
42 | 35 |
43 void PepperUDPSocket::AllowAddressReuse(bool value) { | 36 void PepperUDPSocketPrivateShared::SetBoolSocketFeature(int32_t name, |
44 allow_address_reuse_ = value; | 37 bool value) { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 39 DCHECK(!socket_.get()); |
| 40 DCHECK(!closed()); |
| 41 |
| 42 switch(static_cast<PP_UDPSocketFeature_Private>(name)) { |
| 43 case PP_UDPSOCKETFEATURE_ADDRESS_REUSE: |
| 44 allow_address_reuse_ = value; |
| 45 break; |
| 46 case PP_UDPSOCKETFEATURE_BROADCAST: |
| 47 allow_broadcast_ = value; |
| 48 break; |
| 49 default: |
| 50 NOTREACHED(); |
| 51 break; |
| 52 } |
45 } | 53 } |
46 | 54 |
47 void PepperUDPSocket::AllowBroadcast(bool value) { | 55 void PepperUDPSocketPrivateShared::Bind(const PP_NetAddress_Private& addr) { |
48 allow_broadcast_ = value; | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
49 } | 57 DCHECK(!closed()); |
50 | 58 |
51 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { | |
52 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); | 59 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); |
53 | 60 |
54 net::IPAddressNumber address; | 61 net::IPAddressNumber address; |
55 int port; | 62 int port; |
56 if (!socket_.get() || | 63 if (!socket_.get() || |
57 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address, &port)) { | 64 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address, &port)) { |
58 SendBindACKError(); | 65 SendBindError(); |
59 return; | 66 return; |
60 } | 67 } |
61 | 68 |
62 if (allow_address_reuse_) | 69 if (allow_address_reuse_) |
63 socket_->AllowAddressReuse(); | 70 socket_->AllowAddressReuse(); |
64 if (allow_broadcast_) | 71 if (allow_broadcast_) |
65 socket_->AllowBroadcast(); | 72 socket_->AllowBroadcast(); |
66 | 73 |
67 int result = socket_->Listen(net::IPEndPoint(address, port)); | 74 int result = socket_->Listen(net::IPEndPoint(address, port)); |
68 | 75 |
69 if (result == net::OK && | 76 if (result == net::OK && |
70 socket_->GetLocalAddress(&bound_address_) != net::OK) { | 77 socket_->GetLocalAddress(&bound_address_) != net::OK) { |
71 SendBindACKError(); | 78 SendBindError(); |
72 return; | 79 return; |
73 } | 80 } |
74 | 81 |
75 OnBindCompleted(result); | 82 OnBindCompleted(result); |
76 } | 83 } |
77 | 84 |
78 void PepperUDPSocket::RecvFrom(int32_t num_bytes) { | 85 void PepperUDPSocketPrivateShared::RecvFrom(int32_t num_bytes) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 87 DCHECK(socket_.get()); |
| 88 DCHECK(!closed()); |
| 89 |
79 if (recvfrom_buffer_.get()) { | 90 if (recvfrom_buffer_.get()) { |
80 SendRecvFromACKError(); | 91 SendRecvFromError(); |
81 return; | 92 return; |
82 } | 93 } |
83 | 94 |
84 recvfrom_buffer_ = new net::IOBuffer(num_bytes); | 95 recvfrom_buffer_ = new net::IOBuffer(num_bytes); |
85 int result = socket_->RecvFrom( | 96 int result = socket_->RecvFrom( |
86 recvfrom_buffer_, num_bytes, &recvfrom_address_, | 97 recvfrom_buffer_, num_bytes, &recvfrom_address_, |
87 base::Bind(&PepperUDPSocket::OnRecvFromCompleted, | 98 base::Bind(&PepperUDPSocketPrivateShared::OnRecvFromCompleted, |
88 base::Unretained(this))); | 99 weak_factory_.GetWeakPtr())); |
89 | 100 |
90 if (result != net::ERR_IO_PENDING) | 101 if (result != net::ERR_IO_PENDING) |
91 OnRecvFromCompleted(result); | 102 OnRecvFromCompleted(result); |
92 } | 103 } |
93 | 104 |
94 void PepperUDPSocket::SendTo(const std::string& data, | 105 void PepperUDPSocketPrivateShared::SendTo(const std::string& data, |
95 const PP_NetAddress_Private& addr) { | 106 const PP_NetAddress_Private& addr) { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 108 DCHECK(socket_.get()); |
| 109 DCHECK(!closed()); |
| 110 |
96 if (sendto_buffer_.get() || data.empty()) { | 111 if (sendto_buffer_.get() || data.empty()) { |
97 SendSendToACKError(); | 112 SendSendToError(); |
98 return; | 113 return; |
99 } | 114 } |
100 | 115 |
101 net::IPAddressNumber address; | 116 net::IPAddressNumber address; |
102 int port; | 117 int port; |
103 if (!NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address, &port)) { | 118 if (!NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address, &port)) { |
104 SendSendToACKError(); | 119 SendSendToError(); |
105 return; | 120 return; |
106 } | 121 } |
107 | 122 |
108 int data_size = data.size(); | 123 int data_size = data.size(); |
109 | 124 |
110 sendto_buffer_ = new net::IOBuffer(data_size); | 125 sendto_buffer_ = new net::IOBuffer(data_size); |
111 memcpy(sendto_buffer_->data(), data.data(), data_size); | 126 memcpy(sendto_buffer_->data(), data.data(), data_size); |
112 int result = socket_->SendTo( | 127 int result = socket_->SendTo( |
113 sendto_buffer_, data_size, net::IPEndPoint(address, port), | 128 sendto_buffer_, data_size, net::IPEndPoint(address, port), |
114 base::Bind(&PepperUDPSocket::OnSendToCompleted, base::Unretained(this))); | 129 base::Bind(&PepperUDPSocketPrivateShared::OnSendToCompleted, |
| 130 weak_factory_.GetWeakPtr())); |
115 | 131 |
116 if (result != net::ERR_IO_PENDING) | 132 if (result != net::ERR_IO_PENDING) |
117 OnSendToCompleted(result); | 133 OnSendToCompleted(result); |
118 } | 134 } |
119 | 135 |
120 void PepperUDPSocket::SendRecvFromACKError() { | 136 void PepperUDPSocketPrivateShared::Close() { |
121 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 137 if (socket_.get()) |
122 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | 138 socket_->Close(); |
123 routing_id_, plugin_dispatcher_id_, socket_id_, false, std::string(), | 139 closed_ = true; |
124 addr)); | |
125 } | 140 } |
126 | 141 |
127 void PepperUDPSocket::SendSendToACKError() { | 142 void PepperUDPSocketPrivateShared::SendBindError() { |
128 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 143 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
129 routing_id_, plugin_dispatcher_id_, socket_id_, false, 0)); | 144 SendBindReply(false, addr); |
130 } | 145 } |
131 | 146 |
132 void PepperUDPSocket::SendBindACKError() { | 147 void PepperUDPSocketPrivateShared::SendRecvFromError() { |
133 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 148 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
134 manager_->Send(new PpapiMsg_PPBUDPSocket_BindACK( | 149 SendRecvFromReply(false, "", addr); |
135 routing_id_, plugin_dispatcher_id_, socket_id_, false, addr)); | |
136 } | 150 } |
137 | 151 |
138 void PepperUDPSocket::OnBindCompleted(int result) { | 152 void PepperUDPSocketPrivateShared::SendSendToError() { |
| 153 SendSendToReply(false, 0); |
| 154 } |
| 155 |
| 156 void PepperUDPSocketPrivateShared::OnBindCompleted(int result) { |
139 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 157 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
140 if (result < 0 || | 158 if (result < 0 || |
141 !NetAddressPrivateImpl::IPEndPointToNetAddress(bound_address_.address(), | 159 !NetAddressPrivateImpl::IPEndPointToNetAddress(bound_address_.address(), |
142 bound_address_.port(), | 160 bound_address_.port(), |
143 &addr)) { | 161 &addr)) { |
144 SendBindACKError(); | 162 SendBindError(); |
145 } else { | 163 } else { |
146 manager_->Send(new PpapiMsg_PPBUDPSocket_BindACK( | 164 SendBindReply(true, addr); |
147 routing_id_, plugin_dispatcher_id_, socket_id_, true, addr)); | |
148 } | 165 } |
149 } | 166 } |
150 | 167 |
151 void PepperUDPSocket::OnRecvFromCompleted(int result) { | 168 void PepperUDPSocketPrivateShared::OnRecvFromCompleted(int result) { |
152 DCHECK(recvfrom_buffer_.get()); | 169 DCHECK(recvfrom_buffer_.get()); |
153 | 170 |
154 // Convert IPEndPoint we get back from RecvFrom to a PP_NetAddress_Private, | 171 // Convert IPEndPoint we get back from RecvFrom to a PP_NetAddress_Private, |
155 // to send back. | 172 // to send back. |
156 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 173 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
157 if (result < 0 || | 174 if (result < 0 || |
158 !NetAddressPrivateImpl::IPEndPointToNetAddress( | 175 !NetAddressPrivateImpl::IPEndPointToNetAddress( |
159 recvfrom_address_.address(), | 176 recvfrom_address_.address(), |
160 recvfrom_address_.port(), | 177 recvfrom_address_.port(), |
161 &addr)) { | 178 &addr)) { |
162 SendRecvFromACKError(); | 179 SendRecvFromError(); |
163 } else { | 180 } else { |
164 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | 181 SendRecvFromReply(true, |
165 routing_id_, plugin_dispatcher_id_, socket_id_, true, | 182 std::string(recvfrom_buffer_->data(), result), |
166 std::string(recvfrom_buffer_->data(), result), addr)); | 183 addr); |
167 } | 184 } |
168 | 185 |
169 recvfrom_buffer_ = NULL; | 186 recvfrom_buffer_ = NULL; |
170 } | 187 } |
171 | 188 |
172 void PepperUDPSocket::OnSendToCompleted(int result) { | 189 void PepperUDPSocketPrivateShared::OnSendToCompleted(int result) { |
173 DCHECK(sendto_buffer_.get()); | 190 DCHECK(sendto_buffer_.get()); |
174 | 191 |
175 if (result < 0) { | 192 if (result < 0) |
176 SendSendToACKError(); | 193 SendSendToError(); |
177 } else { | 194 else |
178 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 195 SendSendToReply(true, result); |
179 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); | |
180 } | |
181 | 196 |
182 sendto_buffer_ = NULL; | 197 sendto_buffer_ = NULL; |
183 } | 198 } |
184 | 199 |
185 } // namespace content | 200 } // namespace content |
OLD | NEW |