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 |
| 28 VpnProviderResource::~VpnProviderResource() {} |
| 29 |
| 30 thunk::PPB_VpnProvider_API* VpnProviderResource::AsPPB_VpnProvider_API() { |
| 31 return this; |
| 32 } |
| 33 |
| 34 void VpnProviderResource::OnReplyReceived( |
| 35 const ResourceMessageReplyParams& params, |
| 36 const IPC::Message& msg) { |
| 37 PluginResource::OnReplyReceived(params, msg); |
| 38 } |
| 39 |
| 40 int32_t VpnProviderResource::Bind( |
| 41 const PP_Var& configuration_id, |
| 42 const PP_Var& configuration_name, |
| 43 const scoped_refptr<TrackedCallback>& callback) { |
| 44 if (TrackedCallback::IsPending(bind_callback_)) |
| 45 return PP_ERROR_INPROGRESS; |
| 46 |
| 47 StringVar* configuration_id_var = StringVar::FromPPVar(configuration_id); |
| 48 if (!configuration_id_var) |
| 49 return PP_ERROR_BADARGUMENT; |
| 50 StringVar* configuration_name_var = StringVar::FromPPVar(configuration_name); |
| 51 if (!configuration_name_var) |
| 52 return PP_ERROR_BADARGUMENT; |
| 53 |
| 54 return PP_ERROR_NOTSUPPORTED; |
| 55 } |
| 56 |
| 57 int32_t VpnProviderResource::SendPacket( |
| 58 const PP_Var& packet, |
| 59 const scoped_refptr<TrackedCallback>& callback) { |
| 60 if (!bound_) |
| 61 return PP_ERROR_FAILED; |
| 62 if (TrackedCallback::IsPending(send_packet_callback_)) |
| 63 return PP_ERROR_INPROGRESS; |
| 64 if (!ArrayBufferVar::FromPPVar(packet)) |
| 65 return PP_ERROR_BADARGUMENT; |
| 66 |
| 67 uint32_t id; |
| 68 if (send_packet_buffer_.get() && send_packet_buffer_->GetAvailable(&id)) { |
| 69 // Send packet immediately |
| 70 send_packet_buffer_->SetAvailable(id, false); |
| 71 return DoSendPacket(packet, id); |
| 72 } else { |
| 73 // Packet will be sent later |
| 74 send_packet_callback_ = callback; |
| 75 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(packet); |
| 76 send_packets_.push(packet); |
| 77 |
| 78 return PP_OK_COMPLETIONPENDING; |
| 79 } |
| 80 } |
| 81 |
| 82 int32_t VpnProviderResource::DoSendPacket(const PP_Var& packet, uint32_t id) { |
| 83 // Convert packet to std::vector<char>, then send it. |
| 84 scoped_refptr<ArrayBufferVar> packet_arraybuffer = |
| 85 ArrayBufferVar::FromPPVar(packet); |
| 86 if (!packet_arraybuffer.get()) |
| 87 return PP_ERROR_BADARGUMENT; |
| 88 |
| 89 char* packet_pointer = static_cast<char*>(packet_arraybuffer->Map()); |
| 90 uint32_t packet_size = packet_arraybuffer->ByteLength(); |
| 91 memcpy(send_packet_buffer_->GetBuffer(id), packet_pointer, packet_size); |
| 92 packet_arraybuffer->Unmap(); |
| 93 |
| 94 return PP_OK; |
| 95 } |
| 96 |
| 97 int32_t VpnProviderResource::ReceivePacket( |
| 98 PP_Var* packet, |
| 99 const scoped_refptr<TrackedCallback>& callback) { |
| 100 if (TrackedCallback::IsPending(receive_packet_callback_)) |
| 101 return PP_ERROR_INPROGRESS; |
| 102 |
| 103 // Return previously received packet. |
| 104 if (!received_packets_.empty()) { |
| 105 receive_packet_callback_var_ = packet; |
| 106 WritePacket(); |
| 107 return PP_OK; |
| 108 } |
| 109 |
| 110 // Or retain packet var and install callback. |
| 111 receive_packet_callback_var_ = packet; |
| 112 receive_packet_callback_ = callback; |
| 113 |
| 114 return PP_OK_COMPLETIONPENDING; |
| 115 } |
| 116 |
| 117 void VpnProviderResource::OnPluginMsgOnUnbindReceived( |
| 118 const ResourceMessageReplyParams& params) { |
| 119 bound_ = false; |
| 120 |
| 121 // Cleanup in-flight packets. |
| 122 while (!received_packets_.empty()) { |
| 123 received_packets_.pop(); |
| 124 } |
| 125 while (!send_packets_.empty()) { |
| 126 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(send_packets_.front()); |
| 127 send_packets_.pop(); |
| 128 } |
| 129 } |
| 130 |
| 131 void VpnProviderResource::OnPluginMsgOnPacketReceived( |
| 132 const ResourceMessageReplyParams& params, |
| 133 uint32_t packet_size, |
| 134 uint32_t id) { |
| 135 if (!bound_) { |
| 136 return; |
| 137 } |
| 138 |
| 139 // Append received packet to queue. |
| 140 void* packet_pointer = receive_packet_buffer_->GetBuffer(id); |
| 141 scoped_refptr<Var> packet_var( |
| 142 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size, |
| 143 packet_pointer)); |
| 144 received_packets_.push(packet_var); |
| 145 |
| 146 if (!TrackedCallback::IsPending(receive_packet_callback_) || |
| 147 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) { |
| 148 return; |
| 149 } |
| 150 |
| 151 scoped_refptr<TrackedCallback> callback; |
| 152 callback.swap(receive_packet_callback_); |
| 153 WritePacket(); |
| 154 callback->Run(PP_OK); |
| 155 } |
| 156 |
| 157 void VpnProviderResource::OnPluginMsgBindReply( |
| 158 const ResourceMessageReplyParams& params, |
| 159 uint32_t queue_size, |
| 160 uint32_t max_packet_size, |
| 161 int32_t result) { |
| 162 if (!TrackedCallback::IsPending(bind_callback_)) |
| 163 return; |
| 164 |
| 165 if (params.result() == PP_OK) { |
| 166 std::vector<base::SharedMemoryHandle> shm_handles; |
| 167 params.TakeAllSharedMemoryHandles(&shm_handles); |
| 168 std::unique_ptr<base::SharedMemory> send_shm( |
| 169 new base::SharedMemory(shm_handles[0], false)); |
| 170 std::unique_ptr<base::SharedMemory> receive_shm( |
| 171 new base::SharedMemory(shm_handles[1], false)); |
| 172 size_t buffer_size = queue_size * max_packet_size; |
| 173 if (!send_shm->Map(buffer_size) || !receive_shm->Map(buffer_size)) { |
| 174 NOTREACHED(); |
| 175 return; |
| 176 } |
| 177 send_packet_buffer_ = base::WrapUnique(new ppapi::VpnProviderSharedBuffer( |
| 178 queue_size, max_packet_size, std::move(send_shm))); |
| 179 receive_packet_buffer_ = |
| 180 base::WrapUnique(new ppapi::VpnProviderSharedBuffer( |
| 181 queue_size, max_packet_size, std::move(receive_shm))); |
| 182 |
| 183 bound_ = (result == PP_OK); |
| 184 } |
| 185 |
| 186 scoped_refptr<TrackedCallback> callback; |
| 187 callback.swap(bind_callback_); |
| 188 callback->Run(params.result() ? params.result() : result); |
| 189 } |
| 190 |
| 191 void VpnProviderResource::OnPluginMsgSendPacketReply( |
| 192 const ResourceMessageReplyParams& params, |
| 193 int32_t id) { |
| 194 if (!send_packets_.empty() && bound_) { |
| 195 // Process remaining packets |
| 196 DoSendPacket(send_packets_.front(), id); |
| 197 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(send_packets_.front()); |
| 198 send_packets_.pop(); |
| 199 } else { |
| 200 send_packet_buffer_->SetAvailable(id, true); |
| 201 |
| 202 // Available slots - Run callback to process new packets. |
| 203 if (TrackedCallback::IsPending(send_packet_callback_)) { |
| 204 scoped_refptr<TrackedCallback> callback; |
| 205 callback.swap(send_packet_callback_); |
| 206 callback->Run(PP_OK); |
| 207 } |
| 208 } |
| 209 } |
| 210 |
| 211 void VpnProviderResource::WritePacket() { |
| 212 if (!receive_packet_callback_var_) |
| 213 return; |
| 214 |
| 215 *receive_packet_callback_var_ = received_packets_.front()->GetPPVar(); |
| 216 received_packets_.pop(); |
| 217 receive_packet_callback_var_ = nullptr; |
| 218 } |
| 219 |
| 220 } // namespace proxy |
| 221 } // namespace ppapi |
OLD | NEW |