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// From ppb_websocket.idl modified Thu Feb 28 11:58:17 2013.
6
7#include "ppapi/c/pp_completion_callback.h"
8#include "ppapi/c/pp_errors.h"
9#include "ppapi/c/ppb_websocket.h"
10#include "ppapi/shared_impl/tracked_callback.h"
11#include "ppapi/thunk/enter.h"
12#include "ppapi/thunk/ppb_instance_api.h"
13#include "ppapi/thunk/ppb_websocket_api.h"
14#include "ppapi/thunk/resource_creation_api.h"
15#include "ppapi/thunk/thunk.h"
16
17namespace ppapi {
18namespace thunk {
19
20namespace {
21
22PP_Resource Create(PP_Instance instance) {
23  VLOG(4) << "PPB_WebSocket::Create()";
24  EnterResourceCreation enter(instance);
25  if (enter.failed())
26    return 0;
27  return enter.functions()->CreateWebSocket(instance);
28}
29
30PP_Bool IsWebSocket(PP_Resource resource) {
31  VLOG(4) << "PPB_WebSocket::IsWebSocket()";
32  EnterResource<PPB_WebSocket_API> enter(resource, false);
33  return PP_FromBool(enter.succeeded());
34}
35
36int32_t Connect(PP_Resource web_socket,
37                struct PP_Var url,
38                const struct PP_Var protocols[],
39                uint32_t protocol_count,
40                struct PP_CompletionCallback callback) {
41  VLOG(4) << "PPB_WebSocket::Connect()";
42  EnterResource<PPB_WebSocket_API> enter(web_socket, callback, false);
43  if (enter.failed())
44    return enter.retval();
45  return enter.SetResult(enter.object()->Connect(url,
46                                                 protocols,
47                                                 protocol_count,
48                                                 enter.callback()));
49}
50
51int32_t Close(PP_Resource web_socket,
52              uint16_t code,
53              struct PP_Var reason,
54              struct PP_CompletionCallback callback) {
55  VLOG(4) << "PPB_WebSocket::Close()";
56  EnterResource<PPB_WebSocket_API> enter(web_socket, callback, false);
57  if (enter.failed())
58    return enter.retval();
59  return enter.SetResult(enter.object()->Close(code, reason, enter.callback()));
60}
61
62int32_t ReceiveMessage(PP_Resource web_socket,
63                       struct PP_Var* message,
64                       struct PP_CompletionCallback callback) {
65  VLOG(4) << "PPB_WebSocket::ReceiveMessage()";
66  EnterResource<PPB_WebSocket_API> enter(web_socket, callback, false);
67  if (enter.failed())
68    return enter.retval();
69  return enter.SetResult(enter.object()->ReceiveMessage(message,
70                                                        enter.callback()));
71}
72
73int32_t SendMessage(PP_Resource web_socket, struct PP_Var message) {
74  VLOG(4) << "PPB_WebSocket::SendMessage()";
75  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
76  if (enter.failed())
77    return enter.retval();
78  return enter.object()->SendMessage(message);
79}
80
81uint64_t GetBufferedAmount(PP_Resource web_socket) {
82  VLOG(4) << "PPB_WebSocket::GetBufferedAmount()";
83  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
84  if (enter.failed())
85    return 0;
86  return enter.object()->GetBufferedAmount();
87}
88
89uint16_t GetCloseCode(PP_Resource web_socket) {
90  VLOG(4) << "PPB_WebSocket::GetCloseCode()";
91  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
92  if (enter.failed())
93    return 0;
94  return enter.object()->GetCloseCode();
95}
96
97struct PP_Var GetCloseReason(PP_Resource web_socket) {
98  VLOG(4) << "PPB_WebSocket::GetCloseReason()";
99  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
100  if (enter.failed())
101    return PP_MakeUndefined();
102  return enter.object()->GetCloseReason();
103}
104
105PP_Bool GetCloseWasClean(PP_Resource web_socket) {
106  VLOG(4) << "PPB_WebSocket::GetCloseWasClean()";
107  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
108  if (enter.failed())
109    return PP_FALSE;
110  return enter.object()->GetCloseWasClean();
111}
112
113struct PP_Var GetExtensions(PP_Resource web_socket) {
114  VLOG(4) << "PPB_WebSocket::GetExtensions()";
115  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
116  if (enter.failed())
117    return PP_MakeUndefined();
118  return enter.object()->GetExtensions();
119}
120
121struct PP_Var GetProtocol(PP_Resource web_socket) {
122  VLOG(4) << "PPB_WebSocket::GetProtocol()";
123  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
124  if (enter.failed())
125    return PP_MakeUndefined();
126  return enter.object()->GetProtocol();
127}
128
129PP_WebSocketReadyState GetReadyState(PP_Resource web_socket) {
130  VLOG(4) << "PPB_WebSocket::GetReadyState()";
131  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
132  if (enter.failed())
133    return PP_WEBSOCKETREADYSTATE_INVALID;
134  return enter.object()->GetReadyState();
135}
136
137struct PP_Var GetURL(PP_Resource web_socket) {
138  VLOG(4) << "PPB_WebSocket::GetURL()";
139  EnterResource<PPB_WebSocket_API> enter(web_socket, false);
140  if (enter.failed())
141    return PP_MakeUndefined();
142  return enter.object()->GetURL();
143}
144
145const PPB_WebSocket_1_0 g_ppb_websocket_thunk_1_0 = {
146  &Create,
147  &IsWebSocket,
148  &Connect,
149  &Close,
150  &ReceiveMessage,
151  &SendMessage,
152  &GetBufferedAmount,
153  &GetCloseCode,
154  &GetCloseReason,
155  &GetCloseWasClean,
156  &GetExtensions,
157  &GetProtocol,
158  &GetReadyState,
159  &GetURL
160};
161
162}  // namespace
163
164const PPB_WebSocket_1_0* GetPPB_WebSocket_1_0_Thunk() {
165  return &g_ppb_websocket_thunk_1_0;
166}
167
168}  // namespace thunk
169}  // namespace ppapi
170