OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/udp_socket_private_resource.h" | 5 #include "ppapi/proxy/udp_socket_resource_base.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "ppapi/c/pp_bool.h" | 11 #include "ppapi/c/pp_bool.h" |
13 #include "ppapi/c/pp_completion_callback.h" | 12 #include "ppapi/c/pp_completion_callback.h" |
14 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/thunk/enter.h" | |
16 #include "ppapi/thunk/resource_creation_api.h" | |
16 | 17 |
17 namespace ppapi { | 18 namespace ppapi { |
18 namespace proxy { | 19 namespace proxy { |
19 | 20 |
20 const int32_t UDPSocketPrivateResource::kMaxReadSize = 1024 * 1024; | 21 const int32_t UDPSocketResourceBase::kMaxReadSize = 1024 * 1024; |
21 const int32_t UDPSocketPrivateResource::kMaxWriteSize = 1024 * 1024; | 22 const int32_t UDPSocketResourceBase::kMaxWriteSize = 1024 * 1024; |
22 | 23 |
23 UDPSocketPrivateResource::UDPSocketPrivateResource(Connection connection, | 24 UDPSocketResourceBase::UDPSocketResourceBase(Connection connection, |
24 PP_Instance instance) | 25 PP_Instance instance) |
25 : PluginResource(connection, instance), | 26 : PluginResource(connection, instance), |
26 bound_(false), | 27 bound_(false), |
27 closed_(false), | 28 closed_(false), |
28 read_buffer_(NULL), | 29 read_buffer_(NULL), |
29 bytes_to_read_(-1) { | 30 bytes_to_read_(-1) { |
30 recvfrom_addr_.size = 0; | 31 recvfrom_addr_.size = 0; |
31 memset(recvfrom_addr_.data, 0, | 32 memset(recvfrom_addr_.data, 0, |
32 arraysize(recvfrom_addr_.data) * sizeof(*recvfrom_addr_.data)); | 33 arraysize(recvfrom_addr_.data) * sizeof(*recvfrom_addr_.data)); |
33 bound_addr_.size = 0; | 34 bound_addr_.size = 0; |
34 memset(bound_addr_.data, 0, | 35 memset(bound_addr_.data, 0, |
35 arraysize(bound_addr_.data) * sizeof(*bound_addr_.data)); | 36 arraysize(bound_addr_.data) * sizeof(*bound_addr_.data)); |
36 | 37 |
37 SendCreate(BROWSER, PpapiHostMsg_UDPSocketPrivate_Create()); | 38 SendCreate(BROWSER, PpapiHostMsg_UDPSocketPrivate_Create()); |
38 } | 39 } |
39 | 40 |
40 UDPSocketPrivateResource::~UDPSocketPrivateResource() { | 41 UDPSocketResourceBase::~UDPSocketResourceBase() { |
41 } | 42 } |
42 | 43 |
43 thunk::PPB_UDPSocket_Private_API* | 44 int32_t UDPSocketResourceBase::SetSocketFeatureImpl( |
44 UDPSocketPrivateResource::AsPPB_UDPSocket_Private_API() { | |
45 return this; | |
46 } | |
47 | |
48 int32_t UDPSocketPrivateResource::SetSocketFeature( | |
49 PP_UDPSocketFeature_Private name, | 45 PP_UDPSocketFeature_Private name, |
50 PP_Var value) { | 46 const PP_Var& value) { |
51 if (bound_ || closed_) | 47 if (bound_ || closed_) |
52 return PP_ERROR_FAILED; | 48 return PP_ERROR_FAILED; |
53 | 49 |
54 switch (name) { | 50 switch (name) { |
55 case PP_UDPSOCKETFEATURE_ADDRESS_REUSE: | 51 case PP_UDPSOCKETFEATURE_ADDRESS_REUSE: |
56 case PP_UDPSOCKETFEATURE_BROADCAST: | 52 case PP_UDPSOCKETFEATURE_BROADCAST: { |
57 if (value.type != PP_VARTYPE_BOOL) | 53 if (value.type != PP_VARTYPE_BOOL) |
58 return PP_ERROR_BADARGUMENT; | 54 return PP_ERROR_BADARGUMENT; |
59 SendBoolSocketFeature(static_cast<int32_t>(name), | 55 PpapiHostMsg_UDPSocketPrivate_SetBoolSocketFeature msg( |
60 PP_ToBool(value.value.as_bool)); | 56 static_cast<int32_t>(name), PP_ToBool(value.value.as_bool)); |
57 Post(BROWSER, msg); | |
61 break; | 58 break; |
62 default: | 59 } |
60 default: { | |
63 return PP_ERROR_BADARGUMENT; | 61 return PP_ERROR_BADARGUMENT; |
62 } | |
64 } | 63 } |
65 return PP_OK; | 64 return PP_OK; |
66 } | 65 } |
67 | 66 |
68 int32_t UDPSocketPrivateResource::Bind( | 67 int32_t UDPSocketResourceBase::BindImpl( |
69 const PP_NetAddress_Private* addr, | 68 const PP_NetAddress_Private* addr, |
70 scoped_refptr<TrackedCallback> callback) { | 69 scoped_refptr<TrackedCallback> callback) { |
71 if (!addr) | 70 if (!addr) |
72 return PP_ERROR_BADARGUMENT; | 71 return PP_ERROR_BADARGUMENT; |
73 if (bound_ || closed_) | 72 if (bound_ || closed_) |
74 return PP_ERROR_FAILED; | 73 return PP_ERROR_FAILED; |
75 if (TrackedCallback::IsPending(bind_callback_)) | 74 if (TrackedCallback::IsPending(bind_callback_)) |
76 return PP_ERROR_INPROGRESS; | 75 return PP_ERROR_INPROGRESS; |
77 | 76 |
78 bind_callback_ = callback; | 77 bind_callback_ = callback; |
79 | 78 |
80 // Send the request, the browser will call us back via BindReply. | 79 // Send the request, the browser will call us back via BindReply. |
81 SendBind(*addr); | 80 PpapiHostMsg_UDPSocketPrivate_Bind msg(*addr); |
81 Call<PpapiPluginMsg_UDPSocketPrivate_BindReply>( | |
82 BROWSER, | |
83 msg, | |
bbudge
2013/06/12 18:52:37
Why not construct the msg inline here?
yzshen1
2013/06/12 19:32:53
Done.
| |
84 base::Bind(&UDPSocketResourceBase::OnPluginMsgBindReply, | |
85 base::Unretained(this))); | |
82 return PP_OK_COMPLETIONPENDING; | 86 return PP_OK_COMPLETIONPENDING; |
83 } | 87 } |
84 | 88 |
85 PP_Bool UDPSocketPrivateResource::GetBoundAddress(PP_NetAddress_Private* addr) { | 89 PP_Bool UDPSocketResourceBase::GetBoundAddressImpl( |
90 PP_NetAddress_Private* addr) { | |
86 if (!addr || !bound_ || closed_) | 91 if (!addr || !bound_ || closed_) |
87 return PP_FALSE; | 92 return PP_FALSE; |
88 | 93 |
89 *addr = bound_addr_; | 94 *addr = bound_addr_; |
90 return PP_TRUE; | 95 return PP_TRUE; |
91 } | 96 } |
92 | 97 |
93 int32_t UDPSocketPrivateResource::RecvFrom( | 98 int32_t UDPSocketResourceBase::RecvFromImpl( |
94 char* buffer, | 99 char* buffer, |
95 int32_t num_bytes, | 100 int32_t num_bytes, |
101 PP_Resource* addr, | |
96 scoped_refptr<TrackedCallback> callback) { | 102 scoped_refptr<TrackedCallback> callback) { |
97 if (!buffer || num_bytes <= 0) | 103 if (!buffer || num_bytes <= 0) |
98 return PP_ERROR_BADARGUMENT; | 104 return PP_ERROR_BADARGUMENT; |
99 if (!bound_) | 105 if (!bound_) |
100 return PP_ERROR_FAILED; | 106 return PP_ERROR_FAILED; |
101 if (TrackedCallback::IsPending(recvfrom_callback_)) | 107 if (TrackedCallback::IsPending(recvfrom_callback_)) |
102 return PP_ERROR_INPROGRESS; | 108 return PP_ERROR_INPROGRESS; |
103 | 109 |
104 read_buffer_ = buffer; | 110 read_buffer_ = buffer; |
105 bytes_to_read_ = std::min(num_bytes, kMaxReadSize); | 111 bytes_to_read_ = std::min(num_bytes, kMaxReadSize); |
106 recvfrom_callback_ = callback; | 112 recvfrom_callback_ = callback; |
107 | 113 |
108 // Send the request, the browser will call us back via RecvFromReply. | 114 // Send the request, the browser will call us back via RecvFromReply. |
109 SendRecvFrom(bytes_to_read_); | 115 PpapiHostMsg_UDPSocketPrivate_RecvFrom msg(bytes_to_read_); |
116 Call<PpapiPluginMsg_UDPSocketPrivate_RecvFromReply>( | |
117 BROWSER, | |
118 msg, | |
bbudge
2013/06/12 18:52:37
Same as above.
yzshen1
2013/06/12 19:32:53
Done.
| |
119 base::Bind(&UDPSocketResourceBase::OnPluginMsgRecvFromReply, | |
120 base::Unretained(this), addr)); | |
110 return PP_OK_COMPLETIONPENDING; | 121 return PP_OK_COMPLETIONPENDING; |
111 } | 122 } |
112 | 123 |
113 PP_Bool UDPSocketPrivateResource::GetRecvFromAddress( | 124 PP_Bool UDPSocketResourceBase::GetRecvFromAddressImpl( |
114 PP_NetAddress_Private* addr) { | 125 PP_NetAddress_Private* addr) { |
115 if (!addr) | 126 if (!addr) |
116 return PP_FALSE; | 127 return PP_FALSE; |
117 *addr = recvfrom_addr_; | 128 *addr = recvfrom_addr_; |
118 return PP_TRUE; | 129 return PP_TRUE; |
119 } | 130 } |
120 | 131 |
121 void UDPSocketPrivateResource::PostAbortIfNecessary( | 132 int32_t UDPSocketResourceBase::SendToImpl( |
122 scoped_refptr<TrackedCallback>* callback) { | |
123 if (TrackedCallback::IsPending(*callback)) | |
124 (*callback)->PostAbort(); | |
125 } | |
126 | |
127 int32_t UDPSocketPrivateResource::SendTo( | |
128 const char* buffer, | 133 const char* buffer, |
129 int32_t num_bytes, | 134 int32_t num_bytes, |
130 const PP_NetAddress_Private* addr, | 135 const PP_NetAddress_Private* addr, |
131 scoped_refptr<TrackedCallback> callback) { | 136 scoped_refptr<TrackedCallback> callback) { |
132 if (!buffer || num_bytes <= 0 || !addr) | 137 if (!buffer || num_bytes <= 0 || !addr) |
133 return PP_ERROR_BADARGUMENT; | 138 return PP_ERROR_BADARGUMENT; |
134 if (!bound_) | 139 if (!bound_) |
135 return PP_ERROR_FAILED; | 140 return PP_ERROR_FAILED; |
136 if (TrackedCallback::IsPending(sendto_callback_)) | 141 if (TrackedCallback::IsPending(sendto_callback_)) |
137 return PP_ERROR_INPROGRESS; | 142 return PP_ERROR_INPROGRESS; |
138 | 143 |
139 if (num_bytes > kMaxWriteSize) | 144 if (num_bytes > kMaxWriteSize) |
140 num_bytes = kMaxWriteSize; | 145 num_bytes = kMaxWriteSize; |
141 | 146 |
142 sendto_callback_ = callback; | 147 sendto_callback_ = callback; |
143 | 148 |
144 // Send the request, the browser will call us back via SendToReply. | 149 // Send the request, the browser will call us back via SendToReply. |
145 SendSendTo(std::string(buffer, num_bytes), *addr); | 150 PpapiHostMsg_UDPSocketPrivate_SendTo msg(std::string(buffer, num_bytes), |
151 *addr); | |
152 Call<PpapiPluginMsg_UDPSocketPrivate_SendToReply>( | |
153 BROWSER, | |
154 msg, | |
bbudge
2013/06/12 18:52:37
inline?
yzshen1
2013/06/12 19:32:53
Done.
| |
155 base::Bind(&UDPSocketResourceBase::OnPluginMsgSendToReply, | |
156 base::Unretained(this))); | |
146 return PP_OK_COMPLETIONPENDING; | 157 return PP_OK_COMPLETIONPENDING; |
147 } | 158 } |
148 | 159 |
149 void UDPSocketPrivateResource::Close() { | 160 void UDPSocketResourceBase::CloseImpl() { |
150 if(closed_) | 161 if(closed_) |
151 return; | 162 return; |
152 | 163 |
153 bound_ = false; | 164 bound_ = false; |
154 closed_ = true; | 165 closed_ = true; |
155 | 166 |
156 SendClose(); | 167 PpapiHostMsg_UDPSocketPrivate_Close msg; |
168 Post(BROWSER, msg); | |
157 | 169 |
158 PostAbortIfNecessary(&bind_callback_); | 170 PostAbortIfNecessary(&bind_callback_); |
159 PostAbortIfNecessary(&recvfrom_callback_); | 171 PostAbortIfNecessary(&recvfrom_callback_); |
160 PostAbortIfNecessary(&sendto_callback_); | 172 PostAbortIfNecessary(&sendto_callback_); |
161 } | 173 } |
162 | 174 |
163 void UDPSocketPrivateResource::SendBoolSocketFeature(int32_t name, bool value) { | 175 void UDPSocketResourceBase::PostAbortIfNecessary( |
164 PpapiHostMsg_UDPSocketPrivate_SetBoolSocketFeature msg(name, value); | 176 scoped_refptr<TrackedCallback>* callback) { |
165 Post(BROWSER, msg); | 177 if (TrackedCallback::IsPending(*callback)) |
178 (*callback)->PostAbort(); | |
166 } | 179 } |
167 | 180 |
168 void UDPSocketPrivateResource::SendBind(const PP_NetAddress_Private& addr) { | 181 void UDPSocketResourceBase::OnPluginMsgBindReply( |
169 PpapiHostMsg_UDPSocketPrivate_Bind msg(addr); | |
170 Call<PpapiPluginMsg_UDPSocketPrivate_BindReply>( | |
171 BROWSER, | |
172 msg, | |
173 base::Bind(&UDPSocketPrivateResource::OnPluginMsgBindReply, | |
174 base::Unretained(this))); | |
175 } | |
176 | |
177 void UDPSocketPrivateResource::SendRecvFrom(int32_t num_bytes) { | |
178 PpapiHostMsg_UDPSocketPrivate_RecvFrom msg(num_bytes); | |
179 Call<PpapiPluginMsg_UDPSocketPrivate_RecvFromReply>( | |
180 BROWSER, | |
181 msg, | |
182 base::Bind(&UDPSocketPrivateResource::OnPluginMsgRecvFromReply, | |
183 base::Unretained(this))); | |
184 } | |
185 | |
186 void UDPSocketPrivateResource::SendSendTo(const std::string& buffer, | |
187 const PP_NetAddress_Private& addr) { | |
188 PpapiHostMsg_UDPSocketPrivate_SendTo msg(buffer, addr); | |
189 Call<PpapiPluginMsg_UDPSocketPrivate_SendToReply>( | |
190 BROWSER, | |
191 msg, | |
192 base::Bind(&UDPSocketPrivateResource::OnPluginMsgSendToReply, | |
193 base::Unretained(this))); | |
194 } | |
195 | |
196 void UDPSocketPrivateResource::SendClose() { | |
197 PpapiHostMsg_UDPSocketPrivate_Close msg; | |
198 Post(BROWSER, msg); | |
199 } | |
200 | |
201 void UDPSocketPrivateResource::OnPluginMsgBindReply( | |
202 const ResourceMessageReplyParams& params, | 182 const ResourceMessageReplyParams& params, |
203 const PP_NetAddress_Private& bound_addr) { | 183 const PP_NetAddress_Private& bound_addr) { |
204 if (!TrackedCallback::IsPending(bind_callback_)) { | 184 if (!TrackedCallback::IsPending(bind_callback_)) { |
205 NOTREACHED(); | 185 NOTREACHED(); |
206 return; | 186 return; |
207 } | 187 } |
208 if (params.result() == PP_OK) | 188 if (params.result() == PP_OK) |
209 bound_ = true; | 189 bound_ = true; |
210 bound_addr_ = bound_addr; | 190 bound_addr_ = bound_addr; |
211 bind_callback_->Run(params.result()); | 191 bind_callback_->Run(params.result()); |
212 } | 192 } |
213 | 193 |
214 void UDPSocketPrivateResource::OnPluginMsgRecvFromReply( | 194 void UDPSocketResourceBase::OnPluginMsgRecvFromReply( |
195 PP_Resource* output_addr, | |
215 const ResourceMessageReplyParams& params, | 196 const ResourceMessageReplyParams& params, |
216 const std::string& data, | 197 const std::string& data, |
217 const PP_NetAddress_Private& addr) { | 198 const PP_NetAddress_Private& addr) { |
218 if (!TrackedCallback::IsPending(recvfrom_callback_) || !read_buffer_) { | 199 if (!TrackedCallback::IsPending(recvfrom_callback_)) { |
219 NOTREACHED(); | 200 NOTREACHED(); |
220 return; | 201 return; |
221 } | 202 } |
222 bool succeeded = (params.result() == PP_OK); | 203 bool succeeded = (params.result() == PP_OK); |
204 if (succeeded && output_addr) { | |
205 thunk::EnterResourceCreationNoLock enter(pp_instance()); | |
206 if (enter.succeeded()) { | |
207 *output_addr = enter.functions()->CreateNetAddressFromNetAddressPrivate( | |
208 pp_instance(), addr); | |
209 } else { | |
210 succeeded = false; | |
211 } | |
212 } | |
213 | |
223 if (succeeded) { | 214 if (succeeded) { |
224 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_); | 215 CHECK_LE(static_cast<int32_t>(data.size()), bytes_to_read_); |
225 if (!data.empty()) | 216 if (!data.empty()) |
226 memcpy(read_buffer_, data.c_str(), data.size()); | 217 memcpy(read_buffer_, data.c_str(), data.size()); |
227 } | 218 } |
219 | |
228 read_buffer_ = NULL; | 220 read_buffer_ = NULL; |
229 bytes_to_read_ = -1; | 221 bytes_to_read_ = -1; |
230 recvfrom_addr_ = addr; | 222 recvfrom_addr_ = addr; |
231 | 223 |
232 if (succeeded) | 224 if (succeeded) |
233 recvfrom_callback_->Run(static_cast<int32_t>(data.size())); | 225 recvfrom_callback_->Run(static_cast<int32_t>(data.size())); |
234 else | 226 else |
235 recvfrom_callback_->Run(params.result()); | 227 recvfrom_callback_->Run(params.result()); |
236 } | 228 } |
237 | 229 |
238 void UDPSocketPrivateResource::OnPluginMsgSendToReply( | 230 void UDPSocketResourceBase::OnPluginMsgSendToReply( |
239 const ResourceMessageReplyParams& params, | 231 const ResourceMessageReplyParams& params, |
240 int32_t bytes_written) { | 232 int32_t bytes_written) { |
241 if (!TrackedCallback::IsPending(sendto_callback_)) { | 233 if (!TrackedCallback::IsPending(sendto_callback_)) { |
242 NOTREACHED(); | 234 NOTREACHED(); |
243 return; | 235 return; |
244 } | 236 } |
245 if (params.result() == PP_OK) | 237 if (params.result() == PP_OK) |
246 sendto_callback_->Run(bytes_written); | 238 sendto_callback_->Run(bytes_written); |
247 else | 239 else |
248 sendto_callback_->Run(params.result()); | 240 sendto_callback_->Run(params.result()); |
249 } | 241 } |
250 | 242 |
251 } // namespace proxy | 243 } // namespace proxy |
252 } // namespace ppapi | 244 } // namespace ppapi |
OLD | NEW |