OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/socket/tcp_socket.h" | 5 #include "extensions/browser/api/socket/tcp_socket.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "extensions/browser/api/api_resource.h" | 12 #include "extensions/browser/api/api_resource.h" |
13 #include "net/base/address_list.h" | 13 #include "net/base/address_list.h" |
14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/base/rand_callback.h" | 16 #include "net/base/rand_callback.h" |
| 17 #include "net/log/net_log_source.h" |
17 #include "net/socket/tcp_client_socket.h" | 18 #include "net/socket/tcp_client_socket.h" |
18 | 19 |
19 namespace extensions { | 20 namespace extensions { |
20 | 21 |
21 const char kTCPSocketTypeInvalidError[] = | 22 const char kTCPSocketTypeInvalidError[] = |
22 "Cannot call both connect and listen on the same socket."; | 23 "Cannot call both connect and listen on the same socket."; |
23 const char kSocketListenError[] = "Could not listen on the specified port."; | 24 const char kSocketListenError[] = "Could not listen on the specified port."; |
24 | 25 |
25 static base::LazyInstance< | 26 static base::LazyInstance< |
26 BrowserContextKeyedAPIFactory<ApiResourceManager<ResumableTCPSocket> > > | 27 BrowserContextKeyedAPIFactory<ApiResourceManager<ResumableTCPSocket> > > |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 return; | 95 return; |
95 } | 96 } |
96 | 97 |
97 DCHECK(!server_socket_.get()); | 98 DCHECK(!server_socket_.get()); |
98 socket_mode_ = CLIENT; | 99 socket_mode_ = CLIENT; |
99 connect_callback_ = callback; | 100 connect_callback_ = callback; |
100 | 101 |
101 int result = net::ERR_CONNECTION_FAILED; | 102 int result = net::ERR_CONNECTION_FAILED; |
102 if (!is_connected_) { | 103 if (!is_connected_) { |
103 socket_.reset( | 104 socket_.reset( |
104 new net::TCPClientSocket(address, NULL, NULL, net::NetLog::Source())); | 105 new net::TCPClientSocket(address, NULL, NULL, net::NetLogSource())); |
105 result = socket_->Connect( | 106 result = socket_->Connect( |
106 base::Bind(&TCPSocket::OnConnectComplete, base::Unretained(this))); | 107 base::Bind(&TCPSocket::OnConnectComplete, base::Unretained(this))); |
107 } | 108 } |
108 | 109 |
109 if (result != net::ERR_IO_PENDING) | 110 if (result != net::ERR_IO_PENDING) |
110 OnConnectComplete(result); | 111 OnConnectComplete(result); |
111 } | 112 } |
112 | 113 |
113 void TCPSocket::Disconnect() { | 114 void TCPSocket::Disconnect() { |
114 is_connected_ = false; | 115 is_connected_ = false; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 int backlog, | 196 int backlog, |
196 std::string* error_msg) { | 197 std::string* error_msg) { |
197 if (socket_mode_ == CLIENT) { | 198 if (socket_mode_ == CLIENT) { |
198 *error_msg = kTCPSocketTypeInvalidError; | 199 *error_msg = kTCPSocketTypeInvalidError; |
199 return net::ERR_NOT_IMPLEMENTED; | 200 return net::ERR_NOT_IMPLEMENTED; |
200 } | 201 } |
201 DCHECK(!socket_.get()); | 202 DCHECK(!socket_.get()); |
202 socket_mode_ = SERVER; | 203 socket_mode_ = SERVER; |
203 | 204 |
204 if (!server_socket_.get()) { | 205 if (!server_socket_.get()) { |
205 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source())); | 206 server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLogSource())); |
206 } | 207 } |
207 | 208 |
208 int result = server_socket_->ListenWithAddressAndPort(address, port, backlog); | 209 int result = server_socket_->ListenWithAddressAndPort(address, port, backlog); |
209 if (result) { | 210 if (result) { |
210 server_socket_.reset(); | 211 server_socket_.reset(); |
211 *error_msg = kSocketListenError; | 212 *error_msg = kSocketListenError; |
212 } | 213 } |
213 return result; | 214 return result; |
214 } | 215 } |
215 | 216 |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 369 |
369 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } | 370 bool ResumableTCPSocket::IsPersistent() const { return persistent(); } |
370 | 371 |
371 ResumableTCPServerSocket::ResumableTCPServerSocket( | 372 ResumableTCPServerSocket::ResumableTCPServerSocket( |
372 const std::string& owner_extension_id) | 373 const std::string& owner_extension_id) |
373 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} | 374 : TCPSocket(owner_extension_id), persistent_(false), paused_(false) {} |
374 | 375 |
375 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } | 376 bool ResumableTCPServerSocket::IsPersistent() const { return persistent(); } |
376 | 377 |
377 } // namespace extensions | 378 } // namespace extensions |
OLD | NEW |