| 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 "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" | 16 #include "ppapi/shared_impl/callback_tracker.h" |
| 17 #include "ppapi/shared_impl/ppapi_globals.h" | 17 #include "ppapi/shared_impl/ppapi_globals.h" |
| 18 #include "ppapi/shared_impl/var.h" | 18 #include "ppapi/shared_impl/var.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 22 #include "webkit/plugins/ppapi/common.h" | 22 #include "webkit/plugins/ppapi/common.h" |
| 23 #include "webkit/plugins/ppapi/plugin_module.h" | 23 #include "webkit/plugins/ppapi/plugin_module.h" |
| 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 25 #include "webkit/plugins/ppapi/resource_helper.h" | 25 #include "webkit/plugins/ppapi/resource_helper.h" |
| 26 | 26 |
| 27 using ppapi::ApiCallbackType; |
| 27 using ppapi::StringVar; | 28 using ppapi::StringVar; |
| 28 using ppapi::thunk::PPB_Transport_API; | 29 using ppapi::thunk::PPB_Transport_API; |
| 29 using ppapi::TrackedCallback; | 30 using ppapi::TrackedCallback; |
| 30 using webkit_glue::P2PTransport; | 31 using webkit_glue::P2PTransport; |
| 31 | 32 |
| 32 namespace webkit { | 33 namespace webkit { |
| 33 namespace ppapi { | 34 namespace ppapi { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 break; | 231 break; |
| 231 } | 232 } |
| 232 | 233 |
| 233 default: | 234 default: |
| 234 return PP_ERROR_BADARGUMENT; | 235 return PP_ERROR_BADARGUMENT; |
| 235 } | 236 } |
| 236 | 237 |
| 237 return PP_OK; | 238 return PP_OK; |
| 238 } | 239 } |
| 239 | 240 |
| 240 int32_t PPB_Transport_Impl::Connect(PP_CompletionCallback callback) { | 241 int32_t PPB_Transport_Impl::Connect(ApiCallbackType callback) { |
| 241 if (!callback.func) | |
| 242 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
| 243 if (!p2p_transport_.get()) | 242 if (!p2p_transport_.get()) |
| 244 return PP_ERROR_FAILED; | 243 return PP_ERROR_FAILED; |
| 245 | 244 |
| 246 // Connect() has already been called. | 245 // Connect() has already been called. |
| 247 if (started_) | 246 if (started_) |
| 248 return PP_ERROR_INPROGRESS; | 247 return PP_ERROR_INPROGRESS; |
| 249 | 248 |
| 250 P2PTransport::Protocol protocol = (type_ == PP_TRANSPORTTYPE_STREAM) ? | 249 P2PTransport::Protocol protocol = (type_ == PP_TRANSPORTTYPE_STREAM) ? |
| 251 P2PTransport::PROTOCOL_TCP : P2PTransport::PROTOCOL_UDP; | 250 P2PTransport::PROTOCOL_TCP : P2PTransport::PROTOCOL_UDP; |
| 252 | 251 |
| 253 if (!p2p_transport_->Init( | 252 if (!p2p_transport_->Init( |
| 254 GetFrameForResource(this), name_, protocol, config_, this)) { | 253 GetFrameForResource(this), name_, protocol, config_, this)) { |
| 255 return PP_ERROR_FAILED; | 254 return PP_ERROR_FAILED; |
| 256 } | 255 } |
| 257 | 256 |
| 258 started_ = true; | 257 started_ = true; |
| 259 | 258 |
| 260 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 259 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 261 if (!plugin_module) | 260 if (!plugin_module) |
| 262 return PP_ERROR_FAILED; | 261 return PP_ERROR_FAILED; |
| 263 | 262 |
| 264 connect_callback_ = new TrackedCallback(this, callback); | 263 connect_callback_ = callback; |
| 265 return PP_OK_COMPLETIONPENDING; | 264 return PP_OK_COMPLETIONPENDING; |
| 266 } | 265 } |
| 267 | 266 |
| 268 int32_t PPB_Transport_Impl::GetNextAddress(PP_Var* address, | 267 int32_t PPB_Transport_Impl::GetNextAddress(PP_Var* address, |
| 269 PP_CompletionCallback callback) { | 268 ApiCallbackType callback) { |
| 270 if (!callback.func) | |
| 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 ApiCallbackType 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 ApiCallbackType 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 Loading... |
| 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 |
| OLD | NEW |