| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/tcp_socket.h" | 5 #include "ppapi/cpp/tcp_socket.h" |
| 6 | 6 |
| 7 #include "ppapi/c/pp_errors.h" | 7 #include "ppapi/c/pp_errors.h" |
| 8 #include "ppapi/cpp/completion_callback.h" | 8 #include "ppapi/cpp/completion_callback.h" |
| 9 #include "ppapi/cpp/instance_handle.h" | 9 #include "ppapi/cpp/instance_handle.h" |
| 10 #include "ppapi/cpp/module_impl.h" | 10 #include "ppapi/cpp/module_impl.h" |
| 11 | 11 |
| 12 namespace pp { | 12 namespace pp { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 template <> const char* interface_name<PPB_TCPSocket_1_0>() { | 16 template <> const char* interface_name<PPB_TCPSocket_1_0>() { |
| 17 return PPB_TCPSOCKET_INTERFACE_1_0; | 17 return PPB_TCPSOCKET_INTERFACE_1_0; |
| 18 } | 18 } |
| 19 | 19 |
| 20 template <> const char* interface_name<PPB_TCPSocket_1_1>() { | 20 template <> const char* interface_name<PPB_TCPSocket_1_1>() { |
| 21 return PPB_TCPSOCKET_INTERFACE_1_1; | 21 return PPB_TCPSOCKET_INTERFACE_1_1; |
| 22 } | 22 } |
| 23 | 23 |
| 24 template <> const char* interface_name<PPB_TCPSocket_1_2>() { |
| 25 return PPB_TCPSOCKET_INTERFACE_1_2; |
| 26 } |
| 27 |
| 24 } // namespace | 28 } // namespace |
| 25 | 29 |
| 26 TCPSocket::TCPSocket() { | 30 TCPSocket::TCPSocket() { |
| 27 } | 31 } |
| 28 | 32 |
| 29 TCPSocket::TCPSocket(const InstanceHandle& instance) { | 33 TCPSocket::TCPSocket(const InstanceHandle& instance) { |
| 30 if (has_interface<PPB_TCPSocket_1_1>()) { | 34 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 35 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_2>()->Create( |
| 36 instance.pp_instance())); |
| 37 } else if (has_interface<PPB_TCPSocket_1_1>()) { |
| 31 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_1>()->Create( | 38 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_1>()->Create( |
| 32 instance.pp_instance())); | 39 instance.pp_instance())); |
| 33 } else if (has_interface<PPB_TCPSocket_1_0>()) { | 40 } else if (has_interface<PPB_TCPSocket_1_0>()) { |
| 34 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_0>()->Create( | 41 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_0>()->Create( |
| 35 instance.pp_instance())); | 42 instance.pp_instance())); |
| 36 } | 43 } |
| 37 } | 44 } |
| 38 | 45 |
| 39 TCPSocket::TCPSocket(PassRef, PP_Resource resource) | 46 TCPSocket::TCPSocket(PassRef, PP_Resource resource) |
| 40 : Resource(PASS_REF, resource) { | 47 : Resource(PASS_REF, resource) { |
| 41 } | 48 } |
| 42 | 49 |
| 43 TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) { | 50 TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) { |
| 44 } | 51 } |
| 45 | 52 |
| 46 TCPSocket::~TCPSocket() { | 53 TCPSocket::~TCPSocket() { |
| 47 } | 54 } |
| 48 | 55 |
| 49 TCPSocket& TCPSocket::operator=(const TCPSocket& other) { | 56 TCPSocket& TCPSocket::operator=(const TCPSocket& other) { |
| 50 Resource::operator=(other); | 57 Resource::operator=(other); |
| 51 return *this; | 58 return *this; |
| 52 } | 59 } |
| 53 | 60 |
| 54 // static | 61 // static |
| 55 bool TCPSocket::IsAvailable() { | 62 bool TCPSocket::IsAvailable() { |
| 56 return has_interface<PPB_TCPSocket_1_1>() || | 63 return has_interface<PPB_TCPSocket_1_2>() || |
| 64 has_interface<PPB_TCPSocket_1_1>() || |
| 57 has_interface<PPB_TCPSocket_1_0>(); | 65 has_interface<PPB_TCPSocket_1_0>(); |
| 58 } | 66 } |
| 59 | 67 |
| 60 int32_t TCPSocket::Bind(const NetAddress& addr, | 68 int32_t TCPSocket::Bind(const NetAddress& addr, |
| 61 const CompletionCallback& callback) { | 69 const CompletionCallback& callback) { |
| 70 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 71 return get_interface<PPB_TCPSocket_1_2>()->Bind( |
| 72 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 73 } |
| 62 if (has_interface<PPB_TCPSocket_1_1>()) { | 74 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 63 return get_interface<PPB_TCPSocket_1_1>()->Bind( | 75 return get_interface<PPB_TCPSocket_1_1>()->Bind( |
| 64 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); | 76 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 65 } | 77 } |
| 66 return callback.MayForce(PP_ERROR_NOINTERFACE); | 78 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 67 } | 79 } |
| 68 | 80 |
| 69 int32_t TCPSocket::Connect(const NetAddress& addr, | 81 int32_t TCPSocket::Connect(const NetAddress& addr, |
| 70 const CompletionCallback& callback) { | 82 const CompletionCallback& callback) { |
| 83 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 84 return get_interface<PPB_TCPSocket_1_2>()->Connect( |
| 85 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 86 } |
| 71 if (has_interface<PPB_TCPSocket_1_1>()) { | 87 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 72 return get_interface<PPB_TCPSocket_1_1>()->Connect( | 88 return get_interface<PPB_TCPSocket_1_1>()->Connect( |
| 73 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); | 89 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 74 } | 90 } |
| 75 if (has_interface<PPB_TCPSocket_1_0>()) { | 91 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 76 return get_interface<PPB_TCPSocket_1_0>()->Connect( | 92 return get_interface<PPB_TCPSocket_1_0>()->Connect( |
| 77 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); | 93 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 78 } | 94 } |
| 79 return callback.MayForce(PP_ERROR_NOINTERFACE); | 95 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 80 } | 96 } |
| 81 | 97 |
| 82 NetAddress TCPSocket::GetLocalAddress() const { | 98 NetAddress TCPSocket::GetLocalAddress() const { |
| 99 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 100 return NetAddress( |
| 101 PASS_REF, |
| 102 get_interface<PPB_TCPSocket_1_2>()->GetLocalAddress(pp_resource())); |
| 103 } |
| 83 if (has_interface<PPB_TCPSocket_1_1>()) { | 104 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 84 return NetAddress( | 105 return NetAddress( |
| 85 PASS_REF, | 106 PASS_REF, |
| 86 get_interface<PPB_TCPSocket_1_1>()->GetLocalAddress(pp_resource())); | 107 get_interface<PPB_TCPSocket_1_1>()->GetLocalAddress(pp_resource())); |
| 87 } | 108 } |
| 88 if (has_interface<PPB_TCPSocket_1_0>()) { | 109 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 89 return NetAddress( | 110 return NetAddress( |
| 90 PASS_REF, | 111 PASS_REF, |
| 91 get_interface<PPB_TCPSocket_1_0>()->GetLocalAddress(pp_resource())); | 112 get_interface<PPB_TCPSocket_1_0>()->GetLocalAddress(pp_resource())); |
| 92 } | 113 } |
| 93 return NetAddress(); | 114 return NetAddress(); |
| 94 } | 115 } |
| 95 | 116 |
| 96 NetAddress TCPSocket::GetRemoteAddress() const { | 117 NetAddress TCPSocket::GetRemoteAddress() const { |
| 118 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 119 return NetAddress( |
| 120 PASS_REF, |
| 121 get_interface<PPB_TCPSocket_1_2>()->GetRemoteAddress(pp_resource())); |
| 122 } |
| 97 if (has_interface<PPB_TCPSocket_1_1>()) { | 123 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 98 return NetAddress( | 124 return NetAddress( |
| 99 PASS_REF, | 125 PASS_REF, |
| 100 get_interface<PPB_TCPSocket_1_1>()->GetRemoteAddress(pp_resource())); | 126 get_interface<PPB_TCPSocket_1_1>()->GetRemoteAddress(pp_resource())); |
| 101 } | 127 } |
| 102 if (has_interface<PPB_TCPSocket_1_0>()) { | 128 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 103 return NetAddress( | 129 return NetAddress( |
| 104 PASS_REF, | 130 PASS_REF, |
| 105 get_interface<PPB_TCPSocket_1_0>()->GetRemoteAddress(pp_resource())); | 131 get_interface<PPB_TCPSocket_1_0>()->GetRemoteAddress(pp_resource())); |
| 106 } | 132 } |
| 107 return NetAddress(); | 133 return NetAddress(); |
| 108 } | 134 } |
| 109 | 135 |
| 110 int32_t TCPSocket::Read(char* buffer, | 136 int32_t TCPSocket::Read(char* buffer, |
| 111 int32_t bytes_to_read, | 137 int32_t bytes_to_read, |
| 112 const CompletionCallback& callback) { | 138 const CompletionCallback& callback) { |
| 139 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 140 return get_interface<PPB_TCPSocket_1_2>()->Read( |
| 141 pp_resource(), buffer, bytes_to_read, |
| 142 callback.pp_completion_callback()); |
| 143 } |
| 113 if (has_interface<PPB_TCPSocket_1_1>()) { | 144 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 114 return get_interface<PPB_TCPSocket_1_1>()->Read( | 145 return get_interface<PPB_TCPSocket_1_1>()->Read( |
| 115 pp_resource(), buffer, bytes_to_read, | 146 pp_resource(), buffer, bytes_to_read, |
| 116 callback.pp_completion_callback()); | 147 callback.pp_completion_callback()); |
| 117 } | 148 } |
| 118 if (has_interface<PPB_TCPSocket_1_0>()) { | 149 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 119 return get_interface<PPB_TCPSocket_1_0>()->Read( | 150 return get_interface<PPB_TCPSocket_1_0>()->Read( |
| 120 pp_resource(), buffer, bytes_to_read, | 151 pp_resource(), buffer, bytes_to_read, |
| 121 callback.pp_completion_callback()); | 152 callback.pp_completion_callback()); |
| 122 } | 153 } |
| 123 return callback.MayForce(PP_ERROR_NOINTERFACE); | 154 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 124 } | 155 } |
| 125 | 156 |
| 126 int32_t TCPSocket::Write(const char* buffer, | 157 int32_t TCPSocket::Write(const char* buffer, |
| 127 int32_t bytes_to_write, | 158 int32_t bytes_to_write, |
| 128 const CompletionCallback& callback) { | 159 const CompletionCallback& callback) { |
| 160 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 161 return get_interface<PPB_TCPSocket_1_2>()->Write( |
| 162 pp_resource(), buffer, bytes_to_write, |
| 163 callback.pp_completion_callback()); |
| 164 } |
| 129 if (has_interface<PPB_TCPSocket_1_1>()) { | 165 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 130 return get_interface<PPB_TCPSocket_1_1>()->Write( | 166 return get_interface<PPB_TCPSocket_1_1>()->Write( |
| 131 pp_resource(), buffer, bytes_to_write, | 167 pp_resource(), buffer, bytes_to_write, |
| 132 callback.pp_completion_callback()); | 168 callback.pp_completion_callback()); |
| 133 } | 169 } |
| 134 if (has_interface<PPB_TCPSocket_1_0>()) { | 170 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 135 return get_interface<PPB_TCPSocket_1_0>()->Write( | 171 return get_interface<PPB_TCPSocket_1_0>()->Write( |
| 136 pp_resource(), buffer, bytes_to_write, | 172 pp_resource(), buffer, bytes_to_write, |
| 137 callback.pp_completion_callback()); | 173 callback.pp_completion_callback()); |
| 138 } | 174 } |
| 139 return callback.MayForce(PP_ERROR_NOINTERFACE); | 175 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 140 } | 176 } |
| 141 | 177 |
| 142 int32_t TCPSocket::Listen(int32_t backlog, | 178 int32_t TCPSocket::Listen(int32_t backlog, |
| 143 const CompletionCallback& callback) { | 179 const CompletionCallback& callback) { |
| 180 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 181 return get_interface<PPB_TCPSocket_1_2>()->Listen( |
| 182 pp_resource(), backlog, callback.pp_completion_callback()); |
| 183 } |
| 144 if (has_interface<PPB_TCPSocket_1_1>()) { | 184 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 145 return get_interface<PPB_TCPSocket_1_1>()->Listen( | 185 return get_interface<PPB_TCPSocket_1_1>()->Listen( |
| 146 pp_resource(), backlog, callback.pp_completion_callback()); | 186 pp_resource(), backlog, callback.pp_completion_callback()); |
| 147 } | 187 } |
| 148 return callback.MayForce(PP_ERROR_NOINTERFACE); | 188 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 149 } | 189 } |
| 150 | 190 |
| 151 int32_t TCPSocket::Accept( | 191 int32_t TCPSocket::Accept( |
| 152 const CompletionCallbackWithOutput<TCPSocket>& callback) { | 192 const CompletionCallbackWithOutput<TCPSocket>& callback) { |
| 193 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 194 return get_interface<PPB_TCPSocket_1_2>()->Accept( |
| 195 pp_resource(), callback.output(), callback.pp_completion_callback()); |
| 196 } |
| 153 if (has_interface<PPB_TCPSocket_1_1>()) { | 197 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 154 return get_interface<PPB_TCPSocket_1_1>()->Accept( | 198 return get_interface<PPB_TCPSocket_1_1>()->Accept( |
| 155 pp_resource(), callback.output(), callback.pp_completion_callback()); | 199 pp_resource(), callback.output(), callback.pp_completion_callback()); |
| 156 } | 200 } |
| 157 return callback.MayForce(PP_ERROR_NOINTERFACE); | 201 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 158 } | 202 } |
| 159 | 203 |
| 160 void TCPSocket::Close() { | 204 void TCPSocket::Close() { |
| 161 if (has_interface<PPB_TCPSocket_1_1>()) { | 205 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 206 get_interface<PPB_TCPSocket_1_2>()->Close(pp_resource()); |
| 207 } else if (has_interface<PPB_TCPSocket_1_1>()) { |
| 162 get_interface<PPB_TCPSocket_1_1>()->Close(pp_resource()); | 208 get_interface<PPB_TCPSocket_1_1>()->Close(pp_resource()); |
| 163 } else if (has_interface<PPB_TCPSocket_1_0>()) { | 209 } else if (has_interface<PPB_TCPSocket_1_0>()) { |
| 164 get_interface<PPB_TCPSocket_1_0>()->Close(pp_resource()); | 210 get_interface<PPB_TCPSocket_1_0>()->Close(pp_resource()); |
| 165 } | 211 } |
| 166 } | 212 } |
| 167 | 213 |
| 168 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name, | 214 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name, |
| 169 const Var& value, | 215 const Var& value, |
| 170 const CompletionCallback& callback) { | 216 const CompletionCallback& callback) { |
| 217 if (has_interface<PPB_TCPSocket_1_2>()) { |
| 218 return get_interface<PPB_TCPSocket_1_2>()->SetOption( |
| 219 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 220 } |
| 171 if (has_interface<PPB_TCPSocket_1_1>()) { | 221 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 172 return get_interface<PPB_TCPSocket_1_1>()->SetOption( | 222 return get_interface<PPB_TCPSocket_1_1>()->SetOption( |
| 173 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); | 223 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 174 } | 224 } |
| 175 if (has_interface<PPB_TCPSocket_1_0>()) { | 225 if (has_interface<PPB_TCPSocket_1_0>()) { |
| 176 return get_interface<PPB_TCPSocket_1_0>()->SetOption( | 226 return get_interface<PPB_TCPSocket_1_0>()->SetOption( |
| 177 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); | 227 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 178 } | 228 } |
| 179 return callback.MayForce(PP_ERROR_NOINTERFACE); | 229 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 180 } | 230 } |
| 181 | 231 |
| 182 } // namespace pp | 232 } // namespace pp |
| OLD | NEW |