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