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_->GetMaxPacketSize()) |
| 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 21 matching lines...) Expand all Loading... |
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 } |
129 } | 153 } |
130 | 154 |
131 void VpnProviderResource::OnPluginMsgOnPacketReceived( | 155 void VpnProviderResource::OnPluginMsgOnPacketReceived( |
132 const ResourceMessageReplyParams& params, | 156 const ResourceMessageReplyParams& params, |
133 uint32_t packet_size, | 157 uint32_t packet_size, |
134 uint32_t id) { | 158 uint32_t id) { |
| 159 DCHECK_LE(packet_size, recv_packet_buffer_->GetMaxPacketSize()); |
135 if (!bound_) { | 160 if (!bound_) { |
| 161 // Ignore packet and mark shared memory as available |
| 162 Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id)); |
136 return; | 163 return; |
137 } | 164 } |
138 | 165 |
139 // Append received packet to queue. | 166 // Append received packet to queue. |
140 void* packet_pointer = receive_packet_buffer_->GetBuffer(id); | 167 void* packet_pointer = recv_packet_buffer_->GetBuffer(id); |
141 scoped_refptr<Var> packet_var( | 168 scoped_refptr<Var> packet_var( |
142 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size, | 169 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size, |
143 packet_pointer)); | 170 packet_pointer)); |
144 received_packets_.push(packet_var); | 171 received_packets_.push(packet_var); |
145 | 172 |
| 173 // Mark shared memory as available for next packet |
| 174 Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id)); |
| 175 |
146 if (!TrackedCallback::IsPending(receive_packet_callback_) || | 176 if (!TrackedCallback::IsPending(receive_packet_callback_) || |
147 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) { | 177 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) { |
148 return; | 178 return; |
149 } | 179 } |
150 | 180 |
151 scoped_refptr<TrackedCallback> callback; | 181 scoped_refptr<TrackedCallback> callback; |
152 callback.swap(receive_packet_callback_); | 182 callback.swap(receive_packet_callback_); |
153 WritePacket(); | 183 WritePacket(); |
154 callback->Run(PP_OK); | 184 callback->Run(PP_OK); |
155 } | 185 } |
(...skipping 13 matching lines...) Expand all Loading... |
169 new base::SharedMemory(shm_handles[0], false)); | 199 new base::SharedMemory(shm_handles[0], false)); |
170 std::unique_ptr<base::SharedMemory> receive_shm( | 200 std::unique_ptr<base::SharedMemory> receive_shm( |
171 new base::SharedMemory(shm_handles[1], false)); | 201 new base::SharedMemory(shm_handles[1], false)); |
172 size_t buffer_size = queue_size * max_packet_size; | 202 size_t buffer_size = queue_size * max_packet_size; |
173 if (!send_shm->Map(buffer_size) || !receive_shm->Map(buffer_size)) { | 203 if (!send_shm->Map(buffer_size) || !receive_shm->Map(buffer_size)) { |
174 NOTREACHED(); | 204 NOTREACHED(); |
175 return; | 205 return; |
176 } | 206 } |
177 send_packet_buffer_ = base::WrapUnique(new ppapi::VpnProviderSharedBuffer( | 207 send_packet_buffer_ = base::WrapUnique(new ppapi::VpnProviderSharedBuffer( |
178 queue_size, max_packet_size, std::move(send_shm))); | 208 queue_size, max_packet_size, std::move(send_shm))); |
179 receive_packet_buffer_ = | 209 recv_packet_buffer_ = base::WrapUnique(new ppapi::VpnProviderSharedBuffer( |
180 base::WrapUnique(new ppapi::VpnProviderSharedBuffer( | 210 queue_size, max_packet_size, std::move(receive_shm))); |
181 queue_size, max_packet_size, std::move(receive_shm))); | |
182 | 211 |
183 bound_ = (result == PP_OK); | 212 bound_ = (result == PP_OK); |
184 } | 213 } |
185 | 214 |
186 scoped_refptr<TrackedCallback> callback; | 215 scoped_refptr<TrackedCallback> callback; |
187 callback.swap(bind_callback_); | 216 callback.swap(bind_callback_); |
188 callback->Run(params.result() ? params.result() : result); | 217 callback->Run(params.result() ? params.result() : result); |
189 } | 218 } |
190 | 219 |
191 void VpnProviderResource::OnPluginMsgSendPacketReply( | 220 void VpnProviderResource::OnPluginMsgSendPacketReply( |
(...skipping 20 matching lines...) Expand all Loading... |
212 if (!receive_packet_callback_var_) | 241 if (!receive_packet_callback_var_) |
213 return; | 242 return; |
214 | 243 |
215 *receive_packet_callback_var_ = received_packets_.front()->GetPPVar(); | 244 *receive_packet_callback_var_ = received_packets_.front()->GetPPVar(); |
216 received_packets_.pop(); | 245 received_packets_.pop(); |
217 receive_packet_callback_var_ = nullptr; | 246 receive_packet_callback_var_ = nullptr; |
218 } | 247 } |
219 | 248 |
220 } // namespace proxy | 249 } // namespace proxy |
221 } // namespace ppapi | 250 } // namespace ppapi |
OLD | NEW |