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