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>() { |
| 21 return PPB_TCPSOCKET_INTERFACE_1_1; |
| 22 } |
| 23 |
20 } // namespace | 24 } // namespace |
21 | 25 |
22 TCPSocket::TCPSocket() { | 26 TCPSocket::TCPSocket() { |
23 } | 27 } |
24 | 28 |
25 TCPSocket::TCPSocket(const InstanceHandle& instance) { | 29 TCPSocket::TCPSocket(const InstanceHandle& instance, |
26 if (has_interface<PPB_TCPSocket_1_0>()) { | 30 PP_NetAddress_Family family) { |
| 31 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 32 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_1>()->Create( |
| 33 instance.pp_instance(), family)); |
| 34 } else if (has_interface<PPB_TCPSocket_1_0>()) { |
27 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_0>()->Create( | 35 PassRefFromConstructor(get_interface<PPB_TCPSocket_1_0>()->Create( |
28 instance.pp_instance())); | 36 instance.pp_instance())); |
29 } | 37 } |
30 } | 38 } |
31 | 39 |
32 TCPSocket::TCPSocket(PassRef, PP_Resource resource) | 40 TCPSocket::TCPSocket(PassRef, PP_Resource resource) |
33 : Resource(PASS_REF, resource) { | 41 : Resource(PASS_REF, resource) { |
34 } | 42 } |
35 | 43 |
36 TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) { | 44 TCPSocket::TCPSocket(const TCPSocket& other) : Resource(other) { |
37 } | 45 } |
38 | 46 |
39 TCPSocket::~TCPSocket() { | 47 TCPSocket::~TCPSocket() { |
40 } | 48 } |
41 | 49 |
42 TCPSocket& TCPSocket::operator=(const TCPSocket& other) { | 50 TCPSocket& TCPSocket::operator=(const TCPSocket& other) { |
43 Resource::operator=(other); | 51 Resource::operator=(other); |
44 return *this; | 52 return *this; |
45 } | 53 } |
46 | 54 |
47 // static | 55 // static |
48 bool TCPSocket::IsAvailable() { | 56 bool TCPSocket::IsAvailable() { |
49 return has_interface<PPB_TCPSocket_1_0>(); | 57 return has_interface<PPB_TCPSocket_1_1>() || |
| 58 has_interface<PPB_TCPSocket_1_0>(); |
| 59 } |
| 60 |
| 61 int32_t TCPSocket::Bind(const NetAddress& addr, |
| 62 const CompletionCallback& callback) { |
| 63 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 64 return get_interface<PPB_TCPSocket_1_1>()->Bind( |
| 65 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 66 } |
| 67 return callback.MayForce(PP_ERROR_NOINTERFACE); |
50 } | 68 } |
51 | 69 |
52 int32_t TCPSocket::Connect(const NetAddress& addr, | 70 int32_t TCPSocket::Connect(const NetAddress& addr, |
53 const CompletionCallback& callback) { | 71 const CompletionCallback& callback) { |
| 72 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 73 return get_interface<PPB_TCPSocket_1_1>()->Connect( |
| 74 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
| 75 } |
54 if (has_interface<PPB_TCPSocket_1_0>()) { | 76 if (has_interface<PPB_TCPSocket_1_0>()) { |
55 return get_interface<PPB_TCPSocket_1_0>()->Connect( | 77 return get_interface<PPB_TCPSocket_1_0>()->Connect( |
56 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); | 78 pp_resource(), addr.pp_resource(), callback.pp_completion_callback()); |
57 } | 79 } |
58 return callback.MayForce(PP_ERROR_NOINTERFACE); | 80 return callback.MayForce(PP_ERROR_NOINTERFACE); |
59 } | 81 } |
60 | 82 |
61 NetAddress TCPSocket::GetLocalAddress() const { | 83 NetAddress TCPSocket::GetLocalAddress() const { |
| 84 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 85 return NetAddress( |
| 86 PASS_REF, |
| 87 get_interface<PPB_TCPSocket_1_1>()->GetLocalAddress(pp_resource())); |
| 88 } |
62 if (has_interface<PPB_TCPSocket_1_0>()) { | 89 if (has_interface<PPB_TCPSocket_1_0>()) { |
63 return NetAddress( | 90 return NetAddress( |
64 PASS_REF, | 91 PASS_REF, |
65 get_interface<PPB_TCPSocket_1_0>()->GetLocalAddress(pp_resource())); | 92 get_interface<PPB_TCPSocket_1_0>()->GetLocalAddress(pp_resource())); |
66 } | 93 } |
67 return NetAddress(); | 94 return NetAddress(); |
68 } | 95 } |
69 | 96 |
70 NetAddress TCPSocket::GetRemoteAddress() const { | 97 NetAddress TCPSocket::GetRemoteAddress() const { |
| 98 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 99 return NetAddress( |
| 100 PASS_REF, |
| 101 get_interface<PPB_TCPSocket_1_1>()->GetRemoteAddress(pp_resource())); |
| 102 } |
71 if (has_interface<PPB_TCPSocket_1_0>()) { | 103 if (has_interface<PPB_TCPSocket_1_0>()) { |
72 return NetAddress( | 104 return NetAddress( |
73 PASS_REF, | 105 PASS_REF, |
74 get_interface<PPB_TCPSocket_1_0>()->GetRemoteAddress(pp_resource())); | 106 get_interface<PPB_TCPSocket_1_0>()->GetRemoteAddress(pp_resource())); |
75 } | 107 } |
76 return NetAddress(); | 108 return NetAddress(); |
77 } | 109 } |
78 | 110 |
79 int32_t TCPSocket::Read(char* buffer, | 111 int32_t TCPSocket::Read(char* buffer, |
80 int32_t bytes_to_read, | 112 int32_t bytes_to_read, |
81 const CompletionCallback& callback) { | 113 const CompletionCallback& callback) { |
| 114 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 115 return get_interface<PPB_TCPSocket_1_1>()->Read( |
| 116 pp_resource(), buffer, bytes_to_read, |
| 117 callback.pp_completion_callback()); |
| 118 } |
82 if (has_interface<PPB_TCPSocket_1_0>()) { | 119 if (has_interface<PPB_TCPSocket_1_0>()) { |
83 return get_interface<PPB_TCPSocket_1_0>()->Read( | 120 return get_interface<PPB_TCPSocket_1_0>()->Read( |
84 pp_resource(), buffer, bytes_to_read, | 121 pp_resource(), buffer, bytes_to_read, |
85 callback.pp_completion_callback()); | 122 callback.pp_completion_callback()); |
86 } | 123 } |
87 return callback.MayForce(PP_ERROR_NOINTERFACE); | 124 return callback.MayForce(PP_ERROR_NOINTERFACE); |
88 } | 125 } |
89 | 126 |
90 int32_t TCPSocket::Write(const char* buffer, | 127 int32_t TCPSocket::Write(const char* buffer, |
91 int32_t bytes_to_write, | 128 int32_t bytes_to_write, |
92 const CompletionCallback& callback) { | 129 const CompletionCallback& callback) { |
| 130 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 131 return get_interface<PPB_TCPSocket_1_1>()->Write( |
| 132 pp_resource(), buffer, bytes_to_write, |
| 133 callback.pp_completion_callback()); |
| 134 } |
93 if (has_interface<PPB_TCPSocket_1_0>()) { | 135 if (has_interface<PPB_TCPSocket_1_0>()) { |
94 return get_interface<PPB_TCPSocket_1_0>()->Write( | 136 return get_interface<PPB_TCPSocket_1_0>()->Write( |
95 pp_resource(), buffer, bytes_to_write, | 137 pp_resource(), buffer, bytes_to_write, |
96 callback.pp_completion_callback()); | 138 callback.pp_completion_callback()); |
97 } | 139 } |
98 return callback.MayForce(PP_ERROR_NOINTERFACE); | 140 return callback.MayForce(PP_ERROR_NOINTERFACE); |
99 } | 141 } |
100 | 142 |
| 143 int32_t TCPSocket::Listen(int32_t backlog, |
| 144 const CompletionCallback& callback) { |
| 145 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 146 return get_interface<PPB_TCPSocket_1_1>()->Listen( |
| 147 pp_resource(), backlog, callback.pp_completion_callback()); |
| 148 } |
| 149 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 150 } |
| 151 |
| 152 int32_t TCPSocket::Accept( |
| 153 const CompletionCallbackWithOutput<TCPSocket>& callback) { |
| 154 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 155 return get_interface<PPB_TCPSocket_1_1>()->Accept( |
| 156 pp_resource(), callback.output(), callback.pp_completion_callback()); |
| 157 } |
| 158 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 159 } |
| 160 |
101 void TCPSocket::Close() { | 161 void TCPSocket::Close() { |
102 if (has_interface<PPB_TCPSocket_1_0>()) | 162 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 163 get_interface<PPB_TCPSocket_1_1>()->Close(pp_resource()); |
| 164 } else if (has_interface<PPB_TCPSocket_1_0>()) { |
103 get_interface<PPB_TCPSocket_1_0>()->Close(pp_resource()); | 165 get_interface<PPB_TCPSocket_1_0>()->Close(pp_resource()); |
| 166 } |
104 } | 167 } |
105 | 168 |
106 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name, | 169 int32_t TCPSocket::SetOption(PP_TCPSocket_Option name, |
107 const Var& value, | 170 const Var& value, |
108 const CompletionCallback& callback) { | 171 const CompletionCallback& callback) { |
| 172 if (has_interface<PPB_TCPSocket_1_1>()) { |
| 173 return get_interface<PPB_TCPSocket_1_1>()->SetOption( |
| 174 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
| 175 } |
109 if (has_interface<PPB_TCPSocket_1_0>()) { | 176 if (has_interface<PPB_TCPSocket_1_0>()) { |
110 return get_interface<PPB_TCPSocket_1_0>()->SetOption( | 177 return get_interface<PPB_TCPSocket_1_0>()->SetOption( |
111 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); | 178 pp_resource(), name, value.pp_var(), callback.pp_completion_callback()); |
112 } | 179 } |
113 return callback.MayForce(PP_ERROR_NOINTERFACE); | 180 return callback.MayForce(PP_ERROR_NOINTERFACE); |
114 } | 181 } |
115 | 182 |
116 } // namespace pp | 183 } // namespace pp |
OLD | NEW |