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

Side by Side Diff: ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_websocket.cc

Issue 9227008: WebSocket Pepper API: SRPC proxy implementation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix win32 build Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "native_client/src/shared/ppapi_proxy/plugin_ppb_websocket.h"
6
7 #include "native_client/src/include/nacl_scoped_ptr.h"
8 #include "native_client/src/shared/ppapi_proxy/object_serialize.h"
9 #include "native_client/src/shared/ppapi_proxy/plugin_callback.h"
10 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h"
11 #include "native_client/src/shared/ppapi_proxy/utility.h"
12 #include "native_client/src/shared/srpc/nacl_srpc.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/c/pp_var.h"
16 #include "ppapi/c/dev/ppb_websocket_dev.h"
17 #include "srpcgen/ppb_rpc.h"
18
19 namespace ppapi_proxy {
20
21 namespace {
22
23 PP_Resource Create(PP_Instance instance) {
24 DebugPrintf("PPB_WebSocket::Create: instance=%"NACL_PRIu32"\n", instance);
25
26 PP_Resource resource;
27 NaClSrpcError srpc_result =
28 PpbWebSocketRpcClient::PPB_WebSocket_Create(
29 GetMainSrpcChannel(), instance, &resource);
30 DebugPrintf("PPB_WebSocket::Create: %s\n", NaClSrpcErrorString(srpc_result));
31
32 if (srpc_result == NACL_SRPC_RESULT_OK)
33 return resource;
34 return kInvalidResourceId;
35 }
36
37 PP_Bool IsWebSocket(PP_Resource resource) {
38 DebugPrintf(
39 "PPB_WebSocket::IsWebSocket: resource=%"NACL_PRIu32"\n", resource);
40
41 int32_t is_websocket;
42 NaClSrpcError srpc_result =
43 PpbWebSocketRpcClient::PPB_WebSocket_IsWebSocket(
44 GetMainSrpcChannel(), resource, &is_websocket);
45 DebugPrintf("PPB_WebSocket::IsWebSocket: %s\n",
46 NaClSrpcErrorString(srpc_result));
47
48 if (srpc_result == NACL_SRPC_RESULT_OK && is_websocket)
49 return PP_TRUE;
50 return PP_FALSE;
51 }
52
53 int32_t Connect(PP_Resource ws,
54 PP_Var url,
55 const PP_Var protocols[],
56 uint32_t protocol_count,
57 PP_CompletionCallback callback) {
58 DebugPrintf("PPB_WebSocket::Connect: ws=%"NACL_PRIu32"\n", ws);
59
60 nacl_abi_size_t url_size = kMaxReturnVarSize;
dmichael (off chromium) 2012/01/17 23:32:45 nit: kMaxReturnVarSize is meant for out-params; ur
Takashi Toyoshima 2012/01/18 11:07:16 Done.
61 nacl::scoped_array<char> url_bytes(Serialize(&url, 1, &url_size));
62
63 nacl_abi_size_t protocols_size = kMaxReturnVarSize;
64 nacl::scoped_array<char> protocols_bytes(
65 Serialize(protocols, protocol_count, &protocols_size));
66
67 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback);
68 if (callback_id == 0)
69 return PP_ERROR_BLOCKS_MAIN_THREAD;
70
71 int32_t pp_error;
72 NaClSrpcError srpc_result =
73 PpbWebSocketRpcClient::PPB_WebSocket_Connect(
74 GetMainSrpcChannel(),
75 ws,
76 url_size, url_bytes.get(),
77 protocols_size, protocols_bytes.get(),
78 static_cast<int32_t>(protocol_count),
79 callback_id,
80 &pp_error);
81 DebugPrintf("PPB_WebSocket::Connect: %s\n",
82 NaClSrpcErrorString(srpc_result));
83
84 if (srpc_result != NACL_SRPC_RESULT_OK)
85 pp_error = PP_ERROR_FAILED;
86 return MayForceCallback(callback, pp_error);
87 }
88
89 int32_t Close(PP_Resource ws,
90 uint16_t code,
91 PP_Var reason,
92 PP_CompletionCallback callback) {
93 DebugPrintf("PPB_WebSocket::Close: ws=%"NACL_PRIu32"\n", ws);
94
95 nacl_abi_size_t reason_size = kMaxReturnVarSize;
96 nacl::scoped_array<char> reason_bytes(Serialize(&reason, 1, &reason_size));
97
98 int32_t callback_id = CompletionCallbackTable::Get()->AddCallback(callback);
99 if (callback_id == 0)
100 return PP_ERROR_BLOCKS_MAIN_THREAD;
101
102 int32_t pp_error;
103 NaClSrpcError srpc_result =
104 PpbWebSocketRpcClient::PPB_WebSocket_Close(
105 GetMainSrpcChannel(),
106 ws,
107 code,
108 reason_size, reason_bytes.get(),
109 callback_id,
110 &pp_error);
111 DebugPrintf("PPB_WebSocket::Close: %s\n", NaClSrpcErrorString(srpc_result));
112
113 if (srpc_result != NACL_SRPC_RESULT_OK)
114 pp_error = PP_ERROR_FAILED;
115 return MayForceCallback(callback, pp_error);
116 }
117
118 int32_t ReceiveMessage(PP_Resource ws,
119 PP_Var* message,
120 PP_CompletionCallback callback) {
121 DebugPrintf("PPB_WebSocket::ReceiveMessage: ws=%"NACL_PRIu32"\n", ws);
122
123 int32_t callback_id =
124 CompletionCallbackTable::Get()->AddCallback(callback, message);
125 if (callback_id == 0)
126 return PP_ERROR_BLOCKS_MAIN_THREAD;
127
128 int32_t pp_error;
129 NaClSrpcError srpc_result =
130 PpbWebSocketRpcClient::PPB_WebSocket_ReceiveMessage(
131 GetMainSrpcChannel(),
132 ws,
133 callback_id,
134 &pp_error);
dmichael (off chromium) 2012/01/17 23:32:45 Should you have a faster path, as PPB_URLLoader do
Takashi Toyoshima 2012/01/18 11:07:16 This first implementation doesn't care about perfo
dmichael (off chromium) 2012/01/18 17:04:40 For what it's worth, I'm not *only* concerned with
135 DebugPrintf("PPB_WebSocket::ReceiveMessage: %s\n",
136 NaClSrpcErrorString(srpc_result));
137
138 if (srpc_result != NACL_SRPC_RESULT_OK)
139 pp_error = PP_ERROR_FAILED;
140 return MayForceCallback(callback, pp_error);
141 }
142
143 int32_t SendMessage(PP_Resource ws,
144 PP_Var message) {
145 DebugPrintf("PPB_WebSocket::SendMessage: ws=%"NACL_PRIu32"\n", ws);
146
147 nacl_abi_size_t message_size = kMaxReturnVarSize;
148 nacl::scoped_array<char> message_bytes(
149 Serialize(&message, 1, &message_size));
150
151 int32_t pp_error;
152 NaClSrpcError srpc_result =
153 PpbWebSocketRpcClient::PPB_WebSocket_SendMessage(
154 GetMainSrpcChannel(),
155 ws,
156 message_size, message_bytes.get(),
157 &pp_error);
158 DebugPrintf("PPB_WebSocket::SendMessage: %s\n",
159 NaClSrpcErrorString(srpc_result));
160
161 if (srpc_result != NACL_SRPC_RESULT_OK)
162 return PP_ERROR_FAILED;
163 return pp_error;
164 }
165
166 uint64_t GetBufferedAmount(PP_Resource ws) {
167 DebugPrintf("PPB_WebSocket::GetBufferedAmount: ws=%"NACL_PRIu32"\n", ws);
168
169 int64_t buffered_amount = 0;
170 NaClSrpcError srpc_result =
171 PpbWebSocketRpcClient::PPB_WebSocket_GetBufferedAmount(
172 GetMainSrpcChannel(), ws, &buffered_amount);
173 DebugPrintf("PPB_WebSocket::GetBufferedAmount: %s\n",
174 NaClSrpcErrorString(srpc_result));
175
176 return static_cast<uint64_t>(buffered_amount);
177 }
178
179 uint16_t GetCloseCode(PP_Resource ws) {
180 DebugPrintf("PPB_WebSocket::GetCloseCode: ws=%"NACL_PRIu32"\n", ws);
181
182 int32_t close_code = 0;
183 NaClSrpcError srpc_result =
184 PpbWebSocketRpcClient::PPB_WebSocket_GetCloseCode(
185 GetMainSrpcChannel(), ws, &close_code);
186 DebugPrintf("PPB_WebSocket::GetCloseCode: %s\n",
187 NaClSrpcErrorString(srpc_result));
188
189 return static_cast<uint16_t>(close_code);
190 }
191
192 PP_Var GetCloseReason(PP_Resource ws) {
193 DebugPrintf("PPB_WebSocket::GetCloseReason: ws=%"NACL_PRIu32"\n", ws);
194
195 nacl_abi_size_t reason_size = kMaxReturnVarSize;
196 nacl::scoped_array<char> reason_bytes(new char[reason_size]);
197 NaClSrpcError srpc_result =
198 PpbWebSocketRpcClient::PPB_WebSocket_GetCloseReason(
199 GetMainSrpcChannel(), ws, &reason_size, reason_bytes.get());
200 DebugPrintf("PPB_WebSocket::GetCloseReason: %s\n",
201 NaClSrpcErrorString(srpc_result));
202
203 if (srpc_result == NACL_SRPC_RESULT_OK) {
204 PP_Var reason = PP_MakeUndefined();
205 if (DeserializeTo(reason_bytes.get(), reason_size, 1, &reason))
206 return reason;
207 }
208
209 return PP_MakeUndefined();
210 }
211
212 PP_Bool GetCloseWasClean(PP_Resource ws) {
213 DebugPrintf(
214 "PPB_WebSocket::GetCloseWasClean: ws=%"NACL_PRIu32"\n", ws);
215
216 int32_t was_clean;
217 NaClSrpcError srpc_result =
218 PpbWebSocketRpcClient::PPB_WebSocket_GetCloseWasClean(
219 GetMainSrpcChannel(), ws, &was_clean);
220 DebugPrintf("PPB_WebSocket::GetCloseWasClean: %s\n",
221 NaClSrpcErrorString(srpc_result));
222
223 if (srpc_result == NACL_SRPC_RESULT_OK && was_clean)
224 return PP_TRUE;
225 return PP_FALSE;
226 }
227
228 PP_Var GetExtensions(PP_Resource ws) {
229 DebugPrintf("PPB_WebSocket::GetExtensions: ws=%"NACL_PRIu32"\n", ws);
230
231 nacl_abi_size_t extensions_size = kMaxReturnVarSize;
232 nacl::scoped_array<char> extensions_bytes(new char[extensions_size]);
233 NaClSrpcError srpc_result =
234 PpbWebSocketRpcClient::PPB_WebSocket_GetExtensions(
235 GetMainSrpcChannel(), ws, &extensions_size, extensions_bytes.get());
236 DebugPrintf("PPB_WebSocket::GetExtensions: %s\n",
237 NaClSrpcErrorString(srpc_result));
238
239 if (srpc_result == NACL_SRPC_RESULT_OK) {
240 PP_Var extensions = PP_MakeUndefined();
241 if (DeserializeTo(extensions_bytes.get(), extensions_size, 1, &extensions))
242 return extensions;
243 }
244
245 return PP_MakeUndefined();
246 }
247
248 PP_Var GetProtocol(PP_Resource ws) {
249 DebugPrintf("PPB_WebSocket::GetProtocol: ws=%"NACL_PRIu32"\n", ws);
250
251 nacl_abi_size_t protocol_size = kMaxReturnVarSize;
252 nacl::scoped_array<char> protocol_bytes(new char[protocol_size]);
253 NaClSrpcError srpc_result =
254 PpbWebSocketRpcClient::PPB_WebSocket_GetProtocol(
255 GetMainSrpcChannel(), ws, &protocol_size, protocol_bytes.get());
256 DebugPrintf("PPB_WebSocket::GetProtocol: %s\n",
257 NaClSrpcErrorString(srpc_result));
258
259 if (srpc_result == NACL_SRPC_RESULT_OK) {
260 PP_Var protocol = PP_MakeUndefined();
261 if (DeserializeTo(protocol_bytes.get(), protocol_size, 1, &protocol))
262 return protocol;
263 }
264
265 return PP_MakeUndefined();
266 }
267
268 PP_WebSocketReadyState_Dev GetReadyState(PP_Resource ws) {
269 DebugPrintf(
270 "PPB_WebSocket::GetReadyState: ws=%"NACL_PRIu32"\n", ws);
271
272 int32_t ready_state;
273 NaClSrpcError srpc_result =
274 PpbWebSocketRpcClient::PPB_WebSocket_GetReadyState(
275 GetMainSrpcChannel(), ws, &ready_state);
276 DebugPrintf("PPB_WebSocket::GetReadyState: %s\n",
277 NaClSrpcErrorString(srpc_result));
278
279 if (srpc_result != NACL_SRPC_RESULT_OK)
280 return PP_WEBSOCKETREADYSTATE_INVALID_DEV;
281 return static_cast<PP_WebSocketReadyState_Dev>(ready_state);
282 }
283
284 PP_Var GetURL(PP_Resource ws) {
285 DebugPrintf("PPB_WebSocket::GetURL: ws=%"NACL_PRIu32"\n", ws);
286
287 nacl_abi_size_t url_size = kMaxReturnVarSize;
288 nacl::scoped_array<char> url_bytes(new char[url_size]);
289 NaClSrpcError srpc_result =
290 PpbWebSocketRpcClient::PPB_WebSocket_GetURL(
291 GetMainSrpcChannel(), ws, &url_size, url_bytes.get());
292 DebugPrintf("PPB_WebSocket::GetURL: %s\n",
293 NaClSrpcErrorString(srpc_result));
294
295 if (srpc_result == NACL_SRPC_RESULT_OK) {
296 PP_Var url = PP_MakeUndefined();
297 if (DeserializeTo(url_bytes.get(), url_size, 1, &url))
298 return url;
299 }
300
301 return PP_MakeUndefined();
302 }
303
304 PP_Bool SetBinaryType(PP_Resource ws,
305 PP_WebSocketBinaryType_Dev binary_type) {
306 DebugPrintf("PPB_WebSocket::SetBinaryType: ws=%"NACL_PRIu32"\n", ws);
307
308 int32_t success;
309 NaClSrpcError srpc_result =
310 PpbWebSocketRpcClient::PPB_WebSocket_SetBinaryType(
311 GetMainSrpcChannel(),
312 ws,
313 static_cast<int32_t>(binary_type),
314 &success);
315 DebugPrintf("PPB_WebSocket::SetBinaryType: %s\n",
316 NaClSrpcErrorString(srpc_result));
317
318 if (srpc_result == NACL_SRPC_RESULT_OK && success)
319 return PP_TRUE;
320 return PP_FALSE;
321 }
322
323 PP_WebSocketBinaryType_Dev GetBinaryType(PP_Resource ws) {
324 DebugPrintf("PPB_WebSocket::GetBinaryType: ws=%"NACL_PRIu32"\n", ws);
325
326 int32_t binary_type;
327 NaClSrpcError srpc_result =
328 PpbWebSocketRpcClient::PPB_WebSocket_GetBinaryType(
329 GetMainSrpcChannel(),
330 ws,
331 &binary_type);
332 DebugPrintf("PPB_WebSocket::GetBinaryType: %s\n",
333 NaClSrpcErrorString(srpc_result));
334
335 if (srpc_result != NACL_SRPC_RESULT_OK)
336 return PP_WEBSOCKETBINARYTYPE_INVALID;
337 return static_cast<PP_WebSocketBinaryType_Dev>(binary_type);
338 }
339
340 } // namespace
341
342 const PPB_WebSocket_Dev* PluginWebSocket::GetInterface() {
343 static const PPB_WebSocket_Dev websocket_interface = {
344 &Create,
345 &IsWebSocket,
346 &Connect,
347 &Close,
348 &ReceiveMessage,
349 &SendMessage,
350 &GetBufferedAmount,
351 &GetCloseCode,
352 &GetCloseReason,
353 &GetCloseWasClean,
354 &GetExtensions,
355 &GetProtocol,
356 &GetReadyState,
357 &GetURL,
358 &SetBinaryType,
359 &GetBinaryType
360 };
361 return &websocket_interface;
362 }
363
364 } // namespace ppapi_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698