OLD | NEW |
(Empty) | |
| 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 |
| 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 |
| 14 namespace webkit { |
| 15 namespace ppapi { |
| 16 |
| 17 namespace { |
| 18 |
| 19 PP_Resource Create(PP_Instance instance_id) { |
| 20 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
| 21 if (!instance) |
| 22 return 0; |
| 23 |
| 24 scoped_refptr<PPB_Flash_NetConnector_Impl> connector( |
| 25 new PPB_Flash_NetConnector_Impl(instance)); |
| 26 return connector->GetReference(); |
| 27 } |
| 28 |
| 29 PP_Bool IsFlashNetConnector(PP_Resource resource) { |
| 30 return BoolToPPBool(!!Resource::GetAs<PPB_Flash_NetConnector_Impl>(resource)); |
| 31 } |
| 32 |
| 33 int32_t ConnectTcp(PP_Resource connector_id, |
| 34 const char* host, |
| 35 uint16_t port, |
| 36 PP_FileHandle* socket_out, |
| 37 PP_Flash_NetAddress* local_addr_out, |
| 38 PP_Flash_NetAddress* remote_addr_out, |
| 39 PP_CompletionCallback callback) { |
| 40 scoped_refptr<PPB_Flash_NetConnector_Impl> connector( |
| 41 Resource::GetAs<PPB_Flash_NetConnector_Impl>(connector_id)); |
| 42 if (!connector.get()) |
| 43 return PP_ERROR_BADRESOURCE; |
| 44 |
| 45 return connector->ConnectTcp( |
| 46 host, port, socket_out, local_addr_out, remote_addr_out, callback); |
| 47 } |
| 48 |
| 49 int32_t ConnectTcpAddress(PP_Resource connector_id, |
| 50 const PP_Flash_NetAddress* addr, |
| 51 PP_FileHandle* socket_out, |
| 52 PP_Flash_NetAddress* local_addr_out, |
| 53 PP_Flash_NetAddress* remote_addr_out, |
| 54 PP_CompletionCallback callback) { |
| 55 scoped_refptr<PPB_Flash_NetConnector_Impl> connector( |
| 56 Resource::GetAs<PPB_Flash_NetConnector_Impl>(connector_id)); |
| 57 if (!connector.get()) |
| 58 return PP_ERROR_BADRESOURCE; |
| 59 |
| 60 return connector->ConnectTcpAddress( |
| 61 addr, socket_out, local_addr_out, remote_addr_out, callback); |
| 62 } |
| 63 |
| 64 const PPB_Flash_NetConnector ppb_flash_netconnector = { |
| 65 &Create, |
| 66 &IsFlashNetConnector, |
| 67 &ConnectTcp, |
| 68 &ConnectTcpAddress, |
| 69 }; |
| 70 |
| 71 } // namespace |
| 72 |
| 73 PPB_Flash_NetConnector_Impl::PPB_Flash_NetConnector_Impl( |
| 74 PluginInstance* instance) |
| 75 : Resource(instance) { |
| 76 } |
| 77 |
| 78 PPB_Flash_NetConnector_Impl::~PPB_Flash_NetConnector_Impl() { |
| 79 } |
| 80 |
| 81 // static |
| 82 const PPB_Flash_NetConnector* PPB_Flash_NetConnector_Impl::GetInterface() { |
| 83 return &ppb_flash_netconnector; |
| 84 } |
| 85 |
| 86 PPB_Flash_NetConnector_Impl* |
| 87 PPB_Flash_NetConnector_Impl::AsPPB_Flash_NetConnector_Impl() { |
| 88 return this; |
| 89 } |
| 90 |
| 91 int32_t PPB_Flash_NetConnector_Impl::ConnectTcp( |
| 92 const char* host, |
| 93 uint16_t port, |
| 94 PP_FileHandle* socket_out, |
| 95 PP_Flash_NetAddress* local_addr_out, |
| 96 PP_Flash_NetAddress* remote_addr_out, |
| 97 PP_CompletionCallback callback) { |
| 98 // |socket_out| is not optional. |
| 99 if (!socket_out) |
| 100 return PP_ERROR_BADARGUMENT; |
| 101 |
| 102 if (!callback.func) { |
| 103 NOTIMPLEMENTED(); |
| 104 return PP_ERROR_BADARGUMENT; |
| 105 } |
| 106 |
| 107 if (callback_.get() && !callback_->completed()) |
| 108 return PP_ERROR_INPROGRESS; |
| 109 |
| 110 PP_Resource resource_id = GetReferenceNoAddRef(); |
| 111 if (!resource_id) { |
| 112 NOTREACHED(); |
| 113 return PP_ERROR_FAILED; |
| 114 } |
| 115 |
| 116 int32_t rv = instance()->delegate()->ConnectTcp(this, host, port); |
| 117 if (rv == PP_ERROR_WOULDBLOCK) { |
| 118 // Record callback and output buffers. |
| 119 callback_ = new TrackedCompletionCallback( |
| 120 instance()->module()->GetCallbackTracker(), resource_id, callback); |
| 121 socket_out_ = socket_out; |
| 122 local_addr_out_ = local_addr_out; |
| 123 remote_addr_out_ = remote_addr_out; |
| 124 } else { |
| 125 // This should never be completed synchronously successfully. |
| 126 DCHECK_NE(rv, PP_OK); |
| 127 } |
| 128 return rv; |
| 129 } |
| 130 |
| 131 int32_t PPB_Flash_NetConnector_Impl::ConnectTcpAddress( |
| 132 const PP_Flash_NetAddress* addr, |
| 133 PP_FileHandle* socket_out, |
| 134 PP_Flash_NetAddress* local_addr_out, |
| 135 PP_Flash_NetAddress* remote_addr_out, |
| 136 PP_CompletionCallback callback) { |
| 137 // |socket_out| is not optional. |
| 138 if (!socket_out) |
| 139 return PP_ERROR_BADARGUMENT; |
| 140 |
| 141 if (!callback.func) { |
| 142 NOTIMPLEMENTED(); |
| 143 return PP_ERROR_BADARGUMENT; |
| 144 } |
| 145 |
| 146 if (callback_.get() && !callback_->completed()) |
| 147 return PP_ERROR_INPROGRESS; |
| 148 |
| 149 PP_Resource resource_id = GetReferenceNoAddRef(); |
| 150 if (!resource_id) { |
| 151 NOTREACHED(); |
| 152 return PP_ERROR_FAILED; |
| 153 } |
| 154 |
| 155 int32_t rv = instance()->delegate()->ConnectTcpAddress(this, addr); |
| 156 if (rv == PP_ERROR_WOULDBLOCK) { |
| 157 // Record callback and output buffers. |
| 158 callback_ = new TrackedCompletionCallback( |
| 159 instance()->module()->GetCallbackTracker(), resource_id, callback); |
| 160 socket_out_ = socket_out; |
| 161 local_addr_out_ = local_addr_out; |
| 162 remote_addr_out_ = remote_addr_out; |
| 163 } else { |
| 164 // This should never be completed synchronously successfully. |
| 165 DCHECK_NE(rv, PP_OK); |
| 166 } |
| 167 return rv; |
| 168 } |
| 169 |
| 170 void PPB_Flash_NetConnector_Impl::CompleteConnectTcp( |
| 171 PP_FileHandle socket, |
| 172 const PP_Flash_NetAddress& local_addr, |
| 173 const PP_Flash_NetAddress& remote_addr) { |
| 174 int32_t rv = PP_ERROR_ABORTED; |
| 175 if (!callback_->aborted()) { |
| 176 CHECK(!callback_->completed()); |
| 177 |
| 178 // Write output data. |
| 179 *socket_out_ = socket; |
| 180 if (socket != PP_kInvalidFileHandle) { |
| 181 if (local_addr_out_) |
| 182 *local_addr_out_ = local_addr; |
| 183 if (remote_addr_out_) |
| 184 *remote_addr_out_ = remote_addr; |
| 185 rv = PP_OK; |
| 186 } else { |
| 187 rv = PP_ERROR_FAILED; |
| 188 } |
| 189 } |
| 190 |
| 191 // Theoretically, the plugin should be allowed to try another |ConnectTcp()| |
| 192 // from the callback. |
| 193 scoped_refptr<TrackedCompletionCallback> callback; |
| 194 callback.swap(callback_); |
| 195 // Wipe everything else out for safety. |
| 196 socket_out_ = NULL; |
| 197 local_addr_out_ = NULL; |
| 198 remote_addr_out_ = NULL; |
| 199 |
| 200 callback->Run(rv); // Will complete abortively if necessary. |
| 201 } |
| 202 |
| 203 } // namespace ppapi |
| 204 } // namespace webkit |
| 205 |
OLD | NEW |