| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/tethering_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "content/browser/devtools/devtools_http_handler_impl.h" | 11 #include "content/browser/devtools/devtools_http_handler_impl.h" |
| 12 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 12 #include "content/public/browser/devtools_http_handler_delegate.h" | 13 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
| 17 #include "net/socket/stream_listen_socket.h" | 18 #include "net/socket/stream_listen_socket.h" |
| 18 #include "net/socket/stream_socket.h" | 19 #include "net/socket/stream_socket.h" |
| 19 #include "net/socket/tcp_server_socket.h" | 20 #include "net/socket/tcp_server_socket.h" |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const char kTetheringBind[] = "Tethering.bind"; | |
| 26 const char kTetheringUnbind[] = "Tethering.unbind"; | |
| 27 | |
| 28 const char kTetheringAccepted[] = "Tethering.accepted"; | |
| 29 | |
| 30 const char kPortParam[] = "port"; | |
| 31 const char kConnectionIdParam[] = "connectionId"; | |
| 32 | |
| 33 const char kLocalhost[] = "127.0.0.1"; | 26 const char kLocalhost[] = "127.0.0.1"; |
| 34 | 27 |
| 35 const int kListenBacklog = 5; | 28 const int kListenBacklog = 5; |
| 36 const int kBufferSize = 16 * 1024; | 29 const int kBufferSize = 16 * 1024; |
| 37 | 30 |
| 38 const int kMinTetheringPort = 1024; | 31 const int kMinTetheringPort = 1024; |
| 39 const int kMaxTetheringPort = 32767; | 32 const int kMaxTetheringPort = 32767; |
| 40 | 33 |
| 41 class SocketPump : public net::StreamListenSocket::Delegate { | 34 class SocketPump : public net::StreamListenSocket::Delegate { |
| 42 public: | 35 public: |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 scoped_ptr<net::StreamListenSocket> accepted_socket_; | 156 scoped_ptr<net::StreamListenSocket> accepted_socket_; |
| 164 scoped_refptr<net::IOBuffer> buffer_; | 157 scoped_refptr<net::IOBuffer> buffer_; |
| 165 scoped_refptr<net::GrowableIOBuffer> wire_buffer_; | 158 scoped_refptr<net::GrowableIOBuffer> wire_buffer_; |
| 166 DevToolsHttpHandlerDelegate* delegate_; | 159 DevToolsHttpHandlerDelegate* delegate_; |
| 167 int wire_buffer_size_; | 160 int wire_buffer_size_; |
| 168 bool pending_destruction_; | 161 bool pending_destruction_; |
| 169 }; | 162 }; |
| 170 | 163 |
| 171 } // namespace | 164 } // namespace |
| 172 | 165 |
| 173 const char TetheringHandler::kDomain[] = "Tethering"; | |
| 174 | |
| 175 class TetheringHandler::BoundSocket { | 166 class TetheringHandler::BoundSocket { |
| 176 public: | 167 public: |
| 177 BoundSocket(TetheringHandler* handler, | 168 BoundSocket(TetheringHandler* handler, |
| 178 DevToolsHttpHandlerDelegate* delegate) | 169 DevToolsHttpHandlerDelegate* delegate) |
| 179 : handler_(handler), | 170 : handler_(handler), |
| 180 delegate_(delegate), | 171 delegate_(delegate), |
| 181 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 172 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), |
| 182 port_(0) { | 173 port_(0) { |
| 183 } | 174 } |
| 184 | 175 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 | 229 |
| 239 TetheringHandler* handler_; | 230 TetheringHandler* handler_; |
| 240 DevToolsHttpHandlerDelegate* delegate_; | 231 DevToolsHttpHandlerDelegate* delegate_; |
| 241 scoped_ptr<net::ServerSocket> socket_; | 232 scoped_ptr<net::ServerSocket> socket_; |
| 242 scoped_ptr<net::StreamSocket> accept_socket_; | 233 scoped_ptr<net::StreamSocket> accept_socket_; |
| 243 int port_; | 234 int port_; |
| 244 }; | 235 }; |
| 245 | 236 |
| 246 TetheringHandler::TetheringHandler(DevToolsHttpHandlerDelegate* delegate) | 237 TetheringHandler::TetheringHandler(DevToolsHttpHandlerDelegate* delegate) |
| 247 : delegate_(delegate) { | 238 : delegate_(delegate) { |
| 248 RegisterCommandHandler(kTetheringBind, | 239 RegisterCommandHandler(devtools::Tethering::bind::kName, |
| 249 base::Bind(&TetheringHandler::OnBind, | 240 base::Bind(&TetheringHandler::OnBind, |
| 250 base::Unretained(this))); | 241 base::Unretained(this))); |
| 251 RegisterCommandHandler(kTetheringUnbind, | 242 RegisterCommandHandler(devtools::Tethering::unbind::kName, |
| 252 base::Bind(&TetheringHandler::OnUnbind, | 243 base::Bind(&TetheringHandler::OnUnbind, |
| 253 base::Unretained(this))); | 244 base::Unretained(this))); |
| 254 } | 245 } |
| 255 | 246 |
| 256 TetheringHandler::~TetheringHandler() { | 247 TetheringHandler::~TetheringHandler() { |
| 257 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), | 248 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), |
| 258 bound_sockets_.end()); | 249 bound_sockets_.end()); |
| 259 } | 250 } |
| 260 | 251 |
| 261 void TetheringHandler::Accepted(int port, const std::string& name) { | 252 void TetheringHandler::Accepted(int port, const std::string& name) { |
| 262 base::DictionaryValue* params = new base::DictionaryValue(); | 253 base::DictionaryValue* params = new base::DictionaryValue(); |
| 263 params->SetInteger(kPortParam, port); | 254 params->SetInteger(devtools::Tethering::accepted::kParamPort, port); |
| 264 params->SetString(kConnectionIdParam, name); | 255 params->SetString(devtools::Tethering::accepted::kParamConnectionId, name); |
| 265 SendNotification(kTetheringAccepted, params); | 256 SendNotification(devtools::Tethering::accepted::kName, params); |
| 266 } | 257 } |
| 267 | 258 |
| 268 static int GetPort(scoped_refptr<DevToolsProtocol::Command> command) { | 259 static int GetPort(scoped_refptr<DevToolsProtocol::Command> command, |
| 260 const std::string& paramName) { |
| 269 base::DictionaryValue* params = command->params(); | 261 base::DictionaryValue* params = command->params(); |
| 270 int port = 0; | 262 int port = 0; |
| 271 if (!params || !params->GetInteger(kPortParam, &port) || | 263 if (!params || |
| 264 !params->GetInteger(paramName, &port) || |
| 272 port < kMinTetheringPort || port > kMaxTetheringPort) | 265 port < kMinTetheringPort || port > kMaxTetheringPort) |
| 273 return 0; | 266 return 0; |
| 274 return port; | 267 return port; |
| 275 } | 268 } |
| 276 | 269 |
| 277 scoped_refptr<DevToolsProtocol::Response> | 270 scoped_refptr<DevToolsProtocol::Response> |
| 278 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { | 271 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { |
| 279 int port = GetPort(command); | 272 const std::string& portParamName = devtools::Tethering::bind::kParamPort; |
| 273 int port = GetPort(command, portParamName); |
| 280 if (port == 0) | 274 if (port == 0) |
| 281 return command->InvalidParamResponse(kPortParam); | 275 return command->InvalidParamResponse(portParamName); |
| 282 | 276 |
| 283 if (bound_sockets_.find(port) != bound_sockets_.end()) | 277 if (bound_sockets_.find(port) != bound_sockets_.end()) |
| 284 return command->InternalErrorResponse("Port already bound"); | 278 return command->InternalErrorResponse("Port already bound"); |
| 285 | 279 |
| 286 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(this, delegate_)); | 280 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(this, delegate_)); |
| 287 if (!bound_socket->Listen(port)) | 281 if (!bound_socket->Listen(port)) |
| 288 return command->InternalErrorResponse("Could not bind port"); | 282 return command->InternalErrorResponse("Could not bind port"); |
| 289 | 283 |
| 290 bound_sockets_[port] = bound_socket.release(); | 284 bound_sockets_[port] = bound_socket.release(); |
| 291 return command->SuccessResponse(NULL); | 285 return command->SuccessResponse(NULL); |
| 292 } | 286 } |
| 293 | 287 |
| 294 scoped_refptr<DevToolsProtocol::Response> | 288 scoped_refptr<DevToolsProtocol::Response> |
| 295 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { | 289 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { |
| 296 int port = GetPort(command); | 290 const std::string& portParamName = devtools::Tethering::unbind::kParamPort; |
| 291 int port = GetPort(command, portParamName); |
| 297 if (port == 0) | 292 if (port == 0) |
| 298 return command->InvalidParamResponse(kPortParam); | 293 return command->InvalidParamResponse(portParamName); |
| 299 | 294 |
| 300 BoundSockets::iterator it = bound_sockets_.find(port); | 295 BoundSockets::iterator it = bound_sockets_.find(port); |
| 301 if (it == bound_sockets_.end()) | 296 if (it == bound_sockets_.end()) |
| 302 return command->InternalErrorResponse("Port is not bound"); | 297 return command->InternalErrorResponse("Port is not bound"); |
| 303 | 298 |
| 304 delete it->second; | 299 delete it->second; |
| 305 bound_sockets_.erase(it); | 300 bound_sockets_.erase(it); |
| 306 return command->SuccessResponse(NULL); | 301 return command->SuccessResponse(NULL); |
| 307 } | 302 } |
| 308 | 303 |
| 309 } // namespace content | 304 } // namespace content |
| OLD | NEW |