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