1 // Copyright 2013 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_tcp_socket.idl modified Sun Sep 15 16:14:21 2013.
6
7 #include "ppapi/c/pp_completion_callback.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/c/ppb_tcp_socket.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_tcp_socket_api.h"
14 #include "ppapi/thunk/resource_creation_api.h"
15 #include "ppapi/thunk/thunk.h"
16
17 namespace ppapi {
18 namespace thunk {
19
20 namespace {
21
Create_1_0(PP_Instance instance)22 PP_Resource Create_1_0(PP_Instance instance) {
23 VLOG(4) << "PPB_TCPSocket::Create_1_0()";
24 EnterResourceCreation enter(instance);
25 if (enter.failed())
26 return 0;
27 return enter.functions()->CreateTCPSocket1_0(instance);
28 }
29
Create(PP_Instance instance)30 PP_Resource Create(PP_Instance instance) {
31 VLOG(4) << "PPB_TCPSocket::Create()";
32 EnterResourceCreation enter(instance);
33 if (enter.failed())
34 return 0;
35 return enter.functions()->CreateTCPSocket(instance);
36 }
37
IsTCPSocket(PP_Resource resource)38 PP_Bool IsTCPSocket(PP_Resource resource) {
39 VLOG(4) << "PPB_TCPSocket::IsTCPSocket()";
40 EnterResource<PPB_TCPSocket_API> enter(resource, false);
41 return PP_FromBool(enter.succeeded());
42 }
43
Bind(PP_Resource tcp_socket,PP_Resource addr,struct PP_CompletionCallback callback)44 int32_t Bind(PP_Resource tcp_socket,
45 PP_Resource addr,
46 struct PP_CompletionCallback callback) {
47 VLOG(4) << "PPB_TCPSocket::Bind()";
48 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
49 if (enter.failed())
50 return enter.retval();
51 return enter.SetResult(enter.object()->Bind(addr, enter.callback()));
52 }
53
Connect(PP_Resource tcp_socket,PP_Resource addr,struct PP_CompletionCallback callback)54 int32_t Connect(PP_Resource tcp_socket,
55 PP_Resource addr,
56 struct PP_CompletionCallback callback) {
57 VLOG(4) << "PPB_TCPSocket::Connect()";
58 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
59 if (enter.failed())
60 return enter.retval();
61 return enter.SetResult(enter.object()->Connect(addr, enter.callback()));
62 }
63
GetLocalAddress(PP_Resource tcp_socket)64 PP_Resource GetLocalAddress(PP_Resource tcp_socket) {
65 VLOG(4) << "PPB_TCPSocket::GetLocalAddress()";
66 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, true);
67 if (enter.failed())
68 return 0;
69 return enter.object()->GetLocalAddress();
70 }
71
GetRemoteAddress(PP_Resource tcp_socket)72 PP_Resource GetRemoteAddress(PP_Resource tcp_socket) {
73 VLOG(4) << "PPB_TCPSocket::GetRemoteAddress()";
74 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, true);
75 if (enter.failed())
76 return 0;
77 return enter.object()->GetRemoteAddress();
78 }
79
Read(PP_Resource tcp_socket,char * buffer,int32_t bytes_to_read,struct PP_CompletionCallback callback)80 int32_t Read(PP_Resource tcp_socket,
81 char* buffer,
82 int32_t bytes_to_read,
83 struct PP_CompletionCallback callback) {
84 VLOG(4) << "PPB_TCPSocket::Read()";
85 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
86 if (enter.failed())
87 return enter.retval();
88 return enter.SetResult(enter.object()->Read(buffer,
89 bytes_to_read,
90 enter.callback()));
91 }
92
Write(PP_Resource tcp_socket,const char * buffer,int32_t bytes_to_write,struct PP_CompletionCallback callback)93 int32_t Write(PP_Resource tcp_socket,
94 const char* buffer,
95 int32_t bytes_to_write,
96 struct PP_CompletionCallback callback) {
97 VLOG(4) << "PPB_TCPSocket::Write()";
98 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
99 if (enter.failed())
100 return enter.retval();
101 return enter.SetResult(enter.object()->Write(buffer,
102 bytes_to_write,
103 enter.callback()));
104 }
105
Listen(PP_Resource tcp_socket,int32_t backlog,struct PP_CompletionCallback callback)106 int32_t Listen(PP_Resource tcp_socket,
107 int32_t backlog,
108 struct PP_CompletionCallback callback) {
109 VLOG(4) << "PPB_TCPSocket::Listen()";
110 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
111 if (enter.failed())
112 return enter.retval();
113 return enter.SetResult(enter.object()->Listen(backlog, enter.callback()));
114 }
115
Accept(PP_Resource tcp_socket,PP_Resource * accepted_tcp_socket,struct PP_CompletionCallback callback)116 int32_t Accept(PP_Resource tcp_socket,
117 PP_Resource* accepted_tcp_socket,
118 struct PP_CompletionCallback callback) {
119 VLOG(4) << "PPB_TCPSocket::Accept()";
120 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
121 if (enter.failed())
122 return enter.retval();
123 return enter.SetResult(enter.object()->Accept(accepted_tcp_socket,
124 enter.callback()));
125 }
126
Close(PP_Resource tcp_socket)127 void Close(PP_Resource tcp_socket) {
128 VLOG(4) << "PPB_TCPSocket::Close()";
129 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, true);
130 if (enter.failed())
131 return;
132 enter.object()->Close();
133 }
134
SetOption(PP_Resource tcp_socket,PP_TCPSocket_Option name,struct PP_Var value,struct PP_CompletionCallback callback)135 int32_t SetOption(PP_Resource tcp_socket,
136 PP_TCPSocket_Option name,
137 struct PP_Var value,
138 struct PP_CompletionCallback callback) {
139 VLOG(4) << "PPB_TCPSocket::SetOption()";
140 EnterResource<PPB_TCPSocket_API> enter(tcp_socket, callback, true);
141 if (enter.failed())
142 return enter.retval();
143 return enter.SetResult(enter.object()->SetOption(name,
144 value,
145 enter.callback()));
146 }
147
148 const PPB_TCPSocket_1_0 g_ppb_tcpsocket_thunk_1_0 = {
149 &Create_1_0,
150 &IsTCPSocket,
151 &Connect,
152 &GetLocalAddress,
153 &GetRemoteAddress,
154 &Read,
155 &Write,
156 &Close,
157 &SetOption
158 };
159
160 const PPB_TCPSocket_1_1 g_ppb_tcpsocket_thunk_1_1 = {
161 &Create,
162 &IsTCPSocket,
163 &Bind,
164 &Connect,
165 &GetLocalAddress,
166 &GetRemoteAddress,
167 &Read,
168 &Write,
169 &Listen,
170 &Accept,
171 &Close,
172 &SetOption
173 };
174
175 } // namespace
176
GetPPB_TCPSocket_1_0_Thunk()177 const PPB_TCPSocket_1_0* GetPPB_TCPSocket_1_0_Thunk() {
178 return &g_ppb_tcpsocket_thunk_1_0;
179 }
180
GetPPB_TCPSocket_1_1_Thunk()181 const PPB_TCPSocket_1_1* GetPPB_TCPSocket_1_1_Thunk() {
182 return &g_ppb_tcpsocket_thunk_1_1;
183 }
184
185 } // namespace thunk
186 } // namespace ppapi
187