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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// Copyright 2014 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 "content/browser/devtools/protocol/tethering_handler.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_http_handler_delegate.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/socket/server_socket.h"
#include "net/socket/stream_socket.h"
#include "net/socket/tcp_server_socket.h"
namespace content {
namespace devtools {
namespace tethering {
namespace {
const char kLocalhost[] = "127.0.0.1";
const int kListenBacklog = 5;
const int kBufferSize = 16 * 1024;
const int kMinTetheringPort = 1024;
const int kMaxTetheringPort = 32767;
using Response = DevToolsProtocolClient::Response;
class SocketPump {
public:
SocketPump(DevToolsHttpHandlerDelegate* delegate,
net::StreamSocket* client_socket)
: client_socket_(client_socket),
delegate_(delegate),
pending_writes_(0),
pending_destruction_(false) {
}
std::string Init() {
std::string channel_name;
server_socket_ = delegate_->CreateSocketForTethering(&channel_name);
if (!server_socket_.get() || channel_name.empty())
SelfDestruct();
int result = server_socket_->Accept(
&accepted_socket_,
base::Bind(&SocketPump::OnAccepted, base::Unretained(this)));
if (result != net::ERR_IO_PENDING)
OnAccepted(result);
return channel_name;
}
private:
void OnAccepted(int result) {
if (result < 0) {
SelfDestruct();
return;
}
++pending_writes_; // avoid SelfDestruct in first Pump
Pump(client_socket_.get(), accepted_socket_.get());
--pending_writes_;
if (pending_destruction_) {
SelfDestruct();
} else {
Pump(accepted_socket_.get(), client_socket_.get());
}
}
void Pump(net::StreamSocket* from, net::StreamSocket* to) {
scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kBufferSize);
int result = from->Read(
buffer.get(),
kBufferSize,
base::Bind(
&SocketPump::OnRead, base::Unretained(this), from, to, buffer));
if (result != net::ERR_IO_PENDING)
OnRead(from, to, buffer, result);
}
void OnRead(net::StreamSocket* from,
net::StreamSocket* to,
scoped_refptr<net::IOBuffer> buffer,
int result) {
if (result <= 0) {
SelfDestruct();
return;
}
int total = result;
scoped_refptr<net::DrainableIOBuffer> drainable =
new net::DrainableIOBuffer(buffer.get(), total);
++pending_writes_;
result = to->Write(drainable.get(),
total,
base::Bind(&SocketPump::OnWritten,
base::Unretained(this),
drainable,
from,
to));
if (result != net::ERR_IO_PENDING)
OnWritten(drainable, from, to, result);
}
void OnWritten(scoped_refptr<net::DrainableIOBuffer> drainable,
net::StreamSocket* from,
net::StreamSocket* to,
int result) {
--pending_writes_;
if (result < 0) {
SelfDestruct();
return;
}
drainable->DidConsume(result);
if (drainable->BytesRemaining() > 0) {
++pending_writes_;
result = to->Write(drainable.get(),
drainable->BytesRemaining(),
base::Bind(&SocketPump::OnWritten,
base::Unretained(this),
drainable,
from,
to));
if (result != net::ERR_IO_PENDING)
OnWritten(drainable, from, to, result);
return;
}
if (pending_destruction_) {
SelfDestruct();
return;
}
Pump(from, to);
}
void SelfDestruct() {
if (pending_writes_ > 0) {
pending_destruction_ = true;
return;
}
delete this;
}
private:
scoped_ptr<net::StreamSocket> client_socket_;
scoped_ptr<net::ServerSocket> server_socket_;
scoped_ptr<net::StreamSocket> accepted_socket_;
DevToolsHttpHandlerDelegate* delegate_;
int pending_writes_;
bool pending_destruction_;
};
class BoundSocket {
public:
typedef base::Callback<void(uint16, const std::string&)> AcceptedCallback;
BoundSocket(AcceptedCallback accepted_callback,
DevToolsHttpHandlerDelegate* delegate)
: accepted_callback_(accepted_callback),
delegate_(delegate),
socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())),
port_(0) {
}
virtual ~BoundSocket() {
}
bool Listen(uint16 port) {
port_ = port;
net::IPAddressNumber ip_number;
if (!net::ParseIPLiteralToNumber(kLocalhost, &ip_number))
return false;
net::IPEndPoint end_point(ip_number, port);
int result = socket_->Listen(end_point, kListenBacklog);
if (result < 0)
return false;
net::IPEndPoint local_address;
result = socket_->GetLocalAddress(&local_address);
if (result < 0)
return false;
DoAccept();
return true;
}
private:
typedef std::map<net::IPEndPoint, net::StreamSocket*> AcceptedSocketsMap;
void DoAccept() {
while (true) {
int result = socket_->Accept(
&accept_socket_,
base::Bind(&BoundSocket::OnAccepted, base::Unretained(this)));
if (result == net::ERR_IO_PENDING)
break;
else
HandleAcceptResult(result);
}
}
void OnAccepted(int result) {
HandleAcceptResult(result);
if (result == net::OK)
DoAccept();
}
void HandleAcceptResult(int result) {
if (result != net::OK)
return;
SocketPump* pump = new SocketPump(delegate_, accept_socket_.release());
std::string name = pump->Init();
if (!name.empty())
accepted_callback_.Run(port_, name);
}
AcceptedCallback accepted_callback_;
DevToolsHttpHandlerDelegate* delegate_;
scoped_ptr<net::ServerSocket> socket_;
scoped_ptr<net::StreamSocket> accept_socket_;
uint16 port_;
};
} // namespace
// TetheringHandler::TetheringImpl -------------------------------------------
class TetheringHandler::TetheringImpl {
public:
TetheringImpl(
base::WeakPtr<TetheringHandler> handler,
DevToolsHttpHandlerDelegate* delegate);
~TetheringImpl();
void Bind(DevToolsCommandId command_id, uint16 port);
void Unbind(DevToolsCommandId command_id, uint16 port);
void Accepted(uint16 port, const std::string& name);
private:
void SendInternalError(DevToolsCommandId command_id,
const std::string& message);
base::WeakPtr<TetheringHandler> handler_;
DevToolsHttpHandlerDelegate* delegate_;
typedef std::map<uint16, BoundSocket*> BoundSockets;
BoundSockets bound_sockets_;
};
TetheringHandler::TetheringImpl::TetheringImpl(
base::WeakPtr<TetheringHandler> handler,
DevToolsHttpHandlerDelegate* delegate)
: handler_(handler),
delegate_(delegate) {
}
TetheringHandler::TetheringImpl::~TetheringImpl() {
STLDeleteContainerPairSecondPointers(bound_sockets_.begin(),
bound_sockets_.end());
}
void TetheringHandler::TetheringImpl::Bind(
DevToolsCommandId command_id, uint16 port) {
if (bound_sockets_.find(port) != bound_sockets_.end()) {
SendInternalError(command_id, "Port already bound");
return;
}
BoundSocket::AcceptedCallback callback = base::Bind(
&TetheringHandler::TetheringImpl::Accepted, base::Unretained(this));
scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_));
if (!bound_socket->Listen(port)) {
SendInternalError(command_id, "Could not bind port");
return;
}
bound_sockets_[port] = bound_socket.release();
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&TetheringHandler::SendBindSuccess, handler_, command_id));
}
void TetheringHandler::TetheringImpl::Unbind(
DevToolsCommandId command_id, uint16 port) {
BoundSockets::iterator it = bound_sockets_.find(port);
if (it == bound_sockets_.end()) {
SendInternalError(command_id, "Port is not bound");
return;
}
delete it->second;
bound_sockets_.erase(it);
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command_id));
}
void TetheringHandler::TetheringImpl::Accepted(
uint16 port, const std::string& name) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&TetheringHandler::Accepted, handler_, port, name));
}
void TetheringHandler::TetheringImpl::SendInternalError(
DevToolsCommandId command_id,
const std::string& message) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&TetheringHandler::SendInternalError, handler_,
command_id, message));
}
// TetheringHandler ----------------------------------------------------------
// static
TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr;
TetheringHandler::TetheringHandler(
DevToolsHttpHandlerDelegate* delegate,
scoped_refptr<base::MessageLoopProxy> message_loop_proxy)
: delegate_(delegate),
message_loop_proxy_(message_loop_proxy),
is_active_(false),
weak_factory_(this) {
}
TetheringHandler::~TetheringHandler() {
if (is_active_) {
message_loop_proxy_->DeleteSoon(FROM_HERE, impl_);
impl_ = nullptr;
}
}
void TetheringHandler::SetClient(scoped_ptr<Client> client) {
client_.swap(client);
}
void TetheringHandler::Accepted(uint16 port, const std::string& name) {
client_->Accepted(AcceptedParams::Create()->set_port(port)
->set_connection_id(name));
}
bool TetheringHandler::Activate() {
if (is_active_)
return true;
if (impl_)
return false;
is_active_ = true;
impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_);
return true;
}
Response TetheringHandler::Bind(DevToolsCommandId command_id, int port) {
if (port < kMinTetheringPort || port > kMaxTetheringPort)
return Response::InvalidParams("port");
if (!Activate())
return Response::ServerError("Tethering is used by another connection");
DCHECK(impl_);
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&TetheringImpl::Bind, base::Unretained(impl_),
command_id, port));
return Response::OK();
}
Response TetheringHandler::Unbind(DevToolsCommandId command_id, int port) {
if (!Activate())
return Response::ServerError("Tethering is used by another connection");
DCHECK(impl_);
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_),
command_id, port));
return Response::OK();
}
void TetheringHandler::SendBindSuccess(DevToolsCommandId command_id) {
client_->SendBindResponse(command_id, BindResponse::Create());
}
void TetheringHandler::SendUnbindSuccess(DevToolsCommandId command_id) {
client_->SendUnbindResponse(command_id, UnbindResponse::Create());
}
void TetheringHandler::SendInternalError(DevToolsCommandId command_id,
const std::string& message) {
client_->SendError(command_id, Response::InternalError(message));
}
} // namespace tethering
} // namespace devtools
} // namespace content
|