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