Chromium Code Reviews| 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" |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 | 250 |
| 251 // TetheringHandler::TetheringImpl ------------------------------------------- | 251 // TetheringHandler::TetheringImpl ------------------------------------------- |
| 252 | 252 |
| 253 class TetheringHandler::TetheringImpl { | 253 class TetheringHandler::TetheringImpl { |
| 254 public: | 254 public: |
| 255 TetheringImpl( | 255 TetheringImpl( |
| 256 base::WeakPtr<TetheringHandler> handler, | 256 base::WeakPtr<TetheringHandler> handler, |
| 257 DevToolsHttpHandlerDelegate* delegate); | 257 DevToolsHttpHandlerDelegate* delegate); |
| 258 ~TetheringImpl(); | 258 ~TetheringImpl(); |
| 259 | 259 |
| 260 void Bind(scoped_refptr<DevToolsProtocol::Command> command); | 260 void Bind(scoped_refptr<DevToolsProtocol::Command> command, int port); |
| 261 void Unbind(scoped_refptr<DevToolsProtocol::Command> command); | 261 void Unbind(scoped_refptr<DevToolsProtocol::Command> command, int port); |
| 262 void Accepted(int port, const std::string& name); | 262 void Accepted(int port, const std::string& name); |
| 263 | 263 |
| 264 private: | 264 private: |
| 265 void SendAsyncResponse(scoped_refptr<DevToolsProtocol::Response> response); | 265 void SendBindSuccess(scoped_refptr<DevToolsProtocol::Command> command); |
| 266 void SendUnbindSuccess(scoped_refptr<DevToolsProtocol::Command> command); | |
| 267 void SendInternalError(scoped_refptr<DevToolsProtocol::Command> command, | |
| 268 const std::string& message); | |
| 266 | 269 |
| 267 base::WeakPtr<TetheringHandler> handler_; | 270 base::WeakPtr<TetheringHandler> handler_; |
| 268 DevToolsHttpHandlerDelegate* delegate_; | 271 DevToolsHttpHandlerDelegate* delegate_; |
| 269 | 272 |
| 270 typedef std::map<int, BoundSocket*> BoundSockets; | 273 typedef std::map<int, BoundSocket*> BoundSockets; |
| 271 BoundSockets bound_sockets_; | 274 BoundSockets bound_sockets_; |
| 272 }; | 275 }; |
| 273 | 276 |
| 274 TetheringHandler::TetheringImpl::TetheringImpl( | 277 TetheringHandler::TetheringImpl::TetheringImpl( |
| 275 base::WeakPtr<TetheringHandler> handler, | 278 base::WeakPtr<TetheringHandler> handler, |
| 276 DevToolsHttpHandlerDelegate* delegate) | 279 DevToolsHttpHandlerDelegate* delegate) |
| 277 : handler_(handler), | 280 : handler_(handler), |
| 278 delegate_(delegate) { | 281 delegate_(delegate) { |
| 279 } | 282 } |
| 280 | 283 |
| 281 TetheringHandler::TetheringImpl::~TetheringImpl() { | 284 TetheringHandler::TetheringImpl::~TetheringImpl() { |
| 282 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), | 285 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), |
| 283 bound_sockets_.end()); | 286 bound_sockets_.end()); |
| 284 } | 287 } |
| 285 | 288 |
| 286 void TetheringHandler::TetheringImpl::Bind( | 289 void TetheringHandler::TetheringImpl::Bind( |
| 287 scoped_refptr<DevToolsProtocol::Command> command) { | 290 scoped_refptr<DevToolsProtocol::Command> command, |
| 288 const std::string& portParamName = devtools::Tethering::bind::kParamPort; | 291 int port) { |
| 289 int port = GetPort(command, portParamName); | 292 if (bound_sockets_.find(port) != bound_sockets_.end()) { |
| 290 if (port == 0) { | 293 SendInternalError(command, "Port already bound"); |
| 291 SendAsyncResponse(command->InvalidParamResponse(portParamName)); | |
| 292 return; | 294 return; |
| 293 } | 295 } |
| 294 | 296 |
| 295 if (bound_sockets_.find(port) != bound_sockets_.end()) { | |
| 296 SendAsyncResponse(command->InternalErrorResponse("Port already bound")); | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 BoundSocket::AcceptedCallback callback = base::Bind( | 297 BoundSocket::AcceptedCallback callback = base::Bind( |
| 301 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); | 298 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); |
| 302 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); | 299 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); |
| 303 if (!bound_socket->Listen(port)) { | 300 if (!bound_socket->Listen(port)) { |
| 304 SendAsyncResponse(command->InternalErrorResponse("Could not bind port")); | 301 SendInternalError(command, "Could not bind port"); |
| 305 return; | 302 return; |
| 306 } | 303 } |
| 307 | 304 |
| 308 bound_sockets_[port] = bound_socket.release(); | 305 bound_sockets_[port] = bound_socket.release(); |
| 309 SendAsyncResponse(command->SuccessResponse(NULL)); | 306 SendBindSuccess(command); |
| 310 } | 307 } |
| 311 | 308 |
| 312 void TetheringHandler::TetheringImpl::Unbind( | 309 void TetheringHandler::TetheringImpl::Unbind( |
| 313 scoped_refptr<DevToolsProtocol::Command> command) { | 310 scoped_refptr<DevToolsProtocol::Command> command, |
| 314 const std::string& portParamName = devtools::Tethering::unbind::kParamPort; | 311 int port) { |
| 315 int port = GetPort(command, portParamName); | |
| 316 if (port == 0) { | |
| 317 SendAsyncResponse(command->InvalidParamResponse(portParamName)); | |
| 318 return; | |
| 319 } | |
| 320 | 312 |
| 321 BoundSockets::iterator it = bound_sockets_.find(port); | 313 BoundSockets::iterator it = bound_sockets_.find(port); |
| 322 if (it == bound_sockets_.end()) { | 314 if (it == bound_sockets_.end()) { |
| 323 SendAsyncResponse(command->InternalErrorResponse("Port is not bound")); | 315 SendInternalError(command, "Port is not bound"); |
| 324 return; | 316 return; |
| 325 } | 317 } |
| 326 | 318 |
| 327 delete it->second; | 319 delete it->second; |
| 328 bound_sockets_.erase(it); | 320 bound_sockets_.erase(it); |
| 329 SendAsyncResponse(command->SuccessResponse(NULL)); | 321 SendUnbindSuccess(command); |
| 330 } | 322 } |
| 331 | 323 |
| 332 void TetheringHandler::TetheringImpl::Accepted( | 324 void TetheringHandler::TetheringImpl::Accepted( |
| 333 int port, const std::string& name) { | 325 int port, const std::string& name) { |
| 334 BrowserThread::PostTask( | 326 BrowserThread::PostTask( |
| 335 BrowserThread::UI, | 327 BrowserThread::UI, |
| 336 FROM_HERE, | 328 FROM_HERE, |
| 337 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); | 329 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); |
| 338 } | 330 } |
| 339 | 331 |
| 340 void TetheringHandler::TetheringImpl::SendAsyncResponse( | 332 void TetheringHandler::TetheringImpl::SendBindSuccess( |
|
dgozman
2014/10/17 13:15:16
You can inline SendBindSuccess and SendUnbindSucce
vkuzkokov
2014/10/17 13:30:43
Done.
| |
| 341 scoped_refptr<DevToolsProtocol::Response> response) { | 333 scoped_refptr<DevToolsProtocol::Command> command) { |
| 342 BrowserThread::PostTask( | 334 BrowserThread::PostTask( |
| 343 BrowserThread::UI, | 335 BrowserThread::UI, |
| 344 FROM_HERE, | 336 FROM_HERE, |
| 345 base::Bind(&TetheringHandler::SendAsyncResponse, handler_, response)); | 337 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command)); |
| 346 } | 338 } |
| 347 | 339 |
| 340 void TetheringHandler::TetheringImpl::SendUnbindSuccess( | |
| 341 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 342 BrowserThread::PostTask( | |
| 343 BrowserThread::UI, | |
| 344 FROM_HERE, | |
| 345 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command)); | |
| 346 } | |
| 347 | |
| 348 void TetheringHandler::TetheringImpl::SendInternalError( | |
| 349 scoped_refptr<DevToolsProtocol::Command> command, | |
| 350 const std::string& message) { | |
| 351 BrowserThread::PostTask( | |
| 352 BrowserThread::UI, | |
| 353 FROM_HERE, | |
| 354 base::Bind(&TetheringHandler::SendInternalError, handler_, | |
| 355 command, message)); | |
| 356 } | |
| 357 | |
| 358 | |
| 348 // TetheringHandler ---------------------------------------------------------- | 359 // TetheringHandler ---------------------------------------------------------- |
| 349 | 360 |
| 350 // static | 361 // static |
| 351 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; | 362 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; |
| 352 | 363 |
| 353 TetheringHandler::TetheringHandler( | 364 TetheringHandler::TetheringHandler( |
| 354 DevToolsHttpHandlerDelegate* delegate, | 365 DevToolsHttpHandlerDelegate* delegate, |
| 355 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) | 366 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) |
| 356 : delegate_(delegate), | 367 : delegate_(delegate), |
| 357 message_loop_proxy_(message_loop_proxy), | 368 message_loop_proxy_(message_loop_proxy), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 return true; | 400 return true; |
| 390 } | 401 } |
| 391 | 402 |
| 392 scoped_refptr<DevToolsProtocol::Response> | 403 scoped_refptr<DevToolsProtocol::Response> |
| 393 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { | 404 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { |
| 394 if (!Activate()) { | 405 if (!Activate()) { |
| 395 return command->ServerErrorResponse( | 406 return command->ServerErrorResponse( |
| 396 "Tethering is used by another connection"); | 407 "Tethering is used by another connection"); |
| 397 } | 408 } |
| 398 DCHECK(impl_); | 409 DCHECK(impl_); |
| 410 const std::string& portParamName = devtools::Tethering::bind::kParamPort; | |
|
dgozman
2014/10/17 13:15:16
Perhaps, we can move this check before |Activate()
vkuzkokov
2014/10/17 13:30:43
Done.
| |
| 411 int port = GetPort(command, portParamName); | |
| 412 if (port == 0) | |
| 413 return command->InvalidParamResponse(portParamName); | |
| 414 | |
| 399 message_loop_proxy_->PostTask( | 415 message_loop_proxy_->PostTask( |
| 400 FROM_HERE, | 416 FROM_HERE, |
| 401 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), command)); | 417 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), |
| 418 command, port)); | |
| 402 return command->AsyncResponsePromise(); | 419 return command->AsyncResponsePromise(); |
| 403 } | 420 } |
| 404 | 421 |
| 405 scoped_refptr<DevToolsProtocol::Response> | 422 scoped_refptr<DevToolsProtocol::Response> |
| 406 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { | 423 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { |
| 407 if (!Activate()) { | 424 if (!Activate()) { |
| 408 return command->ServerErrorResponse( | 425 return command->ServerErrorResponse( |
| 409 "Tethering is used by another connection"); | 426 "Tethering is used by another connection"); |
| 410 } | 427 } |
| 411 DCHECK(impl_); | 428 DCHECK(impl_); |
| 429 const std::string& portParamName = devtools::Tethering::unbind::kParamPort; | |
|
dgozman
2014/10/17 13:15:16
ditto
vkuzkokov
2014/10/17 13:30:43
Done.
| |
| 430 int port = GetPort(command, portParamName); | |
| 431 if (port == 0) | |
| 432 return command->InvalidParamResponse(portParamName); | |
| 433 | |
| 412 message_loop_proxy_->PostTask( | 434 message_loop_proxy_->PostTask( |
| 413 FROM_HERE, | 435 FROM_HERE, |
| 414 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), command)); | 436 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), |
| 437 command, port)); | |
| 415 return command->AsyncResponsePromise(); | 438 return command->AsyncResponsePromise(); |
| 416 } | 439 } |
| 417 | 440 |
| 441 void TetheringHandler::SendBindSuccess( | |
|
dgozman
2014/10/17 13:15:16
What's the point of two equal methods?
vkuzkokov
2014/10/17 13:30:43
They will be calling different methods of devtools
dgozman
2014/10/17 13:34:24
Oh, didn't think about that.
| |
| 442 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 443 SendAsyncResponse(command->SuccessResponse(nullptr)); | |
| 444 } | |
| 445 | |
| 446 void TetheringHandler::SendUnbindSuccess( | |
| 447 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 448 SendAsyncResponse(command->SuccessResponse(nullptr)); | |
| 449 } | |
| 450 | |
| 451 void TetheringHandler::SendInternalError( | |
| 452 scoped_refptr<DevToolsProtocol::Command> command, | |
| 453 const std::string& message) { | |
| 454 SendAsyncResponse(command->InternalErrorResponse(message)); | |
| 455 } | |
| 456 | |
| 418 } // namespace content | 457 } // namespace content |
| OLD | NEW |