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 "ppapi/c/pp_completion_callback.h"
6#include "ppapi/c/pp_errors.h"
7#include "ppapi/c/private/ppb_udp_socket_private.h"
8#include "ppapi/shared_impl/tracked_callback.h"
9#include "ppapi/thunk/enter.h"
10#include "ppapi/thunk/ppb_udp_socket_private_api.h"
11#include "ppapi/thunk/resource_creation_api.h"
12#include "ppapi/thunk/thunk.h"
13
14namespace ppapi {
15namespace thunk {
16
17namespace {
18
19typedef EnterResource<PPB_UDPSocket_Private_API> EnterUDP;
20
21PP_Resource Create(PP_Instance instance) {
22  EnterResourceCreation enter(instance);
23  if (enter.failed())
24    return 0;
25  return enter.functions()->CreateUDPSocketPrivate(instance);
26}
27
28PP_Bool IsUDPSocket(PP_Resource resource) {
29  EnterUDP enter(resource, false);
30  return PP_FromBool(enter.succeeded());
31}
32
33int32_t SetSocketFeature(PP_Resource udp_socket,
34                         PP_UDPSocketFeature_Private name,
35                         PP_Var value) {
36  EnterUDP enter(udp_socket, true);
37  if (enter.failed())
38    return PP_ERROR_BADRESOURCE;
39  return enter.object()->SetSocketFeature(name, value);
40}
41
42int32_t Bind(PP_Resource udp_socket,
43             const PP_NetAddress_Private *addr,
44             PP_CompletionCallback callback) {
45  EnterUDP enter(udp_socket, callback, true);
46  if (enter.failed())
47    return enter.retval();
48  return enter.SetResult(enter.object()->Bind(addr, enter.callback()));
49}
50
51PP_Bool GetBoundAddress(PP_Resource udp_socket,
52                        PP_NetAddress_Private* addr) {
53  EnterUDP enter(udp_socket, true);
54  if (enter.failed())
55    return PP_FALSE;
56  return enter.object()->GetBoundAddress(addr);
57}
58
59int32_t RecvFrom(PP_Resource udp_socket,
60                 char* buffer,
61                 int32_t num_bytes,
62                 PP_CompletionCallback callback) {
63#ifdef NDEBUG
64  EnterUDP enter(udp_socket, callback, false);
65#else
66  EnterUDP enter(udp_socket, callback, true);
67#endif
68  if (enter.failed())
69    return enter.retval();
70  return enter.SetResult(enter.object()->RecvFrom(buffer, num_bytes,
71                                                  enter.callback()));
72}
73
74PP_Bool GetRecvFromAddress(PP_Resource udp_socket,
75                           PP_NetAddress_Private* addr) {
76  EnterUDP enter(udp_socket, true);
77  if (enter.failed())
78    return PP_FALSE;
79  return enter.object()->GetRecvFromAddress(addr);
80}
81
82int32_t SendTo(PP_Resource udp_socket,
83               const char* buffer,
84               int32_t num_bytes,
85               const PP_NetAddress_Private* addr,
86               PP_CompletionCallback callback) {
87  EnterUDP enter(udp_socket, callback, true);
88  if (enter.failed())
89    return enter.retval();
90  return enter.SetResult(enter.object()->SendTo(buffer, num_bytes, addr,
91                                                enter.callback()));
92}
93
94void Close(PP_Resource udp_socket) {
95  EnterUDP enter(udp_socket, true);
96  if (enter.succeeded())
97    enter.object()->Close();
98}
99
100const PPB_UDPSocket_Private_0_2 g_ppb_udp_socket_thunk_0_2 = {
101  &Create,
102  &IsUDPSocket,
103  &Bind,
104  &RecvFrom,
105  &GetRecvFromAddress,
106  &SendTo,
107  &Close
108};
109
110const PPB_UDPSocket_Private_0_3 g_ppb_udp_socket_thunk_0_3 = {
111  &Create,
112  &IsUDPSocket,
113  &Bind,
114  &GetBoundAddress,
115  &RecvFrom,
116  &GetRecvFromAddress,
117  &SendTo,
118  &Close
119};
120
121const PPB_UDPSocket_Private_0_4 g_ppb_udp_socket_thunk_0_4 = {
122  &Create,
123  &IsUDPSocket,
124  &SetSocketFeature,
125  &Bind,
126  &GetBoundAddress,
127  &RecvFrom,
128  &GetRecvFromAddress,
129  &SendTo,
130  &Close
131};
132
133}  // namespace
134
135const PPB_UDPSocket_Private_0_2* GetPPB_UDPSocket_Private_0_2_Thunk() {
136  return &g_ppb_udp_socket_thunk_0_2;
137}
138
139const PPB_UDPSocket_Private_0_3* GetPPB_UDPSocket_Private_0_3_Thunk() {
140  return &g_ppb_udp_socket_thunk_0_3;
141}
142
143const PPB_UDPSocket_Private_0_4* GetPPB_UDPSocket_Private_0_4_Thunk() {
144  return &g_ppb_udp_socket_thunk_0_4;
145}
146
147}  // namespace thunk
148}  // namespace ppapi
149