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

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

Issue 1735473002: ppapi: PPB_VpnProvider: Implement Resource Host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@vpn-nacl-sdk
Patch Set: Add packet size range checking. 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
OLDNEW
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
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;
bbudge 2016/06/23 18:10:23 It's fine to check this to help plugin writers, bu
adrian.belgun 2016/06/24 13:19:37 How can it be bypassed? When the plugin will call
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
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) {
135 if (!bound_) { 159 if (!bound_ || (packet_size > receive_packet_buffer_->GetMaxPacketSize())) {
bbudge 2016/06/23 18:10:23 packet_size is coming from the browser so this sho
adrian.belgun 2016/06/24 13:19:37 Done.
160 // Ignore packet and mark shared memory as available
161 Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id));
136 return; 162 return;
137 } 163 }
138 164
139 // Append received packet to queue. 165 // Append received packet to queue.
140 void* packet_pointer = receive_packet_buffer_->GetBuffer(id); 166 void* packet_pointer = receive_packet_buffer_->GetBuffer(id);
141 scoped_refptr<Var> packet_var( 167 scoped_refptr<Var> packet_var(
142 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size, 168 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferVar(packet_size,
143 packet_pointer)); 169 packet_pointer));
144 received_packets_.push(packet_var); 170 received_packets_.push(packet_var);
145 171
172 // Mark shared memory as available for next packet
173 Post(BROWSER, PpapiHostMsg_VpnProvider_OnPacketReceivedReply(id));
174
146 if (!TrackedCallback::IsPending(receive_packet_callback_) || 175 if (!TrackedCallback::IsPending(receive_packet_callback_) ||
147 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) { 176 TrackedCallback::IsScheduledToRun(receive_packet_callback_)) {
148 return; 177 return;
149 } 178 }
150 179
151 scoped_refptr<TrackedCallback> callback; 180 scoped_refptr<TrackedCallback> callback;
152 callback.swap(receive_packet_callback_); 181 callback.swap(receive_packet_callback_);
153 WritePacket(); 182 WritePacket();
154 callback->Run(PP_OK); 183 callback->Run(PP_OK);
155 } 184 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698