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

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

Powered by Google App Engine
This is Rietveld 408576698