Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "ppapi/proxy/vpn_provider_resource.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "ppapi/c/pp_errors.h" | |
| 10 #include "ppapi/proxy/dispatch_reply_message.h" | |
| 11 #include "ppapi/proxy/ppapi_messages.h" | |
| 12 #include "ppapi/shared_impl/array_var.h" | |
| 13 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 14 #include "ppapi/shared_impl/var_tracker.h" | |
| 15 | |
| 16 namespace ppapi { | |
| 17 namespace proxy { | |
| 18 | |
| 19 VpnProviderResource::VpnProviderResource(Connection connection, | |
| 20 PP_Instance instance) | |
| 21 : PluginResource(connection, instance), | |
| 22 bind_callback_(nullptr), | |
| 23 send_packet_callback_(nullptr), | |
| 24 receive_packet_callback_(nullptr), | |
| 25 receive_packet_callback_var_(nullptr), | |
| 26 bound_(false) { | |
| 27 // SendCreate(BROWSER, PpapiHostMsg_VpnProvider_Create()); | |
|
bbudge
2016/06/06 17:53:03
Remove commented out code.
adrian.belgun
2016/06/07 08:14:09
Done.
| |
| 28 } | |
| 29 | |
| 30 VpnProviderResource::~VpnProviderResource() {} | |
| 31 | |
| 32 thunk::PPB_VpnProvider_API* VpnProviderResource::AsPPB_VpnProvider_API() { | |
| 33 return this; | |
| 34 } | |
| 35 | |
| 36 void VpnProviderResource::OnReplyReceived( | |
| 37 const ResourceMessageReplyParams& params, | |
| 38 const IPC::Message& msg) { | |
| 39 PPAPI_BEGIN_MESSAGE_MAP(VpnProviderResource, msg) | |
| 40 // PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_VpnProvider_OnUnbind, | |
| 41 // OnPluginMsgOnUnbindReceived) | |
| 42 // PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | |
| 43 // PpapiPluginMsg_VpnProvider_OnPacketReceived, | |
| 44 // OnPluginMsgOnPacketReceived) | |
|
bbudge
2016/06/06 17:53:03
Remove
adrian.belgun
2016/06/07 08:14:10
Done.
| |
| 45 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( | |
| 46 PluginResource::OnReplyReceived(params, msg)) | |
| 47 PPAPI_END_MESSAGE_MAP() | |
| 48 } | |
| 49 | |
| 50 int32_t VpnProviderResource::Bind( | |
| 51 const PP_Var& configuration_id, | |
| 52 const PP_Var& configuration_name, | |
| 53 const scoped_refptr<TrackedCallback>& callback) { | |
| 54 if (TrackedCallback::IsPending(bind_callback_)) | |
| 55 return PP_ERROR_INPROGRESS; | |
| 56 | |
| 57 StringVar* configuration_id_var = StringVar::FromPPVar(configuration_id); | |
| 58 if (!configuration_id_var) | |
| 59 return PP_ERROR_BADARGUMENT; | |
| 60 StringVar* configuration_name_var = StringVar::FromPPVar(configuration_name); | |
| 61 if (!configuration_name_var) | |
| 62 return PP_ERROR_BADARGUMENT; | |
| 63 | |
| 64 bind_callback_ = callback; | |
|
bbudge
2016/06/06 17:53:03
nit: You should probably also remove this since th
adrian.belgun
2016/06/07 08:14:09
Done.
| |
| 65 | |
| 66 // Call<PpapiPluginMsg_VpnProvider_BindReply>( | |
| 67 // BROWSER, PpapiHostMsg_VpnProvider_Bind( | |
| 68 // configuration_id_var->value(), | |
| 69 // configuration_name_var->value()), | |
| 70 // base::Bind(&VpnProviderResource::OnPluginMsgBindReply, this)); | |
| 71 | |
| 72 // return PP_OK_COMPLETIONPENDING; | |
|
bbudge
2016/06/06 17:53:03
Remove
adrian.belgun
2016/06/07 08:14:10
Done.
| |
| 73 | |
| 74 return PP_ERROR_NOTSUPPORTED; | |
| 75 } | |
| 76 | |
| 77 int32_t VpnProviderResource::SendPacket( | |
| 78 const PP_Var& packet, | |
| 79 const scoped_refptr<TrackedCallback>& callback) { | |
| 80 if (!bound_) | |
| 81 return PP_ERROR_FAILED; | |
| 82 if (TrackedCallback::IsPending(send_packet_callback_)) | |
| 83 return PP_ERROR_INPROGRESS; | |
| 84 if (!ArrayBufferVar::FromPPVar(packet)) | |
| 85 return PP_ERROR_BADARGUMENT; | |
| 86 | |
| 87 uint32_t id; | |
| 88 if (send_packet_buffer_.get() && send_packet_buffer_->GetAvailable(&id)) { | |
| 89 // Send packet immediately | |
| 90 send_packet_buffer_->SetAvailable(id, false); | |
| 91 return DoSendPacket(packet, id); | |
| 92 } else { | |
| 93 // Packet will be sent later | |
| 94 send_packet_callback_ = callback; | |
| 95 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(packet); | |
| 96 send_packets_.push(packet); | |
| 97 | |
| 98 return PP_OK_COMPLETIONPENDING; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 int32_t VpnProviderResource::DoSendPacket(const PP_Var& packet, uint32_t id) { | |
| 103 // Convert packet to std::vector<char>, then send it. | |
| 104 scoped_refptr<ArrayBufferVar> packet_arraybuffer = | |
| 105 ArrayBufferVar::FromPPVar(packet); | |
| 106 if (!packet_arraybuffer.get()) | |
| 107 return PP_ERROR_BADARGUMENT; | |
| 108 | |
| 109 char* packet_pointer = static_cast<char*>(packet_arraybuffer->Map()); | |
| 110 uint32_t packet_size = packet_arraybuffer->ByteLength(); | |
| 111 memcpy(send_packet_buffer_->GetBuffer(id), packet_pointer, packet_size); | |
| 112 packet_arraybuffer->Unmap(); | |
| 113 | |
| 114 // Call<PpapiPluginMsg_VpnProvider_SendPacketReply>( | |
| 115 // BROWSER, PpapiHostMsg_VpnProvider_SendPacket(packet_size, id), | |
| 116 // base::Bind(&VpnProviderResource::OnPluginMsgSendPacketReply, this)); | |
| 117 | |
| 118 return PP_OK; | |
| 119 } | |
| 120 | |
| 121 int32_t VpnProviderResource::ReceivePacket( | |
| 122 PP_Var* packet, | |
| 123 const scoped_refptr<TrackedCallback>& callback) { | |
| 124 if (TrackedCallback::IsPending(receive_packet_callback_)) | |
| 125 return PP_ERROR_INPROGRESS; | |
| 126 | |
| 127 // Return previously received packet. | |
| 128 if (!received_packets_.empty()) { | |
| 129 receive_packet_callback_var_ = packet; | |
| 130 WritePacket(); | |
| 131 return PP_OK; | |
| 132 } | |
| 133 | |
| 134 // Or retain packet var and install callback. | |
| 135 receive_packet_callback_var_ = packet; | |
| 136 receive_packet_callback_ = callback; | |
| 137 | |
| 138 return PP_OK_COMPLETIONPENDING; | |
| 139 } | |
| 140 | |
| 141 // Responds to PpapiPluginMsg_VpnProvider_OnUnbind | |
|
bbudge
2016/06/06 17:53:02
nit: Eliminate comments on implementations of meth
adrian.belgun
2016/06/07 08:14:09
Done.
| |
| 142 void VpnProviderResource::OnPluginMsgOnUnbindReceived( | |
| 143 const ResourceMessageReplyParams& params) { | |
| 144 bound_ = false; | |
| 145 | |
| 146 // Cleanup in-flight packets. | |
| 147 while (!received_packets_.empty()) { | |
| 148 received_packets_.pop(); | |
| 149 } | |
| 150 while (!send_packets_.empty()) { | |
| 151 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(send_packets_.front()); | |
| 152 send_packets_.pop(); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 // Responds to PpapiPluginMsg_VpnProvider_OnPacketReceived | |
| 157 void VpnProviderResource::OnPluginMsgOnPacketReceived( | |
| 158 const ResourceMessageReplyParams& params, | |
| 159 uint32_t packet_size, | |
| 160 uint32_t id) { | |
| 161 if (!bound_) { | |
| 162 // Ignore packet and mark shared memory as available | |
| 163 // Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id)); | |
|
bbudge
2016/06/06 17:53:03
remove commented out code and comment
Here and bel
adrian.belgun
2016/06/07 08:14:10
Done.
| |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 // Append received packet to queue. | |
| 168 void* packet_pointer = receive_packet_buffer_->GetBuffer(id); | |
| 169 scoped_refptr<Var> packet_var( | |
| 170 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size, | |
| 171 packet_pointer)); | |
| 172 received_packets_.push(packet_var); | |
| 173 | |
| 174 // Mark shared memory as available for next packet | |
| 175 // Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id)); | |
| 176 | |
| 177 if (!TrackedCallback::IsPending(receive_packet_callback_) || | |
| 178 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) { | |
| 179 return; | |
| 180 } | |
| 181 | |
| 182 scoped_refptr<TrackedCallback> callback; | |
| 183 callback.swap(receive_packet_callback_); | |
| 184 WritePacket(); | |
| 185 callback->Run(PP_OK); | |
| 186 } | |
| 187 | |
| 188 // Responds to PpapiPluginMsg_VpnProvider_BindReply | |
| 189 // Forwards to bind_callback_ | |
| 190 void VpnProviderResource::OnPluginMsgBindReply( | |
| 191 const ResourceMessageReplyParams& params, | |
| 192 uint32_t queue_size, | |
| 193 uint32_t max_packet_size, | |
| 194 int32_t result) { | |
| 195 if (!TrackedCallback::IsPending(bind_callback_)) | |
| 196 return; | |
| 197 | |
| 198 if (params.result() == PP_OK) { | |
| 199 std::vector<base::SharedMemoryHandle> shm_handles; | |
| 200 params.TakeAllSharedMemoryHandles(&shm_handles); | |
| 201 std::unique_ptr<base::SharedMemory> send_shm( | |
| 202 new base::SharedMemory(shm_handles[0], false)); | |
| 203 std::unique_ptr<base::SharedMemory> receive_shm( | |
| 204 new base::SharedMemory(shm_handles[1], false)); | |
| 205 size_t buffer_size = queue_size * max_packet_size; | |
| 206 if (!send_shm->Map(buffer_size) || !receive_shm->Map(buffer_size)) { | |
| 207 NOTREACHED(); | |
| 208 return; | |
| 209 } | |
| 210 send_packet_buffer_ = base::WrapUnique(new ppapi::VpnProviderSharedBuffer( | |
| 211 queue_size, max_packet_size, std::move(send_shm))); | |
| 212 receive_packet_buffer_ = | |
| 213 base::WrapUnique(new ppapi::VpnProviderSharedBuffer( | |
| 214 queue_size, max_packet_size, std::move(receive_shm))); | |
| 215 | |
| 216 bound_ = (result == PP_OK); | |
| 217 } | |
| 218 | |
| 219 scoped_refptr<TrackedCallback> callback; | |
| 220 callback.swap(bind_callback_); | |
| 221 callback->Run(params.result() ? params.result() : result); | |
| 222 } | |
| 223 | |
| 224 // Responds to PpapiPluginMsg_VpnProvider_SendPacketReply | |
| 225 // Forwards to send_packet_callback_ | |
| 226 void VpnProviderResource::OnPluginMsgSendPacketReply( | |
| 227 const ResourceMessageReplyParams& params, | |
| 228 int32_t id) { | |
| 229 if (!send_packets_.empty() && bound_) { | |
| 230 // Process remaining packets | |
| 231 DoSendPacket(send_packets_.front(), id); | |
| 232 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(send_packets_.front()); | |
| 233 send_packets_.pop(); | |
| 234 } else { | |
| 235 send_packet_buffer_->SetAvailable(id, true); | |
| 236 | |
| 237 // Available slots - Run callback to process new packets. | |
| 238 if (TrackedCallback::IsPending(send_packet_callback_)) { | |
| 239 scoped_refptr<TrackedCallback> callback; | |
| 240 callback.swap(send_packet_callback_); | |
| 241 callback->Run(PP_OK); | |
| 242 } | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 void VpnProviderResource::WritePacket() { | |
| 247 if (!receive_packet_callback_var_) | |
| 248 return; | |
| 249 | |
| 250 *receive_packet_callback_var_ = received_packets_.front()->GetPPVar(); | |
| 251 received_packets_.pop(); | |
| 252 receive_packet_callback_var_ = nullptr; | |
| 253 } | |
| 254 | |
| 255 } // namespace proxy | |
| 256 } // namespace ppapi | |
| OLD | NEW |