Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: webkit/plugins/ppapi/ppb_transport_impl.cc

Issue 10081020: PPAPI: Make blocking completion callbacks work. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: try again Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "webkit/plugins/ppapi/ppb_transport_impl.h" 5 #include "webkit/plugins/ppapi/ppb_transport_impl.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 break; 230 break;
231 } 231 }
232 232
233 default: 233 default:
234 return PP_ERROR_BADARGUMENT; 234 return PP_ERROR_BADARGUMENT;
235 } 235 }
236 236
237 return PP_OK; 237 return PP_OK;
238 } 238 }
239 239
240 int32_t PPB_Transport_Impl::Connect(PP_CompletionCallback callback) { 240 int32_t PPB_Transport_Impl::Connect(scoped_refptr<TrackedCallback> callback) {
241 if (!callback.func)
242 return PP_ERROR_BLOCKS_MAIN_THREAD;
243 if (!p2p_transport_.get()) 241 if (!p2p_transport_.get())
244 return PP_ERROR_FAILED; 242 return PP_ERROR_FAILED;
245 243
246 // Connect() has already been called. 244 // Connect() has already been called.
247 if (started_) 245 if (started_)
248 return PP_ERROR_INPROGRESS; 246 return PP_ERROR_INPROGRESS;
249 247
250 P2PTransport::Protocol protocol = (type_ == PP_TRANSPORTTYPE_STREAM) ? 248 P2PTransport::Protocol protocol = (type_ == PP_TRANSPORTTYPE_STREAM) ?
251 P2PTransport::PROTOCOL_TCP : P2PTransport::PROTOCOL_UDP; 249 P2PTransport::PROTOCOL_TCP : P2PTransport::PROTOCOL_UDP;
252 250
253 if (!p2p_transport_->Init( 251 if (!p2p_transport_->Init(
254 GetFrameForResource(this), name_, protocol, config_, this)) { 252 GetFrameForResource(this), name_, protocol, config_, this)) {
255 return PP_ERROR_FAILED; 253 return PP_ERROR_FAILED;
256 } 254 }
257 255
258 started_ = true; 256 started_ = true;
259 257
260 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); 258 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this);
261 if (!plugin_module) 259 if (!plugin_module)
262 return PP_ERROR_FAILED; 260 return PP_ERROR_FAILED;
263 261
264 connect_callback_ = new TrackedCallback(this, callback); 262 connect_callback_ = callback;
265 return PP_OK_COMPLETIONPENDING; 263 return PP_OK_COMPLETIONPENDING;
266 } 264 }
267 265
268 int32_t PPB_Transport_Impl::GetNextAddress(PP_Var* address, 266 int32_t PPB_Transport_Impl::GetNextAddress(
269 PP_CompletionCallback callback) { 267 PP_Var* address,
270 if (!callback.func) 268 scoped_refptr<TrackedCallback> callback) {
271 return PP_ERROR_BLOCKS_MAIN_THREAD;
272 if (!p2p_transport_.get()) 269 if (!p2p_transport_.get())
273 return PP_ERROR_FAILED; 270 return PP_ERROR_FAILED;
274 271
275 if (TrackedCallback::IsPending(next_address_callback_)) 272 if (TrackedCallback::IsPending(next_address_callback_))
276 return PP_ERROR_INPROGRESS; 273 return PP_ERROR_INPROGRESS;
277 274
278 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); 275 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this);
279 if (!plugin_module) 276 if (!plugin_module)
280 return PP_ERROR_FAILED; 277 return PP_ERROR_FAILED;
281 278
282 if (!local_candidates_.empty()) { 279 if (!local_candidates_.empty()) {
283 *address = StringVar::StringToPPVar(local_candidates_.front()); 280 *address = StringVar::StringToPPVar(local_candidates_.front());
284 local_candidates_.pop_front(); 281 local_candidates_.pop_front();
285 return PP_OK; 282 return PP_OK;
286 } 283 }
287 284
288 next_address_callback_ = new TrackedCallback(this, callback); 285 next_address_callback_ = callback;
289 return PP_OK_COMPLETIONPENDING; 286 return PP_OK_COMPLETIONPENDING;
290 } 287 }
291 288
292 int32_t PPB_Transport_Impl::ReceiveRemoteAddress(PP_Var address) { 289 int32_t PPB_Transport_Impl::ReceiveRemoteAddress(PP_Var address) {
293 if (!p2p_transport_.get()) 290 if (!p2p_transport_.get())
294 return PP_ERROR_FAILED; 291 return PP_ERROR_FAILED;
295 292
296 StringVar* address_str = StringVar::FromPPVar(address); 293 StringVar* address_str = StringVar::FromPPVar(address);
297 if (!address_str) 294 if (!address_str)
298 return PP_ERROR_BADARGUMENT; 295 return PP_ERROR_BADARGUMENT;
299 296
300 return p2p_transport_->AddRemoteCandidate(address_str->value()) ? 297 return p2p_transport_->AddRemoteCandidate(address_str->value()) ?
301 PP_OK : PP_ERROR_FAILED; 298 PP_OK : PP_ERROR_FAILED;
302 } 299 }
303 300
304 int32_t PPB_Transport_Impl::Recv(void* data, uint32_t len, 301 int32_t PPB_Transport_Impl::Recv(void* data, uint32_t len,
305 PP_CompletionCallback callback) { 302 scoped_refptr<TrackedCallback> callback) {
306 if (!callback.func)
307 return PP_ERROR_BLOCKS_MAIN_THREAD;
308 if (!p2p_transport_.get()) 303 if (!p2p_transport_.get())
309 return PP_ERROR_FAILED; 304 return PP_ERROR_FAILED;
310 305
311 if (TrackedCallback::IsPending(recv_callback_)) 306 if (TrackedCallback::IsPending(recv_callback_))
312 return PP_ERROR_INPROGRESS; 307 return PP_ERROR_INPROGRESS;
313 308
314 net::Socket* channel = p2p_transport_->GetChannel(); 309 net::Socket* channel = p2p_transport_->GetChannel();
315 if (!channel) 310 if (!channel)
316 return PP_ERROR_FAILED; 311 return PP_ERROR_FAILED;
317 312
318 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); 313 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this);
319 if (!plugin_module) 314 if (!plugin_module)
320 return PP_ERROR_FAILED; 315 return PP_ERROR_FAILED;
321 316
322 scoped_refptr<net::IOBuffer> buffer = 317 scoped_refptr<net::IOBuffer> buffer =
323 new net::WrappedIOBuffer(static_cast<const char*>(data)); 318 new net::WrappedIOBuffer(static_cast<const char*>(data));
324 int result = MapNetError( 319 int result = MapNetError(
325 channel->Read(buffer, len, base::Bind(&PPB_Transport_Impl::OnRead, 320 channel->Read(buffer, len, base::Bind(&PPB_Transport_Impl::OnRead,
326 base::Unretained(this)))); 321 base::Unretained(this))));
327 if (result == PP_OK_COMPLETIONPENDING) 322 if (result == PP_OK_COMPLETIONPENDING)
328 recv_callback_ = new TrackedCallback(this, callback); 323 recv_callback_ = callback;
329 324
330 return result; 325 return result;
331 } 326 }
332 327
333 int32_t PPB_Transport_Impl::Send(const void* data, uint32_t len, 328 int32_t PPB_Transport_Impl::Send(const void* data, uint32_t len,
334 PP_CompletionCallback callback) { 329 scoped_refptr<TrackedCallback> callback) {
335 if (!callback.func)
336 return PP_ERROR_BLOCKS_MAIN_THREAD;
337 if (!p2p_transport_.get()) 330 if (!p2p_transport_.get())
338 return PP_ERROR_FAILED; 331 return PP_ERROR_FAILED;
339 332
340 if (TrackedCallback::IsPending(send_callback_)) 333 if (TrackedCallback::IsPending(send_callback_))
341 return PP_ERROR_INPROGRESS; 334 return PP_ERROR_INPROGRESS;
342 335
343 net::Socket* channel = p2p_transport_->GetChannel(); 336 net::Socket* channel = p2p_transport_->GetChannel();
344 if (!channel) 337 if (!channel)
345 return PP_ERROR_FAILED; 338 return PP_ERROR_FAILED;
346 339
347 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); 340 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this);
348 if (!plugin_module) 341 if (!plugin_module)
349 return PP_ERROR_FAILED; 342 return PP_ERROR_FAILED;
350 343
351 scoped_refptr<net::IOBuffer> buffer = 344 scoped_refptr<net::IOBuffer> buffer =
352 new net::WrappedIOBuffer(static_cast<const char*>(data)); 345 new net::WrappedIOBuffer(static_cast<const char*>(data));
353 int result = MapNetError( 346 int result = MapNetError(
354 channel->Write(buffer, len, base::Bind(&PPB_Transport_Impl::OnWritten, 347 channel->Write(buffer, len, base::Bind(&PPB_Transport_Impl::OnWritten,
355 base::Unretained(this)))); 348 base::Unretained(this))));
356 if (result == PP_OK_COMPLETIONPENDING) 349 if (result == PP_OK_COMPLETIONPENDING)
357 send_callback_ = new TrackedCallback(this, callback); 350 send_callback_ = callback;
358 351
359 return result; 352 return result;
360 } 353 }
361 354
362 int32_t PPB_Transport_Impl::Close() { 355 int32_t PPB_Transport_Impl::Close() {
363 if (!p2p_transport_.get()) 356 if (!p2p_transport_.get())
364 return PP_ERROR_FAILED; 357 return PP_ERROR_FAILED;
365 358
366 p2p_transport_.reset(); 359 p2p_transport_.reset();
367 360
(...skipping 27 matching lines...) Expand all
395 TrackedCallback::ClearAndRun(&recv_callback_, MapNetError(result)); 388 TrackedCallback::ClearAndRun(&recv_callback_, MapNetError(result));
396 } 389 }
397 390
398 void PPB_Transport_Impl::OnWritten(int result) { 391 void PPB_Transport_Impl::OnWritten(int result) {
399 DCHECK(TrackedCallback::IsPending(send_callback_)); 392 DCHECK(TrackedCallback::IsPending(send_callback_));
400 TrackedCallback::ClearAndRun(&send_callback_, MapNetError(result)); 393 TrackedCallback::ClearAndRun(&send_callback_, MapNetError(result));
401 } 394 }
402 395
403 } // namespace ppapi 396 } // namespace ppapi
404 } // namespace webkit 397 } // namespace webkit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698