OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ppapi/proxy/udp_socket_resource_base.h" | 5 #include "ppapi/proxy/udp_socket_resource_base.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 } | 61 } |
62 | 62 |
63 int32_t UDPSocketResourceBase::SetOptionImpl( | 63 int32_t UDPSocketResourceBase::SetOptionImpl( |
64 PP_UDPSocket_Option name, | 64 PP_UDPSocket_Option name, |
65 const PP_Var& value, | 65 const PP_Var& value, |
66 bool check_bind_state, | 66 bool check_bind_state, |
67 scoped_refptr<TrackedCallback> callback) { | 67 scoped_refptr<TrackedCallback> callback) { |
68 if (closed_) | 68 if (closed_) |
69 return PP_ERROR_FAILED; | 69 return PP_ERROR_FAILED; |
70 | 70 |
71 SocketOptionData option_data; | 71 // Check if socket is expected to be bound or not according to the option. |
72 switch (name) { | 72 switch (name) { |
73 case PP_UDPSOCKET_OPTION_ADDRESS_REUSE: | 73 case PP_UDPSOCKET_OPTION_ADDRESS_REUSE: |
74 case PP_UDPSOCKET_OPTION_BROADCAST: { | 74 check_bind_state = true; // fallthrough |
75 if ((check_bind_state || name == PP_UDPSOCKET_OPTION_ADDRESS_REUSE) && | 75 case PP_UDPSOCKET_OPTION_BROADCAST: |
76 bind_called_) { | 76 case PP_UDPSOCKET_OPTION_MULTICAST_LOOP: |
77 case PP_UDPSOCKET_OPTION_MULTICAST_TTL: { | |
78 if (check_bind_state && bind_called_) { | |
77 // SetOption should fail in this case in order to give predictable | 79 // SetOption should fail in this case in order to give predictable |
78 // behavior while binding. Note that we use |bind_called_| rather | 80 // behavior while binding. Note that we use |bind_called_| rather |
79 // than |bound_| since the latter is only set on successful completion | 81 // than |bound_| since the latter is only set on successful completion |
80 // of Bind(). | 82 // of Bind(). |
81 return PP_ERROR_FAILED; | 83 return PP_ERROR_FAILED; |
82 } | 84 } |
85 break; | |
86 } | |
87 case PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE: | |
88 case PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE: { | |
89 if (check_bind_state && !bound_) | |
90 return PP_ERROR_FAILED; | |
91 break; | |
92 } | |
93 } | |
94 | |
95 SocketOptionData option_data; | |
96 switch (name) { | |
97 case PP_UDPSOCKET_OPTION_ADDRESS_REUSE: | |
98 case PP_UDPSOCKET_OPTION_BROADCAST: | |
99 case PP_UDPSOCKET_OPTION_MULTICAST_LOOP: { | |
83 if (value.type != PP_VARTYPE_BOOL) | 100 if (value.type != PP_VARTYPE_BOOL) |
84 return PP_ERROR_BADARGUMENT; | 101 return PP_ERROR_BADARGUMENT; |
85 option_data.SetBool(PP_ToBool(value.value.as_bool)); | 102 option_data.SetBool(PP_ToBool(value.value.as_bool)); |
86 break; | 103 break; |
87 } | 104 } |
88 case PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE: | 105 case PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE: |
89 case PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE: { | 106 case PP_UDPSOCKET_OPTION_RECV_BUFFER_SIZE: { |
90 if (check_bind_state && !bound_) | |
91 return PP_ERROR_FAILED; | |
92 if (value.type != PP_VARTYPE_INT32) | 107 if (value.type != PP_VARTYPE_INT32) |
93 return PP_ERROR_BADARGUMENT; | 108 return PP_ERROR_BADARGUMENT; |
94 option_data.SetInt32(value.value.as_int); | 109 option_data.SetInt32(value.value.as_int); |
95 break; | 110 break; |
96 } | 111 } |
112 case PP_UDPSOCKET_OPTION_MULTICAST_TTL: { | |
113 int32_t ival = value.value.as_int; | |
114 if (value.type != PP_VARTYPE_INT32 && (ival < 0 || ival > 255)) | |
115 return PP_ERROR_BADARGUMENT; | |
116 option_data.SetInt32(ival); | |
117 break; | |
118 } | |
97 default: { | 119 default: { |
98 NOTREACHED(); | 120 NOTREACHED(); |
99 return PP_ERROR_BADARGUMENT; | 121 return PP_ERROR_BADARGUMENT; |
100 } | 122 } |
101 } | 123 } |
102 | 124 |
103 Call<PpapiPluginMsg_UDPSocket_SetOptionReply>( | 125 Call<PpapiPluginMsg_UDPSocket_SetOptionReply>( |
104 BROWSER, | 126 BROWSER, |
105 PpapiHostMsg_UDPSocket_SetOption(name, option_data), | 127 PpapiHostMsg_UDPSocket_SetOption(name, option_data), |
106 base::Bind(&UDPSocketResourceBase::OnPluginMsgSetOptionReply, | 128 base::Bind(&UDPSocketResourceBase::OnPluginMsgGeneralReply, |
107 base::Unretained(this), | 129 base::Unretained(this), |
108 callback), | 130 callback), |
109 callback); | 131 callback); |
110 return PP_OK_COMPLETIONPENDING; | 132 return PP_OK_COMPLETIONPENDING; |
111 } | 133 } |
112 | 134 |
113 int32_t UDPSocketResourceBase::BindImpl( | 135 int32_t UDPSocketResourceBase::BindImpl( |
114 const PP_NetAddress_Private* addr, | 136 const PP_NetAddress_Private* addr, |
115 scoped_refptr<TrackedCallback> callback) { | 137 scoped_refptr<TrackedCallback> callback) { |
116 if (!addr) | 138 if (!addr) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 scoped_refptr<TrackedCallback> callback) { | 171 scoped_refptr<TrackedCallback> callback) { |
150 if (!buffer || num_bytes <= 0) | 172 if (!buffer || num_bytes <= 0) |
151 return PP_ERROR_BADARGUMENT; | 173 return PP_ERROR_BADARGUMENT; |
152 if (!bound_) | 174 if (!bound_) |
153 return PP_ERROR_FAILED; | 175 return PP_ERROR_FAILED; |
154 if (TrackedCallback::IsPending(recvfrom_callback_)) | 176 if (TrackedCallback::IsPending(recvfrom_callback_)) |
155 return PP_ERROR_INPROGRESS; | 177 return PP_ERROR_INPROGRESS; |
156 | 178 |
157 if (recv_buffers_.empty()) { | 179 if (recv_buffers_.empty()) { |
158 read_buffer_ = buffer; | 180 read_buffer_ = buffer; |
159 bytes_to_read_ = std::min(num_bytes, kMaxReadSize); | |
160 recvfrom_addr_resource_ = addr; | |
bbudge
2015/02/17 15:46:57
Why did these get removed?
etrunko
2015/02/19 12:47:55
For sure it was by mistake, will fix it.
| |
161 recvfrom_callback_ = callback; | 181 recvfrom_callback_ = callback; |
162 | 182 |
163 return PP_OK_COMPLETIONPENDING; | 183 return PP_OK_COMPLETIONPENDING; |
164 } else { | 184 } else { |
165 RecvBuffer& front = recv_buffers_.front(); | 185 RecvBuffer& front = recv_buffers_.front(); |
166 | 186 |
167 if (num_bytes < static_cast<int32_t>(front.data.size())) | 187 if (num_bytes < static_cast<int32_t>(front.data.size())) |
168 return PP_ERROR_MESSAGE_TOO_BIG; | 188 return PP_ERROR_MESSAGE_TOO_BIG; |
169 | 189 |
170 int32_t result = SetRecvFromOutput(front.result, front.data, front.addr, | 190 int32_t result = SetRecvFromOutput(front.result, front.data, front.addr, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 while (!sendto_callbacks_.empty()) { | 246 while (!sendto_callbacks_.empty()) { |
227 scoped_refptr<TrackedCallback> callback = sendto_callbacks_.front(); | 247 scoped_refptr<TrackedCallback> callback = sendto_callbacks_.front(); |
228 sendto_callbacks_.pop(); | 248 sendto_callbacks_.pop(); |
229 PostAbortIfNecessary(&callback); | 249 PostAbortIfNecessary(&callback); |
230 } | 250 } |
231 | 251 |
232 read_buffer_ = NULL; | 252 read_buffer_ = NULL; |
233 bytes_to_read_ = -1; | 253 bytes_to_read_ = -1; |
234 } | 254 } |
235 | 255 |
256 int32_t UDPSocketResourceBase::JoinGroupImpl( | |
257 const PP_NetAddress_Private *group, | |
258 scoped_refptr<TrackedCallback> callback) { | |
259 DCHECK(group); | |
260 | |
261 Call<PpapiPluginMsg_UDPSocket_JoinGroupReply>( | |
262 BROWSER, | |
263 PpapiHostMsg_UDPSocket_JoinGroup(*group), | |
264 base::Bind(&UDPSocketResourceBase::OnPluginMsgGeneralReply, | |
265 base::Unretained(this), | |
266 callback), | |
267 callback); | |
268 return PP_OK_COMPLETIONPENDING; | |
269 } | |
270 | |
271 int32_t UDPSocketResourceBase::LeaveGroupImpl( | |
272 const PP_NetAddress_Private *group, | |
273 scoped_refptr<TrackedCallback> callback) { | |
274 DCHECK(group); | |
275 | |
276 Call<PpapiPluginMsg_UDPSocket_LeaveGroupReply>( | |
277 BROWSER, | |
278 PpapiHostMsg_UDPSocket_LeaveGroup(*group), | |
279 base::Bind(&UDPSocketResourceBase::OnPluginMsgGeneralReply, | |
280 base::Unretained(this), | |
281 callback), | |
282 callback); | |
283 return PP_OK_COMPLETIONPENDING; | |
284 } | |
285 | |
236 void UDPSocketResourceBase::OnReplyReceived( | 286 void UDPSocketResourceBase::OnReplyReceived( |
237 const ResourceMessageReplyParams& params, | 287 const ResourceMessageReplyParams& params, |
238 const IPC::Message& msg) { | 288 const IPC::Message& msg) { |
239 PPAPI_BEGIN_MESSAGE_MAP(UDPSocketResourceBase, msg) | 289 PPAPI_BEGIN_MESSAGE_MAP(UDPSocketResourceBase, msg) |
240 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | 290 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
241 PpapiPluginMsg_UDPSocket_PushRecvResult, | 291 PpapiPluginMsg_UDPSocket_PushRecvResult, |
242 OnPluginMsgPushRecvResult) | 292 OnPluginMsgPushRecvResult) |
243 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( | 293 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( |
244 PluginResource::OnReplyReceived(params, msg)) | 294 PluginResource::OnReplyReceived(params, msg)) |
245 PPAPI_END_MESSAGE_MAP() | 295 PPAPI_END_MESSAGE_MAP() |
246 } | 296 } |
247 | 297 |
248 void UDPSocketResourceBase::PostAbortIfNecessary( | 298 void UDPSocketResourceBase::PostAbortIfNecessary( |
249 scoped_refptr<TrackedCallback>* callback) { | 299 scoped_refptr<TrackedCallback>* callback) { |
250 if (TrackedCallback::IsPending(*callback)) | 300 if (TrackedCallback::IsPending(*callback)) |
251 (*callback)->PostAbort(); | 301 (*callback)->PostAbort(); |
252 } | 302 } |
253 | 303 |
254 void UDPSocketResourceBase::OnPluginMsgSetOptionReply( | 304 void UDPSocketResourceBase::OnPluginMsgGeneralReply( |
255 scoped_refptr<TrackedCallback> callback, | 305 scoped_refptr<TrackedCallback> callback, |
256 const ResourceMessageReplyParams& params) { | 306 const ResourceMessageReplyParams& params) { |
257 if (TrackedCallback::IsPending(callback)) | 307 if (TrackedCallback::IsPending(callback)) |
258 RunCallback(callback, params.result()); | 308 RunCallback(callback, params.result()); |
259 } | 309 } |
260 | 310 |
261 void UDPSocketResourceBase::OnPluginMsgBindReply( | 311 void UDPSocketResourceBase::OnPluginMsgBindReply( |
262 const ResourceMessageReplyParams& params, | 312 const ResourceMessageReplyParams& params, |
263 const PP_NetAddress_Private& bound_addr) { | 313 const PP_NetAddress_Private& bound_addr) { |
264 // It is possible that |bind_callback_| is pending while |closed_| is true: | 314 // It is possible that |bind_callback_| is pending while |closed_| is true: |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 if (result == PP_OK && !data.empty()) | 415 if (result == PP_OK && !data.empty()) |
366 memcpy(output_buffer, data.c_str(), data.size()); | 416 memcpy(output_buffer, data.c_str(), data.size()); |
367 | 417 |
368 recvfrom_addr_ = addr; | 418 recvfrom_addr_ = addr; |
369 | 419 |
370 return result == PP_OK ? static_cast<int32_t>(data.size()) : result; | 420 return result == PP_OK ? static_cast<int32_t>(data.size()) : result; |
371 } | 421 } |
372 | 422 |
373 } // namespace proxy | 423 } // namespace proxy |
374 } // namespace ppapi | 424 } // namespace ppapi |
OLD | NEW |