| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| 12 #include "net/socket/socket.h" | 12 #include "net/socket/socket.h" |
| 13 #include "ppapi/c/dev/ppb_transport_dev.h" | 13 #include "ppapi/c/dev/ppb_transport_dev.h" |
| 14 #include "ppapi/c/pp_completion_callback.h" | 14 #include "ppapi/c/pp_completion_callback.h" |
| 15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/shared_impl/callback_tracker.h" |
| 17 #include "ppapi/shared_impl/ppapi_globals.h" |
| 16 #include "ppapi/shared_impl/var.h" | 18 #include "ppapi/shared_impl/var.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 20 #include "webkit/plugins/ppapi/common.h" | 22 #include "webkit/plugins/ppapi/common.h" |
| 21 #include "webkit/plugins/ppapi/plugin_module.h" | 23 #include "webkit/plugins/ppapi/plugin_module.h" |
| 22 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 23 #include "webkit/plugins/ppapi/resource_helper.h" | 25 #include "webkit/plugins/ppapi/resource_helper.h" |
| 24 | 26 |
| 25 using ppapi::StringVar; | 27 using ppapi::StringVar; |
| 26 using ppapi::thunk::PPB_Transport_API; | 28 using ppapi::thunk::PPB_Transport_API; |
| 29 using ppapi::TrackedCallback; |
| 27 using webkit_glue::P2PTransport; | 30 using webkit_glue::P2PTransport; |
| 28 | 31 |
| 29 namespace webkit { | 32 namespace webkit { |
| 30 namespace ppapi { | 33 namespace ppapi { |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 34 const char kUdpProtocolName[] = "udp"; | 37 const char kUdpProtocolName[] = "udp"; |
| 35 const char kTcpProtocolName[] = "tcp"; | 38 const char kTcpProtocolName[] = "tcp"; |
| 36 | 39 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 GetFrameForResource(this), name_, protocol, config_, this)) { | 254 GetFrameForResource(this), name_, protocol, config_, this)) { |
| 252 return PP_ERROR_FAILED; | 255 return PP_ERROR_FAILED; |
| 253 } | 256 } |
| 254 | 257 |
| 255 started_ = true; | 258 started_ = true; |
| 256 | 259 |
| 257 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 260 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 258 if (!plugin_module) | 261 if (!plugin_module) |
| 259 return PP_ERROR_FAILED; | 262 return PP_ERROR_FAILED; |
| 260 | 263 |
| 261 connect_callback_ = new TrackedCompletionCallback( | 264 connect_callback_ = new TrackedCallback(this, callback); |
| 262 plugin_module->GetCallbackTracker(), pp_resource(), callback); | |
| 263 return PP_OK_COMPLETIONPENDING; | 265 return PP_OK_COMPLETIONPENDING; |
| 264 } | 266 } |
| 265 | 267 |
| 266 int32_t PPB_Transport_Impl::GetNextAddress(PP_Var* address, | 268 int32_t PPB_Transport_Impl::GetNextAddress(PP_Var* address, |
| 267 PP_CompletionCallback callback) { | 269 PP_CompletionCallback callback) { |
| 268 if (!callback.func) | 270 if (!callback.func) |
| 269 return PP_ERROR_BLOCKS_MAIN_THREAD; | 271 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 270 if (!p2p_transport_.get()) | 272 if (!p2p_transport_.get()) |
| 271 return PP_ERROR_FAILED; | 273 return PP_ERROR_FAILED; |
| 272 | 274 |
| 273 if (next_address_callback_.get() && !next_address_callback_->completed()) | 275 if (TrackedCallback::IsPending(next_address_callback_)) |
| 274 return PP_ERROR_INPROGRESS; | 276 return PP_ERROR_INPROGRESS; |
| 275 | 277 |
| 276 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 278 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 277 if (!plugin_module) | 279 if (!plugin_module) |
| 278 return PP_ERROR_FAILED; | 280 return PP_ERROR_FAILED; |
| 279 | 281 |
| 280 if (!local_candidates_.empty()) { | 282 if (!local_candidates_.empty()) { |
| 281 *address = StringVar::StringToPPVar(local_candidates_.front()); | 283 *address = StringVar::StringToPPVar(local_candidates_.front()); |
| 282 local_candidates_.pop_front(); | 284 local_candidates_.pop_front(); |
| 283 return PP_OK; | 285 return PP_OK; |
| 284 } | 286 } |
| 285 | 287 |
| 286 next_address_callback_ = new TrackedCompletionCallback( | 288 next_address_callback_ = new TrackedCallback(this, callback); |
| 287 plugin_module->GetCallbackTracker(), pp_resource(), callback); | |
| 288 return PP_OK_COMPLETIONPENDING; | 289 return PP_OK_COMPLETIONPENDING; |
| 289 } | 290 } |
| 290 | 291 |
| 291 int32_t PPB_Transport_Impl::ReceiveRemoteAddress(PP_Var address) { | 292 int32_t PPB_Transport_Impl::ReceiveRemoteAddress(PP_Var address) { |
| 292 if (!p2p_transport_.get()) | 293 if (!p2p_transport_.get()) |
| 293 return PP_ERROR_FAILED; | 294 return PP_ERROR_FAILED; |
| 294 | 295 |
| 295 StringVar* address_str = StringVar::FromPPVar(address); | 296 StringVar* address_str = StringVar::FromPPVar(address); |
| 296 if (!address_str) | 297 if (!address_str) |
| 297 return PP_ERROR_BADARGUMENT; | 298 return PP_ERROR_BADARGUMENT; |
| 298 | 299 |
| 299 return p2p_transport_->AddRemoteCandidate(address_str->value()) ? | 300 return p2p_transport_->AddRemoteCandidate(address_str->value()) ? |
| 300 PP_OK : PP_ERROR_FAILED; | 301 PP_OK : PP_ERROR_FAILED; |
| 301 } | 302 } |
| 302 | 303 |
| 303 int32_t PPB_Transport_Impl::Recv(void* data, uint32_t len, | 304 int32_t PPB_Transport_Impl::Recv(void* data, uint32_t len, |
| 304 PP_CompletionCallback callback) { | 305 PP_CompletionCallback callback) { |
| 305 if (!callback.func) | 306 if (!callback.func) |
| 306 return PP_ERROR_BLOCKS_MAIN_THREAD; | 307 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 307 if (!p2p_transport_.get()) | 308 if (!p2p_transport_.get()) |
| 308 return PP_ERROR_FAILED; | 309 return PP_ERROR_FAILED; |
| 309 | 310 |
| 310 if (recv_callback_.get() && !recv_callback_->completed()) | 311 if (TrackedCallback::IsPending(recv_callback_)) |
| 311 return PP_ERROR_INPROGRESS; | 312 return PP_ERROR_INPROGRESS; |
| 312 | 313 |
| 313 net::Socket* channel = p2p_transport_->GetChannel(); | 314 net::Socket* channel = p2p_transport_->GetChannel(); |
| 314 if (!channel) | 315 if (!channel) |
| 315 return PP_ERROR_FAILED; | 316 return PP_ERROR_FAILED; |
| 316 | 317 |
| 317 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 318 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 318 if (!plugin_module) | 319 if (!plugin_module) |
| 319 return PP_ERROR_FAILED; | 320 return PP_ERROR_FAILED; |
| 320 | 321 |
| 321 scoped_refptr<net::IOBuffer> buffer = | 322 scoped_refptr<net::IOBuffer> buffer = |
| 322 new net::WrappedIOBuffer(static_cast<const char*>(data)); | 323 new net::WrappedIOBuffer(static_cast<const char*>(data)); |
| 323 int result = MapNetError( | 324 int result = MapNetError( |
| 324 channel->Read(buffer, len, base::Bind(&PPB_Transport_Impl::OnRead, | 325 channel->Read(buffer, len, base::Bind(&PPB_Transport_Impl::OnRead, |
| 325 base::Unretained(this)))); | 326 base::Unretained(this)))); |
| 326 if (result == PP_OK_COMPLETIONPENDING) { | 327 if (result == PP_OK_COMPLETIONPENDING) |
| 327 recv_callback_ = new TrackedCompletionCallback( | 328 recv_callback_ = new TrackedCallback(this, callback); |
| 328 plugin_module->GetCallbackTracker(), pp_resource(), callback); | |
| 329 } | |
| 330 | 329 |
| 331 return result; | 330 return result; |
| 332 } | 331 } |
| 333 | 332 |
| 334 int32_t PPB_Transport_Impl::Send(const void* data, uint32_t len, | 333 int32_t PPB_Transport_Impl::Send(const void* data, uint32_t len, |
| 335 PP_CompletionCallback callback) { | 334 PP_CompletionCallback callback) { |
| 336 if (!callback.func) | 335 if (!callback.func) |
| 337 return PP_ERROR_BLOCKS_MAIN_THREAD; | 336 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 338 if (!p2p_transport_.get()) | 337 if (!p2p_transport_.get()) |
| 339 return PP_ERROR_FAILED; | 338 return PP_ERROR_FAILED; |
| 340 | 339 |
| 341 if (send_callback_.get() && !send_callback_->completed()) | 340 if (TrackedCallback::IsPending(send_callback_)) |
| 342 return PP_ERROR_INPROGRESS; | 341 return PP_ERROR_INPROGRESS; |
| 343 | 342 |
| 344 net::Socket* channel = p2p_transport_->GetChannel(); | 343 net::Socket* channel = p2p_transport_->GetChannel(); |
| 345 if (!channel) | 344 if (!channel) |
| 346 return PP_ERROR_FAILED; | 345 return PP_ERROR_FAILED; |
| 347 | 346 |
| 348 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 347 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 349 if (!plugin_module) | 348 if (!plugin_module) |
| 350 return PP_ERROR_FAILED; | 349 return PP_ERROR_FAILED; |
| 351 | 350 |
| 352 scoped_refptr<net::IOBuffer> buffer = | 351 scoped_refptr<net::IOBuffer> buffer = |
| 353 new net::WrappedIOBuffer(static_cast<const char*>(data)); | 352 new net::WrappedIOBuffer(static_cast<const char*>(data)); |
| 354 int result = MapNetError( | 353 int result = MapNetError( |
| 355 channel->Write(buffer, len, base::Bind(&PPB_Transport_Impl::OnWritten, | 354 channel->Write(buffer, len, base::Bind(&PPB_Transport_Impl::OnWritten, |
| 356 base::Unretained(this)))); | 355 base::Unretained(this)))); |
| 357 if (result == PP_OK_COMPLETIONPENDING) { | 356 if (result == PP_OK_COMPLETIONPENDING) |
| 358 send_callback_ = new TrackedCompletionCallback( | 357 send_callback_ = new TrackedCallback(this, callback); |
| 359 plugin_module->GetCallbackTracker(), pp_resource(), callback); | |
| 360 } | |
| 361 | 358 |
| 362 return result; | 359 return result; |
| 363 } | 360 } |
| 364 | 361 |
| 365 int32_t PPB_Transport_Impl::Close() { | 362 int32_t PPB_Transport_Impl::Close() { |
| 366 if (!p2p_transport_.get()) | 363 if (!p2p_transport_.get()) |
| 367 return PP_ERROR_FAILED; | 364 return PP_ERROR_FAILED; |
| 368 | 365 |
| 369 p2p_transport_.reset(); | 366 p2p_transport_.reset(); |
| 370 | 367 |
| 371 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 368 ::ppapi::PpapiGlobals::Get()->GetCallbackTrackerForInstance( |
| 372 if (plugin_module) | 369 pp_instance())->PostAbortForResource(pp_resource()); |
| 373 plugin_module->GetCallbackTracker()->AbortAll(); | |
| 374 return PP_OK; | 370 return PP_OK; |
| 375 } | 371 } |
| 376 | 372 |
| 377 void PPB_Transport_Impl::OnCandidateReady(const std::string& address) { | 373 void PPB_Transport_Impl::OnCandidateReady(const std::string& address) { |
| 378 // Store the candidate first before calling the callback. | 374 // Store the candidate first before calling the callback. |
| 379 local_candidates_.push_back(address); | 375 local_candidates_.push_back(address); |
| 380 | 376 |
| 381 if (next_address_callback_.get() && !next_address_callback_->completed()) { | 377 if (TrackedCallback::IsPending(next_address_callback_)) |
| 382 scoped_refptr<TrackedCompletionCallback> callback; | 378 TrackedCallback::ClearAndRun(&next_address_callback_, PP_OK); |
| 383 callback.swap(next_address_callback_); | |
| 384 callback->Run(PP_OK); | |
| 385 } | |
| 386 } | 379 } |
| 387 | 380 |
| 388 void PPB_Transport_Impl::OnStateChange(webkit_glue::P2PTransport::State state) { | 381 void PPB_Transport_Impl::OnStateChange(webkit_glue::P2PTransport::State state) { |
| 389 writable_ = (state | webkit_glue::P2PTransport::STATE_WRITABLE) != 0; | 382 writable_ = (state | webkit_glue::P2PTransport::STATE_WRITABLE) != 0; |
| 390 if (writable_ && connect_callback_.get() && !connect_callback_->completed()) { | 383 if (writable_ && TrackedCallback::IsPending(connect_callback_)) |
| 391 scoped_refptr<TrackedCompletionCallback> callback; | 384 TrackedCallback::ClearAndRun(&connect_callback_, PP_OK); |
| 392 callback.swap(connect_callback_); | |
| 393 callback->Run(PP_OK); | |
| 394 } | |
| 395 } | 385 } |
| 396 | 386 |
| 397 void PPB_Transport_Impl::OnError(int error) { | 387 void PPB_Transport_Impl::OnError(int error) { |
| 398 writable_ = false; | 388 writable_ = false; |
| 399 if (connect_callback_.get() && !connect_callback_->completed()) { | 389 if (TrackedCallback::IsPending(connect_callback_)) |
| 400 scoped_refptr<TrackedCompletionCallback> callback; | 390 TrackedCallback::ClearAndRun(&connect_callback_, PP_ERROR_FAILED); |
| 401 callback.swap(connect_callback_); | |
| 402 callback->Run(PP_ERROR_FAILED); | |
| 403 } | |
| 404 } | 391 } |
| 405 | 392 |
| 406 void PPB_Transport_Impl::OnRead(int result) { | 393 void PPB_Transport_Impl::OnRead(int result) { |
| 407 DCHECK(recv_callback_.get() && !recv_callback_->completed()); | 394 DCHECK(TrackedCallback::IsPending(recv_callback_)); |
| 408 | 395 TrackedCallback::ClearAndRun(&recv_callback_, MapNetError(result)); |
| 409 scoped_refptr<TrackedCompletionCallback> callback; | |
| 410 callback.swap(recv_callback_); | |
| 411 callback->Run(MapNetError(result)); | |
| 412 } | 396 } |
| 413 | 397 |
| 414 void PPB_Transport_Impl::OnWritten(int result) { | 398 void PPB_Transport_Impl::OnWritten(int result) { |
| 415 DCHECK(send_callback_.get() && !send_callback_->completed()); | 399 DCHECK(TrackedCallback::IsPending(send_callback_)); |
| 416 | 400 TrackedCallback::ClearAndRun(&send_callback_, MapNetError(result)); |
| 417 scoped_refptr<TrackedCompletionCallback> callback; | |
| 418 callback.swap(send_callback_); | |
| 419 callback->Run(MapNetError(result)); | |
| 420 } | 401 } |
| 421 | 402 |
| 422 } // namespace ppapi | 403 } // namespace ppapi |
| 423 } // namespace webkit | 404 } // namespace webkit |
| OLD | NEW |