1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/socket/tcp_socket.h"
#include "chrome/browser/extensions/api/api_resource.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
#include "net/base/address_list.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/rand_callback.h"
#include "net/socket/tcp_client_socket.h"
namespace extensions {
TCPSocket::TCPSocket(APIResourceEventNotifier* event_notifier)
: Socket(event_notifier) {
}
// For testing.
TCPSocket::TCPSocket(net::TCPClientSocket* tcp_client_socket,
APIResourceEventNotifier* event_notifier)
: Socket(event_notifier),
socket_(tcp_client_socket) {
}
// static
TCPSocket* TCPSocket::CreateSocketForTesting(
net::TCPClientSocket* tcp_client_socket,
APIResourceEventNotifier* event_notifier) {
return new TCPSocket(tcp_client_socket, event_notifier);
}
TCPSocket::~TCPSocket() {
if (is_connected_) {
Disconnect();
}
}
void TCPSocket::Connect(const std::string& address,
int port,
const CompletionCallback& callback) {
DCHECK(!callback.is_null());
if (!connect_callback_.is_null()) {
callback.Run(net::ERR_CONNECTION_FAILED);
return;
}
connect_callback_ = callback;
int result = net::ERR_CONNECTION_FAILED;
do {
if (is_connected_)
break;
net::AddressList address_list;
if (!StringAndPortToAddressList(address, port, &address_list)) {
result = net::ERR_ADDRESS_INVALID;
break;
}
socket_.reset(new net::TCPClientSocket(address_list, NULL,
net::NetLog::Source()));
connect_callback_ = callback;
result = socket_->Connect(base::Bind(
&TCPSocket::OnConnectComplete, base::Unretained(this)));
} while (false);
if (result != net::ERR_IO_PENDING)
OnConnectComplete(result);
}
void TCPSocket::Disconnect() {
is_connected_ = false;
socket_->Disconnect();
}
int TCPSocket::Bind(const std::string& address, int port) {
// TODO(penghuang): Supports bind for tcp?
return net::ERR_FAILED;
}
void TCPSocket::Read(int count,
const ReadCompletionCallback& callback) {
DCHECK(!callback.is_null());
if (!read_callback_.is_null()) {
callback.Run(net::ERR_IO_PENDING, NULL);
return;
} else {
read_callback_ = callback;
}
int result = net::ERR_FAILED;
scoped_refptr<net::IOBuffer> io_buffer;
do {
if (count < 0) {
result = net::ERR_INVALID_ARGUMENT;
break;
}
if (!socket_.get() || !socket_->IsConnected()) {
result = net::ERR_SOCKET_NOT_CONNECTED;
break;
}
io_buffer = new net::IOBuffer(count);
result = socket_->Read(io_buffer.get(), count,
base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this),
io_buffer));
} while (false);
if (result != net::ERR_IO_PENDING)
OnReadComplete(io_buffer, result);
}
void TCPSocket::RecvFrom(int count,
const RecvFromCompletionCallback& callback) {
callback.Run(net::ERR_FAILED, NULL, NULL, 0);
}
void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
int byte_count,
const std::string& address,
int port,
const CompletionCallback& callback) {
callback.Run(net::ERR_FAILED);
}
bool TCPSocket::SetKeepAlive(bool enable, int delay) {
if (!socket_.get())
return false;
return socket_->SetKeepAlive(enable, delay);
}
bool TCPSocket::SetNoDelay(bool no_delay) {
if (!socket_.get())
return false;
return socket_->SetNoDelay(no_delay);
}
int TCPSocket::WriteImpl(net::IOBuffer* io_buffer,
int io_buffer_size,
const net::CompletionCallback& callback) {
if (!socket_.get() || !socket_->IsConnected())
return net::ERR_SOCKET_NOT_CONNECTED;
else
return socket_->Write(io_buffer, io_buffer_size, callback);
}
void TCPSocket::OnConnectComplete(int result) {
DCHECK(!connect_callback_.is_null());
DCHECK(!is_connected_);
is_connected_ = result == net::OK;
connect_callback_.Run(result);
connect_callback_.Reset();
}
void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer,
int result) {
DCHECK(!read_callback_.is_null());
read_callback_.Run(result, io_buffer);
read_callback_.Reset();
}
} // namespace extensions
|