Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: ppapi/proxy/vpn_provider_resource.cc

Issue 1931513002: ppapi: PPB_VpnProvider: Implement Resource Stub (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@vpn-api-messages
Patch Set: Respond to review. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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());
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)
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;
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 }
73
74 int32_t VpnProviderResource::SendPacket(
75 const PP_Var& packet,
76 const scoped_refptr<TrackedCallback>& callback) {
77 if (!bound_)
78 return PP_ERROR_FAILED;
79 if (TrackedCallback::IsPending(send_packet_callback_))
80 return PP_ERROR_INPROGRESS;
81 if (!ArrayBufferVar::FromPPVar(packet))
82 return PP_ERROR_BADARGUMENT;
83
84 uint32_t id;
85 if (send_packet_buffer_.get() && send_packet_buffer_->GetAvailable(&id)) {
86 // Send packet immediately
87 send_packet_buffer_->SetAvailable(id, false);
88 return DoSendPacket(packet, id);
89 } else {
90 // Packet will be sent later
91 send_packet_callback_ = callback;
92 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(packet);
93 send_packets_.push(packet);
94
95 return PP_OK_COMPLETIONPENDING;
96 }
97 }
98
99 int32_t VpnProviderResource::DoSendPacket(const PP_Var& packet, uint32_t id) {
100 // Convert packet to std::vector<char>, then send it.
101 scoped_refptr<ArrayBufferVar> packet_arraybuffer =
102 ArrayBufferVar::FromPPVar(packet);
103 if (!packet_arraybuffer.get())
104 return PP_ERROR_BADARGUMENT;
105
106 char* packet_pointer = static_cast<char*>(packet_arraybuffer->Map());
107 uint32_t packet_size = packet_arraybuffer->ByteLength();
108 memcpy(send_packet_buffer_->GetBuffer(id), packet_pointer, packet_size);
109 packet_arraybuffer->Unmap();
110
111 Call<PpapiPluginMsg_VpnProvider_SendPacketReply>(
112 BROWSER, PpapiHostMsg_VpnProvider_SendPacket(packet_size, id),
113 base::Bind(&VpnProviderResource::OnPluginMsgSendPacketReply, this));
114
115 return PP_OK;
116 }
117
118 int32_t VpnProviderResource::ReceivePacket(
119 PP_Var* packet,
120 const scoped_refptr<TrackedCallback>& callback) {
121 if (TrackedCallback::IsPending(receive_packet_callback_))
122 return PP_ERROR_INPROGRESS;
123
124 // Return previously received packet.
125 if (!received_packets_.empty()) {
126 receive_packet_callback_var_ = packet;
127 WritePacket();
128 return PP_OK;
129 }
130
131 // Or retain packet var and install callback.
132 receive_packet_callback_var_ = packet;
133 receive_packet_callback_ = callback;
134
135 return PP_OK_COMPLETIONPENDING;
136 }
137
138 // Responds to PpapiPluginMsg_VpnProvider_OnUnbind
139 void VpnProviderResource::OnPluginMsgOnUnbindReceived(
140 const ResourceMessageReplyParams& params) {
141 bound_ = false;
142
143 // Cleanup in-flight packets.
144 while (!received_packets_.empty()) {
145 received_packets_.pop();
146 }
147 while (!send_packets_.empty()) {
148 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(send_packets_.front());
149 send_packets_.pop();
150 }
151 }
152
153 // Responds to PpapiPluginMsg_VpnProvider_OnPacketReceived
154 void VpnProviderResource::OnPluginMsgOnPacketReceived(
155 const ResourceMessageReplyParams& params,
156 uint32_t packet_size,
157 uint32_t id) {
158 if (!bound_) {
159 // Ignore packet and mark shared memory as available
160 Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id));
161 return;
162 }
163
164 // Append received packet to queue.
165 void* packet_pointer = receive_packet_buffer_->GetBuffer(id);
166 scoped_refptr<Var> packet_var(
167 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size,
168 packet_pointer));
169 received_packets_.push(packet_var);
170
171 // Mark shared memory as available for next packet
172 Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id));
173
174 if (!TrackedCallback::IsPending(receive_packet_callback_) ||
175 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) {
176 return;
177 }
178
179 scoped_refptr<TrackedCallback> callback;
180 callback.swap(receive_packet_callback_);
181 WritePacket();
182 callback->Run(PP_OK);
183 }
184
185 // Responds to PpapiPluginMsg_VpnProvider_BindReply
186 // Forwards to bind_callback_
187 void VpnProviderResource::OnPluginMsgBindReply(
188 const ResourceMessageReplyParams& params,
189 uint32_t queue_depth,
190 uint32_t max_packet_size,
191 int32_t result) {
192 if (!TrackedCallback::IsPending(bind_callback_))
193 return;
194
195 if (params.result() == PP_OK) {
196 std::vector<base::SharedMemoryHandle> shm_handles;
197 params.TakeAllSharedMemoryHandles(&shm_handles);
198 std::unique_ptr<base::SharedMemory> send_shm(
199 new base::SharedMemory(shm_handles[0], false));
200 std::unique_ptr<base::SharedMemory> receive_shm(
201 new base::SharedMemory(shm_handles[1], false));
202 size_t buffer_size = queue_depth * max_packet_size;
203 if (!send_shm->Map(buffer_size) || !receive_shm->Map(buffer_size)) {
204 NOTREACHED();
205 return;
206 }
207 send_packet_buffer_ = base::WrapUnique(new ppapi::VpnProviderSharedBuffer(
208 queue_depth, max_packet_size, std::move(send_shm)));
209 receive_packet_buffer_ =
210 base::WrapUnique(new ppapi::VpnProviderSharedBuffer(
211 queue_depth, max_packet_size, std::move(receive_shm)));
212
213 bound_ = (result == PP_OK);
214 }
215
216 scoped_refptr<TrackedCallback> callback;
217 callback.swap(bind_callback_);
218 callback->Run(params.result() ? params.result() : result);
219 }
220
221 // Responds to PpapiPluginMsg_VpnProvider_SendPacketReply
222 // Forwards to send_packet_callback_
223 void VpnProviderResource::OnPluginMsgSendPacketReply(
224 const ResourceMessageReplyParams& params,
225 int32_t id) {
226 if (!send_packets_.empty() && bound_) {
227 // Process remaining packets
228 DoSendPacket(send_packets_.front(), id);
229 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(send_packets_.front());
230 send_packets_.pop();
231 } else {
232 send_packet_buffer_->SetAvailable(id, true);
233
234 // Available slots - Run callback to process new packets.
235 if (TrackedCallback::IsPending(send_packet_callback_)) {
236 scoped_refptr<TrackedCallback> callback;
237 callback.swap(send_packet_callback_);
238 callback->Run(PP_OK);
239 }
240 }
241 }
242
243 void VpnProviderResource::WritePacket() {
244 if (!receive_packet_callback_var_)
245 return;
246
247 *receive_packet_callback_var_ = received_packets_.front()->GetPPVar();
248 received_packets_.pop();
249 receive_packet_callback_var_ = nullptr;
250 }
251
252 } // namespace proxy
253 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698