OLD | NEW |
1 // Copyright (c) 2012 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 "content/browser/devtools/tethering_handler.h" | 5 #include "content/browser/devtools/protocol/tethering_handler.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/stl_util.h" | |
10 #include "base/values.h" | |
11 #include "content/browser/devtools/devtools_http_handler_impl.h" | |
12 #include "content/browser/devtools/devtools_protocol_constants.h" | |
13 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/devtools_http_handler_delegate.h" | 8 #include "content/public/browser/devtools_http_handler_delegate.h" |
15 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
16 #include "net/base/ip_endpoint.h" | |
17 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
18 #include "net/base/net_log.h" | |
19 #include "net/socket/stream_listen_socket.h" | 11 #include "net/socket/stream_listen_socket.h" |
20 #include "net/socket/stream_socket.h" | 12 #include "net/socket/stream_socket.h" |
21 #include "net/socket/tcp_server_socket.h" | 13 #include "net/socket/tcp_server_socket.h" |
22 | 14 |
23 namespace content { | 15 namespace content { |
| 16 namespace devtools { |
| 17 namespace tethering { |
24 | 18 |
25 namespace { | 19 namespace { |
26 | 20 |
27 const char kLocalhost[] = "127.0.0.1"; | 21 const char kLocalhost[] = "127.0.0.1"; |
28 | 22 |
29 const int kListenBacklog = 5; | 23 const int kListenBacklog = 5; |
30 const int kBufferSize = 16 * 1024; | 24 const int kBufferSize = 16 * 1024; |
31 | 25 |
32 const int kMinTetheringPort = 1024; | 26 const int kMinTetheringPort = 1024; |
33 const int kMaxTetheringPort = 32767; | 27 const int kMaxTetheringPort = 32767; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 scoped_ptr<net::StreamSocket> client_socket_; | 147 scoped_ptr<net::StreamSocket> client_socket_; |
154 scoped_ptr<net::StreamListenSocket> server_socket_; | 148 scoped_ptr<net::StreamListenSocket> server_socket_; |
155 scoped_ptr<net::StreamListenSocket> accepted_socket_; | 149 scoped_ptr<net::StreamListenSocket> accepted_socket_; |
156 scoped_refptr<net::IOBuffer> buffer_; | 150 scoped_refptr<net::IOBuffer> buffer_; |
157 scoped_refptr<net::GrowableIOBuffer> wire_buffer_; | 151 scoped_refptr<net::GrowableIOBuffer> wire_buffer_; |
158 DevToolsHttpHandlerDelegate* delegate_; | 152 DevToolsHttpHandlerDelegate* delegate_; |
159 int wire_buffer_size_; | 153 int wire_buffer_size_; |
160 bool pending_destruction_; | 154 bool pending_destruction_; |
161 }; | 155 }; |
162 | 156 |
163 static int GetPort(scoped_refptr<DevToolsProtocol::Command> command, | |
164 const std::string& paramName) { | |
165 base::DictionaryValue* params = command->params(); | |
166 int port = 0; | |
167 if (!params || | |
168 !params->GetInteger(paramName, &port) || | |
169 port < kMinTetheringPort || port > kMaxTetheringPort) | |
170 return 0; | |
171 return port; | |
172 } | |
173 | |
174 class BoundSocket { | 157 class BoundSocket { |
175 public: | 158 public: |
176 typedef base::Callback<void(int, const std::string&)> AcceptedCallback; | 159 typedef base::Callback<void(int, const std::string&)> AcceptedCallback; |
177 | 160 |
178 BoundSocket(AcceptedCallback accepted_callback, | 161 BoundSocket(AcceptedCallback accepted_callback, |
179 DevToolsHttpHandlerDelegate* delegate) | 162 DevToolsHttpHandlerDelegate* delegate) |
180 : accepted_callback_(accepted_callback), | 163 : accepted_callback_(accepted_callback), |
181 delegate_(delegate), | 164 delegate_(delegate), |
182 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 165 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), |
183 port_(0) { | 166 port_(0) { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 // static | 328 // static |
346 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; | 329 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; |
347 | 330 |
348 TetheringHandler::TetheringHandler( | 331 TetheringHandler::TetheringHandler( |
349 DevToolsHttpHandlerDelegate* delegate, | 332 DevToolsHttpHandlerDelegate* delegate, |
350 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) | 333 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) |
351 : delegate_(delegate), | 334 : delegate_(delegate), |
352 message_loop_proxy_(message_loop_proxy), | 335 message_loop_proxy_(message_loop_proxy), |
353 is_active_(false), | 336 is_active_(false), |
354 weak_factory_(this) { | 337 weak_factory_(this) { |
355 RegisterCommandHandler(devtools::Tethering::bind::kName, | |
356 base::Bind(&TetheringHandler::OnBind, | |
357 base::Unretained(this))); | |
358 RegisterCommandHandler(devtools::Tethering::unbind::kName, | |
359 base::Bind(&TetheringHandler::OnUnbind, | |
360 base::Unretained(this))); | |
361 } | 338 } |
362 | 339 |
363 TetheringHandler::~TetheringHandler() { | 340 TetheringHandler::~TetheringHandler() { |
364 if (is_active_) { | 341 if (is_active_) { |
365 message_loop_proxy_->DeleteSoon(FROM_HERE, impl_); | 342 message_loop_proxy_->DeleteSoon(FROM_HERE, impl_); |
366 impl_ = nullptr; | 343 impl_ = nullptr; |
367 } | 344 } |
368 } | 345 } |
369 | 346 |
| 347 void TetheringHandler::SetClient(scoped_ptr<Client> client) { |
| 348 client_.swap(client); |
| 349 } |
| 350 |
370 void TetheringHandler::Accepted(int port, const std::string& name) { | 351 void TetheringHandler::Accepted(int port, const std::string& name) { |
371 base::DictionaryValue* params = new base::DictionaryValue(); | 352 client_->Accepted(AcceptedParams::Create()->set_port(port) |
372 params->SetInteger(devtools::Tethering::accepted::kParamPort, port); | 353 ->set_connection_id(name)); |
373 params->SetString(devtools::Tethering::accepted::kParamConnectionId, name); | |
374 SendNotification(devtools::Tethering::accepted::kName, params); | |
375 } | 354 } |
376 | 355 |
377 bool TetheringHandler::Activate() { | 356 bool TetheringHandler::Activate() { |
378 if (is_active_) | 357 if (is_active_) |
379 return true; | 358 return true; |
380 if (impl_) | 359 if (impl_) |
381 return false; | 360 return false; |
382 is_active_ = true; | 361 is_active_ = true; |
383 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); | 362 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); |
384 return true; | 363 return true; |
385 } | 364 } |
386 | 365 |
387 scoped_refptr<DevToolsProtocol::Response> | 366 scoped_refptr<DevToolsProtocol::Response> TetheringHandler::Bind( |
388 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { | 367 int port, scoped_refptr<DevToolsProtocol::Command> command) { |
389 const std::string& portParamName = devtools::Tethering::bind::kParamPort; | 368 if (port < kMinTetheringPort || port > kMaxTetheringPort) |
390 int port = GetPort(command, portParamName); | 369 return command->InvalidParamResponse("port"); |
391 if (port == 0) | |
392 return command->InvalidParamResponse(portParamName); | |
393 | 370 |
394 if (!Activate()) { | 371 if (!Activate()) { |
395 return command->ServerErrorResponse( | 372 return command->ServerErrorResponse( |
396 "Tethering is used by another connection"); | 373 "Tethering is used by another connection"); |
397 } | 374 } |
398 DCHECK(impl_); | 375 DCHECK(impl_); |
399 message_loop_proxy_->PostTask( | 376 message_loop_proxy_->PostTask( |
400 FROM_HERE, | 377 FROM_HERE, |
401 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), | 378 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), |
402 command, port)); | 379 command, port)); |
403 return command->AsyncResponsePromise(); | 380 return command->AsyncResponsePromise(); |
404 } | 381 } |
405 | 382 |
406 scoped_refptr<DevToolsProtocol::Response> | 383 scoped_refptr<DevToolsProtocol::Response> TetheringHandler::Unbind( |
407 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { | 384 int port, scoped_refptr<DevToolsProtocol::Command> command) { |
408 const std::string& portParamName = devtools::Tethering::unbind::kParamPort; | |
409 int port = GetPort(command, portParamName); | |
410 if (port == 0) | |
411 return command->InvalidParamResponse(portParamName); | |
412 | |
413 if (!Activate()) { | 385 if (!Activate()) { |
414 return command->ServerErrorResponse( | 386 return command->ServerErrorResponse( |
415 "Tethering is used by another connection"); | 387 "Tethering is used by another connection"); |
416 } | 388 } |
417 DCHECK(impl_); | 389 DCHECK(impl_); |
418 message_loop_proxy_->PostTask( | 390 message_loop_proxy_->PostTask( |
419 FROM_HERE, | 391 FROM_HERE, |
420 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), | 392 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), |
421 command, port)); | 393 command, port)); |
422 return command->AsyncResponsePromise(); | 394 return command->AsyncResponsePromise(); |
423 } | 395 } |
424 | 396 |
425 void TetheringHandler::SendBindSuccess( | 397 void TetheringHandler::SendBindSuccess( |
426 scoped_refptr<DevToolsProtocol::Command> command) { | 398 scoped_refptr<DevToolsProtocol::Command> command) { |
427 SendAsyncResponse(command->SuccessResponse(nullptr)); | 399 client_->SendBindResponse(command, BindResponse::Create()); |
428 } | 400 } |
429 | 401 |
430 void TetheringHandler::SendUnbindSuccess( | 402 void TetheringHandler::SendUnbindSuccess( |
431 scoped_refptr<DevToolsProtocol::Command> command) { | 403 scoped_refptr<DevToolsProtocol::Command> command) { |
432 SendAsyncResponse(command->SuccessResponse(nullptr)); | 404 client_->SendUnbindResponse(command, UnbindResponse::Create()); |
433 } | 405 } |
434 | 406 |
435 void TetheringHandler::SendInternalError( | 407 void TetheringHandler::SendInternalError( |
436 scoped_refptr<DevToolsProtocol::Command> command, | 408 scoped_refptr<DevToolsProtocol::Command> command, |
437 const std::string& message) { | 409 const std::string& message) { |
438 SendAsyncResponse(command->InternalErrorResponse(message)); | 410 client_->SendInternalErrorResponse(command, message); |
439 } | 411 } |
440 | 412 |
| 413 } // namespace tethering |
| 414 } // namespace devtools |
441 } // namespace content | 415 } // namespace content |
OLD | NEW |