| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/plugins/ppapi/ppb_flash_net_connector_impl.h" | |
| 6 | |
| 7 #include "ppapi/c/pp_completion_callback.h" | |
| 8 #include "ppapi/c/private/ppb_flash_net_connector.h" | |
| 9 #include "webkit/plugins/ppapi/common.h" | |
| 10 #include "webkit/plugins/ppapi/plugin_delegate.h" | |
| 11 #include "webkit/plugins/ppapi/plugin_module.h" | |
| 12 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
| 13 #include "webkit/plugins/ppapi/resource_helper.h" | |
| 14 | |
| 15 using ppapi::thunk::PPB_Flash_NetConnector_API; | |
| 16 using ppapi::TrackedCallback; | |
| 17 | |
| 18 namespace webkit { | |
| 19 namespace ppapi { | |
| 20 | |
| 21 PPB_Flash_NetConnector_Impl::PPB_Flash_NetConnector_Impl(PP_Instance instance) | |
| 22 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | |
| 23 socket_out_(NULL), | |
| 24 local_addr_out_(NULL), | |
| 25 remote_addr_out_(NULL) { | |
| 26 } | |
| 27 | |
| 28 PPB_Flash_NetConnector_Impl::~PPB_Flash_NetConnector_Impl() { | |
| 29 } | |
| 30 | |
| 31 PPB_Flash_NetConnector_API* | |
| 32 PPB_Flash_NetConnector_Impl::AsPPB_Flash_NetConnector_API() { | |
| 33 return this; | |
| 34 } | |
| 35 | |
| 36 int32_t PPB_Flash_NetConnector_Impl::ConnectTcp( | |
| 37 const char* host, | |
| 38 uint16_t port, | |
| 39 PP_FileHandle* socket_out, | |
| 40 PP_NetAddress_Private* local_addr_out, | |
| 41 PP_NetAddress_Private* remote_addr_out, | |
| 42 PP_CompletionCallback callback) { | |
| 43 // |socket_out| is not optional. | |
| 44 if (!socket_out) | |
| 45 return PP_ERROR_BADARGUMENT; | |
| 46 | |
| 47 if (!callback.func) | |
| 48 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
| 49 | |
| 50 if (TrackedCallback::IsPending(callback_)) | |
| 51 return PP_ERROR_INPROGRESS; | |
| 52 | |
| 53 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); | |
| 54 if (!plugin_instance) | |
| 55 return false; | |
| 56 int32_t rv = plugin_instance->delegate()->ConnectTcp(this, host, port); | |
| 57 if (rv == PP_OK_COMPLETIONPENDING) { | |
| 58 // Record callback and output buffers. | |
| 59 callback_ = new TrackedCallback(this, callback); | |
| 60 socket_out_ = socket_out; | |
| 61 local_addr_out_ = local_addr_out; | |
| 62 remote_addr_out_ = remote_addr_out; | |
| 63 } else { | |
| 64 // This should never be completed synchronously successfully. | |
| 65 DCHECK_NE(rv, PP_OK); | |
| 66 } | |
| 67 return rv; | |
| 68 } | |
| 69 | |
| 70 int32_t PPB_Flash_NetConnector_Impl::ConnectTcpAddress( | |
| 71 const PP_NetAddress_Private* addr, | |
| 72 PP_FileHandle* socket_out, | |
| 73 PP_NetAddress_Private* local_addr_out, | |
| 74 PP_NetAddress_Private* remote_addr_out, | |
| 75 PP_CompletionCallback callback) { | |
| 76 // |socket_out| is not optional. | |
| 77 if (!socket_out) | |
| 78 return PP_ERROR_BADARGUMENT; | |
| 79 | |
| 80 if (!callback.func) | |
| 81 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
| 82 | |
| 83 if (TrackedCallback::IsPending(callback_)) | |
| 84 return PP_ERROR_INPROGRESS; | |
| 85 | |
| 86 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); | |
| 87 if (!plugin_instance) | |
| 88 return false; | |
| 89 int32_t rv = plugin_instance->delegate()->ConnectTcpAddress(this, addr); | |
| 90 if (rv == PP_OK_COMPLETIONPENDING) { | |
| 91 // Record callback and output buffers. | |
| 92 callback_ = new TrackedCallback(this, callback); | |
| 93 socket_out_ = socket_out; | |
| 94 local_addr_out_ = local_addr_out; | |
| 95 remote_addr_out_ = remote_addr_out; | |
| 96 } else { | |
| 97 // This should never be completed synchronously successfully. | |
| 98 DCHECK_NE(rv, PP_OK); | |
| 99 } | |
| 100 return rv; | |
| 101 } | |
| 102 | |
| 103 void PPB_Flash_NetConnector_Impl::CompleteConnectTcp( | |
| 104 PP_FileHandle socket, | |
| 105 const PP_NetAddress_Private& local_addr, | |
| 106 const PP_NetAddress_Private& remote_addr) { | |
| 107 int32_t rv = PP_ERROR_ABORTED; | |
| 108 if (!callback_->aborted()) { | |
| 109 CHECK(!callback_->completed()); | |
| 110 | |
| 111 // Write output data. | |
| 112 *socket_out_ = socket; | |
| 113 if (socket != PP_kInvalidFileHandle) { | |
| 114 if (local_addr_out_) | |
| 115 *local_addr_out_ = local_addr; | |
| 116 if (remote_addr_out_) | |
| 117 *remote_addr_out_ = remote_addr; | |
| 118 rv = PP_OK; | |
| 119 } else { | |
| 120 rv = PP_ERROR_FAILED; | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 socket_out_ = NULL; | |
| 125 local_addr_out_ = NULL; | |
| 126 remote_addr_out_ = NULL; | |
| 127 TrackedCallback::ClearAndRun(&callback_, rv); | |
| 128 } | |
| 129 | |
| 130 } // namespace ppapi | |
| 131 } // namespace webkit | |
| 132 | |
| OLD | NEW |