Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 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/proxy/tcp_socket_resource.h" |
| 6 | |
Hans Wennborg | 708fa82 | 2020-04-27 17:23:15 | [diff] [blame] | 7 | #include "base/check_op.h" |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 8 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 9 | #include "ppapi/shared_impl/ppb_tcp_socket_shared.h" |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 10 | #include "ppapi/thunk/enter.h" |
| 11 | #include "ppapi/thunk/ppb_net_address_api.h" |
| 12 | |
| 13 | namespace ppapi { |
| 14 | namespace proxy { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | typedef thunk::EnterResourceNoLock<thunk::PPB_NetAddress_API> |
| 19 | EnterNetAddressNoLock; |
| 20 | |
| 21 | } // namespace |
| 22 | |
| 23 | TCPSocketResource::TCPSocketResource(Connection connection, |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 24 | PP_Instance instance, |
| 25 | TCPSocketVersion version) |
| 26 | : TCPSocketResourceBase(connection, instance, version) { |
| 27 | DCHECK_NE(version, TCP_SOCKET_VERSION_PRIVATE); |
| 28 | SendCreate(BROWSER, PpapiHostMsg_TCPSocket_Create(version)); |
| 29 | } |
| 30 | |
| 31 | TCPSocketResource::TCPSocketResource(Connection connection, |
| 32 | PP_Instance instance, |
| 33 | int pending_host_id, |
| 34 | const PP_NetAddress_Private& local_addr, |
| 35 | const PP_NetAddress_Private& remote_addr) |
| 36 | : TCPSocketResourceBase(connection, instance, |
| 37 | TCP_SOCKET_VERSION_1_1_OR_ABOVE, local_addr, |
| 38 | remote_addr) { |
| 39 | AttachToPendingHost(BROWSER, pending_host_id); |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TCPSocketResource::~TCPSocketResource() { |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | thunk::PPB_TCPSocket_API* TCPSocketResource::AsPPB_TCPSocket_API() { |
| 46 | return this; |
| 47 | } |
| 48 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 49 | int32_t TCPSocketResource::Bind(PP_Resource addr, |
| 50 | scoped_refptr<TrackedCallback> callback) { |
| 51 | EnterNetAddressNoLock enter(addr, true); |
| 52 | if (enter.failed()) |
| 53 | return PP_ERROR_BADARGUMENT; |
| 54 | |
| 55 | return BindImpl(&enter.object()->GetNetAddressPrivate(), callback); |
| 56 | } |
| 57 | |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 58 | int32_t TCPSocketResource::Connect(PP_Resource addr, |
| 59 | scoped_refptr<TrackedCallback> callback) { |
| 60 | EnterNetAddressNoLock enter(addr, true); |
| 61 | if (enter.failed()) |
| 62 | return PP_ERROR_BADARGUMENT; |
| 63 | |
| 64 | return ConnectWithNetAddressImpl(&enter.object()->GetNetAddressPrivate(), |
| 65 | callback); |
| 66 | } |
| 67 | |
| 68 | PP_Resource TCPSocketResource::GetLocalAddress() { |
| 69 | PP_NetAddress_Private addr_private; |
| 70 | if (!GetLocalAddressImpl(&addr_private)) |
| 71 | return 0; |
| 72 | |
| 73 | thunk::EnterResourceCreationNoLock enter(pp_instance()); |
| 74 | if (enter.failed()) |
| 75 | return 0; |
| 76 | return enter.functions()->CreateNetAddressFromNetAddressPrivate( |
| 77 | pp_instance(), addr_private); |
| 78 | } |
| 79 | |
| 80 | PP_Resource TCPSocketResource::GetRemoteAddress() { |
| 81 | PP_NetAddress_Private addr_private; |
| 82 | if (!GetRemoteAddressImpl(&addr_private)) |
| 83 | return 0; |
| 84 | |
| 85 | thunk::EnterResourceCreationNoLock enter(pp_instance()); |
| 86 | if (enter.failed()) |
| 87 | return 0; |
| 88 | return enter.functions()->CreateNetAddressFromNetAddressPrivate( |
| 89 | pp_instance(), addr_private); |
| 90 | } |
| 91 | |
| 92 | int32_t TCPSocketResource::Read(char* buffer, |
| 93 | int32_t bytes_to_read, |
| 94 | scoped_refptr<TrackedCallback> callback) { |
| 95 | return ReadImpl(buffer, bytes_to_read, callback); |
| 96 | } |
| 97 | |
| 98 | int32_t TCPSocketResource::Write(const char* buffer, |
| 99 | int32_t bytes_to_write, |
| 100 | scoped_refptr<TrackedCallback> callback) { |
| 101 | return WriteImpl(buffer, bytes_to_write, callback); |
| 102 | } |
| 103 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 104 | int32_t TCPSocketResource::Listen(int32_t backlog, |
| 105 | scoped_refptr<TrackedCallback> callback) { |
| 106 | return ListenImpl(backlog, callback); |
| 107 | } |
| 108 | |
| 109 | int32_t TCPSocketResource::Accept(PP_Resource* accepted_tcp_socket, |
| 110 | scoped_refptr<TrackedCallback> callback) { |
| 111 | return AcceptImpl(accepted_tcp_socket, callback); |
| 112 | } |
| 113 | |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 114 | void TCPSocketResource::Close() { |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 115 | CloseImpl(); |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 116 | } |
| 117 | |
hidehiko | fd305c12 | 2014-12-11 06:01:47 | [diff] [blame] | 118 | int32_t TCPSocketResource::SetOption1_1( |
| 119 | PP_TCPSocket_Option name, |
| 120 | const PP_Var& value, |
| 121 | scoped_refptr<TrackedCallback> callback) { |
| 122 | return SetOptionImpl(name, value, |
| 123 | true, // Check connect() state. |
| 124 | callback); |
| 125 | } |
| 126 | |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 127 | int32_t TCPSocketResource::SetOption(PP_TCPSocket_Option name, |
| 128 | const PP_Var& value, |
| 129 | scoped_refptr<TrackedCallback> callback) { |
hidehiko | fd305c12 | 2014-12-11 06:01:47 | [diff] [blame] | 130 | return SetOptionImpl(name, value, |
| 131 | false, // Do not check connect() state. |
| 132 | callback); |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 9257679 | 2013-09-20 15:29:13 | [diff] [blame] | 135 | PP_Resource TCPSocketResource::CreateAcceptedSocket( |
| 136 | int pending_host_id, |
| 137 | const PP_NetAddress_Private& local_addr, |
| 138 | const PP_NetAddress_Private& remote_addr) { |
| 139 | return (new TCPSocketResource(connection(), pp_instance(), pending_host_id, |
| 140 | local_addr, remote_addr))->GetReference(); |
| 141 | } |
| 142 | |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 143 | } // namespace proxy |
| 144 | } // namespace ppapi |